felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pde...@apache.org
Subject svn commit: r1575366 - in /felix/sandbox/pderop/dependencymanager-prototype/dm.it/src/dm/it: AdapterAndConsumerTest.java AdapterWithExtraDependenciesTest.java AdapterWithInstanceBoundDependencyTest.java
Date Fri, 07 Mar 2014 18:46:00 GMT
Author: pderop
Date: Fri Mar  7 18:46:00 2014
New Revision: 1575366

URL: http://svn.apache.org/r1575366
Log:
Added some adapter integration tests.
Notice that currently, the AdapterWithModifiedInstanceBoundDependencyTest has not yet 
been added because it does not currently passes OK.
-This line, and those below, will be ignored--

A    dm.it/src/dm/it/AdapterAndConsumerTest.java
A    dm.it/src/dm/it/AdapterWithExtraDependenciesTest.java
A    dm.it/src/dm/it/AdapterWithInstanceBoundDependencyTest.java

Added:
    felix/sandbox/pderop/dependencymanager-prototype/dm.it/src/dm/it/AdapterAndConsumerTest.java
    felix/sandbox/pderop/dependencymanager-prototype/dm.it/src/dm/it/AdapterWithExtraDependenciesTest.java
    felix/sandbox/pderop/dependencymanager-prototype/dm.it/src/dm/it/AdapterWithInstanceBoundDependencyTest.java

