Skip to content

Commit ec7aefa

Browse files
committed
Improve concurrency in DefaultTestContext regarding attributes
This commit inlines the basic implementation of AttributeAccessorSupport and converts the LinkedHashMap to a ConcurrentHashMap. In addition, attributes are now included in toString(). Issue: SPR-5863
1 parent 5fe3bcf commit ec7aefa

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

spring-test/src/main/java/org/springframework/test/context/support/DefaultTestContext.java

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -17,10 +17,11 @@
1717
package org.springframework.test.context.support;
1818

1919
import java.lang.reflect.Method;
20+
import java.util.Map;
21+
import java.util.concurrent.ConcurrentHashMap;
2022

2123
import org.springframework.context.ApplicationContext;
2224
import org.springframework.context.ConfigurableApplicationContext;
23-
import org.springframework.core.AttributeAccessorSupport;
2425
import org.springframework.core.style.ToStringCreator;
2526
import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
2627
import org.springframework.test.context.CacheAwareContextLoaderDelegate;
@@ -33,23 +34,26 @@
3334
*
3435
* @author Sam Brannen
3536
* @author Juergen Hoeller
37+
* @author Rob Harrop
3638
* @since 4.0
3739
*/
38-
public class DefaultTestContext extends AttributeAccessorSupport implements TestContext {
40+
public class DefaultTestContext implements TestContext {
3941

4042
private static final long serialVersionUID = -5827157174866681233L;
4143

44+
private final Map<String, Object> attributes = new ConcurrentHashMap<>(0);
45+
4246
private final CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate;
4347

4448
private final MergedContextConfiguration mergedContextConfiguration;
4549

4650
private final Class<?> testClass;
4751

48-
private Object testInstance;
52+
private volatile Object testInstance;
4953

50-
private Method testMethod;
54+
private volatile Method testMethod;
5155

52-
private Throwable testException;
56+
private volatile Throwable testException;
5357

5458

5559
/**
@@ -84,7 +88,7 @@ public ApplicationContext getApplicationContext() {
8488
if (context instanceof ConfigurableApplicationContext) {
8589
@SuppressWarnings("resource")
8690
ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context;
87-
Assert.state(cac.isActive(), "The ApplicationContext loaded for [" + mergedContextConfiguration
91+
Assert.state(cac.isActive(), () -> "The ApplicationContext loaded for [" + mergedContextConfiguration
8892
+ "] is not active. Ensure that the context has not been closed programmatically.");
8993
}
9094
return context;
@@ -124,6 +128,40 @@ public void updateState(Object testInstance, Method testMethod, Throwable testEx
124128
this.testException = testException;
125129
}
126130

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+
127165

128166
/**
129167
* Provide a String representation of this test context's state.
@@ -136,6 +174,7 @@ public String toString() {
136174
.append("testMethod", this.testMethod)
137175
.append("testException", this.testException)
138176
.append("mergedContextConfiguration", this.mergedContextConfiguration)
177+
.append("attributes", this.attributes)
139178
.toString();
140179
}
141180

0 commit comments

Comments
 (0)