• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.testing.compile.CompilationSubject.assertThat;
20 import static dagger.internal.codegen.Compilers.daggerCompiler;
21 import static dagger.internal.codegen.TestUtils.message;
22 
23 import com.google.testing.compile.Compilation;
24 import com.google.testing.compile.JavaFileObjects;
25 import javax.tools.JavaFileObject;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.JUnit4;
29 
30 @RunWith(JUnit4.class)
31 public final class ConflictingEntryPointsTest {
32 
33   @Test
covariantType()34   public void covariantType() {
35     JavaFileObject base1 =
36         JavaFileObjects.forSourceLines(
37             "test.Base1", //
38             "package test;",
39             "",
40             "interface Base1 {",
41             "  Long foo();",
42             "}");
43     JavaFileObject base2 =
44         JavaFileObjects.forSourceLines(
45             "test.Base2", //
46             "package test;",
47             "",
48             "interface Base2 {",
49             "  Number foo();",
50             "}");
51     JavaFileObject component =
52         JavaFileObjects.forSourceLines(
53             "test.TestComponent",
54             "package test;",
55             "",
56             "import dagger.BindsInstance;",
57             "import dagger.Component;",
58             "",
59             "@Component",
60             "interface TestComponent extends Base1, Base2 {",
61             "",
62             "  @Component.Builder",
63             "  interface Builder {",
64             "    @BindsInstance Builder foo(Long foo);",
65             "    @BindsInstance Builder foo(Number foo);",
66             "    TestComponent build();",
67             "  }",
68             "}");
69     Compilation compilation = daggerCompiler().compile(base1, base2, component);
70     assertThat(compilation).failed();
71     assertThat(compilation)
72         .hadErrorContaining(
73             message(
74                 "conflicting entry point declarations:",
75                 "    Long test.Base1.foo()",
76                 "    Number test.Base2.foo()"))
77         .inFile(component)
78         .onLineContaining("interface TestComponent ");
79   }
80 
81   @Test
covariantTypeFromGenericSupertypes()82   public void covariantTypeFromGenericSupertypes() {
83     JavaFileObject base1 =
84         JavaFileObjects.forSourceLines(
85             "test.Base1", //
86             "package test;",
87             "",
88             "interface Base1<T> {",
89             "  T foo();",
90             "}");
91     JavaFileObject base2 =
92         JavaFileObjects.forSourceLines(
93             "test.Base2", //
94             "package test;",
95             "",
96             "interface Base2<T> {",
97             "  T foo();",
98             "}");
99     JavaFileObject component =
100         JavaFileObjects.forSourceLines(
101             "test.TestComponent",
102             "package test;",
103             "",
104             "import dagger.BindsInstance;",
105             "import dagger.Component;",
106             "",
107             "@Component",
108             "interface TestComponent extends Base1<Long>, Base2<Number> {",
109             "",
110             "  @Component.Builder",
111             "  interface Builder {",
112             "    @BindsInstance Builder foo(Long foo);",
113             "    @BindsInstance Builder foo(Number foo);",
114             "    TestComponent build();",
115             "  }",
116             "}");
117     Compilation compilation = daggerCompiler().compile(base1, base2, component);
118     assertThat(compilation).failed();
119     assertThat(compilation)
120         .hadErrorContaining(
121             message(
122                 "conflicting entry point declarations:",
123                 "    Long test.Base1.foo()",
124                 "    Number test.Base2.foo()"))
125         .inFile(component)
126         .onLineContaining("interface TestComponent ");
127   }
128 
129   @Test
differentQualifier()130   public void differentQualifier() {
131     JavaFileObject base1 =
132         JavaFileObjects.forSourceLines(
133             "test.Base1", //
134             "package test;",
135             "",
136             "interface Base1 {",
137             "  Object foo();",
138             "}");
139     JavaFileObject base2 =
140         JavaFileObjects.forSourceLines(
141             "test.Base2", //
142             "package test;",
143             "",
144             "import javax.inject.Named;",
145             "",
146             "interface Base2 {",
147             "  @Named(\"foo\") Object foo();",
148             "}");
149     JavaFileObject component =
150         JavaFileObjects.forSourceLines(
151             "test.TestComponent",
152             "package test;",
153             "",
154             "import dagger.BindsInstance;",
155             "import dagger.Component;",
156             "import javax.inject.Named;",
157             "",
158             "@Component",
159             "interface TestComponent extends Base1, Base2 {",
160             "",
161             "  @Component.Builder",
162             "  interface Builder {",
163             "    @BindsInstance Builder foo(Object foo);",
164             "    @BindsInstance Builder namedFoo(@Named(\"foo\") Object foo);",
165             "    TestComponent build();",
166             "  }",
167             "}");
168     Compilation compilation = daggerCompiler().compile(base1, base2, component);
169     assertThat(compilation).failed();
170     assertThat(compilation)
171         .hadErrorContaining(
172             message(
173                 "conflicting entry point declarations:",
174                 "    Object test.Base1.foo()",
175                 "    @Named(\"foo\") Object test.Base2.foo()"))
176         .inFile(component)
177         .onLineContaining("interface TestComponent ");
178   }
179 
180   @Test
sameKey()181   public void sameKey() {
182     JavaFileObject base1 =
183         JavaFileObjects.forSourceLines(
184             "test.Base1", //
185             "package test;",
186             "",
187             "interface Base1 {",
188             "  Object foo();",
189             "}");
190     JavaFileObject base2 =
191         JavaFileObjects.forSourceLines(
192             "test.Base2", //
193             "package test;",
194             "",
195             "interface Base2 {",
196             "  Object foo();",
197             "}");
198     JavaFileObject component =
199         JavaFileObjects.forSourceLines(
200             "test.TestComponent",
201             "package test;",
202             "",
203             "import dagger.BindsInstance;",
204             "import dagger.Component;",
205             "",
206             "@Component",
207             "interface TestComponent extends Base1, Base2 {",
208             "",
209             "  @Component.Builder",
210             "  interface Builder {",
211             "    @BindsInstance Builder foo(Object foo);",
212             "    TestComponent build();",
213             "  }",
214             "}");
215     Compilation compilation = daggerCompiler().compile(base1, base2, component);
216     assertThat(compilation).succeeded();
217   }
218 
219   @Test
sameQualifiedKey()220   public void sameQualifiedKey() {
221     JavaFileObject base1 =
222         JavaFileObjects.forSourceLines(
223             "test.Base1", //
224             "package test;",
225             "",
226             "import javax.inject.Named;",
227             "",
228             "interface Base1 {",
229             "  @Named(\"foo\") Object foo();",
230             "}");
231     JavaFileObject base2 =
232         JavaFileObjects.forSourceLines(
233             "test.Base2", //
234             "package test;",
235             "",
236             "import javax.inject.Named;",
237             "",
238             "interface Base2 {",
239             "  @Named(\"foo\") Object foo();",
240             "}");
241     JavaFileObject component =
242         JavaFileObjects.forSourceLines(
243             "test.TestComponent",
244             "package test;",
245             "",
246             "import dagger.BindsInstance;",
247             "import dagger.Component;",
248             "import javax.inject.Named;",
249             "",
250             "@Component",
251             "interface TestComponent extends Base1, Base2 {",
252             "",
253             "  @Component.Builder",
254             "  interface Builder {",
255             "    @BindsInstance Builder foo(@Named(\"foo\") Object foo);",
256             "    TestComponent build();",
257             "  }",
258             "}");
259     Compilation compilation = daggerCompiler().compile(base1, base2, component);
260     assertThat(compilation).succeeded();
261   }
262 }
263