Skip to content

Commit

Permalink
Fixed BasicAnnotationProcessorTest.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
netdpb authored and cgruber committed May 6, 2015
1 parent 6634209 commit 2f7ceda
Showing 1 changed file with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import com.google.testing.compile.JavaFileObjects;

import org.junit.Test;
Expand All @@ -38,15 +39,17 @@
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;

@RunWith(JUnit4.class)
public class BasicAnnotationProcessorTest {

private Set<Element> elementsGeneratingCode = Sets.newHashSet();

@Retention(RetentionPolicy.SOURCE)
public @interface RequiresGeneratedCode {}

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

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

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

/** Generates a class called {@code test.SomeGeneratedClass}. */
public static class GeneratesCodeProcessor extends BasicAnnotationProcessor {
public class GeneratesCodeProcessor extends BasicAnnotationProcessor {

@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
Expand Down Expand Up @@ -109,7 +108,9 @@ public Set<? extends Class<? extends Annotation>> annotations() {
});
}

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

0 comments on commit 2f7ceda

Please # to comment.