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 2016/09/08 10:51:38 UTC

svn commit: r1759777 - in /felix/trunk/dependencymanager/org.apache.felix.dependencymanager.runtime.itest/src/org/apache/felix/dm/runtime/itest: components/OptionalConfiguration.java tests/OptionalConfigurationTest.java

Author: pderop
Date: Thu Sep  8 10:51:38 2016
New Revision: 1759777

URL: http://svn.apache.org/viewvc?rev=1759777&view=rev
Log:
FELIX-5238: Added test case for optional configuration dependency annotation.

Added:
    felix/trunk/dependencymanager/org.apache.felix.dependencymanager.runtime.itest/src/org/apache/felix/dm/runtime/itest/components/OptionalConfiguration.java
    felix/trunk/dependencymanager/org.apache.felix.dependencymanager.runtime.itest/src/org/apache/felix/dm/runtime/itest/tests/OptionalConfigurationTest.java

Added: felix/trunk/dependencymanager/org.apache.felix.dependencymanager.runtime.itest/src/org/apache/felix/dm/runtime/itest/components/OptionalConfiguration.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/org.apache.felix.dependencymanager.runtime.itest/src/org/apache/felix/dm/runtime/itest/components/OptionalConfiguration.java?rev=1759777&view=auto
==============================================================================
--- felix/trunk/dependencymanager/org.apache.felix.dependencymanager.runtime.itest/src/org/apache/felix/dm/runtime/itest/components/OptionalConfiguration.java (added)
+++ felix/trunk/dependencymanager/org.apache.felix.dependencymanager.runtime.itest/src/org/apache/felix/dm/runtime/itest/components/OptionalConfiguration.java Thu Sep  8 10:51:38 2016
@@ -0,0 +1,117 @@
+/*
+* 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.runtime.itest.components;
+
+import java.io.IOException;
+import java.util.Properties;
+
+import org.apache.felix.dm.annotation.api.Component;
+import org.apache.felix.dm.annotation.api.ConfigurationDependency;
+import org.apache.felix.dm.annotation.api.Init;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.apache.felix.dm.annotation.api.Start;
+import org.apache.felix.dm.annotation.api.Stop;
+import org.apache.felix.dm.itest.util.Ensure;
+import org.junit.Assert;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+
+public class OptionalConfiguration {
+    public final static String ENSURE_CONF_CREATOR = "OptionalConfiguration.Conf.Creator";
+    public final static String ENSURE_CONF_CONSUMER = "OptionalConfiguration.Conf.Consumer";
+    final static String PID = "OptionalConfiguration.pid";
+
+    public static interface MyConfig {
+        public default String getTestkey() { return "default"; }
+    }
+
+    @Component
+    public static class OptionalConfigurationConsumer {
+	    @ServiceDependency(required = true, filter = "(name=" + ENSURE_CONF_CONSUMER + ")")
+	    public volatile Ensure m_ensure;
+	    
+        protected volatile int m_updateCount;
+        
+        @ConfigurationDependency(pid=PID, required=false)
+        public void updated(MyConfig cnf) { // optional configuration, called after start(), like any other optional dependency callbacks.
+        	if (cnf != null) {
+        		m_updateCount ++;
+        		if (m_updateCount == 1) {
+        			if (!"default".equals(cnf.getTestkey())) {
+        				Assert.fail("Could not find the configured property.");
+        			}
+        		} else if (m_updateCount == 2) {
+        			if (!"testvalue".equals(cnf.getTestkey())) {
+        				Assert.fail("Could not find the configured property.");
+        			}
+            		m_ensure.step(2);
+        		} else if (m_updateCount == 3) {
+        			if (!"default".equals(cnf.getTestkey())) {
+        				Assert.fail("Could not find the configured property.");
+        			}
+            		m_ensure.step(3);
+        		}
+        	} else {
+        		// configuration destroyed: should never happen
+        		m_ensure.throwable(new Exception("lost configuration"));
+        	}
+        }
+
+        @Start
+        public void start() {
+        	m_ensure.step(1);
+        }
+        
+        @Stop
+        public void stop() {
+        	m_ensure.step(4);
+        }
+    }
+
+	@Component
+	public static class ConfigurationCreator {
+		@ServiceDependency
+	    private volatile ConfigurationAdmin m_ca;
+		
+	    @ServiceDependency(required = true, filter = "(name=" + ENSURE_CONF_CREATOR + ")")
+	    private Ensure m_ensure;
+	    
+	    Configuration m_conf;
+	    
+	    @Start
+		public void start() {
+	        try {
+	        	Assert.assertNotNull(m_ca);
+	            m_conf = m_ca.getConfiguration(PID, null);
+	            Properties props = new Properties();
+	            props.put("testkey", "testvalue");
+	            m_conf.update(props);
+	        }
+	        catch (IOException e) {
+	            Assert.fail("Could not create configuration: " + e.getMessage());
+	        }
+	    }
+	    
+	    @Stop
+	    public void stop() throws IOException {
+	    	m_conf.delete();  
+	    }
+	}
+	
+}

Added: felix/trunk/dependencymanager/org.apache.felix.dependencymanager.runtime.itest/src/org/apache/felix/dm/runtime/itest/tests/OptionalConfigurationTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/org.apache.felix.dependencymanager.runtime.itest/src/org/apache/felix/dm/runtime/itest/tests/OptionalConfigurationTest.java?rev=1759777&view=auto
==============================================================================
--- felix/trunk/dependencymanager/org.apache.felix.dependencymanager.runtime.itest/src/org/apache/felix/dm/runtime/itest/tests/OptionalConfigurationTest.java (added)
+++ felix/trunk/dependencymanager/org.apache.felix.dependencymanager.runtime.itest/src/org/apache/felix/dm/runtime/itest/tests/OptionalConfigurationTest.java Thu Sep  8 10:51:38 2016
@@ -0,0 +1,45 @@
+/*
+* 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.runtime.itest.tests;
+
+import org.apache.felix.dm.itest.util.Ensure;
+import org.apache.felix.dm.itest.util.TestBase;
+import org.apache.felix.dm.runtime.itest.components.OptionalConfiguration;
+import org.osgi.framework.ServiceRegistration;
+
+public class OptionalConfigurationTest extends TestBase {
+	
+    public void testOptionalConfig() throws Throwable {
+        Ensure e = new Ensure();
+        ServiceRegistration ensureConsumer = register(e, OptionalConfiguration.ENSURE_CONF_CONSUMER);
+        e.waitForStep(1, 5000); // consumer called in updated with testKey="default" config, and then called in start
+        
+        // will register a "testKey=testvalue" configuration, using config admin.
+        ServiceRegistration ensureConfCreator = register(e, OptionalConfiguration.ENSURE_CONF_CREATOR);
+        
+        e.waitForStep(2, 5000);  // consumer called in updated with testKey="testvalue"
+        ensureConfCreator.unregister(); // remove configuration.
+        e.waitForStep(3, 5000);  // consumer called in updated with default "testkey=default" property (using the default method from the config interface.        
+        ensureConsumer.unregister(); // stop the OptionalConfigurationConsumer component
+        e.waitForStep(4, 5000);  // consumer stopped
+
+        e.ensure();
+    }
+
+}