Skip to content

Commit 8c7579e

Browse files
committed
Polishing
1 parent a3d763d commit 8c7579e

File tree

8 files changed

+60
-65
lines changed

8 files changed

+60
-65
lines changed

spring-context/src/test/java/org/springframework/scheduling/annotation/EnableAsyncTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public void customExecutorBean() throws InterruptedException {
217217
}
218218

219219
@Test
220-
public void customExecutorConfig() throws InterruptedException {
220+
public void customExecutorConfig() {
221221
// Arrange
222222
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
223223
ctx.register(CustomExecutorConfig.class);

spring-context/src/test/java/org/springframework/tests/mock/jndi/ExpectedLookupTemplate.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,51 +23,49 @@
2323
import org.springframework.jndi.JndiTemplate;
2424

2525
/**
26-
* Simple extension of the JndiTemplate class that always returns
27-
* a given object. Very useful for testing. Effectively a mock object.
26+
* Simple extension of the JndiTemplate class that always returns a given object.
27+
*
28+
* <p>Very useful for testing. Effectively a mock object.
2829
*
2930
* @author Rod Johnson
3031
* @author Juergen Hoeller
3132
*/
3233
public class ExpectedLookupTemplate extends JndiTemplate {
3334

34-
private final Map<String, Object> jndiObjects = new ConcurrentHashMap<>();
35+
private final Map<String, Object> jndiObjects = new ConcurrentHashMap<>(16);
3536

3637

3738
/**
38-
* Construct a new JndiTemplate that will always return given objects
39-
* for given names. To be populated through {@code addObject} calls.
39+
* Construct a new JndiTemplate that will always return given objects for
40+
* given names. To be populated through {@code addObject} calls.
4041
* @see #addObject(String, Object)
4142
*/
4243
public ExpectedLookupTemplate() {
4344
}
4445

4546
/**
46-
* Construct a new JndiTemplate that will always return the
47-
* given object, but honour only requests for the given name.
47+
* Construct a new JndiTemplate that will always return the given object,
48+
* but honour only requests for the given name.
4849
* @param name the name the client is expected to look up
4950
* @param object the object that will be returned
5051
*/
5152
public ExpectedLookupTemplate(String name, Object object) {
5253
addObject(name, object);
5354
}
5455

55-
5656
/**
57-
* Add the given object to the list of JNDI objects that this
58-
* template will expose.
57+
* Add the given object to the list of JNDI objects that this template will expose.
5958
* @param name the name the client is expected to look up
6059
* @param object the object that will be returned
6160
*/
6261
public void addObject(String name, Object object) {
6362
this.jndiObjects.put(name, object);
6463
}
6564

66-
6765
/**
68-
* If the name is the expected name specified in the constructor,
69-
* return the object provided in the constructor. If the name is
70-
* unexpected, a respective NamingException gets thrown.
66+
* If the name is the expected name specified in the constructor, return the
67+
* object provided in the constructor. If the name is unexpected, a
68+
* respective NamingException gets thrown.
7169
*/
7270
@Override
7371
public Object lookup(String name) throws NamingException {

spring-context/src/test/java/org/springframework/tests/mock/jndi/SimpleNamingContext.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -33,6 +33,7 @@
3333
import org.apache.commons.logging.Log;
3434
import org.apache.commons.logging.LogFactory;
3535

36+
import org.springframework.lang.Nullable;
3637
import org.springframework.util.StringUtils;
3738

3839
/**
@@ -80,7 +81,9 @@ public SimpleNamingContext(String root) {
8081
* Create a new naming context with the given naming root,
8182
* the given name/object map, and the JNDI environment entries.
8283
*/
83-
public SimpleNamingContext(String root, Hashtable<String, Object> boundObjects, Hashtable<String, Object> env) {
84+
public SimpleNamingContext(
85+
String root, Hashtable<String, Object> boundObjects, @Nullable Hashtable<String, Object> env) {
86+
8487
this.root = root;
8588
this.boundObjects = boundObjects;
8689
if (env != null) {
@@ -206,6 +209,7 @@ public Hashtable<String, Object> getEnvironment() {
206209
}
207210

208211
@Override
212+
@Nullable
209213
public Object addToEnvironment(String propName, Object propVal) {
210214
return this.environment.put(propName, propVal);
211215
}
@@ -293,7 +297,7 @@ public Name composeName(Name name, Name prefix) throws NamingException {
293297
}
294298

295299

296-
private static abstract class AbstractNamingEnumeration<T> implements NamingEnumeration<T> {
300+
private abstract static class AbstractNamingEnumeration<T> implements NamingEnumeration<T> {
297301

298302
private Iterator<T> iterator;
299303

@@ -353,7 +357,7 @@ public void close() {
353357
}
354358

355359

356-
private static class NameClassPairEnumeration extends AbstractNamingEnumeration<NameClassPair> {
360+
private static final class NameClassPairEnumeration extends AbstractNamingEnumeration<NameClassPair> {
357361

358362
private NameClassPairEnumeration(SimpleNamingContext context, String root) throws NamingException {
359363
super(context, root);
@@ -366,7 +370,7 @@ protected NameClassPair createObject(String strippedName, Object obj) {
366370
}
367371

368372

369-
private static class BindingEnumeration extends AbstractNamingEnumeration<Binding> {
373+
private static final class BindingEnumeration extends AbstractNamingEnumeration<Binding> {
370374

371375
private BindingEnumeration(SimpleNamingContext context, String root) throws NamingException {
372376
super(context, root);

spring-context/src/test/java/org/springframework/tests/mock/jndi/SimpleNamingContextBuilder.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -26,7 +26,10 @@
2626
import org.apache.commons.logging.Log;
2727
import org.apache.commons.logging.LogFactory;
2828

29+
import org.springframework.lang.Nullable;
30+
import org.springframework.util.Assert;
2931
import org.springframework.util.ClassUtils;
32+
import org.springframework.util.ReflectionUtils;
3033

3134
/**
3235
* Simple implementation of a JNDI naming context builder.
@@ -42,7 +45,7 @@
4245
* <ul>
4346
* <li>{@code SingleConnectionDataSource} (using the same Connection for all getConnection calls)
4447
* <li>{@code DriverManagerDataSource} (creating a new Connection on each getConnection call)
45-
* <li>Apache's Jakarta Commons DBCP offers {@code org.apache.commons.dbcp.BasicDataSource} (a real pool)
48+
* <li>Apache's Commons DBCP offers {@code org.apache.commons.dbcp.BasicDataSource} (a real pool)
4649
* </ul>
4750
*
4851
* <p>Typical usage in bootstrap code:
@@ -80,7 +83,8 @@
8083
*/
8184
public class SimpleNamingContextBuilder implements InitialContextFactoryBuilder {
8285

83-
/** An instance of this class bound to JNDI */
86+
/** An instance of this class bound to JNDI. */
87+
@Nullable
8488
private static volatile SimpleNamingContextBuilder activated;
8589

8690
private static boolean initialized = false;
@@ -93,31 +97,33 @@ public class SimpleNamingContextBuilder implements InitialContextFactoryBuilder
9397
* @return the current SimpleNamingContextBuilder instance,
9498
* or {@code null} if none
9599
*/
100+
@Nullable
96101
public static SimpleNamingContextBuilder getCurrentContextBuilder() {
97102
return activated;
98103
}
99104

100105
/**
101106
* If no SimpleNamingContextBuilder is already configuring JNDI,
102-
* create and activate one. Otherwise take the existing activate
107+
* create and activate one. Otherwise take the existing activated
103108
* SimpleNamingContextBuilder, clear it and return it.
104109
* <p>This is mainly intended for test suites that want to
105110
* reinitialize JNDI bindings from scratch repeatedly.
106111
* @return an empty SimpleNamingContextBuilder that can be used
107112
* to control JNDI bindings
108113
*/
109114
public static SimpleNamingContextBuilder emptyActivatedContextBuilder() throws NamingException {
110-
if (activated != null) {
115+
SimpleNamingContextBuilder builder = activated;
116+
if (builder != null) {
111117
// Clear already activated context builder.
112-
activated.clear();
118+
builder.clear();
113119
}
114120
else {
115121
// Create and activate new context builder.
116-
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
122+
builder = new SimpleNamingContextBuilder();
117123
// The activate() call will cause an assignment to the activated variable.
118124
builder.activate();
119125
}
120-
return activated;
126+
return builder;
121127
}
122128

123129

@@ -138,12 +144,10 @@ public void activate() throws IllegalStateException, NamingException {
138144
logger.info("Activating simple JNDI environment");
139145
synchronized (initializationLock) {
140146
if (!initialized) {
141-
if (NamingManager.hasInitialContextFactoryBuilder()) {
142-
throw new IllegalStateException(
147+
Assert.state(!NamingManager.hasInitialContextFactoryBuilder(),
143148
"Cannot activate SimpleNamingContextBuilder: there is already a JNDI provider registered. " +
144149
"Note that JNDI is a JVM-wide service, shared at the JVM system class loader level, " +
145150
"with no reset option. As a consequence, a JNDI provider must only be registered once per JVM.");
146-
}
147151
NamingManager.setInitialContextFactoryBuilder(this);
148152
initialized = true;
149153
}
@@ -192,7 +196,8 @@ public void bind(String name, Object obj) {
192196
* @see SimpleNamingContext
193197
*/
194198
@Override
195-
public InitialContextFactory createInitialContextFactory(Hashtable<?,?> environment) {
199+
@SuppressWarnings("unchecked")
200+
public InitialContextFactory createInitialContextFactory(@Nullable Hashtable<?,?> environment) {
196201
if (activated == null && environment != null) {
197202
Object icf = environment.get(Context.INITIAL_CONTEXT_FACTORY);
198203
if (icf != null) {
@@ -212,22 +217,16 @@ else if (icf instanceof String) {
212217
"Specified class does not implement [" + InitialContextFactory.class.getName() + "]: " + icf);
213218
}
214219
try {
215-
return (InitialContextFactory) icfClass.newInstance();
220+
return (InitialContextFactory) ReflectionUtils.accessibleConstructor(icfClass).newInstance();
216221
}
217222
catch (Throwable ex) {
218-
throw new IllegalStateException("Cannot instantiate specified InitialContextFactory: " + icf, ex);
223+
throw new IllegalStateException("Unable to instantiate specified InitialContextFactory: " + icf, ex);
219224
}
220225
}
221226
}
222227

223228
// Default case...
224-
return new InitialContextFactory() {
225-
@Override
226-
@SuppressWarnings("unchecked")
227-
public Context getInitialContext(Hashtable<?,?> environment) {
228-
return new SimpleNamingContext("", boundObjects, (Hashtable<String, Object>) environment);
229-
}
230-
};
229+
return env -> new SimpleNamingContext("", this.boundObjects, (Hashtable<String, Object>) env);
231230
}
232231

233232
}

spring-core/src/main/java/org/springframework/util/ObjectUtils.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public static boolean containsElement(@Nullable Object[] array, Object element)
199199
/**
200200
* Check whether the given array of enum constants contains a constant with the given name,
201201
* ignoring case when determining a match.
202-
* @param enumValues the enum values to check, typically the product of a call to MyEnum.values()
202+
* @param enumValues the enum values to check, typically obtained via {@code MyEnum.values()}
203203
* @param constant the constant name to find (must not be null or empty string)
204204
* @return whether the constant has been found in the given array
205205
*/
@@ -209,15 +209,14 @@ public static boolean containsConstant(Enum<?>[] enumValues, String constant) {
209209

210210
/**
211211
* Check whether the given array of enum constants contains a constant with the given name.
212-
* @param enumValues the enum values to check, typically the product of a call to MyEnum.values()
212+
* @param enumValues the enum values to check, typically obtained via {@code MyEnum.values()}
213213
* @param constant the constant name to find (must not be null or empty string)
214214
* @param caseSensitive whether case is significant in determining a match
215215
* @return whether the constant has been found in the given array
216216
*/
217217
public static boolean containsConstant(Enum<?>[] enumValues, String constant, boolean caseSensitive) {
218218
for (Enum<?> candidate : enumValues) {
219-
if (caseSensitive ?
220-
candidate.toString().equals(constant) :
219+
if (caseSensitive ? candidate.toString().equals(constant) :
221220
candidate.toString().equalsIgnoreCase(constant)) {
222221
return true;
223222
}
@@ -228,7 +227,7 @@ public static boolean containsConstant(Enum<?>[] enumValues, String constant, bo
228227
/**
229228
* Case insensitive alternative to {@link Enum#valueOf(Class, String)}.
230229
* @param <E> the concrete Enum type
231-
* @param enumValues the array of all Enum constants in question, usually per Enum.values()
230+
* @param enumValues the array of all Enum constants in question, usually per {@code Enum.values()}
232231
* @param constant the constant to get the enum value of
233232
* @throws IllegalArgumentException if the given constant is not found in the given array
234233
* of enum values. Use {@link #containsConstant(Enum[], String)} as a guard to avoid this exception.
@@ -239,9 +238,8 @@ public static <E extends Enum<?>> E caseInsensitiveValueOf(E[] enumValues, Strin
239238
return candidate;
240239
}
241240
}
242-
throw new IllegalArgumentException(
243-
String.format("constant [%s] does not exist in enum type %s",
244-
constant, enumValues.getClass().getComponentType().getName()));
241+
throw new IllegalArgumentException("Constant [" + constant + "] does not exist in enum type " +
242+
enumValues.getClass().getComponentType().getName());
245243
}
246244

247245
/**

spring-core/src/test/java/org/springframework/util/FastByteArrayOutputStreamTests.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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,30 +20,24 @@
2020
import java.io.ByteArrayOutputStream;
2121
import java.io.IOException;
2222
import java.io.InputStream;
23+
import java.nio.charset.StandardCharsets;
2324

24-
import org.junit.Before;
2525
import org.junit.Test;
2626

2727
import static org.junit.Assert.*;
2828

2929
/**
30-
* Test suite for {@link FastByteArrayOutputStream}
30+
* Test suite for {@link FastByteArrayOutputStream}.
31+
*
3132
* @author Craig Andrews
3233
*/
3334
public class FastByteArrayOutputStreamTests {
3435

3536
private static final int INITIAL_CAPACITY = 256;
3637

37-
private FastByteArrayOutputStream os;
38-
39-
private byte[] helloBytes;
38+
private final FastByteArrayOutputStream os = new FastByteArrayOutputStream(INITIAL_CAPACITY);;
4039

41-
42-
@Before
43-
public void setUp() throws Exception {
44-
this.os = new FastByteArrayOutputStream(INITIAL_CAPACITY);
45-
this.helloBytes = "Hello World".getBytes("UTF-8");
46-
}
40+
private final byte[] helloBytes = "Hello World".getBytes(StandardCharsets.UTF_8);;
4741

4842

4943
@Test

spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class ObjectUtilsTests {
4545
@Rule
4646
public final ExpectedException exception = ExpectedException.none();
4747

48+
4849
@Test
4950
public void isCheckedException() {
5051
assertTrue(ObjectUtils.isCheckedException(new Exception()));
@@ -807,7 +808,8 @@ public void caseInsensitiveValueOf() {
807808
assertThat(ObjectUtils.caseInsensitiveValueOf(Tropes.values(), "BAR"), is(Tropes.BAR));
808809

809810
exception.expect(IllegalArgumentException.class);
810-
exception.expectMessage(is("constant [bogus] does not exist in enum type org.springframework.util.ObjectUtilsTests$Tropes"));
811+
exception.expectMessage(
812+
is("Constant [bogus] does not exist in enum type org.springframework.util.ObjectUtilsTests$Tropes"));
811813
ObjectUtils.caseInsensitiveValueOf(Tropes.values(), "bogus");
812814
}
813815

@@ -818,6 +820,6 @@ private void assertEqualHashCodes(int expected, Object array) {
818820
}
819821

820822

821-
enum Tropes { FOO, BAR, baz }
823+
enum Tropes {FOO, BAR, baz}
822824

823825
}

spring-test/src/main/java/org/springframework/mock/jndi/SimpleNamingContextBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public static SimpleNamingContextBuilder getCurrentContextBuilder() {
104104

105105
/**
106106
* If no SimpleNamingContextBuilder is already configuring JNDI,
107-
* create and activate one. Otherwise take the existing activate
107+
* create and activate one. Otherwise take the existing activated
108108
* SimpleNamingContextBuilder, clear it and return it.
109109
* <p>This is mainly intended for test suites that want to
110110
* reinitialize JNDI bindings from scratch repeatedly.
@@ -226,7 +226,7 @@ else if (icf instanceof String) {
226226
}
227227

228228
// Default case...
229-
return environment1 -> new SimpleNamingContext("", this.boundObjects, (Hashtable<String, Object>) environment1);
229+
return env -> new SimpleNamingContext("", this.boundObjects, (Hashtable<String, Object>) env);
230230
}
231231

232232
}

0 commit comments

Comments
 (0)