Skip to content

Commit e91b4d0

Browse files
wilkinsonasnicoll
authored andcommitted
Polish ContextLoader
1 parent eb17aa0 commit e91b4d0

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextLoader.java

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
import static org.assertj.core.api.Assertions.assertThat;
3838

3939
/**
40-
* Manage the lifecycle of an {@link ApplicationContext}. Such helper is best used as
41-
* a field of a test class, describing the shared configuration required for the test:
40+
* Manage the lifecycle of an {@link ApplicationContext}. Such helper is best used as a
41+
* field of a test class, describing the shared configuration required for the test:
4242
*
4343
* <pre class="code">
4444
* public class FooAutoConfigurationTests {
@@ -48,12 +48,14 @@
4848
*
4949
* }</pre>
5050
*
51-
* <p>The initialization above makes sure to register {@code FooAutoConfiguration} for all
51+
* <p>
52+
* The initialization above makes sure to register {@code FooAutoConfiguration} for all
5253
* tests and set the {@code spring.foo} property to {@code bar} unless specified
5354
* otherwise.
5455
*
55-
* <p>Based on the configuration above, a specific test can simulate what would happen
56-
* if the user customizes a property and/or provides its own configuration:
56+
* <p>
57+
* Based on the configuration above, a specific test can simulate what would happen if the
58+
* user customizes a property and/or provides its own configuration:
5759
*
5860
* <pre class="code">
5961
* public class FooAutoConfigurationTests {
@@ -68,13 +70,15 @@
6870
*
6971
* }</pre>
7072
*
71-
* <p>The test above includes an extra {@code UserConfig} class that is guaranteed to
72-
* be processed <strong>before</strong> any auto-configuration. Also, {@code spring.foo}
73-
* has been overwritten to {@code biz}. The {@link #load(ContextConsumer) load} method
74-
* takes a consumer that can use the context to assert its state. Upon completion, the
75-
* context is automatically closed.
73+
* <p>
74+
* The test above includes an extra {@code UserConfig} class that is guaranteed to be
75+
* processed <strong>before</strong> any auto-configuration. Also, {@code spring.foo} has
76+
* been overwritten to {@code biz}. The {@link #load(ContextConsumer) load} method takes a
77+
* consumer that can use the context to assert its state. Upon completion, the context is
78+
* automatically closed.
7679
*
77-
* <p>If a failure scenario has to be tested, {@link #loadAndFail(Consumer)} can be used
80+
* <p>
81+
* If a failure scenario has to be tested, {@link #loadAndFail(Consumer)} can be used
7882
* instead: it expects the startup of the context to fail and call the {@link Consumer}
7983
* with the exception for further assertions.
8084
*
@@ -95,10 +99,9 @@ public class ContextLoader {
9599
private ClassLoader classLoader;
96100

97101
/**
98-
* Set the specified system property prior to loading the context and restore
99-
* its previous value once the consumer has been invoked and the context closed. If
100-
* the {@code value} is {@code null} this removes any prior customization for that
101-
* key.
102+
* Set the specified system property prior to loading the context and restore its
103+
* previous value once the consumer has been invoked and the context closed. If the
104+
* {@code value} is {@code null} this removes any prior customization for that key.
102105
* @param key the system property
103106
* @param value the value (can be null to remove any existing customization)
104107
* @return this instance
@@ -115,10 +118,10 @@ public ContextLoader systemProperty(String key, String value) {
115118
}
116119

117120
/**
118-
* Add the specified property pairs. Name-value pairs can be specified with
119-
* colon (":") or equals ("=") separators. Override matching keys that might
120-
* have been specified previously.
121-
* @param pairs The key value pairs for properties that need to be added to the
121+
* Add the specified property pairs. Key-value pairs can be specified with colon (":")
122+
* or equals ("=") separators. Override matching keys that might have been specified
123+
* previously.
124+
* @param pairs the key-value pairs for properties that need to be added to the
122125
* environment
123126
* @return this instance
124127
*/
@@ -155,10 +158,9 @@ public ContextLoader autoConfig(Class<?>... autoConfigurations) {
155158

156159
/**
157160
* Add the specified auto-configurations at the beginning (in that order) so that it
158-
* is applied before any other existing auto-configurations, but after any
159-
* user configuration. If {@code A} and {@code B} are specified, {@code A} will
160-
* be processed, then {@code B} and finally the rest of the existing
161-
* auto-configuration.
161+
* is applied before any other existing auto-configurations, but after any user
162+
* configuration. If {@code A} and {@code B} are specified, {@code A} will be
163+
* processed, then {@code B} and finally the rest of the existing auto-configuration.
162164
* @param autoConfigurations the auto-configuration to add
163165
* @return this instance
164166
*/
@@ -168,9 +170,9 @@ public ContextLoader autoConfigFirst(Class<?>... autoConfigurations) {
168170
}
169171

170172
/**
171-
* Customize the {@link ClassLoader} that the {@link ApplicationContext} should
172-
* use. Customizing the {@link ClassLoader} is an effective manner to hide resources
173-
* from the classpath.
173+
* Customize the {@link ClassLoader} that the {@link ApplicationContext} should use.
174+
* Customizing the {@link ClassLoader} is an effective manner to hide resources from
175+
* the classpath.
174176
* @param classLoader the classloader to use (can be null to use the default)
175177
* @return this instance
176178
* @see HidePackagesClassLoader
@@ -181,9 +183,9 @@ public ContextLoader classLoader(ClassLoader classLoader) {
181183
}
182184

183185
/**
184-
* Create and refresh a new {@link ApplicationContext} based on the current state
185-
* of this loader. The context is consumed by the specified {@link ContextConsumer}
186-
* and closed upon completion.
186+
* Create and refresh a new {@link ApplicationContext} based on the current state of
187+
* this loader. The context is consumed by the specified {@link ContextConsumer} and
188+
* closed upon completion.
187189
* @param consumer the consumer of the created {@link ApplicationContext}
188190
*/
189191
public void load(ContextConsumer consumer) {
@@ -196,15 +198,15 @@ public void load(ContextConsumer consumer) {
196198
throw ex;
197199
}
198200
catch (Throwable ex) {
199-
throw new IllegalStateException("An unexpected error occurred: "
200-
+ ex.getMessage(), ex);
201+
throw new IllegalStateException(
202+
"An unexpected error occurred: " + ex.getMessage(), ex);
201203
}
202204
}
203205
}
204206

