Search in sources :

Example 1 with URLConverter

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());
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) URL(java.net.URL) LinkedHashMap(java.util.LinkedHashMap) StringWriter(java.io.StringWriter) BundleException(org.osgi.framework.BundleException) AssertionFailedError(junit.framework.AssertionFailedError) ExecutionException(java.util.concurrent.ExecutionException) PrintWriter(java.io.PrintWriter) FrameworkEvent(org.osgi.framework.FrameworkEvent) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) AssertionFailedError(junit.framework.AssertionFailedError) IOException(java.io.IOException) Equinox(org.eclipse.osgi.launch.Equinox) URLConverter(org.eclipse.osgi.service.urlconversion.URLConverter) ExecutorService(java.util.concurrent.ExecutorService) FrameworkListener(org.osgi.framework.FrameworkListener) File(java.io.File) BundleContext(org.osgi.framework.BundleContext) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 AssertionFailedError (junit.framework.AssertionFailedError)1 Equinox (org.eclipse.osgi.launch.Equinox)1 URLConverter (org.eclipse.osgi.service.urlconversion.URLConverter)1 Bundle (org.osgi.framework.Bundle)1 BundleContext (org.osgi.framework.BundleContext)1 BundleException (org.osgi.framework.BundleException)1 FrameworkEvent (org.osgi.framework.FrameworkEvent)1 FrameworkListener (org.osgi.framework.FrameworkListener)1 BundleWiring (org.osgi.framework.wiring.BundleWiring)1