Search in sources :

Example 1 with BundleArchiveRevision

use of org.apache.felix.framework.cache.BundleArchiveRevision in project felix by apache.

the class CollisionHookTest method testCollisionHookInstall.

public void testCollisionHookInstall() throws Exception {
    BundleImpl identicalBundle = mockBundleImpl(1L, "foo", "1.2.1.a");
    BundleImpl differentBundle = mockBundleImpl(2L, "bar", "1.2.1.a");
    BundleImpl originatingBundle = mockBundleImpl(4L, "xyz", "1.0.0");
    CollisionHook testCollisionHook = new CollisionHook() {

        @Override
        public void filterCollisions(int operationType, Bundle target, Collection<Bundle> collisionCandidates) {
            if ((target.getBundleId() == 4L) && (operationType == CollisionHook.INSTALLING)) {
                collisionCandidates.clear();
            }
        }
    };
    @SuppressWarnings("unchecked") ServiceReference<CollisionHook> chRef = Mockito.mock(ServiceReference.class);
    // Mock the framework
    StatefulResolver mockResolver = Mockito.mock(StatefulResolver.class);
    Felix felixMock = Mockito.mock(Felix.class);
    HookRegistry hReg = mock(HookRegistry.class);
    when(hReg.getHooks(CollisionHook.class)).thenReturn(Collections.singleton(chRef));
    when(felixMock.getHookRegistry()).thenReturn(hReg);
    Mockito.when(felixMock.getResolver()).thenReturn(mockResolver);
    Mockito.when(felixMock.getBundles()).thenReturn(new Bundle[] { differentBundle, identicalBundle });
    Mockito.when(felixMock.getService(felixMock, chRef, false)).thenReturn(testCollisionHook);
    // Mock the archive of the bundle being installed
    Map<String, Object> headerMap = new HashMap<String, Object>();
    headerMap.put(Constants.BUNDLE_SYMBOLICNAME, "foo");
    headerMap.put(Constants.BUNDLE_VERSION, "1.2.1.a");
    headerMap.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    BundleArchiveRevision archiveRevision = Mockito.mock(BundleArchiveRevision.class);
    Mockito.when(archiveRevision.getManifestHeader()).thenReturn(headerMap);
    BundleArchive archive = Mockito.mock(BundleArchive.class);
    Mockito.when(archive.getCurrentRevision()).thenReturn(archiveRevision);
    Mockito.when(archive.getId()).thenReturn(3L);
    BundleImpl bi = new BundleImpl(felixMock, originatingBundle, archive);
    assertEquals(3L, bi.getBundleId());
    // Do the revise operation.
    try {
        bi.revise(null, null);
        fail("Should have thrown a BundleException because the installed bundle is not unique");
    } catch (BundleException be) {
        // good
        assertTrue(be.getMessage().contains("not unique"));
    }
}
Also used : BundleArchive(org.apache.felix.framework.cache.BundleArchive) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleArchiveRevision(org.apache.felix.framework.cache.BundleArchiveRevision) Collection(java.util.Collection) BundleException(org.osgi.framework.BundleException) CollisionHook(org.osgi.framework.hooks.bundle.CollisionHook)

Example 2 with BundleArchiveRevision

use of org.apache.felix.framework.cache.BundleArchiveRevision in project felix by apache.

the class CollisionHookTest method testCollisionNotEnabled.

