Skip to content

Commit 2f7ceda

Browse files
netdpbcgruber
authored andcommitted
Fixed BasicAnnotationProcessorTest.
Its logic to test that GeneratesCodeProcessor had run before RequiresGeneratedCodeProcessor was incorrect. Specifically: 1. Filer.getResource() does not throw for nonexisting files if the location is the output location. 2. The resource to test should have a ".java" extension. 3. The filer fails if you even try to read a resource that exists. Instead, we just keep track of whether the GeneratesCodeProcessor has run on elements and check that explicitly. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=92832468
1 parent 6634209 commit 2f7ceda

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

common/src/test/java/com/google/auto/common/BasicAnnotationProcessorTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.common.collect.ImmutableList;
2323
import com.google.common.collect.ImmutableSet;
2424
import com.google.common.collect.SetMultimap;
25+
import com.google.common.collect.Sets;
2526
import com.google.testing.compile.JavaFileObjects;
2627

2728
import org.junit.Test;
@@ -38,15 +39,17 @@
3839
import javax.lang.model.SourceVersion;
3940
import javax.lang.model.element.Element;
4041
import javax.tools.JavaFileObject;
41-
import javax.tools.StandardLocation;
4242

4343
@RunWith(JUnit4.class)
4444
public class BasicAnnotationProcessorTest {
45+
46+
private Set<Element> elementsGeneratingCode = Sets.newHashSet();
47+
4548
@Retention(RetentionPolicy.SOURCE)
4649
public @interface RequiresGeneratedCode {}
4750

4851
/** Asserts that the code generated by {@link GeneratesCode} and its processor is present. */
49-
public static class RequiresGeneratedCodeProcessor extends BasicAnnotationProcessor {
52+
public class RequiresGeneratedCodeProcessor extends BasicAnnotationProcessor {
5053
boolean processed = false;
5154

5255
@Override
@@ -61,12 +64,7 @@ protected Iterable<? extends ProcessingStep> initSteps() {
6164
public void process(
6265
SetMultimap<Class<? extends Annotation>, Element> elementsByAnnotation) {
6366
processed = true;
64-
try {
65-
processingEnv.getFiler()
66-
.getResource(StandardLocation.SOURCE_OUTPUT, "test", "SomeGeneratedClass");
67-
} catch (IOException e) {
68-
throw new AssertionError(e);
69-
}
67+
assertThat(elementsGeneratingCode).isNotEmpty();
7068
}
7169

7270
@Override
@@ -81,7 +79,8 @@ public Set<? extends Class<? extends Annotation>> annotations() {
8179
public @interface GeneratesCode {}
8280

8381
/** Generates a class called {@code test.SomeGeneratedClass}. */
84-
public static class GeneratesCodeProcessor extends BasicAnnotationProcessor {
82+
public class GeneratesCodeProcessor extends BasicAnnotationProcessor {
83+
8584
@Override
8685
public SourceVersion getSupportedSourceVersion() {
8786
return SourceVersion.latestSupported();
@@ -109,7 +108,9 @@ public Set<? extends Class<? extends Annotation>> annotations() {
109108
});
110109
}
111110

111+
// TODO(gak): Use jimfs to simulate the file system.
112112
private void generateClass(Element sourceType) throws IOException {
113+
elementsGeneratingCode.add(sourceType);
113114
JavaFileObject source =
114115
processingEnv.getFiler().createSourceFile("test.SomeGeneratedClass", sourceType);
115116
PrintWriter writer = new PrintWriter(source.openWriter());

0 commit comments

Comments
 (0)