Reorganized annotation tests. Added tests for annotated temporal service dependencies

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@902652 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ConsumerTest.java b/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ConsumerTest.java
new file mode 100644
index 0000000..7ebc41c
--- /dev/null
+++ b/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ConsumerTest.java
@@ -0,0 +1,27 @@
+package org.apache.felix.dm.test.annotation;
+
+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;
+
+@Service
+public class ConsumerTest
+{
+    @ServiceDependency
+    Runnable m_runnable;
+    
+    @ServiceDependency(filter="(test=simple)")
+    Sequencer m_sequencer;
+
+    @Start
+    protected void start() {
+        m_sequencer.step(2);
+        m_runnable.run();
+    }
+    
+    @Stop
+    protected void stop() {
+        m_sequencer.step(4);
+    }
+}
diff --git a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/Factory.java b/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/Factory.java
deleted file mode 100644
index 0e7bba5..0000000
--- a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/Factory.java
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * 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;
-
-/**
- * Class used to instantiate various components unsing Dependency Manager Factories.
- */
-public class Factory
-{
-    public FactoryTest createFactoryTest()
-    {
-        return new FactoryTest("factory");
-    }
-}
diff --git a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/FactoryTest.java b/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/FactoryTest.java
index a5ca145..4fa2a53 100644
--- a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/FactoryTest.java
+++ b/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/FactoryTest.java
@@ -18,7 +18,7 @@
 /**
  * Validate DependencyManager Factories declarared with annotations.
  */
-@Service(factory=Factory.class, factoryMethod="createFactoryTest")
+@Service(factory=FactoryTest.Factory.class, factoryMethod="createFactoryTest")
 public class FactoryTest
 {
     String m_id;
@@ -36,6 +36,14 @@
         if (! "factory".equals(m_id)) {
             throw new IllegalStateException();
         }
-        m_sequencer.next(1);
+        m_sequencer.step(1);
+    }
+    
+    public static class Factory
+    {
+        public FactoryTest createFactoryTest()
+        {
+            return new FactoryTest("factory");
+        }
     }
 }
