Search in sources :

Example 6 with DeploymentPackageBuilder

use of org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder in project felix by apache.

the class DeploymentAdminEventTest method testSuccessfulInstallationCausesCompletionEventOk.

/**
 * FELIX-4466 - test that an event is fired when an installation of a DP succeeds.
 */
@Test
public void testSuccessfulInstallationCausesCompletionEventOk() throws Exception {
    DeploymentPackageBuilder dpBuilder = createNewDeploymentPackageBuilder("1.0.0");
    dpBuilder.add(dpBuilder.createBundleResource().setUrl(getTestBundleURL("bundleapi1", "bundleapi1", "1.0.0"))).add(dpBuilder.createBundleResource().setUrl(getTestBundleURL("bundleimpl1", "bundleimpl1", "1.0.0")));
    final AtomicReference<Event> completionEventRef = new AtomicReference<Event>();
    final AtomicReference<Event> installEventRef = new AtomicReference<Event>();
    final CountDownLatch cdl = new CountDownLatch(2);
    EventHandler eventHandler = new EventHandler() {

        @Override
        public void handleEvent(Event event) {
            if (Constants.EVENTTOPIC_COMPLETE.equals(event.getTopic())) {
                assertTrue("Multiple events received?!", completionEventRef.compareAndSet(null, event));
                cdl.countDown();
            } else if (Constants.EVENTTOPIC_INSTALL.equals(event.getTopic())) {
                assertTrue("Multiple events received?!", installEventRef.compareAndSet(null, event));
                cdl.countDown();
            }
        }
    };
    Dictionary props = new Properties();
    props.put(EventConstants.EVENT_TOPIC, new String[] { Constants.EVENTTOPIC_COMPLETE, Constants.EVENTTOPIC_INSTALL });
    ServiceRegistration sreg = m_context.registerService(EventHandler.class, eventHandler, props);
    try {
        installDeploymentPackage(dpBuilder);
        assertTrue("Not all events were received in time?!", cdl.await(5, TimeUnit.SECONDS));
        Event event;
        // Verify we've got the expected events...
        event = installEventRef.get();
        assertNotNull("No install event received?!", event);
        event = completionEventRef.get();
        assertNotNull("No completion event received?!", event);
        assertTrue("Completion property set to false?!", Boolean.TRUE.equals(event.getProperty(Constants.EVENTPROPERTY_SUCCESSFUL)));
    } finally {
        sreg.unregister();
    }
}
Also used : Dictionary(java.util.Dictionary) DeploymentPackageBuilder(org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder) Event(org.osgi.service.event.Event) EventHandler(org.osgi.service.event.EventHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Properties(java.util.Properties) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 7 with DeploymentPackageBuilder

use of org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder in project felix by apache.

the class DeploymentAdminTest method testBundleVersionMustMatchManifestEntry.

@Test
public void testBundleVersionMustMatchManifestEntry() throws Exception {
    DeploymentPackageBuilder dpBuilder = createNewDeploymentPackageBuilder("1.0.0");
    dpBuilder.add(dpBuilder.createBundleResource().setUrl(getTestBundleURL("bundle1"))).add(dpBuilder.createBundleResource().setUrl(getTestBundleURL("bundle2")).setFilter(new JarManifestManipulatingFilter("Bundle-Version", "1.1.0")));
    try {
        installDeploymentPackage(dpBuilder);
        fail("Succeeded into installing a bundle with a fake version?!");
    } catch (DeploymentException exception) {
        // Ok; expected...
        assertDeploymentException(CODE_OTHER_ERROR, exception);
    }
}
Also used : DeploymentPackageBuilder(org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder) JarManifestManipulatingFilter(org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder.JarManifestManipulatingFilter) DeploymentException(org.osgi.service.deploymentadmin.DeploymentException) Test(org.junit.Test)

Example 8 with DeploymentPackageBuilder

use of org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder in project felix by apache.

the class DeploymentAdminTest method testManifestEntryMustMatchBundleVersion.

@Test
public void testManifestEntryMustMatchBundleVersion() throws Exception {
    DeploymentPackageBuilder dpBuilder = createNewDeploymentPackageBuilder("1.0.0");
    dpBuilder.add(dpBuilder.createBundleResource().setUrl(getTestBundleURL("bundle1"))).add(dpBuilder.createBundleResource().setVersion("1.1.0").setUrl(getTestBundleURL("bundle2")));
    try {
        installDeploymentPackage(dpBuilder);
        fail("Succeeded into installing a bundle with a fake version?!");
    } catch (DeploymentException exception) {
        // Ok; expected...
        assertDeploymentException(CODE_OTHER_ERROR, exception);
    }
}
Also used : DeploymentPackageBuilder(org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder) DeploymentException(org.osgi.service.deploymentadmin.DeploymentException) Test(org.junit.Test)

Example 9 with DeploymentPackageBuilder

use of org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder in project felix by apache.

the class DeploymentPackageBuilderTest method testCreateMissingBundleResourceOk.

/**
 * Tests that we can build a deployment package with a bundle resource.
 */
@Test
public void testCreateMissingBundleResourceOk() throws Exception {
    DeploymentPackageBuilder dpBuilder = DeploymentPackageBuilder.create("dp-test", "1.0.0");
    dpBuilder.setFixPackage().add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle1")).setMissing());
    JarInputStream jis = new JarInputStream(dpBuilder.generate());
    assertNotNull(jis);
    Manifest manifest = jis.getManifest();
    assertManifestHeader(manifest, "DeploymentPackage-SymbolicName", "dp-test");
    assertManifestHeader(manifest, "DeploymentPackage-Version", "1.0.0");
    String filename = getBundleName("bundle1");
    assertManifestEntry(manifest, filename, "Name", filename);
    assertManifestEntry(manifest, filename, "Bundle-SymbolicName", "testbundles.bundle1");
    assertManifestEntry(manifest, filename, "Bundle-Version", "1.0.0");
    assertManifestEntry(manifest, filename, "DeploymentPackage-Missing", "true");
    int count = countJarEntries(jis);
    assertEquals("Expected two entries in the JAR!", 0, count);
}
Also used : JarInputStream(java.util.jar.JarInputStream) DeploymentPackageBuilder(org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder) Manifest(java.util.jar.Manifest) Test(org.junit.Test)

Example 10 with DeploymentPackageBuilder

use of org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder in project felix by apache.

the class DeploymentPackageBuilderTest method testResourceFilterOk.

/**
 * Tests that we can filter a resource.
 */
@Test
public void testResourceFilterOk() throws Exception {
    DeploymentPackageBuilder dpBuilder = DeploymentPackageBuilder.create("dp-test", "1.0.0");
    dpBuilder.add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle2"))).add(dpBuilder.createBundleResource().setVersion("1.1.0").setFilter(new JarManifestManipulatingFilter("Bundle-Version", "1.1.0", "Foo", "bar")).setUrl(getTestBundle("bundle1")));
    JarInputStream jis = new JarInputStream(dpBuilder.generate());
    assertNotNull(jis);
    Manifest manifest = jis.getManifest();
    assertManifestHeader(manifest, "DeploymentPackage-SymbolicName", "dp-test");
    assertManifestHeader(manifest, "DeploymentPackage-Version", "1.0.0");
    String filename = getBundleName("bundle1");
    assertManifestEntry(manifest, filename, "Name", filename);
    assertManifestEntry(manifest, filename, "Bundle-SymbolicName", "testbundles.bundle1");
    assertManifestEntry(manifest, filename, "Bundle-Version", "1.1.0");
    filename = getBundleName("bundle2");
    assertManifestEntry(manifest, filename, "Name", filename);
    assertManifestEntry(manifest, filename, "Bundle-SymbolicName", "testbundles.bundle2");
    assertManifestEntry(manifest, filename, "Bundle-Version", "1.0.0");
    try {
        byte[] buf = new byte[32 * 1024];
        JarEntry entry;
        while ((entry = jis.getNextJarEntry()) != null) {
            if (entry.getName().endsWith("valid-bundle1.jar")) {
                int read = jis.read(buf);
                JarInputStream jis2 = new JarInputStream(new ByteArrayInputStream(Arrays.copyOf(buf, read)));
                Manifest manifest2 = jis2.getManifest();
                Attributes mainAttributes = manifest2.getMainAttributes();
                assertEquals("1.1.0", mainAttributes.getValue("Bundle-Version"));
                assertEquals("bar", mainAttributes.getValue("Foo"));
                jis2.close();
            }
            jis.closeEntry();
        }
    } finally {
        jis.close();
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DeploymentPackageBuilder(org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder) Attributes(java.util.jar.Attributes) JarManifestManipulatingFilter(org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder.JarManifestManipulatingFilter) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) Test(org.junit.Test)

Aggregations

DeploymentPackageBuilder (org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder)67 Test (org.junit.Test)67 DeploymentPackage (org.osgi.service.deploymentadmin.DeploymentPackage)46 DeploymentException (org.osgi.service.deploymentadmin.DeploymentException)28 Bundle (org.osgi.framework.Bundle)15 JarInputStream (java.util.jar.JarInputStream)6 JarManifestManipulatingFilter (org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder.JarManifestManipulatingFilter)6 Manifest (java.util.jar.Manifest)5 File (java.io.File)4 Dictionary (java.util.Dictionary)4 Properties (java.util.Properties)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 ServiceRegistration (org.osgi.framework.ServiceRegistration)4 Event (org.osgi.service.event.Event)4 EventHandler (org.osgi.service.event.EventHandler)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URL (java.net.URL)2 JarEntry (java.util.jar.JarEntry)2 SignerInfo (org.apache.felix.deploymentadmin.itest.util.CertificateUtil.SignerInfo)2