1
1
/*
2
- * Copyright 2006-2010 the original author or authors.
2
+ * Copyright 2006-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
61
61
* </pre>
62
62
*
63
63
* @author Dave Syer
64
- *
64
+ * @author Chris Schaefer
65
65
*/
66
66
public class StepScopeTestExecutionListener implements TestExecutionListener {
67
-
68
67
private static final String STEP_EXECUTION = StepScopeTestExecutionListener .class .getName () + ".STEP_EXECUTION" ;
68
+ private static final String SET_ATTRIBUTE_METHOD_NAME = "setAttribute" ;
69
+ private static final String HAS_ATTRIBUTE_METHOD_NAME = "hasAttribute" ;
70
+ private static final String GET_ATTRIBUTE_METHOD_NAME = "getAttribute" ;
71
+ private static final String GET_TEST_INSTANCE_METHOD = "getTestInstance" ;
69
72
70
73
/**
71
74
* Set up a {@link StepExecution} as a test context attribute.
@@ -74,11 +77,13 @@ public class StepScopeTestExecutionListener implements TestExecutionListener {
74
77
* @throws Exception if there is a problem
75
78
* @see TestExecutionListener#prepareTestInstance(TestContext)
76
79
*/
77
- @ Override
80
+ @ Override
78
81
public void prepareTestInstance (TestContext testContext ) throws Exception {
79
82
StepExecution stepExecution = getStepExecution (testContext );
83
+
80
84
if (stepExecution != null ) {
81
- testContext .setAttribute (STEP_EXECUTION , stepExecution );
85
+ Method method = TestContext .class .getMethod (SET_ATTRIBUTE_METHOD_NAME , String .class , Object .class );
86
+ ReflectionUtils .invokeMethod (method , testContext , STEP_EXECUTION , stepExecution );
82
87
}
83
88
}
84
89
@@ -87,38 +92,45 @@ public void prepareTestInstance(TestContext testContext) throws Exception {
87
92
* @throws Exception if there is a problem
88
93
* @see TestExecutionListener#beforeTestMethod(TestContext)
89
94
*/
90
- @ Override
91
- public void beforeTestMethod (org .springframework .test .context .TestContext testContext ) throws Exception {
92
- if (testContext .hasAttribute (STEP_EXECUTION )) {
93
- StepExecution stepExecution = (StepExecution ) testContext .getAttribute (STEP_EXECUTION );
95
+ @ Override
96
+ public void beforeTestMethod (TestContext testContext ) throws Exception {
97
+ Method hasAttributeMethod = TestContext .class .getMethod (HAS_ATTRIBUTE_METHOD_NAME , String .class );
98
+ Boolean hasAttribute = (Boolean ) ReflectionUtils .invokeMethod (hasAttributeMethod , testContext , STEP_EXECUTION );
99
+
100
+ if (hasAttribute ) {
101
+ Method method = TestContext .class .getMethod (GET_ATTRIBUTE_METHOD_NAME , String .class );
102
+ StepExecution stepExecution = (StepExecution ) ReflectionUtils .invokeMethod (method , testContext , STEP_EXECUTION );
103
+
94
104
StepSynchronizationManager .register (stepExecution );
95
105
}
96
-
97
106
}
98
107
99
108
/**
100
109
* @param testContext the current test context
101
110
* @throws Exception if there is a problem
102
111
* @see TestExecutionListener#afterTestMethod(TestContext)
103
112
*/
104
- @ Override
113
+ @ Override
105
114
public void afterTestMethod (TestContext testContext ) throws Exception {
106
- if (testContext .hasAttribute (STEP_EXECUTION )) {
115
+ Method method = TestContext .class .getMethod (HAS_ATTRIBUTE_METHOD_NAME , String .class );
116
+ Boolean hasAttribute = (Boolean ) ReflectionUtils .invokeMethod (method , testContext , STEP_EXECUTION );
117
+
118
+ if (hasAttribute ) {
107
119
StepSynchronizationManager .close ();
108
120
}
109
121
}
110
122
111
123
/*
112
124
* Support for Spring 3.0 (empty).
113
125
*/
114
- @ Override
126
+ @ Override
115
127
public void afterTestClass (TestContext testContext ) throws Exception {
116
128
}
117
129
118
130
/*
119
131
* Support for Spring 3.0 (empty).
120
132
*/
121
- @ Override
133
+ @ Override
122
134
public void beforeTestClass (TestContext testContext ) throws Exception {
123
135
}
124
136
@@ -130,8 +142,14 @@ public void beforeTestClass(TestContext testContext) throws Exception {
130
142
* @return a {@link StepExecution}
131
143
*/
132
144
protected StepExecution getStepExecution (TestContext testContext ) {
145
+ Object target ;
133
146
134
- Object target = testContext .getTestInstance ();
147
+ try {
148
+ Method method = TestContext .class .getMethod (GET_TEST_INSTANCE_METHOD );
149
+ target = ReflectionUtils .invokeMethod (method , testContext );
150
+ } catch (NoSuchMethodException e ) {
151
+ throw new IllegalStateException ("No such method " + GET_TEST_INSTANCE_METHOD + " on provided TestContext" , e );
152
+ }
135
153
136
154
ExtractorMethodCallback method = new ExtractorMethodCallback (StepExecution .class , "getStepExecution" );
137
155
ReflectionUtils .doWithMethods (target .getClass (), method );
@@ -173,7 +191,7 @@ public String getName() {
173
191
return result == null ? null : result .getName ();
174
192
}
175
193
176
- @ Override
194
+ @ Override
177
195
public void doWith (Method method ) throws IllegalArgumentException , IllegalAccessException {
178
196
Class <?> type = method .getReturnType ();
179
197
if (preferredType .isAssignableFrom (type )) {
@@ -183,5 +201,4 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
183
201
}
184
202
}
185
203
}
186
-
187
204
}
0 commit comments