1 /*
2  * Copyright (C) 2014 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 BindsInstanceValidationTest {
31   @Test
bindsInstanceInModule()32   public void bindsInstanceInModule() {
33     JavaFileObject testModule =
34         JavaFileObjects.forSourceLines(
35             "test.TestModule",
36             "package test;",
37             "",
38             "import dagger.BindsInstance;",
39             "import dagger.Module;",
40             "",
41             "@Module",
42             "abstract class TestModule {",
43             "  @BindsInstance abstract void str(String string);",
44             "}");
45     Compilation compilation = daggerCompiler().compile(testModule);
46     assertThat(compilation).failed();
47     assertThat(compilation)
48         .hadErrorContaining(
49             "@BindsInstance methods should not be included in @Modules. Did you mean @Binds");
50   }
51 
52   @Test
bindsInstanceInComponent()53   public void bindsInstanceInComponent() {
54     JavaFileObject testComponent =
55         JavaFileObjects.forSourceLines(
56             "test.TestComponent",
57             "package test;",
58             "",
59             "import dagger.BindsInstance;",
60             "import dagger.Component;",
61             "",
62             "@Component",
63             "interface TestComponent {",
64             "  @BindsInstance String s(String s);",
65             "}");
66     Compilation compilation = daggerCompiler().compile(testComponent);
67     assertThat(compilation).failed();
68     assertThat(compilation)
69         .hadErrorContaining(
70             "@BindsInstance methods should not be included in @Components. "
71                 + "Did you mean to put it in a @Component.Builder?");
72   }
73 
74   @Test
bindsInstanceNotAbstract()75   public void bindsInstanceNotAbstract() {
76     JavaFileObject notAbstract =
77         JavaFileObjects.forSourceLines(
78             "test.BindsInstanceNotAbstract",
79             "package test;",
80             "",
81             "import dagger.BindsInstance;",
82             "import dagger.Component;",
83             "",
84             "class BindsInstanceNotAbstract {",
85             "  @BindsInstance BindsInstanceNotAbstract bind(int unused) { return this; }",
86             "}");
87     Compilation compilation = daggerCompiler().compile(notAbstract);
88     assertThat(compilation).failed();
89     assertThat(compilation)
90         .hadErrorContaining("@BindsInstance methods must be abstract")
91         .inFile(notAbstract)
92         .onLine(7);
93   }
94 
95   @Test
bindsInstanceNoParameters()96   public void bindsInstanceNoParameters() {
97     JavaFileObject notAbstract =
98         JavaFileObjects.forSourceLines(
99             "test.BindsInstanceNoParameters",
100             "package test;",
101             "",
102             "import dagger.BindsInstance;",
103             "",
104             "interface BindsInstanceNoParameters {",
105             "  @BindsInstance void noParams();",
106             "}");
107     Compilation compilation = daggerCompiler().compile(notAbstract);
108     assertThat(compilation).failed();
109     assertThat(compilation)
110         .hadErrorContaining(
111             "@BindsInstance methods should have exactly one parameter for the bound type")
112         .inFile(notAbstract)
113         .onLine(6);
114   }
115 
116   @Test
bindsInstanceManyParameters()117   public void bindsInstanceManyParameters() {
118     JavaFileObject notAbstract =
119         JavaFileObjects.forSourceLines(
120             "test.BindsInstanceNoParameter",
121             "package test;",
122             "",
123             "import dagger.BindsInstance;",
124             "",
125             "interface BindsInstanceManyParameters {",
126             "  @BindsInstance void manyParams(int i, long l);",
127             "}");
128     Compilation compilation = daggerCompiler().compile(notAbstract);
129     assertThat(compilation).failed();
130     assertThat(compilation)
131         .hadErrorContaining(
132             "@BindsInstance methods should have exactly one parameter for the bound type")
133         .inFile(notAbstract)
134         .onLine(6);
135   }
136 
137   @Test
bindsInstanceFrameworkType()138   public void bindsInstanceFrameworkType() {
139     JavaFileObject bindsFrameworkType =
140         JavaFileObjects.forSourceLines(
141             "test.BindsInstanceFrameworkType",
142             "package test;",
143             "",
144             "import dagger.BindsInstance;",
145             "import dagger.producers.Producer;",
146             "import javax.inject.Provider;",
147             "",
148             "interface BindsInstanceFrameworkType {",
149             "  @BindsInstance void bindsProvider(Provider<Object> objectProvider);",
150             "  @BindsInstance void bindsProducer(Producer<Object> objectProducer);",
151             "}");
152     Compilation compilation = daggerCompiler().compile(bindsFrameworkType);
153     assertThat(compilation).failed();
154     assertThat(compilation)
155         .hadErrorContaining("@BindsInstance parameters must not be framework types")
156         .inFile(bindsFrameworkType)
157         .onLine(8);
158 
159     assertThat(compilation)
160         .hadErrorContaining("@BindsInstance parameters must not be framework types")
161         .inFile(bindsFrameworkType)
162         .onLine(9);
163   }
164 
165 }
166