reworked and cleaned this test

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1461513 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/ExtraDependency1.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/ExtraDependency1.java
index cd41043..b4720cb 100644
--- a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/ExtraDependency1.java
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/ExtraDependency1.java
@@ -21,10 +21,8 @@
 import org.apache.felix.dm.annotation.api.Component;
 import org.apache.felix.dm.annotation.api.Property;
 
-@Component(properties=@Property(name="foo", value="bar"))
-public class ExtraDependency1 implements Runnable
-{
-    public void run()
-    {
+@Component(properties = @Property(name = "foo", value = "bar"))
+public class ExtraDependency1 implements Runnable {
+    public void run() {
     }
 }
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/ExtraDependency2.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/ExtraDependency2.java
index 164a01c..1dcdf31 100644
--- a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/ExtraDependency2.java
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/ExtraDependency2.java
@@ -21,11 +21,9 @@
 import org.apache.felix.dm.annotation.api.Component;
 import org.apache.felix.dm.annotation.api.Property;
 
-@Component(properties=@Property(name="foo", value="bar2"))
-public class ExtraDependency2 implements Runnable
-{
-    public void run()
-    {
+@Component(properties = @Property(name = "foo", value = "bar2"))
+public class ExtraDependency2 implements Runnable {
+    public void run() {
         System.out.println("ExtraDependency2.run()");
     }
 }
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/MyService.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/MyService.java
index b5604a6..7f42ffa 100644
--- a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/MyService.java
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/MyService.java
@@ -33,64 +33,64 @@
 import org.apache.felix.dm.test.bundle.annotation.sequencer.Sequencer;
 
 /**
- * This service will be instantiated by our MyServiceFactory class.
+ * This service is instantaited using a "factory set" from the
+ * ServiceFactoryAnnotationTest class.
+ * 
+ * @see org.apache.felix.dm.test.annotation.ServiceFactoryAnnotationTest
  */
-@Component(factorySet = "MyServiceFactory", 
-           factoryConfigure = "configure",
-           properties = { @Property(name = "foo", value = "bar") })
-public class MyService implements MyServiceInterface
-{
+@Component(factorySet = "MyServiceFactory", factoryConfigure = "configure", properties = { @Property(name = "foo", value = "bar") })
+public class MyService implements MyServiceInterface {
     /**
-     *  The configuration provided by MyServiceFactory
+     * The configuration provided by MyServiceFactory
      */
     @SuppressWarnings("unchecked")
     Dictionary m_configuration;
 
     /**
-     *  Our sequencer.
+     * Our sequencer.
      */
     @ServiceDependency
     Sequencer m_sequencer;
-    
+
     /**
-     *  An extra dependency (we'll dynamically configure the filter from our init() method).
+     * An extra dependency (we'll dynamically configure the filter from our
+     * init() method).
      */
-    @ServiceDependency(name="extra")
+    @ServiceDependency(name = "extra")
     Runnable m_extra;
 
     /**
-     * This is the first method called: we are provided with the MyServiceFactory configuration.
+     * This is the first method called: we are provided with the
+     * MyServiceFactory configuration.
      */
-    public void configure(Dictionary<?, ?> configuration)
-    {
-        if (m_configuration == null)
-        {
+    public void configure(Dictionary<?, ?> configuration) {
+        if (m_configuration == null) {
             m_configuration = configuration;
-        }
-        else
-        {
+        } else {
             m_sequencer.step(5);
         }
     }
 
     /**
-     * Initialize our Service: we'll dynamically configure our dependency whose name is "extra".
+     * Initialize our Service: we'll dynamically configure our dependency whose
+     * name is "extra".
      */
     @Init
-    Map init() 
-    {
-        return new HashMap() {{
-            put("extra.filter", "(foo=bar2)");
-            put("extra.required", "true");
-        }};
+    Map init() {
+        return new HashMap() {
+            {
+                put("extra.filter", "(foo=bar2)");
+                put("extra.required", "true");
+            }
+        };
     }
-        
+
     /**
-     * our Service is starting: at this point, all required dependencies have been injected.
+     * our Service is starting: at this point, all required dependencies have
+     * been injected.
      */
     @Start
-    public void start()
-    {
+    public void start() {
         Assert.assertNotNull("Extra dependency not injected", m_extra);
         m_extra.run();
         m_sequencer.step(2);
@@ -100,29 +100,23 @@
      * Our service is stopping.
      */
     @Stop
-    public void stop()
-    {
+    public void stop() {
         m_sequencer.step(10);
     }
 
-    public void added(String instanceId)
-    {
-        if (instanceId.equals(m_configuration.get("instance.id")))
-        {
+    public void added(String instanceId) {
+        if (instanceId.equals(m_configuration.get("instance.id"))) {
             m_sequencer.step(4);
         }
     }
 
-    public void changed(String modified)
-    {
-        if (modified.equals(m_configuration.get("instance.modified")))
-        {
+    public void changed(String modified) {
+        if (modified.equals(m_configuration.get("instance.modified"))) {
             m_sequencer.step(7);
         }
     }
 
-    public void removed()
-    {
+    public void removed() {
         m_sequencer.step(9);
     }
 }
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/MyServiceFactory.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/MyServiceClient.java
similarity index 64%
rename from dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/MyServiceFactory.java
rename to dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/MyServiceClient.java
index 71c1ef9..3727feb 100644
--- a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/MyServiceFactory.java
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/MyServiceClient.java
@@ -18,10 +18,7 @@
  */
 package org.apache.felix.dm.test.bundle.annotation.factory;
 
-import java.util.Dictionary;
-import java.util.Hashtable;
 import java.util.Map;
-import java.util.Set;
 
 import junit.framework.Assert;
 
@@ -30,54 +27,30 @@
 import org.apache.felix.dm.annotation.api.Start;
 import org.apache.felix.dm.test.bundle.annotation.sequencer.Sequencer;
 
-@SuppressWarnings("unchecked")
 @Component
-public class MyServiceFactory
-{
-    @ServiceDependency(filter = "(" + Component.FACTORY_NAME + "=MyServiceFactory)")
-    Set<Dictionary> m_myServiceFactory;
-    
+public class MyServiceClient {
     @ServiceDependency
-    Sequencer m_sequencer;    
-    
-    Hashtable m_conf;
+    Sequencer m_sequencer;
 
     @Start
-    void start()
-    {
+    void start() {
         m_sequencer.step(1);
-        // create a service instance with this configuration
-        m_conf = new Hashtable();
-        m_conf.put("instance.id", "instance");
-        m_conf.put(".private.param", "private");
-        m_conf.put(Component.FACTORY_INSTANCE, new MyService()); // we explicitly provide the instance
-        Assert.assertTrue(m_myServiceFactory.add(m_conf));
     }
 
     @ServiceDependency(required = false, changed = "update", removed = "removed")
-    void bind(Map serviceProperties, MyServiceInterface service)
-    {
+    void bind(Map serviceProperties, MyServiceInterface service) {
         m_sequencer.step(3);
         Assert.assertEquals("bar", serviceProperties.get("foo"));
         Assert.assertNull(serviceProperties.get(".private.param"));
         service.added((String) serviceProperties.get("instance.id"));
-
-        // update the service instance
-        m_conf.put("instance.modified", "true");
-        Assert.assertFalse(m_myServiceFactory.add(m_conf));
     }
 
-    void update(Map serviceProperties, MyServiceInterface service)
-    {
+    void update(Map serviceProperties, MyServiceInterface service) {
         m_sequencer.step(6);
         service.changed((String) serviceProperties.get("instance.modified"));
-
-        // remove instance
-        Assert.assertTrue(m_myServiceFactory.remove(m_conf));
     }
 
-    void removed(MyServiceInterface service)
-    {
+    void removed(MyServiceInterface service) {
         m_sequencer.step(8);
         service.removed();
     }
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/MyServiceInterface.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/MyServiceInterface.java
index 75a051e..5eb9859 100644
--- a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/MyServiceInterface.java
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factory/MyServiceInterface.java
@@ -18,9 +18,10 @@
  */
 package org.apache.felix.dm.test.bundle.annotation.factory;
 
-public interface MyServiceInterface
-{
+public interface MyServiceInterface {
     public void added(String instanceId);
+
     public void changed(String modified);
+
     public void removed();
 }
diff --git a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/ServiceFactoryAnnotationTest.java b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/ServiceFactoryAnnotationTest.java
index 9136384..a10bb2b 100644
--- a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/ServiceFactoryAnnotationTest.java
+++ b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/ServiceFactoryAnnotationTest.java
@@ -1,21 +1,21 @@
 /*
-* 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.
-*/
+ * 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;
@@ -23,9 +23,19 @@
 import static org.ops4j.pax.exam.CoreOptions.provision;
 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
 
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.Set;
+
+import junit.framework.Assert;
+
 import org.apache.felix.dm.DependencyManager;
+import org.apache.felix.dm.annotation.api.Component;
 import org.apache.felix.dm.test.Base;
 import org.apache.felix.dm.test.BundleGenerator;
+import org.apache.felix.dm.test.bundle.annotation.factory.MyService;
+import org.apache.felix.dm.test.bundle.annotation.factory.MyServiceInterface;
 import org.apache.felix.dm.test.bundle.annotation.sequencer.Sequencer;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -36,34 +46,60 @@
 import org.osgi.framework.Constants;
 
 @RunWith(JUnit4TestRunner.class)
-public class ServiceFactoryAnnotationTest extends AnnotationBase
-{
+public class ServiceFactoryAnnotationTest extends AnnotationBase {
     @Configuration
-    public static Option[] 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, "ServiceFactoryAnnotationTest")
-                .set("Export-Package", "org.apache.felix.dm.test.bundle.annotation.sequencer")
-                .set("Private-Package", "org.apache.felix.dm.test.bundle.annotation.factory")
-                .set("Import-Package", "*")
-                .set("-plugin", "org.apache.felix.dm.annotation.plugin.bnd.AnnotationPlugin")
-                .build()));
-        }
+                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, "ServiceFactoryAnnotationTest")
+                        .set("Export-Package", "org.apache.felix.dm.test.bundle.annotation.sequencer")
+                        .set("Private-Package", "org.apache.felix.dm.test.bundle.annotation.factory")
+                        .set("Import-Package", "*")
+                        .set("-plugin", "org.apache.felix.dm.annotation.plugin.bnd.AnnotationPlugin").build()));
+    }
 
-        @Test
-        public void testServiceFactory(BundleContext context)
-        {
-            DependencyManager m = new DependencyManager(context);
-            // We provide ourself as a "Sequencer" service to the annotated bundles. 
-            m.add(m.createComponent().setImplementation(this).setInterface(Sequencer.class.getName(), null));
-            // Check if the test.annotation components have been initialized orderly
-            m_ensure.waitForStep(10, 10000);
-        }        
-    }
\ No newline at end of file
+    @Test
+    public void testServiceFactory(BundleContext context) {
+        DependencyManager m = new DependencyManager(context);
+
+        // We provide ourself as a "Sequencer" service to the annotated bundles.
+        m.add(m.createComponent().setImplementation(ServiceFactoryAnnotationTest.this)
+                .setInterface(Sequencer.class.getName(), null));
+
+        // Wait for the factory.
+        m.add(m.createComponent()
+                .setImplementation(this)
+                .add(m.createServiceDependency()
+                        .setService(Set.class, "(" + Component.FACTORY_NAME + "=MyServiceFactory)")
+                        .setRequired(true).setCallbacks("bindFactory", null)));
+
+        // Check if the test.annotation components have been initialized orderly
+        m_ensure.waitForStep(10, 5000);
+    }
+
+    void bindFactory(Set factory) {
+        // create a service instance with this configuration
+        Hashtable conf = new Hashtable();
+        conf.put("instance.id", "instance");
+        conf.put(".private.param", "private");
+        Assert.assertTrue(factory.add(conf));        
+        m_ensure.waitForStep(4, 5000);
+
+        // update the service instance
+        conf.put("instance.modified", "true");
+        Assert.assertFalse(factory.add(conf));
+        m_ensure.waitForStep(7, 5000);
+
+        // remove instance
+        Assert.assertTrue(factory.remove(conf));
+    }
+}
\ No newline at end of file