Fix issue FELIX-2093 : iPOJO doesn't always use the correct class loader to load Nullable classes
This came from the dynamic proxy strange behavior. The proxy don't use the specification classloader to load transitive classes. To fix that, the delegation is made manually.
This commit also update some tests to the latest pax:exam / tinybundle API.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@912323 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/tests/core/service-dependency-optional/pom.xml b/ipojo/tests/core/service-dependency-optional/pom.xml
new file mode 100644
index 0000000..2bced55
--- /dev/null
+++ b/ipojo/tests/core/service-dependency-optional/pom.xml
@@ -0,0 +1,121 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>ipojo.tests</groupId>
+ <artifactId>tests.core.dependency.optional</artifactId>
+ <name>iPOJO Optional Dependency Test Suite</name>
+ <version>1.5.0-SNAPSHOT</version>
+ <description>Test the optional dependencies</description>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.servicemix.tooling</groupId>
+ <artifactId>depends-maven-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>generate-depends-file</id>
+ <goals>
+ <goal>generate-depends-file</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.ipojo</artifactId>
+ <version>${pom.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.ipojo.annotations</artifactId>
+ <version>${pom.version}</version>
+ </dependency>
+ <!--
+ <dependency> <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.log</artifactId>
+ <version>1.0.0</version> </dependency>
+ -->
+
+ <!--
+ Pax Exam API:
+ -->
+ <dependency>
+ <groupId>org.ops4j.pax.exam</groupId>
+ <artifactId>pax-exam</artifactId>
+ <version>1.2.0</version>
+ </dependency>
+ <!--
+ During runtime Pax Exam will discover the OSGi container to use by
+ searching metadata available into classpath. Pax Exam comes with a
+ default container that uses [Pax Runner] for implementing the
+ container requirements:
+ -->
+ <dependency>
+ <groupId>org.ops4j.pax.exam</groupId>
+ <artifactId>pax-exam-container-default
+ </artifactId>
+ <version>1.2.0</version>
+ </dependency>
+ <!--
+ If your test code is based on JUnit you will have to have the Junit
+ support artifact:
+ -->
+ <dependency>
+ <groupId>org.ops4j.pax.exam</groupId>
+ <artifactId>pax-exam-junit</artifactId>
+ <version>1.2.0</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.5</version>
+ <type>jar</type>
+ <scope>test</scope>
+ </dependency>
+ <!-- Tinybundles -->
+ <dependency>
+ <groupId>org.ops4j.pax.swissbox</groupId>
+ <artifactId>pax-swissbox-tinybundles</artifactId>
+ <version>1.2.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.ipojo.tinybundles.bundleAsiPOJO
+ </artifactId>
+ <version>${pom.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.ipojo.test.helpers</artifactId>
+ <version>${pom.version}</version>
+ </dependency>
+ </dependencies>
+
+ <repositories>
+ <repository>
+ <id>ops4j.releases</id>
+ <name>OPS4J Release</name>
+ <url> http://repository.ops4j.org/maven2/</url>
+ <releases>
+ <enabled>true</enabled>
+ </releases>
+ <snapshots>
+ <enabled>false</enabled>
+ </snapshots>
+ </repository>
+ </repositories>
+
+</project>
diff --git a/ipojo/tests/core/service-dependency-optional/src/main/java/org/apache/felix/ipojo/optional/MyComponent.java b/ipojo/tests/core/service-dependency-optional/src/main/java/org/apache/felix/ipojo/optional/MyComponent.java
new file mode 100644
index 0000000..b7a13ef
--- /dev/null
+++ b/ipojo/tests/core/service-dependency-optional/src/main/java/org/apache/felix/ipojo/optional/MyComponent.java
@@ -0,0 +1,23 @@
+package org.apache.felix.ipojo.optional;
+
+import org.apache.felix.ipojo.Nullable;
+import org.apache.felix.ipojo.annotations.Component;
+import org.apache.felix.ipojo.annotations.Requires;
+import org.osgi.service.log.LogService;
+
+
+@Component(immediate=true, name="optional-log-cons")
+public class MyComponent {
+
+ @Requires(optional=true, proxy=false)
+ private LogService log;
+
+
+ public MyComponent() {
+ System.out.println("Created ! : " + (log instanceof Nullable) + " - " + log);
+ log.log(LogService.LOG_INFO, "Created !");
+
+ }
+
+
+}
diff --git a/ipojo/tests/core/service-dependency-optional/src/main/resources/metadata.xml b/ipojo/tests/core/service-dependency-optional/src/main/resources/metadata.xml
new file mode 100644
index 0000000..c8a1900
--- /dev/null
+++ b/ipojo/tests/core/service-dependency-optional/src/main/resources/metadata.xml
@@ -0,0 +1,2 @@
+<ipojo>
+</ipojo>
diff --git a/ipojo/tests/core/service-dependency-optional/src/test/java/org/apache/felix/ipojo/test/optional/NullableTransitiveClassloadingTest.java b/ipojo/tests/core/service-dependency-optional/src/test/java/org/apache/felix/ipojo/test/optional/NullableTransitiveClassloadingTest.java
new file mode 100644
index 0000000..6de1cc1
--- /dev/null
+++ b/ipojo/tests/core/service-dependency-optional/src/test/java/org/apache/felix/ipojo/test/optional/NullableTransitiveClassloadingTest.java
@@ -0,0 +1,98 @@
+package org.apache.felix.ipojo.test.optional;
+
+import static org.ops4j.pax.exam.CoreOptions.equinox;
+import static org.ops4j.pax.exam.CoreOptions.felix;
+import static org.ops4j.pax.exam.CoreOptions.knopflerfish;
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.CoreOptions.options;
+import static org.ops4j.pax.exam.CoreOptions.provision;
+import static org.ops4j.pax.exam.MavenUtils.asInProject;
+
+import java.io.File;
+
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.ConfigurationException;
+import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.MissingHandlerException;
+import org.apache.felix.ipojo.UnacceptableConfiguration;
+import org.apache.felix.ipojo.optional.MyComponent;
+import org.apache.felix.ipojo.test.helpers.IPOJOHelper;
+import org.apache.felix.ipojo.test.helpers.OSGiHelper;
+import org.apache.felix.ipojo.tinybundles.BundleAsiPOJO;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Inject;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.Configuration;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.ops4j.pax.swissbox.tinybundles.core.TinyBundles;
+import org.osgi.framework.BundleContext;
+
+import aQute.lib.osgi.Constants;
+
+
+/**
+ * Reproduces FELIX-2093
+ * iPOJO doesn't always use the correct class loader to load nullable object.
+ */
+@RunWith( JUnit4TestRunner.class )
+public class NullableTransitiveClassloadingTest {
+
+ @Inject
+ private BundleContext context;
+
+ private OSGiHelper osgi;
+
+ private IPOJOHelper ipojo;
+
+ @Before
+ public void init() {
+ osgi = new OSGiHelper(context);
+ ipojo = new IPOJOHelper(context);
+ }
+
+ @After
+ public void stop() {
+ ipojo.dispose();
+ osgi.dispose();
+ }
+
+ @Configuration
+ public static Option[] configure() {
+
+ File tmp = new File("target/tmp");
+ tmp.mkdirs();
+
+
+ Option[] opt = options(
+ felix(),
+ equinox(),
+ knopflerfish(),
+ provision(
+ // Runtime.
+ mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.ipojo").version(asInProject()),
+ mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.ipojo.test.helpers").version(asInProject())
+ ),
+ provision(
+ TinyBundles.newBundle()
+ .add(MyComponent.class)
+ .set(Constants.IMPORT_PACKAGE, "*")
+ .build(BundleAsiPOJO.asiPOJOBundle(new File("src/main/resources/metadata.xml"))
+ )
+
+ ));
+
+ return opt;
+ }
+
+ @Test
+ public void testCreation() throws UnacceptableConfiguration, MissingHandlerException, ConfigurationException {
+ Factory factory = ipojo.getFactory("optional-log-cons");
+ ComponentInstance ci = factory.createComponentInstance(null);
+
+ ci.dispose();
+ }
+
+}