Author: pderop
Date: Sun Jan 24 21:45:13 2010
New Revision: 902652

URL: http://svn.apache.org/viewvc?rev=902652&view=rev
Log:
Reorganized annotation tests. Added tests for annotated temporal service 
dependencies

Added:
    
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ConsumerTest.java
    
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/MultipleAnnotationTest.java
    
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ProducerTest.java
    
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/TemporalServiceDependencyTest.java
Removed:
    
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/Factory.java
    
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ServiceConsumer.java
    
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ServiceInterface.java
    
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ServiceProvider.java
    
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ServiceProvider2.java
Modified:
    
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/FactoryTest.java
    
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/Sequencer.java
    
felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AnnotationTest.java

Added: 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ConsumerTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ConsumerTest.java?rev=902652&view=auto
==============================================================================
--- 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ConsumerTest.java
 (added)
+++ 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ConsumerTest.java
 Sun Jan 24 21:45:13 2010
@@ -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);
+    }
+}

Modified: 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/FactoryTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/FactoryTest.java?rev=902652&r1=902651&r2=902652&view=diff
==============================================================================
--- 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/FactoryTest.java
 (original)
+++ 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/FactoryTest.java
 Sun Jan 24 21:45:13 2010
@@ -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");
+        }
     }
 }

Added: 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/MultipleAnnotationTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/MultipleAnnotationTest.java?rev=902652&view=auto
==============================================================================
--- 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/MultipleAnnotationTest.java
 (added)
+++ 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/MultipleAnnotationTest.java
 Sun Jan 24 21:45:13 2010
@@ -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);
+        }
+    }
+}

Added: 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ProducerTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ProducerTest.java?rev=902652&view=auto
==============================================================================
--- 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ProducerTest.java
 (added)
+++ 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/ProducerTest.java
 Sun Jan 24 21:45:13 2010
@@ -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);
+    }
+}

Modified: 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/Sequencer.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/Sequencer.java?rev=902652&r1=902651&r2=902652&view=diff
==============================================================================
--- 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/Sequencer.java
 (original)
+++ 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/Sequencer.java
 Sun Jan 24 21:45:13 2010
@@ -16,5 +16,6 @@
  */
 public interface Sequencer
 {
-    void next(int step);
+    void step(int step);
+    void waitForStep(int nr, int timeout);
 }

Added: 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/TemporalServiceDependencyTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/TemporalServiceDependencyTest.java?rev=902652&view=auto
==============================================================================
--- 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/TemporalServiceDependencyTest.java
 (added)
+++ 
felix/trunk/dependencymanager/test/annotation/src/main/java/org/apache/felix/dm/test/annotation/TemporalServiceDependencyTest.java
 Sun Jan 24 21:45:13 2010
@@ -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

Modified: 
felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AnnotationTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AnnotationTest.java?rev=902652&r1=902651&r2=902652&view=diff
==============================================================================
--- 
felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AnnotationTest.java
 (original)
+++ 
felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/AnnotationTest.java
 Sun Jan 24 21:45:13 2010
@@ -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);
+    }    
 }


Reply via email to