1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 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.
17
17
package org .springframework .test .context .support ;
18
18
19
19
import java .lang .reflect .Method ;
20
+ import java .util .Map ;
21
+ import java .util .concurrent .ConcurrentHashMap ;
20
22
21
23
import org .springframework .context .ApplicationContext ;
22
24
import org .springframework .context .ConfigurableApplicationContext ;
23
- import org .springframework .core .AttributeAccessorSupport ;
24
25
import org .springframework .core .style .ToStringCreator ;
25
26
import org .springframework .test .annotation .DirtiesContext .HierarchyMode ;
26
27
import org .springframework .test .context .CacheAwareContextLoaderDelegate ;
33
34
*
34
35
* @author Sam Brannen
35
36
* @author Juergen Hoeller
37
+ * @author Rob Harrop
36
38
* @since 4.0
37
39
*/
38
- public class DefaultTestContext extends AttributeAccessorSupport implements TestContext {
40
+ public class DefaultTestContext implements TestContext {
39
41
40
42
private static final long serialVersionUID = -5827157174866681233L ;
41
43
44
+ private final Map <String , Object > attributes = new ConcurrentHashMap <>(0 );
45
+
42
46
private final CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate ;
43
47
44
48
private final MergedContextConfiguration mergedContextConfiguration ;
45
49
46
50
private final Class <?> testClass ;
47
51
48
- private Object testInstance ;
52
+ private volatile Object testInstance ;
49
53
50
- private Method testMethod ;
54
+ private volatile Method testMethod ;
51
55
52
- private Throwable testException ;
56
+ private volatile Throwable testException ;
53
57
54
58
55
59
/**
@@ -84,7 +88,7 @@ public ApplicationContext getApplicationContext() {
84
88
if (context instanceof ConfigurableApplicationContext ) {
85
89
@ SuppressWarnings ("resource" )
86
90
ConfigurableApplicationContext cac = (ConfigurableApplicationContext ) context ;
87
- Assert .state (cac .isActive (), "The ApplicationContext loaded for [" + mergedContextConfiguration
91
+ Assert .state (cac .isActive (), () -> "The ApplicationContext loaded for [" + mergedContextConfiguration
88
92
+ "] is not active. Ensure that the context has not been closed programmatically." );
89
93
}
90
94
return context ;
@@ -124,6 +128,40 @@ public void updateState(Object testInstance, Method testMethod, Throwable testEx
124
128
this .testException = testException ;
125
129
}
126
130
131
+ @ Override
132
+ public void setAttribute (String name , Object value ) {
133
+ Assert .notNull (name , "Name must not be null" );
134
+ if (value != null ) {
135
+ this .attributes .put (name , value );
136
+ }
137
+ else {
138
+ removeAttribute (name );
139
+ }
140
+ }
141
+
142
+ @ Override
143
+ public Object getAttribute (String name ) {
144
+ Assert .notNull (name , "Name must not be null" );
145
+ return this .attributes .get (name );
146
+ }
147
+
148
+ @ Override
149
+ public Object removeAttribute (String name ) {
150
+ Assert .notNull (name , "Name must not be null" );
151
+ return this .attributes .remove (name );
152
+ }
153
+
154
+ @ Override
155
+ public boolean hasAttribute (String name ) {
156
+ Assert .notNull (name , "Name must not be null" );
157
+ return this .attributes .containsKey (name );
158
+ }
159
+
160
+ @ Override
161
+ public String [] attributeNames () {
162
+ return this .attributes .keySet ().stream ().toArray (String []::new );
163
+ }
164
+
127
165
128
166
/**
129
167
* Provide a String representation of this test context's state.
@@ -136,6 +174,7 @@ public String toString() {
136
174
.append ("testMethod" , this .testMethod )
137
175
.append ("testException" , this .testException )
138
176
.append ("mergedContextConfiguration" , this .mergedContextConfiguration )
177
+ .append ("attributes" , this .attributes )
139
178
.toString ();
140
179
}
141
180
0 commit comments