@@ -41,7 +41,7 @@ public class DefaultTestContext implements TestContext {
41
41
42
42
private static final long serialVersionUID = -5827157174866681233L ;
43
43
44
- private final Map <String , Object > attributes = new ConcurrentHashMap <>(0 );
44
+ private final Map <String , Object > attributes = new ConcurrentHashMap <>(4 );
45
45
46
46
private final CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate ;
47
47
@@ -58,15 +58,17 @@ public class DefaultTestContext implements TestContext {
58
58
59
59
/**
60
60
* <em>Copy constructor</em> for creating a new {@code DefaultTestContext}
61
- * based on the immutable state and <em>attributes</em> of the supplied context.
62
- *
63
- * <p><em>Immutable state</em> includes all arguments supplied to
64
- * {@link #DefaultTestContext(Class, MergedContextConfiguration, CacheAwareContextLoaderDelegate)}.
61
+ * based on the <em>attributes</em> and immutable state of the supplied context.
62
+ * <p><em>Immutable state</em> includes all arguments supplied to the
63
+ * {@linkplain #DefaultTestContext(Class, MergedContextConfiguration,
64
+ * CacheAwareContextLoaderDelegate) standard constructor}.
65
+ * @throws NullPointerException if the supplied {@code DefaultTestContext}
66
+ * is {@code null}
65
67
*/
66
68
public DefaultTestContext (DefaultTestContext testContext ) {
67
69
this (testContext .testClass , testContext .mergedContextConfiguration ,
68
70
testContext .cacheAwareContextLoaderDelegate );
69
- testContext .attributes .forEach ( this .attributes :: put );
71
+ this .attributes .putAll ( testContext .attributes );
70
72
}
71
73
72
74
/**
@@ -144,11 +146,13 @@ public void updateState(Object testInstance, Method testMethod, Throwable testEx
144
146
@ Override
145
147
public void setAttribute (String name , Object value ) {
146
148
Assert .notNull (name , "Name must not be null" );
147
- if (value != null ) {
148
- this .attributes .put (name , value );
149
- }
150
- else {
151
- removeAttribute (name );
149
+ synchronized (this .attributes ) {
150
+ if (value != null ) {
151
+ this .attributes .put (name , value );
152
+ }
153
+ else {
154
+ this .attributes .remove (name );
155
+ }
152
156
}
153
157
}
154
158
@@ -172,7 +176,9 @@ public boolean hasAttribute(String name) {
172
176
173
177
@ Override
174
178
public String [] attributeNames () {
175
- return this .attributes .keySet ().stream ().toArray (String []::new );
179
+ synchronized (this .attributes ) {
180
+ return this .attributes .keySet ().stream ().toArray (String []::new );
181
+ }
176
182
}
177
183
178
184
0 commit comments