Skip to content

Commit d2d528d

Browse files
committed
Polishing
1 parent cfdb683 commit d2d528d

File tree

7 files changed

+172
-205
lines changed

7 files changed

+172
-205
lines changed

spring-test/src/main/java/org/springframework/test/context/BootstrapUtils.java

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,51 +43,47 @@
4343
*/
4444
abstract class BootstrapUtils {
4545

46-
private static final String DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME = "org.springframework.test.context.support.DefaultBootstrapContext";
46+
private static final String DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME =
47+
"org.springframework.test.context.support.DefaultBootstrapContext";
4748

48-
private static final String DEFAULT_CACHE_AWARE_CONTEXT_LOADER_DELEGATE_CLASS_NAME = "org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate";
49+
private static final String DEFAULT_CACHE_AWARE_CONTEXT_LOADER_DELEGATE_CLASS_NAME =
50+
"org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate";
4951

50-
private static final String DEFAULT_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME = "org.springframework.test.context.support.DefaultTestContextBootstrapper";
52+
private static final String DEFAULT_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME =
53+
"org.springframework.test.context.support.DefaultTestContextBootstrapper";
5154

52-
private static final String DEFAULT_WEB_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME = "org.springframework.test.context.web.WebTestContextBootstrapper";
55+
private static final String DEFAULT_WEB_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME =
56+
"org.springframework.test.context.web.WebTestContextBootstrapper";
5357

54-
private static final String WEB_APP_CONFIGURATION_ANNOTATION_CLASS_NAME = "org.springframework.test.context.web.WebAppConfiguration";
58+
private static final String WEB_APP_CONFIGURATION_ANNOTATION_CLASS_NAME =
59+
"org.springframework.test.context.web.WebAppConfiguration";
5560

5661
private static final Log logger = LogFactory.getLog(BootstrapUtils.class);
5762

5863

59-
private BootstrapUtils() {
60-
/* no-op */
61-
}
62-
6364
/**
6465
* Create the {@code BootstrapContext} for the specified {@linkplain Class test class}.
65-
*
6666
* <p>Uses reflection to create a {@link org.springframework.test.context.support.DefaultBootstrapContext}
6767
* that uses a {@link org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate}.
68-
*
6968
* @param testClass the test class for which the bootstrap context should be created
7069
* @return a new {@code BootstrapContext}; never {@code null}
7170
*/
7271
@SuppressWarnings("unchecked")
7372
static BootstrapContext createBootstrapContext(Class<?> testClass) {
7473
CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate = createCacheAwareContextLoaderDelegate();
75-
7674
Class<? extends BootstrapContext> clazz = null;
7775
try {
78-
clazz = (Class<? extends BootstrapContext>) ClassUtils.forName(DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME,
79-
BootstrapUtils.class.getClassLoader());
80-
81-
Constructor<? extends BootstrapContext> constructor = clazz.getConstructor(Class.class,
82-
CacheAwareContextLoaderDelegate.class);
83-
76+
clazz = (Class<? extends BootstrapContext>) ClassUtils.forName(
77+
DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME, BootstrapUtils.class.getClassLoader());
78+
Constructor<? extends BootstrapContext> constructor = clazz.getConstructor(
79+
Class.class, CacheAwareContextLoaderDelegate.class);
8480
if (logger.isDebugEnabled()) {
8581
logger.debug(String.format("Instantiating BootstrapContext using constructor [%s]", constructor));
8682
}
8783
return instantiateClass(constructor, testClass, cacheAwareContextLoaderDelegate);
8884
}
89-
catch (Throwable t) {
90-
throw new IllegalStateException("Could not load BootstrapContext [" + clazz + "]", t);
85+
catch (Throwable ex) {
86+
throw new IllegalStateException("Could not load BootstrapContext [" + clazz + "]", ex);
9187
}
9288
}
9389

@@ -104,16 +100,15 @@ private static CacheAwareContextLoaderDelegate createCacheAwareContextLoaderDele
104100
}
105101
return instantiateClass(clazz, CacheAwareContextLoaderDelegate.class);
106102
}
107-
catch (Throwable t) {
108-
throw new IllegalStateException("Could not load CacheAwareContextLoaderDelegate [" + clazz + "]", t);
103+
catch (Throwable ex) {
104+
throw new IllegalStateException("Could not load CacheAwareContextLoaderDelegate [" + clazz + "]", ex);
109105
}
110106
}
111107

