Skip to content

Commit 53192bb

Browse files
committed
Polishing
1 parent 5b35fc2 commit 53192bb

File tree

1 file changed

+56
-82
lines changed

1 file changed

+56
-82
lines changed

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

Lines changed: 56 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ public class ContextConfigurationAttributes {
5151

5252
private final boolean inheritLocations;
5353

54-
private final Class<? extends ContextLoader> contextLoaderClass;
55-
5654
private final Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[] initializers;
5755

5856
private final boolean inheritInitializers;
5957

6058
private final String name;
6159

60+
private final Class<? extends ContextLoader> contextLoaderClass;
61+
6262

6363
/**
6464
* Construct a new {@link ContextConfigurationAttributes} instance for the
@@ -69,8 +69,8 @@ public class ContextConfigurationAttributes {
6969
*/
7070
public ContextConfigurationAttributes(Class<?> declaringClass, ContextConfiguration contextConfiguration) {
7171
this(declaringClass, resolveLocations(declaringClass, contextConfiguration), contextConfiguration.classes(),
72-
contextConfiguration.inheritLocations(), contextConfiguration.initializers(),
73-
contextConfiguration.inheritInitializers(), contextConfiguration.name(), contextConfiguration.loader());
72+
contextConfiguration.inheritLocations(), contextConfiguration.initializers(),
73+
contextConfiguration.inheritInitializers(), contextConfiguration.name(), contextConfiguration.loader());
7474
}
7575

