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.ImmutableList;
19 import com.google.testing.compile.JavaFileObjects;
20 import javax.tools.JavaFileObject;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.junit.runners.JUnit4;
24 
25 import static com.google.common.truth.Truth.assert_;
26 import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources;
27 
28 @RunWith(JUnit4.class)
29 public class MultipleRequestTest {
30   private static final JavaFileObject DEP_FILE = JavaFileObjects.forSourceLines("test.Dep",
31       "package test;",
32       "",
33       "import javax.inject.Inject;",
34       "",
35       "class Dep {",
36       "  @Inject Dep() {}",
37       "}");
38 
multipleRequests_constructor()39   @Test public void multipleRequests_constructor() {
40     assert_().about(javaSources())
41         .that(ImmutableList.of(
42             DEP_FILE,
43             JavaFileObjects.forSourceLines("test.ConstructorInjectsMultiple",
44                 "package test;",
45                 "",
46                 "import javax.inject.Inject;",
47                 "",
48                 "class ConstructorInjectsMultiple {",
49                 "  @Inject ConstructorInjectsMultiple(Dep d1, Dep d2) {}",
50                 "}"),
51             JavaFileObjects.forSourceLines("test.SimpleComponent",
52                 "package test;",
53                 "",
54                 "import dagger.Component;",
55                 "",
56                 "@Component",
57                 "interface SimpleComponent {",
58                 "  ConstructorInjectsMultiple get();",
59                 "}")))
60         .processedWith(new ComponentProcessor())
61         .compilesWithoutError();
62   }
63 
multipleRequests_field()64   @Test public void multipleRequests_field() {
65     assert_().about(javaSources())
66         .that(ImmutableList.of(
67             DEP_FILE,
68             JavaFileObjects.forSourceLines("test.FieldInjectsMultiple",
69                 "package test;",
70                 "",
71                 "import javax.inject.Inject;",
72                 "",
73                 "class FieldInjectsMultiple {",
74                 "  @Inject Dep d1;",
75                 "  @Inject Dep d2;",
76                 "  @Inject FieldInjectsMultiple() {}",
77                 "}"),
78             JavaFileObjects.forSourceLines("test.SimpleComponent",
79                 "package test;",
80                 "",
81                 "import dagger.Component;",
82                 "",
83                 "@Component",
84                 "interface SimpleComponent {",
85                 "  FieldInjectsMultiple get();",
86                 "}")))
87         .processedWith(new ComponentProcessor())
88         .compilesWithoutError();
89   }
90 
multipleRequests_providesMethod()91   @Test public void multipleRequests_providesMethod() {
92     assert_().about(javaSources())
93         .that(ImmutableList.of(
94             DEP_FILE,
95             JavaFileObjects.forSourceLines("test.FieldInjectsMultiple",
96                 "package test;",
97                 "",
98                 "import dagger.Module;",
99                 "import dagger.Provides;",
100                 "",
101                 "@Module",
102                 "class SimpleModule {",
103                 "  @Provides Object provide(Dep d1, Dep d2) {",
104                 "    return null;",
105                 "  }",
106                 "}"),
107             JavaFileObjects.forSourceLines("test.SimpleComponent",
108                 "package test;",
109                 "",
110                 "import dagger.Component;",
111                 "",
112                 "@Component(modules = SimpleModule.class)",
113                 "interface SimpleComponent {",
114                 "  Object get();",
115                 "}")))
116         .processedWith(new ComponentProcessor())
117         .compilesWithoutError();
118   }
119 }
120