Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
class CodeWarningsTests {

private static final TestCompiler TEST_COMPILER = TestCompiler.forSystem()
.withCompilerOptions("-Xlint:all", "-Werror");
.failOnDeprecationWarning();

private final CodeWarnings codeWarnings = new CodeWarnings();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ public TestCompiler failOnWarning() {
return withCompilerOptions("-Xlint:all", "-Werror");
}

/**
* Create a new {@link TestCompiler} instance that fails if a deprecation
* warning is encountered. This sets the {@code -Xlint:deprecation},
* {@code -Xlint:removal}, and {@code -Werror} compiler options.
* @return a new {@code TestCompiler} instance
* @since 7.1
* @see #withCompilerOptions(String...)
*/
public TestCompiler failOnDeprecationWarning() {
return withCompilerOptions("-Xlint:deprecation", "-Xlint:removal", "-Werror");
}

/**
* Compile content from this instance along with the additional provided
* content.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ public String get() {
}
""";

private static final String HELLO_DEPRECATED_FOR_REMOVAL = """
package com.example;

import java.util.function.Supplier;

public class Hello implements Supplier<String> {

@Deprecated(forRemoval = true)
public String get() {
return "Hello Deprecated";
}

}
""";

@Test
@SuppressWarnings("unchecked")
void compileWhenHasDifferentClassesWithSameClassNameCompilesBoth() {
Expand Down Expand Up @@ -212,6 +227,101 @@ public static void main(String[] args) {
});
}

@Test
void compileWhenSourceUseDeprecateCodeAndFailOnDeprecationWarningIsSet() {
SourceFile main = sourceFileUsingDeprecatedHello("""
public static void main(String[] args) {
new Hello().get();
}
""");
assertThatExceptionOfType(CompilationException.class).isThrownBy(
() -> TestCompiler.forSystem().failOnDeprecationWarning().withSources(
SourceFile.of(HELLO_DEPRECATED), main).compile(compiled -> {
})).satisfies(compilationException -> {
assertThat(compilationException.getProblems(Diagnostic.Kind.ERROR)).singleElement()
.satisfies(error -> assertThat(error.message())
.contains("-Werror"));
assertThat(compilationException.getProblems(Diagnostic.Kind.MANDATORY_WARNING)).singleElement()
.satisfies(warning -> assertThat(warning.message())
.contains("get()", "com.example.Hello"));
});
}

@Test
@SuppressWarnings("unchecked")
void compileWhenSourceUseDeprecateCodeAndFailOnDeprecationWarningWithSuppressWarnings() {
SourceFile main = sourceFileUsingDeprecatedHello("""
@SuppressWarnings("deprecation")
public static void main(String[] args) {
new Hello().get();
}
""");
TestCompiler.forSystem().failOnDeprecationWarning().withSources(
SourceFile.of(HELLO_DEPRECATED), main).compile(compiled -> {
Supplier<String> supplier = compiled.getInstance(Supplier.class,
"com.example.Hello");
assertThat(supplier.get()).isEqualTo("Hello Deprecated");
});
}

@Test
void compileWhenSourceExposesDeprecateTypeAndFailOnDeprecationWarningIsSet() {
SourceFile main = sourceFileUsingDeprecatedHello("""
public Hello getHello() {
return new Hello();
}
""");
assertThatExceptionOfType(CompilationException.class).isThrownBy(
() -> TestCompiler.forSystem().failOnDeprecationWarning().withSources(
SourceFile.of(HELLO_WORLD), main).compile(compiled -> {
})).satisfies(compilationException -> {
assertThat(compilationException.getProblems(Diagnostic.Kind.ERROR)).singleElement()
.satisfies(error -> assertThat(error.message())
.contains("-Werror"));
assertThat(compilationException.getProblems(Diagnostic.Kind.MANDATORY_WARNING))
.hasSize(2)
.allSatisfy(warning -> assertThat(warning.message())
.contains("Hello", "com.example"));
});
}

@Test
void compileWhenSourceUseDeprecateForRemovalCodeAndFailOnDeprecationWarningIsSet() {
SourceFile main = sourceFileUsingDeprecatedHello("""
public static void main(String[] args) {
new Hello().get();
}
""");
assertThatExceptionOfType(CompilationException.class).isThrownBy(
() -> TestCompiler.forSystem().failOnDeprecationWarning().withSources(
SourceFile.of(HELLO_DEPRECATED_FOR_REMOVAL), main).compile(compiled -> {
})).satisfies(compilationException -> {
assertThat(compilationException.getProblems(Diagnostic.Kind.ERROR)).singleElement()
.satisfies(error -> assertThat(error.message())
.contains("-Werror"));
assertThat(compilationException.getProblems(Diagnostic.Kind.MANDATORY_WARNING)).singleElement()
.satisfies(warning -> assertThat(warning.message())
.contains("get()", "com.example.Hello"));
});
}

@Test
@SuppressWarnings({ "removal", "unchecked" })
void compileWhenSourceUseDeprecateForRemovalCodeAndFailOnDeprecationWarningWithSuppressWarnings() {
SourceFile main = sourceFileUsingDeprecatedHello("""
@SuppressWarnings("removal")
public static void main(String[] args) {
new Hello().get();
}
""");
TestCompiler.forSystem().failOnDeprecationWarning().withSources(
SourceFile.of(HELLO_DEPRECATED_FOR_REMOVAL), main).compile(compiled -> {
Supplier<String> supplier = compiled.getInstance(Supplier.class,
"com.example.Hello");
assertThat(supplier.get()).isEqualTo("Hello Deprecated");
});
}

@Test
void withSourcesArrayAddsSource() {
SourceFile sourceFile = SourceFile.of(HELLO_WORLD);
Expand Down Expand Up @@ -398,6 +508,18 @@ private void assertHasResource(Compiled compiled) {
"META-INF/myfile")).hasContent("test");
}

private SourceFile sourceFileUsingDeprecatedHello(String method) {
return SourceFile.of("""
package com.example;

public class Main {

%s

}
""".formatted(method.indent(1)));
}

@SupportedAnnotationTypes("java.lang.Deprecated")
static class TestProcessor extends AbstractProcessor {

Expand Down