7676
/**
@@ -84,11 +84,11 @@ public ContextConfigurationAttributes(Class<?> declaringClass, ContextConfigurat
8484
@SuppressWarnings("unchecked")
8585
public ContextConfigurationAttributes(Class<?> declaringClass, AnnotationAttributes annAttrs) {
8686
this(declaringClass,
87-
resolveLocations(declaringClass, annAttrs.getStringArray("locations"), annAttrs.getStringArray("value")),
88-
annAttrs.getClassArray("classes"), annAttrs.getBoolean("inheritLocations"),
89-
(Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[]) annAttrs.getClassArray("initializers"),
90-
annAttrs.getBoolean("inheritInitializers"), annAttrs.getString("name"),
91-
(Class<? extends ContextLoader>) annAttrs.getClass("loader"));
87+
resolveLocations(declaringClass, annAttrs.getStringArray("locations"), annAttrs.getStringArray("value")),
88+
annAttrs.getClassArray("classes"), annAttrs.getBoolean("inheritLocations"),
89+
(Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[]) annAttrs.getClassArray("initializers"),
90+
annAttrs.getBoolean("inheritInitializers"), annAttrs.getString("name"),
91+
(Class<? extends ContextLoader>) annAttrs.getClass("loader"));
9292
}
9393

9494
/**
@@ -119,7 +119,6 @@ public ContextConfigurationAttributes(Class<?> declaringClass, String[] location
119119
* {@linkplain Class test class} that declared the
120120
* {@link ContextConfiguration @ContextConfiguration} annotation and its
121121
* corresponding attributes.
122-
*
123122
* @param declaringClass the test class that declared {@code @ContextConfiguration}
124123
* @param locations the resource locations declared via {@code @ContextConfiguration}
125124
* @param classes the annotated classes declared via {@code @ContextConfiguration}
@@ -134,8 +133,9 @@ public ContextConfigurationAttributes(
134133
Class<?> declaringClass, String[] locations, Class<?>[] classes, boolean inheritLocations,
135134
Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[] initializers,
136135
boolean inheritInitializers, Class<? extends ContextLoader> contextLoaderClass) {
136+
137137
this(declaringClass, locations, classes, inheritLocations, initializers, inheritInitializers, null,
138-
contextLoaderClass);
138+
contextLoaderClass);
139139
}
140140

141141
/**
@@ -164,11 +164,11 @@ public ContextConfigurationAttributes(
164164

165165
if (!ObjectUtils.isEmpty(locations) && !ObjectUtils.isEmpty(classes) && logger.isDebugEnabled()) {
166166
logger.debug(String.format(
167-
"Test class [%s] has been configured with @ContextConfiguration's 'locations' (or 'value') %s "
168-
+ "and 'classes' %s attributes. Most SmartContextLoader implementations support "
169-
+ "only one declaration of resources per @ContextConfiguration annotation.",
170-
declaringClass.getName(), ObjectUtils.nullSafeToString(locations),
171-
ObjectUtils.nullSafeToString(classes)));
167+
"Test class [%s] has been configured with @ContextConfiguration's 'locations' (or 'value') %s " +
168+
"and 'classes' %s attributes. Most SmartContextLoader implementations support " +
169+
"only one declaration of resources per @ContextConfiguration annotation.",
170+
declaringClass.getName(), ObjectUtils.nullSafeToString(locations),
171+
ObjectUtils.nullSafeToString(classes)));
172172
}
173173

174174
this.declaringClass = declaringClass;
@@ -177,7 +177,7 @@ public ContextConfigurationAttributes(
177177
this.inheritLocations = inheritLocations;
178178
this.initializers = initializers;
179179
this.inheritInitializers = inheritInitializers;
180-
this.name = StringUtils.hasText(name) ? name : null;
180+
this.name = (StringUtils.hasText(name) ? name : null);
181181
this.contextLoaderClass = contextLoaderClass;
182182
}
183183

@@ -245,6 +245,17 @@ public Class<?>[] getClasses() {
245245
return this.classes;
246246
}
247247

248+
/**
249+
* Determine if this {@code ContextConfigurationAttributes} instance has
250+
* class-based resources.
251+
* @return {@code true} if the {@link #getClasses() classes} array is not empty
252+
* @see #hasResources()
253+
* @see #hasLocations()
254+
*/
255+
public boolean hasClasses() {
256+
return !ObjectUtils.isEmpty(getClasses());
257+
}
258+
248259
/**
249260
* Set the <em>processed</em> resource locations, effectively overriding the
250261
* original value declared via {@link ContextConfiguration @ContextConfiguration}.
@@ -280,17 +291,6 @@ public boolean hasLocations() {
280291
return !ObjectUtils.isEmpty(getLocations());
281292
}
282293

283-
/**
284-
* Determine if this {@code ContextConfigurationAttributes} instance has
285-
* class-based resources.
286-
* @return {@code true} if the {@link #getClasses() classes} array is not empty
287-
* @see #hasResources()
288-
* @see #hasLocations()
289-
*/
290-
public boolean hasClasses() {
291-
return !ObjectUtils.isEmpty(getClasses());
292-
}
293-
294294
/**
295295
* Determine if this {@code ContextConfigurationAttributes} instance has
296296
* either path-based resource locations or class-based resources.
@@ -300,7 +300,7 @@ public boolean hasClasses() {
300300
* @see #hasClasses()
301301
*/
302302
public boolean hasResources() {
303-
return hasLocations() || hasClasses();
303+
return (hasLocations() || hasClasses());
304304
}
305305

306306
/**
@@ -310,7 +310,7 @@ public boolean hasResources() {
310310
* @see ContextConfiguration#inheritLocations
311311
*/
312312
public boolean isInheritLocations() {
313-
return inheritLocations;
313+
return this.inheritLocations;
314314
}
315315

316316
/**
@@ -320,7 +320,7 @@ public boolean isInheritLocations() {
320320
* @since 3.2
321321
*/
322322
public Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[] getInitializers() {
323-
return initializers;
323+
return this.initializers;
324324
}
325325

326326
/**
@@ -330,17 +330,7 @@ public Class<? extends ApplicationContextInitializer<? extends ConfigurableAppli
330330
* @since 3.2
331331
*/
332332
public boolean isInheritInitializers() {
333-
return inheritInitializers;
334-
}
335-
336-
/**
337-
* Get the {@code ContextLoader} class that was declared via
338-
* {@link ContextConfiguration @ContextConfiguration}.
339-
* @return the {@code ContextLoader} class
340-
* @see ContextConfiguration#loader
341-
*/
342-
public Class<? extends ContextLoader> getContextLoaderClass() {
343-
return contextLoaderClass;
333+
return this.inheritInitializers;
344334
}
345335

