Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSGi Bundle for issue #11 #32

Closed
wants to merge 14 commits into from
9 changes: 9 additions & 0 deletions .idea/libraries/OSGi.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 75 additions & 2 deletions build.xml
@@ -1,13 +1,43 @@
<project name="hamcrest" default="all">

<property name="version" value="SNAPSHOT" description="Version number to use in build files"/>
<property name="version" value="0.0.0-SNAPSHOT" description="Version number to use in build files"/>
<property name="haltonfailure" value="true" description="Whether to halt the build if the tests fail"/>
<property name="debug" value="true" description="Whether to build with debug information"/>
<property name="javaversion" value="1.5" description="Java version to target"/>
<tstamp><format property="build.timestamp" pattern="yyyy-MM-dd HH:mm:ss" timezone="GMT"/></tstamp>

<target name="all" depends="clean, bigjar" description="Performs clean build, runs tests and packages for distribution"/>

<target name="examples-tests" depends="all, osgi, examples">
<path id="cp-hamcrest-examples-test-complete">
<path id="cp-hamcrest-examples-main" path=""/>
<path id="cp-hamcrest-examples-test" path=""/>
<path path="build/hamcrest-api-${version}.jar"/>
<path path="build/hamcrest-core-${version}.jar"/>
<path path="build/hamcrest-library-${version}.jar"/>
<path path="build/hamcrest-integration-${version}.jar"/>
<fileset dir="lib/osgi" includes="org.apache.felix.framework-*.jar"/>
<fileset dir="lib/integration" includes="easymock-*.jar"/>
<fileset dir="lib/integration" includes="jmock-*.jar"/>
<fileset dir="lib/integration" includes="junit-dep-*.jar"/>
<fileset dir="lib/integration" includes="testng-*.jar"/>
</path>
<delete file="build/hamcrest-examples-${version}-tests.jar"/>
<copy file="build/hamcrest-examples-${version}.jar" tofile="build/hamcrest-examples-${version}-tests.jar"/>
<jar-osgiconsumer/>
<test-module modulename="hamcrest-examples" testpattern="ExampleWith*"/>
</target>

<target name="osgi" description="Build OSGi bundles and generate maven jars for generator, core, library, integration, all">
<parallel>
<bundle-osgi-module modulename="hamcrest-generator"/>
<bundle-osgi-module modulename="hamcrest-core"/>
<bundle-osgi-module modulename="hamcrest-library"/>
<bundle-osgi-module modulename="hamcrest-integration"/>
<bundle-osgi-module modulename="hamcrest-all"/>
</parallel>
</target>

<target name="clean" description="Clean up all built files">
<delete dir="build"/>
</target>
Expand Down Expand Up @@ -75,6 +105,7 @@
<fileset file="build/hamcrest-library-${version}.jar"/>
<fileset file="build/hamcrest-integration-${version}.jar"/>
<fileset dir="lib/integration" includes="**/*.jar"/>
<fileset dir="lib/osgi" includes="org.apache.felix.framework-*.jar"/>
</path>
<compile-module modulename="hamcrest-examples"/>
<jar-module modulename="hamcrest-examples"/>
Expand Down Expand Up @@ -230,6 +261,7 @@

<macrodef name="test-module" description="run unit tests.">
<attribute name="modulename" description="Name of the module to jar"/>
<attribute name="testpattern" default="*Test"/>
<sequential>
<mkdir dir="build/temp/@{modulename}/test-wrk"/>
<junit printsummary="no" forkmode="once" tempdir="build/temp/@{modulename}/test-wrk" haltonfailure="${haltonfailure}">
Expand All @@ -238,7 +270,9 @@
<classpath path="build/@{modulename}-${version}-tests.jar"/>
<batchtest fork="yes" todir="${build.dir}/testreport">
<zipfileset src="build/@{modulename}-${version}-tests.jar">
<include name="org/hamcrest/**/*Test.class"/>
<include name="org/hamcrest/**/@{testpattern}.class"/>
<exclude name="**/*$*.class"/>
<exclude name="**/testng/*.class"/>
<exclude name="**/Abstract*.class"/>
</zipfileset>
</batchtest>
Expand Down Expand Up @@ -269,19 +303,58 @@
</sequential>
</macrodef>

