Search in sources :

Example 1 with DriverLocator

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");
    }
}
Also used : DriverLocator(org.osgi.service.device.DriverLocator) Device(org.osgi.service.device.Device) Driver(org.osgi.service.device.Driver) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with DriverLocator

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");
    }
}
Also used : Answer(org.mockito.stubbing.Answer) Dictionary(java.util.Dictionary) DriverLocator(org.osgi.service.device.DriverLocator) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Device(org.osgi.service.device.Device) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with DriverLocator

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 );
}
Also used : Device(org.osgi.service.device.Device) Bundle(org.osgi.framework.Bundle) Driver(org.osgi.service.device.Driver) CountDownLatch(java.util.concurrent.CountDownLatch) ServiceReference(org.osgi.framework.ServiceReference) Answer(org.mockito.stubbing.Answer) DriverLocator(org.osgi.service.device.DriverLocator) InvocationOnMock(org.mockito.invocation.InvocationOnMock) BundleException(org.osgi.framework.BundleException) Test(org.junit.Test)

Example 4 with DriverLocator

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());
}
Also used : DriverLocator(org.osgi.service.device.DriverLocator) ArrayList(java.util.ArrayList) Properties(java.util.Properties) Test(org.junit.Test)

Example 5 with DriverLocator

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);
}
Also used : DriverLocator(org.osgi.service.device.DriverLocator) ArrayList(java.util.ArrayList) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

DriverLocator (org.osgi.service.device.DriverLocator)16 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)8 ServiceReference (org.osgi.framework.ServiceReference)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 Device (org.osgi.service.device.Device)5 Properties (java.util.Properties)4 Driver (org.osgi.service.device.Driver)4 Dictionary (java.util.Dictionary)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Answer (org.mockito.stubbing.Answer)3 BundleException (org.osgi.framework.BundleException)3 Bundle (org.osgi.framework.Bundle)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Ignore (org.junit.Ignore)1 DriverSelector (org.osgi.service.device.DriverSelector)1