1 /* 2 * Copyright (C) 2019 The Dagger Authors. 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 17 package dagger.internal.codegen; 18 19 import static com.google.common.collect.Iterables.getOnlyElement; 20 import static com.google.common.truth.Truth.assertThat; 21 22 import com.google.auto.common.AnnotationMirrors; 23 import com.google.testing.compile.CompilationRule; 24 import dagger.internal.codegen.langmodel.DaggerElements; 25 import dagger.internal.codegen.langmodel.DaggerTypes; 26 import java.lang.annotation.Retention; 27 import java.lang.annotation.RetentionPolicy; 28 import javax.lang.model.element.AnnotationMirror; 29 import org.junit.Before; 30 import org.junit.Rule; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.junit.runners.JUnit4; 34 35 /** Tests {@link TypeProtoConverter}. */ 36 @RunWith(JUnit4.class) 37 public class AnnotationProtoConverterTest { 38 @Rule public CompilationRule compilationRule = new CompilationRule(); 39 40 private DaggerElements elements; 41 private DaggerTypes types; 42 private AnnotationProtoConverter annotationProtoConverter; 43 44 @Before setUp()45 public void setUp() { 46 this.elements = new DaggerElements(compilationRule.getElements(), compilationRule.getTypes()); 47 this.types = new DaggerTypes(compilationRule.getTypes(), elements); 48 this.annotationProtoConverter = 49 new AnnotationProtoConverter(new TypeProtoConverter(types, elements)); 50 } 51 52 @Retention(RetentionPolicy.RUNTIME) 53 @interface TestAnnotation { b()54 byte b(); bool()55 boolean bool(); s()56 short s(); c()57 char c(); i()58 int i(); l()59 long l(); d()60 double d(); f()61 float f(); 62 string()63 String string(); enumValue()64 RetentionPolicy enumValue(); classValue()65 Class<?> classValue(); nestedAnnotations()66 HasDefaults[] nestedAnnotations(); 67 } 68 69 @Retention(RetentionPolicy.RUNTIME) 70 @interface HasDefaults { value()71 int value() default 2; 72 } 73 74 @TestAnnotation( 75 b = 1, 76 bool = true, 77 s = 2, 78 c = 'c', 79 i = 4, 80 l = 5, 81 d = 6.0d, 82 f = 7.0f, 83 string = "hello, world", 84 enumValue = RetentionPolicy.CLASS, 85 classValue = AnnotationProtoConverter.class, 86 nestedAnnotations = {@HasDefaults, @HasDefaults(8)}) 87 static class TestSubject {} 88 89 @Test conversion()90 public void conversion() { 91 AnnotationMirror actual = 92 getOnlyElement(elements.getTypeElement(TestSubject.class).getAnnotationMirrors()); 93 AnnotationMirror translated = 94 annotationProtoConverter.fromProto(AnnotationProtoConverter.toProto(actual)); 95 assertThat(AnnotationMirrors.equivalence().equivalent(actual, translated)).isTrue(); 96 } 97 } 98