arquillian是JBoss公司的集成测试工具,支持ejb,cloud,android等目标容器的测试,本文以http://books.sonatype.com/mcookbook/reference/osgi-sect-starting-osgi-container.html为基础,演示了在osgi环境下,如何进行集成测试。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<relativePath>../poms/compiled/</relativePath>
<groupId>com.liming.platform.project.build</groupId>
<artifactId>compiled-bundle-settings</artifactId>
<version>1.0</version>
</parent>
<properties>
<bundle.symbolicName>com.liming.platform</bundle.symbolicName>
<bundle.namespace>com.liming.platform</bundle.namespace>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>com.liming.platform.project</groupId>
<artifactId>com.liming.platform</artifactId>
<version>1.0-SNAPSHOT</version>
<name>${bundle.symbolicName}</name>
<packaging>bundle</packaging>
<dependencies>
<!--
| uncomment to add all imported (non-local) bundles to your compilation classpath
<dependency>
<type>pom</type>
<groupId>${parent.groupId}</groupId>
<artifactId>provision</artifactId>
<optional>true</optional>
</dependency>
-->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
</dependency>
<!--<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>provided</scope>
</dependency>-->
<!--<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.2</version>
<scope>provided</scope>
</dependency>-->
<dependency>
<groupId>org.ops4j.pax.logging</groupId>
<artifactId>pax-logging-api</artifactId>
<version>1.7.1</version>
<scope>provided</scope>
</dependency>
<!-- OSGi Arquillian -->
<!--<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-container-osgi-embedded</artifactId>
<version>2.0.0.CR3</version>
<scope>test</scope>
</dependency>-->
<!--<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>2.0.0.CR3</version>
<scope>test</scope>
</dependency>-->
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-container-karaf-embedded</artifactId>
<version>2.1.0.CR4</version>
<scope>test</scope>
</dependency>
<!--<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-container-felix-embedded</artifactId>
<version>2.1.0.CR4</version>
<scope>test</scope>
</dependency>-->
<!--<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-container-jbosgi-embedded</artifactId>
<version>2.1.0.CR4</version>
</dependency>-->
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.2.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>1.1.2.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Maven Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
</repositories>
</project>
以上是pom文件,可以运行felix和karaf容器,具体的测试代码如下:
package com.liming.internal.platform;
import com.liming.platform.ExampleService;
import com.liming.platform.internal.ExampleActivator;
import com.liming.platform.internal.ExampleServiceImpl;
import org.apache.log4j.Logger;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.osgi.metadata.OSGiManifestBuilder;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import java.io.InputStream;
import static org.junit.Assert.*;
import org.jboss.arquillian.junit.Arquillian;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.util.tracker.ServiceTracker;
@RunWith(Arquillian.class)
public class ExampleActivatorTest
{
@Deployment
public static JavaArchive createdeployment()
{
final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "test.jar");
archive.addClasses( ExampleActivator.class, ExampleServiceImpl.class,ExampleService.class,Logger.class);
archive.setManifest(new Asset()
{
public InputStream openStream()
{
OSGiManifestBuilder builder = OSGiManifestBuilder.newInstance();
builder.addBundleSymbolicName("test.jar");
builder.addBundleManifestVersion(2);
builder.addBundleActivator(ExampleActivator.class);
builder.addImportPackages(PackageAdmin.class, BundleContext.class,
ServiceTracker.class, Logger.class);
/*builder.addBundleVersion("1.0-SNAPSHOT");
builder.addImportPackages("org.apache.log4j.Logger");*/
return builder.openStream();
}
});
return archive;
}
@ArquillianResource
// @Inject
public Bundle bundle;
@ArquillianResource
BundleContext context;
@Test
public void testBundleInjection() throws Exception
{
// Assert that the bundle is injected
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
assertEquals("Bundle RESOLVED", Bundle.RESOLVED, bundle.getState());
// Start the bundle
bundle.start();
assertEquals("Bundle ACTIVE", Bundle.ACTIVE, bundle.getState());
// Get the service reference
//BundleContext context = bundle.getBundleContext();
ServiceReference sref = context.getServiceReference(ExampleService.class.getName());
assertNotNull("ServiceReference not null", sref);
// Get the service for the reference
ExampleService service = (ExampleService)context.getService(sref);
assertNotNull("Service not null",service);
// Invoke the service
String sum = service.scramble("123");
assertNotNull("321", sum);
// Stop the bundle
bundle.stop();
assertEquals("Bundle RESOLVED", Bundle.RESOLVED, bundle.getState());
}
}
https://community.jboss.org/wiki/JBossOSGi-210Released 这里可以下载jbossOSGI,安装后example目录下有很多测试的例子,可以参考。