Polish TestCompiler support

This commit is contained in:
Sam Brannen 2022-07-30 10:32:47 +03:00
parent 11d790c6eb
commit 3912ef1507
8 changed files with 16 additions and 17 deletions

View File

@ -37,5 +37,4 @@ import org.junit.jupiter.api.extension.ExtendWith;
@Documented
@ExtendWith(CompileWithTargetClassAccessExtension.class)
public @interface CompileWithTargetClassAccess {
}

View File

@ -63,6 +63,7 @@ final class CompileWithTargetClassAccessClassLoader extends ClassLoader {
}
// Invoked reflectively by DynamicClassLoader.findDefineClassMethod(ClassLoader)
Class<?> defineClassWithTargetAccess(String name, byte[] b, int off, int len) {
return super.defineClass(name, b, off, len);
}

View File

@ -161,6 +161,7 @@ class CompileWithTargetClassAccessExtension implements InvocationInterceptor {
}
@FunctionalInterface
interface Action {
static Action NONE = () -> {

View File

@ -36,7 +36,6 @@ import org.springframework.util.Assert;
*/
public class Compiled {
private final ClassLoader classLoader;
private final SourceFiles sourceFiles;
@ -47,8 +46,7 @@ public class Compiled {
private List<Class<?>> compiledClasses;
Compiled(ClassLoader classLoader, SourceFiles sourceFiles,
ResourceFiles resourceFiles) {
Compiled(ClassLoader classLoader, SourceFiles sourceFiles, ResourceFiles resourceFiles) {
this.classLoader = classLoader;
this.sourceFiles = sourceFiles;
this.resourceFiles = resourceFiles;

View File

@ -35,8 +35,7 @@ class DynamicClassFileObject extends SimpleJavaFileObject {
DynamicClassFileObject(String className) {
super(URI.create("class:///" + className.replace('.', '/') + ".class"),
Kind.CLASS);
super(URI.create("class:///" + className.replace('.', '/') + ".class"), Kind.CLASS);
}

View File

@ -184,7 +184,6 @@ public class DynamicClassLoader extends ClassLoader {
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(
this.file.getContent().getBytes(StandardCharsets.UTF_8));
}
}

View File

@ -103,12 +103,11 @@ public final class TestCompiler {
*/
public TestCompiler withFiles(InMemoryGeneratedFiles generatedFiles) {
List<SourceFile> sourceFiles = new ArrayList<>();
generatedFiles.getGeneratedFiles(Kind.SOURCE).forEach((path,
inputStreamSource) -> sourceFiles.add(SourceFile.of(inputStreamSource)));
generatedFiles.getGeneratedFiles(Kind.SOURCE).forEach(
(path, inputStreamSource) -> sourceFiles.add(SourceFile.of(inputStreamSource)));
List<ResourceFile> resourceFiles = new ArrayList<>();
generatedFiles.getGeneratedFiles(Kind.RESOURCE)
.forEach((path, inputStreamSource) -> resourceFiles
.add(ResourceFile.of(path, inputStreamSource)));
generatedFiles.getGeneratedFiles(Kind.RESOURCE).forEach(
(path, inputStreamSource) -> resourceFiles.add(ResourceFile.of(path, inputStreamSource)));
return withSources(sourceFiles).withResources(resourceFiles);
}
@ -256,7 +255,7 @@ public final class TestCompiler {
}
catch (IllegalAccessError ex) {
throw new IllegalAccessError(ex.getMessage() + ". " +
"For non-public access ensure annotate your tests with @CompileWithTargetClassAccess");
"For non-public access ensure you annotate your test class or test method with @CompileWithTargetClassAccess");
}
finally {
Thread.currentThread().setContextClassLoader(previousClassLoader);
@ -318,12 +317,12 @@ public final class TestCompiler {
@Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
this.message.append("\n");
this.message.append('\n');
this.message.append(diagnostic.getMessage(Locale.getDefault()));
this.message.append(" ");
this.message.append(' ');
this.message.append(diagnostic.getSource().getName());
this.message.append(" ");
this.message.append(diagnostic.getLineNumber()).append(":")
this.message.append(' ');
this.message.append(diagnostic.getLineNumber()).append(':')
.append(diagnostic.getColumnNumber());
}
}

View File

@ -42,6 +42,8 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for {@link TestCompiler}.
*
* @since 6.0
* @author Phillip Webb
* @author Andy Wilkinson
* @author Scott Frederick
@ -89,6 +91,7 @@ class TestCompilerTests {
}
""";
@Test
@SuppressWarnings("unchecked")
void compileWhenHasDifferentClassesWithSameClassNameCompilesBoth() {