public void start(final BundleContext context) throws Exception {
arqBundleId = context.getBundle().getBundleId();
final BundleContext syscontext = context.getBundle(0).getBundleContext();
final TestClassLoader testClassLoader = new TestClassLoader() {
@Override
public Class<?> loadTestClass(String className) throws ClassNotFoundException {
String namePath = className.replace('.', '/') + ".class";
// Get all installed bundles and remove some
List<Bundle> bundles = new ArrayList<Bundle>(Arrays.asList(syscontext.getBundles()));
Iterator<Bundle> iterator = bundles.iterator();
while(iterator.hasNext()) {
Bundle aux = iterator.next();
if (aux.getBundleId() <= arqBundleId || aux.getState() == Bundle.UNINSTALLED) {
iterator.remove();
}
}
// Load the the test class from the bundle that contains the entry
for (Bundle aux : bundles) {
if (aux.getEntry(namePath) != null) {
return aux.loadClass(className);
}
}
// Load the the test class from bundle that defines a Bundle-ClassPath
for (Bundle aux : bundles) {
String bundlecp = (String) aux.getHeaders().get(Constants.BUNDLE_CLASSPATH);
if (bundlecp != null) {
try {
return aux.loadClass(className);
} catch (ClassNotFoundException ex) {
// ignore
}
}
}
throw new ClassNotFoundException("Test '" + className + "' not found in: " + bundles);
}
};
// Register the JMXTestRunner
MBeanServer mbeanServer = findOrCreateMBeanServer();
testRunner = new JMXTestRunner(testClassLoader) {
@Override
public byte[] runTestMethod(String className, String methodName) {
Class<?> testClass;
try {
testClass = testClassLoader.loadTestClass(className);
} catch (ClassNotFoundException ex) {
throw new IllegalStateException(ex);
}
BundleAssociation.setBundle(getTestBundle(syscontext, testClass, methodName));
BundleContextAssociation.setBundleContext(syscontext);