1 /*
2  * Copyright (C) 2020 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 
22 import com.google.testing.compile.Compilation;
23 import com.google.testing.compile.JavaFileObjects;
24 import javax.tools.JavaFileObject;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28 
29 @RunWith(JUnit4.class)
30 public final class ComponentDependenciesTest {
31   @Test
dependenciesWithTwoOfSameMethodOnDifferentInterfaces_fail()32   public void dependenciesWithTwoOfSameMethodOnDifferentInterfaces_fail() {
33     JavaFileObject interfaceOne = JavaFileObjects.forSourceLines("test.One",
34         "package test;",
35         "",
36         "interface One {",
37         "  String getOne();",
38         "}");
39     JavaFileObject interfaceTwo = JavaFileObjects.forSourceLines("test.Two",
40         "package test;",
41         "",
42         "interface Two {",
43         "  String getTwo();",
44         "}");
45     JavaFileObject mergedInterface = JavaFileObjects.forSourceLines("test.Merged",
46         "package test;",
47         "",
48         "interface Merged extends One, Two {}");
49     JavaFileObject componentFile = JavaFileObjects.forSourceLines("test.TestComponent",
50         "package test;",
51         "",
52         "import dagger.Component;",
53         "",
54         "@Component(dependencies = Merged.class)",
55         "interface TestComponent {",
56         "  String getString();",
57         "}");
58     Compilation compilation = daggerCompiler().compile(
59         interfaceOne, interfaceTwo, mergedInterface, componentFile);
60     assertThat(compilation).failed();
61     assertThat(compilation).hadErrorContaining("DuplicateBindings");
62   }
63 
64   @Test
dependenciesWithTwoOfSameMethodOnDifferentInterfaces_producers_fail()65   public void dependenciesWithTwoOfSameMethodOnDifferentInterfaces_producers_fail() {
66     JavaFileObject interfaceOne = JavaFileObjects.forSourceLines("test.One",
67         "package test;",
68         "",
69         "import com.google.common.util.concurrent.ListenableFuture;",
70         "",
71         "interface One {",
72         "  ListenableFuture<String> getOne();",
73         "}");
74     JavaFileObject interfaceTwo = JavaFileObjects.forSourceLines("test.Two",
75         "package test;",
76         "",
77         "import com.google.common.util.concurrent.ListenableFuture;",
78         "",
79         "interface Two {",
80         "  ListenableFuture<String> getTwo();",
81         "}");
82     JavaFileObject mergedInterface = JavaFileObjects.forSourceLines("test.Merged",
83         "package test;",
84         "",
85         "interface Merged extends One, Two {}");
86     JavaFileObject componentFile = JavaFileObjects.forSourceLines("test.TestComponent",
87         "package test;",
88         "",
89         "import com.google.common.util.concurrent.ListenableFuture;",
90         "import dagger.producers.ProductionComponent;",
91         "",
92         "@ProductionComponent(dependencies = Merged.class)",
93         "interface TestComponent {",
94         "  ListenableFuture<String> getString();",
95         "}");
96     Compilation compilation = daggerCompiler().compile(
97         interfaceOne, interfaceTwo, mergedInterface, componentFile);
98     assertThat(compilation).failed();
99     assertThat(compilation).hadErrorContaining("DuplicateBindings");
100   }
101 
102   @Test
dependenciesWithTwoOfSameMethodButDifferentNullability_fail()103   public void dependenciesWithTwoOfSameMethodButDifferentNullability_fail() {
104     JavaFileObject interfaceOne = JavaFileObjects.forSourceLines("test.One",
105         "package test;",
106         "",
107         "interface One {",
108         "  String getString();",
109         "}");
110     JavaFileObject interfaceTwo = JavaFileObjects.forSourceLines("test.Two",
111         "package test;",
112         "import javax.annotation.Nullable;",
113         "",
114         "interface Two {",
115         "  @Nullable String getString();",
116         "}");
117     JavaFileObject mergedInterface = JavaFileObjects.forSourceLines("test.Merged",
118         "package test;",
119         "",
120         "interface Merged extends One, Two {}");
121     JavaFileObject componentFile = JavaFileObjects.forSourceLines("test.TestComponent",
122         "package test;",
123         "",
124         "import dagger.Component;",
125         "",
126         "@Component(dependencies = Merged.class)",
127         "interface TestComponent {",
128         "  String getString();",
129         "}");
130     Compilation compilation = daggerCompiler().compile(
131         interfaceOne, interfaceTwo, mergedInterface, componentFile);
132     assertThat(compilation).failed();
133     assertThat(compilation).hadErrorContaining("DuplicateBindings");
134   }
135 
136 }
137