<macrodef name="bundle-osgi-module" description="Build OSGi bundle and generate maven bundle jar for a module">
<attribute name="modulename" description="Name of the module to bundle"/>
<sequential>
<make-osgi-bundle modulename="@{modulename}"/>
<sign target="@{modulename}-${version}.jar"/>
<jar-bundle modulename="@{modulename}"/>
</sequential>
</macrodef>

<macrodef name="jar-bundle" description="Jar maven bundle contents">
<attribute name="modulename" description="Name of the module to create bundle jar for"/>
<sequential>
<delete file="build/maven-bundle-@{modulename}.jar"/>
<jar destfile="build/maven-bundle-@{modulename}.jar">
<fileset dir="build"><include name="@{modulename}-${version}*"/></fileset>
</jar>
</sequential>
</macrodef>

<macrodef name="jar-osgiconsumer">
<sequential>
<mkdir dir="build/temp/hamcrest-osgiconsumer-${version}.jar.contents"/>
<copy file="build/temp/hamcrest-examples-${version}.jar.contents/org/hamcrest/examples/osgi/TestActivator.class"
todir="build/temp/hamcrest-osgiconsumer-${version}.jar.contents/org/hamcrest/examples/osgi"/>
<mkdir dir="build/temp/hamcrest-osgiconsumer-${version}.jar.manifest"/>
<copy file="build/temp/hamcrest-examples-${version}.jar.manifest/MANIFEST.MF"
todir="build/temp/hamcrest-osgiconsumer-${version}.jar.manifest"/>
<jar-module-component modulename="hamcrest-osgiconsumer"/>
<bundle-osgi-module modulename="hamcrest-osgiconsumer"/>
</sequential>
</macrodef>

<macrodef name="sign" description="Sign a file with a gpg key">
<attribute name="target" description="Name of the resource to sign"/>
<sequential>
<exec executable="gpg" dir="build"><arg value="-ab"/><arg value="@{target}"/></exec>
</sequential>
</macrodef>

<macrodef name="make-osgi-bundle" description="Extract JAR and create OSGi bundle">
<attribute name="modulename" description="Name of the OSGi bundle to jar"/>
<sequential>
<taskdef resource="aQute/bnd/ant/taskdef.properties" classpath="lib/integration/bnd-1.50.0.jar"/>
<unzip src="build/@{modulename}-${version}.jar" dest="build/temp/@{modulename}-${version}.jar.unzip"/>
<delete file="build/temp/@{modulename}-${version}.jar.unzip/META-INF/MANIFEST.MF"/>
<delete file="build/@{modulename}-${version}.jar"/>
<bnd classpath="build/temp/@{modulename}-${version}.jar.unzip" output="build/@{modulename}-${version}.jar" files="scripts/@{modulename}.bnd" eclipse="false" exceptions="true"/>
<mkdir dir="build/temp/@{modulename}-${version}.jar.license/META-INF"/>
<copy file="LICENSE.txt" todir="build/temp/@{modulename}-${version}.jar.license/META-INF"/>
<jar jarfile="build/@{modulename}-${version}.jar" update="true">
<fileset dir="build/temp/@{modulename}-${version}.jar.license"/>
</jar>
</sequential>
</macrodef>
</project>
1 change: 1 addition & 0 deletions hamcrest-examples/hamcrest-examples.iml
Expand Up @@ -10,6 +10,7 @@
<orderEntry type="library" name="integration" level="project" />
<orderEntry type="library" name="ant_output" level="project" />
<orderEntry type="module" module-name="hamcrest-integration" />
<orderEntry type="library" name="OSGi" level="project" />
</component>
</module>

Expand Up @@ -44,7 +44,7 @@ public String whichOne(boolean first) {
}
}

