Search in sources :

Example 76 with Equinox

use of org.eclipse.osgi.launch.Equinox 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)

Example 77 with Equinox

use of org.eclipse.osgi.launch.Equinox in project rt.equinox.framework by eclipse.

the class SystemBundleTests method testSystemBundle11.

public void testSystemBundle11() {
    // test extra packages property
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile("testSystemBundle11");
    Map<String, Object> configuration = new HashMap<String, Object>();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    // $NON-NLS-1$
    configuration.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "test.pkg1, test.pkg2");
    Equinox equinox = new Equinox(configuration);
    try {
        equinox.init();
        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());
    BundleContext systemContext = equinox.getBundleContext();
    // $NON-NLS-1$
    assertNotNull("SystemBundle context is null", systemContext);
    PackageAdmin pa = (PackageAdmin) equinox.getBundleContext().getService(equinox.getBundleContext().getServiceReference(PackageAdmin.class.getName()));
    // $NON-NLS-1$
    ExportedPackage[] pkg1 = pa.getExportedPackages("test.pkg1");
    assertNotNull(pkg1);
    // $NON-NLS-1$
    assertEquals("Wrong number of exports", 1, pkg1.length);
    // $NON-NLS-1$ //$NON-NLS-2$
    assertEquals("Wrong package name", "test.pkg1", pkg1[0].getName());
    // $NON-NLS-1$
    ExportedPackage[] pkg2 = pa.getExportedPackages("test.pkg2");
    assertNotNull(pkg2);
    // $NON-NLS-1$
    assertEquals("Wrong number of exports", 1, pkg2.length);
    // $NON-NLS-1$ //$NON-NLS-2$
    assertEquals("Wrong package name", "test.pkg2", pkg2[0].getName());
    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());
}
Also used : Equinox(org.eclipse.osgi.launch.Equinox) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) ExportedPackage(org.osgi.service.packageadmin.ExportedPackage) BundleException(org.osgi.framework.BundleException) File(java.io.File) BundleContext(org.osgi.framework.BundleContext)

Example 78 with Equinox

use of org.eclipse.osgi.launch.Equinox in project rt.equinox.framework by eclipse.

the class SystemBundleTests method testSystemBundle03.

public void testSystemBundle03() {
    // create/stop/ test
    // $NON-NLS-1$
    File config = OSGiTestsActivator.getContext().getDataFile("testSystemBundle03");
    Map<String, Object> configuration = new HashMap<String, Object>();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    Equinox equinox = new Equinox(configuration);
    try {
        equinox.init();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected exception in init()", e);
    }
    // should be in the STARTING state
    // $NON-NLS-1$
    assertEquals("Wrong state for SystemBundle", Bundle.STARTING, equinox.getState());
    BundleContext systemContext = equinox.getBundleContext();
    // $NON-NLS-1$
    assertNotNull("System context is null", systemContext);
    // $NON-NLS-1$
    String configArea = systemContext.getProperty("osgi.configuration.area");
    // $NON-NLS-1$
    assertNotNull("config property is null", configArea);
    // $NON-NLS-1$ //$NON-NLS-2$
    assertTrue("Wrong configuration area", configArea.endsWith("testSystemBundle03/"));
    // don't do anything; just put the framework back to the RESOLVED state
    try {
        equinox.stop();
    } catch (BundleException e) {
        // $NON-NLS-1$
        fail("Unexpected error 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());
}
Also used : Equinox(org.eclipse.osgi.launch.Equinox) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) BundleException(org.osgi.framework.BundleException) File(java.io.File) BundleContext(org.osgi.framework.BundleContext)

Example 79 with Equinox

use of org.eclipse.osgi.launch.Equinox in project rt.equinox.framework by eclipse.

the class SystemBundleTests method testNullConfigurationValueRequiredProperty.

public void testNullConfigurationValueRequiredProperty() throws BundleException {
    final String systemProcessor = System.getProperty(Constants.FRAMEWORK_PROCESSOR);
    assertNotNull(systemProcessor);
    try {
        System.setProperty(Constants.FRAMEWORK_PROCESSOR, "hyperflux");
        // $NON-NLS-1$
        File config = OSGiTestsActivator.getContext().getDataFile(getName());
        Map<String, Object> configuration = new HashMap<String, Object>();
        configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
        configuration.put(Constants.FRAMEWORK_PROCESSOR, null);
        Equinox equinox = new Equinox(configuration);
        equinox.start();
        String processor = equinox.getBundleContext().getProperty(Constants.FRAMEWORK_PROCESSOR);
        assertEquals("Wrong " + Constants.FRAMEWORK_PROCESSOR, systemProcessor, processor);
        String systemValue = System.getProperty(Constants.FRAMEWORK_PROCESSOR);
        assertEquals("Wrong system value.", "hyperflux", systemValue);
        equinox.stop();
    } finally {
        System.setProperty(Constants.FRAMEWORK_PROCESSOR, systemProcessor);
    }
}
Also used : Equinox(org.eclipse.osgi.launch.Equinox) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) File(java.io.File)

Example 80 with Equinox

use of org.eclipse.osgi.launch.Equinox in project rt.equinox.framework by eclipse.

the class SystemBundleTests method doTestConfigSpecialChar.

private void doTestConfigSpecialChar(char c) throws BundleException, IOException {
    File config = OSGiTestsActivator.getContext().getDataFile(getName() + c + "config");
    config.mkdirs();
    // create a config.ini with some system property substitutes
    Properties configIni = new Properties();
    configIni.setProperty("test.config", getName());
    configIni.store(new FileOutputStream(new File(config, "config.ini")), "Test config.ini");
    Map<String, Object> configuration = new HashMap<String, Object>();
    configuration.put(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath());
    Equinox equinox = new Equinox(configuration);
    equinox.init();
    BundleContext systemContext = equinox.getBundleContext();
    // check for substitution
    assertEquals("Wrong value for test.config", getName(), systemContext.getProperty("test.config"));
    equinox.stop();
    try {
        equinox.waitForStop(5000);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        fail("Unexpected interruption.", e);
    }
}
Also used : Equinox(org.eclipse.osgi.launch.Equinox) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) FileOutputStream(java.io.FileOutputStream) Properties(java.util.Properties) File(java.io.File) BundleContext(org.osgi.framework.BundleContext)

Aggregations

Equinox (org.eclipse.osgi.launch.Equinox)105 File (java.io.File)88 HashMap (java.util.HashMap)61 LinkedHashMap (java.util.LinkedHashMap)58 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)58 BundleException (org.osgi.framework.BundleException)52 BundleContext (org.osgi.framework.BundleContext)51 Bundle (org.osgi.framework.Bundle)43 IOException (java.io.IOException)18 FileNotFoundException (java.io.FileNotFoundException)11 URL (java.net.URL)10 Location (org.eclipse.osgi.service.datalocation.Location)10 Properties (java.util.Properties)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)8 FrameworkEvent (org.osgi.framework.FrameworkEvent)8 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)8 BundleWiring (org.osgi.framework.wiring.BundleWiring)8 ArrayList (java.util.ArrayList)7 ExecutionException (java.util.concurrent.ExecutionException)6 FileOutputStream (java.io.FileOutputStream)5