use of org.apache.felix.fileinstall.plugins.installer.InstallableListener in project felix by apache.
the class InstallerIntegrationTest method testInstallArchives.
/*
* This is a complex case that tests the interplay of multiple archives with
* overlapping content.
*
* - Archive 1 contains bundles A and B.
*
* - Archive 2 contains bundles A and C.
*
* The process is as follows:
*
* i. Make installer aware of both archives. Both now present as an
* installable unit with 2 artifacts (A+B and A+C).
*
* ii. Install archive 1.
* Archive 2 gets invalidated and re-resolved. It now presents as an
* installable unit with 1 artifact (C) because A is already in the
* framework.
*
* iii. Remove archive 1. Archive 2 gets invalidated and
* re-resolved. It now presents as an installable unit with 2 artifacts
* (A+C) because A is no longer in the framework.
*
* Note that step (iii) is equivalent to deleting the archive from
* FileInstall's load directory. Therefore it doesn't reappear as a RESOLVED
* installable unit. To reinstall this archive, the user would have to copy
* it back into the load directory.
*
*/
@Test
public void testInstallArchives() throws Exception {
// Register InstallableListener
EventQueue eventQueue = new EventQueue();
ServiceRegistration<InstallableListener> mockListenerReg = this.bundleContext.registerService(InstallableListener.class, eventQueue, null);
try {
assertEquals("Shouldn't be any install events yet", 0, eventQueue.depth());
InstallableUnitEvent event;
// Provide the sample archive
File sampleArchive1 = new File(this.dataDir, "valid1.bar");
ArtifactInstaller installer = this.artifactInstallerTracker.getService();
assertNotNull("ArtifactInstaller service should exist", installer);
assertTrue("installer should handle sample archive", installer.canHandle(sampleArchive1));
installer.install(sampleArchive1);
// Wait for resolve to occur
Thread.sleep(2000);
assertEquals(1, eventQueue.depth());
event = eventQueue.pop();
InstallableUnit unit1 = event.getUnit();
assertEquals(State.RESOLVED, event.getNewState());
assertEquals("samples.valid1", event.getUnit().getSymbolicName());
assertEquals("samples.valid1 requires 2 bundles to be installed", 2, event.getUnit().getArtifacts().size());
// Add a second archive
File sampleArchive2 = new File(this.dataDir, "valid2.bar");
assertTrue("installer should handle sample archive", installer.canHandle(sampleArchive2));
installer.install(sampleArchive2);
// Wait for resolve to occur
Thread.sleep(2000);
assertEquals(1, eventQueue.depth());
event = eventQueue.pop();
assertEquals(State.RESOLVED, event.getNewState());
assertEquals("samples.valid2", event.getUnit().getSymbolicName());
assertEquals("samples.valid2 requires 2 bundles to be installed", 2, event.getUnit().getArtifacts().size());
// Install first archive.
unit1.install().getValue();
// Check bundles were actually installed!
assertNotNull(findBundle("org.example.a"));
assertNotNull(findBundle("org.example.b"));
// The installation of first archive should cause invalidation of second archive resolution result, followed by re-resolve.
// First, installing #1
event = eventQueue.pop();
assertEquals(State.INSTALLING, event.getNewState());
assertEquals("samples.valid1", event.getUnit().getSymbolicName());
// Next installed #1
event = eventQueue.pop();
assertEquals(State.INSTALLED, event.getNewState());
assertEquals("samples.valid1", event.getUnit().getSymbolicName());
// Next removed #2 (due to invalidation)
// Short sleep necessary because the invalidate can happen on another thread (eg framework refresh)
Thread.sleep(500);
event = eventQueue.pop();
assertEquals(State.REMOVED, event.getNewState());
assertEquals("samples.valid2", event.getUnit().getSymbolicName());
// Next resolved #2 (re-resolve after invalidation)
// Another sleep because the re-resolve happens after at least 1 sec delay
Thread.sleep(2000);
event = eventQueue.pop();
assertEquals(State.RESOLVED, event.getNewState());
assertEquals("samples.valid2", event.getUnit().getSymbolicName());
assertEquals("samples.valid2 now requires 1 bundle to be installed", 1, event.getUnit().getArtifacts().size());
assertEquals(0, eventQueue.depth());
// Now uninstall first archive
installer.uninstall(sampleArchive1);
Thread.sleep(500);
// Check bundles were actually uninstalled!
assertNull(findBundle("org.example.a"));
assertNull(findBundle("org.example.b"));
// Removed event for #1
Thread.sleep(500);
event = eventQueue.pop();
assertEquals(State.REMOVED, event.getNewState());
assertEquals("samples.valid1", event.getUnit().getSymbolicName());
// Removed event for #2 due to invalidation
event = eventQueue.pop();
assertEquals(State.REMOVED, event.getNewState());
assertEquals("samples.valid2", event.getUnit().getSymbolicName());
// Re-resolution of #2 after a little sleep
Thread.sleep(2000);
event = eventQueue.pop();
assertEquals(State.RESOLVED, event.getNewState());
assertEquals("samples.valid2", event.getUnit().getSymbolicName());
assertEquals("samples.valid2 now requires 2 bundle to be installed", 2, event.getUnit().getArtifacts().size());
assertEquals(0, eventQueue.depth());
} finally {
mockListenerReg.unregister();
}
}
use of org.apache.felix.fileinstall.plugins.installer.InstallableListener in project felix by apache.
the class InstallerIntegrationTest method testErrorFromInvalidArchive.
@Test
public void testErrorFromInvalidArchive() throws Exception {
Object[] artifactInstallers = this.artifactInstallerTracker.getServices();
assertNotNull("Should be exactly one ArtifactInstaller service", artifactInstallers);
assertEquals("Should be exactly one ArtifactInstaller service", 1, artifactInstallers.length);
// Register mock InstallableListener
List<String> installEvents = new LinkedList<>();
InstallableListener mockInstallListener = new InstallableListener() {
@Override
public void installableUnitsChanged(Collection<InstallableUnitEvent> events) {
for (InstallableUnitEvent event : events) {
String message = String.format("%s %s", event.getNewState(), event.getUnit().getSymbolicName());
installEvents.add(message);
}
}
};
ServiceRegistration<InstallableListener> mockListenerReg = this.bundleContext.registerService(InstallableListener.class, mockInstallListener, null);
try {
assertEquals("Shouldn't be any install events yet", 0, installEvents.size());
// Provide the sample archive
File sampleArchive = new File(this.dataDir, "org.example.invalid-missing-rb.bar");
ArtifactInstaller installer = this.artifactInstallerTracker.getService();
assertTrue("installer should handle sample archive", installer.canHandle(sampleArchive));
installer.install(sampleArchive);
// Wait for resolve to occur
Thread.sleep(2000);
assertEquals(1, installEvents.size());
assertEquals("ERROR org.example.invalid-missing-rb", installEvents.get(0));
} finally {
mockListenerReg.unregister();
}
}
Aggregations