public void testCollisionNotEnabled() throws Exception {
    BundleImpl identicalBundle = mockBundleImpl(1L, "foo", "1.2.1.a");
    BundleImpl differentBundle = mockBundleImpl(2L, "bar", "1.2.1.a");
    CollisionHook testCollisionHook = new CollisionHook() {

        @Override
        public void filterCollisions(int operationType, Bundle target, Collection<Bundle> collisionCandidates) {
            if ((target.getBundleId() == 3L) && (operationType == CollisionHook.INSTALLING)) {
                collisionCandidates.clear();
            }
        }
    };
    @SuppressWarnings("unchecked") ServiceReference<CollisionHook> chRef = Mockito.mock(ServiceReference.class);
    Map<String, Object> config = new HashMap<String, Object>();
    config.put(Constants.FRAMEWORK_BSNVERSION, Constants.FRAMEWORK_BSNVERSION_SINGLE);
    // Mock the framework
    StatefulResolver mockResolver = Mockito.mock(StatefulResolver.class);
    Felix felixMock = Mockito.mock(Felix.class);
    Mockito.when(felixMock.getConfig()).thenReturn(config);
    HookRegistry hReg = mock(HookRegistry.class);
    when(hReg.getHooks(CollisionHook.class)).thenReturn(Collections.singleton(chRef));
    when(felixMock.getHookRegistry()).thenReturn(hReg);
    Mockito.when(felixMock.getResolver()).thenReturn(mockResolver);
    Mockito.when(felixMock.getBundles()).thenReturn(new Bundle[] { differentBundle, identicalBundle });
    Mockito.when(felixMock.getService(felixMock, chRef, false)).thenReturn(testCollisionHook);
    // Mock the archive of the bundle being installed
    Map<String, Object> headerMap = new HashMap<String, Object>();
    headerMap.put(Constants.BUNDLE_SYMBOLICNAME, "foo");
    headerMap.put(Constants.BUNDLE_VERSION, "1.2.1.a");
    headerMap.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    BundleArchiveRevision archiveRevision = Mockito.mock(BundleArchiveRevision.class);
    Mockito.when(archiveRevision.getManifestHeader()).thenReturn(headerMap);
    BundleArchive archive = Mockito.mock(BundleArchive.class);
    Mockito.when(archive.getCurrentRevision()).thenReturn(archiveRevision);
    Mockito.when(archive.getId()).thenReturn(3L);
    try {
        new BundleImpl(felixMock, null, archive);
        fail("Should have thrown a BundleException because the collision hook is not enabled");
    } catch (BundleException be) {
        // good
        assertTrue(be.getMessage().contains("not unique"));
    }
}
Also used : BundleArchive(org.apache.felix.framework.cache.BundleArchive) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleArchiveRevision(org.apache.felix.framework.cache.BundleArchiveRevision) Collection(java.util.Collection) BundleException(org.osgi.framework.BundleException) CollisionHook(org.osgi.framework.hooks.bundle.CollisionHook)

Example 3 with BundleArchiveRevision

use of org.apache.felix.framework.cache.BundleArchiveRevision in project felix by apache.

the class CollisionHookTest method testCollisionHookUpdate.

public void testCollisionHookUpdate() throws Exception {
    BundleImpl identicalBundle = mockBundleImpl(1L, "foo", "1.2.1.a");
    BundleImpl differentBundle = mockBundleImpl(2L, "foo", "1.2.1");
    CollisionHook testCollisionHook = new CollisionHook() {

        @Override
        public void filterCollisions(int operationType, Bundle target, Collection<Bundle> collisionCandidates) {
            if ((target.getBundleId() == 3L) && (operationType == CollisionHook.UPDATING)) {
                collisionCandidates.clear();
            }
        }
    };
    @SuppressWarnings("unchecked") ServiceReference<CollisionHook> chRef = Mockito.mock(ServiceReference.class);
    Map<String, Object> config = new HashMap<String, Object>();
    config.put(Constants.FRAMEWORK_BSNVERSION, Constants.FRAMEWORK_BSNVERSION_MANAGED);
    // Mock the framework
    StatefulResolver mockResolver = Mockito.mock(StatefulResolver.class);
    Felix felixMock = Mockito.mock(Felix.class);
    Mockito.when(felixMock.getConfig()).thenReturn(config);
    HookRegistry hReg = mock(HookRegistry.class);
    when(hReg.getHooks(CollisionHook.class)).thenReturn(Collections.singleton(chRef));
    when(felixMock.getHookRegistry()).thenReturn(hReg);
    Mockito.when(felixMock.getResolver()).thenReturn(mockResolver);
    Mockito.when(felixMock.getBundles()).thenReturn(new Bundle[] { differentBundle, identicalBundle });
    Mockito.when(felixMock.getService(felixMock, chRef, false)).thenReturn(testCollisionHook);
    // Mock the archive of the bundle being installed
    Map<String, Object> headerMap = new HashMap<String, Object>();
    headerMap.put(Constants.BUNDLE_SYMBOLICNAME, "zar");
    headerMap.put(Constants.BUNDLE_VERSION, "1.2.1.a");
    headerMap.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    BundleArchiveRevision archiveRevision = Mockito.mock(BundleArchiveRevision.class);
    Mockito.when(archiveRevision.getManifestHeader()).thenReturn(headerMap);
    BundleArchive archive = Mockito.mock(BundleArchive.class);
    Mockito.when(archive.getCurrentRevision()).thenReturn(archiveRevision);
    Mockito.when(archive.getId()).thenReturn(3L);
    BundleImpl bi = new BundleImpl(felixMock, null, archive);
    assertEquals("zar", bi.getSymbolicName());
    // Do the revise operation, change the bsn to foo
    headerMap.put(Constants.BUNDLE_SYMBOLICNAME, "foo");
    bi.revise(null, null);
    assertEquals("foo", bi.getSymbolicName());
}
Also used : BundleArchive(org.apache.felix.framework.cache.BundleArchive) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleArchiveRevision(org.apache.felix.framework.cache.BundleArchiveRevision) Collection(java.util.Collection) CollisionHook(org.osgi.framework.hooks.bundle.CollisionHook)