205207
/**
206-
* Create and refresh a new {@link ApplicationContext} based on the current state
207-
* of this loader that this expected to fail. If the context does not fail, an
208+
* Create and refresh a new {@link ApplicationContext} based on the current state of
209+
* this loader that this expected to fail. If the context does not fail, an
208210
* {@link AssertionError} is thrown. Otherwise the exception is consumed by the
209211
* specified {@link Consumer} with no expectation on the type of the exception.
210212
* @param consumer the consumer of the failure
@@ -214,8 +216,8 @@ public void loadAndFail(Consumer<Throwable> consumer) {
214216
}
215217

216218
/**
217-
* Create and refresh a new {@link ApplicationContext} based on the current state
218-
* of this loader that this expected to fail. If the context does not fail, an
219+
* Create and refresh a new {@link ApplicationContext} based on the current state of
220+
* this loader that this expected to fail. If the context does not fail, an
219221
* {@link AssertionError} is thrown. If the exception does not match the specified
220222
* {@code exceptionType}, an {@link AssertionError} is thrown as well. If the
221223
* exception type matches, it is consumed by the specified {@link Consumer}.
@@ -246,14 +248,14 @@ private ConfigurableApplicationContext createApplicationContext() {
246248
.applyTo(ctx);
247249
}
248250
if (!ObjectUtils.isEmpty(this.userConfigurations)) {
249-
ctx.register(this.userConfigurations.toArray(
250-
new Class<?>[this.userConfigurations.size()]));
251+
ctx.register(this.userConfigurations
252+
.toArray(new Class<?>[this.userConfigurations.size()]));
251253
}
252254
if (!ObjectUtils.isEmpty(this.autoConfigurations)) {
253-
LinkedHashSet<Class<?>> linkedHashSet =
254-
new LinkedHashSet(this.autoConfigurations);
255-
ctx.register(linkedHashSet.toArray(
256-
new Class<?>[this.autoConfigurations.size()]));
255+
LinkedHashSet<Class<?>> linkedHashSet = new LinkedHashSet<>(
256+
this.autoConfigurations);
257+
ctx.register(
258+
linkedHashSet.toArray(new Class<?>[this.autoConfigurations.size()]));
257259
}
258260
return ctx;
259261
}

0 commit comments

Comments
 (0)