Add a test about FELIX-4247 Memory leak with ServiceUsage and inner class (Listener style) to confirm the fix.


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1528324 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/components/leak/DefaultHelloService.java b/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/components/leak/DefaultHelloService.java
new file mode 100644
index 0000000..1267dc1
--- /dev/null
+++ b/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/components/leak/DefaultHelloService.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.felix.ipojo.runtime.core.test.components.leak;
+
+import org.apache.felix.ipojo.annotations.Component;
+import org.apache.felix.ipojo.annotations.Instantiate;
+import org.apache.felix.ipojo.annotations.Provides;
+import org.apache.felix.ipojo.runtime.core.test.services.HelloService;
+
+/**
+ * A implementation of the Hello Service
+ * (FELIX-4247 Memory leak with ServiceUsage and inner class (Listener style))
+ */
+@Component
+@Provides
+public class DefaultHelloService implements HelloService {
+    @Override
+    public String hello(final String name) {
+        System.out.println("Hello "+ name);
+        return "Hello " + name;
+    }
+}
diff --git a/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/components/leak/DefaultLeakingService.java b/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/components/leak/DefaultLeakingService.java
new file mode 100644
index 0000000..b8b735a
--- /dev/null
+++ b/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/components/leak/DefaultLeakingService.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.felix.ipojo.runtime.core.test.components.leak;
+
+import org.apache.felix.ipojo.annotations.Component;
+import org.apache.felix.ipojo.annotations.Instantiate;
+import org.apache.felix.ipojo.annotations.Provides;
+import org.apache.felix.ipojo.annotations.Requires;
+import org.apache.felix.ipojo.runtime.core.test.services.HelloService;
+import org.apache.felix.ipojo.runtime.core.test.services.LeakingService;
+import org.apache.felix.ipojo.runtime.core.test.services.Listener;
+
+/**
+ * A component reproducing the leak (FELIX-4247 Memory leak with ServiceUsage and inner class (Listener style)).
+ */
+@Component
+@Provides
+@Instantiate
+public class DefaultLeakingService implements LeakingService {
+
+    @Requires(optional = true)
+    HelloService service;
+
+    private Listener listener;
+
+    public DefaultLeakingService() {
+        listener = new Listener() {
+            // When the Hello Service will become unavailable, calling doSomething should use a nullable object.
+            // Therefore, it should return 'null'.
+            @Override
+            public String doSomething() {
+                return service.hello("iPOJO");
+            }
+        };
+    }
+
+    @Override
+    public Listener getListener() {
+        return listener;
+    }
+
+    @Override
+    public String executeListener() {
+        return listener.doSomething();
+    }
+}
diff --git a/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/services/HelloService.java b/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/services/HelloService.java
new file mode 100644
index 0000000..420be52
--- /dev/null
+++ b/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/services/HelloService.java
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.felix.ipojo.runtime.core.test.services;
+
+/**
+ * A service used to reproduce the leak bug (FELIX-4247 Memory leak with ServiceUsage and inner class (Listener
+ * style)).
+ */
+public interface HelloService {
+    String hello(String name);
+}
diff --git a/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/services/LeakingService.java b/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/services/LeakingService.java
new file mode 100644
index 0000000..5f07aad
--- /dev/null
+++ b/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/services/LeakingService.java
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.felix.ipojo.runtime.core.test.services;
+
+/**
+ * A service providing a listener.
+ * USed to reproduce the leak bug (FELIX-4247 Memory leak with ServiceUsage and inner class (Listener style))
+ */
+public interface LeakingService {
+    Listener getListener();
+    String executeListener();
+}
diff --git a/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/services/Listener.java b/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/services/Listener.java
new file mode 100644
index 0000000..f39a82f
--- /dev/null
+++ b/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/main/java/org/apache/felix/ipojo/runtime/core/test/services/Listener.java
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.felix.ipojo.runtime.core.test.services;
+
+/**
+ * A listener to reproduce the listener leak (FELIX-4247 Memory leak with ServiceUsage and inner class (Listener
+ * style))
+ */
+public interface Listener {
+    String doSomething();
+}
diff --git a/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/dependencies/leak/ListenerStyleLeakTest.java b/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/dependencies/leak/ListenerStyleLeakTest.java
new file mode 100644
index 0000000..fc81b60
--- /dev/null
+++ b/ipojo/runtime/core-it/ipojo-core-service-dependency-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/dependencies/leak/ListenerStyleLeakTest.java
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.felix.ipojo.runtime.core.test.dependencies.leak;
+
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.runtime.core.test.dependencies.Common;
+import org.apache.felix.ipojo.runtime.core.test.services.HelloService;
+import org.apache.felix.ipojo.runtime.core.test.services.LeakingService;
+import org.apache.felix.ipojo.runtime.core.test.services.Listener;
+import org.junit.Test;
+import org.osgi.framework.ServiceReference;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+/**
+ * Check the fix of the leak FELIX-4247 Memory leak with ServiceUsage and inner class (Listener style).
+ */
+public class ListenerStyleLeakTest extends Common {
+
+    public static final String DEFAULT_HELLO_SERVICE = "org.apache.felix.ipojo.runtime.core.test.components.leak" +
+            ".DefaultHelloService";
+    public static final String DEFAULT_LEAKING_SERVICE = "org.apache.felix.ipojo.runtime.core.test.components.leak" +
+            ".DefaultLeakingService";
+
+    /**
+     * This test does not use the listener-style.
+     * <ol>
+     *     <li>Gets the hello message from the leaking instance using the hello instance</li>
+     *     <li>Stops the hello instance</li>
+     *     <li>Get the hello message again, must be {@code null}</li>
+     * </ol>
+     *
+     */
+    @Test
+    public void testNormalStyle() {
+
+        ComponentInstance hello = ipojoHelper.createComponentInstance(DEFAULT_HELLO_SERVICE);
+        ComponentInstance leaking = ipojoHelper.createComponentInstance(DEFAULT_LEAKING_SERVICE);
+
+        assertThat(hello.getState()).isEqualTo(ComponentInstance.VALID);
+        assertThat(leaking.getState()).isEqualTo(ComponentInstance.VALID);
+
+        LeakingService service = osgiHelper.waitForService(LeakingService.class,
+                "(instance.name=" + leaking.getInstanceName() + ")", 1000);
+        osgiHelper.waitForService(HelloService.class, "(instance.name=" + hello.getInstanceName() + ")", 1000);
+
+        String result = service.executeListener();
+        assertThat(result).isEqualToIgnoringCase("hello iPOJO");
+
+        hello.stop();
+
+        result = service.executeListener();
+        assertThat(result).isNull();
+    }
+
+    /**
+     * This test do use the listener-style.
+     * <ol>
+     *     <li>Gets the listener object provided by the leaking instance</li>
+     *     <li>Gets the hello message from this listener. As the hello service is bound,
+     *     it relies on the hello instance</li>
+     *     <li>Stops the hello instance</li>
+     *     <li>Get the hello message again, must be {@code null}</li>
+     * </ol>
+     *
+     */
+    @Test
+    public void testListenerStyle() {
+
+        ComponentInstance hello = ipojoHelper.createComponentInstance(DEFAULT_HELLO_SERVICE);
+        ComponentInstance leaking = ipojoHelper.createComponentInstance(DEFAULT_LEAKING_SERVICE);
+
+        assertThat(hello.getState()).isEqualTo(ComponentInstance.VALID);
+        assertThat(leaking.getState()).isEqualTo(ComponentInstance.VALID);
+
+        LeakingService service = osgiHelper.waitForService(LeakingService.class,
+                "(instance.name=" + leaking.getInstanceName() + ")", 1000);
+        osgiHelper.waitForService(HelloService.class, "(instance.name=" + hello.getInstanceName() + ")", 1000);
+
+        Listener listener = service.getListener();
+        String result = listener.doSomething();
+        assertThat(result).isEqualToIgnoringCase("hello iPOJO");
+
+        hello.stop();
+
+        result = listener.doSomething();
+        assertThat(result).isNull();
+    }
+}
diff --git a/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java b/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java
index b23ea03..6037f11 100644
--- a/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java
+++ b/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/DependencyHandler.java
@@ -278,7 +278,6 @@
                 if (meths != null) {
                     for (MethodMetadata method : meths) {
                         for (Dependency dep : m_dependencies) {
-                            System.out.println("Registering " + method.getMethodName());
                             getInstanceManager().register(method, inner, dep);
                         }
                     }