AugmentingEnhancer implements DroneInstanceEnhancer<RemoteWebDriver> { @Override public boolean canEnhance(InstanceOrCallableInstance instance, Class<?> droneType, Class<? extends Annotation> qualifier) { if (RemoteWebDriver.class == droneType || ReusableRemoteWebDriver.class == droneType) { return true; } Class<?> realInstanceClass = instance.asInstance(droneType).getClass(); if (RemoteWebDriver.class == realInstanceClass || ReusableRemoteWebDriver.class == realInstanceClass || DroneAugmented.class.isAssignableFrom(realInstanceClass)) { return true; } return false; }  @Override int getPrecedence(); @Override boolean canEnhance(InstanceOrCallableInstance instance, Class<?> droneType,
        Class<? extends Annotation> qualifier); @Override RemoteWebDriver enhance(RemoteWebDriver instance, Class<? extends Annotation> qualifier); @Override RemoteWebDriver deenhance(RemoteWebDriver enhancedInstance, Class<? extends Annotation> qualifier); static final String DRONE_AUGMENTED; }
@Test public void testCanEnhance() throws Exception { Unsafe unsafe = new Unsafe(); RemoteWebDriver remoteDriver = unsafe.createInstanceWithoutInvokingConstructor(RemoteWebDriver.class); RemoteWebDriver reusableRemoteDriver = mock(ReusableRemoteWebDriver.class); DroneAugmented augmentedDriver = mock(DroneAugmented.class); InstanceOrCallableInstance instance1 = mock(InstanceOrCallableInstance.class); InstanceOrCallableInstance instance2 = mock(InstanceOrCallableInstance.class); InstanceOrCallableInstance instance3 = mock(InstanceOrCallableInstance.class); InstanceOrCallableInstance instance4 = mock(InstanceOrCallableInstance.class); doReturn(remoteDriver).when(instance1).asInstance(RemoteWebDriver.class); doReturn(remoteDriver).when(instance1).asInstance(WebDriver.class); doReturn(augmentedDriver).when(instance1).asInstance(ReusableRemoteWebDriver.class); doReturn(unsafe.createInstanceWithoutInvokingConstructor(FirefoxDriver.class)).when(instance2) .asInstance(WebDriver.class); doReturn(reusableRemoteDriver).when(instance3).asInstance(WebDriver.class); doReturn(augmentedDriver).when(instance4).asInstance(WebDriver.class); assertTrue("AugmentingEnhancer should enhance when droneType == RemoteWebDriver.class", enhancer.canEnhance(instance1, RemoteWebDriver.class, Default.class)); assertTrue("AugmentingEnhancer should enhance when droneType == ReusableRemoteWebDriver.class", enhancer.canEnhance(instance1, ReusableRemoteWebDriver.class, Default.class)); assertTrue("AugmentingEnhancer should enhance when real instance is RemoteWebDriver", enhancer.canEnhance(instance1, WebDriver.class, Default.class)); assertTrue("AugmentingEnhancer should enhance when real instance already augmented!", enhancer.canEnhance(instance4, WebDriver.class, Default.class)); assertFalse( "AugmentingEnhancer should not enhance when real instance is not RemoteWebDriver || ReusableRemoteWebDriver", enhancer.canEnhance(instance2, WebDriver.class, Default.class)); assertFalse("AugmentingEnhancer should not enhance extensions of supported classes!", enhancer.canEnhance(instance3, WebDriver.class, Default.class)); }