|
| 1 | +/* |
| 2 | + * Copyright (C) 2014 Google, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.testing.compile; |
| 17 | + |
| 18 | +import static com.google.common.base.Preconditions.checkState; |
| 19 | + |
| 20 | +import com.google.common.collect.ImmutableList; |
| 21 | +import com.google.common.collect.ImmutableSet; |
| 22 | +import com.google.testing.compile.Compilation.Result; |
| 23 | + |
| 24 | +import org.junit.Rule; |
| 25 | +import org.junit.rules.TestRule; |
| 26 | +import org.junit.runner.Description; |
| 27 | +import org.junit.runners.JUnit4; |
| 28 | +import org.junit.runners.model.Statement; |
| 29 | + |
| 30 | +import java.util.Set; |
| 31 | +import java.util.concurrent.atomic.AtomicReference; |
| 32 | + |
| 33 | +import javax.annotation.processing.AbstractProcessor; |
| 34 | +import javax.annotation.processing.ProcessingEnvironment; |
| 35 | +import javax.annotation.processing.RoundEnvironment; |
| 36 | +import javax.lang.model.SourceVersion; |
| 37 | +import javax.lang.model.element.TypeElement; |
| 38 | +import javax.lang.model.util.Elements; |
| 39 | +import javax.lang.model.util.Types; |
| 40 | + |
| 41 | +/** |
| 42 | + * A {@link JUnit4} {@link Rule} that executes tests such that a instances of {@link Elements} and |
| 43 | + * {@link Types} are available during execution. |
| 44 | + * |
| 45 | + * <p>To use this rule in a test, just add the following field: <pre> {@code |
| 46 | + * @Rule public CompilationRule compilationRule = new CompilationRule();} |
| 47 | + * |
| 48 | + * @author Gregory Kick |
| 49 | + */ |
| 50 | +public final class CompilationRule implements TestRule { |
| 51 | + private Elements elements; |
| 52 | + private Types types; |
| 53 | + |
| 54 | + @Override |
| 55 | + public Statement apply(final Statement base, Description description) { |
| 56 | + return new Statement() { |
| 57 | + @Override public void evaluate() throws Throwable { |
| 58 | + final AtomicReference<Throwable> thrown = new AtomicReference<Throwable>(); |
| 59 | + Result result = Compilation.compile(ImmutableList.of(new AbstractProcessor() { |
| 60 | + @Override |
| 61 | + public SourceVersion getSupportedSourceVersion() { |
| 62 | + return SourceVersion.latest(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Set<String> getSupportedAnnotationTypes() { |
| 67 | + return ImmutableSet.of("*"); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public synchronized void init(ProcessingEnvironment processingEnv) { |
| 72 | + super.init(processingEnv); |
| 73 | + elements = processingEnv.getElementUtils(); |
| 74 | + types = processingEnv.getTypeUtils(); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public boolean process(Set<? extends TypeElement> annotations, |
| 79 | + RoundEnvironment roundEnv) { |
| 80 | + // just run the test on the last round after compilation is over |
| 81 | + if (roundEnv.processingOver()) { |
| 82 | + try { |
| 83 | + base.evaluate(); |
| 84 | + } catch (Throwable e) { |
| 85 | + thrown.set(e); |
| 86 | + } |
| 87 | + } |
| 88 | + return false; |
| 89 | + } |
| 90 | + }), |
| 91 | + // just compile _something_ |
| 92 | + ImmutableList.of(JavaFileObjects.forSourceLines("Dummy", "final class Dummy {}"))); |
| 93 | + checkState(result.successful(), result); |
| 94 | + Throwable t = thrown.get(); |
| 95 | + if (t != null) { |
| 96 | + throw t; |
| 97 | + } |
| 98 | + } |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Returns the {@link Elements} instance associated with the current execution of the rule. |
| 104 | + * |
| 105 | + * @throws IllegalStateException if this method is invoked outside the execution of the rule. |
| 106 | + */ |
| 107 | + public Elements getElements() { |
| 108 | + checkState(elements != null, "Not running within the rule"); |
| 109 | + return elements; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Returns the {@link Types} instance associated with the current execution of the rule. |
| 114 | + * |
| 115 | + * @throws IllegalStateException if this method is invoked outside the execution of the rule. |
| 116 | + */ |
| 117 | + public Types getTypes() { |
| 118 | + checkState(elements != null, "Not running within the rule"); |
| 119 | + return types; |
| 120 | + } |
| 121 | +} |
0 commit comments