Example 4 with BundleArchiveRevision

use of org.apache.felix.framework.cache.BundleArchiveRevision in project felix by apache.

the class CollisionHookTest method testNoCollisionHook.

public void testNoCollisionHook() throws Exception {
    BundleImpl identicalBundle = mockBundleImpl(1L, "foo", "1.2.1.a");
    BundleImpl differentBundle = mockBundleImpl(2L, "bar", "1.2.1.a");
    // Mock the framework
    StatefulResolver mockResolver = Mockito.mock(StatefulResolver.class);
    Felix felixMock = Mockito.mock(Felix.class);
    HookRegistry hReg = Mockito.mock(HookRegistry.class);
    Mockito.when(felixMock.getHookRegistry()).thenReturn(hReg);
    Mockito.when(felixMock.getResolver()).thenReturn(mockResolver);
    Mockito.when(felixMock.getBundles()).thenReturn(new Bundle[] { differentBundle, identicalBundle });
    // Mock the archive of the bundle being installed
    Map<String, Object> headerMap = new HashMap<String, Object>();
    headerMap.put(Constants.BUNDLE_SYMBOLICNAME, "foo");
    headerMap.put(Constants.BUNDLE_VERSION, "1.2.1.a");
    headerMap.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    BundleArchiveRevision archiveRevision = Mockito.mock(BundleArchiveRevision.class);
    Mockito.when(archiveRevision.getManifestHeader()).thenReturn(headerMap);
    BundleArchive archive = Mockito.mock(BundleArchive.class);
    Mockito.when(archive.getCurrentRevision()).thenReturn(archiveRevision);
    Mockito.when(archive.getId()).thenReturn(3L);
    try {
        new BundleImpl(felixMock, null, archive);
        fail("Should have thrown a BundleException because the installed bundle is not unique");
    } catch (BundleException be) {
        // good
        assertTrue(be.getMessage().contains("not unique"));
    }
}
Also used : BundleArchive(org.apache.felix.framework.cache.BundleArchive) HashMap(java.util.HashMap) BundleException(org.osgi.framework.BundleException) BundleArchiveRevision(org.apache.felix.framework.cache.BundleArchiveRevision)

Example 5 with BundleArchiveRevision

use of org.apache.felix.framework.cache.BundleArchiveRevision in project felix by apache.

the class CollisionHookTest method testAllowMultiple.

public void testAllowMultiple() throws Exception {
    BundleImpl identicalBundle = mockBundleImpl(1L, "foo", "1.2.1.a");
    BundleImpl differentBundle = mockBundleImpl(2L, "bar", "1.2.1.a");
    Map<String, Object> config = new HashMap<String, Object>();
    config.put(Constants.FRAMEWORK_BSNVERSION, Constants.FRAMEWORK_BSNVERSION_MULTIPLE);
    // Mock the framework
    StatefulResolver mockResolver = Mockito.mock(StatefulResolver.class);
    Felix felixMock = Mockito.mock(Felix.class);
    Mockito.when(felixMock.getConfig()).thenReturn(config);
    Mockito.when(felixMock.getResolver()).thenReturn(mockResolver);
    Mockito.when(felixMock.getBundles()).thenReturn(new Bundle[] { differentBundle, identicalBundle });
    // Mock the archive of the bundle being installed
    Map<String, Object> headerMap = new HashMap<String, Object>();
    headerMap.put(Constants.BUNDLE_SYMBOLICNAME, "foo");
    headerMap.put(Constants.BUNDLE_VERSION, "1.2.1.a");
    headerMap.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    BundleArchiveRevision archiveRevision = Mockito.mock(BundleArchiveRevision.class);
    Mockito.when(archiveRevision.getManifestHeader()).thenReturn(headerMap);
    BundleArchive archive = Mockito.mock(BundleArchive.class);
    Mockito.when(archive.getCurrentRevision()).thenReturn(archiveRevision);
    Mockito.when(archive.getId()).thenReturn(3L);
    BundleImpl bi = new BundleImpl(felixMock, null, archive);
    assertEquals(3L, bi.getBundleId());
}
Also used : BundleArchive(org.apache.felix.framework.cache.BundleArchive) HashMap(java.util.HashMap) BundleArchiveRevision(org.apache.felix.framework.cache.BundleArchiveRevision)

Aggregations

HashMap (java.util.HashMap)5 BundleArchive (org.apache.felix.framework.cache.BundleArchive)5 BundleArchiveRevision (org.apache.felix.framework.cache.BundleArchiveRevision)5 Collection (java.util.Collection)3 Bundle (org.osgi.framework.Bundle)3 BundleException (org.osgi.framework.BundleException)3 CollisionHook (org.osgi.framework.hooks.bundle.CollisionHook)3