Relax test that used to rely on localized message

This reverts commit 93206c3f6e and updates
the related test to only rely on the fact the compiler fails. Relying
on a message will not work and the status code can be implementation
independent according to its javadoc.

Closes gh-31536
This commit is contained in:
Stéphane Nicoll 2023-11-01 16:23:01 +01:00
parent a5841fede5
commit 1d38d649fa
2 changed files with 31 additions and 58 deletions

View File

@ -53,8 +53,6 @@ public final class TestCompiler {
private final JavaCompiler compiler; private final JavaCompiler compiler;
private final Locale locale;
private final SourceFiles sourceFiles; private final SourceFiles sourceFiles;
private final ResourceFiles resourceFiles; private final ResourceFiles resourceFiles;
@ -66,13 +64,12 @@ public final class TestCompiler {
private final List<String> compilerOptions; private final List<String> compilerOptions;
private TestCompiler(@Nullable ClassLoader classLoader, JavaCompiler compiler, Locale locale, private TestCompiler(@Nullable ClassLoader classLoader, JavaCompiler compiler,
SourceFiles sourceFiles, ResourceFiles resourceFiles, ClassFiles classFiles, List<Processor> processors, SourceFiles sourceFiles, ResourceFiles resourceFiles, ClassFiles classFiles,
List<String> compilerOptions) { List<Processor> processors, List<String> compilerOptions) {
this.classLoader = classLoader; this.classLoader = classLoader;
this.compiler = compiler; this.compiler = compiler;
this.locale = locale;
this.sourceFiles = sourceFiles; this.sourceFiles = sourceFiles;
this.resourceFiles = resourceFiles; this.resourceFiles = resourceFiles;
this.classFiles = classFiles; this.classFiles = classFiles;
@ -95,9 +92,8 @@ public final class TestCompiler {
* @return a new {@code TestCompiler} instance * @return a new {@code TestCompiler} instance
*/ */
public static TestCompiler forCompiler(JavaCompiler javaCompiler) { public static TestCompiler forCompiler(JavaCompiler javaCompiler) {
return new TestCompiler(null, javaCompiler, Locale.getDefault(), return new TestCompiler(null, javaCompiler, SourceFiles.none(),
SourceFiles.none(), ResourceFiles.none(), ResourceFiles.none(), ClassFiles.none(), Collections.emptyList(), Collections.emptyList());
ClassFiles.none(), Collections.emptyList(), Collections.emptyList());
} }
/** /**
@ -109,26 +105,13 @@ public final class TestCompiler {
return customizer.apply(this); return customizer.apply(this);
} }
/**
* Create a new {@code TestCompiler} instance that uses the specified {@link Locale}
* to render compiler messages.
* @param locale the locale to use
* @return a new {@code TestCompiler} instance
* @since 6.1
*/
public TestCompiler withLocale(Locale locale) {
return new TestCompiler(this.classLoader, this.compiler, locale,
this.sourceFiles, this.resourceFiles,
this.classFiles, this.processors, this.compilerOptions);
}
/** /**
* Create a new {@code TestCompiler} instance with additional source files. * Create a new {@code TestCompiler} instance with additional source files.
* @param sourceFiles the additional source files * @param sourceFiles the additional source files
* @return a new {@code TestCompiler} instance * @return a new {@code TestCompiler} instance
*/ */
public TestCompiler withSources(SourceFile... sourceFiles) { public TestCompiler withSources(SourceFile... sourceFiles) {
return new TestCompiler(this.classLoader, this.compiler, this.locale, return new TestCompiler(this.classLoader, this.compiler,
this.sourceFiles.and(sourceFiles), this.resourceFiles, this.sourceFiles.and(sourceFiles), this.resourceFiles,
this.classFiles, this.processors, this.compilerOptions); this.classFiles, this.processors, this.compilerOptions);
} }
@ -139,7 +122,7 @@ public final class TestCompiler {
* @return a new {@code TestCompiler} instance * @return a new {@code TestCompiler} instance
*/ */
public TestCompiler withSources(Iterable<SourceFile> sourceFiles) { public TestCompiler withSources(Iterable<SourceFile> sourceFiles) {
return new TestCompiler(this.classLoader, this.compiler, this.locale, return new TestCompiler(this.classLoader, this.compiler,
this.sourceFiles.and(sourceFiles), this.resourceFiles, this.sourceFiles.and(sourceFiles), this.resourceFiles,
this.classFiles, this.processors, this.compilerOptions); this.classFiles, this.processors, this.compilerOptions);
} }
@ -150,7 +133,7 @@ public final class TestCompiler {
* @return a new {@code TestCompiler} instance * @return a new {@code TestCompiler} instance
*/ */
public TestCompiler withSources(SourceFiles sourceFiles) { public TestCompiler withSources(SourceFiles sourceFiles) {
return new TestCompiler(this.classLoader, this.compiler, this.locale, return new TestCompiler(this.classLoader, this.compiler,
this.sourceFiles.and(sourceFiles), this.resourceFiles, this.sourceFiles.and(sourceFiles), this.resourceFiles,
this.classFiles, this.processors, this.compilerOptions); this.classFiles, this.processors, this.compilerOptions);
} }
@ -161,9 +144,9 @@ public final class TestCompiler {
* @return a new {@code TestCompiler} instance * @return a new {@code TestCompiler} instance
*/ */
public TestCompiler withResources(ResourceFile... resourceFiles) { public TestCompiler withResources(ResourceFile... resourceFiles) {
return new TestCompiler(this.classLoader, this.compiler, this.locale, return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles,
this.sourceFiles, this.resourceFiles.and(resourceFiles), this.resourceFiles.and(resourceFiles), this.classFiles, this.processors,
this.classFiles, this.processors, this.compilerOptions); this.compilerOptions);
} }
/** /**
@ -172,9 +155,9 @@ public final class TestCompiler {
* @return a new {@code TestCompiler} instance * @return a new {@code TestCompiler} instance
*/ */
public TestCompiler withResources(Iterable<ResourceFile> resourceFiles) { public TestCompiler withResources(Iterable<ResourceFile> resourceFiles) {
return new TestCompiler(this.classLoader, this.compiler, this.locale, return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles,
this.sourceFiles, this.resourceFiles.and(resourceFiles), this.resourceFiles.and(resourceFiles), this.classFiles, this.processors,
this.classFiles, this.processors, this.compilerOptions); this.compilerOptions);
} }
/** /**
@ -183,9 +166,9 @@ public final class TestCompiler {
* @return a new {@code TestCompiler} instance * @return a new {@code TestCompiler} instance
*/ */
public TestCompiler withResources(ResourceFiles resourceFiles) { public TestCompiler withResources(ResourceFiles resourceFiles) {
return new TestCompiler(this.classLoader, this.compiler, this.locale, return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles,
this.sourceFiles, this.resourceFiles.and(resourceFiles), this.resourceFiles.and(resourceFiles), this.classFiles, this.processors,
this.classFiles, this.processors, this.compilerOptions); this.compilerOptions);
} }
/** /**
@ -194,9 +177,9 @@ public final class TestCompiler {
* @return a new {@code TestCompiler} instance * @return a new {@code TestCompiler} instance
*/ */
public TestCompiler withClasses(Iterable<ClassFile> classFiles) { public TestCompiler withClasses(Iterable<ClassFile> classFiles) {
return new TestCompiler(this.classLoader, this.compiler, this.locale, return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles,
this.sourceFiles, this.resourceFiles, this.classFiles.and(classFiles), this.resourceFiles, this.classFiles.and(classFiles), this.processors,
this.processors, this.compilerOptions); this.compilerOptions);
} }
/** /**
@ -207,9 +190,8 @@ public final class TestCompiler {
public TestCompiler withProcessors(Processor... processors) { public TestCompiler withProcessors(Processor... processors) {
List<Processor> mergedProcessors = new ArrayList<>(this.processors); List<Processor> mergedProcessors = new ArrayList<>(this.processors);
mergedProcessors.addAll(Arrays.asList(processors)); mergedProcessors.addAll(Arrays.asList(processors));
return new TestCompiler(this.classLoader, this.compiler, this.locale, return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles,
this.sourceFiles, this.resourceFiles, this.classFiles, mergedProcessors, this.resourceFiles, this.classFiles, mergedProcessors, this.compilerOptions);
this.compilerOptions);
} }
/** /**
@ -220,9 +202,8 @@ public final class TestCompiler {
public TestCompiler withProcessors(Iterable<Processor> processors) { public TestCompiler withProcessors(Iterable<Processor> processors) {
List<Processor> mergedProcessors = new ArrayList<>(this.processors); List<Processor> mergedProcessors = new ArrayList<>(this.processors);
processors.forEach(mergedProcessors::add); processors.forEach(mergedProcessors::add);
return new TestCompiler(this.classLoader, this.compiler, this.locale, return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles,
this.sourceFiles, this.resourceFiles, this.classFiles, this.resourceFiles, this.classFiles, mergedProcessors, this.compilerOptions);
mergedProcessors, this.compilerOptions);
} }
/** /**
@ -234,9 +215,8 @@ public final class TestCompiler {
public TestCompiler withCompilerOptions(String... options) { public TestCompiler withCompilerOptions(String... options) {
List<String> mergedCompilerOptions = Stream.concat(this.compilerOptions.stream(), List<String> mergedCompilerOptions = Stream.concat(this.compilerOptions.stream(),
Arrays.stream(options)).distinct().toList(); Arrays.stream(options)).distinct().toList();
return new TestCompiler(this.classLoader, this.compiler, this.locale, return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles,
this.sourceFiles, this.resourceFiles, this.classFiles, this.resourceFiles, this.classFiles, this.processors, mergedCompilerOptions);
this.processors, mergedCompilerOptions);
} }
/** /**
@ -328,7 +308,7 @@ public final class TestCompiler {
DynamicJavaFileManager fileManager = new DynamicJavaFileManager( DynamicJavaFileManager fileManager = new DynamicJavaFileManager(
standardFileManager, classLoaderToUse, this.classFiles, this.resourceFiles); standardFileManager, classLoaderToUse, this.classFiles, this.resourceFiles);
if (!this.sourceFiles.isEmpty()) { if (!this.sourceFiles.isEmpty()) {
Errors errors = new Errors(this.locale); Errors errors = new Errors();
CompilationTask task = this.compiler.getTask(null, fileManager, errors, CompilationTask task = this.compiler.getTask(null, fileManager, errors,
this.compilerOptions, null, compilationUnits); this.compilerOptions, null, compilationUnits);
if (!this.processors.isEmpty()) { if (!this.processors.isEmpty()) {
@ -369,19 +349,13 @@ public final class TestCompiler {
*/ */
static class Errors implements DiagnosticListener<JavaFileObject> { static class Errors implements DiagnosticListener<JavaFileObject> {
private final Locale locale;
private final StringBuilder message = new StringBuilder(); private final StringBuilder message = new StringBuilder();
Errors(Locale locale) {
this.locale = locale;
}
@Override @Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) { public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
this.message.append('\n'); this.message.append('\n');
this.message.append(diagnostic.getMessage(this.locale)); this.message.append(diagnostic.getMessage(Locale.getDefault()));
if (diagnostic.getSource() != null) { if (diagnostic.getSource() != null) {
this.message.append(' '); this.message.append(' ');
this.message.append(diagnostic.getSource().getName()); this.message.append(diagnostic.getSource().getName());

View File

@ -19,7 +19,6 @@ package org.springframework.core.test.tools;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -171,9 +170,9 @@ class TestCompilerTests {
} }
"""); """);
assertThatExceptionOfType(CompilationException.class).isThrownBy( assertThatExceptionOfType(CompilationException.class).isThrownBy(
() -> TestCompiler.forSystem().failOnWarning().withLocale(Locale.ENGLISH) () -> TestCompiler.forSystem().failOnWarning().withSources(
.withSources(SourceFile.of(HELLO_DEPRECATED), main).compile(compiled -> { SourceFile.of(HELLO_DEPRECATED), main).compile(compiled -> {
})).withMessageContaining("warnings found and -Werror specified"); }));
} }
@Test @Test