Skip to content

Commit

Permalink
ARQ-2119 Introduced system property for skippping Drone creation/inje…
Browse files Browse the repository at this point in the history
…ction (#94)

Introduced system property arquillian.drone.skip.creation for
skipping Drone creation/injection
  • Loading branch information
MatousJobanek committed Jun 2, 2017
1 parent 295b076 commit 07e3a2e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 22 deletions.
4 changes: 4 additions & 0 deletions docs/configuring-drone-instances.adoc
Expand Up @@ -218,3 +218,7 @@ performed.

Then System property are applied in the same fashion.

== Skipping creation of @Drone instances

In case you want to skip a creation/injection of `@Drone` instances you can use a system property `arquillian.drone.skip.creation` with a value `true`. This property is checked in a `@Before` phase, so you can modify the property during the test execution.

7 changes: 6 additions & 1 deletion drone-impl/pom.xml
Expand Up @@ -105,13 +105,18 @@
<artifactId>arquillian-config-impl-base</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${version.mockito}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>${version.system.rules}</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
Expand Up @@ -20,9 +20,12 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.jboss.arquillian.core.api.Event;
import org.jboss.arquillian.core.api.Injector;
import org.jboss.arquillian.core.api.Instance;
Expand Down Expand Up @@ -62,6 +65,11 @@ public class DroneTestEnricher implements TestEnricher {
@ApplicationScoped
private InstanceProducer<DeploymentDronePointsRegistry> deploymentDronePointsRegistry;

public static final String ARQUILLIAN_DRONE_CREATION_PROPERTY = "arquillian.drone.skip.creation";
private static final String ARQUILLIAN_DRONE_CREATION_PROPERTY_MSG = "The property "
+ ARQUILLIAN_DRONE_CREATION_PROPERTY
+ " is set to true - the Drone injection point won't be instantiated for the ";

@Override
public void enrich(Object testCase) {
enrichTestClass(testCase.getClass(), testCase, false);
Expand All @@ -72,28 +80,38 @@ public Object[] resolve(Method method) {
DroneContext context = droneContext.get();
DronePoint<?>[] dronePoints = InjectionPoints.parametersInMethod(droneContext.get(), method);
Object[] resolution = new Object[dronePoints.length];
for (int i = 0; i < dronePoints.length; i++) {
DronePoint<?> dronePoint = dronePoints[i];
if (dronePoint == null) {
resolution[i] = null;
continue;
}

if (!ensureInjectionPointPrepared(dronePoint, true)) {
// in case of deployment-scoped drone point tied to a deployment that isn't deployed, only register
// it - it will be injected when the deployment is finished
registerDeploymentDronePoint(dronePoint, method);
continue;
if (droneInstantiationShouldBeSkipped()) {
List<DronePoint<?>> points =
Arrays.stream(dronePoints).filter(dronePoint -> dronePoint != null).collect(Collectors.toList());
if (points.size() > 0) {
log.info(ARQUILLIAN_DRONE_CREATION_PROPERTY_MSG + "method: " + method);
}
log.log(Level.FINE, "Injecting @Drone for method {0}, injection point {1}",
new Object[] {method.getName(), dronePoint}
);
} else {
for (int i = 0; i < dronePoints.length; i++) {
DronePoint<?> dronePoint = dronePoints[i];
if (dronePoint == null) {
resolution[i] = null;
continue;
}

Object drone = context.get(dronePoint).getInstance();
Validate.stateNotNull(drone, "Retrieved a null from Drone Context, which is not a valid Drone browser " +
"object" +
".\nMethod: {0}, injection point: {1},", method.getName(), dronePoint);
resolution[i] = drone;
if (!ensureInjectionPointPrepared(dronePoint, true)) {
// in case of deployment-scoped drone point tied to a deployment that isn't deployed, only register
// it - it will be injected when the deployment is finished
registerDeploymentDronePoint(dronePoint, method);
continue;
}
log.log(Level.FINE, "Injecting @Drone for method {0}, injection point {1}",
new Object[] {method.getName(), dronePoint}
);

Object drone = context.get(dronePoint).getInstance();
Validate
.stateNotNull(drone, "Retrieved a null from Drone Context, which is not a valid Drone browser " +
"object" +
".\nMethod: {0}, injection point: {1},", method.getName(), dronePoint);
resolution[i] = drone;
}
}

return resolution;
Expand All @@ -116,6 +134,11 @@ public void enrichTestClass(Class<?> testClass, Object testCase, boolean onlySta
Map<Field, DronePoint<?>> injectionPoints = InjectionPoints.fieldsInClass(droneContext.get(),
testClass);

if (injectionPoints.size() > 0 && droneInstantiationShouldBeSkipped()) {
log.info(ARQUILLIAN_DRONE_CREATION_PROPERTY_MSG + "field(s): " + injectionPoints.keySet());
return;
}

for (Field field : injectionPoints.keySet()) {
if (onlyStatic && !Modifier.isStatic(field.getModifiers())) {
continue;
Expand Down Expand Up @@ -148,6 +171,10 @@ public void enrichTestClass(Class<?> testClass, Object testCase, boolean onlySta
}
}

private boolean droneInstantiationShouldBeSkipped() {
return Boolean.valueOf(SecurityActions.getProperty(ARQUILLIAN_DRONE_CREATION_PROPERTY));
}

private void registerDeploymentDronePoint(DronePoint dronePoint, Object testCase) {
if (deploymentDronePointsRegistry.get() == null) {
deploymentDronePointsRegistry.set(injector.get().inject(new DeploymentDronePointsRegistry()));
Expand Down
Expand Up @@ -49,12 +49,15 @@
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import static org.jboss.arquillian.drone.impl.DroneTestEnricher.ARQUILLIAN_DRONE_CREATION_PROPERTY;

/**
* Tests Configurator precedence and its retrieval chain, uses qualifier as well.
* <p/>
Expand All @@ -79,6 +82,9 @@ public class EnricherTestCase extends AbstractTestTestBase {
@Mock
private ServiceLoader serviceLoader;

@Rule
public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties();

@Override
protected void addExtensions(List<Class<?>> extensions) {
extensions.add(DroneLifecycleManager.class);
Expand Down Expand Up @@ -185,6 +191,22 @@ public void testMethodQualiferUnregistered() throws Exception {

@Test
public void testClassWithoutArquillianLifecycle() throws Exception {
verifyTestClassWithoutArquillianLifecycle(true);
}

@Test
public void testClassWithoutArquillianLifecycleWithSkipDroneCreationTrue() throws Exception {
System.setProperty(ARQUILLIAN_DRONE_CREATION_PROPERTY, "true");
verifyTestClassWithoutArquillianLifecycle(false);
}

@Test
public void testClassWithoutArquillianLifecycleWithSkipDroneCreationFalse() throws Exception {
System.setProperty(ARQUILLIAN_DRONE_CREATION_PROPERTY, "false");
verifyTestClassWithoutArquillianLifecycle(true);
}

private void verifyTestClassWithoutArquillianLifecycle(boolean shouldBeInstantiated) throws Exception {
Object instance = new NonArquillianClass();
Method testMethod = NonArquillianClass.class.getMethod("someMethod", MockDrone.class);

Expand All @@ -201,12 +223,12 @@ public void testClassWithoutArquillianLifecycle() throws Exception {
DronePoint<MockDrone> classDronePoint = new DronePointImpl<MockDrone>(MockDrone.class,
DronePoint.Lifecycle.CLASS,
AnnotationMocks.drone());
verifyDronePointInstantiated(true, context, classDronePoint);
verifyDronePointInstantiated(shouldBeInstantiated, context, classDronePoint);

DronePoint<MockDrone> methodDronePoint = new DronePointImpl<MockDrone>(MockDrone.class,
DronePoint.Lifecycle.METHOD,
AnnotationMocks.drone());
verifyDronePointInstantiated(true, context, methodDronePoint);
verifyDronePointInstantiated(shouldBeInstantiated, context, methodDronePoint);

testMethod.invoke(instance, parameters);
}
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -45,6 +45,7 @@
<version.junit>4.12</version.junit>
<version.assertj>3.6.2</version.assertj>
<version.mockito>2.3.11</version.mockito>
<version.system.rules>1.16.1</version.system.rules>

<!-- To avoid collisions with Jetty version we are using the same as packaged in Selenium Server -->
<version.org.eclipse.jetty>9.4.3.v20170317</version.org.eclipse.jetty>
Expand Down

0 comments on commit 07e3a2e

Please sign in to comment.