diff --git a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/MultipleAnnotationTest.java b/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/MultipleAnnotationTest.java
new file mode 100644
index 0000000..a38af3a
--- /dev/null
+++ b/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/MultipleAnnotationTest.java
@@ -0,0 +1,130 @@
+package org.apache.felix.dm.test.annotation;
+
+import org.apache.felix.dm.annotation.api.Composition;
+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;
+
+public class MultipleAnnotationTest
+{
+    public static class Factory {
+        public ServiceConsumer createServiceConsumer() {
+            return new ServiceConsumer();
+        }
+        
+        public ServiceProvider createServiceProvider() {
+            return new ServiceProvider();
+        }
+        
+        public ServiceProvider2 createServiceProvider2() {
+            return new ServiceProvider2();
+        }
+    }
+    
+    public interface ServiceInterface {
+        public void doService();
+    }
+
+    @Service(factory=Factory.class, factoryMethod="createServiceConsumer")
+    static class ServiceConsumer
+    {
+        @ServiceDependency
+        volatile Sequencer m_sequencer;
+
+        @ServiceDependency(filter = "(foo=bar)")
+        volatile ServiceInterface m_service;
+
+        @Start
+        void start()
+        {
+            m_sequencer.step(6);
+            m_service.doService();
+        }
+
+        @Stop
+        void stop()
+        {
+            m_sequencer.step(8);
+        }
+    }
+
+    @Service(properties = { "foo=bar" }, factory=Factory.class, factoryMethod="createServiceProvider")
+    static class ServiceProvider implements ServiceInterface
+    {
+        @ServiceDependency(filter="(test=multiple)")
+        Sequencer m_sequencer;
+
+        @ServiceDependency(removed="unbind")
+        void bind(ServiceProvider2 provider2)
+        {
+            m_sequencer.step(4);
+        }
+
+        void unbind(ServiceProvider2 provider2)
+        {
+            m_sequencer.step(10);
+        }
+
+        @Start
+        void start()
+        {
+            m_sequencer.step(5);
+        }
+
+        @Stop
+        void stop()
+        {
+            m_sequencer.step(9);
+        }
+
+        public void doService()
+        {
+            m_sequencer.step(7);
+        }
+    }
+
+    @Service(provide = { ServiceProvider2.class }, factory=Factory.class, factoryMethod="createServiceProvider2")
+    static class ServiceProvider2
+    {
+        Composite m_composite = new Composite();
+        Sequencer m_sequencer;
+
+        @ServiceDependency(required=false, filter="(foo=bar)")
+        Runnable m_runnable;
+
+        @ServiceDependency(service=Sequencer.class, filter="(test=multiple)")
+        void bind(Sequencer seq)
+        {
+            m_sequencer = seq;
+            m_sequencer.step(1);
+        }
+
+        @Start
+        void start()
+        {
+            m_sequencer.step(3);
+            m_runnable.run();
+        }
+
+        @Stop
+        void stop()
+        {
+            m_sequencer.step(11);
+        }
+
+        @Composition
+        Object[] getComposition()
+        {
+            return new Object[] { this, m_composite };
+        }
+    }
+
+    static class Composite
+    {
+        void bind(Sequencer seq)
+        {
+            seq.step(2);
+        }
+    }
+}
diff --git a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ProducerTest.java b/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ProducerTest.java
new file mode 100644
index 0000000..82ae905
--- /dev/null
+++ b/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ProducerTest.java
@@ -0,0 +1,28 @@
+package org.apache.felix.dm.test.annotation;
+
+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;
+
+@Service
+public class ProducerTest implements Runnable
+{
+    @ServiceDependency(filter="(test=simple)")
+    Sequencer m_sequencer;
+    
+    @Start
+    protected void start() {
+        m_sequencer.step(1);
+    }
+    
+    @Stop
+    protected void stop() {
+        m_sequencer.step(5);
+    }
+    
+    public void run()
+    {
+        m_sequencer.step(3);
+    }
+}
diff --git a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/Sequencer.java b/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/Sequencer.java
index 5c95c5c..84e9163 100644
--- a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/Sequencer.java
+++ b/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/Sequencer.java
@@ -16,5 +16,6 @@
  */
 public interface Sequencer
 {
-    void next(int step);
+    void step(int step);
+    void waitForStep(int nr, int timeout);
 }
