EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.test]

COVERAGE SUMMARY FOR SOURCE FILE [StepScopeTestExecutionListener.java]

nameclass, %method, %block, %line, %
StepScopeTestExecutionListener.java100% (2/2)82%  (9/11)83%  (116/139)88%  (34.3/39)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class StepScopeTestExecutionListener100% (1/1)75%  (6/8)82%  (80/97)86%  (24/28)
afterTestClass (TestContext): void 0%   (0/1)0%   (0/1)0%   (0/1)
beforeTestClass (TestContext): void 0%   (0/1)0%   (0/1)0%   (0/1)
getStepExecution (TestContext): StepExecution 100% (1/1)71%  (36/51)83%  (10/12)
<static initializer> 100% (1/1)100% (11/11)100% (1/1)
StepScopeTestExecutionListener (): void 100% (1/1)100% (3/3)100% (2/2)
afterTestMethod (TestContext): void 100% (1/1)100% (6/6)100% (3/3)
beforeTestMethod (TestContext): void 100% (1/1)100% (13/13)100% (4/4)
prepareTestInstance (TestContext): void 100% (1/1)100% (11/11)100% (4/4)
     
class StepScopeTestExecutionListener$ExtractorMethodCallback100% (1/1)100% (3/3)86%  (36/42)94%  (10.3/11)
doWith (Method): void 100% (1/1)71%  (15/21)87%  (4.3/5)
StepScopeTestExecutionListener$ExtractorMethodCallback (StepScopeTestExecutio... 100% (1/1)100% (12/12)100% (5/5)
getName (): String 100% (1/1)100% (9/9)100% (1/1)

1/*
2 * Copyright 2006-2010 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.springframework.batch.test;
17 
18import java.lang.reflect.Method;
19 
20import org.springframework.batch.core.StepExecution;
21import org.springframework.batch.core.scope.context.StepContext;
22import org.springframework.batch.core.scope.context.StepSynchronizationManager;
23import org.springframework.batch.item.adapter.HippyMethodInvoker;
24import org.springframework.test.context.TestContext;
25import org.springframework.test.context.TestExecutionListener;
26import org.springframework.util.ReflectionUtils;
27import org.springframework.util.ReflectionUtils.MethodCallback;
28 
29/**
30 * A {@link TestExecutionListener} that sets up step-scope context for
31 * dependency injection into unit tests. A {@link StepContext} will be created
32 * for the duration of a test method and made available to any dependencies that
33 * are injected. The default behaviour is just to create a {@link StepExecution}
34 * with fixed properties. Alternatively it can be provided by the test case as a
35 * factory methods returning the correct type.  Example:
36 * 
37 * <pre>
38 * &#064;ContextConfiguration
39 * &#064;TestExecutionListeners( { DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class })
40 * &#064;RunWith(SpringJUnit4ClassRunner.class)
41 * public class StepScopeTestExecutionListenerIntegrationTests {
42 * 
43 *         // A step-scoped dependency configured in the ApplicationContext
44 *         &#064;Autowired
45 *         private ItemReader&lt;String&gt; reader;
46 * 
47 *  public StepExecution getStepExecution() {
48 *    StepExecution execution = MetaDataInstanceFactory.createStepExecution();
49 *    execution.getExecutionContext().putString("foo", "bar");
50 *    return execution;
51 *  }
52 * 
53 *         &#064;Test
54 *         public void testStepScopedReader() {
55 *                 // Step context is active here so the reader can be used,
56 *      // and the step execution context will contain foo=bar...
57 *                 assertNotNull(reader.read());
58 *         }
59 * 
60 * }
61 * </pre>
62 * 
63 * @author Dave Syer
64 * 
65 */
66public class StepScopeTestExecutionListener implements TestExecutionListener {
67 
68        private static final String STEP_EXECUTION = StepScopeTestExecutionListener.class.getName() + ".STEP_EXECUTION";
69 
70        /**
71         * Set up a {@link StepExecution} as a test context attribute.
72         * 
73         * @param testContext the current test context
74         * @throws Exception if there is a problem
75         * @see TestExecutionListener#prepareTestInstance(TestContext)
76         */
77        public void prepareTestInstance(TestContext testContext) throws Exception {
78                StepExecution stepExecution = getStepExecution(testContext);
79                if (stepExecution != null) {
80                        testContext.setAttribute(STEP_EXECUTION, stepExecution);
81                }
82        }
83 
84        /**
85         * @param testContext the current test context
86         * @throws Exception if there is a problem
87         * @see TestExecutionListener#beforeTestMethod(TestContext)
88         */
89        public void beforeTestMethod(org.springframework.test.context.TestContext testContext) throws Exception {
90                if (testContext.hasAttribute(STEP_EXECUTION)) {
91                        StepExecution stepExecution = (StepExecution) testContext.getAttribute(STEP_EXECUTION);
92                        StepSynchronizationManager.register(stepExecution);
93                }
94 
95        }
96 
97        /**
98         * @param testContext the current test context
99         * @throws Exception if there is a problem
100         * @see TestExecutionListener#afterTestMethod(TestContext)
101         */
102        public void afterTestMethod(TestContext testContext) throws Exception {
103                if (testContext.hasAttribute(STEP_EXECUTION)) {
104                        StepSynchronizationManager.close();
105                }
106        }
107 
108        /*
109         * Support for Spring 3.0 (empty).
110         */
111        public void afterTestClass(TestContext testContext) throws Exception {
112        }
113 
114        /*
115         * Support for Spring 3.0 (empty).
116         */
117        public void beforeTestClass(TestContext testContext) throws Exception {
118        }
119        
120        /**
121         * Discover a {@link StepExecution} as a field in the test case or create
122         * one if none is available.
123         * 
124         * @param testContext the current test context
125         * @return a {@link StepExecution}
126         */
127        protected StepExecution getStepExecution(TestContext testContext) {
128 
129                Object target = testContext.getTestInstance();
130 
131                ExtractorMethodCallback method = new ExtractorMethodCallback(StepExecution.class, "getStepExecution");
132                ReflectionUtils.doWithMethods(target.getClass(), method);
133                if (method.getName() != null) {
134                        HippyMethodInvoker invoker = new HippyMethodInvoker();
135                        invoker.setTargetObject(target);
136                        invoker.setTargetMethod(method.getName());
137                        try {
138                                invoker.prepare();
139                                return (StepExecution) invoker.invoke();
140                        }
141                        catch (Exception e) {
142                                throw new IllegalArgumentException("Could not create step execution from method: " + method.getName(),
143                                                e);
144                        }
145                }
146 
147                return MetaDataInstanceFactory.createStepExecution();
148        }
149 
150        /**
151         * Look for a method returning the type provided, preferring one with the
152         * name provided.
153         */
154        private final class ExtractorMethodCallback implements MethodCallback {
155                private String preferredName;
156 
157                private final Class<?> preferredType;
158 
159                private Method result;
160 
161                public ExtractorMethodCallback(Class<?> preferredType, String preferredName) {
162                        super();
163                        this.preferredType = preferredType;
164                        this.preferredName = preferredName;
165                }
166 
167                public String getName() {
168                        return result == null ? null : result.getName();
169                }
170 
171                public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
172                        Class<?> type = method.getReturnType();
173                        if (preferredType.isAssignableFrom(type)) {
174                                if (result == null || method.getName().equals(preferredName)) {
175                                        result = method;
176                                }
177                        }
178                }
179        }
180 
181}

[all classes][org.springframework.batch.test]
EMMA 2.0.5312 (C) Vladimir Roubtsov