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