Search in sources :

Example 1 with Device

use of org.osgi.service.device.Device in project felix by apache.

the class DeviceManagerTest method DeviceAddedNoDriverLocator.

// intended flow, various configurations
/**
 * 	We add a device, but there are no driver locators, so
 *  the noDriverFound method must be called
 * @throws InterruptedException
 */
@Test
public void DeviceAddedNoDriverLocator() throws InterruptedException {
    // create a mocked device
    Device device = tstCreateDevice(new String[] { "org.apache.felix" });
    CountDownLatch latch = tstExpectNoDriverFound(device);
    m_manager.deviceAdded(m_osgi.getReference(device), device);
    if (!latch.await(5, TimeUnit.SECONDS)) {
        Assert.fail("expected call noDriverFound");
    }
}
Also used : Device(org.osgi.service.device.Device) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with Device

use of org.osgi.service.device.Device in project felix by apache.

the class DeviceManagerTest method DeviceAddedNoDriverLocatorSuccessfulAttach.

/**
 * 	We add a device, but there are no driver locators, however, there is a driver
 *  that matches. Thus an attach must follow.
 * @throws Exception
 */
@Test
public void DeviceAddedNoDriverLocatorSuccessfulAttach() throws Exception {
    Device device = tstCreateDevice(new String[] { "org.apache.felix" });
    Driver driver = tstCreateDriver("org.apache.felix.driver-1.0", 1);
    CountDownLatch attachLatch = tstExpectAttach(driver, device);
    m_manager.driverAdded(m_osgi.getReference(driver), driver);
    m_manager.deviceAdded(m_osgi.getReference(device), device);
    if (!attachLatch.await(5, TimeUnit.SECONDS)) {
        Assert.fail("expected attach");
    }
}
Also used : Device(org.osgi.service.device.Device) Driver(org.osgi.service.device.Driver) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with Device

use of org.osgi.service.device.Device in project felix by apache.

the class DeviceManagerTest method DeviceAddedNoDriverLocatorAttachFails.

/**
 * 	We add a device, but there are no driver locators, however, there is a driver
 *  but it sadly doesn't match. Thus a <code>noDriverFound()</code> is called.
 *
 * @throws Exception
 */
@Test
public void DeviceAddedNoDriverLocatorAttachFails() throws Exception {
    Device device = tstCreateDevice(new String[] { "org.apache.felix" });
    Driver driver = tstCreateDriver("org.apache.felix.driver-1.0", Device.MATCH_NONE);
    CountDownLatch attachLatch = tstExpectNoDriverFound(device);
    m_manager.driverAdded(m_osgi.getReference(driver), driver);
    m_manager.deviceAdded(m_osgi.getReference(device), device);
    if (!attachLatch.await(5, TimeUnit.SECONDS)) {
        Assert.fail("expected attach");
    }
}
Also used : Device(org.osgi.service.device.Device) Driver(org.osgi.service.device.Driver) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with Device

use of org.osgi.service.device.Device 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 5 with Device

use of org.osgi.service.device.Device 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)

Aggregations

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