Added: felix/sandbox/pderop/dependencymanager-prototype/dm.it/src/dm/it/AdapterAndConsumerTest.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-prototype/dm.it/src/dm/it/AdapterAndConsumerTest.java?rev=1575366&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-prototype/dm.it/src/dm/it/AdapterAndConsumerTest.java
(added)
+++ felix/sandbox/pderop/dependencymanager-prototype/dm.it/src/dm/it/AdapterAndConsumerTest.java
Fri Mar  7 18:46:00 2014
@@ -0,0 +1,108 @@
+/*
+ * 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 dm.it;
+
+import dm.Component;
+import dm.DependencyManager;
+
+public class AdapterAndConsumerTest extends TestBase {
+    
+    public void testServiceWithAdapterAndConsumer() {
+        DependencyManager m = new DependencyManager(context);
+        // helper class that ensures certain steps get executed in sequence
+        Ensure e = new Ensure();
+
+        Component provider = m.createComponent()
+            .setInterface(OriginalService.class.getName(), null)
+            .setImplementation(new ServiceProvider(e));
+
+        Component consumer = m.createComponent()
+            .setImplementation(new ServiceConsumer(e))
+            .add(m.createServiceDependency()
+                .setService(AdaptedService.class)
+                .setRequired(true)
+            );
+
+        Component adapter = m.createAdapterService(OriginalService.class, null)
+            .setInterface(AdaptedService.class.getName(), null)
+            .setImplementation(ServiceAdapter.class);
+        
+        // add the provider and the adapter
+        m.add(provider);
+        m.add(adapter);
+        // add a consumer that will invoke the adapter
+        // which will in turn invoke the original provider
+        m.add(consumer);
+        // now validate that both have been invoked in the right order
+        e.waitForStep(2, 5000);
+        // remove the provider again
+        m.remove(provider);
+        // ensure that the consumer is stopped
+        e.waitForStep(3, 5000);
+        // remove adapter and consumer
+        m.remove(adapter);
+        m.remove(consumer);
+    }
+
+    static interface OriginalService {
+        public void invoke();
+    }
+    
+    static interface AdaptedService {
+        public void invoke();
+    }
+    
+    static class ServiceProvider implements OriginalService {
+        private final Ensure m_ensure;
+        public ServiceProvider(Ensure e) {
+            m_ensure = e;
+        }
+        public void invoke() {
+            m_ensure.step(2);
+        }
+    }
+    
+    public static class ServiceAdapter implements AdaptedService {
+        private volatile OriginalService m_originalService;
+        
+        public void start() { System.out.println("start"); }
+        public void stop() { System.out.println("stop"); }
+        public void invoke() {
+            m_originalService.invoke();
+        }
+    }
+
+    static class ServiceConsumer {
+        private volatile AdaptedService m_service;
+        private final Ensure m_ensure;
+        
+        public ServiceConsumer(Ensure e) {
+            m_ensure = e;
+        }
+        public void start() {
+            m_ensure.step(1);
+            m_service.invoke();
+        }
+        public void stop() {
+            m_ensure.step(3);
+        }
+    }
+}
+
+

Added: felix/sandbox/pderop/dependencymanager-prototype/dm.it/src/dm/it/AdapterWithExtraDependenciesTest.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-prototype/dm.it/src/dm/it/AdapterWithExtraDependenciesTest.java?rev=1575366&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-prototype/dm.it/src/dm/it/AdapterWithExtraDependenciesTest.java
(added)
+++ felix/sandbox/pderop/dependencymanager-prototype/dm.it/src/dm/it/AdapterWithExtraDependenciesTest.java
Fri Mar  7 18:46:00 2014
@@ -0,0 +1,100 @@
+/*
+ * 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 dm.it;
+
+import dm.Component;
+import dm.DependencyManager;
+
+
+public class AdapterWithExtraDependenciesTest extends TestBase {
+    public void testAdapterWithExtraDependenciesAndCallbacks() {
+        DependencyManager m = new DependencyManager(context);
+        // helper class that ensures certain steps get executed in sequence
+        Ensure e = new Ensure();
+        
+        // create a service adapter that adapts to services S1 and has an optional dependency
on services S2
+        Component sa = m.createAdapterService(S1.class, null)
+            .setImplementation(SA.class)
+            .add(m.createServiceDependency().setService(S2.class).setCallbacks("add", "remove"));
+        m.add(sa);
+        
+        // create a service S1, which triggers the creation of the first adapter instance
(A1)
+        Component s1 = m.createComponent().setInterface(S1.class.getName(), null).setImplementation(new
S1Impl());
+        m.add(s1);
+        
+        // create a service S2, which will be added to A1
+        Component s2 = m.createComponent().setInterface(S2.class.getName(), null).setImplementation(new
S2Impl(e));
+        m.add(s2);
+        
+        // create a second service S1, which triggers the creation of the second adapter
instance (A2)
+        Component s1b = m.createComponent().setInterface(S1.class.getName(), null).setImplementation(new
S1Impl());
+        m.add(s1b);
+        
+        // observe that S2 is also added to A2
+        e.waitForStep(2, 5000);
+        
+        // remove S2 again
+        m.remove(s2);
+        
+        // make sure both adapters have their "remove" callbacks invoked
+        e.waitForStep(4, 5000);
+        
+        m.remove(s1);
+        m.remove(sa);
+    }
+    
+    static interface S1 {
+    }
+    static interface S2 {
+        public void invoke();
+    }
+    static class S1Impl implements S1 {
+    }
+    static class S2Impl implements S2 {
+
+        private final Ensure m_e;
+
+        public S2Impl(Ensure e) {
+            m_e = e;
+        }
+
+        public void invoke() {
+            m_e.step();
+        }
+    }
+    
+    public static class SA {
+        volatile S2 s2;
+        
+        public SA() {
+            System.out.println("Adapter created");
+        }
+        public void init() {
+            System.out.println("Adapter init " + s2);
+        }
+        public void add(S2 s) {
+            System.out.println("adding " + s);
+            s.invoke();
+        }
+        public void remove(S2 s) {
+            System.out.println("removing " + s);
+            s.invoke();
+        }
+    }
+}

Added: felix/sandbox/pderop/dependencymanager-prototype/dm.it/src/dm/it/AdapterWithInstanceBoundDependencyTest.java
URL: http://svn.apache.org/viewvc/felix/sandbox/pderop/dependencymanager-prototype/dm.it/src/dm/it/AdapterWithInstanceBoundDependencyTest.java?rev=1575366&view=auto
==============================================================================
--- felix/sandbox/pderop/dependencymanager-prototype/dm.it/src/dm/it/AdapterWithInstanceBoundDependencyTest.java
(added)
+++ felix/sandbox/pderop/dependencymanager-prototype/dm.it/src/dm/it/AdapterWithInstanceBoundDependencyTest.java
Fri Mar  7 18:46:00 2014
@@ -0,0 +1,132 @@
+/*
+ * 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 dm.it;
+
+import dm.Component;
+import dm.DependencyManager;
+
+
+public class AdapterWithInstanceBoundDependencyTest extends TestBase {
+    public void testInstanceBoundDependency() {
+        DependencyManager m = new DependencyManager(context);
+        // helper class that ensures certain steps get executed in sequence
+        Ensure e = new Ensure();
+        // create a service provider and consumer
+        Component sp = m.createComponent()
+            .setInterface(ServiceInterface.class.getName(), null)
+            .setImplementation(new ServiceProvider(e));
+        Component sp2 = m.createComponent()
+        .setInterface(ServiceInterface2.class.getName(), null)
+            .setImplementation(new ServiceProvider2(e));
+        Component sc = m.createComponent()
+            .setImplementation(new ServiceConsumer())
+            .add(m.createServiceDependency()
+                .setService(ServiceInterface3.class)
+                .setRequired(true));
+        Component sa = m.createAdapterService(ServiceInterface.class, null)
+            .setInterface(ServiceInterface3.class.getName(), null)
+            .setImplementation(new ServiceAdapter(e));
+        m.add(sc);
+        m.add(sp);
+        m.add(sp2);
+        m.add(sa);
+        e.waitForStep(5, 15000);
+        m.remove(sa);
+        m.remove(sp2);
+        m.remove(sp);
+        m.remove(sc);
+    }
+    
+    static interface ServiceInterface {
+        public void invoke();
+    }
+    
+    static interface ServiceInterface2 {
+        public void invoke();
+    }
+    
+    static interface ServiceInterface3 {
+        public void invoke();
+    }
+    
+    static class ServiceProvider2 implements ServiceInterface2 {
+        private final Ensure m_ensure;
+
+        public ServiceProvider2(Ensure ensure) {
+            m_ensure = ensure;
+        }
+
+        public void invoke() {
+            m_ensure.step(4);
+        }
+    }
+
+    static class ServiceProvider implements ServiceInterface {
+        private final Ensure m_ensure;
+        public ServiceProvider(Ensure e) {
+            m_ensure = e;
+        }
+        public void invoke() {
+            m_ensure.step(5);
+        }
+    }
+    
+    static class ServiceAdapter implements ServiceInterface3 {
+        private Ensure m_ensure;
+        private volatile ServiceInterface m_originalService;
+        private volatile ServiceInterface2 m_injectedService;
+        private volatile Component m_service;
+        private volatile DependencyManager m_manager;
+        
+        public ServiceAdapter(Ensure e) {
+            m_ensure = e;
+        }
+        public void init() {
+            m_ensure.step(1);
+            m_service.add(m_manager.createServiceDependency().setRequired(true).setService(ServiceInterface2.class));
+        }
+        public void start() {
+            m_ensure.step(2);
+        }
+        public void invoke() {
+            m_ensure.step(3);
+            m_injectedService.invoke();
+            m_originalService.invoke();
+        }
+        
+        public void stop() {
+            m_ensure.step(6);
+        }
+    }
+
+    static class ServiceConsumer implements Runnable {
+        private volatile ServiceInterface3 m_service;
+        
+        public void init() {
+            Thread t = new Thread(this);
+            t.start();
+        }
+        
+        public void run() {
+            m_service.invoke();
+        }
+    }
+}
+
+



Mime
View raw message