Add test using the config admin and lifecycle controller
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1034701 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/tests/core/factories/pom.xml b/ipojo/tests/core/factories/pom.xml
index afd45fe..18688d5 100644
--- a/ipojo/tests/core/factories/pom.xml
+++ b/ipojo/tests/core/factories/pom.xml
@@ -51,6 +51,12 @@
<artifactId>org.osgi.compendium</artifactId>
<version>4.0.0</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.configadmin</artifactId>
+ <version>1.2.0</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
<plugins>
diff --git a/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/ReconfigurableSimpleType.java b/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/ReconfigurableSimpleType.java
index 81ebd04..33a01bd 100644
--- a/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/ReconfigurableSimpleType.java
+++ b/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/ReconfigurableSimpleType.java
@@ -5,6 +5,9 @@
private String prop; // Property.
+
+ boolean controller;
+
public void start () {
if (prop == null || prop.equals("KO")) {
throw new IllegalStateException("Bad Configuration : " + prop);
@@ -12,6 +15,14 @@
System.out.println("OK !!!!");
}
-
+ public void setProp(String p) {
+ prop = p;
+ if (prop == null || prop.equals("KO")) {
+ controller = false;
+ } else {
+ controller = true;
+ System.out.println("OK !!!!");
+ }
+ }
}
diff --git a/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/ReconfigurationTest.java b/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/ReconfigurationTest.java
index 5628b4b..cbd3938 100644
--- a/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/ReconfigurationTest.java
+++ b/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/ReconfigurationTest.java
@@ -1,19 +1,65 @@
package org.apache.felix.ipojo.test.scenarios.factories;
+import java.io.IOException;
+import java.util.Dictionary;
import java.util.Properties;
+import junit.framework.Assert;
+
import org.apache.felix.ipojo.ComponentFactory;
import org.apache.felix.ipojo.ComponentInstance;
import org.apache.felix.ipojo.ConfigurationException;
import org.apache.felix.ipojo.MissingHandlerException;
import org.apache.felix.ipojo.UnacceptableConfiguration;
+import org.apache.felix.ipojo.architecture.Architecture;
import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
import org.apache.felix.ipojo.test.scenarios.util.Utils;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
public class ReconfigurationTest extends OSGiTestCase {
+ private ConfigurationAdmin admin;
- public void testRevalidationOnREconfiguration() {
+ public void setUp() {
+ admin = (ConfigurationAdmin) Utils.getServiceObject(getContext(), ConfigurationAdmin.class.getName(), null);
+ assertNotNull("Check configuration admin availability", admin);
+ try {
+ Configuration[] configurations = admin.listConfigurations(
+ "(service.factoryPid=org.apache.felix.ipojo.test.scenarios.component.ReconfigurableSimpleType)");
+ for (int i = 0; configurations != null && i < configurations.length; i++) {
+ configurations[i].delete();
+ }
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (InvalidSyntaxException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ public void tearDown() {
+ try {
+ Configuration[] configurations = admin.listConfigurations(
+ "(service.factoryPid=org.apache.felix.ipojo.test.scenarios.component.ReconfigurableSimpleType)");
+ for (int i = 0; configurations != null && i < configurations.length; i++) {
+ configurations[i].delete();
+ }
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (InvalidSyntaxException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ admin = null;
+
+
+ }
+
+ public void testRevalidationOnReconfiguration() {
ComponentFactory factory = (ComponentFactory) Utils.getFactoryByName(getContext(),
"org.apache.felix.ipojo.test.scenarios.component.ReconfigurableSimpleType");
@@ -44,6 +90,154 @@
assertEquals("instance valid", ComponentInstance.VALID, ci.getState());
}
+ public static long UPDATE_WAIT_TIME = 2000;
+
+ public void testRevalidationOnReconfigurationUsingConfigAdmin() throws InvalidSyntaxException {
+ Configuration configuration = null;
+ try {
+ configuration = admin.createFactoryConfiguration("org.apache.felix.ipojo.test.scenarios.component.ReconfigurableSimpleType", null);
+ } catch (IOException e) {
+ fail(e.getMessage());
+ }
+ Dictionary props = configuration.getProperties();
+ if(props == null) {
+ props = new Properties();
+ }
+ // First inject a configuration triggering an exception of the validate method.
+ props.put("prop", "KO");
+
+ try {
+ configuration.update(props);
+ } catch (IOException e) {
+ fail(e.getMessage());
+ }
+
+ String pid = configuration.getPid();
+
+ // Wait for the processing of the first configuration.
+ try {
+ Thread.sleep(UPDATE_WAIT_TIME);
+ } catch (InterruptedException e1) {
+ fail(e1.getMessage());
+ }
+
+ Assert.assertNull("No architecture", getContext().getServiceReferences(Architecture.class.getName(), "(architecture.instance="+pid+")"));
+ // Reconfigure
+ props = new Properties();
+ props.put("prop", "OK");
+
+ try {
+ configuration.update(props);
+ } catch (IOException e) {
+ fail(e.getMessage());
+ }
+
+ pid = configuration.getPid();
+
+ // Wait for the processing of the first configuration.
+ try {
+ Thread.sleep(UPDATE_WAIT_TIME);
+ } catch (InterruptedException e1) {
+ fail(e1.getMessage());
+ }
+
+ Assert.assertNotNull("architecture", getContext().getServiceReferences(Architecture.class.getName(), "(architecture.instance="+pid+")"));
+ Architecture arch = (Architecture) Utils.getServiceObject(getContext(), Architecture.class.getName(), "(architecture.instance="+pid+")");
+
+ Assert.assertEquals("Is valid ?", ComponentInstance.VALID, arch.getInstanceDescription().getState());
+ }
+
+ public void testRevalidationOnReconfigurationWithController() {
+ ComponentFactory factory = (ComponentFactory) Utils.getFactoryByName(getContext(),
+ "org.apache.felix.ipojo.test.scenarios.component.ReconfigurableSimpleType2");
+
+ // First inject a configuration triggering an exception of the validate method.
+ Properties props = new Properties();
+ props.put("prop", "KO");
+ ComponentInstance ci = null;
+ try {
+ ci = factory.createComponentInstance(props);
+ } catch (UnacceptableConfiguration e) {
+ e.printStackTrace();
+ } catch (MissingHandlerException e) {
+ e.printStackTrace();
+ } catch (ConfigurationException e) {
+ e.printStackTrace();
+ }
+
+ assertNotNull(ci);
+ assertEquals("instance invalid", ComponentInstance.INVALID, ci.getState()); // Controller effect.
+
+ // Reconfigure
+ props = new Properties();
+ props.put("prop", "OK");
+
+ ci.reconfigure(props);
+
+ assertNotNull(ci);
+ assertEquals("instance valid", ComponentInstance.VALID, ci.getState());
+ }
+
+ public void testRevalidationOnReconfigurationUsingConfigAdminAndController() throws InvalidSyntaxException {
+ Configuration configuration = null;
+ try {
+ configuration = admin.createFactoryConfiguration("org.apache.felix.ipojo.test.scenarios.component.ReconfigurableSimpleType2",
+ null);
+ } catch (IOException e) {
+ fail(e.getMessage());
+ }
+ Dictionary props = configuration.getProperties();
+ if(props == null) {
+ props = new Properties();
+ }
+ // First inject a configuration triggering an exception of the validate method.
+ props.put("prop", "KO");
+
+ try {
+ configuration.update(props);
+ } catch (IOException e) {
+ fail(e.getMessage());
+ }
+
+ String pid = configuration.getPid();
+
+ // Wait for the processing of the first configuration.
+ try {
+ Thread.sleep(UPDATE_WAIT_TIME);
+ } catch (InterruptedException e1) {
+ fail(e1.getMessage());
+ }
+
+ // Invalid ... controller effect
+ Assert.assertNotNull("architecture", getContext().getServiceReferences(Architecture.class.getName(), "(architecture.instance="+pid+")"));
+ Architecture arch = (Architecture) Utils.getServiceObject(getContext(), Architecture.class.getName(), "(architecture.instance="+pid+")");
+
+ Assert.assertEquals("Is invalid ?", ComponentInstance.INVALID, arch.getInstanceDescription().getState());
+
+ // Reconfigure
+ props = new Properties();
+ props.put("prop", "OK");
+
+ try {
+ configuration.update(props);
+ } catch (IOException e) {
+ fail(e.getMessage());
+ }
+
+ pid = configuration.getPid();
+
+ // Wait for the processing of the first configuration.
+ try {
+ Thread.sleep(UPDATE_WAIT_TIME);
+ } catch (InterruptedException e1) {
+ fail(e1.getMessage());
+ }
+
+ Assert.assertNotNull("architecture", getContext().getServiceReferences(Architecture.class.getName(), "(architecture.instance="+pid+")"));
+ arch = (Architecture) Utils.getServiceObject(getContext(), Architecture.class.getName(), "(architecture.instance="+pid+")");
+
+ Assert.assertEquals("Is valid ?", ComponentInstance.VALID, arch.getInstanceDescription().getState());
+ }
}
diff --git a/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/util/Utils.java b/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/util/Utils.java
index 25d599e..8fa2fda 100644
--- a/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/util/Utils.java
+++ b/ipojo/tests/core/factories/src/main/java/org/apache/felix/ipojo/test/scenarios/util/Utils.java
@@ -1,4 +1,4 @@
-/*
+/*
* 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
@@ -158,7 +158,7 @@
return refs[0];
}
}
-
+
public static ServiceReference getServiceReferenceByPID(BundleContext bc, String itf, String pid) {
ServiceReference[] refs = null;
String filter = "(" + "service.pid" + "=" + pid + ")";
@@ -307,7 +307,7 @@
return new Object[0];
}
}
-
+
public static boolean contains(String string, String[] array) {
for (int i = 0; array != null && i < array.length; i++) {
if (array[i] != null && array[i].equals(string)) {
@@ -316,7 +316,7 @@
}
return false;
}
-
+
public static boolean contains(int value, int[] array) {
for (int i = 0; array != null && i < array.length; i++) {
if (array[i] == value) {
@@ -326,4 +326,25 @@
return false;
}
+ public static void waitForService(BundleContext context, String itf, String filter) {
+ ServiceReference[] refs = getServiceReferences(context, itf, filter);
+ int count = 0;
+ if (refs.length != 0) {
+ return;
+ } else {
+ while(refs.length == 0) {
+ try {
+ Thread.sleep(5);
+ } catch (InterruptedException e) {
+ // Interrupted
+ }
+ count++;
+ if (count == 100) {
+ throw new RuntimeException("Timeout ... no services match with " + filter);
+ }
+ refs = getServiceReferences(context, itf, filter);
+ }
+ }
+ }
+
}
diff --git a/ipojo/tests/core/factories/src/main/resources/metadata.xml b/ipojo/tests/core/factories/src/main/resources/metadata.xml
index 24be561..b59bd95 100644
--- a/ipojo/tests/core/factories/src/main/resources/metadata.xml
+++ b/ipojo/tests/core/factories/src/main/resources/metadata.xml
@@ -135,4 +135,12 @@
</properties>
<callback transition="validate" method="start"/>
</component>
+ <component classname="org.apache.felix.ipojo.test.scenarios.component.ReconfigurableSimpleType"
+ name="org.apache.felix.ipojo.test.scenarios.component.ReconfigurableSimpleType2"
+ architecture="true" immediate="true">
+ <properties>
+ <property name="prop" method="setProp"/>
+ </properties>
+ <controller field="controller"/>
+ </component>
</ipojo>