• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 dagger.internal.codegen;
17 
18 import com.google.common.collect.Iterables;
19 import com.google.testing.compile.CompilationRule;
20 import dagger.MembersInjector;
21 import dagger.internal.codegen.writer.ClassName;
22 import dagger.internal.codegen.writer.ParameterizedTypeName;
23 import dagger.internal.codegen.writer.TypeName;
24 import dagger.internal.codegen.writer.TypeNames;
25 import javax.inject.Inject;
26 import javax.inject.Provider;
27 import javax.lang.model.element.ExecutableElement;
28 import javax.lang.model.element.TypeElement;
29 import javax.lang.model.util.ElementFilter;
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.Test;
35 import org.junit.runner.RunWith;
36 import org.junit.runners.JUnit4;
37 
38 import static com.google.common.truth.Truth.assertThat;
39 
40 /**
41  * Test case for {@link FrameworkField}.
42  */
43 @RunWith(JUnit4.class)
44 public class BindingFieldTest {
45   @Rule public CompilationRule compilationRule = new CompilationRule();
46 
47   private Elements elements;
48   private Types types;
49   private Key.Factory keyFactory;
50 
setUp()51   @Before public void setUp() {
52     this.types = compilationRule.getTypes();
53     this.elements = compilationRule.getElements();
54     this.keyFactory = new Key.Factory(types, elements);
55   }
56 
getXConstructor()57   private ExecutableElement getXConstructor() {
58     TypeElement classElement = elements.getTypeElement(X.class.getCanonicalName());
59     return Iterables.getOnlyElement(
60         ElementFilter.constructorsIn(classElement.getEnclosedElements()));
61   }
62 
frameworkType()63   @Test public void frameworkType() {
64     Key key = keyFactory.forInjectConstructorWithResolvedType(
65         getXConstructor().getEnclosingElement().asType());
66     TypeName xClass = TypeNames.forTypeMirror(key.type());
67     assertThat(FrameworkField.createWithTypeFromKey(Provider.class,
68             BindingKey.create(BindingKey.Kind.CONTRIBUTION, key), "test")
69         .frameworkType())
70         .isEqualTo(ParameterizedTypeName.create(
71             ClassName.fromClass(Provider.class), xClass));
72     assertThat(FrameworkField.createWithTypeFromKey(MembersInjector.class,
73             BindingKey.create(BindingKey.Kind.MEMBERS_INJECTION, key), "test")
74         .frameworkType())
75         .isEqualTo(ParameterizedTypeName.create(
76             ClassName.fromClass(MembersInjector.class), xClass));
77   }
78 
nameSuffix()79   @Test public void nameSuffix() {
80     Key key = keyFactory.forInjectConstructorWithResolvedType(
81         getXConstructor().getEnclosingElement().asType());
82     assertThat(FrameworkField.createWithTypeFromKey(Provider.class,
83             BindingKey.create(BindingKey.Kind.CONTRIBUTION, key), "foo").name())
84         .isEqualTo("fooProvider");
85     assertThat(FrameworkField.createWithTypeFromKey(Provider.class,
86             BindingKey.create(BindingKey.Kind.CONTRIBUTION, key), "fooProvider").name())
87         .isEqualTo("fooProvider");
88 
89   }
90 
91   static final class X {
X()92     @Inject X() {}
93   }
94 }
95