Skip to content

Commit

Permalink
Add test using the config admin and lifecycle controller
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1034701 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
cescoffier committed Nov 13, 2010
1 parent 279ebce commit 993435b
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 6 deletions.
6 changes: 6 additions & 0 deletions ipojo/tests/core/factories/pom.xml
Expand Up @@ -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>
Expand Down
Expand Up @@ -5,13 +5,24 @@ public class ReconfigurableSimpleType {

private String prop; // Property.


boolean controller;

public void start () {
if (prop == null || prop.equals("KO")) {
throw new IllegalStateException("Bad Configuration : " + prop);
}
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 !!!!");
}
}

}
@@ -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 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() {
public void testRevalidationOnReconfiguration() {
ComponentFactory factory = (ComponentFactory) Utils.getFactoryByName(getContext(),
"org.apache.felix.ipojo.test.scenarios.component.ReconfigurableSimpleType");

Expand Down Expand Up @@ -44,6 +90,154 @@ public void testRevalidationOnREconfiguration() {
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());
}
}
@@ -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
Expand Down Expand Up @@ -158,7 +158,7 @@ public static ServiceReference getServiceReferenceByName(BundleContext bc, Strin
return refs[0];
}
}

public static ServiceReference getServiceReferenceByPID(BundleContext bc, String itf, String pid) {
ServiceReference[] refs = null;
String filter = "(" + "service.pid" + "=" + pid + ")";
Expand Down Expand Up @@ -307,7 +307,7 @@ public static Object[] getServiceObjects(ServiceContext bc, String itf, String f
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)) {
Expand All @@ -316,7 +316,7 @@ public static boolean contains(String string, String[] array) {
}
return false;
}

public static boolean contains(int value, int[] array) {
for (int i = 0; array != null && i < array.length; i++) {
if (array[i] == value) {
Expand All @@ -326,4 +326,25 @@ public static boolean contains(int value, int[] array) {
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);
}
}
}

}
8 changes: 8 additions & 0 deletions ipojo/tests/core/factories/src/main/resources/metadata.xml
Expand Up @@ -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>

0 comments on commit 993435b

Please sign in to comment.