Skip to content

Commit

Permalink
Extract a field for the compiler. Also reuse a local variable.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 532527930
  • Loading branch information
netdpb authored and Google Java Core Libraries committed May 16, 2023
1 parent 591731c commit 2fecb7f
Showing 1 changed file with 28 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@
/** Tests the {@link AutoServiceProcessor}. */
@RunWith(JUnit4.class)
public class AutoServiceProcessorTest {
private final Compiler compiler = Compiler.javac().withProcessors(new AutoServiceProcessor());

@Test
public void autoService() {
Compilation compilation =
Compiler.javac()
.withProcessors(new AutoServiceProcessor())
.compile(
JavaFileObjects.forResource("test/SomeService.java"),
JavaFileObjects.forResource("test/SomeServiceProvider1.java"),
JavaFileObjects.forResource("test/SomeServiceProvider2.java"),
JavaFileObjects.forResource("test/Enclosing.java"),
JavaFileObjects.forResource("test/AnotherService.java"),
JavaFileObjects.forResource("test/AnotherServiceProvider.java"));
compiler.compile(
JavaFileObjects.forResource("test/SomeService.java"),
JavaFileObjects.forResource("test/SomeServiceProvider1.java"),
JavaFileObjects.forResource("test/SomeServiceProvider2.java"),
JavaFileObjects.forResource("test/Enclosing.java"),
JavaFileObjects.forResource("test/AnotherService.java"),
JavaFileObjects.forResource("test/AnotherServiceProvider.java"));
assertThat(compilation).succeededWithoutWarnings();
assertThat(compilation)
.generatedFile(StandardLocation.CLASS_OUTPUT, "META-INF/services/test.SomeService")
Expand All @@ -58,12 +58,10 @@ public void autoService() {
@Test
public void multiService() {
Compilation compilation =
Compiler.javac()
.withProcessors(new AutoServiceProcessor())
.compile(
JavaFileObjects.forResource("test/SomeService.java"),
JavaFileObjects.forResource("test/AnotherService.java"),
JavaFileObjects.forResource("test/MultiServiceProvider.java"));
compiler.compile(
JavaFileObjects.forResource("test/SomeService.java"),
JavaFileObjects.forResource("test/AnotherService.java"),
JavaFileObjects.forResource("test/MultiServiceProvider.java"));
assertThat(compilation).succeededWithoutWarnings();
// We have @AutoService({SomeService.class, AnotherService.class}) class MultiServiceProvider.
// So we expect META-INF/services/test.SomeService with contents that name MultiServiceProvider
Expand All @@ -80,21 +78,15 @@ public void multiService() {

@Test
public void badMultiService() {
Compilation compilation =
Compiler.javac()
.withProcessors(new AutoServiceProcessor())
.compile(JavaFileObjects.forResource("test/NoServices.java"));
Compilation compilation = compiler.compile(JavaFileObjects.forResource("test/NoServices.java"));
assertThat(compilation).failed();
assertThat(compilation).hadErrorContaining(MISSING_SERVICES_ERROR);
}

@Test
public void doesNotImplement_failsByDefault() {
Compilation compilation =
Compiler.javac()
.withProcessors(new AutoServiceProcessor())
.compile(
JavaFileObjects.forResource("test/DoesNotImplement.java"));
compiler.compile(JavaFileObjects.forResource("test/DoesNotImplement.java"));
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining("test.DoesNotImplement does not implement test.SomeService");
Expand All @@ -103,8 +95,7 @@ public void doesNotImplement_failsByDefault() {
@Test
public void doesNotImplement_succeedsWithVerifyFalse() {
Compilation compilation =
Compiler.javac()
.withProcessors(new AutoServiceProcessor())
compiler
.withOptions("-Averify=false")
.compile(
JavaFileObjects.forResource("test/DoesNotImplement.java"),
Expand All @@ -119,11 +110,9 @@ public void doesNotImplement_succeedsWithVerifyFalse() {
@Test
public void doesNotImplement_suppressed() {
Compilation compilation =
Compiler.javac()
.withProcessors(new AutoServiceProcessor())
.compile(
JavaFileObjects.forResource("test/DoesNotImplementSuppressed.java"),
JavaFileObjects.forResource("test/SomeService.java"));
compiler.compile(
JavaFileObjects.forResource("test/DoesNotImplementSuppressed.java"),
JavaFileObjects.forResource("test/SomeService.java"));
assertThat(compilation).succeededWithoutWarnings();
assertThat(compilation)
.generatedFile(StandardLocation.CLASS_OUTPUT, "META-INF/services/test.SomeService")
Expand All @@ -134,8 +123,7 @@ public void doesNotImplement_suppressed() {
@Test
public void generic() {
Compilation compilation =
Compiler.javac()
.withProcessors(new AutoServiceProcessor())
compiler
.withOptions("-Averify=false")
.compile(
JavaFileObjects.forResource("test/GenericService.java"),
Expand All @@ -150,11 +138,9 @@ public void generic() {
@Test
public void genericWithNoVerifyOption() {
Compilation compilation =
Compiler.javac()
.withProcessors(new AutoServiceProcessor())
.compile(
JavaFileObjects.forResource("test/GenericService.java"),
JavaFileObjects.forResource("test/GenericServiceProvider.java"));
compiler.compile(
JavaFileObjects.forResource("test/GenericService.java"),
JavaFileObjects.forResource("test/GenericServiceProvider.java"));
assertThat(compilation).succeeded();
assertThat(compilation)
.hadWarningContaining(
Expand All @@ -165,8 +151,7 @@ public void genericWithNoVerifyOption() {
@Test
public void genericWithExplicitVerify() {
Compilation compilation =
Compiler.javac()
.withProcessors(new AutoServiceProcessor())
compiler
.withOptions("-Averify=true")
.compile(
JavaFileObjects.forResource("test/GenericService.java"),
Expand All @@ -181,8 +166,7 @@ public void genericWithExplicitVerify() {
@Test
public void genericWithVerifyOptionAndSuppressWarings() {
Compilation compilation =
Compiler.javac()
.withProcessors(new AutoServiceProcessor())
compiler
.withOptions("-Averify=true")
.compile(
JavaFileObjects.forResource("test/GenericService.java"),
Expand All @@ -193,8 +177,7 @@ public void genericWithVerifyOptionAndSuppressWarings() {
@Test
public void nestedGenericWithVerifyOptionAndSuppressWarnings() {
Compilation compilation =
Compiler.javac()
.withProcessors(new AutoServiceProcessor())
compiler
.withOptions("-Averify=true")
.compile(
JavaFileObjects.forResource("test/GenericService.java"),
Expand All @@ -210,8 +193,7 @@ public void nestedGenericWithVerifyOptionAndSuppressWarnings() {
public void missing() {
AutoServiceProcessor processor = new AutoServiceProcessor();
Compilation compilation =
Compiler.javac()
.withProcessors(processor)
compiler
.withOptions("-Averify=true")
.compile(
JavaFileObjects.forResource(
Expand Down Expand Up @@ -246,7 +228,7 @@ public void autoServiceOnAbstractClass() {
Compiler.javac()
.withProcessors(processor)
.withOptions("-Averify=true")
.compile(JavaFileObjects.forResource("test/AutoServiceOnAbstractClass.java"));
.compile(autoServiceOnAbstractClass);
assertThat(compilation)
.hadErrorContaining("@AutoService cannot be applied to an abstract class or an interface")
.inFile(autoServiceOnAbstractClass)
Expand Down

0 comments on commit 2fecb7f

Please sign in to comment.