diff --git a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ServiceConsumer.java b/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ServiceConsumer.java
deleted file mode 100644
index 122c97b..0000000
--- a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ServiceConsumer.java
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * 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 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;
-
-@Service
-public class ServiceConsumer
-{
-    @ServiceDependency
-    volatile Sequencer m_sequencer;
-
-    @ServiceDependency(filter = "(foo=bar)")
-    volatile ServiceInterface m_service;
-
-    @Start
-    void start()
-    {
-        m_sequencer.next(6);
-        m_service.doService();
-    }
-
-    @Stop
-    void stop()
-    {
-        m_sequencer.next(8);
-    }
-}
diff --git a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ServiceInterface.java b/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ServiceInterface.java
deleted file mode 100644
index 73128c7..0000000
--- a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ServiceInterface.java
+++ /dev/null
@@ -1,16 +0,0 @@
-/*
- * 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;
-
-public interface ServiceInterface {
-  public void doService();
-}
diff --git a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ServiceProvider.java b/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ServiceProvider.java
deleted file mode 100644
index ad75ff4..0000000
--- a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ServiceProvider.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * 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 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;
-
-@Service(properties = { "foo=bar" })
-public class ServiceProvider implements ServiceInterface
-{
-    @ServiceDependency(filter="(test=simple)")
-    Sequencer m_sequencer;
-
-    @ServiceDependency(removed="unbind")
-    void bind(ServiceProvider2 provider2)
-    {
-        m_sequencer.next(4);
-    }
-
-    void unbind(ServiceProvider2 provider2)
-    {
-        m_sequencer.next(10);
-    }
-
-    @Start
-    void start()
-    {
-        m_sequencer.next(5);
-    }
-
-    @Stop
-    void stop()
-    {
-        m_sequencer.next(9);
-    }
-
-    public void doService()
-    {
-        m_sequencer.next(7);
-    }
-}
diff --git a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ServiceProvider2.java b/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ServiceProvider2.java
deleted file mode 100644
index 11de5a7..0000000
--- a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ServiceProvider2.java
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * 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 org.apache.felix.dm.annotation.api.Composition;
-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;
-
-@Service(provide = { ServiceProvider2.class })
-public class ServiceProvider2
-{
-    Composite m_composite = new Composite();
-    Sequencer m_sequencer;
-
-    @ServiceDependency(required=false, filter="(foo=bar)")
-    Runnable m_runnable;
-
-    @ServiceDependency(service=Sequencer.class, filter="(test=simple)")
-    void bind(Sequencer seq)
-    {
-        m_sequencer = seq;
-        m_sequencer.next(1);
-    }
-
-    @Start
-    void start()
-    {
-        m_sequencer.next(3);
-        m_runnable.run();
-    }
-
-    @Stop
-    void stop()
-    {
-        m_sequencer.next(11);
-    }
-
-    @Composition
-    Object[] getComposition()
-    {
-        return new Object[] { this, m_composite };
-    }
-}
-
-class Composite
-{
-    void bind(Sequencer seq)
-    {
-        seq.next(2);
-    }
-}
-
diff --git a/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/TemporalServiceDependencyTest.java b/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/TemporalServiceDependencyTest.java
new file mode 100644
index 0000000..8d0eb2c
--- /dev/null
+++ b/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/TemporalServiceDependencyTest.java
@@ -0,0 +1,48 @@
+package org.apache.felix.dm.test.annotation;
+
+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.TemporalServiceDependency;
+
+@Service(provide={})
+public class TemporalServiceDependencyTest implements Runnable
+{
+    Thread m_thread;
+    
+    @ServiceDependency(filter="(test=temporal)")
+    Sequencer m_sequencer;
+    
+    @TemporalServiceDependency(timeout=4000L,filter="(test=temporal)")
+    Runnable m_service;
+    
+    @Start
+    protected void start() {
+        m_thread = new Thread(this);
+        m_thread.start();
+    }
+    
+    protected void stop() { 
+        m_thread.interrupt();
+        try
+        {
+            m_thread.join();
+        }
+        catch (InterruptedException e)
+        {
+        }
+    }
+
+    public void run()
+    {
+        m_service.run();
+        m_sequencer.waitForStep(2, 15000);
+        m_service.run(); // we should block here
+        m_sequencer.waitForStep(4, 15000);
+        try {
+            m_service.run(); // should raise IllegalStateException
+        } catch (IllegalStateException e) {
+            m_sequencer.step(5);
+        }
+    }
+}
\ No newline at end of file
diff --git a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AnnotationTest.java b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AnnotationTest.java
index bc41b4d..493c86b 100644
--- a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AnnotationTest.java
+++ b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AnnotationTest.java
@@ -26,6 +26,7 @@
 import java.util.Hashtable;
 
 import org.apache.felix.dm.DependencyManager;
+import org.apache.felix.dm.service.Service;
 import org.apache.felix.dm.test.annotation.Sequencer;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -61,8 +62,76 @@
         Dictionary props = new Hashtable() {{ put("test", "simple"); }};
         m.add(m.createService().setImplementation(this).setInterface(Sequencer.class.getName(), props));
         // Check if the test.annotation components have been initialized orderly
+        m_ensure.waitForStep(3, 10000);
+        // Stop our annotation bundle.
+        stopAnnotationBundle(context);
+        // And check if components have been deactivated orderly.
+        m_ensure.waitForStep(5, 10000);
+    }
+
+    @Test
+    public void testFactoryAnnotation(BundleContext context)
+    {
+        m_ensure = new Ensure();
+        DependencyManager m = new DependencyManager(context);
+        // We provide ourself as a "Sequencer" service: this will active the "org.apache.felix.dependencymanager.test.annotation" bundle
+        Dictionary props = new Hashtable() {{ put("test", "factory"); }};
+        m.add(m.createService().setImplementation(this).setInterface(Sequencer.class.getName(), props));
+        // Check if the test.annotation components have been initialized orderly
+        m_ensure.waitForStep(1, 10000);
+    }
+        
+    @Test
+    public void testMultipleAnnotations(BundleContext context)
+    {
+        m_ensure = new Ensure();
+        DependencyManager m = new DependencyManager(context);
+        // We provide ourself as a "Sequencer" service: this will active the "org.apache.felix.dependencymanager.test.annotation" bundle
+        Dictionary props = new Hashtable() {{ put("test", "multiple"); }};
+        m.add(m.createService().setImplementation(this).setInterface(Sequencer.class.getName(), props));
+        // Check if the test.annotation components have been initialized orderly
         m_ensure.waitForStep(7, 10000);
         // Stop the test.annotation bundle
+        stopAnnotationBundle(context);
+        // And check if the test.annotation bundle has been deactivated orderly
+        m_ensure.waitForStep(11, 10000);
+    }
+
+    @Test
+    public void testTemporalServiceDepedendencyAnnotation(BundleContext context)
+    {
+        m_ensure = new Ensure();
+        DependencyManager m = new DependencyManager(context);
+        // We provide ourself as a "Sequencer" service: this will active the "org.apache.felix.dependencymanager.test.annotation" bundle
+        Dictionary props = new Hashtable() {{ put("test", "temporal"); }};
+        m.add(m.createService().setImplementation(this).setInterface(Sequencer.class.getName(), props));
+                 
+        Runnable r = Ensure.createRunnableStep(m_ensure, 1);
+        Service s = m.createService().setImplementation(r).setInterface(Runnable.class.getName(), props);        
+        m.add(s);
+        m_ensure.waitForStep(1, 15000);
+        m.remove(s);
+        m_ensure.step(2);
+        sleep(2000);
+        r = Ensure.createRunnableStep(m_ensure, 3);
+        s = m.createService().setImplementation(r).setInterface(Runnable.class.getName(), props);
+        m.add(s);
+        m_ensure.waitForStep(3, 15000);
+        m.remove(s);
+        m_ensure.step(4);
+        sleep(5000);
+        m_ensure.waitForStep(5, 15000);
+    }
+
+    private void sleep(int i)
+    {
+        try {
+            Thread.sleep(i);
+        } catch (InterruptedException e) {}
+    }
+    
+    private void stopAnnotationBundle(BundleContext context) {
+        // Stop the test.annotation bundle
         boolean found = false;
         for (Bundle b : context.getBundles())
         {
@@ -83,25 +152,16 @@
         {
             throw new IllegalStateException("org.apache.felix.dependencymanager.test.annotation bundle not found");
         }
-        // And check if the test.annotation bundle has been deactivated orderly
-        m_ensure.waitForStep(11, 10000);
     }
+    // ----------------------- Sequencer interface ------------------------------------------
 
-    @Test
-    public void testFactoryAnnotation(BundleContext context)
-    {
-        m_ensure = new Ensure();
-        DependencyManager m = new DependencyManager(context);
-        // We provide ourself as a "Sequencer" service: this will active the "org.apache.felix.dependencymanager.test.annotation" bundle
-        Dictionary props = new Hashtable() {{ put("test", "factory"); }};
-        m.add(m.createService().setImplementation(this).setInterface(Sequencer.class.getName(), props));
-        // Check if the test.annotation components have been initialized orderly
-        m_ensure.waitForStep(1, 10000);
-    }
-        
-    // The test.annotation bundle will call back us here
-    public void next(int step)
+    public void step(int step)
     {
         m_ensure.step(step);
     }
+
+    public void waitForStep(int nr, int timeout)
+    {
+        m_ensure.waitForStep(nr, timeout);
+    }    
 }