8000 ParameterRule for Arquillian junit tests · coder1650/javaee7-samples@e15ffd5 · GitHub
[go: up one dir, main page]

Skip to content

Commit e15ffd5

Browse files
ParameterRule for Arquillian junit tests
As described here: http://www.poolik.com/2014/02/how-to-run-parameterized-junit-arquillian-tests/ where the credits should go
1 parent c929944 commit e15ffd5

File tree

3 files changed

+128
-1
lines changed

3 files changed

+128
-1
lines changed

test-utils/pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,16 @@
1010
</parent>
1111
<artifactId>test-utils</artifactId>
1212
<name>Java EE 7 Sample: javaee7-samples - test-utils</name>
13-
</project>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>junit</groupId>
17+
<artifactId>junit</artifactId>
18+
<version>4.11</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.jboss.arquillian.container</groupId>
22+
<artifactId>arquillian-container-test-api</artifactId>
23+
</dependency>
24+
</dependencies>
25+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.javaee7;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target({ElementType.FIELD})
10+
public @interface Parameter {
11+
}
12+
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package org.javaee7;
2+
3+
import org.jboss.arquillian.container.test.api.Deployment;
4+
5+
import org.junit.rules.MethodRule;
6+
import org.junit.runners.model.FrameworkMethod;
7+
import org.junit.runners.model.Statement;
8+
9+
import javax.naming.InitialContext;
10+
import javax.naming.NamingException;
11+
import java.lang.reflect.Field;
12+
import java.lang.reflect.Method;
13+
import java.util.List;
14+
15+
/**
16+
* Helper class for Parametrized tests as described here:
17+
* http://blog.schauderhaft.de/2012/12/16/writing-parameterized-tests-with-junit-rules/
18+
*
19+
* @param <T>
20+
*/
21+
public class ParameterRule<T> implements MethodRule {
22+
private final List<T> params;
23+
24+
public ParameterRule(List<T> params) {
25+
if (params == null || params.size() == 0) {
26+
throw new IllegalArgumentException("'params' must be specified and have more then zero length!");
27+
}
28+
this.params = params;
29+
}
30+
31+
@Override
32+
public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
33+
return new Statement() {
34+
@Override
35+
public void evaluate() throws Throwable {
36+
boolean runInContainer = getDeploymentMethod(target).getAnnotation(Deployment.class).testable();
37+
if (runInContainer) {
38+
evaluateParametersInContainer(base, target);
39+
} else {
40+
evaluateParametersInClient(base, target);
41+
}
42+
}
43+
};
44+
}
45+
46+
private Method getDeploymentMethod(Object target) throws NoSuchMethodException {
47+
Method[] methods = target.getClass().getDeclaredMethods();
48+
for (Method method : methods) {
49+
if (method.getAnnotation(Deployment.class) != null) return method;
50+
}
51+
throw new IllegalStateException("No method with @Deployment annotation found!");
52+
}
53+
54+
private void evaluateParametersInContainer(Statement base, Object target) throws Throwable {
55+
if (isRunningInContainer()) {
56+
evaluateParamsToTarget(base, target);
57+
} else {
58+
ignoreStatementExecution(base);
59+
}
60+
}
61+
62+
private void evaluateParametersInClient(Statement base, Object target) throws Throwable {
63+
if (isRunningInContainer()) {
64+
ignoreStatementExecution(base);
65+
} else {
66+
evaluateParamsToTarget(base, target);
67+
}
68+
}
69+
70+
private boolean isRunningInContainer() {
71+
try {
72+
new InitialContext().lookup("java:comp/env");
73+
return true;
74+
} catch (NamingException e) {
75+
return false;
76+
}
77+
}
78+
79+
private void evaluateParamsToTarget(Statement base, Object target) throws Throwable {
80+
for (Object param : params) {
81+
Field targetField = getTargetField(target);
82+
if (!targetField.isAccessible()) {
83+
targetField.setAccessible(true);
84+
}
85+
targetField.set(target, param);
86+
base.evaluate();
87+
}
88+
}
89+
90+
private Field getTargetField(Object target) throws NoSuchFieldException {
91+
Field[] allFields = target.getClass().getDeclaredFields();
92+
for (Field field : allFields) {
93+
if (field.getAnnotation(Parameter.class) != null) return field;
94+
}
95+
throw new IllegalStateException("No field with @Parameter annotation found! Forgot to add it?");
96+
}
97+
98+
private void ignoreStatementExecution(Statement base) {
99+
try {
100+
base.evaluate();
101+
} catch (Throwable ignored) {}
102+
}
103+
}

0 commit comments

Comments
 (0)
0