use of org.eclipse.osgi.service.urlconversion.URLConverter in project rt.equinox.framework by eclipse.
the class SystemBundleTests method testZipBundleFileOpenLock.
public void testZipBundleFileOpenLock() throws IOException, BundleException, InvalidSyntaxException {
// $NON-NLS-1$
File config = OSGiTestsActivator.getContext().getDataFile(getName());
config.mkdirs();
Map<String, String> bundleHeaders = new HashMap<String, String>();
bundleHeaders.put(Constants.BUNDLE_MANIFESTVERSION, "2");
bundleHeaders.put(Constants.BUNDLE_SYMBOLICNAME, getName());
Map<String, String> bundleEntries = new LinkedHashMap<>();
bundleEntries.put("dirA/", null);
bundleEntries.put("dirA/fileA", "fileA");
bundleEntries.put("dirA/dirB/", null);
bundleEntries.put("dirA/dirB/fileB", "fileB");
// file in a directory with no directory entry
bundleEntries.put("dirA/dirC/fileC", "fileC");
File testBundleFile = SystemBundleTests.createBundle(config, getName(), bundleHeaders, bundleEntries);
Map<String, Object> configuration = new HashMap<String, Object>();
configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
final Equinox equinox = new Equinox(configuration);
try {
equinox.start();
} catch (BundleException e) {
// $NON-NLS-1$
fail("Failed to start the framework", e);
}
// $NON-NLS-1$
assertEquals("Wrong state for SystemBundle", Bundle.ACTIVE, equinox.getState());
final BundleContext systemContext = equinox.getBundleContext();
String converterFilter = "(&(objectClass=" + URLConverter.class.getName() + ")(protocol=bundleentry))";
final URLConverter converter = systemContext.getService(systemContext.getServiceReferences(URLConverter.class, converterFilter).iterator().next());
final Bundle testBundle = systemContext.installBundle("file:///" + testBundleFile.getAbsolutePath());
testBundle.start();
final List<FrameworkEvent> errorsAndWarnings = new CopyOnWriteArrayList<FrameworkEvent>();
FrameworkListener fwkListener = new FrameworkListener() {
@Override
public void frameworkEvent(FrameworkEvent event) {
int type = event.getType();
if (type == FrameworkEvent.ERROR || type == FrameworkEvent.WARNING) {
errorsAndWarnings.add(event);
}
}
};
systemContext.addFrameworkListener(fwkListener);
Runnable asyncTest = new Runnable() {
@Override
public void run() {
try {
assertNotNull("Entry not found.", testBundle.getEntry("dirA/fileA"));
assertNotNull("Entry not found.", testBundle.getEntry("dirA/dirB/fileB"));
assertNotNull("Entry not found.", testBundle.getEntry("dirA/dirC/fileC"));
assertNotNull("Entry not found.", testBundle.getEntry("dirA/dirC/"));
URL dirBURL = converter.toFileURL(testBundle.getEntry("dirA/dirB/"));
assertNotNull("Failed to convert to file URL", dirBURL);
URL dirAURL = converter.toFileURL(testBundle.getEntry("dirA/"));
assertNotNull("Failed to convert to file URL", dirAURL);
List<URL> allEntries = testBundle.adapt(BundleWiring.class).findEntries("/", "*", BundleWiring.FINDENTRIES_RECURSE);
assertEquals("Wrong number of entries: " + allEntries, 8, allEntries.size());
} catch (IOException e) {
throw new AssertionFailedError(e.getMessage());
}
}
};
// do test with two threads to make sure open lock is not held by a thread
ExecutorService executor1 = Executors.newFixedThreadPool(1);
ExecutorService executor2 = Executors.newFixedThreadPool(1);
try {
executor1.submit(asyncTest).get();
executor2.submit(asyncTest).get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
fail("Interrupted.", e);
} catch (ExecutionException e) {
if (e.getCause() instanceof Error) {
throw (Error) e.getCause();
}
throw (RuntimeException) e.getCause();
} finally {
executor1.shutdown();
executor2.shutdown();
}
try {
equinox.stop();
} catch (BundleException e) {
// $NON-NLS-1$
fail("Unexpected erorr stopping framework", e);
}
try {
equinox.waitForStop(10000);
} catch (InterruptedException e) {
// $NON-NLS-1$
fail("Unexpected interrupted exception", e);
}
// $NON-NLS-1$
assertEquals("Wrong state for SystemBundle", Bundle.RESOLVED, equinox.getState());
if (!errorsAndWarnings.isEmpty()) {
StringWriter errorStackTraces = new StringWriter();
PrintWriter writer = new PrintWriter(errorStackTraces);
for (FrameworkEvent frameworkEvent : errorsAndWarnings) {
if (frameworkEvent.getThrowable() != null) {
frameworkEvent.getThrowable().printStackTrace(writer);
}
}
writer.close();
fail("Found errors or warnings: " + errorsAndWarnings.size() + " - " + errorStackTraces.toString());
}
}
Aggregations