Split the iPOJO test suite in several bundles.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@651646 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckProviderParentClass.java b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckProviderParentClass.java
new file mode 100644
index 0000000..7f7f37d
--- /dev/null
+++ b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckProviderParentClass.java
@@ -0,0 +1,51 @@
+/*
+ * 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.test.scenarios.component;
+
+import org.apache.felix.ipojo.test.scenarios.factories.service.FooService;
+import org.osgi.framework.ServiceReference;
+
+public abstract class CheckProviderParentClass {
+
+ int simpleU = 0;
+ int objectU = 0;
+ int refU = 0;
+ int bothU = 0;
+
+
+ public void bothUnbind(FooService o, ServiceReference sr) {
+ if(sr != null && o != null && o instanceof FooService) { bothU++; }
+ }
+
+ public void refUnbind(ServiceReference sr) {
+ if(sr != null) { refU++; }
+ }
+
+ public void objectUnbind(FooService o) {
+ if(o != null && o instanceof FooService) { objectU++; }
+ else {
+ System.err.println("Unbind null : " + o);
+ }
+ }
+
+ public void voidUnbind() {
+ simpleU++;
+ }
+
+}
diff --git a/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckServiceProvider.java b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckServiceProvider.java
new file mode 100644
index 0000000..fdaf7c1
--- /dev/null
+++ b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/CheckServiceProvider.java
@@ -0,0 +1,83 @@
+/*
+ * 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.test.scenarios.component;
+
+import java.util.Properties;
+
+import org.apache.felix.ipojo.test.scenarios.factories.service.CheckService;
+import org.apache.felix.ipojo.test.scenarios.factories.service.FooService;
+import org.osgi.framework.ServiceReference;
+
+public class CheckServiceProvider extends CheckProviderParentClass implements CheckService {
+
+ FooService fs;
+
+ int simpleB = 0;
+ int objectB = 0;
+ int refB = 0;
+ int bothB = 0;
+
+ public boolean check() {
+ return fs.foo();
+ }
+
+ public Properties getProps() {
+ Properties props = new Properties();
+ props.put("voidB", new Integer(simpleB));
+ props.put("objectB", new Integer(objectB));
+ props.put("refB", new Integer(refB));
+ props.put("bothB", new Integer(bothB));
+ props.put("voidU", new Integer(simpleU));
+ props.put("objectU", new Integer(objectU));
+ props.put("refU", new Integer(refU));
+ props.put("bothU", new Integer(bothU));
+ if (fs != null) {
+ props.put("result", new Boolean(fs.foo()));
+ props.put("boolean", new Boolean(fs.getBoolean()));
+ props.put("int", new Integer(fs.getInt()));
+ props.put("long", new Long(fs.getLong()));
+ props.put("double", new Double(fs.getDouble()));
+ if(fs.getObject() != null) { props.put("object", fs.getObject()); }
+ }
+ props.put("static", CheckService.foo);
+ props.put("class", CheckService.class.getName());
+ return props;
+ }
+
+ private void voidBind() {
+ simpleB++;
+ }
+
+ protected void objectBind(FooService o) {
+ if (o == null) {
+ System.err.println("Bind receive null !!! ");
+ return;
+ }
+ if(o != null && o instanceof FooService) { objectB++; }
+ }
+
+ public void refBind(ServiceReference sr) {
+ if(sr != null) { refB++; }
+ }
+
+ public void bothBind(FooService o, ServiceReference sr) {
+ if(sr != null && o != null && o instanceof FooService) { bothB++; }
+ }
+
+}
diff --git a/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/FooBarProviderType1.java b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/FooBarProviderType1.java
new file mode 100644
index 0000000..419da2e
--- /dev/null
+++ b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/FooBarProviderType1.java
@@ -0,0 +1,54 @@
+/*
+ * 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.test.scenarios.component;
+
+import java.util.Properties;
+
+import org.apache.felix.ipojo.test.scenarios.factories.service.BarService;
+import org.apache.felix.ipojo.test.scenarios.factories.service.FooService;
+
+public class FooBarProviderType1 implements FooService, BarService {
+
+ public boolean foo() {
+ return true;
+ }
+
+ public Properties fooProps() {
+ return new Properties();
+ }
+
+ public boolean bar() {
+ return true;
+ }
+
+ public Properties getProps() {
+ return new Properties();
+ }
+
+ public boolean getBoolean() { return true; }
+
+ public double getDouble() { return 1.0; }
+
+ public int getInt() { return 1; }
+
+ public long getLong() { return 1; }
+
+ public Boolean getObject() { return new Boolean(true); }
+
+}
diff --git a/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/FooProviderType1.java b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/FooProviderType1.java
new file mode 100644
index 0000000..d1217a1
--- /dev/null
+++ b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/FooProviderType1.java
@@ -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.ipojo.test.scenarios.component;
+
+import java.util.Properties;
+
+import org.apache.felix.ipojo.test.scenarios.factories.service.FooService;
+import org.osgi.framework.BundleContext;
+
+public class FooProviderType1 implements FooService {
+
+ private int m_bar;
+ private String m_foo;
+
+ private BundleContext m_context;
+
+ private static FooProviderType1 singleton;
+ private static int count = 0;
+
+ private static FooProviderType1 singleton(BundleContext bc) {
+ if (singleton == null) {
+ count++;
+ singleton = new FooProviderType1(bc);
+ }
+ return singleton;
+ }
+
+ public static FooProviderType1 several(BundleContext bc) {
+ count++;
+ return new FooProviderType1(bc);
+ }
+
+ public FooProviderType1(BundleContext bc) {
+ if (bc ==null) {
+ throw new RuntimeException("Injected bundle context null");
+ }
+ m_context = bc;
+ }
+
+ public boolean foo() {
+ return true;
+ }
+
+ public Properties fooProps() {
+ Properties p = new Properties();
+ p.put("bar", new Integer(m_bar));
+ if(m_foo != null) {
+ p.put("foo", m_foo);
+ }
+ p.put("context", m_context);
+
+ p.put("count", new Integer(count));
+ return p;
+ }
+
+ public void testException() throws Exception {
+ String a = "foobarbaz";
+ throw new Exception("foo"+a);
+ }
+
+ public void testTry() {
+ String a = "foo";
+ a.charAt(0);
+ }
+
+ public void testTry2(String s) {
+ String a = "foo";
+ a.charAt(0);
+ }
+
+ private void nexttry(String s) {
+ try {
+ s += "foo";
+ } catch(RuntimeException e) {
+
+ }
+ }
+
+ public boolean getBoolean() { return true; }
+
+ public double getDouble() { return 1.0; }
+
+ public int getInt() { return 1; }
+
+ public long getLong() { return 1; }
+
+ public Boolean getObject() { return new Boolean(true); }
+
+ /**
+ * Custom constructor.
+ * @param bar
+ * @param foo
+ * @param bc
+ */
+ public FooProviderType1(int bar, String foo, BundleContext bc) {
+ m_bar = bar;
+ m_foo = foo;
+ m_context = bc;
+ }
+
+}
diff --git a/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/FooProviderTypeDyn.java b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/FooProviderTypeDyn.java
new file mode 100644
index 0000000..77a1583
--- /dev/null
+++ b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/FooProviderTypeDyn.java
@@ -0,0 +1,64 @@
+/*
+ * 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.test.scenarios.component;
+
+import java.util.Properties;
+
+import org.apache.felix.ipojo.test.scenarios.factories.service.FooService;
+
+
+public class FooProviderTypeDyn implements FooService {
+
+ private int intProp;
+ private String strProp;
+ private String[] strAProp;
+ private int[] intAProp;
+ private boolean boolProp;
+
+ public boolean foo() {
+ intProp = 3;
+ boolProp = true;
+ if(strProp.equals("foo")) { strProp = "bar"; }
+ else { strProp = "foo"; }
+ strAProp = new String[] {"foo", "bar", "baz"};
+ intAProp = new int[] {3, 2, 1};
+ return true;
+ }
+
+ public Properties fooProps() {
+ Properties p = new Properties();
+ p.put("intProp", new Integer(intProp));
+ p.put("boolProp", new Boolean(boolProp));
+ p.put("strProp", strProp);
+ p.put("strAProp", strAProp);
+ p.put("intAProp", intAProp);
+ return p;
+ }
+
+ public boolean getBoolean() { return true; }
+
+ public double getDouble() { return 1.0; }
+
+ public int getInt() { return 1; }
+
+ public long getLong() { return 1; }
+
+ public Boolean getObject() { return new Boolean(true); }
+
+}
diff --git a/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/FooProviderTypeDyn2.java b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/FooProviderTypeDyn2.java
new file mode 100644
index 0000000..8cc7d12
--- /dev/null
+++ b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/component/FooProviderTypeDyn2.java
@@ -0,0 +1,58 @@
+/*
+ * 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.test.scenarios.component;
+
+import java.util.Properties;
+
+import org.apache.felix.ipojo.test.scenarios.factories.service.FooService;
+
+public class FooProviderTypeDyn2 implements FooService {
+
+ private int intProp = 2;
+ private boolean boolProp = true;
+ private String strProp = "foo";
+ private String[] strAProp = new String[] {"foo", "bar"};
+ private int[] intAProp = new int[] {1, 2, 3};
+
+ public boolean foo() {
+ intAProp = null;
+ return true;
+ }
+
+ public Properties fooProps() {
+ Properties p = new Properties();
+ p.put("intProp", new Integer(intProp));
+ p.put("boolProp", new Boolean(boolProp));
+ p.put("strProp", strProp);
+ p.put("strAProp", strAProp);
+ p.put("intAProp", intAProp);
+ return p;
+ }
+
+ public boolean getBoolean() { return true; }
+
+ public double getDouble() { return 1.0; }
+
+ public int getInt() { return 1; }
+
+ public long getLong() { return 1; }
+
+ public Boolean getObject() { return new Boolean(true); }
+
+}
diff --git a/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/ComponentDesc.java b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/ComponentDesc.java
new file mode 100644
index 0000000..41dbcc2
--- /dev/null
+++ b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/ComponentDesc.java
@@ -0,0 +1,363 @@
+/*
+ * 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.test.scenarios.factories;
+
+import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.architecture.ComponentTypeDescription;
+import org.apache.felix.ipojo.architecture.PropertyDescription;
+import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
+import org.apache.felix.ipojo.metadata.Element;
+import org.apache.felix.ipojo.test.scenarios.factories.service.BarService;
+import org.apache.felix.ipojo.test.scenarios.factories.service.FooService;
+import org.apache.felix.ipojo.test.scenarios.util.Utils;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * Check component type description.
+ */
+public class ComponentDesc extends OSGiTestCase {
+
+ ServiceReference sr_fooProvider1;
+ ServiceReference sr_fooProvider2;
+ ServiceReference sr_fooProviderDyn2;
+ ServiceReference sr_fooProvider3;
+ ServiceReference sr_foobarProvider;
+// ServiceReference sr_simple;
+// ServiceReference sr_optional;
+// ServiceReference sr_multiple;
+// ServiceReference sr_multiple_optional;
+
+ Factory fooProvider1;
+ Factory fooProvider2;
+ Factory fooProviderDyn2;
+ Factory fooProvider3;
+ Factory foobarProvider;
+// Factory simple;
+// Factory optional;
+// Factory multiple;
+// Factory multiple_optional;
+
+ public void setUp() {
+ sr_fooProvider1 = Utils.getServiceReferenceByName(context, Factory.class.getName(), "Factories-FooProviderType-1");
+ sr_fooProvider2 = Utils.getServiceReferenceByName(context, Factory.class.getName(), "Factories-FooProviderType-2");
+ sr_fooProviderDyn2 = Utils.getServiceReferenceByName(context, Factory.class.getName(), "Factories-FooProviderType-Dyn2");
+ sr_fooProvider3 = Utils.getServiceReferenceByName(context, Factory.class.getName(), "Factories-FooProviderType-3");
+ sr_foobarProvider = Utils.getServiceReferenceByName(context, Factory.class.getName(), "Factories-FooBarProviderType-1");
+// sr_simple = Utils.getServiceReferenceByName(context, Factory.class.getName(), "SimpleCheckServiceProvider");
+// sr_optional = Utils.getServiceReferenceByName(context, Factory.class.getName(), "SimpleOptionalCheckServiceProvider");
+// sr_multiple = Utils.getServiceReferenceByName(context, Factory.class.getName(), "SimpleMultipleCheckServiceProvider");
+// sr_multiple_optional = Utils.getServiceReferenceByName(context, Factory.class.getName(), "SimpleOptionalMultipleCheckServiceProvider");
+
+ fooProvider1 = (Factory) context.getService(sr_fooProvider1);
+ fooProvider2 = (Factory) context.getService(sr_fooProvider2);
+ fooProviderDyn2 = (Factory) context.getService(sr_fooProviderDyn2);
+ fooProvider3 = (Factory) context.getService(sr_fooProvider3);
+ foobarProvider = (Factory) context.getService(sr_foobarProvider);
+// simple = (Factory) context.getService(sr_simple);
+// optional = (Factory) context.getService(sr_optional);
+// multiple = (Factory) context.getService(sr_multiple);
+// multiple_optional = (Factory) context.getService(sr_multiple_optional);
+
+ }
+
+ public void tearDown() {
+ fooProvider1 = null;
+ fooProvider2 = null;
+ fooProviderDyn2 = null;
+ fooProvider3 = null;
+ foobarProvider = null;
+// simple = null;
+// multiple = null;
+// optional = null;
+// multiple_optional = null;
+
+ context.ungetService(sr_fooProvider1);
+ context.ungetService(sr_fooProvider2);
+ context.ungetService(sr_fooProviderDyn2);
+ context.ungetService(sr_fooProvider3);
+ context.ungetService(sr_foobarProvider);
+// context.ungetService(sr_simple);
+// context.ungetService(sr_optional);
+// context.ungetService(sr_multiple);
+// context.ungetService(sr_multiple_optional);
+ }
+
+ /**
+ * Check simple providing.
+ */
+ public void testFooProvider1() {
+ // Test SR properties
+// String impl = (String) sr_fooProvider1.getProperty("component.class");
+// assertEquals("Check component.class", impl, "org.apache.felix.ipojo.test.scenarios.component.FooProviderType1");
+
+ String[] specs = (String[]) sr_fooProvider1.getProperty("component.providedServiceSpecifications");
+ assertEquals("Check component.providedServiceSpecifications length", specs.length, 1);
+ assertEquals("Check component.providedServiceSpecifications", FooService.class.getName(), specs[0]);
+
+ PropertyDescription[] pd = (PropertyDescription[]) sr_fooProvider1.getProperty("component.properties");
+ assertEquals("Check component.properties length", pd.length, 0);
+
+ // Test factory
+ assertEquals("Check factory name", fooProvider1.getName(), "Factories-FooProviderType-1");
+ Element cd = fooProvider1.getDescription();
+
+// assertEquals("Check implementation class ", cd.getAttribute("implementation-class"), impl);
+
+ Element[] specs2 = cd.getElements("provides");
+ assertEquals("Check specs length", specs2.length, 1);
+ assertEquals("Check specs", FooService.class.getName(), specs2[0].getAttribute("specification"));
+
+ Element[] pd2 = cd.getElements("property");
+ assertNull("Check props null", pd2);
+
+ // Check Description equality
+ ComponentTypeDescription desc = (ComponentTypeDescription) sr_fooProvider1.getProperty("component.description");
+ assertNotNull("check description equality", desc);
+ }
+
+ /**
+ * Check component properties.
+ */
+ public void testFooProvider2() {
+ // Test SR properties
+// String impl = (String) sr_fooProvider2.getProperty("component.class");
+// assertEquals("Check component.class", impl, "org.apache.felix.ipojo.test.scenarios.component.FooProviderType1");
+
+ String[] specs = (String[]) sr_fooProvider2.getProperty("component.providedServiceSpecifications");
+ assertEquals("Check component.providedServiceSpecifications length", specs.length, 1);
+ assertEquals("Check component.providedServiceSpecifications", FooService.class.getName(), specs[0]);
+
+ PropertyDescription[] pd = (PropertyDescription[]) sr_fooProvider2.getProperty("component.properties");
+ assertEquals("Check component.properties length", pd.length, 5);
+
+ assertEquals("Check component.properties name [" + 0 + "]", "int", pd[0].getName());
+ assertEquals("Check component.properties type [" + 0 + "]", "int", pd[0].getType());
+ assertEquals("Check component.properties value [" + 0 + "]", "2", pd[0].getValue());
+
+ assertEquals("Check component.properties name [" + 1 + "]", "long", pd[1].getName());
+ assertEquals("Check component.properties type [" + 1 + "]", "long", pd[1].getType());
+ assertEquals("Check component.properties value [" + 1 + "]", "40", pd[1].getValue());
+
+ assertEquals("Check component.properties name [" + 2 + "]", "string", pd[2].getName());
+ assertEquals("Check component.properties type [" + 2 + "]", "java.lang.String", pd[2].getType());
+ assertEquals("Check component.properties value [" + 2 + "]", "foo", pd[2].getValue());
+
+ assertEquals("Check component.properties name [" + 3 + "]", "strAProp", pd[3].getName());
+ assertEquals("Check component.properties type [" + 3 + "]", "java.lang.String[]", pd[3].getType());
+
+ assertEquals("Check component.properties name [" + 4 + "]", "intAProp", pd[4].getName());
+ assertEquals("Check component.properties type [" + 4 + "]", "int[]", pd[4].getType());
+
+ // Test factory
+ assertEquals("Check factory name", fooProvider2.getName(), "Factories-FooProviderType-2");
+ Element cd = fooProvider2.getDescription();
+
+// assertEquals("Check implementation class ", cd.getAttribute("implementation-class"), impl);
+
+ Element[] specs2 = cd.getElements("provides");
+ assertEquals("Check specs length", specs2.length, 1);
+ assertEquals("Check specs", FooService.class.getName(), specs2[0].getAttribute("specification"));
+
+ Element[] pd2 = cd.getElements("property");
+ assertEquals("Check props length", pd2.length, 5);
+
+ assertEquals("Check component.properties name [" + 0 + "]", "int", pd2[0].getAttribute("name"));
+ assertEquals("Check component.properties type [" + 0 + "]", "int", pd2[0].getAttribute("type"));
+ assertEquals("Check component.properties value [" + 0 + "]", "2", pd2[0].getAttribute("value"));
+
+ assertEquals("Check component.properties name [" + 1 + "]", "long", pd2[1].getAttribute("name"));
+ assertEquals("Check component.properties type [" + 1 + "]", "long", pd2[1].getAttribute("type"));
+ assertEquals("Check component.properties value [" + 1 + "]", "40", pd2[1].getAttribute("value"));
+
+ assertEquals("Check component.properties name [" + 2 + "]", "string", pd2[2].getAttribute("name"));
+ assertEquals("Check component.properties type [" + 2 + "]", "java.lang.String", pd2[2].getAttribute("type"));
+ assertEquals("Check component.properties value [" + 2 + "]", "foo", pd2[2].getAttribute("value"));
+
+ assertEquals("Check component.properties name [" + 3 + "]", "strAProp", pd2[3].getAttribute("name"));
+ assertEquals("Check component.properties type [" + 3 + "]", "java.lang.String[]", pd2[3].getAttribute("type"));
+
+ assertEquals("Check component.properties name [" + 4 + "]", "intAProp", pd2[4].getAttribute("name"));
+ assertEquals("Check component.properties type [" + 4 + "]", "int[]", pd2[4].getAttribute("type"));
+
+ // Check Description equality
+ ComponentTypeDescription desc = (ComponentTypeDescription) sr_fooProvider2.getProperty("component.description");
+ assertNotNull("check description equality", desc);
+ }
+
+ /**
+ * Check component properties (dynamic).
+ */
+ public void testFooProviderDyn2() {
+ // Test SR properties
+// String impl = (String) sr_fooProviderDyn2.getProperty("component.class");
+// assertEquals("Check component.class", impl, "org.apache.felix.ipojo.test.scenarios.component.FooProviderTypeDyn2");
+
+ String[] specs = (String[]) sr_fooProviderDyn2.getProperty("component.providedServiceSpecifications");
+ assertEquals("Check component.providedServiceSpecifications length", specs.length, 1);
+ assertEquals("Check component.providedServiceSpecifications", FooService.class.getName(), specs[0]);
+
+ PropertyDescription[] pd = (PropertyDescription[]) sr_fooProviderDyn2.getProperty("component.properties");
+ assertEquals("Check component.properties length", pd.length, 5);
+
+ assertEquals("Check component.properties name [" + 0 + "]", "int", pd[0].getName());
+ assertEquals("Check component.properties type [" + 0 + "]", "int", pd[0].getType());
+ assertEquals("Check component.properties value [" + 0 + "]", "4", pd[0].getValue());
+
+ assertEquals("Check component.properties name [" + 1 + "]", "boolean", pd[1].getName());
+ assertEquals("Check component.properties type [" + 1 + "]", "boolean", pd[1].getType());
+
+ assertEquals("Check component.properties name [" + 2 + "]", "string", pd[2].getName());
+ assertEquals("Check component.properties type [" + 2 + "]", "java.lang.String", pd[2].getType());
+
+ assertEquals("Check component.properties name [" + 3 + "]", "strAProp", pd[3].getName());
+ assertEquals("Check component.properties type [" + 3 + "]", "java.lang.String[]", pd[3].getType());
+
+ assertEquals("Check component.properties name [" + 4 + "]", "intAProp", pd[4].getName());
+ assertEquals("Check component.properties type [" + 4 + "]", "int[]", pd[4].getType());
+
+ // Test factory
+ assertEquals("Check factory name", fooProviderDyn2.getName(), "Factories-FooProviderType-Dyn2");
+ Element cd = fooProviderDyn2.getDescription();
+
+// assertEquals("Check implementation class ", cd.getAttribute("implementation-class"), impl);
+
+ Element[] specs2 = cd.getElements("provides");
+ assertEquals("Check specs length", specs2.length, 1);
+ assertEquals("Check specs", FooService.class.getName(), specs2[0].getAttribute("specification"));
+
+ Element[] pd2 = cd.getElements("property");
+ assertEquals("Check props length", pd2.length, 5);
+
+ assertEquals("Check component.properties name [" + 0 + "]", "int", pd2[0].getAttribute("name"));
+ assertEquals("Check component.properties type [" + 0 + "]", "int", pd2[0].getAttribute("type"));
+ assertEquals("Check component.properties value [" + 0 + "]", "4", pd2[0].getAttribute("value"));
+
+ assertEquals("Check component.properties name [" + 1 + "]", "boolean", pd2[1].getAttribute("name"));
+ assertEquals("Check component.properties type [" + 1 + "]", "boolean", pd2[1].getAttribute("type"));
+
+ assertEquals("Check component.properties name [" + 2 + "]", "string", pd2[2].getAttribute("name"));
+ assertEquals("Check component.properties type [" + 2 + "]", "java.lang.String", pd2[2].getAttribute("type"));
+
+ assertEquals("Check component.properties name [" + 3 + "]", "strAProp", pd2[3].getAttribute("name"));
+ assertEquals("Check component.properties type [" + 3 + "]", "java.lang.String[]", pd2[3].getAttribute("type"));
+
+ assertEquals("Check component.properties name [" + 4 + "]", "intAProp", pd2[4].getAttribute("name"));
+ assertEquals("Check component.properties type [" + 4 + "]", "int[]", pd2[4].getAttribute("type"));
+
+ // Check Description equality
+ ComponentTypeDescription desc = (ComponentTypeDescription) sr_fooProviderDyn2.getProperty("component.description");
+ assertNotNull("check description equality", desc);
+ }
+
+ /**
+ * Check component properties.
+ */
+ public void testFooProvider3() {
+ // Test SR properties
+// String impl = (String) sr_fooProvider3.getProperty("component.class");
+// assertEquals("Check component.class", impl, "org.apache.felix.ipojo.test.scenarios.component.FooProviderType1");
+
+ String[] specs = (String[]) sr_fooProvider3.getProperty("component.providedServiceSpecifications");
+ assertEquals("Check component.providedServiceSpecifications length", specs.length, 1);
+ assertEquals("Check component.providedServiceSpecifications", FooService.class.getName(), specs[0]);
+
+ PropertyDescription[] pd = (PropertyDescription[]) sr_fooProvider3.getProperty("component.properties");
+ assertEquals("Check component.properties length (" + pd.length +")", pd.length, 3);
+
+ assertEquals("Check component.properties name [" + 0 + "]", "foo", pd[0].getName());
+
+ assertEquals("Check component.properties name [" + 1 + "]", "bar", pd[1].getName());
+
+ assertEquals("Check component.properties name [" + 2 + "]", "baz", pd[2].getName());
+ assertEquals("Check component.properties type [" + 2 + "]", "java.lang.String", pd[2].getType());
+
+ // Test factory
+ assertEquals("Check factory name", fooProvider3.getName(), "Factories-FooProviderType-3");
+ Element cd = fooProvider3.getDescription();
+
+// assertEquals("Check implementation class ", cd.getAttribute("implementation-class"), impl);
+
+ Element[] specs2 = cd.getElements("provides");
+ assertEquals("Check specs length", specs2.length, 1);
+ assertEquals("Check specs", FooService.class.getName(), specs2[0].getAttribute("specification"));
+
+ Element[] pd2 = cd.getElements("property");
+ assertEquals("Check props length", pd2.length, 3);
+
+ assertEquals("Check component.properties name [" + 0 + "]", "foo", pd2[0].getAttribute("name"));
+
+ assertEquals("Check component.properties name [" + 1 + "]", "bar", pd2[1].getAttribute("name"));
+
+ assertEquals("Check component.properties name [" + 2 + "]", "baz", pd2[2].getAttribute("name"));
+ assertEquals("Check component.properties type [" + 2 + "]", "java.lang.String", pd2[2].getAttribute("type"));
+
+ // Check Description equality
+ ComponentTypeDescription desc = (ComponentTypeDescription) sr_fooProvider3.getProperty("component.description");
+ assertNotNull("check description equality", desc);
+ }
+
+ /**
+ * Test two services provider.
+ */
+ public void testFooBar() {
+ // Test SR properties
+// String impl = (String) sr_foobarProvider.getProperty("component.class");
+// assertEquals("Check component.class", impl, "org.apache.felix.ipojo.test.scenarios.component.FooBarProviderType1");
+
+ String[] specs = (String[]) sr_foobarProvider.getProperty("component.providedServiceSpecifications");
+ assertEquals("Check component.providedServiceSpecifications length", specs.length, 2);
+ assertTrue("Check component.providedServiceSpecifications 1", Utils.contains(FooService.class.getName(), specs));
+ assertTrue("Check component.providedServiceSpecifications 2", Utils.contains(BarService.class.getName(), specs));
+
+ PropertyDescription[] pd = (PropertyDescription[]) sr_foobarProvider.getProperty("component.properties");
+ assertEquals("Check component.properties length", pd.length, 0);
+
+ // Test factory
+ assertEquals("Check factory name", foobarProvider.getName(), "Factories-FooBarProviderType-1");
+ Element cd = foobarProvider.getDescription();
+
+// assertEquals("Check implementation class ", cd.getAttribute("implementation-class"), impl);
+
+ Element[] specs2 = cd.getElements("provides");
+ assertEquals("Check specs length", specs2.length, 2);
+ assertTrue("Check specs", containsSpecification(FooService.class.getName(), specs2));
+ assertTrue("Check specs", containsSpecification(BarService.class.getName(), specs2));
+
+ Element[] pd2 = cd.getElements("property");
+ assertNull("Check props null", pd2);
+
+ // Check Description equality
+ ComponentTypeDescription desc = (ComponentTypeDescription) sr_foobarProvider.getProperty("component.description");
+ assertNotNull("check description equality", desc);
+
+ }
+
+ private boolean containsSpecification(String value, Element[] array) {
+ for (int i = 0; array != null && i < array.length; i++) {
+ if (array[i] != null && array[i].containsAttribute("specification") && array[i].getAttribute("specification").equals(value)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+
+
+
+}
diff --git a/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/ConfigAdminTest.java b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/ConfigAdminTest.java
new file mode 100644
index 0000000..3b6d440
--- /dev/null
+++ b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/ConfigAdminTest.java
@@ -0,0 +1,159 @@
+/*
+ * 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.test.scenarios.factories;
+
+import java.util.Properties;
+
+import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
+import org.apache.felix.ipojo.test.scenarios.factories.service.FooService;
+import org.apache.felix.ipojo.test.scenarios.util.Utils;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedServiceFactory;
+
+/**
+ * Check configuration admin reconfiguration.
+ */
+public class ConfigAdminTest extends OSGiTestCase {
+
+ private ManagedServiceFactory getFactoryByName(String pid) {
+ ServiceReference[] refs;
+ try {
+ refs = context.getServiceReferences(ManagedServiceFactory.class.getName(), "(service.pid="+pid+")");
+ if(refs == null) { return null; }
+ return ((org.osgi.service.cm.ManagedServiceFactory) context.getService(refs[0]));
+ } catch (InvalidSyntaxException e) {
+ System.err.println("Cannot get the factory " + pid + " : " + e.getMessage());
+ return null;
+ }
+ }
+
+ /**
+ * Check creation.
+ */
+ public void testCreation() {
+ ManagedServiceFactory f = getFactoryByName("Factories-FooProviderType-2");
+
+ Properties p = new Properties();
+ p.put("int", new Integer(3));
+ p.put("long", new Long(42));
+ p.put("string", "absdir");
+ p.put("strAProp", new String[] {"a"});
+ p.put("intAProp", new int[] {1,2});
+
+ try {
+ f.updated("ok2", p);
+ ServiceReference ref = Utils.getServiceReferenceByName(context, FooService.class.getName(), "ok2");
+ assertNotNull("Check instance creation", ref);
+ f.deleted("ok2");
+ ref = Utils.getServiceReferenceByName(context, FooService.class.getName(), "ok2");
+ assertNull("Check instance deletion", ref);
+ } catch (ConfigurationException e) {
+ fail("An acceptable configuration is rejected : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Check creation (push String).
+ */
+ public void testCreationString() {
+ ManagedServiceFactory f = getFactoryByName("Factories-FooProviderType-2");
+
+ Properties p = new Properties();
+ p.put("int", "3");
+ p.put("long", "42");
+ p.put("string", "absdir");
+ p.put("strAProp", "{a}");
+ p.put("intAProp", "{1,2}");
+
+ try {
+ f.updated("ok2", p);
+ ServiceReference ref = Utils.getServiceReferenceByName(context, FooService.class.getName(), "ok2");
+ assertNotNull("Check instance creation", ref);
+ f.deleted("ok2");
+ ref = Utils.getServiceReferenceByName(context, FooService.class.getName(), "ok2");
+ assertNull("Check instance deletion", ref);
+ } catch (ConfigurationException e) {
+ fail("An acceptable configuration is rejected : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Check update and delete.
+ */
+ public void testUpdate() {
+ ManagedServiceFactory f = getFactoryByName("Factories-FooProviderType-2");
+
+ Properties p = new Properties();
+ p.put("int", new Integer(3));
+ p.put("long", new Long(42));
+ p.put("string", "absdir");
+ p.put("strAProp", new String[] {"a"});
+ p.put("intAProp", new int[] {1,2});
+
+ try {
+ f.updated("okkkk", p);
+ ServiceReference ref = Utils.getServiceReferenceByName(context, FooService.class.getName(), "okkkk");
+ assertNotNull("Check instance creation", ref);
+ p.put("int", new Integer("4"));
+ f.updated("okkkk", p);
+ ref = Utils.getServiceReferenceByName(context, FooService.class.getName(), "okkkk");
+ Integer test = (Integer) ref.getProperty("int");
+ assertEquals("Check instance modification", 4, test.intValue());
+ f.deleted("okkkk");
+ ref = Utils.getServiceReferenceByName(context, FooService.class.getName(), "okkkk");
+ assertNull("Check instance deletion", ref);
+ } catch (ConfigurationException e) {
+ fail("An acceptable configuration is rejected : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Check update and delete.
+ * (Push String).
+ */
+ public void testUpdateString() {
+ ManagedServiceFactory f = getFactoryByName("Factories-FooProviderType-2");
+
+ Properties p = new Properties();
+ p.put("int", "3");
+ p.put("long", "42");
+ p.put("string", "absdir");
+ p.put("strAProp", "{a}");
+ p.put("intAProp", "{1,2}");
+
+ try {
+ f.updated("okkkk", p);
+ ServiceReference ref = Utils.getServiceReferenceByName(context, FooService.class.getName(), "okkkk");
+ assertNotNull("Check instance creation", ref);
+ p.put("int", new Integer("4"));
+ f.updated("okkkk", p);
+ ref = Utils.getServiceReferenceByName(context, FooService.class.getName(), "okkkk");
+ Integer test = (Integer) ref.getProperty("int");
+ assertEquals("Check instance modification", 4, test.intValue());
+ f.deleted("okkkk");
+ ref = Utils.getServiceReferenceByName(context, FooService.class.getName(), "okkkk");
+ assertNull("Check instance deletion", ref);
+ } catch (ConfigurationException e) {
+ fail("An acceptable configuration is rejected : " + e.getMessage());
+ }
+ }
+
+}
diff --git a/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/FactoryProps.java b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/FactoryProps.java
new file mode 100644
index 0000000..d2604d0
--- /dev/null
+++ b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/FactoryProps.java
@@ -0,0 +1,87 @@
+/*
+ * 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.test.scenarios.factories;
+
+import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.architecture.PropertyDescription;
+import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
+import org.apache.felix.ipojo.test.scenarios.factories.service.BarService;
+import org.apache.felix.ipojo.test.scenarios.factories.service.FooService;
+import org.apache.felix.ipojo.test.scenarios.util.Utils;
+import org.osgi.framework.ServiceReference;
+
+public class FactoryProps extends OSGiTestCase {
+
+// public void testImplementationClass() {
+// ServiceReference ref1 = Utils.getServiceReferenceByName(context, Factory.class.getName(), "FooProviderType-1");
+// assertNotNull("The factory is available", ref1);
+// String clazz = (String) ref1.getProperty("component.class");
+// assertEquals("Check the implementation class", clazz, FooProviderType1.class.getName());
+// }
+
+ public void testSimpleExposition() {
+ ServiceReference ref1 = Utils.getServiceReferenceByName(context, Factory.class.getName(), "Factories-FooProviderType-1");
+ assertNotNull("The factory is available", ref1);
+ String[] spec = (String[]) ref1.getProperty("component.providedServiceSpecifications");
+ assertEquals("Check array length", spec.length, 1);
+ assertEquals("Check spec", spec[0], FooService.class.getName());
+ }
+
+ public void testDoubleExposition() {
+ ServiceReference ref1 = Utils.getServiceReferenceByName(context, Factory.class.getName(), "Factories-FooBarProviderType-1");
+ assertNotNull("The factory is available", ref1);
+ String[] spec = (String[]) ref1.getProperty("component.providedServiceSpecifications");
+ assertEquals("Check array length", spec.length, 2);
+ assertContains("Check spec 1", spec, FooService.class.getName());
+ assertContains("Check spec 2", spec, BarService.class.getName());
+ }
+
+ public void testProps() {
+ ServiceReference ref1 = Utils.getServiceReferenceByName(context, Factory.class.getName(), "Factories-FooProviderType-Dyn2");
+ assertNotNull("The factory is available", ref1);
+ PropertyDescription[] pd = (PropertyDescription[]) ref1.getProperty("component.properties");
+ assertEquals("Check property list size", pd.length, 5);
+
+ //P0
+ assertEquals("0) Check name", "int", pd[0].getName());
+ assertEquals("0) Check type", "int", pd[0].getType());
+ assertEquals("0) Check value", "4", pd[0].getValue());
+
+ //P1
+ assertEquals("1) Check name", "boolean", pd[1].getName());
+ assertEquals("1) Check type", "boolean", pd[1].getType());
+ assertNull("1) Check value", pd[1].getValue());
+
+ //P2
+ assertEquals("2) Check name", "string", pd[2].getName());
+ assertEquals("2) Check type", String.class.getName(), pd[2].getType());
+ assertNull("2) Check value", pd[2].getValue());
+
+ //P3
+ assertEquals("3) Check name", "strAProp", pd[3].getName());
+ assertEquals("3) Check type", "java.lang.String[]", pd[3].getType());
+ assertNull("3) Check value", pd[3].getValue());
+
+ //P4
+ assertEquals("4) Check name", "intAProp", pd[4].getName());
+ assertEquals("4) Check type", "int[]", pd[4].getType());
+ }
+
+
+}
diff --git a/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/FactoryTestSuite.java b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/FactoryTestSuite.java
new file mode 100644
index 0000000..e34477a
--- /dev/null
+++ b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/FactoryTestSuite.java
@@ -0,0 +1,38 @@
+/*
+ * 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.test.scenarios.factories;
+
+import junit.framework.Test;
+
+import org.apache.felix.ipojo.junit4osgi.OSGiTestSuite;
+import org.osgi.framework.BundleContext;
+
+public class FactoryTestSuite {
+
+ public static Test suite(BundleContext bc) {
+ OSGiTestSuite ots = new OSGiTestSuite("Factories Test Suite", bc);
+ ots.addTestSuite(ComponentDesc.class);
+ ots.addTestSuite(UnacceptableConfigurationTest.class);
+ ots.addTestSuite(ConfigAdminTest.class);
+ ots.addTestSuite(ObedienceTest.class);
+ ots.addTestSuite(FactoryProps.class);
+ return ots;
+ }
+
+}
diff --git a/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/ObedienceTest.java b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/ObedienceTest.java
new file mode 100644
index 0000000..8ecdf56
--- /dev/null
+++ b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/ObedienceTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.test.scenarios.factories;
+
+import java.util.Properties;
+
+import org.apache.felix.ipojo.ComponentFactory;
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
+import org.apache.felix.ipojo.test.scenarios.factories.service.FooService;
+import org.apache.felix.ipojo.test.scenarios.util.Utils;
+
+/**
+ * Check that instances are disposed when the factory is stopped.
+ */
+public class ObedienceTest extends OSGiTestCase {
+
+ public void testObedience() {
+ assertNull("Check no foo service", context.getServiceReference(FooService.class.getName()));
+ ComponentFactory factory = (ComponentFactory) Utils.getFactoryByName(context, "Factories-FooProviderType-1");
+ assertNotNull("Check factory existing", factory);
+
+ Properties props1 = new Properties();
+ props1.put("name", "foo1");
+ Properties props2 = new Properties();
+ props2.put("name", "foo2");
+
+ ComponentInstance ci1 = null, ci2 = null;
+ try {
+ ci1 = factory.createComponentInstance(props1);
+ ci2 = factory.createComponentInstance(props2);
+ } catch(Exception e) {
+ fail("Cannot instantiate foo providers : " + e.getMessage());
+ }
+
+ assertTrue("Check foo1 validity", ci1.getState() == ComponentInstance.VALID);
+ assertTrue("Check foo2 validity", ci2.getState() == ComponentInstance.VALID);
+
+ assertNotNull("Check foo service", context.getServiceReference(FooService.class.getName()));
+ assertEquals("Check the number of Foo", Utils.getServiceReferences(context, FooService.class.getName(), null).length, 2);
+
+ factory.stop();
+
+ assertTrue("Check foo1 invalidity ("+ci1.getState()+")", ci1.getState() == ComponentInstance.DISPOSED);
+ assertTrue("Check foo2 invalidity ("+ci1.getState()+")", ci2.getState() == ComponentInstance.DISPOSED);
+
+ assertNull("Check no foo service", context.getServiceReference(FooService.class.getName()));
+
+ factory.start();
+ assertNull("Check no foo service", context.getServiceReference(FooService.class.getName()));
+ }
+
+}
diff --git a/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/UnacceptableConfigurationTest.java b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/UnacceptableConfigurationTest.java
new file mode 100644
index 0000000..b4f7aa0
--- /dev/null
+++ b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/UnacceptableConfigurationTest.java
@@ -0,0 +1,421 @@
+/*
+ * 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.test.scenarios.factories;
+
+import java.util.Properties;
+
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
+import org.apache.felix.ipojo.test.scenarios.util.Utils;
+
+/**
+ * Test unacceptable configuration.
+ */
+public class UnacceptableConfigurationTest extends OSGiTestCase {
+
+ /**
+ * Configuration without the name property.
+ */
+ public void testWithoutName() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-2");
+
+ Properties p = new Properties();
+ p.put("int", new Integer(3));
+ p.put("long", new Long(42));
+ p.put("string", "absdir");
+ p.put("strAProp", new String[] {"a"});
+ p.put("intAProp", new int[] {1,2});
+
+ ComponentInstance ci = null;
+ try {
+ ci = f.createComponentInstance(p);
+ ci.dispose();
+ } catch(Exception e) { fail("an acceptable configuration is refused : " + e.getMessage()); }
+
+ }
+
+ /**
+ * Empty configuration.
+ */
+ public void testEmptyConfiguration() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-2");
+ Properties p = new Properties();
+
+ ComponentInstance ci = null;
+ try {
+ ci = f.createComponentInstance(p);
+ ci.dispose();
+ } catch(Exception e) { fail("An acceptable configuration is refused"); }
+ }
+
+ /**
+ * Empty configuration (just the name).
+ */
+ public void testEmptyConfiguration2() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-Dyn2");
+ Properties p = new Properties();
+ p.put("name", "ko");
+ ComponentInstance ci = null;
+ try {
+ ci = f.createComponentInstance(p);
+ ci.dispose();
+ } catch(Exception e) { return; }
+
+ fail("An unacceptable configuration is accepted");
+ }
+
+ /**
+ * Null configuration (accept).
+ */
+ public void testNull() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-2");
+
+ ComponentInstance ci = null;
+ try {
+ ci = f.createComponentInstance(null);
+ ci.dispose();
+ } catch(Exception e) { fail("An acceptable configuration is refused"); }
+ }
+
+ /**
+ * Null configuration (fail).
+ */
+ public void testNull2() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-Dyn2");
+
+ ComponentInstance ci = null;
+ try {
+ ci = f.createComponentInstance(null);
+ ci.dispose();
+ } catch(Exception e) { return; }
+
+ fail("An unacceptable configuration is accepted");
+ }
+
+ /**
+ * Check static properties.
+ */
+ public void testStaticOK() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-2");
+
+ Properties p = new Properties();
+ p.put("name", "ok");
+ p.put("int", new Integer(3));
+ p.put("long", new Long(42));
+ p.put("string", "absdir");
+ p.put("strAProp", new String[] {"a"});
+ p.put("intAProp", new int[] {1,2});
+
+ ComponentInstance ci = null;
+ try {
+ ci = f.createComponentInstance(p);
+ ci.dispose();
+ } catch(Exception e) {
+ fail("An acceptable configuration is rejected : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Check dynamic properties.
+ */
+ public void testDynamicOK() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-Dyn");
+
+ Properties p = new Properties();
+ p.put("name", "ok");
+ p.put("int", new Integer(3));
+ p.put("boolean", new Boolean(true));
+ p.put("string", "absdir");
+ p.put("strAProp", new String[] {"a"});
+ p.put("intAProp", new int[] {1,2});
+
+ ComponentInstance ci = null;
+ try {
+ ci = f.createComponentInstance(p);
+ ci.dispose();
+ } catch(Exception e) {
+ e.printStackTrace();
+ fail("An acceptable configuration is rejected : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Check inconsistent types.
+ */
+ public void testDynamicBadType() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-Dyn");
+
+ Properties p = new Properties();
+ p.put("name", "ok");
+ p.put("int", new Integer(3));
+ p.put("long", new Long(42));
+ p.put("string", "absdir");
+ p.put("strAProp", new String[] {"a"});
+ p.put("intAProp", new int[] {1,2});
+
+ ComponentInstance ci = null;
+ try {
+ ci = f.createComponentInstance(p);
+ ci.dispose();
+ } catch(Exception e) {
+ fail("An acceptable configuration is rejected : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Check good configuration (with overriding).
+ */
+ public void testDynamicComplete() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-Dyn2");
+
+ Properties p = new Properties();
+ p.put("name", "ok");
+ p.put("int", new Integer(3));
+ p.put("boolean", new Boolean(true));
+ p.put("string", "absdir");
+ p.put("strAProp", new String[] {"a"});
+ p.put("intAProp", new int[] {1,2});
+
+ ComponentInstance ci = null;
+ try {
+ ci = f.createComponentInstance(p);
+ ci.dispose();
+ } catch(Exception e) {
+ fail("An acceptable configuration is rejected : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Check good configuration.
+ */
+ public void testDynamicJustEnough() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-Dyn2");
+
+ Properties p = new Properties();
+ p.put("name", "ok");
+ p.put("boolean", new Boolean(true));
+ p.put("string", "absdir");
+ p.put("strAProp", new String[] {"a"});
+
+ ComponentInstance ci = null;
+ try {
+ ci = f.createComponentInstance(p);
+ ci.dispose();
+ } catch(Exception e) {
+ fail("An acceptable configuration is rejected : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Check good configuration.
+ */
+ public void testDynamicMix() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-Dyn2");
+
+ Properties p = new Properties();
+ p.put("name", "ok");
+ p.put("boolean", new Boolean(true));
+ p.put("string", "absdir");
+ p.put("strAProp", new String[] {"a"});
+ p.put("intAProp", new int[] {1,2});
+
+ ComponentInstance ci = null;
+ try {
+ ci = f.createComponentInstance(p);
+ ci.dispose();
+ } catch(Exception e) {
+ fail("An acceptable configuration is rejected : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Check uncomplete configuration.
+ */
+ public void testDynamicUncomplete() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-Dyn2");
+
+ Properties p = new Properties();
+ p.put("name", "ok");
+ p.put("string", "absdir");
+ p.put("strAProp", new String[] {"a"});
+ p.put("intAProp", new int[] {1,2});
+
+ ComponentInstance ci = null;
+ try {
+ ci = f.createComponentInstance(p);
+ ci.dispose();
+ } catch(Exception e) { return; }
+
+ fail("An unacceptable configuration is accepted");
+ }
+
+ /**
+ * Check good configuration (more properties).
+ */
+ public void testDynamicMore() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-Dyn2");
+
+ Properties p = new Properties();
+ p.put("name", "ok");
+ p.put("int", new Integer(3));
+ p.put("boolean", new Boolean(true));
+ p.put("string", "absdir");
+ p.put("strAProp", new String[] {"a"});
+ p.put("intAProp", new int[] {1,2});
+ p.put("tralala", "foo");
+
+ ComponentInstance ci = null;
+ try {
+ ci = f.createComponentInstance(p);
+ ci.dispose();
+ } catch(Exception e) {
+ fail("An acceptable configuration is rejected : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Check properties affecting services and component.
+ */
+ public void testDoubleProps() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-Dyn2");
+
+ Properties p = new Properties();
+ p.put("name", "ok");
+ p.put("int", new Integer(3));
+ p.put("boolean", new Boolean(true));
+ p.put("string", "absdir");
+ p.put("strAProp", new String[] {"a"});
+ p.put("intAProp", new int[] {1,2});
+ p.put("boolean", new Boolean(false));
+ p.put("string", "toto");
+
+ ComponentInstance ci = null;
+ try {
+ ci = f.createComponentInstance(p);
+ ci.dispose();
+ } catch(Exception e) {
+ fail("An acceptable configuration is rejected : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Check instance name unicity.
+ */
+ public void testUnicity1() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-2");
+
+ ComponentInstance ci1,ci2, ci3 = null;
+ try {
+ ci1 = f.createComponentInstance(null);
+ ci2 = f.createComponentInstance(null);
+ ci3 = f.createComponentInstance(null);
+ assertNotEquals("Check name ci1, ci2", ci1.getInstanceName(), ci2.getInstanceName());
+ assertNotEquals("Check name ci1, ci3", ci1.getInstanceName(), ci3.getInstanceName());
+ assertNotEquals("Check name ci3, ci2", ci3.getInstanceName(), ci2.getInstanceName());
+ ci1.dispose();
+ ci2.dispose();
+ ci3.dispose();
+ } catch(Exception e) { fail("An acceptable configuration is refused"); }
+ }
+
+ /**
+ * Check instance name unicity.
+ */
+ public void testUnicity2() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-2");
+
+ ComponentInstance ci1,ci2, ci3 = null;
+ try {
+ Properties p1 = new Properties();
+ p1.put("name", "name1");
+ ci1 = f.createComponentInstance(p1);
+ Properties p2 = new Properties();
+ p2.put("name", "name2");
+ ci2 = f.createComponentInstance(p2);
+ Properties p3 = new Properties();
+ p3.put("name", "name3");
+ ci3 = f.createComponentInstance(p3);
+ assertNotEquals("Check name ci1, ci2", ci1.getInstanceName(), ci2.getInstanceName());
+ assertNotEquals("Check name ci1, ci3", ci1.getInstanceName(), ci3.getInstanceName());
+ assertNotEquals("Check name ci3, ci2", ci3.getInstanceName(), ci2.getInstanceName());
+ ci1.dispose();
+ ci2.dispose();
+ ci3.dispose();
+ } catch(Exception e) { fail("An acceptable configuration is refused"); }
+ }
+
+ /**
+ * Check instance name unicity.
+ */
+ public void testUnicity3() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-2");
+
+ ComponentInstance ci1 = null,ci2 = null, ci3 = null;
+ try {
+ Properties p1 = new Properties();
+ p1.put("name", "name1");
+ ci1 = f.createComponentInstance(p1);
+ Properties p2 = new Properties();
+ p2.put("name", "name1");
+ ci2 = f.createComponentInstance(p2);
+ assertNotEquals("Check name ci1, ci2", ci1.getInstanceName(), ci2.getInstanceName());
+ assertNotEquals("Check name ci1, ci3", ci1.getInstanceName(), ci3.getInstanceName());
+ assertNotEquals("Check name ci3, ci2", ci3.getInstanceName(), ci3.getInstanceName());
+ ci1.dispose();
+ ci2.dispose();
+ ci3.dispose();
+ } catch(Exception e) {
+ ci1.dispose();
+ return; }
+
+ fail("An unacceptable configuration is acceptable");
+ }
+
+ /**
+ * Check instance name unicity.
+ */
+ public void testUnicity4() {
+ Factory f = Utils.getFactoryByName(context, "Factories-FooProviderType-2");
+ Factory f2 = Utils.getFactoryByName(context, "Factories-FooProviderType-1");
+
+ ComponentInstance ci1 = null,ci2 = null, ci3 = null;
+ try {
+ Properties p1 = new Properties();
+ p1.put("name", "name1");
+ ci1 = f.createComponentInstance(p1);
+ Properties p2 = new Properties();
+ p2.put("name", "name1");
+ ci2 = f2.createComponentInstance(p2);
+ System.err.println("==== " + ci1.getInstanceName() + " === " + ci2.getInstanceName());
+ assertNotEquals("Check name ci1, ci2", ci1.getInstanceName(), ci2.getInstanceName());
+ assertNotEquals("Check name ci1, ci3", ci1.getInstanceName(), ci3.getInstanceName());
+ assertNotEquals("Check name ci3, ci2", ci3.getInstanceName(), ci3.getInstanceName());
+ ci1.dispose();
+ ci2.dispose();
+ ci3.dispose();
+ } catch(Exception e) {
+ ci1.dispose();
+ return; }
+
+ fail("An unacceptable configuration is acceptable");
+ }
+
+
+}
diff --git a/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/service/BarService.java b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/service/BarService.java
new file mode 100644
index 0000000..e29c179
--- /dev/null
+++ b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/service/BarService.java
@@ -0,0 +1,29 @@
+/*
+ * 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.test.scenarios.factories.service;
+
+import java.util.Properties;
+
+public interface BarService {
+
+ public boolean bar();
+
+ public Properties getProps();
+
+}
diff --git a/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/service/CheckService.java b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/service/CheckService.java
new file mode 100644
index 0000000..3fd1e2f
--- /dev/null
+++ b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/service/CheckService.java
@@ -0,0 +1,31 @@
+/*
+ * 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.test.scenarios.factories.service;
+
+import java.util.Properties;
+
+public interface CheckService {
+
+ public static final String foo = "foo";
+
+ public boolean check();
+
+ public Properties getProps();
+
+}
diff --git a/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/service/FooService.java b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/service/FooService.java
new file mode 100644
index 0000000..9ba67c9
--- /dev/null
+++ b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/factories/service/FooService.java
@@ -0,0 +1,39 @@
+/*
+ * 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.test.scenarios.factories.service;
+
+import java.util.Properties;
+
+public interface FooService {
+
+ boolean foo();
+
+ Properties fooProps();
+
+ Boolean getObject();
+
+ boolean getBoolean();
+
+ int getInt();
+
+ long getLong();
+
+ double getDouble();
+
+}
diff --git a/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/util/Utils.java b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/util/Utils.java
new file mode 100644
index 0000000..cb87c86
--- /dev/null
+++ b/ipojo/tests/tests.core.factories/src/main/java/org/apache/felix/ipojo/test/scenarios/util/Utils.java
@@ -0,0 +1,326 @@
+/*
+ * 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.test.scenarios.util;
+
+import java.util.Dictionary;
+import java.util.Properties;
+
+import junit.framework.Assert;
+
+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.composite.CompositeManager;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.ManagedServiceFactory;
+
+public class Utils {
+
+ public static Factory getFactoryByName(BundleContext bc, String factoryName) {
+ ServiceReference[] refs;
+ try {
+ refs = bc.getServiceReferences(Factory.class.getName(), "(factory.name=" + factoryName + ")");
+ if (refs == null) {
+ System.err.println("Cannot get the factory " + factoryName);
+ return null;
+ }
+ return ((Factory) bc.getService(refs[0]));
+ } catch (InvalidSyntaxException e) {
+ System.err.println("Cannot get the factory " + factoryName + " : " + e.getMessage());
+ return null;
+ }
+ }
+
+ public static HandlerFactory getHandlerFactoryByName(BundleContext bc, String factoryName) {
+ ServiceReference[] refs;
+ try {
+ refs = bc.getServiceReferences(Factory.class.getName(), "(" + Handler.HANDLER_NAME_PROPERTY + "=" + factoryName + ")");
+ if (refs == null) {
+ System.err.println("Cannot get the factory " + factoryName);
+ return null;
+ }
+ return (HandlerFactory) bc.getService(refs[0]);
+ } catch (InvalidSyntaxException e) {
+ System.err.println("Cannot get the factory " + factoryName + " : " + e.getMessage());
+ return null;
+ }
+ }
+
+ public static ComponentInstance getComponentInstance(BundleContext bc, String factoryName, Dictionary configuration) {
+ Factory fact = getFactoryByName(bc, factoryName);
+
+ if (fact == null) {
+ System.err.println("Factory " + factoryName + " not found");
+ return null;
+ }
+
+ // if(fact.isAcceptable(configuration)) {
+ try {
+ return fact.createComponentInstance(configuration);
+ } catch (Exception e) {
+ e.printStackTrace();
+ Assert.fail("Cannot create the instance from " + factoryName + " : " + e.getMessage());
+ return null;
+ }
+ // }
+ // else {
+ // System.err.println("Configuration not accepted by : " + factoryName);
+ // return null;
+ // }
+ }
+
+ public static ComponentInstance getComponentInstanceByName(BundleContext bc, String factoryName, String name) {
+ Factory fact = getFactoryByName(bc, factoryName);
+
+ if (fact == null) {
+ System.err.println("Factory " + factoryName + " not found");
+ return null;
+ }
+
+ try {
+ Properties props = new Properties();
+ props.put("name", name);
+ return fact.createComponentInstance(props);
+ } catch (Exception e) {
+ System.err.println("Cannot create the instance from " + factoryName + " : " + e.getMessage());
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ public static ServiceReference[] getServiceReferences(BundleContext bc, String itf, String filter) {
+ ServiceReference[] refs = null;
+ try {
+ refs = bc.getServiceReferences(itf, filter);
+ } catch (InvalidSyntaxException e) {
+ System.err.println("Invalid Filter : " + filter);
+ }
+ if (refs == null) {
+ return new ServiceReference[0];
+ } else {
+ return refs;
+ }
+ }
+
+ public static ServiceReference getServiceReference(BundleContext bc, String itf, String filter) {
+ ServiceReference[] refs = null;
+ try {
+ refs = bc.getServiceReferences(itf, filter);
+ } catch (InvalidSyntaxException e) {
+ System.err.println("Invalid Filter : " + filter);
+ }
+ if (refs == null) {
+ return null;
+ } else {
+ return refs[0];
+ }
+ }
+
+ public static ServiceReference getServiceReferenceByName(BundleContext bc, String itf, String name) {
+ ServiceReference[] refs = null;
+ String filter = null;
+ if (itf.equals(Factory.class.getName()) || itf.equals(ManagedServiceFactory.class.getName())) {
+ filter = "(" + "factory.name" + "=" + name + ")";
+ } else {
+ filter = "(" + "instance.name" + "=" + name + ")";
+ }
+ try {
+ refs = bc.getServiceReferences(itf, filter);
+ } catch (InvalidSyntaxException e) {
+ System.err.println("Invalid Filter : " + filter);
+ }
+ if (refs == null) {
+ return null;
+ } else {
+ return refs[0];
+ }
+ }
+
+ public static ServiceReference getServiceReferenceByPID(BundleContext bc, String itf, String pid) {
+ ServiceReference[] refs = null;
+ String filter = "(" + "service.pid" + "=" + pid + ")";
+ try {
+ refs = bc.getServiceReferences(itf, filter);
+ } catch (InvalidSyntaxException e) {
+ System.err.println("Invalid Filter : " + filter);
+ }
+ if (refs == null) {
+ return null;
+ } else if (refs.length == 1) {
+ return refs[0];
+ } else {
+ Assert.fail("A service lookup by PID returned several providers (" + refs.length + ")" + " for " + itf + " with " + pid);
+ return null;
+ }
+ }
+
+ public static Object getServiceObject(BundleContext bc, String itf, String filter) {
+ ServiceReference ref = getServiceReference(bc, itf, filter);
+ if (ref != null) {
+ return bc.getService(ref);
+ } else {
+ return null;
+ }
+ }
+
+ public static Object[] getServiceObjects(BundleContext bc, String itf, String filter) {
+ ServiceReference[] refs = getServiceReferences(bc, itf, filter);
+ if (refs != null) {
+ Object[] list = new Object[refs.length];
+ for (int i = 0; i < refs.length; i++) {
+ list[i] = bc.getService(refs[i]);
+ }
+ return list;
+ } else {
+ return new Object[0];
+ }
+ }
+
+// public static ServiceContext getServiceContext(ComponentInstance ci) {
+// if (ci instanceof CompositeManager) {
+// return ((CompositeManager) ci).getServiceContext();
+// } else {
+// throw new RuntimeException("Cannot get the service context form an non composite instance");
+// }
+// }
+
+ public static Factory getFactoryByName(ServiceContext bc, String factoryName) {
+ ServiceReference[] refs;
+ try {
+ refs = bc.getServiceReferences(Factory.class.getName(), "(factory.name=" + factoryName + ")");
+ if (refs == null) { return null; }
+ return ((Factory) bc.getService(refs[0]));
+ } catch (InvalidSyntaxException e) {
+ System.err.println("Cannot get the factory " + factoryName + " : " + e.getMessage());
+ return null;
+ }
+ }
+
+ public static ComponentInstance getComponentInstance(ServiceContext bc, String factoryName, Dictionary configuration) {
+ Factory fact = getFactoryByName(bc, factoryName);
+
+ if (fact == null) { return null; }
+
+ if (fact.isAcceptable(configuration)) {
+ try {
+ return fact.createComponentInstance(configuration);
+ } catch (Exception e) {
+ System.err.println(e.getMessage());
+ e.printStackTrace();
+ return null;
+ }
+ } else {
+ System.err.println("Configuration not accepted by : " + factoryName);
+ return null;
+ }
+ }
+
+ public static ServiceReference[] getServiceReferences(ServiceContext bc, String itf, String filter) {
+ ServiceReference[] refs = null;
+ try {
+ refs = bc.getServiceReferences(itf, filter);
+ } catch (InvalidSyntaxException e) {
+ System.err.println("Invalid Filter : " + filter);
+ }
+ if (refs == null) {
+ return new ServiceReference[0];
+ } else {
+ return refs;
+ }
+ }
+
+ public static ServiceReference getServiceReference(ServiceContext bc, String itf, String filter) {
+ ServiceReference[] refs = null;
+ try {
+ refs = bc.getServiceReferences(itf, filter);
+ } catch (InvalidSyntaxException e) {
+ System.err.println("Invalid Filter : " + filter);
+ }
+ if (refs == null) {
+ return null;
+ } else {
+ return refs[0];
+ }
+ }
+
+ public static ServiceReference getServiceReferenceByName(ServiceContext bc, String itf, String name) {
+ ServiceReference[] refs = null;
+ String filter = null;
+ if (itf.equals(Factory.class.getName()) || itf.equals(ManagedServiceFactory.class.getName())) {
+ filter = "(" + "factory.name" + "=" + name + ")";
+ } else {
+ filter = "(" + "instance.name" + "=" + name + ")";
+ }
+ try {
+ refs = bc.getServiceReferences(itf, filter);
+ } catch (InvalidSyntaxException e) {
+ System.err.println("Invalid Filter : " + filter);
+ }
+ if (refs == null) {
+ return null;
+ } else {
+ return refs[0];
+ }
+ }
+
+ public static Object getServiceObject(ServiceContext bc, String itf, String filter) {
+ ServiceReference ref = getServiceReference(bc, itf, filter);
+ if (ref != null) {
+ return bc.getService(ref);
+ } else {
+ return null;
+ }
+ }
+
+ public static Object[] getServiceObjects(ServiceContext bc, String itf, String filter) {
+ ServiceReference[] refs = getServiceReferences(bc, itf, filter);
+ if (refs != null) {
+ Object[] list = new Object[refs.length];
+ for (int i = 0; i < refs.length; i++) {
+ list[i] = bc.getService(refs[i]);
+ }
+ return list;
+ } else {
+ 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)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static boolean contains(int value, int[] array) {
+ for (int i = 0; array != null && i < array.length; i++) {
+ if (array[i] == value) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+}
diff --git a/ipojo/tests/tests.core.factories/src/main/resources/metadata.xml b/ipojo/tests/tests.core.factories/src/main/resources/metadata.xml
new file mode 100644
index 0000000..2d10ac8
--- /dev/null
+++ b/ipojo/tests/tests.core.factories/src/main/resources/metadata.xml
@@ -0,0 +1,69 @@
+<ipojo>
+ <!-- Simple provider -->
+ <component
+ className="org.apache.felix.ipojo.test.scenarios.component.FooProviderType1"
+ factory="Factories-FooProviderType-1" architecture="true">
+ <provides />
+ </component>
+
+ <!-- Provider providing 2 services -->
+ <component
+ className="org.apache.felix.ipojo.test.scenarios.component.FooBarProviderType1"
+ factory="Factories-FooBarProviderType-1" architecture="true">
+ <provides />
+ </component>
+
+ <!-- Provider with dynamic property -->
+ <component
+ className="org.apache.felix.ipojo.test.scenarios.component.FooProviderTypeDyn"
+ name="Factories-FooProviderType-Dyn" architecture="true">
+ <provides>
+ <property name="int" field="intProp" value="2" />
+ <property name="boolean" field="boolProp" value="false" />
+ <property name="string" field="strProp" value="foo" />
+ <property name="strAProp" field="strAProp"
+ value="{foo, bar}" />
+ <property name="intAProp" field="intAProp" value="{ 1,2,3}" />
+ </provides>
+ </component>
+
+ <component
+ className="org.apache.felix.ipojo.test.scenarios.component.FooProviderType1"
+ name="Factories-FooProviderType-2" architecture="true">
+ <provides>
+ <property name="int" type="int" value="2" />
+ <property name="long" type="long" value="40" />
+ <property name="string" type="java.lang.String" value="foo" />
+ <property name="strAProp" type="java.lang.String[]"
+ value="{foo, bar}" />
+ <property name="intAProp" type="int[]" value="{1,2,3}" />
+ </provides>
+ </component>
+
+ <component
+ className="org.apache.felix.ipojo.test.scenarios.component.FooProviderTypeDyn2"
+ name="Factories-FooProviderType-Dyn2" factory="true" architecture="true">
+ <provides>
+ <property name="int" field="intProp" value="4" />
+ <property name="boolean" field="boolProp" />
+ <property name="string" field="strProp" />
+ <property name="strAProp" field="strAProp" />
+ <property name="intAProp" field="intAProp"
+ value="{1, 2,3 }" />
+ </provides>
+ </component>
+
+ <component
+ className="org.apache.felix.ipojo.test.scenarios.component.FooProviderType1"
+ factory="Factories-FooProviderType-3" architecture="true">
+ <provides>
+ <property name="foo" field="m_foo" />
+ <property name="bar" field="m_bar" />
+ <property name="baz" type="java.lang.String" />
+ </provides>
+ <properties propagation="true">
+ <property name="foo" field="m_foo" />
+ <property name="bar" field="m_bar" />
+ </properties>
+ </component>
+</ipojo>