• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 class RepeatedModuleValidationTest {
31   private static final JavaFileObject MODULE_FILE =
32       JavaFileObjects.forSourceLines(
33           "test.TestModule",
34           "package test;",
35           "",
36           "import dagger.Module;",
37           "",
38           "@Module",
39           "final class TestModule {}");
40 
41   @Test
moduleRepeatedInSubcomponentFactoryMethod()42   public void moduleRepeatedInSubcomponentFactoryMethod() {
43     JavaFileObject subcomponentFile =
44         JavaFileObjects.forSourceLines(
45             "test.TestSubcomponent",
46             "package test;",
47             "",
48             "import dagger.Subcomponent;",
49             "",
50             "@Subcomponent(modules = TestModule.class)",
51             "interface TestSubcomponent {",
52             "}");
53     JavaFileObject componentFile =
54         JavaFileObjects.forSourceLines(
55             "test.TestComponent",
56             "package test;",
57             "",
58             "import dagger.Component;",
59             "",
60             "@Component(modules = TestModule.class)",
61             "interface TestComponent {",
62             "  TestSubcomponent newTestSubcomponent(TestModule module);",
63             "}");
64     Compilation compilation =
65         daggerCompiler().compile(MODULE_FILE, subcomponentFile, componentFile);
66     assertThat(compilation).failed();
67     assertThat(compilation)
68         .hadErrorContaining("TestModule is present in test.TestComponent.")
69         .inFile(componentFile)
70         .onLine(7)
71         .atColumn(51);
72   }
73 
74   @Test
moduleRepeatedInSubcomponentBuilderMethod()75   public void moduleRepeatedInSubcomponentBuilderMethod() {
76     JavaFileObject subcomponentFile =
77         JavaFileObjects.forSourceLines(
78             "test.TestSubcomponent",
79             "package test;",
80             "",
81             "import dagger.Subcomponent;",
82             "",
83             "@Subcomponent(modules = TestModule.class)",
84             "interface TestSubcomponent {",
85             "  @Subcomponent.Builder",
86             "  interface Builder {",
87             "    Builder testModule(TestModule testModule);",
88             "    TestSubcomponent build();",
89             "  }",
90             "}");
91     JavaFileObject componentFile =
92         JavaFileObjects.forSourceLines(
93             "test.TestComponent",
94             "package test;",
95             "",
96             "import dagger.Component;",
97             "",
98             "@Component(modules = TestModule.class)",
99             "interface TestComponent {",
100             "  TestSubcomponent.Builder newTestSubcomponentBuilder();",
101             "}");
102     Compilation compilation =
103         daggerCompiler().compile(MODULE_FILE, subcomponentFile, componentFile);
104     assertThat(compilation).succeeded();
105     // TODO(gak): assert about the warning when we have that ability
106   }
107 
108   @Test
moduleRepeatedButNotPassed()109   public void moduleRepeatedButNotPassed() {
110     JavaFileObject subcomponentFile =
111         JavaFileObjects.forSourceLines(
112             "test.TestSubcomponent",
113             "package test;",
114             "",
115             "import dagger.Subcomponent;",
116             "",
117             "@Subcomponent(modules = TestModule.class)",
118             "interface TestSubcomponent {",
119             "}");
120     JavaFileObject componentFile =
121         JavaFileObjects.forSourceLines(
122             "test.TestComponent",
123             "package test;",
124             "",
125             "import dagger.Component;",
126             "",
127             "@Component(modules = TestModule.class)",
128             "interface TestComponent {",
129             "  TestSubcomponent newTestSubcomponent();",
130             "}");
131     Compilation compilation =
132         daggerCompiler().compile(MODULE_FILE, subcomponentFile, componentFile);
133     assertThat(compilation).succeeded();
134   }
135 }
136