xref: /aosp_15_r20/external/dagger2/javatests/dagger/internal/codegen/MultipleRequestTest.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
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 dagger.testing.compile.CompilerTests;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.junit.runners.JUnit4;
23 
24 @RunWith(JUnit4.class)
25 public class MultipleRequestTest {
multipleRequests_constructor()26   @Test public void multipleRequests_constructor() {
27     CompilerTests.daggerCompiler(
28             CompilerTests.javaSource(
29                 "test.Dep",
30                 "package test;",
31                 "",
32                 "import javax.inject.Inject;",
33                 "",
34                 "class Dep {",
35                 "  @Inject Dep() {}",
36                 "}"),
37             CompilerTests.javaSource(
38                 "test.ConstructorInjectsMultiple",
39                 "package test;",
40                 "",
41                 "import javax.inject.Inject;",
42                 "",
43                 "class ConstructorInjectsMultiple {",
44                 "  @Inject ConstructorInjectsMultiple(Dep d1, Dep d2) {}",
45                 "}"),
46             CompilerTests.javaSource(
47                 "test.SimpleComponent",
48                 "package test;",
49                 "",
50                 "import dagger.Component;",
51                 "",
52                 "@Component",
53                 "interface SimpleComponent {",
54                 "  ConstructorInjectsMultiple get();",
55                 "}"))
56         .compile(subject -> subject.hasErrorCount(0));
57   }
58 
multipleRequests_field()59   @Test public void multipleRequests_field() {
60     CompilerTests.daggerCompiler(
61             CompilerTests.javaSource(
62                 "test.Dep",
63                 "package test;",
64                 "",
65                 "import javax.inject.Inject;",
66                 "",
67                 "class Dep {",
68                 "  @Inject Dep() {}",
69                 "}"),
70             CompilerTests.javaSource(
71                 "test.FieldInjectsMultiple",
72                 "package test;",
73                 "",
74                 "import javax.inject.Inject;",
75                 "",
76                 "class FieldInjectsMultiple {",
77                 "  @Inject Dep d1;",
78                 "  @Inject Dep d2;",
79                 "  @Inject FieldInjectsMultiple() {}",
80                 "}"),
81             CompilerTests.javaSource(
82                 "test.SimpleComponent",
83                 "package test;",
84                 "",
85                 "import dagger.Component;",
86                 "",
87                 "@Component",
88                 "interface SimpleComponent {",
89                 "  FieldInjectsMultiple get();",
90                 "}"))
91         .compile(subject -> subject.hasErrorCount(0));
92   }
93 
multipleRequests_providesMethod()94   @Test public void multipleRequests_providesMethod() {
95     CompilerTests.daggerCompiler(
96             CompilerTests.javaSource(
97                 "test.Dep",
98                 "package test;",
99                 "",
100                 "import javax.inject.Inject;",
101                 "",
102                 "class Dep {",
103                 "  @Inject Dep() {}",
104                 "}"),
105             CompilerTests.javaSource(
106                 "test.SimpleModule",
107                 "package test;",
108                 "",
109                 "import dagger.Module;",
110                 "import dagger.Provides;",
111                 "",
112                 "@Module",
113                 "class SimpleModule {",
114                 "  @Provides Object provide(Dep d1, Dep d2) {",
115                 "    return null;",
116                 "  }",
117                 "}"),
118             CompilerTests.javaSource(
119                 "test.SimpleComponent",
120                 "package test;",
121                 "",
122                 "import dagger.Component;",
123                 "",
124                 "@Component(modules = SimpleModule.class)",
125                 "interface SimpleComponent {",
126                 "  Object get();",
127                 "}"))
128         .compile(subject -> subject.hasErrorCount(0));
129   }
130 }
131