use of org.osgi.service.device.DriverLocator in project felix by apache.
the class DeviceManagerTest method DeviceAddedWithADriverLocator.
/**
* We add a device while there's one driverlocator that will successfully
* locate and load two driver bundles. We expect a <code>Driver.attach()</code> for
* the best matching driver. There's already a driver loaded that should not match.
*
* @throws Exception
*/
@Test
public void DeviceAddedWithADriverLocator() throws Exception {
final String driverId1 = "org.apache.felix.driver-1.0";
final String driverId2 = "org.apache.felix.driver-1.1";
final String notMatchingButLoadedDriverId = "dotorg.apache.felix.driver-1.0";
DriverLocator locator = Mockito.mock(DriverLocator.class);
Map<String, Driver> drivers = tstExpectDriverLocatorFor(locator, new String[] { driverId1, driverId2 }, new int[] { 30, 3 });
Driver noMatcher = tstCreateDriver(notMatchingButLoadedDriverId, 100);
Device device = tstCreateDevice(new String[] { "org.apache.felix" });
final CountDownLatch attachLatch = tstExpectAttach(drivers.get(driverId1), device);
final CountDownLatch unloadDriverLatch = tstExpectUnloadDriverBundle(drivers.get(driverId2));
m_manager.locatorAdded(locator);
m_manager.driverAdded(m_osgi.getReference(noMatcher), noMatcher);
m_manager.deviceAdded(m_osgi.getReference(device), device);
if (!attachLatch.await(5, TimeUnit.SECONDS)) {
Assert.fail("expected an attach");
}
// of all other (dynamically loaded) driver bundles
if (!unloadDriverLatch.await(5, TimeUnit.SECONDS)) {
Assert.fail("expected an unload");
}
}
use of org.osgi.service.device.DriverLocator in project felix by apache.
the class DeviceManagerTest method DriverLocator_findDriverFails.
// exceptional flow
@Test
public void DriverLocator_findDriverFails() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
Answer<String[]> answer = new Answer<String[]>() {
public String[] answer(InvocationOnMock invocation) throws Throwable {
latch.countDown();
throw new RuntimeException("test exception");
}
};
DriverLocator locator = Mockito.mock(DriverLocator.class, "locator");
Mockito.when(locator.findDrivers(Mockito.isA(Dictionary.class))).thenAnswer(answer);
Device device = tstCreateDevice(new String[] { "org.apache.felix" });
final CountDownLatch latch2 = new CountDownLatch(1);
Answer<Object> answer2 = new Answer<Object>() {
public Object answer(InvocationOnMock invocation) throws Throwable {
latch2.countDown();
return null;
}
};
Mockito.doAnswer(answer2).when(device).noDriverFound();
m_manager.locatorAdded(locator);
m_manager.deviceAdded(m_osgi.getReference(device), device);
if (!latch.await(5, TimeUnit.SECONDS)) {
Assert.fail("expected a call to DriverLocator.findDrivers");
}
if (!latch2.await(5, TimeUnit.SECONDS)) {
Assert.fail("expected a call to Driver.noDriverFound");
}
}
use of org.osgi.service.device.DriverLocator in project felix by apache.
the class DeviceManagerTest method DeviceAddedWithADriverLocatorUnloadFails.
@Test
public void DeviceAddedWithADriverLocatorUnloadFails() throws Exception {
final String driverId1 = "org.apache.felix.driver-1.0";
final String driverId2 = "org.apache.felix.driver-1.1";
final String notMatchingButLoadedDriverId = "dotorg.apache.felix.driver-1.0";
DriverLocator locator = Mockito.mock(DriverLocator.class);
Map<String, Driver> drivers = tstExpectDriverLocatorFor(locator, new String[] { driverId1, driverId2 }, new int[] { 30, 3 });
Driver noMatcher = tstCreateDriver(notMatchingButLoadedDriverId, 100);
Device device = tstCreateDevice(new String[] { "org.apache.felix" });
final CountDownLatch attachLatch = tstExpectAttach(drivers.get(driverId1), device);
final CountDownLatch unloadDriverLatch = new CountDownLatch(1);
ServiceReference driver2Ref = m_osgi.getReference(drivers.get(driverId2));
Bundle driver2Bundle = m_osgi.getBundle(driver2Ref);
Answer<Object> answer = new Answer<Object>() {
public Object answer(InvocationOnMock invocation) throws Throwable {
try {
throw new BundleException("test driverBundle uninstall failed");
} finally {
unloadDriverLatch.countDown();
}
}
};
Mockito.doAnswer(answer).when(driver2Bundle).uninstall();
m_manager.locatorAdded(locator);
m_manager.driverAdded(m_osgi.getReference(noMatcher), noMatcher);
m_manager.deviceAdded(m_osgi.getReference(device), device);
if (!attachLatch.await(5, TimeUnit.SECONDS)) {
Assert.fail("expected an attach");
}
if (!unloadDriverLatch.await(5, TimeUnit.SECONDS)) {
Assert.fail("expected an unload");
}
// since driver1 is attached, we expect an uninstall()
// of all other (dynamically loaded) driver bundles
// Driver driver = drivers.get( driverId2 );
// tstVerifyBundleUninstall( driver );
}
use of org.osgi.service.device.DriverLocator in project felix by apache.
the class DriverLoaderTest method findDriversWithDriverLocator.
/**
* in this test there is a driver locator. the driver locator is instructed to
* even return some driver ids.
* this test tests whether these driver ids are really returned.
*/
@Test
public void findDriversWithDriverLocator() {
List<DriverLocator> locators = new ArrayList<DriverLocator>();
DriverLocator dl = tstExpectDriverIdsFor(new String[] { "org.apache.felix.driver-1.0", "org.apache.felix.driver-1.1" });
locators.add(dl);
Properties dict = new Properties();
Set<String> driverIds = m_loader.findDrivers(locators, dict);
Assert.assertEquals("should not be an empty list", 2, driverIds.size());
}
use of org.osgi.service.device.DriverLocator in project felix by apache.
the class DriverLoaderTest method findDriversNoDriverLocators.
/**
* test whether the driver loader can handle a situation where
* there are no DriverLocators.
*/
@Test
public void findDriversNoDriverLocators() {
List<DriverLocator> locators = new ArrayList<DriverLocator>();
Set<String> driverIds = m_loader.findDrivers(locators, new Properties());
Assert.assertTrue("should be an empty list", driverIds.size() == 0);
}
Aggregations