• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 Google LLC
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.auto.value.extension.serializable.serializer.utils;
17 
18 import com.google.common.collect.ImmutableList;
19 import com.google.common.collect.ImmutableMap;
20 import com.google.common.collect.Iterables;
21 import com.google.common.reflect.Reflection;
22 import com.google.testing.compile.CompilationRule;
23 import java.lang.reflect.InvocationHandler;
24 import java.util.Arrays;
25 import javax.annotation.processing.Messager;
26 import javax.annotation.processing.ProcessingEnvironment;
27 import javax.lang.model.element.TypeElement;
28 import javax.lang.model.type.DeclaredType;
29 import javax.lang.model.type.TypeMirror;
30 import javax.lang.model.util.Elements;
31 import javax.lang.model.util.Types;
32 import org.junit.Before;
33 import org.junit.Rule;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.JUnit4;
36 
37 @RunWith(JUnit4.class)
38 public abstract class CompilationAbstractTest {
39 
40   @Rule public final CompilationRule compilationRule = new CompilationRule();
41 
42   protected ProcessingEnvironment mockProcessingEnvironment = mockProcessingEnvironment();
43   protected Messager mockMessager = mockMessager();
44 
45   protected Types typeUtils;
46   protected Elements elementUtils;
47 
mockProcessingEnvironment()48   private ProcessingEnvironment mockProcessingEnvironment() {
49     InvocationHandler handler = (proxy, method, args) -> {
50       switch (method.getName()) {
51         case "getTypeUtils":
52           return typeUtils;
53         case "getElementUtils":
54           return elementUtils;
55         case "getMessager":
56           return mockMessager;
57         case "getOptions":
58           return ImmutableMap.of();
59         case "toString":
60           return "MockProcessingEnvironment";
61         default:
62           throw new IllegalArgumentException("Unsupported method: " + method);
63       }
64     };
65     return Reflection.newProxy(ProcessingEnvironment.class, handler);
66   }
67 
mockMessager()68   private Messager mockMessager() {
69     InvocationHandler handler = (proxy, method, args) -> null;
70     return Reflection.newProxy(Messager.class, handler);
71   }
72 
73   @Before
setUp()74   public final void setUp() {
75     typeUtils = compilationRule.getTypes();
76     elementUtils = compilationRule.getElements();
77   }
78 
typeElementOf(Class<?> c)79   protected TypeElement typeElementOf(Class<?> c) {
80     return elementUtils.getTypeElement(c.getCanonicalName());
81   }
82 
typeMirrorOf(Class<?> c)83   protected TypeMirror typeMirrorOf(Class<?> c) {
84     return typeElementOf(c).asType();
85   }
86 
declaredTypeOf(Class<?> enclosingClass, Class<?> containedClass)87   protected DeclaredType declaredTypeOf(Class<?> enclosingClass, Class<?> containedClass) {
88     return typeUtils.getDeclaredType(typeElementOf(enclosingClass), typeMirrorOf(containedClass));
89   }
90 
declaredTypeOf(Class<?> enclosingClass, Class<?>... classArgs)91   protected DeclaredType declaredTypeOf(Class<?> enclosingClass, Class<?>... classArgs) {
92     return typeUtils.getDeclaredType(
93         typeElementOf(enclosingClass),
94         Iterables.toArray(
95             Arrays.stream(classArgs)
96                 .map(this::typeMirrorOf)
97                 .collect(ImmutableList.toImmutableList()),
98             TypeMirror.class));
99   }
100 }
101