FELIX-3903
Migrate the core factory version test to pax exam 3
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1447490 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/pom.xml b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/pom.xml
new file mode 100644
index 0000000..a960ee6
--- /dev/null
+++ b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/pom.xml
@@ -0,0 +1,35 @@
+<!--
+ 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.
+-->
+<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">
+
+ <parent>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.ipojo.runtime.core-it</artifactId>
+ <version>1.9.0-SNAPSHOT</version>
+ <relativePath>../../../pom.xml</relativePath>
+ </parent>
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>ipojo-core-factory-version-test</artifactId>
+
+ <name>${project.artifactId}</name>
+
+</project>
diff --git a/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/java/org/apache/felix/ipojo/core/tests/components/MyComponent.java b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/java/org/apache/felix/ipojo/core/tests/components/MyComponent.java
new file mode 100644
index 0000000..ea36a83
--- /dev/null
+++ b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/java/org/apache/felix/ipojo/core/tests/components/MyComponent.java
@@ -0,0 +1,11 @@
+package org.apache.felix.ipojo.core.tests.components;
+
+import org.apache.felix.ipojo.core.tests.services.MyService;
+
+public class MyComponent implements MyService {
+
+ public void foo() {
+ // Nothing to do.
+ }
+
+}
diff --git a/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/java/org/apache/felix/ipojo/core/tests/components/MyCons.java b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/java/org/apache/felix/ipojo/core/tests/components/MyCons.java
new file mode 100644
index 0000000..a0c4fb5
--- /dev/null
+++ b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/java/org/apache/felix/ipojo/core/tests/components/MyCons.java
@@ -0,0 +1,13 @@
+package org.apache.felix.ipojo.core.tests.components;
+
+import org.apache.felix.ipojo.core.tests.services.MyService;
+
+public class MyCons {
+
+ private MyService[] services;
+
+ public MyCons() {
+ System.out.println("Bound to " + services.length);
+ }
+
+}
diff --git a/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/java/org/apache/felix/ipojo/core/tests/services/MyService.java b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/java/org/apache/felix/ipojo/core/tests/services/MyService.java
new file mode 100644
index 0000000..bc7e809
--- /dev/null
+++ b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/java/org/apache/felix/ipojo/core/tests/services/MyService.java
@@ -0,0 +1,7 @@
+package org.apache.felix.ipojo.core.tests.services;
+
+public interface MyService {
+
+ public void foo();
+
+}
diff --git a/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/cons.xml b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/cons.xml
new file mode 100644
index 0000000..58e7e9c
--- /dev/null
+++ b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/cons.xml
@@ -0,0 +1,8 @@
+<ipojo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="org.apache.felix.ipojo http://felix.apache.org/ipojo/schemas/SNAPSHOT/core.xsd"
+ xmlns="org.apache.felix.ipojo">
+ <component name="cons" classname="org.apache.felix.ipojo.core.tests.components.MyCons">
+ <requires field="services"/>
+ </component>
+ <instance component="cons" name="mycons"/>
+</ipojo>
\ No newline at end of file
diff --git a/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/instances.xml b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/instances.xml
new file mode 100644
index 0000000..0ffad9e
--- /dev/null
+++ b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/instances.xml
@@ -0,0 +1,22 @@
+<ipojo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="org.apache.felix.ipojo http://people.apache.org/~clement/ipojo/SCHEMA-DEV/core.xsd"
+ xmlns="org.apache.felix.ipojo">
+
+ <instance component="org.apache.felix.ipojo.core.tests.components.MyComponent"
+ version="1.0"
+ name="instance-v1"
+ />
+
+ <instance component="org.apache.felix.ipojo.core.tests.components.MyComponent"
+ version="1.1"
+ name="instance-v1.1"
+ />
+
+ <instance component="org.apache.felix.ipojo.core.tests.components.MyComponent"
+ name="instance-any"
+ />
+
+ <instance component="MyComponent"
+ name="MyComponentInstance"
+ />
+</ipojo>
\ No newline at end of file
diff --git a/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/provider-v1.1.xml b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/provider-v1.1.xml
new file mode 100644
index 0000000..b2fe30d
--- /dev/null
+++ b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/provider-v1.1.xml
@@ -0,0 +1,7 @@
+<ipojo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="org.apache.felix.ipojo http://people.apache.org/~clement/ipojo/SCHEMA-DEV/core.xsd"
+ xmlns="org.apache.felix.ipojo">
+ <component classname="org.apache.felix.ipojo.core.tests.components.MyComponent" version="1.1">
+ <provides/>
+ </component>
+</ipojo>
\ No newline at end of file
diff --git a/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/provider-v1.xml b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/provider-v1.xml
new file mode 100644
index 0000000..e224135
--- /dev/null
+++ b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/provider-v1.xml
@@ -0,0 +1,12 @@
+<ipojo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="org.apache.felix.ipojo http://people.apache.org/~clement/ipojo/SCHEMA-DEV/core.xsd"
+ xmlns="org.apache.felix.ipojo">
+ <component classname="org.apache.felix.ipojo.core.tests.components.MyComponent" version="1.0">
+ <provides/>
+ </component>
+
+
+ <component classname="org.apache.felix.ipojo.core.tests.components.MyComponent" name="MyComponent">
+ <provides/>
+ </component>
+</ipojo>
\ No newline at end of file
diff --git a/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/vprovider-v1.xml b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/vprovider-v1.xml
new file mode 100644
index 0000000..25ae073
--- /dev/null
+++ b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/vprovider-v1.xml
@@ -0,0 +1,8 @@
+<ipojo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="org.apache.felix.ipojo http://people.apache.org/~clement/ipojo/SCHEMA-DEV/core.xsd"
+ xmlns="org.apache.felix.ipojo">
+ <component name="provider" classname="org.apache.felix.ipojo.core.tests.components.MyComponent" version="1.0">
+ <provides/>
+ </component>
+ <instance component="provider" version="1.0"/>
+</ipojo>
\ No newline at end of file
diff --git a/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/vprovider-v2.xml b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/vprovider-v2.xml
new file mode 100644
index 0000000..9f814f8
--- /dev/null
+++ b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/main/resources/vprovider-v2.xml
@@ -0,0 +1,9 @@
+<ipojo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="org.apache.felix.ipojo http://people.apache.org/~clement/ipojo/SCHEMA-DEV/core.xsd"
+ xmlns="org.apache.felix.ipojo">
+ <component name="provider" classname="org.apache.felix.ipojo.core.tests.components.MyComponent" version="2.0">
+ <provides/>
+ </component>
+ <instance component="provider" version="2.0"/>
+
+</ipojo>
\ No newline at end of file
diff --git a/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/Common.java b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/Common.java
new file mode 100644
index 0000000..76eacae
--- /dev/null
+++ b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/Common.java
@@ -0,0 +1,196 @@
+package org.apache.felix.ipojo.tests.core;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.Logger;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.filefilter.TrueFileFilter;
+import org.apache.felix.ipojo.core.tests.services.MyService;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.ops4j.pax.exam.options.CompositeOption;
+import org.ops4j.pax.exam.options.DefaultCompositeOption;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+import org.ops4j.pax.tinybundles.core.TinyBundle;
+import org.ops4j.pax.tinybundles.core.TinyBundles;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.ow2.chameleon.testing.helpers.IPOJOHelper;
+import org.ow2.chameleon.testing.helpers.OSGiHelper;
+import org.ow2.chameleon.testing.tinybundles.ipojo.IPOJOStrategy;
+import org.slf4j.LoggerFactory;
+
+import javax.inject.Inject;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import static org.ops4j.pax.exam.CoreOptions.*;
+import static org.ops4j.pax.tinybundles.core.TinyBundles.withBnd;
+
+/**
+ * Bootstrap the test from this project
+ */
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerClass.class)
+public class Common {
+
+ @Inject
+ BundleContext bc;
+
+ OSGiHelper osgiHelper;
+ IPOJOHelper ipojoHelper;
+
+ Bundle testedBundle;
+
+ @Configuration
+ public Option[] config() throws IOException {
+ Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
+ root.setLevel(Level.INFO);
+
+ return options(
+ ipojoBundles(),
+ junitBundles(),
+ //testedBundle(),
+ systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level").value("WARN")
+ );
+ }
+
+ public Option createServiceBundleV1() throws MalformedURLException {
+ File out = new File("target/tested/service-v1.jar");
+
+ if (out.exists()) {
+ return bundle(out.toURI().toURL().toExternalForm());
+ }
+
+ InputStream is = TinyBundles.bundle()
+ .add(MyService.class)
+ .set(Constants.BUNDLE_SYMBOLICNAME, "ServiceInterface")
+ .set(Constants.EXPORT_PACKAGE, "org.apache.felix.ipojo.core.tests.services")
+ .build(withBnd());
+ try {
+ FileUtils.copyInputStreamToFile(is, out);
+ return bundle(out.toURI().toURL().toExternalForm());
+ } catch (MalformedURLException e) {
+ throw new RuntimeException("Cannot compute the url of the bundle");
+ } catch (IOException e) {
+ throw new RuntimeException("Cannot write of the bundle");
+ }
+ }
+
+ public Option createServiceBundleV2() throws MalformedURLException {
+ File out = new File("target/tested/service-v2.jar");
+
+ if (out.exists()) {
+ return bundle(out.toURI().toURL().toExternalForm());
+ }
+
+ InputStream is = TinyBundles.bundle()
+ .add(MyService.class)
+ .set(Constants.BUNDLE_SYMBOLICNAME, "ServiceInterface")
+ .set(Constants.EXPORT_PACKAGE, "org.apache.felix.ipojo.core.tests.services; version=\"2.0.0\"")
+ .build(withBnd());
+ try {
+ FileUtils.copyInputStreamToFile(is, out);
+ return bundle(out.toURI().toURL().toExternalForm());
+ } catch (MalformedURLException e) {
+ throw new RuntimeException("Cannot compute the url of the bundle");
+ } catch (IOException e) {
+ throw new RuntimeException("Cannot write of the bundle");
+ }
+ }
+
+
+
+
+
+
+
+ @Before
+ public void commonSetUp() {
+ osgiHelper = new OSGiHelper(bc);
+ ipojoHelper = new IPOJOHelper(bc);
+
+ // Dump OSGi Framework information
+ String vendor = (String) osgiHelper.getBundle(0).getHeaders().get(Constants.BUNDLE_VENDOR);
+ if (vendor == null) {
+ vendor = (String) osgiHelper.getBundle(0).getHeaders().get(Constants.BUNDLE_SYMBOLICNAME);
+ }
+ String version = (String) osgiHelper.getBundle(0).getHeaders().get(Constants.BUNDLE_VERSION);
+ System.out.println("OSGi Framework : " + vendor + " - " + version);
+ }
+
+ @After
+ public void commonTearDown() {
+ ipojoHelper.dispose();
+ osgiHelper.dispose();
+ }
+
+ public CompositeOption ipojoBundles() {
+ return new DefaultCompositeOption(
+ mavenBundle("org.apache.felix", "org.apache.felix.ipojo").versionAsInProject(),
+ mavenBundle("org.ow2.chameleon.testing", "osgi-helpers").versionAsInProject());
+ }
+
+ public Option testedBundle() throws MalformedURLException {
+ File out = new File("target/tested/bundle.jar");
+
+ TinyBundle tested = TinyBundles.bundle();
+
+ // We look inside target/classes to find the class and resources
+ File classes = new File("target/classes");
+ Collection<File> files = FileUtils.listFilesAndDirs(classes, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
+ List<File> services = new ArrayList<File>();
+ for (File file : files) {
+ if (file.isDirectory()) {
+ // By convention we export of .services and .service package
+ if (file.getName().endsWith("services") || file.getName().endsWith("service")) {
+ services.add(file);
+ }
+ } else {
+ // We need to compute the path
+ String path = file.getAbsolutePath().substring(classes.getAbsolutePath().length() + 1);
+ tested.add(path, file.toURI().toURL());
+ System.out.println(file.getName() + " added to " + path);
+ }
+ }
+
+ String export = "";
+ for (File file : services) {
+ if (export.length() > 0) {
+ export += ", ";
+ }
+ String path = file.getAbsolutePath().substring(classes.getAbsolutePath().length() + 1);
+ String packageName = path.replace('/', '.');
+ export += packageName;
+ }
+
+ System.out.println("Exported packages : " + export);
+
+ InputStream inputStream = tested
+ .set(Constants.BUNDLE_SYMBOLICNAME, "test.bundle")
+ .set(Constants.IMPORT_PACKAGE, "*")
+ .set(Constants.EXPORT_PACKAGE, export)
+ .build(IPOJOStrategy.withiPOJO(new File("src/main/resources")));
+
+ try {
+ FileUtils.copyInputStreamToFile(inputStream, out);
+ return bundle(out.toURI().toURL().toExternalForm());
+ } catch (MalformedURLException e) {
+ throw new RuntimeException("Cannot compute the url of the manipulated bundle");
+ } catch (IOException e) {
+ throw new RuntimeException("Cannot write of the manipulated bundle");
+ }
+ }
+
+
+}
diff --git a/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/FactoryVersionTest.java b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/FactoryVersionTest.java
new file mode 100644
index 0000000..58bbd38
--- /dev/null
+++ b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/FactoryVersionTest.java
@@ -0,0 +1,157 @@
+package org.apache.felix.ipojo.tests.core;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.felix.ipojo.architecture.Architecture;
+import org.apache.felix.ipojo.core.tests.components.MyComponent;
+import org.apache.felix.ipojo.core.tests.services.MyService;
+import org.junit.Assert;
+import org.junit.Test;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.tinybundles.core.TinyBundles;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.ow2.chameleon.testing.tinybundles.ipojo.IPOJOStrategy;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.ops4j.pax.exam.CoreOptions.bundle;
+
+public class FactoryVersionTest extends Common {
+
+
+ @org.ops4j.pax.exam.Configuration
+ public Option[] config() throws IOException {
+
+ Option[] options = super.config();
+
+ File provider1 = new File("target/tested/provider-v1.jar");
+ FileUtils.copyInputStreamToFile(
+ TinyBundles.bundle()
+ .add(MyComponent.class)
+ .set(Constants.BUNDLE_SYMBOLICNAME, "ProviderV1")
+ .set(Constants.IMPORT_PACKAGE, "org.apache.felix.ipojo.core.tests.services")
+ .build(IPOJOStrategy.withiPOJO(new File("src/main/resources/provider-v1.xml"))),
+ provider1);
+
+ File provider2 = new File("target/tested/provider-v1.1.jar");
+ FileUtils.copyInputStreamToFile(
+ TinyBundles.bundle()
+ .add(MyComponent.class)
+ .set(Constants.BUNDLE_SYMBOLICNAME, "ProviderV1.1")
+ .set(Constants.IMPORT_PACKAGE, "org.apache.felix.ipojo.core.tests.services")
+ .build(IPOJOStrategy.withiPOJO(new File("src/main/resources/provider-v1.1.xml"))),
+ provider2);
+
+ File instance = new File("target/tested/instance.jar");
+ FileUtils.copyInputStreamToFile(
+ TinyBundles.bundle()
+ .set(Constants.BUNDLE_SYMBOLICNAME, "Instances")
+ .build(IPOJOStrategy.withiPOJO(new File("src/main/resources/instances.xml"))),
+ instance);
+
+ List<Option> optionList = new ArrayList<Option>();
+ optionList.addAll(Arrays.asList(options));
+
+ optionList.add(createServiceBundleV1());
+ optionList.add(bundle(provider1.toURI().toURL().toExternalForm()));
+ optionList.add(bundle(provider2.toURI().toURL().toExternalForm()));
+ optionList.add(bundle(instance.toURI().toURL().toExternalForm()));
+
+ return optionList.toArray(new Option[optionList.size()]);
+ }
+
+ @Test
+ public void testDeploy() {
+ Bundle[] bundles = bc.getBundles();
+ for (Bundle bundle : bundles) {
+ System.out.println("Bundle deployed : " + bundle.getSymbolicName() + " / " + bundle.getState());
+ Assert.assertEquals(bundle.getSymbolicName() + " is not active", Bundle.ACTIVE, bundle.getState());
+ }
+ }
+
+ @Test
+ public void testInstanceArchitecture() {
+ // Version 1.0
+ ServiceReference refv1 = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "instance-v1");
+ Assert.assertNotNull(refv1);
+ Architecture archv1 = (Architecture) osgiHelper.getServiceObject(refv1);
+
+ String version = archv1.getInstanceDescription().getComponentDescription().getVersion();
+ Assert.assertEquals("1.0", version);
+
+ // Version 1.1
+ ServiceReference refv11 = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "instance-v1.1");
+ Assert.assertNotNull(refv11);
+ Architecture archv11 = (Architecture) osgiHelper.getServiceObject(refv11);
+
+ String version11 = archv11.getInstanceDescription().getComponentDescription().getVersion();
+ Assert.assertEquals("1.1", version11);
+
+ // No Version
+ ServiceReference refany = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "instance-any");
+ Assert.assertNotNull(refany);
+ Architecture archany = (Architecture) osgiHelper.getServiceObject(refany);
+
+ String any = archany.getInstanceDescription().getComponentDescription().getVersion();
+ Assert.assertNotNull(any);
+
+ // No version set in the factory, so no version.
+ ServiceReference refmci = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "MyComponentInstance");
+ Assert.assertNotNull(refmci);
+ Architecture archmcy = (Architecture) osgiHelper.getServiceObject(refmci);
+
+ String mci = archmcy.getInstanceDescription().getComponentDescription().getVersion();
+ Assert.assertNull(mci);
+
+ }
+
+ @Test
+ public void testServiceProperty() throws InvalidSyntaxException {
+
+ // Version 1.0
+ //ServiceReference refv1 = ipojoHelper.getServiceReferenceByName(MyService.class.getName(), "instance-v1");
+ ServiceReference[] refv1 = bc.getAllServiceReferences(MyService.class.getName(),
+ "(instance.name=instance-v1)");
+ Assert.assertNotNull(refv1);
+ String version = (String) refv1[0].getProperty("factory.version");
+ Assert.assertEquals("1.0", version);
+
+ // Version 1.1
+ for(ServiceReference ref : bc.getAllServiceReferences(null, null)) {
+ System.out.println("Service : " + ref.getProperty("instance.name"));
+ }
+
+ ServiceReference[] refv11 = bc.getAllServiceReferences(MyService.class.getName(),
+ "(instance.name=instance-v1.1)");
+ //ServiceReference refv11 = ipojoHelper.getServiceReferenceByName(MyService.class.getName(), "instance-v1.1");
+ Assert.assertNotNull(refv11);
+ String version11 = (String) refv11[0].getProperty("factory.version");
+
+ Assert.assertEquals("1.1", version11);
+
+ // No Version
+ ServiceReference[] refany = bc.getAllServiceReferences(MyService.class.getName(),
+ "(instance.name=instance-any)");
+
+ // ServiceReference refany = ipojoHelper.getServiceReferenceByName(MyService.class.getName(), "instance-any");
+ Assert.assertNotNull(refany);
+ String any = (String) refany[0].getProperty("factory.version");
+ Assert.assertNotNull(any);
+
+ // No version set in the factory, so no version.
+ ServiceReference[] refmci = bc.getAllServiceReferences(MyService.class.getName(),
+ "(instance.name=MyComponentInstance)");
+ //ServiceReference refmci = ipojoHelper.getServiceReferenceByName(MyService.class.getName(), "MyComponentInstance");
+ Assert.assertNotNull(refmci);
+ String mci = (String) refmci[0].getProperty("factory.version");
+ Assert.assertNull(mci);
+ }
+
+
+}
diff --git a/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/VersionConflictTest.java b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/VersionConflictTest.java
new file mode 100644
index 0000000..5f9cddf
--- /dev/null
+++ b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/VersionConflictTest.java
@@ -0,0 +1,286 @@
+package org.apache.felix.ipojo.tests.core;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.felix.ipojo.architecture.Architecture;
+import org.apache.felix.ipojo.architecture.HandlerDescription;
+import org.apache.felix.ipojo.core.tests.components.MyComponent;
+import org.apache.felix.ipojo.core.tests.components.MyCons;
+import org.apache.felix.ipojo.core.tests.services.MyService;
+import org.apache.felix.ipojo.handlers.dependency.Dependency;
+import org.apache.felix.ipojo.handlers.dependency.DependencyDescription;
+import org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription;
+import org.junit.Assert;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerMethod;
+import org.ops4j.pax.tinybundles.core.TinyBundles;
+import org.osgi.framework.*;
+import org.osgi.service.packageadmin.ExportedPackage;
+import org.osgi.service.packageadmin.PackageAdmin;
+import org.ow2.chameleon.testing.tinybundles.ipojo.IPOJOStrategy;
+
+import javax.inject.Inject;
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.ops4j.pax.exam.CoreOptions.options;
+import static org.ops4j.pax.exam.CoreOptions.systemProperty;
+import static org.ops4j.pax.tinybundles.core.TinyBundles.withBnd;
+
+@ExamReactorStrategy(PerMethod.class)
+public class VersionConflictTest extends Common {
+
+ @Inject
+ private BundleContext context;
+
+
+ @Configuration
+ public Option[] config() throws IOException {
+
+ List<Option> options = new ArrayList<Option>();
+ options.addAll(Arrays.asList(super.config()));
+
+ File tmp = new File("target/tmp");
+ tmp.mkdirs();
+
+ File f1 = new File(tmp, "service-interface-v1.jar");
+ FileUtils.copyInputStreamToFile(
+ TinyBundles.bundle()
+ .add(MyService.class)
+ .set(Constants.BUNDLE_SYMBOLICNAME, "ServiceInterfaceV1")
+ .set(Constants.EXPORT_PACKAGE, "org.apache.felix.ipojo.core.tests.services; version=\"1.0.0\"")
+ .build(withBnd()),
+ f1);
+
+ File f2 = new File(tmp, "service-interface-v2.jar");
+ FileUtils.copyInputStreamToFile(
+ TinyBundles.bundle()
+ .add(MyService.class)
+ .set(Constants.BUNDLE_SYMBOLICNAME, "ServiceInterfaceV2")
+ .set(Constants.BUNDLE_VERSION, "2.0.0")
+ .set(Constants.EXPORT_PACKAGE, "org.apache.felix.ipojo.core.tests.services; version=\"2.0.0\"")
+ .build(withBnd()),
+ f2);
+
+ File c1 = new File(tmp, "component-v1.jar");
+ FileUtils.copyInputStreamToFile(
+ TinyBundles.bundle()
+ .add(MyComponent.class)
+ .set(Constants.BUNDLE_SYMBOLICNAME, "ProviderV1")
+ .set(Constants.IMPORT_PACKAGE, "org.apache.felix.ipojo.core.tests.services; version=\"[1.0.0, 1.0.0]\"")
+ .build(IPOJOStrategy.withiPOJO(new File("src/main/resources/vprovider-v1.xml"))),
+ c1);
+
+ File c2 = new File(tmp, "component-v2.jar");
+ FileUtils.copyInputStreamToFile(
+ TinyBundles.bundle()
+ .add(MyComponent.class)
+ .set(Constants.BUNDLE_SYMBOLICNAME, "ProviderV2")
+ .set(Constants.IMPORT_PACKAGE, "org.apache.felix.ipojo.core.tests.services; version=\"[2.0.0," +
+ " 2.0.0]\"")
+ .build(IPOJOStrategy.withiPOJO(new File("src/main/resources/vprovider-v2.xml"))),
+ c2);
+
+ File cons = new File(tmp, "cons.jar");
+ FileUtils.copyInputStreamToFile(
+ TinyBundles.bundle()
+ .add(MyCons.class)
+ .set(Constants.BUNDLE_SYMBOLICNAME, "MyCons")
+ .set(Constants.IMPORT_PACKAGE, "org.apache.felix.ipojo.core.tests.services; version=\"[2.0.0, " +
+ "2.0.0]\"")
+ .set(Constants.BUNDLE_VERSION, "2.0")
+ .build(IPOJOStrategy.withiPOJO(new File("src/main/resources/cons.xml"))),
+ cons);
+
+ File consV1 = new File(tmp, "cons-v1.jar");
+ FileUtils.copyInputStreamToFile(
+ TinyBundles.bundle()
+ .add(MyCons.class)
+ .set(Constants.BUNDLE_SYMBOLICNAME, "MyCons")
+ .set(Constants.IMPORT_PACKAGE, "org.apache.felix.ipojo.core.tests.services; version=\"[1.0.0, " +
+ "2.0.0)\"")
+ .set(Constants.BUNDLE_VERSION, "1.0")
+ .build(IPOJOStrategy.withiPOJO(new File("src/main/resources/cons.xml"))),
+ consV1);
+
+ Option[] opt = options(
+ systemProperty("url1").value(f1.toURI().toURL().toExternalForm()),
+ systemProperty("url2").value(f2.toURI().toURL().toExternalForm()),
+
+ systemProperty("c1").value(c1.toURI().toURL().toExternalForm()),
+ systemProperty("c2").value(c2.toURI().toURL().toExternalForm()),
+ systemProperty("cons").value(cons.toURI().toURL().toExternalForm()),
+ systemProperty("consV1").value(consV1.toURI().toURL().toExternalForm())
+ );
+
+ options.addAll(Arrays.asList(opt));
+
+ return options.toArray(new Option[options.size()]);
+ }
+
+// @ProbeBuilder
+// public TestProbeBuilder probe(TestProbeBuilder builder) {
+// builder.setHeader(Constants.IMPORT_PACKAGE, "org.osgi.framework, org.apache.felix.ipojo, " +
+// "org.ow2.chameleon.testing.helpers, org.osgi.service.packageadmin, " +
+// "org.apache.felix.ipojo.architecture, org.apache.felix.ipojo.handlers.dependency");
+// builder.setHeader(Constants.DYNAMICIMPORT_PACKAGE, "org.ops4j.pax.exam,org.junit,javax.inject," +
+// "org.ops4j.pax.exam.options");
+// builder.setHeader("Bundle-ManifestVersion", "2");
+// return builder;
+// }
+
+ @Test
+ public void deployBundlesAtRuntime() throws MalformedURLException, BundleException, InvalidSyntaxException {
+
+ Bundle b1 = context.installBundle(context.getProperty("url1"));
+ b1.start();
+
+
+ Bundle b3 = context.installBundle(context.getProperty("c1"));
+ b3.start();
+
+ Bundle b2 = context.installBundle(context.getProperty("url2"));
+ b2.start();
+
+ Bundle b4 = context.installBundle(context.getProperty("c2"));
+ b4.start();
+
+ Bundle b5 = context.installBundle(context.getProperty("cons"));
+ b5.start();
+
+
+ Bundle[] bundles = context.getBundles();
+ for (Bundle bundle : bundles) {
+ System.out.println("bundle " + bundle.getSymbolicName() + " : " + (bundle.getState() == Bundle.ACTIVE));
+ //Assert.assertEquals(bundles[i].getSymbolicName() + " is not active", Bundle.ACTIVE, bundles[i].getState());
+ }
+
+ //TODO Migrate to new API, be aware that the new API may not be implemented on all platforms.
+ PackageAdmin pa = osgiHelper.getPackageAdmin();
+ Bundle b = pa.getBundles("ServiceInterfaceV1", null)[0];
+ ExportedPackage[] packages = pa.getExportedPackages(b);
+ if (packages == null) {
+ System.out.println("Packages ServiceInterfaceV1 : " + 0);
+ } else {
+ System.out.println("Packages ServiceInterfaceV1 : " + packages.length);
+ for (ExportedPackage p : packages) {
+ System.out.println("Package : " + p.getName() + " - " + p.getVersion().toString());
+ }
+ }
+ b = pa.getBundles("ServiceInterfaceV2", null)[0];
+ packages = pa.getExportedPackages(b);
+ System.out.println("Packages ServiceInterfaceV2 : " + packages.length);
+ for (ExportedPackage p : packages) {
+ System.out.println("Package : " + p.getName() + " - " + p.getVersion().toString());
+ }
+
+ osgiHelper.waitForService(Architecture.class.getName(), "(architecture.instance=mycons)", 2000);
+
+ // Check that the two services are provided.
+ ServiceReference[] refs = context.getAllServiceReferences("org.apache.felix.ipojo.core.tests.services.MyService", null);
+ Assert.assertNotNull(refs);
+ Assert.assertEquals(2, refs.length);
+
+ ServiceReference refv1 = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "mycons");
+ Assert.assertNotNull(refv1);
+ Architecture arch = (Architecture) osgiHelper.getServiceObject(refv1);
+
+ HandlerDescription desc = arch.getInstanceDescription().getHandlerDescription("org.apache.felix.ipojo:requires");
+ Assert.assertNotNull(desc);
+
+ DependencyHandlerDescription d = (DependencyHandlerDescription) desc;
+ Assert.assertNotNull(d.getDependencies());
+ Assert.assertEquals(1, d.getDependencies().length);
+
+ DependencyDescription dep = d.getDependencies()[0];
+ Assert.assertEquals(Dependency.RESOLVED, dep.getState());
+
+ Assert.assertEquals(1, dep.getServiceReferences().size());
+ ServiceReference r = (ServiceReference) dep.getServiceReferences().get(0);
+ Assert.assertEquals("provider", r.getProperty("factory.name"));
+ Assert.assertEquals("2.0", r.getProperty("factory.version"));
+ }
+
+ @Test
+ @Ignore("Does not work anymore, but the scenario runs as expected in a regular framework. The check find the " +
+ "version 2.0 of the service instead of the 1.0")
+ public void deployBundlesAtRuntimeV1() throws MalformedURLException, BundleException, InvalidSyntaxException {
+
+ Bundle b1 = context.installBundle(context.getProperty("url1"));
+ b1.start();
+
+
+ Bundle b3 = context.installBundle(context.getProperty("c1"));
+ b3.start();
+
+ Bundle b2 = context.installBundle(context.getProperty("url2"));
+ b2.start();
+
+ Bundle b4 = context.installBundle(context.getProperty("c2"));
+ b4.start();
+
+ Bundle b5 = context.installBundle(context.getProperty("consV1"));
+ b5.start();
+
+
+ Bundle[] bundles = context.getBundles();
+ for (int i = 0; i < bundles.length; i++) {
+ System.out.println("bundle " + bundles[i].getSymbolicName() + " : " + (bundles[i].getState() == Bundle.ACTIVE));
+ }
+
+
+ PackageAdmin pa = osgiHelper.getPackageAdmin();
+ Bundle b = pa.getBundles("ServiceInterfaceV1", null)[0];
+ ExportedPackage[] packages = pa.getExportedPackages(b);
+ if (packages == null) {
+ System.out.println("Packages ServiceInterfaceV1 : " + 0);
+ } else {
+ System.out.println("Packages ServiceInterfaceV1 : " + packages.length);
+ for (ExportedPackage p : packages) {
+ System.out.println("Package : " + p.getName() + " - " + p.getVersion().toString());
+ }
+ }
+ b = pa.getBundles("ServiceInterfaceV2", null)[0];
+ packages = pa.getExportedPackages(b);
+ System.out.println("Packages ServiceInterfaceV2 : " + packages.length);
+ for (ExportedPackage p : packages) {
+ System.out.println("Package : " + p.getName() + " - " + p.getVersion().toString());
+ }
+
+ osgiHelper.waitForService(Architecture.class.getName(), "(architecture.instance=mycons)", 2000);
+
+ // Check that the two services are provided.
+ ServiceReference[] refs = context.getAllServiceReferences("org.apache.felix.ipojo.core.tests.services.MyService", null);
+ Assert.assertNotNull(refs);
+ Assert.assertEquals(2, refs.length);
+
+ ServiceReference refv1 = ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "mycons");
+ Assert.assertNotNull(refv1);
+ Architecture arch = (Architecture) osgiHelper.getServiceObject(refv1);
+
+ HandlerDescription desc = arch.getInstanceDescription().getHandlerDescription("org.apache.felix.ipojo:requires");
+ Assert.assertNotNull(desc);
+
+ DependencyHandlerDescription d = (DependencyHandlerDescription) desc;
+ Assert.assertNotNull(d.getDependencies());
+ Assert.assertEquals(1, d.getDependencies().length);
+
+ DependencyDescription dep = d.getDependencies()[0];
+ Assert.assertEquals(Dependency.RESOLVED, dep.getState());
+
+ Assert.assertEquals(1, dep.getServiceReferences().size());
+ ServiceReference r = (ServiceReference) dep.getServiceReferences().get(0);
+
+ Assert.assertEquals("provider", r.getProperty("factory.name"));
+ Assert.assertEquals("1.0", r.getProperty("factory.version"));
+ }
+
+
+}