346336
/**
@@ -354,6 +344,16 @@ public String getName() {
354344
return this.name;
355345
}
356346

347+
/**
348+
* Get the {@code ContextLoader} class that was declared via
349+
* {@link ContextConfiguration @ContextConfiguration}.
350+
* @return the {@code ContextLoader} class
351+
* @see ContextConfiguration#loader
352+
*/
353+
public Class<? extends ContextLoader> getContextLoaderClass() {
354+
return this.contextLoaderClass;
355+
}
356+
357357

358358
/**
359359
* Determine if the supplied object is equal to this
@@ -367,47 +367,22 @@ public String getName() {
367367
* {@link #getContextLoaderClass() ContextLoader class}.
368368
*/
369369
@Override
370-
public boolean equals(Object obj) {
371-
if (this == obj) {
370+
public boolean equals(Object other) {
371+
if (this == other) {
372372
return true;
373373
}
374-
if (!(obj instanceof ContextConfigurationAttributes)) {
375-
return false;
376-
}
377-
378-
ContextConfigurationAttributes that = (ContextConfigurationAttributes) obj;
379-
if (this.declaringClass == null) {
380-
if (that.declaringClass != null) {
381-
return false;
382-
}
383-
}
384-
else if (!this.declaringClass.equals(that.declaringClass)) {
374+
if (!(other instanceof ContextConfigurationAttributes)) {
385375
return false;
386376
}
387-
if (!Arrays.equals(this.locations, that.locations)) {
388-
return false;
389-
}
390-
if (!Arrays.equals(this.classes, that.classes)) {
391-
return false;
392-
}
393-
if (this.inheritLocations != that.inheritLocations) {
394-
return false;
395-
}
396-
if (!Arrays.equals(this.initializers, that.initializers)) {
397-
return false;
398-
}
399-
if (this.inheritInitializers != that.inheritInitializers) {
400-
return false;
401-
}
402-
if (this.contextLoaderClass == null) {
403-
if (that.contextLoaderClass != null) {
404-
return false;
405-
}
406-
}
407-
else if (!this.contextLoaderClass.equals(that.contextLoaderClass)) {
408-
return false;
409-
}
410-
return true;
377+
ContextConfigurationAttributes otherAttr = (ContextConfigurationAttributes) other;
378+
return (ObjectUtils.nullSafeEquals(this.declaringClass, otherAttr.declaringClass) &&
379+
Arrays.equals(this.classes, otherAttr.classes)) &&
380+
Arrays.equals(this.locations, otherAttr.locations) &&
381+
this.inheritLocations == otherAttr.inheritLocations &&
382+
Arrays.equals(this.initializers, otherAttr.initializers) &&
383+
this.inheritInitializers == otherAttr.inheritInitializers &&
384+
ObjectUtils.nullSafeEquals(this.name, otherAttr.name) &&
385+
ObjectUtils.nullSafeEquals(this.contextLoaderClass, otherAttr.contextLoaderClass);
411386
}
412387

413388
/**
@@ -418,10 +393,9 @@ else if (!this.contextLoaderClass.equals(that.contextLoaderClass)) {
418393
@Override
419394
public int hashCode() {
420395
int result = this.declaringClass.hashCode();
421-
result = 31 * result + Arrays.hashCode(this.locations);
422396
result = 31 * result + Arrays.hashCode(this.classes);
397+
result = 31 * result + Arrays.hashCode(this.locations);
423398
result = 31 * result + Arrays.hashCode(this.initializers);
424-
result = 31 * result + this.contextLoaderClass.hashCode();
425399
return result;
426400
}
427401

@@ -433,8 +407,8 @@ public int hashCode() {
433407
public String toString() {
434408
return new ToStringCreator(this)
435409
.append("declaringClass", this.declaringClass.getName())
436-
.append("locations", ObjectUtils.nullSafeToString(this.locations))
437410
.append("classes", ObjectUtils.nullSafeToString(this.classes))
411+
.append("locations", ObjectUtils.nullSafeToString(this.locations))
438412
.append("inheritLocations", this.inheritLocations)
439413
.append("initializers", ObjectUtils.nullSafeToString(this.initializers))
440414
.append("inheritInitializers", this.inheritInitializers)

0 commit comments

Comments
 (0)