Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3eb7511

Browse files
chrisjsmminella
authored andcommitted
Handle TestContext class to interface conversion from Spring 3 to Spring 4
Use reflection to make calls to the provided TestContext in StepScopeTestExecutionListener Enables usage of Spring 4 w/ Batch 2.2 (in this case)
1 parent 9affdaf commit 3eb7511

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

spring-batch-test/src/main/java/org/springframework/batch/test/StepScopeTestExecutionListener.java

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2010 the original author or authors.
2+
* Copyright 2006-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,11 +61,14 @@
6161
* </pre>
6262
*
6363
* @author Dave Syer
64-
*
64+
* @author Chris Schaefer
6565
*/
6666
public class StepScopeTestExecutionListener implements TestExecutionListener {
67-
6867
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";
6972

7073
/**
7174
* Set up a {@link StepExecution} as a test context attribute.
@@ -74,11 +77,13 @@ public class StepScopeTestExecutionListener implements TestExecutionListener {
7477
* @throws Exception if there is a problem
7578
* @see TestExecutionListener#prepareTestInstance(TestContext)
7679
*/
77-
@Override
80+
@Override
7881
public void prepareTestInstance(TestContext testContext) throws Exception {
7982
StepExecution stepExecution = getStepExecution(testContext);
83+
8084
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);
8287
}
8388
}
8489

@@ -87,38 +92,45 @@ public void prepareTestInstance(TestContext testContext) throws Exception {
8792
* @throws Exception if there is a problem
8893
* @see TestExecutionListener#beforeTestMethod(TestContext)
8994
*/
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+
94104
StepSynchronizationManager.register(stepExecution);
95105
}
96-
97106
}
98107

99108
/**
100109
* @param testContext the current test context
101110
* @throws Exception if there is a problem
102111
* @see TestExecutionListener#afterTestMethod(TestContext)
103112
*/
104-
@Override
113+
@Override
105114
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) {
107119
StepSynchronizationManager.close();
108120
}
109121
}
110122

111123
/*
112124
* Support for Spring 3.0 (empty).
113125
*/
114-
@Override
126+
@Override
115127
public void afterTestClass(TestContext testContext) throws Exception {
116128
}
117129

118130
/*
119131
* Support for Spring 3.0 (empty).
120132
*/
121-
@Override
133+
@Override
122134
public void beforeTestClass(TestContext testContext) throws Exception {
123135
}
124136

@@ -130,8 +142,14 @@ public void beforeTestClass(TestContext testContext) throws Exception {
130142
* @return a {@link StepExecution}
131143
*/
132144
protected StepExecution getStepExecution(TestContext testContext) {
145+
Object target;
133146

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+
}
135153

136154
ExtractorMethodCallback method = new ExtractorMethodCallback(StepExecution.class, "getStepExecution");
137155
ReflectionUtils.doWithMethods(target.getClass(), method);
@@ -173,7 +191,7 @@ public String getName() {
173191
return result == null ? null : result.getName();
174192
}
175193

176-
@Override
194+
@Override
177195
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
178196
Class<?> type = method.getReturnType();
179197
if (preferredType.isAssignableFrom(type)) {
@@ -183,5 +201,4 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
183201
}
184202
}
185203
}
186-
187204
}

0 commit comments

Comments
 (0)