use of org.osgi.service.device.Device 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.Device in project felix by apache.
the class UtilTest method VerifyValidIsDeviceInstanceValidationIfDevice.
@Test
public void VerifyValidIsDeviceInstanceValidationIfDevice() throws InvalidSyntaxException {
Properties p = new Properties();
p.put(org.osgi.framework.Constants.OBJECTCLASS, new String[] { Device.class.getName() });
ServiceReference ref = createReference(p);
Assert.assertTrue("Incorrectly determined as no device", Util.isDeviceInstance(ref));
}
use of org.osgi.service.device.Device in project felix by apache.
the class DeviceManagerTest method DriverReferral_ReferralFails.
/**
* This test verified correct behavior when after a driver
* attach led to a referral, this referral leads to an exception.
*
* @throws Exception
*/
@Ignore
public void DriverReferral_ReferralFails() throws Exception {
final String referredDriver = "org.apache.felix.driver-2.0";
String[] driverIds = new String[] { "org.apache.felix.driver-1.0", "org.apache.felix.driver-1.1" };
int[] driverMatches = new int[] { 1, Device.MATCH_NONE };
DriverLocator locator = Mockito.mock(DriverLocator.class, "locator for v1.x");
Map<String, Driver> drivers = tstExpectDriverLocatorFor(locator, driverIds, driverMatches);
DriverLocator locatorv2 = Mockito.mock(DriverLocator.class, "locator for v2.x (fails always)");
Mockito.when(locatorv2.findDrivers(Mockito.isA(Dictionary.class))).thenReturn(null);
Mockito.when(locatorv2.loadDriver(Mockito.startsWith("org.apache.felix.driver-1"))).thenReturn(null);
InputStream referredInputStream = Mockito.mock(InputStream.class);
Mockito.when(locatorv2.loadDriver(referredDriver)).thenReturn(referredInputStream);
// this is what initial driver referral eventually leads
// to: the loading of a driver bundle
// we fake it, so that it fails
Mockito.when(m_context.installBundle(Mockito.anyString(), Mockito.isA(InputStream.class))).thenThrow(new BundleException("test exception"));
Driver matched = drivers.get("org.apache.felix.driver-1.0");
final CountDownLatch latch = new CountDownLatch(1);
Answer<String> driver10_attach = new Answer<String>() {
public String answer(InvocationOnMock invocation) throws Throwable {
System.out.println("driver10_attach()");
latch.countDown();
return referredDriver;
}
};
Device device = tstCreateDevice(new String[] { "org.apache.felix" });
Mockito.when(matched.match(m_osgi.getReference(device))).thenReturn(10);
Mockito.when(matched.attach(Mockito.isA(ServiceReference.class))).thenAnswer(driver10_attach);
// for ( String driverId : driverIds )
// {
// Driver driver = drivers.get( driverId );
// tstExpectBundleUninstall( driver );
// }
// the actual test
m_manager.locatorAdded(locator);
m_manager.locatorAdded(locatorv2);
// depman induced callback
m_manager.deviceAdded(m_osgi.getReference(device), device);
if (!latch.await(5, TimeUnit.SECONDS)) {
Assert.fail("expected an attach to: " + driverIds[0]);
}
Mockito.verify(device).noDriverFound();
}
use of org.osgi.service.device.Device in project felix by apache.
the class DeviceManagerTest method EqualMatchWithDriverSelector.
/**
* Two drivers equally match the device. There is a driver selector
* that comes to the rescue that selects driver2.
*
* @throws Exception
*/
@Test
public void EqualMatchWithDriverSelector() throws Exception {
final String driverId1 = "org.apache.felix.driver-1.0";
final String driverId2 = "org.apache.felix.driver-1.1";
DriverLocator locator = Mockito.mock(DriverLocator.class);
Map<String, Driver> drivers = tstExpectDriverLocatorFor(locator, new String[] { driverId1, driverId2 }, new int[] { 20, 20 });
Device device = tstCreateDevice(new String[] { "org.apache.felix" });
DriverSelector selector = Mockito.mock(DriverSelector.class);
SelectorMatcher matcher = new SelectorMatcher(driverId2);
Mockito.when(selector.select(Mockito.eq(m_osgi.getReference(device)), Mockito.isA(Match[].class))).thenAnswer(matcher);
final CountDownLatch attachLatch = tstExpectAttach(drivers.get(driverId2), device);
Utils.inject(m_manager, DriverSelector.class, selector);
m_manager.locatorAdded(locator);
m_manager.deviceAdded(m_osgi.getReference(device), device);
if (!attachLatch.await(5, TimeUnit.SECONDS)) {
Assert.fail("expected an attach");
}
// driver2 is attached, so driver1 bundle should uninstall.
// Driver driver = drivers.get( driverId1 );
// tstVerifyBundleUninstall( driver );
}
use of org.osgi.service.device.Device in project felix by apache.
the class DeviceManagerTest method tstCreateDevice.
private Device tstCreateDevice(String[] cat, boolean isDevice) {
Properties p = new Properties();
p.put(Constants.DEVICE_CATEGORY, cat);
if (isDevice) {
return (Device) tstCreateService(p, Device.class);
}
return tstCreateService(p, Object.class);
}
Aggregations