112108
/**
113109
* Resolve the {@link TestContextBootstrapper} type for the test class in the
114110
* supplied {@link BootstrapContext}, instantiate it, and provide it a reference
115111
* to the {@link BootstrapContext}.
116-
*
117112
* <p>If the {@link BootstrapWith @BootstrapWith} annotation is present on
118113
* the test class, either directly or as a meta-annotation, then its
119114
* {@link BootstrapWith#value value} will be used as the bootstrapper type.
@@ -123,7 +118,6 @@ private static CacheAwareContextLoaderDelegate createCacheAwareContextLoaderDele
123118
* {@link org.springframework.test.context.web.WebTestContextBootstrapper
124119
* WebTestContextBootstrapper} will be used, depending on the presence of
125120
* {@link org.springframework.test.context.web.WebAppConfiguration @WebAppConfiguration}.
126-
*
127121
* @param bootstrapContext the bootstrap context to use
128122
* @return a fully configured {@code TestContextBootstrapper}
129123
*/
@@ -138,36 +132,36 @@ static TestContextBootstrapper resolveTestContextBootstrapper(BootstrapContext b
138132
}
139133
if (logger.isDebugEnabled()) {
140134
logger.debug(String.format("Instantiating TestContextBootstrapper for test class [%s] from class [%s]",
141-
testClass.getName(), clazz.getName()));
135+
testClass.getName(), clazz.getName()));
142136
}
143-
144137
TestContextBootstrapper testContextBootstrapper = instantiateClass(clazz, TestContextBootstrapper.class);
145138
testContextBootstrapper.setBootstrapContext(bootstrapContext);
146-
147139
return testContextBootstrapper;
148140
}
149141
catch (Throwable ex) {
150142
if (ex instanceof IllegalStateException) {
151143
throw (IllegalStateException) ex;
152144
}
153-
throw new IllegalStateException("Could not load TestContextBootstrapper [" + clazz
154-
+ "]. Specify @BootstrapWith's 'value' attribute "
155-
+ "or make the default bootstrapper class available.", ex);
145+
throw new IllegalStateException("Could not load TestContextBootstrapper [" + clazz +
146+
"]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available.",
147+
ex);
156148
}
157149
}
158150

159151
/**
160152
* @since 4.3
161153
*/
162154
private static Class<?> resolveExplicitTestContextBootstrapper(Class<?> testClass) {
163-
MultiValueMap<String, Object> attributesMultiMap = AnnotatedElementUtils.getAllAnnotationAttributes(
164-
testClass, BootstrapWith.class.getName());
165-
List<Object> values = (attributesMultiMap == null ? null : attributesMultiMap.get(AnnotationUtils.VALUE));
155+
MultiValueMap<String, Object> attributesMultiMap =
156+
AnnotatedElementUtils.getAllAnnotationAttributes(testClass, BootstrapWith.class.getName());
157+
List<Object> values = (attributesMultiMap != null ? attributesMultiMap.get(AnnotationUtils.VALUE) : null);
166158
if (values == null) {
167159
return null;
168160
}
169-
Assert.state(values.size() == 1, String.format("Configuration error: found multiple declarations of "
170-
+ "@BootstrapWith on test class [%s] with values %s", testClass.getName(), values));
161+
if (values.size() != 1) {
162+
throw new IllegalStateException(String.format("Configuration error: found multiple declarations of " +
163+
"@BootstrapWith on test class [%s] with values %s", testClass.getName(), values));
164+
}
171165
return (Class<?>) values.get(0);
172166
}
173167

spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 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.
@@ -20,14 +20,17 @@
2020
* {@code TestExecutionListener} defines a <em>listener</em> API for reacting to
2121
* test execution events published by the {@link TestContextManager} with which
2222
* the listener is registered.
23+
*
2324
* <p>Concrete implementations must provide a {@code public} no-args constructor,
2425
* so that listeners can be instantiated transparently by tools and configuration
2526
* mechanisms.
27+
*
2628
* <p>Implementations may optionally declare the position in which they should
2729
* be ordered among the chain of default listeners via the
2830
* {@link org.springframework.core.Ordered Ordered} interface or
2931
* {@link org.springframework.core.annotation.Order @Order} annotation. See
3032
* {@link TestContextBootstrapper#getTestExecutionListeners()} for details.
33+
*
3134
* <p>Spring provides the following out-of-the-box implementations (all of
3235
* which implement {@code Ordered}):
3336
* <ul>
@@ -58,7 +61,6 @@ public interface TestExecutionListener {
5861
* <em>before class</em> lifecycle callbacks.
5962
* <p>If a given testing framework does not support <em>before class</em>
6063
* lifecycle callbacks, this method will not be called for that framework.
61-
*
6264
* @param testContext the test context for the test; never {@code null}
6365
* @throws Exception allows any exception to propagate
6466
*/
@@ -69,7 +71,6 @@ public interface TestExecutionListener {
6971
* {@link TestContext test context}, for example by injecting dependencies.
7072
* <p>This method should be called immediately after instantiation of the test
7173
* instance but prior to any framework-specific lifecycle callbacks.
72-
*
7374
* @param testContext the test context for the test; never {@code null}
7475
* @throws Exception allows any exception to propagate
7576
*/
@@ -82,7 +83,6 @@ public interface TestExecutionListener {
8283
* fixtures.
8384
* <p>This method should be called immediately prior to framework-specific
8485
* <em>before</em> lifecycle callbacks.
85-
*
8686
* @param testContext the test context in which the test method will be
8787
* executed; never {@code null}
8888
* @throws Exception allows any exception to propagate
@@ -96,7 +96,6 @@ public interface TestExecutionListener {
9696
* fixtures.
9797
* <p>This method should be called immediately after framework-specific
9898
* <em>after</em> lifecycle callbacks.
99-
*
10099
* @param testContext the test context in which the test method was
101100
* executed; never {@code null}
102101
* @throws Exception allows any exception to propagate
@@ -110,7 +109,6 @@ public interface TestExecutionListener {
110109
* <em>after class</em> lifecycle callbacks.
111110
* <p>If a given testing framework does not support <em>after class</em>
112111
* lifecycle callbacks, this method will not be called for that framework.
113-
*
114112
* @param testContext the test context for the test; never {@code null}
115113
* @throws Exception allows any exception to propagate
116114
*/

spring-test/src/main/java/org/springframework/test/context/TestExecutionListeners.java

Lines changed: 40 additions & 46 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.
@@ -30,8 +30,8 @@
3030
* which {@link TestExecutionListener TestExecutionListeners} should be
3131
* registered with a {@link TestContextManager}.
3232
*
33-
* <p>Typically, {@code @TestExecutionListeners} will be used in conjunction with
34-
* {@link ContextConfiguration @ContextConfiguration}.
33+
* <p>Typically, {@code @TestExecutionListeners} will be used in conjunction
34+
* with {@link ContextConfiguration @ContextConfiguration}.
3535
*
3636
* <p>As of Spring Framework 4.0, this annotation may be used as a
3737
* <em>meta-annotation</em> to create custom <em>composed annotations</em>.
@@ -48,42 +48,8 @@
4848
@Target(ElementType.TYPE)
4949
public @interface TestExecutionListeners {
5050

51-
/**
52-
* Enumeration of <em>modes</em> that dictate whether or not explicitly
53-
* declared listeners are merged with the default listeners when
54-
* {@code @TestExecutionListeners} is declared on a class that does
55-
* <strong>not</strong> inherit listeners from a superclass.
56-
* @since 4.1
57-
*/
58-
static enum MergeMode {
59-
60-
/**
61-
* Indicates that locally declared listeners should replace the default
62-
* listeners.
63-
*/
64-
REPLACE_DEFAULTS,
65-
66-
/**
67-
* Indicates that locally declared listeners should be merged with the
68-
* default listeners.
69-
* <p>The merging algorithm ensures that duplicates are removed from
70-
* the list and that the resulting set of merged listeners is sorted
71-
* according to the semantics of
72-
* {@link org.springframework.core.annotation.AnnotationAwareOrderComparator
73-
* AnnotationAwareOrderComparator}. If a listener implements
74-
* {@link org.springframework.core.Ordered Ordered} or is annotated
75-
* with {@link org.springframework.core.annotation.Order @Order} it can
76-
* influence the position in which it is merged with the defaults; otherwise,
77-
* locally declared listeners will simply be appended to the list of default
78-
* listeners when merged.
79-
*/
80-
MERGE_WITH_DEFAULTS
81-
}
82-
83-
8451
/**
8552
* Alias for {@link #listeners}.
86-
*
8753
* <p>This attribute may <strong>not</strong> be used in conjunction with
8854
* {@link #listeners}, but it may be used instead of {@link #listeners}.
8955
*/
@@ -93,10 +59,8 @@ static enum MergeMode {
9359
/**
9460
* The {@link TestExecutionListener TestExecutionListeners} to register with
9561
* the {@link TestContextManager}.
96-
*
9762
* <p>This attribute may <strong>not</strong> be used in conjunction with
9863
* {@link #value}, but it may be used instead of {@link #value}.
99-
*
10064
* @see org.springframework.test.context.web.ServletTestExecutionListener
10165
* @see org.springframework.test.context.support.DependencyInjectionTestExecutionListener
10266
* @see org.springframework.test.context.support.DirtiesContextTestExecutionListener
@@ -109,7 +73,6 @@ static enum MergeMode {
10973
/**
11074
* Whether or not {@link #listeners TestExecutionListeners} from superclasses
11175
* should be <em>inherited</em>.
112-
*
11376
* <p>The default value is {@code true}, which means that an annotated
11477
* class will <em>inherit</em> the listeners defined by an annotated
11578
* superclass. Specifically, the listeners for an annotated class will be
@@ -122,21 +85,19 @@ static enum MergeMode {
12285
* {@code DependencyInjectionTestExecutionListener},
12386
* {@code DirtiesContextTestExecutionListener}, <strong>and</strong>
12487
* {@code TransactionalTestExecutionListener}, in that order.
125-
*
12688
* <pre class="code">
12789
* &#064;TestExecutionListeners({
128-
* DependencyInjectionTestExecutionListener.class,
129-
* DirtiesContextTestExecutionListener.class
90+
* DependencyInjectionTestExecutionListener.class,
91+
* DirtiesContextTestExecutionListener.class
13092
* })
13193
* public abstract class AbstractBaseTest {
132-
* // ...
94+
* // ...
13395
* }
13496
*
13597
* &#064;TestExecutionListeners(TransactionalTestExecutionListener.class)
13698
* public class TransactionalTest extends AbstractBaseTest {
137-
* // ...
99+
* // ...
138100
* }</pre>
139-
*
140101
* <p>If {@code inheritListeners} is set to {@code false}, the listeners for
141102
* the annotated class will <em>shadow</em> and effectively replace any
142103
* listeners defined by a superclass.
@@ -158,4 +119,37 @@ static enum MergeMode {
158119
*/
159120
MergeMode mergeMode() default MergeMode.REPLACE_DEFAULTS;
160121

122+
123+
/**
124+
* Enumeration of <em>modes</em> that dictate whether or not explicitly
125+
* declared listeners are merged with the default listeners when
126+
* {@code @TestExecutionListeners} is declared on a class that does
127+
* <strong>not</strong> inherit listeners from a superclass.
128+
* @since 4.1
129+
*/
130+
enum MergeMode {
131+
132+
/**
133+
* Indicates that locally declared listeners should replace the default
134+
* listeners.
135+
*/
136+
REPLACE_DEFAULTS,
137+
138+
/**
139+
* Indicates that locally declared listeners should be merged with the
140+
* default listeners.
141+
* <p>The merging algorithm ensures that duplicates are removed from
142+
* the list and that the resulting set of merged listeners is sorted
143+
* according to the semantics of
144+
* {@link org.springframework.core.annotation.AnnotationAwareOrderComparator
145+
* AnnotationAwareOrderComparator}. If a listener implements
146+
* {@link org.springframework.core.Ordered Ordered} or is annotated
147+
* with {@link org.springframework.core.annotation.Order @Order} it can
148+
* influence the position in which it is merged with the defaults; otherwise,
149+
* locally declared listeners will simply be appended to the list of default
150+
* listeners when merged.
151+
*/
152+
MERGE_WITH_DEFAULTS
153+
}
154+
161155
}

0 commit comments

Comments
 (0)