Search in sources :

Example 11 with Driver

use of org.osgi.service.device.Driver 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();
}
Also used : Dictionary(java.util.Dictionary) InputStream(java.io.InputStream) Device(org.osgi.service.device.Device) 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) Ignore(org.junit.Ignore)

Example 12 with Driver

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

Example 13 with Driver

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

the class DeviceManagerTest method tstExpectInstallDriverBundle.

/**
 * prepared all expected behavior for the installation of a dynamic driver
 * bundle based on an acquired InputStream.
 *
 * @param driverId
 * @param match
 * @param in
 * @return
 * @throws BundleException
 * @throws Exception
 */
private Driver tstExpectInstallDriverBundle(String driverId, int match, InputStream in) throws BundleException, Exception {
    Bundle bundle = Mockito.mock(Bundle.class, "driverBundle");
    Mockito.when(m_context.installBundle(Mockito.eq("_DD_" + driverId), Mockito.eq(in))).thenReturn(bundle);
    final Driver driver = tstCreateDriver(driverId, match);
    final ServiceReference driverRef = m_osgi.getReference(driver);
    Answer<Object> answer = new Answer<Object>() {

        public Object answer(InvocationOnMock invocation) throws Throwable {
            m_manager.driverAdded(driverRef, driver);
            return null;
        }
    };
    // bundle start leads to the addition of the driver to
    // the device manager.
    Mockito.doAnswer(answer).when(bundle).start();
    Mockito.when(bundle.getRegisteredServices()).thenReturn(new ServiceReference[] { driverRef });
    return driver;
}
Also used : Answer(org.mockito.stubbing.Answer) Bundle(org.osgi.framework.Bundle) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Driver(org.osgi.service.device.Driver) ServiceReference(org.osgi.framework.ServiceReference)

Example 14 with Driver

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

the class DeviceManagerTest method tstCreateDriver.

private Driver tstCreateDriver(Properties p) throws Exception {
    Driver driver = Mockito.mock(Driver.class);
    ServiceReference ref = m_osgi.registerService(new String[] { Driver.class.getName() }, driver, p);
    MatchAnswer answer = new MatchAnswer(ref);
    Mockito.when(driver.match(Mockito.isA(ServiceReference.class))).thenAnswer(answer);
    Bundle bundle = m_osgi.getBundle(ref);
    Mockito.when(bundle.getLocation()).thenReturn(DriverLoader.DRIVER_LOCATION_PREFIX + p.getProperty(Constants.DRIVER_ID));
    return driver;
}
Also used : Bundle(org.osgi.framework.Bundle) Driver(org.osgi.service.device.Driver) ServiceReference(org.osgi.framework.ServiceReference)

Example 15 with Driver

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

the class DriverMatcherTest method tstCreateDriverAttributes.

private DriverAttributes tstCreateDriverAttributes(String id, int match, int ranking) throws Exception {
    Bundle bundle = Mockito.mock(Bundle.class);
    ServiceReference ref = Mockito.mock(ServiceReference.class);
    Mockito.when(ref.getBundle()).thenReturn(bundle);
    Mockito.when(bundle.getLocation()).thenReturn(DriverLoader.DRIVER_LOCATION_PREFIX + "-" + id);
    Mockito.when(ref.getProperty(Constants.SERVICE_ID)).thenReturn(m_serviceId++);
    Mockito.when(ref.getProperty(org.osgi.service.device.Constants.DRIVER_ID)).thenReturn(id);
    if (ranking > 0) {
        Mockito.when(ref.getProperty(Constants.SERVICE_RANKING)).thenReturn(ranking);
    } else if (ranking == 0) {
        Mockito.when(ref.getProperty(Constants.SERVICE_RANKING)).thenReturn(null);
    } else {
        // an invalid ranking object
        Mockito.when(ref.getProperty(Constants.SERVICE_RANKING)).thenReturn(new Object());
    }
    Driver driver = Mockito.mock(Driver.class);
    Mockito.when(driver.match(Mockito.isA(ServiceReference.class))).thenReturn(match);
    return new DriverAttributes(ref, driver);
}
Also used : DriverAttributes(org.apache.felix.das.DriverAttributes) Bundle(org.osgi.framework.Bundle) Driver(org.osgi.service.device.Driver) ServiceReference(org.osgi.framework.ServiceReference)

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