Browse Source

Allow TestCompiler SourceFile to work with records

Update `SourceFile` to try a regex replace to make `record` files look
like regular classes.

Closes gh-29236
pull/29238/head
Phillip Webb 3 years ago
parent
commit
7fd8e081bb
  1. 2
      framework-platform/framework-platform.gradle
  2. 9
      spring-core-test/src/main/java/org/springframework/core/test/tools/SourceFile.java
  3. 27
      spring-core-test/src/test/java/org/springframework/core/test/tools/SourceFileTests.java

2
framework-platform/framework-platform.gradle

@ -41,7 +41,7 @@ dependencies {
api("com.sun.xml.bind:jaxb-core:3.0.2") api("com.sun.xml.bind:jaxb-core:3.0.2")
api("com.sun.xml.bind:jaxb-impl:3.0.2") api("com.sun.xml.bind:jaxb-impl:3.0.2")
api("com.sun.xml.bind:jaxb-xjc:3.0.2") api("com.sun.xml.bind:jaxb-xjc:3.0.2")
api("com.thoughtworks.qdox:qdox:2.0.1") api("com.thoughtworks.qdox:qdox:2.0.2")
api("com.thoughtworks.xstream:xstream:1.4.19") api("com.thoughtworks.xstream:xstream:1.4.19")
api("commons-io:commons-io:2.11.0") api("commons-io:commons-io:2.11.0")
api("de.bechte.junit:junit-hierarchicalcontextrunner:4.12.1") api("de.bechte.junit:junit-hierarchicalcontextrunner:4.12.1")

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

@ -171,6 +171,11 @@ public final class SourceFile extends DynamicFile implements AssertProvider<Sour
JavaProjectBuilder builder = new JavaProjectBuilder(); JavaProjectBuilder builder = new JavaProjectBuilder();
try { try {
JavaSource javaSource = builder.addSource(new StringReader(content)); JavaSource javaSource = builder.addSource(new StringReader(content));
if (javaSource.getClasses().isEmpty()) {
// QDOX doesn't let us inspect records yet, but we only need the
// class name so lets make the content look like a class
javaSource = builder.addSource(new StringReader(makeRecordsLookLikeClasses(content)));
}
Assert.state(javaSource.getClasses().size() == 1, "Source must define a single class"); Assert.state(javaSource.getClasses().size() == 1, "Source must define a single class");
JavaClass javaClass = javaSource.getClasses().get(0); JavaClass javaClass = javaSource.getClasses().get(0);
return (javaSource.getPackage() != null) return (javaSource.getPackage() != null)
@ -183,6 +188,10 @@ public final class SourceFile extends DynamicFile implements AssertProvider<Sour
} }
} }
private static String makeRecordsLookLikeClasses(String content) {
return content.replaceAll("record\\s(\\S+)\\(\\X+?\\)", "class $1");
}
/** /**
* AssertJ {@code assertThat} support. * AssertJ {@code assertThat} support.
* @deprecated use {@code assertThat(sourceFile)} rather than calling this * @deprecated use {@code assertThat(sourceFile)} rather than calling this

27
spring-core-test/src/test/java/org/springframework/core/test/tools/SourceFileTests.java

@ -119,6 +119,33 @@ class SourceFileTests {
assertThat(sourceFile.getContent()).isEqualTo(HELLO_WORLD); assertThat(sourceFile.getContent()).isEqualTo(HELLO_WORLD);
} }
@Test
void getClassNameFromSimpleRecord() {
SourceFile sourceFile = SourceFile.of("""
package com.example.helloworld;
record HelloWorld(String name) {
}
""");
assertThat(sourceFile.getClassName()).isEqualTo("com.example.helloworld.HelloWorld");
}
@Test
void getClassNameFromMoreComplexRecord() {
SourceFile sourceFile = SourceFile.of("""
package com.example.helloworld;
public record HelloWorld(String name) {
String getFoo() {
return name();
}
}
""");
assertThat(sourceFile.getClassName()).isEqualTo("com.example.helloworld.HelloWorld");
}
/** /**
* JavaPoet style API with a {@code writeTo} method. * JavaPoet style API with a {@code writeTo} method.
*/ */

Loading…
Cancel
Save