Maven + OSGi + Jboss7 + Arquillian
f_cavarretta Feb 22, 2012 11:20 AMHi,
I'm tring to use arquillian framework to write some test for an OSGì bundle that runs on JbossA7 7.1.0 Final distribution. As far as now I could not get any sample working :-(
Jboss is started as managed container
The root cause of the erroe seems to be:
Caused by: org.jboss.osgi.resolver.XResolverException: Unable to resolve Module[example-arquillian.jar:0.0.0]: missing requirement [Module[example-arquillian.jar:0.0.0]] package; (package=org.junit.runner)
at org.jboss.osgi.resolver.felix.FelixResolver.resolveInternal(FelixResolver.java:117) [jbosgi-resolver-felix-1.0.11.jar:1.0.11]
at org.jboss.osgi.resolver.spi.AbstractResolver.resolve(AbstractResolver.java:149) [jbosgi-resolver-spi-1.0.11.jar:1.0.11]
at org.jboss.osgi.framework.internal.ResolverPlugin.resolve(ResolverPlugin.java:155) [jbosgi-framework-core-1.1.5.jar:1.1.5]
The pom is:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jboss-arquillian</groupId>
<artifactId>unittest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<arquillian.version>1.0.0.CR7</arquillian.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.jbossas_7>7.1.0.Final</version.jbossas_7>
<jboss-as-maven-plugin-version>7.1.0.Final</jboss-as-maven-plugin-version>
<version.arquillian_container_jbossas>1.0.0.Final</version.arquillian_container_jbossas>
<version.jboss_javaee6_spec>3.0.0.Beta1</version.jboss_javaee6_spec>
<version.shrinkwrap_resolver_impl_maven>1.0.0-beta-6</version.shrinkwrap_resolver_impl_maven>
<version.shrinkwrap_api>1.0.0-beta-6</version.shrinkwrap_api>
</properties>
<name>unittest</name>
<url>http://maven.apache.org</url>
<pluginRepositories>
<pluginRepository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
<layout>default</layout>
</pluginRepository>
</pluginRepositories>
<repositories>
<repository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
<layout>default</layout>
</repository>
<repository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>jboss-central</id>
<name>JBoss Central</name>
<url>https://repository.jboss.org/nexus/content/repositories/central/</url>
<layout>default</layout>
</repository>
<repository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>jboss-deprecated</id>
<name>JBoss Deprecated</name>
<url>https://repository.jboss.org/nexus/content/repositories/deprecated/</url>
<layout>default</layout>
</repository>
<repository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>jboss-public</id>
<name>JBoss Public</name>
<url>https://repository.jboss.org/nexus/content/repositories/public-jboss/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>${version.jboss_javaee6_spec}</version>
<type>pom</type>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.apache.xalan</groupId>
<artifactId>xalan</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-web-6.0</artifactId>
<version>${version.jboss_javaee6_spec}</version>
<type>pom</type>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.apache.xalan</groupId>
<artifactId>xalan</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.0-SP4</version>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>${arquillian.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.protocol</groupId>
<artifactId>arquillian-protocol-jmx</artifactId>
<version>${arquillian.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.protocol</groupId>
<artifactId>arquillian-protocol-osgi</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.protocol</groupId>
<artifactId>arquillian-protocol-servlet</artifactId>
<version>${arquillian.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-junit</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<!-- <dependency> <groupId>org.jboss.shrinkwrap.descriptors</groupId> <artifactId>shrinkwrap-descriptors-api</artifactId>
<version>1.0.0-beta-1</version> <scope>test</scope> <optional>false</optional>
</dependency> -->
<dependency>
<groupId>org.jboss.shrinkwrap</groupId>
<artifactId>shrinkwrap-api</artifactId>
<version>${version.shrinkwrap_api}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-impl-maven</artifactId>
<version>${version.shrinkwrap_resolver_impl_maven}</version>
</dependency>
<dependency>
<groupId>org.apache.xalan</groupId>
<artifactId>serializer</artifactId>
<version>2.7.1-1.jbossorg</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-arquillian-container-managed</artifactId>
<version>${version.jbossas_7}</version>
</dependency>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-web-6.0</artifactId>
<version>${version.jboss_javaee6_spec}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemProperties>
<jboss.version>${version.jbossas_7}</jboss.version>
</systemProperties>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>process-test-classes</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-dist</artifactId>
<version>${version.jbossas_7}</version>
<type>zip</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<inherited>true</inherited>
<configuration>
<source>1.6</source>
<target>1.6</target>
<showDeprecation>false</showDeprecation>
<showWarnings>true</showWarnings>
<optimize>true</optimize>
<compilerVersion>1.6</compilerVersion>
<fork>true</fork>
<argLine>-Xmx512M</argLine>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<versionRange>[2.0,)</versionRange>
<goals>
<goal>unpack</goal>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
The test class is:
package jboss.arquillian.test1;
import java.io.InputStream;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.osgi.testing.OSGiManifestBuilder;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
@RunWith(Arquillian.class)
public class SimpleArquillianTestCase {
@Inject
public Bundle bundle;
@Deployment
public static JavaArchive createdeployment() {
final JavaArchive archive = ShrinkWrap.create(JavaArchive.class,
"example-arquillian.jar");
archive.addPackage(Activator.class.getPackage());
archive.setManifest(new Asset() {
public InputStream openStream() {
OSGiManifestBuilder builder = OSGiManifestBuilder.newInstance();
builder.addBundleSymbolicName(archive.getName());
builder.addBundleManifestVersion(2);
builder.addBundleActivator(Activator.class.getName());
builder.addImportPackages(org.junit.runner.JUnitCore.class);
builder.addExportPackages(SimpleArquillianTestCase.class);
builder.addImportPackages("org.jboss.arquillian.junit");
builder.addImportPackages("org.jboss.shrinkwrap.api",
"org.jboss.shrinkwrap.api.asset",
"org.jboss.shrinkwrap.api.spec");
builder.addImportPackages("javax.inject", "org.junit",
"org.junit.runner");
return builder.openStream();
}
});
return archive;
}
@Test
public void testBundleInjection() throws Exception {
// Assert that the bundle is injected
Assert.assertNotNull("Bundle injected", bundle);
// Assert that the bundle is in state RESOLVED
// Note when the test bundle contains the test case it
// must be resolved already when this test method is called
Assert.assertEquals("Bundle RESOLVED", Bundle.RESOLVED,
bundle.getState());
// Start the bundle
bundle.start();
Assert.assertEquals("Bundle ACTIVE", Bundle.ACTIVE, bundle.getState());
// Get the service reference
BundleContext context = bundle.getBundleContext();
ServiceReference sref = context.getServiceReference(Service.class
.getName());
Assert.assertNotNull("ServiceReference not null", sref);
// Get the service for the reference
Service service = (Service) context.getService(sref);
Assert.assertNotNull("Service not null", service);
// Invoke the service
int sum = service.sum(1, 2, 3);
Assert.assertEquals(6, sum);
// Stop the bundle
bundle.stop();
Assert.assertEquals("Bundle RESOLVED", Bundle.RESOLVED,
bundle.getState());
}
}
Thanks in advance for your help.
Ceers,
Fulvio