Added test for validating composition lifecycle callbaks, using annotation

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@958128 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/composite/Component.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/composite/Component.java
new file mode 100644
index 0000000..1e4145c
--- /dev/null
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/composite/Component.java
@@ -0,0 +1,51 @@
+/*
+ * 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.composite;
+
+import org.apache.felix.dm.test.bundle.annotation.sequencer.Sequencer;
+
+/**
+ * The CompositeService is also made up of this Class.
+ */
+public class Component
+{
+    // Injected dependency (from CompositeService)
+    private Sequencer m_sequencer;
+
+    // Injected dependency (from CompositeService)
+    Runnable m_runnable;
+
+    // lifecycle callback (same method as the one from CompositeService)
+    void init()
+    {
+        m_sequencer.step(2);
+    }
+
+    // lifecycle callback (same method as the one from CompositeService)
+    void start()
+    {
+        m_sequencer.step(5);
+        m_runnable.run(); /* step 6 */
+    }
+
+    // lifecycle callback (same method as the one from CompositeService)
+    void stop()
+    {
+        m_sequencer.step(8);
+    }
+
+    // lifecycle callback (same method as the one from CompositeService)
+    void destroy()
+    {
+        m_sequencer.step(10);
+    }
+}
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/composite/CompositeService.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/composite/CompositeService.java
new file mode 100644
index 0000000..c8e6685
--- /dev/null
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/composite/CompositeService.java
@@ -0,0 +1,99 @@
+/*
+ * 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.composite;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.felix.dm.annotation.api.Composition;
+import org.apache.felix.dm.annotation.api.Destroy;
+import org.apache.felix.dm.annotation.api.Init;
+import org.apache.felix.dm.annotation.api.Service;
+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;
+
+/**
+ * This service is also composed of the Component object.
+ */
+@Service
+public class CompositeService
+{
+    /* We are composed of this object, which will also be injected with our dependencies */
+    private Component m_component = new Component();
+
+    /* This dependency filter will be configured from our init method */
+    @ServiceDependency(name = "D")
+    private Runnable m_runnable;
+
+    /* Object used to check that methods are called in the proper sequence */
+    @ServiceDependency
+    private Sequencer m_sequencer;
+
+    /**
+     *  Dynamically configure our "D" dependency, using a dependency cusomization map 
+     */
+    @Init
+    Map<String, String> init()
+    {
+        m_sequencer.step(1);
+        // Configure a filter for our dependency whose name is "D"
+        Map<String, String> customization = new HashMap<String, String>()
+        {
+            {
+                put("D.filter", "(foo=bar2)");
+            }
+        };
+        return customization;
+    }
+
+    /**
+     * Return the list of object our service is composed of
+     */
+    @Composition
+    Object[] getComposition()
+    {
+        return new Object[] { this, m_component };
+    }
+
+    /** 
+     * Our Service is starting, and our Composites will also be 
+     */
+    @Start
+    void start()
+    {
+        m_sequencer.step(3);
+        m_runnable.run(); /* step 4 */
+        // Our Component.start() method should be called once this method returns.
+    }
+
+    /**
+     *  Our Service is stopping, and our Composites will also be 
+     */
+    @Stop
+    void stop()
+    {
+        m_sequencer.step(7);
+        // Our Component.stop() method should be called once this method returns.
+    }
+
+    /**
+     * Our Service is destroying, and our Composites will also be.
+     */
+    @Destroy
+    void destroy()
+    {
+        m_sequencer.step(9);
+        // Our Component.destroy() method should be called once this method returns.
+    }
+}
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/composite/Dependency1.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/composite/Dependency1.java
new file mode 100644
index 0000000..138f46a
--- /dev/null
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/composite/Dependency1.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.dm.test.bundle.annotation.composite;
+
+import org.apache.felix.dm.annotation.api.Property;
+import org.apache.felix.dm.annotation.api.Service;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.apache.felix.dm.test.bundle.annotation.sequencer.Sequencer;
+
+@Service(properties = @Property(name = "foo", value = "bar1"))
+public class Dependency1 implements Runnable
+{
+    @ServiceDependency
+    Sequencer m_sequencer;
+
+    public void run()
+    {
+        m_sequencer.step(Integer.MAX_VALUE); // Makes the test fail.
+    }
+}
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/composite/Dependency2.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/composite/Dependency2.java
new file mode 100644
index 0000000..62855f0
--- /dev/null
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/composite/Dependency2.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.dm.test.bundle.annotation.composite;
+
+import org.apache.felix.dm.annotation.api.Property;
+import org.apache.felix.dm.annotation.api.Service;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.apache.felix.dm.test.bundle.annotation.sequencer.Sequencer;
+
+@Service(properties = @Property(name = "foo", value = "bar2"))
+public class Dependency2 implements Runnable
+{
+    @ServiceDependency
+    Sequencer m_sequencer;
+
+    public void run()
+    {
+        m_sequencer.step();
+    }
+}
diff --git a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/CompositeAnnotationsTest.java b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/CompositeAnnotationsTest.java
new file mode 100644
index 0000000..deb3cbd
--- /dev/null
+++ b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/CompositeAnnotationsTest.java
@@ -0,0 +1,75 @@
+/*
+* 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.DependencyManager;
+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;
+
+/**
+ * Use case: Verify Composite annotated services.
+ */
+@RunWith(JUnit4TestRunner.class)
+public class CompositeAnnotationsTest extends AnnotationBase
+{
+    @Configuration
+    public static Option[] configuration()
+    {
+        return options(
+            systemProperty("dm.log").value( "true" ),
+            provision(
+                mavenBundle().groupId("org.osgi").artifactId("org.osgi.compendium").version("4.1.0"),
+                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, "CompositeAnnotationsTest")
+                    .set("Export-Package", "org.apache.felix.dm.test.bundle.annotation.sequencer")
+                    .set("Private-Package", "org.apache.felix.dm.test.bundle.annotation.composite")
+                    .set("Import-Package", "*")
+                    .set("-plugin", "org.apache.felix.dm.annotation.plugin.bnd.AnnotationPlugin")
+                    .build()));            
+    }
+
+    @Test
+    public void testComposite(BundleContext context)
+    {
+        DependencyManager m = new DependencyManager(context);
+        // Provide the Sequencer service to the "Component" service.
+        m.add(m.createService() .setImplementation(this).setInterface(Sequencer.class.getName(), null));
+        // Check if the components have been initialized orderly
+        m_ensure.waitForStep(4, 10000);
+        // Stop the bundle
+        stopBundle("CompositeAnnotationsTest", context);
+        // And check if the components lifecycle callbacks are called orderly
+        m_ensure.waitForStep(10, 10000);
+    }
+}