Skip to content

Commit 74caa92

Browse files
committed
Simplify SourceFileAssert assertion methods
Remove assertion methods that turned out not to be needed when testing Spring Framework's AOT generated code. Closes gh-28556
1 parent 5f4bcf1 commit 74caa92

File tree

8 files changed

+30
-333
lines changed

8 files changed

+30
-333
lines changed

spring-core-test/src/main/java/org/springframework/aot/test/generator/file/MethodAssert.java

Lines changed: 0 additions & 63 deletions
This file was deleted.

spring-core-test/src/main/java/org/springframework/aot/test/generator/file/SourceFile.java

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323

2424
import com.thoughtworks.qdox.JavaProjectBuilder;
2525
import com.thoughtworks.qdox.model.JavaClass;
26-
import com.thoughtworks.qdox.model.JavaPackage;
2726
import com.thoughtworks.qdox.model.JavaSource;
2827
import org.assertj.core.api.AssertProvider;
29-
import org.assertj.core.util.Strings;
3028

3129
import org.springframework.core.io.InputStreamSource;
3230
import org.springframework.lang.Nullable;
31+
import org.springframework.util.Assert;
32+
import org.springframework.util.ClassUtils;
3333
import org.springframework.util.FileCopyUtils;
34+
import org.springframework.util.StringUtils;
3435

