Skip to content

Commit 2f84096

Browse files
committed
Polish
1 parent 321092c commit 2f84096

File tree

9 files changed

+61
-32
lines changed

9 files changed

+61
-32
lines changed

spring-core-test/src/main/java/org/springframework/core/test/io/support/MockSpringFactoriesLoader.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.core.test.io.support;
1818

1919
import java.util.ArrayList;
20+
import java.util.Collections;
2021
import java.util.HashMap;
2122
import java.util.LinkedHashMap;
2223
import java.util.List;
@@ -58,16 +59,17 @@ public MockSpringFactoriesLoader(@Nullable ClassLoader classLoader) {
5859
this(classLoader, new LinkedHashMap<>());
5960
}
6061

61-
protected MockSpringFactoriesLoader(ClassLoader classLoader, Map<String, List<String>> factories) {
62+
protected MockSpringFactoriesLoader(@Nullable ClassLoader classLoader,
63+
Map<String, List<String>> factories) {
6264
super(classLoader, factories);
6365
this.factories = factories;
6466
}
6567

6668

6769
@Override
6870
@SuppressWarnings("unchecked")
69-
protected <T> T instantiateFactory(String implementationName, Class<T> type, ArgumentResolver argumentResolver,
70-
FailureHandler failureHandler) {
71+
protected <T> T instantiateFactory(String implementationName, Class<T> type,
72+
@Nullable ArgumentResolver argumentResolver, FailureHandler failureHandler) {
7173
if (implementationName.startsWith("!")) {
7274
Object implementation = this.implementations.get(implementationName);
7375
if (implementation != null) {
@@ -83,7 +85,6 @@ protected <T> T instantiateFactory(String implementationName, Class<T> type, Arg
8385
* @param factoryImplementations the implementation classes
8486
*/
8587
@SafeVarargs
86-
@SuppressWarnings("unchecked")
8788
public final <T> void add(Class<T> factoryType, Class<? extends T>... factoryImplementations) {
8889
for (Class<? extends T> factoryImplementation : factoryImplementations) {
8990
add(factoryType.getName(), factoryImplementation.getName());
@@ -96,10 +97,9 @@ public final <T> void add(Class<T> factoryType, Class<? extends T>... factoryImp
9697
* @param factoryImplementations the implementation class names
9798
*/
9899
public void add(String factoryType, String... factoryImplementations) {
99-
List<String> implementations = this.factories.computeIfAbsent(factoryType, key -> new ArrayList<>());
100-
for (String factoryImplementation : factoryImplementations) {
101-
implementations.add(factoryImplementation);
102-
}
100+
List<String> implementations = this.factories.computeIfAbsent(
101+
factoryType, key -> new ArrayList<>());
102+
Collections.addAll(implementations, factoryImplementations);
103103
}
104104

105105
/**

spring-core-test/src/main/java/org/springframework/core/test/tools/CompilationException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ private static String buildMessage(String errors, SourceFiles sourceFiles,
3838
message.append(errors);
3939
message.append("\n\n");
4040
for (SourceFile sourceFile : sourceFiles) {
41-
message.append("---- source: " + sourceFile.getPath() + "\n\n");
41+
message.append("---- source: ").append(sourceFile.getPath()).append("\n\n");
4242
message.append(sourceFile.getContent());
4343
message.append("\n\n");
4444
}
4545
for (ResourceFile resourceFile : resourceFiles) {
46-
message.append("---- resource: " + resourceFile.getPath() + "\n\n");
46+
message.append("---- resource: ").append(resourceFile.getPath()).append("\n\n");
4747
message.append(resourceFile.getContent());
4848
message.append("\n\n");
4949
}

spring-core-test/src/main/java/org/springframework/core/test/tools/DynamicClassFileObject.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import javax.tools.JavaFileObject;
2727
import javax.tools.SimpleJavaFileObject;
2828

29+
import org.springframework.lang.Nullable;
30+
2931
/**
3032
* In-memory {@link JavaFileObject} used to hold class bytecode.
3133
*
@@ -36,6 +38,7 @@ class DynamicClassFileObject extends SimpleJavaFileObject {
3638

3739
private final String className;
3840

41+
@Nullable
3942
private volatile byte[] bytes;
4043

4144

@@ -57,10 +60,11 @@ private static URI createUri(String className) {
5760

5861
@Override
5962
public InputStream openInputStream() throws IOException {
60-
if (this.bytes == null) {
63+
byte[] content = this.bytes;
64+
if (content == null) {
6165
throw new IOException("No data written");
6266
}
63-
return new ByteArrayInputStream(this.bytes);
67+
return new ByteArrayInputStream(content);
6468
}
6569

6670
@Override
@@ -76,6 +80,7 @@ String getClassName() {
7680
return this.className;
7781
}
7882

83+
@Nullable
7984
byte[] getBytes() {
8085
return this.bytes;
8186
}

spring-core-test/src/main/java/org/springframework/core/test/tools/DynamicClassLoader.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.function.Supplier;
3131

3232
import org.springframework.lang.Nullable;
33+
import org.springframework.util.Assert;
3334
import org.springframework.util.ReflectionUtils;
3435

3536
/**
@@ -65,12 +66,12 @@ public DynamicClassLoader(ClassLoader parent, ClassFiles classFiles, ResourceFil
6566
this.dynamicResourceFiles = dynamicResourceFiles;
6667
Class<? extends ClassLoader> parentClass = parent.getClass();
6768
if (parentClass.getName().equals(CompileWithForkedClassLoaderClassLoader.class.getName())) {
68-
Method setClassResourceLookupMethod = ReflectionUtils.findMethod(parentClass,
69+
Method setClassResourceLookupMethod = lookupMethod(parentClass,
6970
"setClassResourceLookup", Function.class);
7071
ReflectionUtils.makeAccessible(setClassResourceLookupMethod);
7172
ReflectionUtils.invokeMethod(setClassResourceLookupMethod,
7273
getParent(), (Function<String, byte[]>) this::findClassBytes);
73-
this.defineClassMethod = ReflectionUtils.findMethod(parentClass,
74+
this.defineClassMethod = lookupMethod(parentClass,
7475
"defineDynamicClass", String.class, byte[].class, int.class, int.class);
7576
ReflectionUtils.makeAccessible(this.defineClassMethod);
7677
this.dynamicClassFiles.forEach((name, file) -> defineClass(name, file.getBytes()));
@@ -84,12 +85,13 @@ public DynamicClassLoader(ClassLoader parent, ClassFiles classFiles, ResourceFil
8485
@Override
8586
protected Class<?> findClass(String name) throws ClassNotFoundException {
8687
byte[] bytes = findClassBytes(name);
87-
if(bytes != null) {
88+
if (bytes != null) {
8889
return defineClass(name, bytes);
8990
}
9091
return super.findClass(name);
9192
}
9293

94+
@Nullable
9395
private byte[] findClassBytes(String name) {
9496
ClassFile classFile = this.classFiles.get(name);
9597
if (classFile != null) {
@@ -141,6 +143,12 @@ private URL createResourceUrl(String name, Supplier<byte[]> bytesSupplier) {
141143
}
142144
}
143145

146+
private static Method lookupMethod(Class<?> target, String name, Class<?>... parameterTypes) {
147+
Method method = ReflectionUtils.findMethod(target, name, parameterTypes);
148+
Assert.notNull(method, "Expected method '" + name + "' on '" + target.getName());
149+
return method;
150+
}
151+
144152

145153
private static class SingletonEnumeration<E> implements Enumeration<E> {
146154

spring-core-test/src/main/java/org/springframework/core/test/tools/DynamicFileAssert.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* Assertion methods for {@code DynamicFile} instances.
2727
*
2828
* @author Phillip Webb
29+
* @author Stephane Nicoll
2930
* @since 6.0
3031
* @param <A> the assertion type
3132
* @param <F> the file type
@@ -38,25 +39,35 @@ public class DynamicFileAssert<A extends DynamicFileAssert<A, F>, F extends Dyna
3839
super(actual, selfType);
3940
}
4041

42+
/**
43+
* Verify that the actual content is equal to the given one.
44+
* @param content the expected content of the file
45+
* @return {@code this}, to facilitate method chaining
46+
*/
47+
public A hasContent(@Nullable CharSequence content) {
48+
assertThat(this.actual.getContent()).isEqualTo(
49+
content != null ? content.toString() : null);
50+
return this.myself;
51+
}
4152

53+
/**
54+
* Verify that the actual content contains all the given values.
55+
* @param values the values to look for
56+
* @return {@code this}, to facilitate method chaining
57+
*/
4258
public A contains(CharSequence... values) {
4359
assertThat(this.actual.getContent()).contains(values);
4460
return this.myself;
4561
}
4662

63+
/**
64+
* Verify that the actual content does not contain any of the given values.
65+
* @param values the values to look for
66+
* @return {@code this}, to facilitate method chaining
67+
*/
4768
public A doesNotContain(CharSequence... values) {
4869
assertThat(this.actual.getContent()).doesNotContain(values);
4970
return this.myself;
5071
}
5172

52-
@Override
53-
public A isEqualTo(@Nullable Object expected) {
54-
if (expected instanceof DynamicFile) {
55-
return super.isEqualTo(expected);
56-
}
57-
assertThat(this.actual.getContent()).isEqualTo(
58-
expected != null ? expected.toString() : null);
59-
return this.myself;
60-
}
61-
6273
}

spring-core-test/src/main/java/org/springframework/core/test/tools/DynamicResourceFileObject.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import javax.tools.JavaFileObject;
2727
import javax.tools.SimpleJavaFileObject;
2828

29+
import org.springframework.lang.Nullable;
30+
2931
/**
3032
* In-memory {@link JavaFileObject} used to hold generated resource file contents.
3133
*
@@ -35,6 +37,7 @@
3537
*/
3638
class DynamicResourceFileObject extends SimpleJavaFileObject {
3739

40+
@Nullable
3841
private volatile byte[] bytes;
3942

4043

@@ -54,10 +57,11 @@ private static URI createUri(String fileName) {
5457

5558
@Override
5659
public InputStream openInputStream() throws IOException {
57-
if (this.bytes == null) {
60+
byte[] content = this.bytes;
61+
if (content == null) {
5862
throw new IOException("No data written");
5963
}
60-
return new ByteArrayInputStream(this.bytes);
64+
return new ByteArrayInputStream(content);
6165
}
6266

6367
@Override
@@ -69,6 +73,7 @@ private void closeOutputStream(byte[] bytes) {
6973
this.bytes = bytes;
7074
}
7175

76+
@Nullable
7277
byte[] getBytes() {
7378
return this.bytes;
7479
}

spring-core-test/src/main/java/org/springframework/core/test/tools/TestCompiler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,12 @@ private DynamicClassLoader compile() {
309309
*/
310310
public TestCompiler printFiles(PrintStream printStream) {
311311
for (SourceFile sourceFile : this.sourceFiles) {
312-
printStream.append("---- source: " + sourceFile.getPath() + "\n\n");
312+
printStream.append("---- source: ").append(sourceFile.getPath()).append("\n\n");
313313
printStream.append(sourceFile.getContent());
314314
printStream.append("\n\n");
315315
}
316316
for (ResourceFile resourceFile : this.resourceFiles) {
317-
printStream.append("---- resource: " + resourceFile.getPath() + "\n\n");
317+
printStream.append("---- resource: ").append(resourceFile.getPath()).append("\n\n");
318318
printStream.append(resourceFile.getContent());
319319
printStream.append("\n\n");
320320
}

spring-core-test/src/test/java/org/springframework/core/test/io/support/MockSpringFactoriesLoaderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private void assertThatLoaderHasTestFactories(MockSpringFactoriesLoader loader)
6666
assertThat(factories.get(1)).isInstanceOf(TestFactoryTwo.class);
6767
}
6868

69-
static interface TestFactoryType {
69+
interface TestFactoryType {
7070

7171
}
7272

spring-core-test/src/test/java/org/springframework/core/test/tools/SourceFileAssertTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ void containsWhenMissingOneThrowsException() {
6565

6666
@Test
6767
void isEqualToWhenEqual() {
68-
assertThat(this.sourceFile).isEqualTo(SAMPLE);
68+
assertThat(this.sourceFile).hasContent(SAMPLE);
6969
}
7070

7171
@Test
7272
void isEqualToWhenNotEqualThrowsException() {
7373
assertThatExceptionOfType(AssertionError.class).isThrownBy(
74-
() -> assertThat(this.sourceFile).isEqualTo("no")).withMessageContaining(
74+
() -> assertThat(this.sourceFile).hasContent("no")).withMessageContaining(
7575
"expected", "but was");
7676
}
7777

0 commit comments

Comments
 (0)