@Test
@Test(expected = AssertionError.class)
public void showMismatch() {
ComplicatedClass complicated = new ComplicatedClass();

Expand Down
@@ -0,0 +1,39 @@
package org.hamcrest.examples.osgi;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Collection;

/**
* @author Tibor Digana (tibor17)
*/
final class BundlesUtil {

private BundlesUtil() {
}

static Collection<String> relativePathFiles(String relativePath, final String... fileNamesRegex) {
relativePath = relativePath.trim();
File dir = new File(relativePath).getAbsoluteFile();
File[] jars = dir.listFiles(new FileFilter() {
public boolean accept(File pathname) {
if (pathname.isFile()) {
String fileName = pathname.getName();
for (String regex : fileNamesRegex) {
if (fileName.matches(regex)) {
return true;
}
}
}
return false;
}
});
ArrayList<String> paths = new ArrayList<String>(jars.length);
relativePath = relativePath.length() == 0 ? "" : relativePath + "/";
for (File jar : jars) {
paths.add(relativePath + jar.getName());
}
return paths;
}
}
@@ -0,0 +1,82 @@
/* Copyright (c) 2000-2010 hamcrest.org
*/
package org.hamcrest.examples.osgi;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

/**
* Demonstrates that the Hamcrest's OSGi bundles can be used by some consumer.
*
* @author Tibor Digana (tibor17)
*/
public class ExampleWithOSGi {
private Framework framework;

@Before
public void init() throws Exception {
// Create a framework
Map<String, String> config = new HashMap<String, String>();
// OSGi stores its persistent data:
config.put(Constants.FRAMEWORK_STORAGE, new File("build/osgi-cache").getCanonicalPath());
// Request OSGi to clean its storage area on startup
config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "onFirstInit");
// current versions of JUnit and Easymock are not OSGi bundles in this project
// load them from system class loader
config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "org.junit,org.easymock");
ServiceLoader<FrameworkFactory> service = ServiceLoader.load(FrameworkFactory.class);
FrameworkFactory frameworkFactory = service.iterator().next();
framework = frameworkFactory.newFramework(config);
framework.init();
}

@After
public void deinit() throws Exception {
BundleContext context = framework.getBundleContext();
for (Bundle bundle : context.getBundles()) {
if (bundle.getSymbolicName().equals("hamcrest-test")) {
bundle.stop();
}
}

framework.stop();
framework.waitForStop(0);
}

@Test
public void testOSGi() throws Exception {
BundleContext context = framework.getBundleContext();
String coreRegex = "^hamcrest-core-([^-])+.jar$";
String libRegex = "^hamcrest-library-([^-])+.jar$";
String intRegex = "^hamcrest-integration-([^-])+.jar$";
String consumerReges = "^hamcrest-osgiconsumer-([^-])+.jar$";
for (String bundle : BundlesUtil.relativePathFiles("build", coreRegex, libRegex, intRegex, consumerReges)) {
context.installBundle("file:" + bundle);
}

framework.start();

System.setProperty("osgi.test.passed", "false");

for (Bundle bundle : context.getBundles()) {
bundle.start();
}

assertThat(System.getProperty("osgi.test.passed"), is("true"));
}
}
@@ -0,0 +1,43 @@
package org.hamcrest.examples.osgi;

import org.hamcrest.JMock1Matchers;
import org.hamcrest.StringDescription;
import org.hamcrest.collection.IsArrayContaining;
import org.hamcrest.core.Is;
import org.hamcrest.integration.EasyMock2Adapter;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import static org.junit.Assert.fail;

/**
* Attempts to fail the OSGi framework as soon as core's classes
* which are supposed to be successfully imported cannot be loaded.
*
* @author Tibor Digana (tibor17)
*/
public class TestActivator implements BundleActivator {
@Override
public void start(BundleContext bundleContext) throws Exception {
new Is(null);
// see the section Require-Bundle in library-consumer.jar!/META-INF/MANIFEST.MF
new StringDescription();
try {
Class.forName("org.hamcrest.internal.ArrayIterator");
fail();
} catch (ClassNotFoundException e) {
// expected: org.hamcrest.internal is private package in osgi bundle
}

new IsArrayContaining(null);

new EasyMock2Adapter(null);
// see the section Require-Bundle in library-consumer.jar!/META-INF/MANIFEST.MF
new JMock1Matchers();
System.setProperty("osgi.test.passed", "true");
}

@Override
public void stop(BundleContext bundleContext) throws Exception {
}
}
9 changes: 9 additions & 0 deletions hamcrest-examples/src/main/resources/META-INF/MANIFEST.MF
@@ -0,0 +1,9 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Hamcrest hamcrest test
Bundle-SymbolicName: hamcrest-test
Bundle-Version: 1.0
Import-Package: org.osgi.framework, org.hamcrest.core, org.hamcrest.collection, org.hamcrest.integration
Bundle-Activator: org.hamcrest.examples.osgi.TestActivator
Require-Bundle: org.hamcrest.hamcrest-core, org.hamcrest.hamcrest-integration

Binary file added lib/integration/bnd-1.50.0.jar
Binary file not shown.