3536
/**
3637
* {@link DynamicFile} that holds Java source code and provides
@@ -48,12 +49,12 @@ public final class SourceFile extends DynamicFile
4849
implements AssertProvider<SourceFileAssert> {
4950

5051

51-
private final JavaSource javaSource;
52+
private final String className;
5253

5354

54-
private SourceFile(String path, String content, JavaSource javaSource) {
55+
private SourceFile(String path, String content, String className) {
5556
super(path, content);
56-
this.javaSource = javaSource;
57+
this.className = className;
5758
}
5859

5960

@@ -126,53 +127,38 @@ public static SourceFile of(WritableContent writableContent) {
126127
*/
127128
public static SourceFile of(@Nullable String path, WritableContent writableContent) {
128129
String content = toString(writableContent);
129-
if (Strings.isNullOrEmpty(content)) {
130-
throw new IllegalStateException("WritableContent did not append any content");
130+
Assert.state(StringUtils.hasLength(content), "WritableContent did not append any content");
131+
String className = getClassName(content);
132+
if (!StringUtils.hasLength(path)) {
133+
path = ClassUtils.convertClassNameToResourcePath(className) + ".java";
131134
}
132-
JavaSource javaSource = parse(content);
133-
if (path == null || path.isEmpty()) {
134-
path = deducePath(javaSource);
135-
}
136-
return new SourceFile(path, content, javaSource);
135+
return new SourceFile(path, content, className);
137136
}
138137

139-
private static JavaSource parse(String content) {
138+
/**
139+
* Return the fully-qualified class name.
140+
* @return the fully qualified class name
141+
*/
142+
public String getClassName() {
143+
return this.className;
144+
}
145+
146+
private static String getClassName(String content) {
140147
JavaProjectBuilder builder = new JavaProjectBuilder();
141148
try {
142149
JavaSource javaSource = builder.addSource(new StringReader(content));
143-
if (javaSource.getClasses().size() != 1) {
144-
throw new IllegalStateException("Source must define a single class");
145-
}
146-
return javaSource;
150+
Assert.state(javaSource.getClasses().size() == 1, "Source must define a single class");
151+
JavaClass javaClass = javaSource.getClasses().get(0);
152+
return (javaSource.getPackage() != null)
153+
? javaSource.getPackageName() + "." + javaClass.getName()
154+
: javaClass.getName();
147155
}
148156
catch (Exception ex) {
149157
throw new IllegalStateException(
150158
"Unable to parse source file content:\n\n" + content, ex);
151159
}
152160
}
153161

154-
private static String deducePath(JavaSource javaSource) {
155-
JavaPackage javaPackage = javaSource.getPackage();
156-
JavaClass javaClass = javaSource.getClasses().get(0);
157-
String path = javaClass.getName() + ".java";
158-
if (javaPackage != null) {
159-
path = javaPackage.getName().replace('.', '/') + "/" + path;
160-
}
161-
return path;
162-
}
163-
164-
JavaSource getJavaSource() {
165-
return this.javaSource;
166-
}
167-
168-
/**
169-
* Return the class name of the source file.
170-
* @return the class name
171-
*/
172-
public String getClassName() {
173-
return this.javaSource.getClasses().get(0).getFullyQualifiedName();
174-
}
175-
176162
/**
177163
* AssertJ {@code assertThat} support.
178164
* @deprecated use {@code assertThat(sourceFile)} rather than calling this
@@ -184,4 +170,6 @@ public SourceFileAssert assertThat() {
184170
return new SourceFileAssert(this);
185171
}
186172

173+
174+
187175
}

spring-core-test/src/main/java/org/springframework/aot/test/generator/file/SourceFileAssert.java

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,6 @@
1616

1717
package org.springframework.aot.test.generator.file;
1818

19-
import java.util.Arrays;
20-
import java.util.List;
21-
import java.util.stream.Collectors;
22-
23-
import com.thoughtworks.qdox.model.JavaClass;
24-
import com.thoughtworks.qdox.model.JavaMethod;
25-
import com.thoughtworks.qdox.model.JavaParameter;
26-
import com.thoughtworks.qdox.model.JavaType;
27-
import org.assertj.core.error.BasicErrorMessageFactory;
28-
import org.assertj.core.internal.Failures;
29-
30-
import org.springframework.lang.Nullable;
31-
32-
import static org.assertj.core.api.Assertions.assertThat;
33-
3419
/**
3520
* Assertion methods for {@code SourceFile} instances.
3621
*
@@ -44,87 +29,4 @@ public class SourceFileAssert extends DynamicFileAssert<SourceFileAssert, Source
4429
super(actual, SourceFileAssert.class);
4530
}
4631

47-
48-
public SourceFileAssert implementsInterface(@Nullable Class<?> type) {
49-
return implementsInterface((type != null ? type.getName() : null));
50-
}
51-
52-
public SourceFileAssert implementsInterface(@Nullable String name) {
53-
JavaClass javaClass = getJavaClass();
54-
assertThat(javaClass.getImplements()).as("implements").map(
55-
JavaType::getFullyQualifiedName).contains(name);
56-
return this;
57-
}
58-
59-
public MethodAssert hasMethodNamed(String name) {
60-
JavaClass javaClass = getJavaClass();
61-
JavaMethod method = null;
62-
for (JavaMethod candidate : javaClass.getMethods()) {
63-
if (candidate.getName().equals(name)) {
64-
if (method != null) {
65-
throw Failures.instance().failure(this.info,
66-
new BasicErrorMessageFactory(String.format(
67-
"%nExpecting actual:%n %s%nto contain unique method:%n %s%n",
68-
this.actual.getContent(), name)));
69-
}
70-
method = candidate;
71-
}
72-
}
73-
if (method == null) {
74-
throw Failures.instance().failure(this.info,
75-
new BasicErrorMessageFactory(String.format(
76-
"%nExpecting actual:%n %s%nto contain method:%n %s%n",
77-
this.actual.getContent(), name)));
78-
}
79-
return new MethodAssert(method);
80-
}
81-
82-
public MethodAssert hasMethod(String name, Class<?>... parameters) {
83-
JavaClass javaClass = getJavaClass();
84-
JavaMethod method = null;
85-
for (JavaMethod candidate : javaClass.getMethods()) {
86-
if (candidate.getName().equals(name)
87-
&& hasParameters(candidate, parameters)) {
88-
if (method != null) {
89-
throw Failures.instance().failure(this.info,
90-
new BasicErrorMessageFactory(String.format(
91-
"%nExpecting actual:%n %s%nto contain unique method:%n %s%n",
92-
this.actual.getContent(), name)));
93-
}
94-
method = candidate;
95-
}
96-
}
97-
if (method == null) {
98-
String methodDescription = getMethodDescription(name, parameters);
99-
throw Failures.instance().failure(this.info,
100-
new BasicErrorMessageFactory(String.format(
101-
"%nExpecting actual:%n %s%nto contain method:%n %s%n",
102-
this.actual.getContent(), methodDescription)));
103-
}
104-
return new MethodAssert(method);
105-
}
106-
107-
private boolean hasParameters(JavaMethod method, Class<?>[] requiredParameters) {
108-
List<JavaParameter> parameters = method.getParameters();
109-
if (parameters.size() != requiredParameters.length) {
110-
return false;
111-
}
112-
for (int i = 0; i < requiredParameters.length; i++) {
113-
if (!requiredParameters[i].getName().equals(
114-
parameters.get(i).getFullyQualifiedName())) {
115-
return false;
116-
}
117-
}
118-
return true;
119-
}
120-
121-
private String getMethodDescription(String name, Class<?>... parameters) {
122-
return name + "(" + Arrays.stream(parameters).map(Class::getName).collect(
123-
Collectors.joining(", ")) + ")";
124-
}
125-
126-
private JavaClass getJavaClass() {
127-
return this.actual.getJavaSource().getClasses().get(0);
128-
}
129-
13032
}

spring-core-test/src/main/java/org/springframework/aot/test/generator/file/SourceFiles.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.stream.Stream;
2323

2424
import org.springframework.lang.Nullable;
25+
import org.springframework.util.ClassUtils;
2526

2627
/**
2728
* An immutable collection of {@link SourceFile} instances.
@@ -155,7 +156,7 @@ private SourceFile getSingle(Pattern pattern) {
155156
*/
156157
public SourceFile getSingleFromPackage(String packageName) {
157158
return this.files.getSingle(candidate -> Objects.equals(packageName,
158-
candidate.getJavaSource().getPackageName()));
159+
ClassUtils.getPackageName(candidate.getClassName())));
159160
}
160161

161162
@Override

spring-core-test/src/test/java/org/springframework/aot/test/generator/compile/TestCompilerTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ void compileWhenHasDifferentClassesWithSameClassNameCompilesBoth() {
100100
@Test
101101
void compileAndGetSourceFile() {
102102
TestCompiler.forSystem().withSources(SourceFile.of(HELLO_SPRING)).compile(
103-
compiled -> assertThat(compiled.getSourceFile()).hasMethodNamed(
104-
"get").withBodyContaining("// !!"));
103+
compiled -> assertThat(compiled.getSourceFile()).contains("// !!"));
105104
}
106105

107106
@Test

spring-core-test/src/test/java/org/springframework/aot/test/generator/file/MethodAssertTests.java

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)