You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cl...@apache.org on 2009/04/15 15:21:30 UTC
svn commit: r765176 [1/2] - in /felix/trunk/ipojo: api/
api/src/main/java/org/apache/felix/ipojo/api/composite/
api/src/test/java/org/apache/felix/ipojo/api/composite/ tests/ tests/api/
tests/api/src/ tests/api/src/main/ tests/api/src/main/java/ tests/...
Author: clement
Date: Wed Apr 15 13:21:29 2009
New Revision: 765176
URL: http://svn.apache.org/viewvc?rev=765176&view=rev
Log:
Commit API test suite (composite)
Added:
felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/ExportedServiceTest.java (with props)
felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/ImportedServiceTest.java (with props)
felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/InstantiatedServiceTest.java (with props)
felix/trunk/ipojo/tests/api/ (with props)
felix/trunk/ipojo/tests/api/pom.xml (with props)
felix/trunk/ipojo/tests/api/src/
felix/trunk/ipojo/tests/api/src/main/
felix/trunk/ipojo/tests/api/src/main/java/
felix/trunk/ipojo/tests/api/src/main/java/org/
felix/trunk/ipojo/tests/api/src/main/java/org/apache/
felix/trunk/ipojo/tests/api/src/main/java/org/apache/felix/
felix/trunk/ipojo/tests/api/src/main/java/org/apache/felix/ipojo/
felix/trunk/ipojo/tests/api/src/main/java/org/apache/felix/ipojo/tests/
felix/trunk/ipojo/tests/api/src/main/java/org/apache/felix/ipojo/tests/api/
felix/trunk/ipojo/tests/api/src/test/
felix/trunk/ipojo/tests/api/src/test/java/
felix/trunk/ipojo/tests/api/src/test/java/org/
felix/trunk/ipojo/tests/api/src/test/java/org/apache/
felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/
felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/
felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/
felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/
felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/AppTest.java (with props)
felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/CompositeTest.java (with props)
felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/IPOJOHelper.java (with props)
felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/OSGiHelper.java (with props)
felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/PrimitiveComponentTest.java (with props)
felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/SingletonComponentTest.java (with props)
felix/trunk/ipojo/tests/api/src/test/java/org/example/
felix/trunk/ipojo/tests/api/src/test/java/org/example/service/
felix/trunk/ipojo/tests/api/src/test/java/org/example/service/BarService.java (with props)
felix/trunk/ipojo/tests/api/src/test/java/org/example/service/Foo.java (with props)
felix/trunk/ipojo/tests/api/src/test/java/org/example/service/MyService.java (with props)
felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/
felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/FooImpl.java (with props)
felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/MyComponentImpl.java (with props)
felix/trunk/ipojo/tests/api/src/test/java/org/example/service/impl/MyServiceImpl.java (with props)
Modified:
felix/trunk/ipojo/api/pom.xml
felix/trunk/ipojo/api/src/main/java/org/apache/felix/ipojo/api/composite/CompositeComponentType.java
felix/trunk/ipojo/tests/composite/import-export/src/main/resources/metadata.xml
felix/trunk/ipojo/tests/core/service-providing-strategies/src/main/java/org/apache/felix/ipojo/test/scenarios/component/strategies/DummyCreationStrategy.java
felix/trunk/ipojo/tests/pom.xml
Modified: felix/trunk/ipojo/api/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/api/pom.xml?rev=765176&r1=765175&r2=765176&view=diff
==============================================================================
--- felix/trunk/ipojo/api/pom.xml (original)
+++ felix/trunk/ipojo/api/pom.xml Wed Apr 15 13:21:29 2009
@@ -84,6 +84,7 @@
<configLocation>http://felix.apache.org/ipojo/dev/checkstyle_ipojo.xml</configLocation>
</configuration>
</plugin>
+
</plugins>
</build>
<dependencies>
Modified: felix/trunk/ipojo/api/src/main/java/org/apache/felix/ipojo/api/composite/CompositeComponentType.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/api/src/main/java/org/apache/felix/ipojo/api/composite/CompositeComponentType.java?rev=765176&r1=765175&r2=765176&view=diff
==============================================================================
--- felix/trunk/ipojo/api/src/main/java/org/apache/felix/ipojo/api/composite/CompositeComponentType.java (original)
+++ felix/trunk/ipojo/api/src/main/java/org/apache/felix/ipojo/api/composite/CompositeComponentType.java Wed Apr 15 13:21:29 2009
@@ -99,7 +99,7 @@
*/
private void ensureValidity() {
if (m_context == null) {
- throw new IllegalStateException("The primitive component type has no bundle context");
+ throw new IllegalStateException("The composite component type has no bundle context");
}
}
@@ -223,6 +223,14 @@
InstantiatedService inst = (InstantiatedService) m_instantiated.get(i);
element.addElement(inst.getElement());
}
+ for (int i = 0; i < m_exported.size(); i++) {
+ ExportedService inst = (ExportedService) m_exported.get(i);
+ element.addElement(inst.getElement());
+ }
+ for (int i = 0; i < m_provided.size(); i++) {
+ ProvidedService inst = (ProvidedService) m_provided.get(i);
+ element.addElement(inst.getElement());
+ }
return element;
}
Added: felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/ExportedServiceTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/ExportedServiceTest.java?rev=765176&view=auto
==============================================================================
--- felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/ExportedServiceTest.java (added)
+++ felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/ExportedServiceTest.java Wed Apr 15 13:21:29 2009
@@ -0,0 +1,119 @@
+package org.apache.felix.ipojo.api.composite;
+
+
+import junit.framework.TestCase;
+
+import org.apache.felix.ipojo.metadata.Element;
+
+public class ExportedServiceTest extends TestCase {
+
+
+
+//
+//<comp:composite name="composite.export.5" architecture="true">
+// <subservice action="import"
+// specification="org.apache.felix.ipojo.test.composite.service.BazService"
+// aggregate="true" optional="true" filter="(!(instance.name=export))"
+// scope="composite" />
+// <comp:provides action="export"
+// specification="org.apache.felix.ipojo.test.composite.service.BazService"
+// filter="(instance.name=foo1)" />
+//</comp:composite>
+
+// <comp:provides action="export"
+// specification="org.apache.felix.ipojo.test.composite.service.BazService" />
+ public void testSimple() {
+ ExportedService svc = new ExportedService()
+ .setSpecification("org.apache.felix.ipojo.test.composite.service.BazService");
+
+ Element elem = svc.getElement();
+ String name = elem.getName();
+ String action = elem.getAttribute("action");
+ String spec = elem.getAttribute("specification");
+ assertEquals("spec", "org.apache.felix.ipojo.test.composite.service.BazService", spec);
+ }
+
+ public void testBad() {
+ ExportedService svc = new ExportedService()
+ //.setSpecification("org.apache.felix.ipojo.test.composite.service.BarService") NO SPEC
+ ;
+ try {
+ svc.getElement();
+ fail("Invalid element accepted");
+ } catch (IllegalStateException e) {
+ // OK
+ }
+ }
+
+
+// <comp:provides action="export"
+// specification="org.apache.felix.ipojo.test.composite.service.BazService"
+// aggregate="true" />
+ public void testAggregate() {
+ ExportedService svc = new ExportedService()
+ .setSpecification("org.apache.felix.ipojo.test.composite.service.BazService")
+ .setAggregate(true);
+
+
+ Element elem = svc.getElement();
+ String spec = elem.getAttribute("specification");
+ String agg = elem.getAttribute("aggregate");
+
+ assertEquals("spec", "org.apache.felix.ipojo.test.composite.service.BazService", spec);
+ assertEquals("aggregate", "true", agg);
+ }
+
+// <comp:provides action="export"
+// specification="org.apache.felix.ipojo.test.composite.service.BazService"
+// optional="true" />
+ public void testOptional() {
+ ExportedService svc = new ExportedService()
+ .setSpecification("org.apache.felix.ipojo.test.composite.service.BazService")
+ .setOptional(true);
+
+
+ Element elem = svc.getElement();
+ String spec = elem.getAttribute("specification");
+ String opt = elem.getAttribute("optional");
+
+ assertEquals("spec", "org.apache.felix.ipojo.test.composite.service.BazService", spec);
+ assertEquals("optional", "true", opt);
+ }
+// <comp:provides action="export"
+// specification="org.apache.felix.ipojo.test.composite.service.BazService"
+// aggregate="true" optional="true" />
+ public void testOptionalAndAggregate() {
+ ExportedService svc = new ExportedService()
+ .setSpecification("org.apache.felix.ipojo.test.composite.service.FooService")
+ .setOptional(true)
+ .setAggregate(true);
+
+
+ Element elem = svc.getElement();
+ String spec = elem.getAttribute("specification");
+ String opt = elem.getAttribute("optional");
+ String agg = elem.getAttribute("aggregate");
+
+
+ assertEquals("spec", "org.apache.felix.ipojo.test.composite.service.FooService", spec);
+ assertEquals("optional", "true", opt);
+ assertEquals("aggregate", "true", agg);
+ }
+
+// <comp:provides action="export"
+// specification="org.apache.felix.ipojo.test.composite.service.BazService"
+// filter="(instance.name=foo1)" />
+ public void testFilter() {
+ ExportedService svc = new ExportedService()
+ .setSpecification("org.apache.felix.ipojo.test.composite.service.FooService")
+ .setFilter("(&(int=2)(long=40))");
+
+ Element elem = svc.getElement();
+ String spec = elem.getAttribute("specification");
+ String filter = elem.getAttribute("filter");
+
+ assertEquals("spec", "org.apache.felix.ipojo.test.composite.service.FooService", spec);
+ assertEquals("filter", "(&(int=2)(long=40))", filter);
+ }
+
+}
Propchange: felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/ExportedServiceTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/ImportedServiceTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/ImportedServiceTest.java?rev=765176&view=auto
==============================================================================
--- felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/ImportedServiceTest.java (added)
+++ felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/ImportedServiceTest.java Wed Apr 15 13:21:29 2009
@@ -0,0 +1,143 @@
+package org.apache.felix.ipojo.api.composite;
+
+
+import junit.framework.TestCase;
+
+import org.apache.felix.ipojo.metadata.Element;
+
+public class ImportedServiceTest extends TestCase {
+
+// <comp:composite name="composite.requires.1" architecture="true">
+// <subservice action="import"
+// specification="org.apache.felix.ipojo.test.composite.service.FooService"
+// scope="composite" />
+//</comp:composite>
+ public void testSimple() {
+ ImportedService svc = new ImportedService()
+ .setSpecification("org.apache.felix.ipojo.test.composite.service.FooService");
+
+ Element elem = svc.getElement();
+ String spec = elem.getAttribute("specification");
+ assertEquals("spec", "org.apache.felix.ipojo.test.composite.service.FooService", spec);
+ }
+
+ public void testBad() {
+ ImportedService svc = new ImportedService()
+ //.setSpecification("org.apache.felix.ipojo.test.composite.service.BarService") NO SPEC
+ ;
+ try {
+ svc.getElement();
+ fail("Invalid element accepted");
+ } catch (IllegalStateException e) {
+ // OK
+ }
+ }
+
+ public void testScope() {
+ ImportedService svc = new ImportedService()
+ .setSpecification("org.apache.felix.ipojo.test.composite.service.FooService")
+ .setScope(ImportedService.COMPOSITE_SCOPE);
+
+
+ Element elem = svc.getElement();
+ String spec = elem.getAttribute("specification");
+ String scope = elem.getAttribute("scope");
+ assertEquals("spec", "org.apache.felix.ipojo.test.composite.service.FooService", spec);
+ assertEquals("scope", "composite", scope);
+
+ }
+
+//
+//<comp:composite name="composite.requires.2" architecture="true">
+// <subservice action="import"
+// specification="org.apache.felix.ipojo.test.composite.service.FooService"
+// aggregate="true" scope="composite" />
+//</comp:composite>
+ public void testAggregate() {
+ ImportedService svc = new ImportedService()
+ .setSpecification("org.apache.felix.ipojo.test.composite.service.FooService")
+ .setScope(ImportedService.COMPOSITE_SCOPE)
+ .setAggregate(true);
+
+
+ Element elem = svc.getElement();
+ String spec = elem.getAttribute("specification");
+ String scope = elem.getAttribute("scope");
+ String agg = elem.getAttribute("aggregate");
+
+ assertEquals("spec", "org.apache.felix.ipojo.test.composite.service.FooService", spec);
+ assertEquals("scope", "composite", scope);
+ assertEquals("aggregate", "true", agg);
+ }
+
+//
+//<comp:composite name="composite.requires.3" architecture="true">
+// <subservice action="import"
+// specification="org.apache.felix.ipojo.test.composite.service.FooService"
+// optional="true" scope="composite" />
+//</comp:composite>
+ public void testOptional() {
+ ImportedService svc = new ImportedService()
+ .setSpecification("org.apache.felix.ipojo.test.composite.service.FooService")
+ .setScope(ImportedService.COMPOSITE_SCOPE)
+ .setOptional(true);
+
+
+ Element elem = svc.getElement();
+ String spec = elem.getAttribute("specification");
+ String scope = elem.getAttribute("scope");
+ String opt = elem.getAttribute("optional");
+
+ assertEquals("spec", "org.apache.felix.ipojo.test.composite.service.FooService", spec);
+ assertEquals("scope", "composite", scope);
+ assertEquals("optional", "true", opt);
+ }
+//
+//<comp:composite name="composite.requires.4" architecture="true">
+// <subservice action="import"
+// specification="org.apache.felix.ipojo.test.composite.service.FooService"
+// optional="true" aggregate="true" scope="comp:composite" />
+//</comp:composite>
+ public void testOptionalAndAggregate() {
+ ImportedService svc = new ImportedService()
+ .setSpecification("org.apache.felix.ipojo.test.composite.service.FooService")
+ .setScope(ImportedService.COMPOSITE_SCOPE)
+ .setOptional(true)
+ .setAggregate(true);
+
+
+ Element elem = svc.getElement();
+ String spec = elem.getAttribute("specification");
+ String scope = elem.getAttribute("scope");
+ String opt = elem.getAttribute("optional");
+ String agg = elem.getAttribute("aggregate");
+
+
+ assertEquals("spec", "org.apache.felix.ipojo.test.composite.service.FooService", spec);
+ assertEquals("scope", "composite", scope);
+ assertEquals("optional", "true", opt);
+ assertEquals("aggregate", "true", agg);
+ }
+//
+//<comp:composite name="composite.requires.5" architecture="true">
+// <subservice action="import"
+// specification="org.apache.felix.ipojo.test.composite.service.FooService"
+// filter="(&(int=2)(long=40))" scope="composite" />
+//</comp:composite>
+ public void testFilter() {
+ ImportedService svc = new ImportedService()
+ .setSpecification("org.apache.felix.ipojo.test.composite.service.FooService")
+ .setScope(ImportedService.COMPOSITE_SCOPE)
+ .setFilter("(&(int=2)(long=40))");
+
+ Element elem = svc.getElement();
+ String spec = elem.getAttribute("specification");
+ String scope = elem.getAttribute("scope");
+ String filter = elem.getAttribute("filter");
+
+ assertEquals("spec", "org.apache.felix.ipojo.test.composite.service.FooService", spec);
+ assertEquals("scope", "composite", scope);
+ assertEquals("filter", "(&(int=2)(long=40))", filter);
+ }
+
+}
Propchange: felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/ImportedServiceTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/InstantiatedServiceTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/InstantiatedServiceTest.java?rev=765176&view=auto
==============================================================================
--- felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/InstantiatedServiceTest.java (added)
+++ felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/InstantiatedServiceTest.java Wed Apr 15 13:21:29 2009
@@ -0,0 +1,170 @@
+package org.apache.felix.ipojo.api.composite;
+
+
+import junit.framework.TestCase;
+
+import org.apache.felix.ipojo.metadata.Element;
+
+public class InstantiatedServiceTest extends TestCase {
+
+// <comp:composite name="composite.bar.1" architecture="true">
+// <subservice action="instantiate" specification="org.apache.felix.ipojo.test.composite.service.BarService"/>
+//</comp:composite>
+
+ public void testSimple() {
+ InstantiatedService svc = new InstantiatedService()
+ .setSpecification("org.apache.felix.ipojo.test.composite.service.BarService");
+ Element elem = svc.getElement();
+ String spec = elem.getAttribute("specification");
+ String action = elem.getAttribute("action");
+
+ assertEquals("spec" , "org.apache.felix.ipojo.test.composite.service.BarService", spec);
+ assertEquals("action" , "instantiate", action);
+ }
+
+ public void testBad() {
+ InstantiatedService svc = new InstantiatedService()
+ //.setSpecification("org.apache.felix.ipojo.test.composite.service.BarService") NO SPEC
+ ;
+ try {
+ svc.getElement();
+ fail("Invalid element accepted");
+ } catch (IllegalStateException e) {
+ // OK
+ }
+ }
+
+
+
+
+//
+//<comp:composite name="composite.bar.2" architecture="true">
+// <subservice action="instantiate" specification="org.apache.felix.ipojo.test.composite.service.BarService" aggregate="true"/>
+//</comp:composite>
+
+ public void testAggregate() {
+ InstantiatedService svc = new InstantiatedService()
+ .setSpecification("org.apache.felix.ipojo.test.composite.service.BarService")
+ .setAggregate(true);
+ Element elem = svc.getElement();
+ String spec = elem.getAttribute("specification");
+ String action = elem.getAttribute("action");
+ String agg = elem.getAttribute("aggregate");
+
+
+ assertEquals("spec" , "org.apache.felix.ipojo.test.composite.service.BarService", spec);
+ assertEquals("action" , "instantiate", action);
+ assertEquals("aggregate" , "true", agg);
+
+ }
+
+//
+//<comp:composite name="composite.bar.3" architecture="true">
+// <subservice action="instantiate" specification="org.apache.felix.ipojo.test.composite.service.BarService" optional="true"/>
+//</comp:composite>
+ public void testOptional() {
+ InstantiatedService svc = new InstantiatedService()
+ .setSpecification("org.apache.felix.ipojo.test.composite.service.BarService")
+ .setOptional(true);
+ Element elem = svc.getElement();
+ String spec = elem.getAttribute("specification");
+ String action = elem.getAttribute("action");
+ String agg = elem.getAttribute("aggregate");
+ String opt = elem.getAttribute("optional");
+
+ assertEquals("spec" , "org.apache.felix.ipojo.test.composite.service.BarService", spec);
+ assertEquals("action" , "instantiate", action);
+ assertNull("aggregate" , agg);
+ assertEquals("optional" , "true", opt);
+ }
+
+//
+//<comp:composite name="composite.bar.4" architecture="true">
+// <subservice action="instantiate" specification="org.apache.felix.ipojo.test.composite.service.FooService" aggregate="true" optional="true"/>
+//</comp:composite>
+ public void testOptionalAndAggregate() {
+ InstantiatedService svc = new InstantiatedService()
+ .setSpecification("org.apache.felix.ipojo.test.composite.service.FooService")
+ .setOptional(true)
+ .setAggregate(true);
+ Element elem = svc.getElement();
+ String spec = elem.getAttribute("specification");
+ String action = elem.getAttribute("action");
+ String agg = elem.getAttribute("aggregate");
+ String opt = elem.getAttribute("optional");
+
+ assertEquals("spec" , "org.apache.felix.ipojo.test.composite.service.FooService", spec);
+ assertEquals("action" , "instantiate", action);
+ assertEquals("aggregate" , "true", agg);
+ assertEquals("optional" , "true", opt);
+ }
+
+//
+//<comp:composite name="composite.bar.5-accept" architecture="true">
+// <subservice action="instantiate" specification="org.apache.felix.ipojo.test.composite.service.FooService">
+// <property name="boolean" value="true"/>
+// <property name="string" value="foo"/>
+// <property name="strAprop" value="{foo, bar, baz}"/>
+// <property name="int" value="5"/>
+// </subservice>
+//</comp:composite>
+ public void testWithConfiguration() {
+ InstantiatedService svc = new InstantiatedService()
+ .setSpecification("org.apache.felix.ipojo.test.composite.service.FooService")
+ .addProperty("boolean", "true")
+ .addProperty("string", "foo")
+ .addProperty("stringAprop", new String[] {"foo", "bar", "baz" })
+ .addProperty("int", "5");
+ Element elem = svc.getElement();
+ String spec = elem.getAttribute("specification");
+ String action = elem.getAttribute("action");
+ String agg = elem.getAttribute("aggregate");
+ String opt = elem.getAttribute("optional");
+
+ assertEquals("spec" , "org.apache.felix.ipojo.test.composite.service.FooService", spec);
+ assertEquals("action" , "instantiate", action);
+ assertNull("aggregate" , agg);
+ assertNull("optional" , opt);
+
+ Element[] props = elem.getElements("property");
+ assertEquals("Number of properties", 4, props.length);
+ String n0 = props[0].getAttribute("name");
+ String v0 = props[0].getAttribute("value");
+ assertEquals("n0" , "boolean", n0);
+ assertEquals("v0" , "true", v0);
+
+ String n1 = props[1].getAttribute("name");
+ String v1 = props[1].getAttribute("value");
+ assertEquals("n1" , "string", n1);
+ assertEquals("v1" , "foo", v1);
+
+ String n2 = props[2].getAttribute("name");
+ String v2 = props[2].getAttribute("value");
+ String t2 = props[2].getAttribute("type");
+ Element[] sub = props[2].getElements();
+ assertEquals("Number of sub-properties", 3, sub.length);
+ assertEquals("n2", "stringAprop", n2);
+ assertNull("v2", v2);
+ assertEquals("t2", "array", t2);
+
+ String n20 = sub[0].getAttribute("name");
+ String v20 = sub[0].getAttribute("value");
+ assertNull("n20" , n20);
+ assertEquals("v20" , "foo", v20);
+ String n21 = sub[1].getAttribute("name");
+ String v21 = sub[1].getAttribute("value");
+ assertNull("n21" , n21);
+ assertEquals("v21" , "bar", v21);
+ String n22 = sub[2].getAttribute("name");
+ String v22 = sub[2].getAttribute("value");
+ assertNull("n22" , n22);
+ assertEquals("v22" , "baz", v22);
+
+ String n3 = props[3].getAttribute("name");
+ String v3 = props[3].getAttribute("value");
+ assertEquals("n3" , "int", n3);
+ assertEquals("v3" , "5", v3);
+ }
+
+
+}
Propchange: felix/trunk/ipojo/api/src/test/java/org/apache/felix/ipojo/api/composite/InstantiatedServiceTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Propchange: felix/trunk/ipojo/tests/api/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Wed Apr 15 13:21:29 2009
@@ -0,0 +1,7 @@
+.settings
+
+target
+
+.classpath
+
+.project
Added: felix/trunk/ipojo/tests/api/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/api/pom.xml?rev=765176&view=auto
==============================================================================
--- felix/trunk/ipojo/tests/api/pom.xml (added)
+++ felix/trunk/ipojo/tests/api/pom.xml Wed Apr 15 13:21:29 2009
@@ -0,0 +1,114 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>ipojo.tests</groupId>
+ <artifactId>org.apache.felix.ipojo.tests.api</artifactId>
+ <packaging>jar</packaging>
+ <version>1.3.0-SNAPSHOT</version>
+ <name>org.apache.felix.ipojo.tests.api</name>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ <!-- <plugin>
+ <groupId>org.ops4j.pax.exam</groupId>
+ <artifactId>maven-paxexam-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>generate-paxexam-config</id>
+ <goals>
+ <goal>generate-paxexam-config</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <settings>
+ <platform>felix</platform>
+ </settings>
+ </configuration>
+ </plugin> -->
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.ipojo</artifactId>
+ <version>1.3.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.ipojo.api</artifactId>
+ <version>1.3.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.ipojo.composite</artifactId>
+ <version>1.3.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.ipojo.manipulator</artifactId>
+ <version>1.3.0-SNAPSHOT</version>
+ </dependency>
+
+
+ <!--
+ Pax Exam API:
+ -->
+ <dependency>
+ <groupId>org.ops4j.pax.exam</groupId>
+ <artifactId>pax-exam</artifactId>
+ <version>0.4.0</version>
+ </dependency>
+ <!--
+ During runtime Pax Exam will discover the OSGi container to use by
+ searching metadata available into classpath. Pax Exam comes with a
+ default container that uses [Pax Runner] for implementing the
+ container requirements:
+ -->
+ <dependency>
+ <groupId>org.ops4j.pax.exam</groupId>
+ <artifactId>pax-exam-container-default
+ </artifactId>
+ <version>0.4.0</version>
+ </dependency>
+ <!--
+ If your test code is based on JUnit you will have to have the Junit
+ support artifact:
+ -->
+ <dependency>
+ <groupId>org.ops4j.pax.exam</groupId>
+ <artifactId>pax-exam-junit</artifactId>
+ <version>0.4.0</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.5</version>
+ <type>jar</type>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <repositories>
+ <repository>
+ <id>ops4j.releases</id>
+ <name>OPS4J Release</name>
+ <url> http://repository.ops4j.org/maven2/</url>
+ <releases>
+ <enabled>true</enabled>
+ </releases>
+ <snapshots>
+ <enabled>false</enabled>
+ </snapshots>
+ </repository>
+ </repositories>
+</project>
Propchange: felix/trunk/ipojo/tests/api/pom.xml
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/AppTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/AppTest.java?rev=765176&view=auto
==============================================================================
--- felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/AppTest.java (added)
+++ felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/AppTest.java Wed Apr 15 13:21:29 2009
@@ -0,0 +1,38 @@
+package org.apache.felix.ipojo.tests.api;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit test for simple App.
+ */
+public class AppTest
+ extends TestCase
+{
+ /**
+ * Create the test case
+ *
+ * @param testName name of the test case
+ */
+ public AppTest( String testName )
+ {
+ super( testName );
+ }
+
+ /**
+ * @return the suite of tests being tested
+ */
+ public static Test suite()
+ {
+ return new TestSuite( AppTest.class );
+ }
+
+ /**
+ * Rigourous Test :-)
+ */
+ public void testApp()
+ {
+ assertTrue( true );
+ }
+}
Propchange: felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/AppTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/CompositeTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/CompositeTest.java?rev=765176&view=auto
==============================================================================
--- felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/CompositeTest.java (added)
+++ felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/CompositeTest.java Wed Apr 15 13:21:29 2009
@@ -0,0 +1,285 @@
+package org.apache.felix.ipojo.tests.api;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.junit.Assert.assertThat;
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.CoreOptions.options;
+import static org.ops4j.pax.exam.CoreOptions.provision;
+
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.ConfigurationException;
+import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.MissingHandlerException;
+import org.apache.felix.ipojo.UnacceptableConfiguration;
+import org.apache.felix.ipojo.api.Dependency;
+import org.apache.felix.ipojo.api.PrimitiveComponentType;
+import org.apache.felix.ipojo.api.Service;
+import org.apache.felix.ipojo.api.composite.CompositeComponentType;
+import org.apache.felix.ipojo.api.composite.ExportedService;
+import org.apache.felix.ipojo.api.composite.ImportedService;
+import org.apache.felix.ipojo.api.composite.Instance;
+import org.apache.felix.ipojo.api.composite.InstantiatedService;
+import org.example.service.Foo;
+import org.example.service.impl.FooImpl;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Inject;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.Configuration;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
+
+
+@RunWith( JUnit4TestRunner.class )
+public class CompositeTest {
+
+ @Inject
+ private BundleContext context;
+
+ private OSGiHelper osgi;
+
+ private IPOJOHelper ipojo;
+
+ @Before
+ public void init() {
+ osgi = new OSGiHelper(context);
+ ipojo = new IPOJOHelper(context);
+ }
+
+ @After
+ public void stop() {
+ ipojo.dispose();
+ osgi.dispose();
+ }
+
+ @Configuration
+ public static Option[] configure() {
+ Option[] opt = options(
+ provision(
+ mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.ipojo").version("1.3.0-SNAPSHOT"),
+ mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.ipojo.composite").version("1.3.0-SNAPSHOT"),
+ mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.ipojo.api").version("1.3.0-SNAPSHOT")
+ )
+ );
+ return opt;
+ }
+
+ @Test
+ public void createACompositeWithcontainedInstance() throws UnacceptableConfiguration, MissingHandlerException, ConfigurationException {
+ // Define the component types
+ PrimitiveComponentType prov = createAProvider();
+ PrimitiveComponentType cons = createAConsumer();
+
+ CompositeComponentType type = new CompositeComponentType()
+ .setBundleContext(context)
+ .setComponentTypeName("comp1")
+ .addInstance(new Instance(prov.getFactory().getName()))
+ .addInstance(new Instance(cons.getFactory().getName()));
+
+ ComponentInstance ci = type.createInstance();
+
+ assertThat("ci is valid", ci.getState(), is(ComponentInstance.VALID));
+
+ // Stop cons
+ cons.stop();
+ assertThat("ci is invalid", ci.getState(), is(ComponentInstance.INVALID));
+
+ // Restart cons
+ cons.start();
+ assertThat("ci is valid - 2", ci.getState(), is(ComponentInstance.VALID));
+
+ }
+
+ @Test
+ public void createACompositeWithAnInstantiatedService() throws UnacceptableConfiguration, MissingHandlerException, ConfigurationException {
+ // Define the component types
+ PrimitiveComponentType prov = createAProvider();
+ prov.start();
+ PrimitiveComponentType cons = createAConsumer();
+
+ ServiceReference[] refs = osgi.getServiceReferences(Factory.class.getName(),
+ "(component.providedServiceSpecifications=" + Foo.class.getName() +")");
+ assertThat(refs.length, is(not(0)));
+
+ Factory factory = (Factory) osgi.getServiceObject(refs[0]);
+ System.out.println(factory.getComponentDescription().getDescription());
+
+ CompositeComponentType type = new CompositeComponentType()
+ .setBundleContext(context)
+ .setComponentTypeName("comp2")
+ .addSubService(new InstantiatedService().setSpecification(Foo.class.getName()))
+ .addInstance(new Instance(cons.getFactory().getName()));
+
+ ComponentInstance ci = type.createInstance();
+
+ System.out.println(ci.getInstanceDescription().getDescription());
+
+ assertThat("ci is valid", ci.getState(), is(ComponentInstance.VALID));
+
+ // Stop prov
+ prov.stop();
+ assertThat("ci is invalid", ci.getState(), is(ComponentInstance.INVALID));
+
+ // Restart prov
+ prov.start();
+ assertThat("ci is valid - 2", ci.getState(), is(ComponentInstance.VALID));
+
+ }
+
+ @Test
+ public void createACompositeWithAnOptionalInstantiatedService() throws UnacceptableConfiguration, MissingHandlerException, ConfigurationException {
+ // Define the component types
+ PrimitiveComponentType prov = createAProvider();
+ prov.start();
+
+ CompositeComponentType type = new CompositeComponentType()
+ .setBundleContext(context)
+ .setComponentTypeName("comp3")
+ .addSubService(new InstantiatedService().setSpecification(Foo.class.getName()).setOptional(true));
+
+ ComponentInstance ci = type.createInstance();
+
+ System.out.println(ci.getInstanceDescription().getDescription());
+
+ assertThat("ci is valid", ci.getState(), is(ComponentInstance.VALID));
+
+ // Stop prov
+ prov.stop();
+ assertThat("ci is valid - 1", ci.getState(), is(ComponentInstance.VALID));
+
+ // Restart prov
+ prov.start();
+ assertThat("ci is valid - 2", ci.getState(), is(ComponentInstance.VALID));
+
+ }
+
+ @Test
+ public void createACompositeWithAnImportedService() throws UnacceptableConfiguration, MissingHandlerException, ConfigurationException {
+ // Define the component types
+ PrimitiveComponentType prov = createAProvider();
+ prov.createInstance();
+ PrimitiveComponentType cons = createAConsumer();
+
+ ServiceReference[] refs = osgi.getServiceReferences(Factory.class.getName(),
+ "(component.providedServiceSpecifications=" + Foo.class.getName() +")");
+ assertThat(refs.length, is(not(0)));
+
+ CompositeComponentType type = new CompositeComponentType()
+ .setBundleContext(context)
+ .setComponentTypeName("comp2")
+ .addSubService(new ImportedService().setSpecification(Foo.class.getName()))
+ .addInstance(new Instance(cons.getFactory().getName()));
+
+ ComponentInstance ci = type.createInstance();
+
+ System.out.println(ci.getInstanceDescription().getDescription());
+
+ assertThat("ci is valid", ci.getState(), is(ComponentInstance.VALID));
+
+ // Stop prov
+ prov.stop();
+ assertThat("ci is invalid", ci.getState(), is(ComponentInstance.INVALID));
+
+ // Restart prov
+ prov.start();
+ prov.createInstance();
+ assertThat("ci is valid - 2", ci.getState(), is(ComponentInstance.VALID));
+
+ }
+
+ @Test
+ public void createACompositeWithAnOptionalImportedService() throws UnacceptableConfiguration, MissingHandlerException, ConfigurationException {
+ // Define the component types
+ PrimitiveComponentType prov = createAProvider();
+ prov.createInstance();
+
+ CompositeComponentType type = new CompositeComponentType()
+ .setBundleContext(context)
+ .setComponentTypeName("comp3")
+ .addSubService(new ImportedService().setSpecification(Foo.class.getName()).setOptional(true));
+
+ ComponentInstance ci = type.createInstance();
+
+ System.out.println(ci.getInstanceDescription().getDescription());
+
+ assertThat("ci is valid", ci.getState(), is(ComponentInstance.VALID));
+
+ // Stop prov
+ prov.stop();
+ assertThat("ci is valid - 1", ci.getState(), is(ComponentInstance.VALID));
+
+ // Restart prov
+ prov.start();
+ prov.createInstance();
+ assertThat("ci is valid - 2", ci.getState(), is(ComponentInstance.VALID));
+
+ }
+
+ @Test
+ public void createACompositeWithExportingAService() throws UnacceptableConfiguration, MissingHandlerException, ConfigurationException {
+ // Define the component types
+ PrimitiveComponentType prov = createAProvider();
+ prov.start();
+ PrimitiveComponentType cons = createAConsumer();
+ ComponentInstance c = cons.createInstance();
+
+ CompositeComponentType type = new CompositeComponentType()
+ .setBundleContext(context)
+ .setComponentTypeName("compExport")
+ .addSubService(new InstantiatedService().setSpecification(Foo.class.getName()))
+ .addService(new ExportedService().setSpecification(Foo.class.getName()));
+
+ ComponentInstance ci = type.createInstance();
+
+ System.out.println(ci.getInstanceDescription().getDescription());
+
+ assertThat("ci is valid", ci.getState(), is(ComponentInstance.VALID));
+ assertThat("c is valid", c.getState(), is(ComponentInstance.VALID));
+
+
+ // Stop prov
+ prov.stop();
+ assertThat("ci is invalid", ci.getState(), is(ComponentInstance.INVALID));
+ assertThat("c is invalid", c.getState(), is(ComponentInstance.INVALID));
+
+
+ // Restart prov
+ prov.start();
+ assertThat("ci is valid - 2", ci.getState(), is(ComponentInstance.VALID));
+ assertThat("c is valid - 2", c.getState(), is(ComponentInstance.VALID));
+
+
+ }
+
+ private PrimitiveComponentType createAProvider() {
+ return new PrimitiveComponentType()
+ .setBundleContext(context)
+ .setClassName(FooImpl.class.getName())
+ .setPublic(true)
+ .addService(new Service()); // Provide the FooService
+ }
+
+ private PrimitiveComponentType createAConsumer() {
+ return new PrimitiveComponentType()
+ .setBundleContext(context)
+ .setClassName(org.example.service.impl.MyComponentImpl.class.getName())
+ .addDependency(new Dependency().setField("myFoo"))
+ .setValidateMethod("start");
+ }
+
+ private PrimitiveComponentType createAnOptionalConsumer() {
+ return new PrimitiveComponentType()
+ .setBundleContext(context)
+ .setClassName(org.example.service.impl.MyComponentImpl.class.getName())
+ .addDependency(new Dependency().setField("myFoo").setOptional(true))
+ .setValidateMethod("start");
+ }
+
+
+
+}
Propchange: felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/CompositeTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/IPOJOHelper.java
URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/IPOJOHelper.java?rev=765176&view=auto
==============================================================================
--- felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/IPOJOHelper.java (added)
+++ felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/IPOJOHelper.java Wed Apr 15 13:21:29 2009
@@ -0,0 +1,730 @@
+/*
+ * 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.ipojo.tests.api;
+
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.Handler;
+import org.apache.felix.ipojo.HandlerFactory;
+import org.apache.felix.ipojo.ServiceContext;
+import org.apache.felix.ipojo.architecture.Architecture;
+import org.apache.felix.ipojo.metadata.Element;
+import org.apache.felix.ipojo.parser.ManifestMetadataParser;
+import org.apache.felix.ipojo.parser.ParseException;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.ManagedServiceFactory;
+
+/**
+ * iPOJO Helper.
+ * This helper helps getting {@link Factory}, and managing
+ * {@link ComponentInstance}.
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public class IPOJOHelper {
+
+ /**
+ * The bundle context.
+ */
+ private BundleContext m_context;
+
+
+ /**
+ * List of instances.
+ */
+ private List<ComponentInstance> m_instances;
+
+ /**
+ * Creates a IPOJOHelper.
+ * @param tc the OSGi Test Case
+ */
+ public IPOJOHelper(BundleContext context) {
+ m_context = context;
+ m_instances = new ArrayList<ComponentInstance>();
+ }
+
+ /**
+ * Disposes created instances.
+ * @see org.apache.felix.ipojo.junit4osgi.Helper#dispose()
+ */
+ public void dispose() {
+ for (int i = 0; i < m_instances.size(); i++) {
+ ((ComponentInstance) m_instances.get(i)).dispose();
+ }
+ m_instances.clear();
+ }
+
+ /**
+ * Gets a created instance from the instance name.
+ * @param name the instance name.
+ * @return the created {@link ComponentInstance} or <code>null</code>
+ * if the instance was not created during the session.
+ */
+ public ComponentInstance getInstanceByName(String name) {
+ for (int i = 0; i < m_instances.size(); i++) {
+ if (((ComponentInstance) m_instances.get(i)).getInstanceName()
+ .equals(name)) {
+ return (ComponentInstance) m_instances.get(i);
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Creates a new component instance with the given name (and empty
+ * configuration), from the factory specified in the given bundle.
+ *
+ * @param bundle the bundle from which the component factory is defined.
+ * @param factoryName the name of the component factory, defined in the
+ * specified bundle.
+ * @param instanceName the name of the component instance to create.
+ * @return the newly created component instance.
+ */
+ public static ComponentInstance createComponentInstance(Bundle bundle,
+ String factoryName, String instanceName) {
+
+ // Create the instance configuration
+ Properties configuration = new Properties();
+ configuration.put("instance.name", instanceName);
+
+ return createComponentInstance(bundle, factoryName, configuration);
+ }
+
+ /**
+ * Creates a new component instance with the given configuration, from the
+ * factory specified in the given bundle.
+ *
+ * @param bundle the bundle from which the component factory is defined.
+ * @param factoryName the name of the component factory, defined in the
+ * specified bundle.
+ * @param configuration the configuration of the component instance to
+ * create.
+ * @return the newly created component instance.
+ */
+ public static ComponentInstance createComponentInstance(Bundle bundle,
+ String factoryName, Dictionary configuration) {
+
+ // Retrieve the component factory.
+ Factory fact = getFactory(bundle, factoryName);
+
+ if (fact == null) {
+ // Factory not found...
+ throw new IllegalArgumentException(
+ "Cannot find the component factory (" + factoryName
+ + ") in the specified bundle ("
+ + bundle.getSymbolicName() + ").");
+ }
+
+ try {
+ return fact.createComponentInstance(configuration);
+ } catch (Exception e) {
+ throw new IllegalArgumentException(
+ "Cannot create the component instance with the given configuration:"
+ + e.getMessage());
+ }
+ }
+
+ /**
+ * Creates a new component instance with the given name and configuration,
+ * from the factory specified in the given bundle.
+ *
+ * @param bundle the bundle from which the component factory is defined.
+ * @param factoryName the name of the component factory, defined in the
+ * specified bundle.
+ * @param instanceName the name of the component instance to create.
+ * @param configuration the configuration of the instance to create.
+ * @return the newly created component instance.
+ */
+ public static ComponentInstance createComponentInstance(Bundle bundle,
+ String factoryName, String instanceName, Dictionary configuration) {
+
+ // Add the instance name to the configuration
+ configuration.put("instance.name", instanceName);
+
+ return createComponentInstance(bundle, factoryName, configuration);
+ }
+
+ /**
+ * Creates a new component instance with the given name (and an empty
+ * configuration), from the factory specified in the given service context.
+ *
+ * @param serviceContext the service context in which the component factory
+ * service is registered.
+ * @param factoryName the name of the component factory, defined in the
+ * specified service context.
+ * @param instanceName the name of the component instance to create.
+ * @return the newly created component instance.
+ */
+ public static ComponentInstance createComponentInstance(
+ ServiceContext serviceContext, String factoryName,
+ String instanceName) {
+
+ // Create the instance configuration
+ Properties configuration = new Properties();
+ configuration.put("instance.name", instanceName);
+
+ return createComponentInstance(serviceContext, factoryName,
+ configuration);
+ }
+
+ /**
+ * Creates a new component instance with the given name and configuration,
+ * from the factory specified in the given service context.
+ *
+ * @param serviceContext the service context in which the component factory
+ * service is registered.
+ * @param factoryName the name of the component factory, defined in the
+ * specified service context.
+ * @param configuration the configuration of the instance to create.
+ * @return the newly created component instance.
+ */
+ public static ComponentInstance createComponentInstance(
+ ServiceContext serviceContext, String factoryName,
+ Dictionary configuration) {
+
+ // Retrieve the component factory.
+ Factory fact = getFactory(serviceContext, factoryName);
+
+ if (fact == null) {
+ // Factory not found...
+ throw new IllegalArgumentException(
+ "Cannot find the component factory (" + factoryName
+ + ") in the specified service context.");
+ }
+
+ try {
+ return fact.createComponentInstance(configuration);
+ } catch (Exception e) {
+ throw new IllegalArgumentException(
+ "Cannot create the component instance with the given configuration: "
+ + e.getMessage());
+ }
+ }
+
+ /**
+ * Creates a new component instance with the given name and configuration,
+ * from the factory specified in the given service context.
+ *
+ * @param serviceContext the service context in which the component factory
+ * service is registered.
+ * @param factoryName the name of the component factory, defined in the
+ * specified service context.
+ * @param instanceName the name of the component instance to create.
+ * @param configuration the configuration of the instance to create.
+ * @return the newly created component instance.
+ */
+ public static ComponentInstance createComponentInstance(
+ ServiceContext serviceContext, String factoryName,
+ String instanceName, Dictionary configuration) {
+
+ // Add the instance name to the configuration
+ configuration.put("instance.name", instanceName);
+
+ return createComponentInstance(serviceContext, factoryName,
+ configuration);
+ }
+
+ /**
+ * Creates a new component instance with the given name (and empty
+ * configuration), from the factory specified in the local bundle.
+ *
+ * @param factoryName the name of the component factory, defined in the
+ * local bundle.
+ * @param instanceName the name of the component instance to create.
+ * @return the newly created component instance.
+ */
+ public ComponentInstance createComponentInstance(String factoryName,
+ String instanceName) {
+ ComponentInstance ci = createComponentInstance(m_context.getBundle(),
+ factoryName, instanceName);
+ m_instances.add(ci);
+ return ci;
+ }
+
+ /**
+ * Creates a new component instance with the given configuration, from the
+ * factory specified in the local bundle.
+ *
+ * @param factoryName the name of the component factory, in the local
+ * bundle.
+ * @param configuration the configuration of the component instance to
+ * create.
+ * @return the newly created component instance.
+ */
+ public ComponentInstance createComponentInstance(String factoryName,
+ Dictionary configuration) {
+ ComponentInstance ci = createComponentInstance(m_context.getBundle(),
+ factoryName, configuration);
+ m_instances.add(ci);
+ return ci;
+ }
+
+ /**
+ * Creates a new component instance with no configuration, from the factory
+ * specified in the local bundle.
+ *
+ * @param factoryName the name of the component factory, in the local
+ * bundle.
+ * @return the newly created component instance.
+ */
+ public ComponentInstance createComponentInstance(String factoryName) {
+ ComponentInstance ci = createComponentInstance(m_context.getBundle(),
+ factoryName, (Dictionary) null);
+ m_instances.add(ci);
+ return ci;
+ }
+
+ /**
+ * Creates a new component instance with the given name and configuration,
+ * from the factory specified in the given bundle.
+ *
+ * @param factoryName the name of the component factory, defined in the
+ * specified bundle.
+ * @param instanceName the name of the component instance to create.
+ * @param configuration the configuration of the instance to create.
+ * @return the newly created component instance.
+ */
+ public ComponentInstance createComponentInstance(String factoryName,
+ String instanceName, Dictionary configuration) {
+ ComponentInstance ci = createComponentInstance(m_context.getBundle(),
+ factoryName, instanceName, configuration);
+ m_instances.add(ci);
+ return ci;
+ }
+
+ /**
+ * Returns the component factory with the given name in the local bundle.
+ *
+ * @param factoryName the name of the factory to retrieve.
+ * @return the component factory with the given name in the local bundle, or
+ * {@code null} if not found.
+ */
+ public Factory getFactory(String factoryName) {
+ return getFactory(m_context.getBundle(), factoryName);
+ }
+
+ /**
+ * Returns the handler factory with the given name in the local bundle.
+ *
+ * @param factoryName the name of the handler factory to retrieve.
+ * @return the handler factory with the given name in the local bundle, or
+ * {@code null} if not found.
+ */
+ public HandlerFactory getHandlerFactory(String factoryName) {
+ return getHandlerFactory(m_context.getBundle(), factoryName);
+ }
+
+ /**
+ * Returns the metadata description of the component defined in this bundle.
+ *
+ * @param component the name of the locally defined component.
+ * @return the metadata description of the component with the given name,
+ * defined in this given bundle, or {@code null} if not found.
+ */
+ public Element getMetadata(String component) {
+ return getMetadata(m_context.getBundle(), component);
+ }
+
+ /**
+ * Returns the component factory with the given name in the given bundle.
+ *
+ * @param bundle the bundle from which the component factory is defined.
+ * @param factoryName the name of the defined factory.
+ * @return the component factory with the given name in the given bundle, or
+ * {@code null} if not found.
+ */
+ public static Factory getFactory(Bundle bundle, String factoryName) {
+ ServiceReference[] refs;
+ try {
+ // Retrieves the component factories services in the bundle.
+ refs = bundle.getBundleContext().getServiceReferences(
+ Factory.class.getName(),
+ "(factory.name=" + factoryName + ")");
+ if (refs != null) {
+ return (Factory) bundle.getBundleContext().getService(refs[0]);
+ }
+
+ // Factory not found...
+ return null;
+
+ } catch (InvalidSyntaxException e) {
+ throw new IllegalArgumentException(
+ "Cannot get the component factory services: "
+ + e.getMessage());
+ }
+ }
+
+ /**
+ * Returns the component factory with the given name, registered in the
+ * given service context.
+ *
+ * @param serviceContext the service context in which the factory service is
+ * defined.
+ * @param factoryName the name of the factory.
+ * @return the component factory with the given name, registered in the
+ * given service context.
+ */
+ public static Factory getFactory(ServiceContext serviceContext,
+ String factoryName) {
+ ServiceReference[] refs;
+ try {
+ // Retrieves the component factories services in the service
+ // context.
+ refs = serviceContext.getServiceReferences(Factory.class.getName(),
+ "(factory.name=" + factoryName + ")");
+ if (refs != null) {
+ return (Factory) serviceContext.getService(refs[0]);
+ }
+ return null;
+
+ } catch (InvalidSyntaxException e) {
+ System.err.println("Cannot get the factory " + factoryName + " : "
+ + e.getMessage());
+ return null;
+ }
+ }
+
+ /**
+ * Returns the handler factory with the given name in the given bundle.
+ *
+ * @param bundle the bundle from which the handler factory is defined.
+ * @param factoryName the name of the handler factory to retrieve.
+ * @return the handler factory with the given name in the given bundle, or
+ * {@code null} if not found.
+ */
+ public static HandlerFactory getHandlerFactory(Bundle bundle,
+ String factoryName) {
+ ServiceReference[] refs;
+ try {
+ // Retrieves the handler factories services in the bundle.
+ refs = bundle.getBundleContext().getServiceReferences(
+ HandlerFactory.class.getName(),
+ "(" + Handler.HANDLER_NAME_PROPERTY + "=" + factoryName
+ + ")");
+ if (refs != null) {
+ return (HandlerFactory) bundle.getBundleContext().getService(
+ refs[0]);
+ }
+
+ // Factory not found...
+ return null;
+ } catch (InvalidSyntaxException e) {
+ throw new IllegalArgumentException(
+ "Cannot get the handler factory services: "
+ + e.getMessage());
+ }
+ }
+
+ /**
+ * Returns the metadata description of the component with the given name,
+ * defined in the given bundle.
+ *
+ * @param bundle the bundle from which the component is defined.
+ * @param component the name of the defined component.
+ * @return the metadata description of the component with the given name,
+ * defined in the given bundle, or {@code null} if not found.
+ */
+ public static Element getMetadata(Bundle bundle, String component) {
+
+ // Retrieves the component description from the bundle's manifest.
+ String elem = (String) bundle.getHeaders().get("iPOJO-Components");
+ if (elem == null) {
+ throw new IllegalArgumentException(
+ "Cannot find iPOJO-Components descriptor in the specified bundle ("
+ + bundle.getSymbolicName()
+ + "). Not an iPOJO bundle.");
+ }
+
+ // Parses the retrieved description and find the component with the
+ // given name.
+ try {
+ Element element = ManifestMetadataParser.parseHeaderMetadata(elem);
+ Element[] childs = element.getElements("component");
+ for (int i = 0; i < childs.length; i++) {
+ String name = childs[i].getAttribute("name");
+ String clazz = childs[i].getAttribute("classname");
+ if (name != null && name.equalsIgnoreCase(component)) {
+ return childs[i];
+ }
+ if (clazz.equalsIgnoreCase(component)) {
+ return childs[i];
+ }
+ }
+
+ // Component not found...
+ return null;
+
+ } catch (ParseException e) {
+ throw new IllegalStateException(
+ "Cannot parse the components from specified bundle ("
+ + bundle.getSymbolicName() + "): " + e.getMessage());
+ }
+ }
+
+ /**
+ * Returns the service object of a service registered in the specified
+ * service context, offering the specified interface and matching the given
+ * filter.
+ *
+ * @param serviceContext the service context in which the service is
+ * searched.
+ * @param itf the interface provided by the searched service.
+ * @param filter an additional filter (can be {@code null}).
+ * @return the service object provided by the specified bundle, offering the
+ * specified interface and matching the given filter.
+ */
+ public static Object getServiceObject(ServiceContext serviceContext,
+ String itf, String filter) {
+ ServiceReference ref = getServiceReference(serviceContext, itf, filter);
+ if (ref != null) {
+ return serviceContext.getService(ref);
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Returns the service objects of the services registered in the specified
+ * service context, offering the specified interface and matching the given
+ * filter.
+ *
+ * @param serviceContext the service context in which services are searched.
+ * @param itf the interface provided by the searched services.
+ * @param filter an additional filter (can be {@code null}).
+ * @return the service objects provided by the specified bundle, offering
+ * the specified interface and matching the given filter.
+ */
+ public static Object[] getServiceObjects(ServiceContext serviceContext,
+ String itf, String filter) {
+ ServiceReference[] refs = getServiceReferences(serviceContext, itf,
+ filter);
+ if (refs != null) {
+ Object[] list = new Object[refs.length];
+ for (int i = 0; i < refs.length; i++) {
+ list[i] = serviceContext.getService(refs[i]);
+ }
+ return list;
+ } else {
+ return new Object[0];
+ }
+ }
+
+ /**
+ * Returns the service reference of a service registered in the specified
+ * service context, offering the specified interface and matching the given
+ * filter.
+ *
+ * @param serviceContext the service context in which services are searched.
+ * @param itf the interface provided by the searched service.
+ * @param filter an additional filter (can be {@code null}).
+ * @return a service reference registered in the specified service context,
+ * offering the specified interface and matching the given filter.
+ * If no service is found, {@code null} is returned.
+ */
+ public static ServiceReference getServiceReference(
+ ServiceContext serviceContext, String itf, String filter) {
+ ServiceReference[] refs = getServiceReferences(serviceContext, itf,
+ filter);
+ if (refs.length != 0) {
+ return refs[0];
+ } else {
+ // No service found
+ return null;
+ }
+ }
+
+ /**
+ * Returns the service reference of the service registered in the specified
+ * service context, offering the specified interface and having the given
+ * persistent ID.
+ *
+ * @param serviceContext the service context in which services are searched.
+ * @param itf the interface provided by the searched service.
+ * @param pid the persistent ID of the searched service.
+ * @return a service registered in the specified service context, offering
+ * the specified interface and having the given persistent ID.
+ */
+ public static ServiceReference getServiceReferenceByPID(
+ ServiceContext serviceContext, String itf, String pid) {
+ String filter = "(" + "service.pid" + "=" + pid + ")";
+ ServiceReference[] refs = getServiceReferences(serviceContext, itf,
+ filter);
+ if (refs == null) {
+ return null;
+ } else if (refs.length == 1) {
+ return refs[0];
+ } else {
+ throw new IllegalStateException(
+ "A service lookup by PID returned several providers ("
+ + refs.length + ")" + " for " + itf + " with pid="
+ + pid);
+ }
+ }
+
+ /**
+ * Returns the service reference of all the services registered in the
+ * specified service context, offering the specified interface and matching
+ * the given filter.
+ *
+ * @param serviceContext the service context in which services are searched.
+ * @param itf the interface provided by the searched services.
+ * @param filter an additional filter (can be {@code null}).
+ * @return all the service references registered in the specified service
+ * context, offering the specified interface and matching the given
+ * filter. If no service matches, an empty array is returned.
+ */
+ public static ServiceReference[] getServiceReferences(
+ ServiceContext serviceContext, String itf, String filter) {
+ ServiceReference[] refs = null;
+ try {
+ // Get all the service references
+ refs = serviceContext.getServiceReferences(itf, filter);
+ } catch (InvalidSyntaxException e) {
+ throw new IllegalArgumentException(
+ "Cannot get service references: " + e.getMessage());
+ }
+ if (refs == null) {
+ return new ServiceReference[0];
+ } else {
+ return refs;
+ }
+ }
+
+ /**
+ * Returns the service reference of a service registered in the specified
+ * service context, offering the specified interface and having the given
+ * name.
+ *
+ * @param serviceContext the service context in which services are searched.
+ * @param itf the interface provided by the searched service.
+ * @param name the name of the searched service.
+ * @return a service registered in the specified service context, offering
+ * the specified interface and having the given name.
+ */
+ public static ServiceReference getServiceReferenceByName(
+ ServiceContext serviceContext, String itf, String name) {
+ String filter = null;
+ if (itf.equals(Factory.class.getName())
+ || itf.equals(ManagedServiceFactory.class.getName())) {
+ filter = "(" + "factory.name" + "=" + name + ")";
+ } else if (itf.equals(Architecture.class.getName())) {
+ filter = "(" + "architecture.instance" + "=" + name + ")";
+ } else {
+ filter = "(" + "instance.name" + "=" + name + ")";
+ }
+ return getServiceReference(serviceContext, itf, filter);
+ }
+
+ /**
+ * Checks the availability of a service inside the given service context.
+ * @param sc the service context
+ * @param itf the service interface to found
+ * @return <code>true</code> if the service is available in the service
+ * context, <code>false</code> otherwise.
+ */
+ public static boolean isServiceAvailable(ServiceContext sc, String itf) {
+ ServiceReference ref = getServiceReference(sc, itf, null);
+ return ref != null;
+ }
+
+ /**
+ * Checks the availability of a service inside the given service context.
+ * @param sc the service context
+ * @param itf the service interface to found
+ * @param name the service provider name
+ * @return <code>true</code> if the service is available in the service
+ * context, <code>false</code> otherwise.
+ */
+ public static boolean isServiceAvailableByName(ServiceContext sc,
+ String itf, String name) {
+ ServiceReference ref = getServiceReferenceByName(sc, itf, name);
+ return ref != null;
+ }
+
+ /**
+ * Checks the availability of a service inside the given service context.
+ * @param sc the service context
+ * @param itf the service interface to found
+ * @param pid the pid of the service
+ * @return <code>true</code> if the service is available in the service
+ * context, <code>false</code> otherwise.
+ */
+ public static boolean isServiceAvailableByPID(ServiceContext sc,
+ String itf, String pid) {
+ ServiceReference ref = getServiceReferenceByPID(sc, itf, pid);
+ return ref != null;
+ }
+
+ /**
+ * Returns the service reference of a service provided by the specified
+ * bundle, offering the specified interface and having the given name.
+ *
+ * @param bundle the bundle from which the service is searched.
+ * @param itf the interface provided by the searched service.
+ * @param name the name of the searched service.
+ * @return a service provided by the specified bundle, offering the
+ * specified interface and having the given name.
+ */
+ public static ServiceReference getServiceReferenceByName(Bundle bundle,
+ String itf, String name) {
+ String filter = null;
+ if (itf.equals(Factory.class.getName())
+ || itf.equals(ManagedServiceFactory.class.getName())) {
+ filter = "(" + "factory.name" + "=" + name + ")";
+ } else if (itf.equals(Architecture.class.getName())) {
+ filter = "(" + "architecture.instance" + "=" + name + ")";
+ } else {
+ filter = "(" + "instance.name" + "=" + name + ")";
+ }
+ return OSGiHelper.getServiceReference(bundle, itf, filter);
+ }
+
+ /**
+ * Returns the service reference of a service provided by the local bundle,
+ * offering the specified interface and having the given name.
+ *
+ * @param itf the interface provided by the searched service.
+ * @param name the name of the searched service.
+ * @return a service provided by the specified bundle, offering the
+ * specified interface and having the given name.
+ */
+ public ServiceReference getServiceReferenceByName(String itf, String name) {
+ return getServiceReferenceByName(m_context.getBundle(), itf, name);
+ }
+
+ /**
+ * Checks if the service is available.
+ * @param itf the service interface
+ * @param name the service provider name
+ * @return <code>true</code> if the service is available, <code>false</code>
+ * otherwise.
+ */
+ public boolean isServiceAvailableByName(String itf, String name) {
+ ServiceReference ref = getServiceReferenceByName(itf, name);
+ return ref != null;
+ }
+
+}
Propchange: felix/trunk/ipojo/tests/api/src/test/java/org/apache/felix/ipojo/tests/api/IPOJOHelper.java
------------------------------------------------------------------------------
svn:mime-type = text/plain