Skip to content

Instantly share code, notes, and snippets.

@leadVisionary
Created August 15, 2012 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leadVisionary/3362698 to your computer and use it in GitHub Desktop.
Save leadVisionary/3362698 to your computer and use it in GitHub Desktop.
Ugly vs Clean OSGi Tests - Dumb Example
package dumb.test;
import java.util.Comparator;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.osgi.framework.Bundle;
public class BundleInformer {
private List<Bundle> bs;
public BundleInformer(List<Bundle> bundles){
bs = bundles;
sort();
}
private void sort() {
java.util.Collections.sort(bs, new Comparator<Bundle>(){
public int compare(Bundle arg0, Bundle arg1) {
return arg0.toString().compareTo(arg1.toString());
}
});
}
public void emitAllBundleInfo() {
for(Bundle b : bs ){
emitInformation(b);
}
}
public void emitInformation(Bundle b){
String name = b.toString();
StringBuffer toWrite = new StringBuffer();
toWrite.append(String.format("Bundle is: %s %n", name));
for(Entry<String, String> header : getHeaders(b).entrySet()){
toWrite.append(String.format("Header %s : %s %n", header.getKey(), header.getValue()));
}
toWrite.append(retrieveClassLoaderInformation(b));
String result = toWrite.toString();
BundleWriter.emitToConsole(result);
BundleWriter.emitToFile(name, result);
}
public Map<String, String> getHeaders(Bundle b) {
Map<String, String> result = new LinkedHashMap<String,String>();
Dictionary<?,?> headers = b.getHeaders();
Enumeration<?> heads =headers.keys();
while(heads.hasMoreElements()){
String key = heads.nextElement().toString();
result.put(key, headers.get(key).toString());
}
return result;
}
public String retrieveClassLoaderInformation(Bundle b) {
StringBuffer result = new StringBuffer();
ClassLoader load = b.getClass().getClassLoader();
while(load.getParent() != null){
result.append(String.format("loader was %s %n", load.toString()));
load = load.getParent();
}
return result.toString();
}
}
package dumb.test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class BundleWriter {
public static void emitToConsole(String toWrite){
write(System.out, toWrite);
}
public static void emitToFile(String fileName, String toWrite){
try {
write(new FileOutputStream(fileName), toWrite);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private static void write(OutputStream toOut, String toWrite) {
Writer out = null;
try{
out = new OutputStreamWriter(toOut);
out.write(toWrite);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.lmco.i4ce;
import static org.junit.Assert.assertTrue;
import static org.openengsb.labs.paxexam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
import static org.openengsb.labs.paxexam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
import static org.ops4j.pax.exam.CoreOptions.felix;
import static org.ops4j.pax.exam.CoreOptions.maven;
import static org.ops4j.pax.exam.CoreOptions.options;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.TestProbeBuilder;
import org.ops4j.pax.exam.junit.Configuration;
import org.ops4j.pax.exam.junit.ExamReactorStrategy;
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
import org.ops4j.pax.exam.junit.ProbeBuilder;
import org.ops4j.pax.exam.spi.reactors.AllConfinedStagedReactorFactory;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
@RunWith(JUnit4TestRunner.class)
@ExamReactorStrategy(AllConfinedStagedReactorFactory.class)
public class CleanTest {
@Inject
BundleContext bc;
@ProbeBuilder
public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
probe.setHeader(Constants.DYNAMICIMPORT_PACKAGE,
"*,org.apache.felix.service.*;status=provisional");
return probe;
}
@Configuration
public static Option[] karafConfig(){
return options(
karafDistributionConfiguration().frameworkUrl(maven()
.groupId("org.apache.karaf")
.artifactId("apache-karaf")
.type("zip")
.versionAsInProject())
.karafVersion("2.2.7")
.name("Apache Karaf"),
keepRuntimeFolder(),
felix()
);
}
PrintStream old;
PrintStream newOut;
ByteArrayOutputStream o;
@Before
public void setup(){
old = System.out;
o = new ByteArrayOutputStream();
newOut = new PrintStream(o);
System.setOut(newOut);
}
@Test
public void test() throws Exception {
List<Bundle> bundles = bundlesAsList();
BundleInformer informer = new BundleInformer(bundles);
informer.emitAllBundleInfo();
newOut.flush();
String result = o.toString();
assertTrue(result.contains("pax"));
}
private List<Bundle> bundlesAsList(){
Bundle[] b = bc.getBundles();
List<Bundle> bundles = java.util.Arrays.asList(b);
List <Bundle> bs = new ArrayList<Bundle>();
bs.addAll(bundles);
return bs;
}
@After
public void tearDown(){
System.setOut(old);
try {
o.close();
newOut.close();
} catch (IOException e) {
Assert.fail();
}
}
}
package dumb.test;
import static org.junit.Assert.assertTrue;
import static org.openengsb.labs.paxexam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
import static org.openengsb.labs.paxexam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
import static org.ops4j.pax.exam.CoreOptions.felix;
import static org.ops4j.pax.exam.CoreOptions.maven;
import static org.ops4j.pax.exam.CoreOptions.options;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Comparator;
import java.util.Dictionary;
import java.util.Enumeration;
import javax.inject.Inject;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.TestProbeBuilder;
import org.ops4j.pax.exam.junit.Configuration;
import org.ops4j.pax.exam.junit.ExamReactorStrategy;
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
import org.ops4j.pax.exam.junit.ProbeBuilder;
import org.ops4j.pax.exam.spi.reactors.AllConfinedStagedReactorFactory;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
@RunWith(JUnit4TestRunner.class)
@ExamReactorStrategy(AllConfinedStagedReactorFactory.class)
public class UglyTest {
@Inject
BundleContext bc;
@ProbeBuilder
public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
probe.setHeader(Constants.DYNAMICIMPORT_PACKAGE,
"*,org.apache.felix.service.*;status=provisional");
return probe;
}
@Configuration
public static Option[] karafConfig(){
return options(
karafDistributionConfiguration().frameworkUrl(maven()
.groupId("org.apache.karaf")
.artifactId("apache-karaf")
.type("zip")
.versionAsInProject())
.karafVersion("2.2.7")
.name("Apache Karaf"),
keepRuntimeFolder(),
felix()
);
}
PrintStream old;
PrintStream newOut;
ByteArrayOutputStream o;
@Before
public void setup(){
old = System.out;
o = new ByteArrayOutputStream();
newOut = new PrintStream(o);
System.setOut(newOut);
}
@Test
public void testShowBundleInformation() throws Exception {
Bundle[] b = bc.getBundles();
java.util.Arrays.sort(b, new Comparator<Bundle>(){
public int compare(Bundle arg0, Bundle arg1) {
return arg0.toString().compareTo(arg1.toString());
}
});
for(Bundle g : b){
System.out.printf("%s %d %n", g.toString(), g.getState());
Dictionary headers = g.getHeaders();
Enumeration<?> keys = headers.keys();
while(keys.hasMoreElements()){
Object key = keys.nextElement();
if(headers.get(key).toString().contains(".*pax.*")){
System.out.printf("\t %s : %s %n", key, headers.get(key));
}
}
}
newOut.flush();
String result = o.toString();
assertTrue(result.contains("pax"));
}
@After
public void tearDown(){
System.setOut(old);
try {
o.close();
newOut.close();
} catch (IOException e) {
Assert.fail();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment