1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 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.
27
27
import org .springframework .test .annotation .DirtiesContext ;
28
28
import org .springframework .test .annotation .DirtiesContext .ClassMode ;
29
29
import org .springframework .test .annotation .DirtiesContext .HierarchyMode ;
30
+ import org .springframework .test .annotation .DirtiesContext .MethodMode ;
30
31
import org .springframework .test .context .TestContext ;
31
32
import org .springframework .util .Assert ;
32
33
33
34
import static org .springframework .test .annotation .DirtiesContext .ClassMode .*;
35
+ import static org .springframework .test .annotation .DirtiesContext .MethodMode .*;
34
36
35
37
/**
36
38
* {@code TestExecutionListener} which provides support for marking the
37
39
* {@code ApplicationContext} associated with a test as <em>dirty</em> for
38
- * both test classes and test methods configured with the {@link DirtiesContext
39
- * @ DirtiesContext} annotation.
40
+ * both test classes and test methods annotated with the
41
+ * {@link DirtiesContext @ DirtiesContext} annotation.
40
42
*
41
43
* @author Sam Brannen
42
44
* @author Juergen Hoeller
@@ -56,20 +58,98 @@ public final int getOrder() {
56
58
return 3000 ;
57
59
}
58
60
61
+ /**
62
+ * If the test class of the supplied {@linkplain TestContext test context}
63
+ * is annotated with {@code @DirtiesContext} and the {@linkplain
64
+ * DirtiesContext#classMode() class mode} is set to {@link
65
+ * ClassMode#BEFORE_CLASS BEFORE_CLASS}, the {@linkplain ApplicationContext
66
+ * application context} of the test context will be
67
+ * {@linkplain TestContext#markApplicationContextDirty marked as dirty}, and the
68
+ * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE
69
+ * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context will be set to
70
+ * {@code true}.
71
+ */
72
+ @ Override
73
+ public void beforeTestClass (TestContext testContext ) throws Exception {
74
+ beforeOrAfterTestClass (testContext , "Before" , BEFORE_CLASS );
75
+ }
76
+
59
77
/**
60
78
* If the current test method of the supplied {@linkplain TestContext test
61
- * context} is annotated with {@link DirtiesContext @DirtiesContext},
62
- * or if the test class is annotated with {@link DirtiesContext
63
- * @DirtiesContext} and the {@linkplain DirtiesContext#classMode() class
64
- * mode} is set to {@link ClassMode#AFTER_EACH_TEST_METHOD
65
- * AFTER_EACH_TEST_METHOD}, the {@linkplain ApplicationContext application
66
- * context} of the test context will be
67
- * {@linkplain TestContext#markApplicationContextDirty marked as dirty} and the
68
- * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE}
69
- * in the test context will be set to {@code true}.
79
+ * context} is annotated with {@code @DirtiesContext} and the {@linkplain
80
+ * DirtiesContext#methodMode() method mode} is set to {@link
81
+ * MethodMode#BEFORE_METHOD BEFORE_METHOD}, or if the test class is
82
+ * annotated with {@code @DirtiesContext} and the {@linkplain
83
+ * DirtiesContext#classMode() class mode} is set to {@link
84
+ * ClassMode#BEFORE_EACH_TEST_METHOD BEFORE_EACH_TEST_METHOD}, the
85
+ * {@linkplain ApplicationContext application context} of the test context
86
+ * will be {@linkplain TestContext#markApplicationContextDirty marked as dirty} and the
87
+ * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE
88
+ * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context will be set to {@code true}.
89
+ * @since 4.2
90
+ */
91
+ @ Override
92
+ public void beforeTestMethod (TestContext testContext ) throws Exception {
93
+ beforeOrAfterTestMethod (testContext , "Before" , BEFORE_METHOD , BEFORE_EACH_TEST_METHOD );
94
+ }
95
+
96
+ /**
97
+ * If the current test method of the supplied {@linkplain TestContext test
98
+ * context} is annotated with {@code @DirtiesContext} and the {@linkplain
99
+ * DirtiesContext#methodMode() method mode} is set to {@link
100
+ * MethodMode#AFTER_METHOD AFTER_METHOD}, or if the test class is
101
+ * annotated with {@code @DirtiesContext} and the {@linkplain
102
+ * DirtiesContext#classMode() class mode} is set to {@link
103
+ * ClassMode#AFTER_EACH_TEST_METHOD AFTER_EACH_TEST_METHOD}, the
104
+ * {@linkplain ApplicationContext application context} of the test context
105
+ * will be {@linkplain TestContext#markApplicationContextDirty marked as dirty} and the
106
+ * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE
107
+ * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context will be set to {@code true}.
70
108
*/
71
109
@ Override
72
110
public void afterTestMethod (TestContext testContext ) throws Exception {
111
+ beforeOrAfterTestMethod (testContext , "After" , AFTER_METHOD , AFTER_EACH_TEST_METHOD );
112
+ }
113
+
114
+ /**
115
+ * If the test class of the supplied {@linkplain TestContext test context}
116
+ * is annotated with {@code @DirtiesContext} and the {@linkplain
117
+ * DirtiesContext#classMode() class mode} is set to {@link
118
+ * ClassMode#AFTER_CLASS AFTER_CLASS}, the {@linkplain ApplicationContext
119
+ * application context} of the test context will be
120
+ * {@linkplain TestContext#markApplicationContextDirty marked as dirty}, and the
121
+ * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE
122
+ * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context will be set to
123
+ * {@code true}.
124
+ */
125
+ @ Override
126
+ public void afterTestClass (TestContext testContext ) throws Exception {
127
+ beforeOrAfterTestClass (testContext , "After" , AFTER_CLASS );
128
+ }
129
+
130
+ /**
131
+ * Marks the {@linkplain ApplicationContext application context} of the supplied
132
+ * {@linkplain TestContext test context} as
133
+ * {@linkplain TestContext#markApplicationContextDirty(DirtiesContext.HierarchyMode) dirty}
134
+ * and sets {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE
135
+ * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context to {@code true}.
136
+ * @param testContext the test context whose application context should
137
+ * marked as dirty
138
+ * @param hierarchyMode the context cache clearing mode to be applied if the
139
+ * context is part of a hierarchy; may be {@code null}
140
+ * @since 3.2.2
141
+ */
142
+ protected void dirtyContext (TestContext testContext , HierarchyMode hierarchyMode ) {
143
+ testContext .markApplicationContextDirty (hierarchyMode );
144
+ testContext .setAttribute (DependencyInjectionTestExecutionListener .REINJECT_DEPENDENCIES_ATTRIBUTE , Boolean .TRUE );
145
+ }
146
+
147
+ /**
148
+ * Perform the actual work for {@link #beforeTestMethod} and {@link #afterTestMethod}.
149
+ * @since 4.2
150
+ */
151
+ private void beforeOrAfterTestMethod (TestContext testContext , String phase , MethodMode requiredMethodMode ,
152
+ ClassMode requiredClassMode ) throws Exception {
73
153
Class <?> testClass = testContext .getTestClass ();
74
154
Assert .notNull (testClass , "The test class of the supplied TestContext must not be null" );
75
155
Method testMethod = testContext .getTestMethod ();
@@ -78,67 +158,48 @@ public void afterTestMethod(TestContext testContext) throws Exception {
78
158
final String annotationType = DirtiesContext .class .getName ();
79
159
AnnotationAttributes methodAnnAttrs = AnnotatedElementUtils .getAnnotationAttributes (testMethod , annotationType );
80
160
AnnotationAttributes classAnnAttrs = AnnotatedElementUtils .getAnnotationAttributes (testClass , annotationType );
81
- boolean methodDirtiesContext = methodAnnAttrs != null ;
82
- boolean classDirtiesContext = classAnnAttrs != null ;
83
- ClassMode classMode = classDirtiesContext ? classAnnAttrs .<ClassMode > getEnum ("classMode" ) : null ;
161
+ boolean methodAnnotated = methodAnnAttrs != null ;
162
+ boolean classAnnotated = classAnnAttrs != null ;
163
+ MethodMode methodMode = methodAnnotated ? methodAnnAttrs .<MethodMode > getEnum ("methodMode" ) : null ;
164
+ ClassMode classMode = classAnnotated ? classAnnAttrs .<ClassMode > getEnum ("classMode" ) : null ;
84
165
85
166
if (logger .isDebugEnabled ()) {
86
167
logger .debug (String .format (
87
- "After test method: context %s, class dirties context [%s], class mode [%s], method dirties context [%s]." ,
88
- testContext , classDirtiesContext , classMode , methodDirtiesContext ));
168
+ "%s test method: context %s, class annotated with @DirtiesContext [%s] with mode [%s], method annotated with @DirtiesContext [%s] with mode [%s]." ,
169
+ phase , testContext , classAnnotated , classMode , methodAnnotated , methodMode ));
89
170
}
90
171
91
- if (methodDirtiesContext || (classMode == AFTER_EACH_TEST_METHOD )) {
92
- HierarchyMode hierarchyMode = methodDirtiesContext ? methodAnnAttrs .<HierarchyMode > getEnum ("hierarchyMode" )
172
+ if (( methodMode == requiredMethodMode ) || (classMode == requiredClassMode )) {
173
+ HierarchyMode hierarchyMode = methodAnnotated ? methodAnnAttrs .<HierarchyMode > getEnum ("hierarchyMode" )
93
174
: classAnnAttrs .<HierarchyMode > getEnum ("hierarchyMode" );
94
175
dirtyContext (testContext , hierarchyMode );
95
176
}
96
177
}
97
178
98
179
/**
99
- * If the test class of the supplied {@linkplain TestContext test context} is
100
- * annotated with {@link DirtiesContext @DirtiesContext}, the
101
- * {@linkplain ApplicationContext application context} of the test context will
102
- * be {@linkplain TestContext#markApplicationContextDirty marked as dirty},
103
- * and the
104
- * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE
105
- * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context will be set to
106
- * {@code true}.
180
+ * Perform the actual work for {@link #beforeTestClass} and {@link #afterTestClass}.
181
+ * @since 4.2
107
182
*/
108
- @ Override
109
- public void afterTestClass ( TestContext testContext ) throws Exception {
183
+ private void beforeOrAfterTestClass ( TestContext testContext , String phase , ClassMode requiredClassMode )
184
+ throws Exception {
110
185
Class <?> testClass = testContext .getTestClass ();
111
186
Assert .notNull (testClass , "The test class of the supplied TestContext must not be null" );
112
187
113
188
final String annotationType = DirtiesContext .class .getName ();
114
- AnnotationAttributes annAttrs = AnnotatedElementUtils .getAnnotationAttributes (testClass , annotationType );
115
- boolean dirtiesContext = annAttrs != null ;
189
+ AnnotationAttributes classAnnAttrs = AnnotatedElementUtils .getAnnotationAttributes (testClass , annotationType );
190
+ boolean classAnnotated = classAnnAttrs != null ;
191
+ ClassMode classMode = classAnnotated ? classAnnAttrs .<ClassMode > getEnum ("classMode" ) : null ;
116
192
117
193
if (logger .isDebugEnabled ()) {
118
- logger .debug (String .format ("After test class: context %s, dirtiesContext [%s]." , testContext ,
119
- dirtiesContext ));
194
+ logger .debug (String .format (
195
+ "%s test class: context %s, class annotated with @DirtiesContext [%s] with mode [%s]." , phase ,
196
+ testContext , classAnnotated , classMode ));
120
197
}
121
- if (dirtiesContext ) {
122
- HierarchyMode hierarchyMode = annAttrs .<HierarchyMode > getEnum ("hierarchyMode" );
198
+
199
+ if (classMode == requiredClassMode ) {
200
+ HierarchyMode hierarchyMode = classAnnAttrs .<HierarchyMode > getEnum ("hierarchyMode" );
123
201
dirtyContext (testContext , hierarchyMode );
124
202
}
125
203
}
126
204
127
- /**
128
- * Marks the {@linkplain ApplicationContext application context} of the supplied
129
- * {@linkplain TestContext test context} as
130
- * {@linkplain TestContext#markApplicationContextDirty(DirtiesContext.HierarchyMode) dirty}
131
- * and sets {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE}
132
- * in the test context to {@code true}.
133
- * @param testContext the test context whose application context should
134
- * marked as dirty
135
- * @param hierarchyMode the context cache clearing mode to be applied if the
136
- * context is part of a hierarchy; may be {@code null}
137
- * @since 3.2.2
138
- */
139
- protected void dirtyContext (TestContext testContext , HierarchyMode hierarchyMode ) {
140
- testContext .markApplicationContextDirty (hierarchyMode );
141
- testContext .setAttribute (DependencyInjectionTestExecutionListener .REINJECT_DEPENDENCIES_ATTRIBUTE , Boolean .TRUE );
142
- }
143
-
144
205
}
0 commit comments