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 static org.mockito.Mockito.when; 19 20 import com.google.common.collect.ImmutableList; 21 import com.google.common.collect.Iterables; 22 import com.google.testing.compile.CompilationRule; 23 import java.util.Arrays; 24 import javax.annotation.processing.Messager; 25 import javax.annotation.processing.ProcessingEnvironment; 26 import javax.lang.model.element.TypeElement; 27 import javax.lang.model.type.DeclaredType; 28 import javax.lang.model.type.TypeMirror; 29 import javax.lang.model.util.Elements; 30 import javax.lang.model.util.Types; 31 import org.junit.Before; 32 import org.junit.Rule; 33 import org.junit.runner.RunWith; 34 import org.junit.runners.JUnit4; 35 import org.mockito.Mock; 36 import org.mockito.junit.MockitoJUnit; 37 import org.mockito.junit.MockitoRule; 38 39 @RunWith(JUnit4.class) 40 public abstract class CompilationAbstractTest { 41 42 @Rule public final CompilationRule compilationRule = new CompilationRule(); 43 @Rule public final MockitoRule mockito = MockitoJUnit.rule(); 44 45 @Mock protected ProcessingEnvironment mockProcessingEnvironment; 46 @Mock protected Messager mockMessager; 47 48 protected Types typeUtils; 49 protected Elements elementUtils; 50 51 @Before setUp()52 public final void setUp() { 53 typeUtils = compilationRule.getTypes(); 54 elementUtils = compilationRule.getElements(); 55 56 when(mockProcessingEnvironment.getTypeUtils()).thenReturn(typeUtils); 57 when(mockProcessingEnvironment.getElementUtils()).thenReturn(elementUtils); 58 when(mockProcessingEnvironment.getMessager()).thenReturn(mockMessager); 59 } 60 typeElementOf(Class<?> c)61 protected TypeElement typeElementOf(Class<?> c) { 62 return elementUtils.getTypeElement(c.getCanonicalName()); 63 } 64 typeMirrorOf(Class<?> c)65 protected TypeMirror typeMirrorOf(Class<?> c) { 66 return typeElementOf(c).asType(); 67 } 68 declaredTypeOf(Class<?> enclosingClass, Class<?> containedClass)69 protected DeclaredType declaredTypeOf(Class<?> enclosingClass, Class<?> containedClass) { 70 return typeUtils.getDeclaredType(typeElementOf(enclosingClass), typeMirrorOf(containedClass)); 71 } 72 declaredTypeOf(Class<?> enclosingClass, Class<?>... classArgs)73 protected DeclaredType declaredTypeOf(Class<?> enclosingClass, Class<?>... classArgs) { 74 return typeUtils.getDeclaredType( 75 typeElementOf(enclosingClass), 76 Iterables.toArray( 77 Arrays.stream(classArgs) 78 .map(this::typeMirrorOf) 79 .collect(ImmutableList.toImmutableList()), 80 TypeMirror.class)); 81 } 82 } 83