You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by pd...@apache.org on 2015/11/22 19:42:22 UTC

svn commit: r1715673 - /felix/trunk/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/api/FactoryInjectedWithConfigurationBeforeTheCreateMethod.java

Author: pderop
Date: Sun Nov 22 18:42:22 2015
New Revision: 1715673

URL: http://svn.apache.org/viewvc?rev=1715673&view=rev
Log:
Added a missing test that is verifying that a ConfigurationDependency callback instance is always called on a Factory object
before the Factory.create method is called.

Added:
    felix/trunk/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/api/FactoryInjectedWithConfigurationBeforeTheCreateMethod.java

Added: felix/trunk/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/api/FactoryInjectedWithConfigurationBeforeTheCreateMethod.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/api/FactoryInjectedWithConfigurationBeforeTheCreateMethod.java?rev=1715673&view=auto
==============================================================================
--- felix/trunk/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/api/FactoryInjectedWithConfigurationBeforeTheCreateMethod.java (added)
+++ felix/trunk/dependencymanager/org.apache.felix.dependencymanager.itest/src/org/apache/felix/dm/itest/api/FactoryInjectedWithConfigurationBeforeTheCreateMethod.java Sun Nov 22 18:42:22 2015
@@ -0,0 +1,107 @@
+/*
+ * 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.itest.api;
+
+import java.io.IOException;
+import java.util.Dictionary;
+import java.util.Properties;
+
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
+import org.apache.felix.dm.itest.util.Ensure;
+import org.apache.felix.dm.itest.util.TestBase;
+import org.junit.Assert;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+
+/**
+ * Use case: one component is instantiated using another factory object, and the 
+ * factory object needs the configuration before the factory.create method is called.
+ */
+public class FactoryInjectedWithConfigurationBeforeTheCreateMethod extends TestBase {
+    Ensure m_e;
+    
+    public void testServiceInjection() {
+        DependencyManager m = getDM();
+        m_e = new Ensure();
+        
+        // Create the component that creates a configuration.
+        Component configurator = m.createComponent().setImplementation(new Configurator("foobar")).add(m.createServiceDependency().setService(ConfigurationAdmin.class).setRequired(true));
+        
+        // Create the object that has to be injected with the configuration before its create method is called.
+        MyFactory factory = new MyFactory();
+        
+        // Create the Component for the MyComponent class that is created using the factory above.
+        Component myComponent = m.createComponent().setFactory(factory, "create").add(m.createConfigurationDependency().setPid("foobar").setCallback(factory, "updated"));
+        
+        // provide the configuration
+        m.add(configurator);
+        
+        m.add(myComponent);
+        m_e.waitForStep(4, 300000000);
+        m.remove(myComponent);
+        m.remove(configurator);
+    }
+    
+    class Configurator {
+        private volatile ConfigurationAdmin m_ca;
+        Configuration m_conf;
+        final String m_pid;
+        
+        public Configurator(String pid) {
+            m_pid = pid;
+        }
+
+        public void init() {
+            try {
+                Assert.assertNotNull(m_ca);
+                m_e.step(1);
+                m_conf = m_ca.getConfiguration(m_pid, null);
+                Properties props = new Properties();
+                props.setProperty("testkey", "testvalue");
+                m_conf.update(props);
+            }
+            catch (IOException e) {
+                Assert.fail("Could not create configuration: " + e.getMessage());
+            }
+        }
+        
+        public void destroy() throws IOException {
+            m_conf.delete();  
+        }
+    }
+
+    public class MyFactory {
+        public void updated(Dictionary<String, Object> conf) {
+            Assert.assertNotNull("configuration is null", conf);
+            m_e.step(2);
+        }
+        
+        public MyComponent create() {
+            m_e.step(3);
+            return new MyComponent();
+        }
+    }
+    
+    public class MyComponent {
+        void start() {
+            m_e.step(4);
+        }        
+    }
+}