Skip to content

Commit

Permalink
Prepare AutoFactoryProcessorTest for future changes.
Browse files Browse the repository at this point in the history
We will shortly be supporting both `javax.inject` and `jakarta.inject`. The test here is being parameterized, though it only has one value for the parameter at the moment. With the `jakarta.inject` change, we will have several.

RELNOTES=n/a
PiperOrigin-RevId: 538848481
  • Loading branch information
eamonnmcmanus authored and Google Java Core Libraries committed Jun 8, 2023
1 parent d3a6beb commit f8955c9
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 28 deletions.
6 changes: 6 additions & 0 deletions factory/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@
<version>0.21.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.testparameterinjector</groupId>
<artifactId>test-parameter-injector</artifactId>
<version>1.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright 2013 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.auto.factory.processor;

import static com.google.testing.compile.CompilationSubject.assertThat;

import com.google.testing.compile.Compilation;
import com.google.testing.compile.Compiler;
import com.google.testing.compile.JavaFileObjects;
import javax.tools.JavaFileObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Testing compilation errors from {@link AutoFactoryProcessor}. */
@RunWith(JUnit4.class)
public class AutoFactoryProcessorNegativeTest {
private final Compiler javac = Compiler.javac().withProcessors(new AutoFactoryProcessor());

@Test
public void failsWithMixedFinals() {
JavaFileObject file = JavaFileObjects.forResource("bad/MixedFinals.java");
Compilation compilation = javac.compile(file);
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining(
"Cannot mix allowSubclasses=true and allowSubclasses=false in one factory.")
.inFile(file)
.onLine(24);
assertThat(compilation)
.hadErrorContaining(
"Cannot mix allowSubclasses=true and allowSubclasses=false in one factory.")
.inFile(file)
.onLine(27);
}

@Test
public void providedButNoAutoFactory() {
JavaFileObject file = JavaFileObjects.forResource("bad/ProvidedButNoAutoFactory.java");
Compilation compilation = javac.compile(file);
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining(
"@Provided may only be applied to constructors requesting an auto-factory")
.inFile(file)
.onLineContaining("@Provided");
}

@Test
public void providedOnMethodParameter() {
JavaFileObject file = JavaFileObjects.forResource("bad/ProvidedOnMethodParameter.java");
Compilation compilation = javac.compile(file);
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining("@Provided may only be applied to constructor parameters")
.inFile(file)
.onLineContaining("@Provided");
}

@Test
public void invalidCustomName() {
JavaFileObject file = JavaFileObjects.forResource("bad/InvalidCustomName.java");
Compilation compilation = javac.compile(file);
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining("\"SillyFactory!\" is not a valid Java identifier")
.inFile(file)
.onLineContaining("SillyFactory!");
}

@Test
public void factoryExtendingAbstractClass_withConstructorParams() {
JavaFileObject file =
JavaFileObjects.forResource("bad/FactoryExtendingAbstractClassWithConstructorParams.java");
Compilation compilation = javac.compile(file);
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining(
"tests.FactoryExtendingAbstractClassWithConstructorParams.AbstractFactory is not a"
+ " valid supertype for a factory. Factory supertypes must have a no-arg"
+ " constructor.")
.inFile(file)
.onLineContaining("@AutoFactory");
}

@Test
public void factoryExtendingInterface() {
JavaFileObject file = JavaFileObjects.forResource("bad/InterfaceSupertype.java");
Compilation compilation = javac.compile(file);
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining(
"java.lang.Runnable is not a valid supertype for a factory. Supertypes must be"
+ " non-final classes.")
.inFile(file)
.onLineContaining("@AutoFactory");
}

@Test
public void factoryExtendingEnum() {
JavaFileObject file = JavaFileObjects.forResource("bad/EnumSupertype.java");
Compilation compilation = javac.compile(file);
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining(
"java.util.concurrent.TimeUnit is not a valid supertype for a factory. Supertypes must"
+ " be non-final classes.")
.inFile(file)
.onLineContaining("@AutoFactory");
}

@Test
public void factoryExtendingFinalClass() {
JavaFileObject file = JavaFileObjects.forResource("bad/FinalSupertype.java");
Compilation compilation = javac.compile(file);
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining(
"java.lang.Boolean is not a valid supertype for a factory. Supertypes must be"
+ " non-final classes.")
.inFile(file)
.onLineContaining("@AutoFactory");
}
}
Loading

0 comments on commit f8955c9

Please sign in to comment.