Added test case for FELIX-4050 issue.


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1486129 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/felix4050/A.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/felix4050/A.java
new file mode 100644
index 0000000..5880268
--- /dev/null
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/felix4050/A.java
@@ -0,0 +1,26 @@
+/*
+ * 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.dm.test.bundle.annotation.felix4050;
+
+import org.apache.felix.dm.annotation.api.Component;
+
+@Component(provides = { A.class })
+public class A {
+
+}
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/felix4050/B.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/felix4050/B.java
new file mode 100644
index 0000000..5b68a96
--- /dev/null
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/felix4050/B.java
@@ -0,0 +1,23 @@
+/*
+ * 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.dm.test.bundle.annotation.felix4050;
+
+public interface B {
+    void run();
+}
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/felix4050/B1.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/felix4050/B1.java
new file mode 100644
index 0000000..38f420c
--- /dev/null
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/felix4050/B1.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.dm.test.bundle.annotation.felix4050;
+
+import org.apache.felix.dm.annotation.api.Component;
+import org.apache.felix.dm.annotation.api.Property;
+
+@Component(properties = { @Property(name = "type", value = "b1") })
+public class B1 implements B {
+    public void run() {
+    }
+}
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/felix4050/B2.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/felix4050/B2.java
new file mode 100644
index 0000000..b36c821
--- /dev/null
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/felix4050/B2.java
@@ -0,0 +1,66 @@
+/*
+ * 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.dm.test.bundle.annotation.felix4050;
+
+import java.util.Hashtable;
+
+import org.apache.felix.dm.annotation.api.Component;
+import org.apache.felix.dm.annotation.api.Inject;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.apache.felix.dm.annotation.api.Start;
+import org.apache.felix.dm.test.bundle.annotation.sequencer.Sequencer;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+@Component(provides = {})
+public class B2 implements B {
+    @Inject
+    BundleContext _ctx;
+    
+    @ServiceDependency
+    Sequencer m_sequencer;
+
+    @Start
+    void start() {
+        Thread t = new Thread(new Runnable() {
+            public void run() {
+                try {
+                    Thread.sleep(2000);
+                } catch (InterruptedException e) {
+                }
+                System.out.println("Registering B2");
+                ServiceRegistration sr = _ctx.registerService(B.class.getName(), B2.this, new Hashtable() {
+                    {
+                        put("type", "b2");
+                    }
+                });
+
+                try {
+                    Thread.sleep(1000);
+                } catch (InterruptedException e) {
+                }
+            }
+        });
+        t.start();
+    }
+    
+    public void run() {
+        m_sequencer.step(3);
+    }
+}
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/felix4050/S.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/felix4050/S.java
new file mode 100644
index 0000000..2cec3f9
--- /dev/null
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/felix4050/S.java
@@ -0,0 +1,95 @@
+/*
+ * 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.dm.test.bundle.annotation.felix4050;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.felix.dm.annotation.api.Component;
+import org.apache.felix.dm.annotation.api.Destroy;
+import org.apache.felix.dm.annotation.api.Init;
+import org.apache.felix.dm.annotation.api.Inject;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.apache.felix.dm.annotation.api.Start;
+import org.apache.felix.dm.annotation.api.Stop;
+import org.apache.felix.dm.test.bundle.annotation.sequencer.Sequencer;
+
+@Component
+public class S {
+    @ServiceDependency
+    volatile Sequencer m_sequencer;
+
+    @Inject
+    volatile org.apache.felix.dm.Component _component;
+
+    volatile A m_a;
+    volatile B m_b;
+
+    void bind(A a) {
+        System.out.println("bind(A): " + a);
+        m_a = a;
+    }
+
+    @ServiceDependency(name = "B")
+    void bind(B b) {
+        System.out.println("bind(B): " + b);
+        m_b = b;
+    }
+
+    @Init
+    Map init() {
+        m_sequencer.step(1);
+
+        List l = new ArrayList();
+        l.add(_component.getDependencyManager().createServiceDependency().setService(A.class)
+                .setRequired(true).setCallbacks("bind", null).setInstanceBound(true));
+        _component.add(l);
+
+        return new HashMap() {
+            {
+                put("B.required", "true");
+                put("B.filter", "(type=b2)");
+            }
+        };
+    }
+
+    @Start
+    void start() {
+        if (m_a == null) {
+            throw new RuntimeException("A not injected");
+        }
+        if (m_b == null) {
+            throw new RuntimeException("B not injected");
+        }
+        m_sequencer.step(2);
+        m_b.run(); // step(3)
+    }
+
+    @Stop
+    void stop() {
+        m_sequencer.step(4);
+    }
+
+    @Destroy
+    void destroy() {
+        m_sequencer.step(5);
+    }
+}
diff --git a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/Felix4050Test.java b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/Felix4050Test.java
new file mode 100644
index 0000000..28d9fcf
--- /dev/null
+++ b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/Felix4050Test.java
@@ -0,0 +1,80 @@
+/*
+ * 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.dm.test.annotation;
+
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.CoreOptions.options;
+import static org.ops4j.pax.exam.CoreOptions.provision;
+import static org.ops4j.pax.exam.CoreOptions.systemProperty;
+
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
+import org.apache.felix.dm.test.Base;
+import org.apache.felix.dm.test.BundleGenerator;
+import org.apache.felix.dm.test.bundle.annotation.sequencer.Sequencer;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.Configuration;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+
+/**
+ * Test for FELIX-4050 issue: It validates that component state calculation does not mess up
+ * when an @Init method adds an available dependency using the API, and also returns a Map for
+ * configuring a named dependency.
+ */
+@RunWith(JUnit4TestRunner.class)
+public class Felix4050Test extends AnnotationBase
+{
+    @Configuration
+    public static Option[] configuration()
+    {
+        return options(
+            systemProperty(DMLOG_PROPERTY).value( "true" ),
+            provision(
+                mavenBundle().groupId("org.osgi").artifactId("org.osgi.compendium").version(Base.OSGI_SPEC_VERSION),
+                mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.dependencymanager").versionAsInProject(),
+                mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.dependencymanager.runtime").versionAsInProject()),
+            provision(
+                new BundleGenerator()
+                    .set(Constants.BUNDLE_SYMBOLICNAME, "AspectLifecycleTest")
+                    .set("Export-Package", "org.apache.felix.dm.test.bundle.annotation.sequencer")
+                    .set("Private-Package", "org.apache.felix.dm.test.bundle.annotation.felix4050")
+                    .set("Import-Package", "*")
+                    .set("-plugin", "org.apache.felix.dm.annotation.plugin.bnd.AnnotationPlugin")
+                    .build()));         
+    }
+
+    @Test
+    public void testFelix4050(BundleContext context)
+    {
+        DependencyManager m = new DependencyManager(context);
+        // provide a sequencer to the test classes
+        Component seq = m.createComponent().setImplementation(this).setInterface(Sequencer.class.getName(), null);
+        m.add(seq);
+        // wait for S to be started
+        m_ensure.waitForStep(3, 10000);
+        // remove our sequencer: this will stop S
+        m.remove(seq);
+        // ensure that S is stopped and destroyed
+        m_ensure.waitForStep(5, 10000);
+    }
+}