diff options
author | Christian Edward Gruber <cgruber@google.com> | 2014-07-18 16:03:26 -0700 |
---|---|---|
committer | Christian Edward Gruber <cgruber@google.com> | 2014-07-18 16:22:45 -0700 |
commit | 4874fbf0c227a62a90af31f37ad2753e6a0adf80 (patch) | |
tree | f8dbe5b519aba962220f00ba1ff2a2575f8f27a4 /common/src/test/java/com | |
parent | 88a2404048aca1c83cf8cd554e394c6234bfe620 (diff) | |
download | auto-4874fbf0c227a62a90af31f37ad2753e6a0adf80.tar.gz |
Add a utility that does quick, superficial validation on elements to ensure that all type information is present while running a processor.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=70831442
Diffstat (limited to 'common/src/test/java/com')
-rw-r--r-- | common/src/test/java/com/google/auto/common/SuperficialValidationTest.java | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/common/src/test/java/com/google/auto/common/SuperficialValidationTest.java b/common/src/test/java/com/google/auto/common/SuperficialValidationTest.java new file mode 100644 index 00000000..b886cae8 --- /dev/null +++ b/common/src/test/java/com/google/auto/common/SuperficialValidationTest.java @@ -0,0 +1,157 @@ +/* + * Copyright (C) 2014 Google, Inc. + * + * 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.common; + +import com.google.common.collect.ImmutableSet; +import com.google.testing.compile.JavaFileObjects; +import java.util.Set; +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.RoundEnvironment; +import javax.lang.model.element.TypeElement; +import javax.tools.JavaFileObject; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import static com.google.common.truth.Truth.assert_; +import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource; + +@RunWith(JUnit4.class) +public class SuperficialValidationTest { + @Test + public void missingReturnType() { + JavaFileObject javaFileObject = JavaFileObjects.forSourceLines( + "test.TestClass", + "package test;", + "", + "abstract class TestClass {", + " abstract MissingType blah();", + "}"); + assert_().about(javaSource()) + .that(javaFileObject) + .processedWith(new AssertingProcessor() { + @Override void runAssertions() { + TypeElement testClassElement = + processingEnv.getElementUtils().getTypeElement("test.TestClass"); + assert_().that(SuperficialValidation.validateElement(testClassElement)).isFalse(); + } + }) + .failsToCompile(); + } + + @Test + public void missingGenericReturnType() { + JavaFileObject javaFileObject = JavaFileObjects.forSourceLines( + "test.TestClass", + "package test;", + "", + "abstract class TestClass {", + " abstract MissingType<?> blah();", + "}"); + assert_().about(javaSource()) + .that(javaFileObject) + .processedWith(new AssertingProcessor() { + @Override void runAssertions() { + TypeElement testClassElement = + processingEnv.getElementUtils().getTypeElement("test.TestClass"); + assert_().that(SuperficialValidation.validateElement(testClassElement)).isFalse(); + } + }) + .failsToCompile(); + } + + @Test + public void missingReturnTypeTypeParameter() { + JavaFileObject javaFileObject = JavaFileObjects.forSourceLines( + "test.TestClass", + "package test;", + "", + "import java.util.Map;", + "import java.util.Set;", + "", + "abstract class TestClass {", + " abstract Map<Set<?>, MissingType<?>> blah();", + "}"); + assert_().about(javaSource()) + .that(javaFileObject) + .processedWith(new AssertingProcessor() { + @Override void runAssertions() { + TypeElement testClassElement = + processingEnv.getElementUtils().getTypeElement("test.TestClass"); + assert_().that(SuperficialValidation.validateElement(testClassElement)).isFalse(); + } + }) + .failsToCompile(); + } + + @Test + public void missingTypeParameter() { + JavaFileObject javaFileObject = JavaFileObjects.forSourceLines( + "test.TestClass", + "package test;", + "", + "class TestClass<T extends MissingType> {}"); + assert_().about(javaSource()) + .that(javaFileObject) + .processedWith(new AssertingProcessor() { + @Override void runAssertions() { + TypeElement testClassElement = + processingEnv.getElementUtils().getTypeElement("test.TestClass"); + assert_().that(SuperficialValidation.validateElement(testClassElement)).isFalse(); + } + }) + .failsToCompile(); + } + + @Test + public void missingAnnotation() { + JavaFileObject javaFileObject = JavaFileObjects.forSourceLines( + "test.TestClass", + "package test;", + "", + "@MissingAnnotation", + "class TestClass {}"); + assert_().about(javaSource()) + .that(javaFileObject) + .processedWith(new AssertingProcessor() { + @Override void runAssertions() { + TypeElement testClassElement = + processingEnv.getElementUtils().getTypeElement("test.TestClass"); + assert_().that(SuperficialValidation.validateElement(testClassElement)).isFalse(); + } + }) + .failsToCompile(); + } + + private static abstract class AssertingProcessor extends AbstractProcessor { + @Override + public Set<String> getSupportedAnnotationTypes() { + return ImmutableSet.of("*"); + } + + @Override + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { + try { + runAssertions(); + } catch (Exception e) { + throw new RuntimeException(e); + } + return false; + } + + abstract void runAssertions() throws Exception; + } +} |