• 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.Lazy;
21 import dagger.MembersInjector;
22 import dagger.Module;
23 import dagger.Provides;
24 import dagger.producers.Produced;
25 import dagger.producers.Producer;
26 import dagger.producers.ProducerModule;
27 import dagger.producers.Produces;
28 import java.util.List;
29 import javax.inject.Provider;
30 import javax.lang.model.element.ExecutableElement;
31 import javax.lang.model.element.TypeElement;
32 import javax.lang.model.element.VariableElement;
33 import javax.lang.model.util.ElementFilter;
34 import javax.lang.model.util.Elements;
35 import javax.lang.model.util.Types;
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.junit.runners.JUnit4;
41 
42 import static com.google.common.truth.Truth.assertThat;
43 
44 /**
45  * Test case for {@link DependencyRequestMapper}.
46  */
47 @RunWith(JUnit4.class)
48 public class DependencyRequestMapperTest {
49   @Rule public CompilationRule compilationRule = new CompilationRule();
50 
51   private Elements elements;
52   private Types types;
53   private Key.Factory keyFactory;
54   private DependencyRequest.Factory dependencyRequestFactory;
55 
setUp()56   @Before public void setUp() {
57     this.types = compilationRule.getTypes();
58     this.elements = compilationRule.getElements();
59     this.keyFactory = new Key.Factory(types, elements);
60     this.dependencyRequestFactory = new DependencyRequest.Factory(elements, keyFactory);
61   }
62 
sampleProviderParameters()63   private List<? extends VariableElement> sampleProviderParameters() {
64     TypeElement moduleElement =
65         elements.getTypeElement(ProvidesMethodModule.class.getCanonicalName());
66     ExecutableElement providesMethod =
67         Iterables.getOnlyElement(ElementFilter.methodsIn(moduleElement.getEnclosedElements()));
68     return providesMethod.getParameters();
69   }
70 
sampleProducerParameters()71   private List<? extends VariableElement> sampleProducerParameters() {
72     TypeElement moduleElement =
73         elements.getTypeElement(ProducesMethodModule.class.getCanonicalName());
74     ExecutableElement producesMethod =
75         Iterables.getOnlyElement(ElementFilter.methodsIn(moduleElement.getEnclosedElements()));
76     return producesMethod.getParameters();
77   }
78 
dependencyRequestForInstance()79   private DependencyRequest dependencyRequestForInstance() {
80     return dependencyRequestFactory.forRequiredVariable(sampleProviderParameters().get(0));
81   }
82 
dependencyRequestForLazy()83   private DependencyRequest dependencyRequestForLazy() {
84     return dependencyRequestFactory.forRequiredVariable(sampleProviderParameters().get(1));
85   }
86 
dependencyRequestForProvider()87   private DependencyRequest dependencyRequestForProvider() {
88     return dependencyRequestFactory.forRequiredVariable(sampleProviderParameters().get(2));
89   }
90 
dependencyRequestForMembersInjector()91   private DependencyRequest dependencyRequestForMembersInjector() {
92     return dependencyRequestFactory.forRequiredVariable(sampleProviderParameters().get(3));
93   }
94 
dependencyRequestForProducer()95   private DependencyRequest dependencyRequestForProducer() {
96     return dependencyRequestFactory.forRequiredVariable(sampleProducerParameters().get(0));
97   }
98 
dependencyRequestForProduced()99   private DependencyRequest dependencyRequestForProduced() {
100     return dependencyRequestFactory.forRequiredVariable(sampleProducerParameters().get(1));
101   }
102 
forProvider()103   @Test public void forProvider() {
104     DependencyRequestMapper mapper = DependencyRequestMapper.FOR_PROVIDER;
105     assertThat(mapper.getFrameworkClass(dependencyRequestForInstance()))
106         .isEqualTo(Provider.class);
107     assertThat(mapper.getFrameworkClass(dependencyRequestForLazy()))
108         .isEqualTo(Provider.class);
109     assertThat(mapper.getFrameworkClass(dependencyRequestForProvider()))
110         .isEqualTo(Provider.class);
111     assertThat(mapper.getFrameworkClass(dependencyRequestForMembersInjector()))
112         .isEqualTo(MembersInjector.class);
113   }
114 
forProducer()115   @Test public void forProducer() {
116     DependencyRequestMapper mapper = DependencyRequestMapper.FOR_PRODUCER;
117     assertThat(mapper.getFrameworkClass(dependencyRequestForInstance()))
118         .isEqualTo(Producer.class);
119     assertThat(mapper.getFrameworkClass(dependencyRequestForLazy()))
120         .isEqualTo(Provider.class);
121     assertThat(mapper.getFrameworkClass(dependencyRequestForProvider()))
122         .isEqualTo(Provider.class);
123     assertThat(mapper.getFrameworkClass(dependencyRequestForMembersInjector()))
124         .isEqualTo(MembersInjector.class);
125     assertThat(mapper.getFrameworkClass(dependencyRequestForProducer()))
126         .isEqualTo(Producer.class);
127     assertThat(mapper.getFrameworkClass(dependencyRequestForProduced()))
128         .isEqualTo(Producer.class);
129   }
130 
131   @Module
132   static final class ProvidesMethodModule {
provideString( Integer a, Lazy<Integer> b, Provider<Integer> c, MembersInjector<Y> d)133     @Provides String provideString(
134         Integer a, Lazy<Integer> b, Provider<Integer> c, MembersInjector<Y> d) {
135       return null;
136     }
137   }
138 
139   @ProducerModule
140   static final class ProducesMethodModule {
produceString(Producer<Integer> a, Produced<Integer> b)141     @Produces String produceString(Producer<Integer> a, Produced<Integer> b) {
142       return null;
143     }
144   }
145 
146   static final class Y {}
147 }
148