Search in sources :

Example 6 with Driver

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

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

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

the class DriverAnalyzerTest method VerifyCorrectDriverIsIgnored.

@Test
public void VerifyCorrectDriverIsIgnored() {
    Properties p = new Properties();
    p.put(Constants.DRIVER_ID, "a-driver-id");
    ServiceReference ref = m_osgi.createReference(new String[] { Driver.class.getName() }, p);
    m_analyzer.driverAdded(ref);
    Mockito.verifyZeroInteractions(m_log);
}
Also used : Driver(org.osgi.service.device.Driver) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 9 with Driver

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

the class DeviceManager method driverModified.

public void driverModified(ServiceReference ref, Object obj) {
    final Driver driver = Driver.class.cast(obj);
    debug("driver modified: " + Util.showDriver(ref));
    m_drivers.remove(ref);
    m_drivers.put(ref, new DriverAttributes(ref, driver));
    // check if devices have become idle
    // after some time
    schedule(new CheckForIdleDevices());
}
Also used : Driver(org.osgi.service.device.Driver)

Example 10 with Driver

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

the class DeviceManager method driverAdded.

public void driverAdded(ServiceReference ref, Object obj) {
    final Driver driver = Driver.class.cast(obj);
    m_drivers.put(ref, new DriverAttributes(ref, driver));
    debug("driver appeared: " + Util.showDriver(ref));
    // immediately check for idle devices
    submit(new CheckForIdleDevices());
}
Also used : Driver(org.osgi.service.device.Driver)

Aggregations

Driver (org.osgi.service.device.Driver)15 Test (org.junit.Test)8 ServiceReference (org.osgi.framework.ServiceReference)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Device (org.osgi.service.device.Device)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Answer (org.mockito.stubbing.Answer)4 Bundle (org.osgi.framework.Bundle)4 DriverLocator (org.osgi.service.device.DriverLocator)4 InputStream (java.io.InputStream)2 Dictionary (java.util.Dictionary)2 BundleException (org.osgi.framework.BundleException)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 DriverAttributes (org.apache.felix.das.DriverAttributes)1 Ignore (org.junit.Ignore)1 DriverSelector (org.osgi.service.device.DriverSelector)1