• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2013 Google, Inc.
3  * Copyright (C) 2013 Square, Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package dagger.tests.integration.operation;
18 
19 import com.google.testing.compile.JavaFileObjects;
20 import dagger.internal.codegen.ComponentProcessor;
21 import javax.tools.JavaFileObject;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.junit.runners.JUnit4;
25 
26 import static com.google.common.truth.Truth.assert_;
27 import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources;
28 import static java.util.Arrays.asList;
29 
30 @RunWith(JUnit4.class)
31 public final class PrimitiveInjectionTest {
32 
33   // TODO(cgruber): Use @test.ForTest to qualify primitives once qualifier equivalence is working.
34   /*
35   JavaFileObject annotation = JavaFileObjects.forSourceLines("test.ForTest",
36       "package test;",
37       "import javax.inject.Qualifier;",
38       "@Qualifier",
39       "public @interface ForTest {",
40       "}");
41   */
42 
43   // TODO(cgruber): Expand test to support more primitive types when b/15512877 is fixed.
44   JavaFileObject primitiveInjectable = JavaFileObjects.forSourceLines("test.PrimitiveInjectable",
45       "package test;",
46       "import javax.inject.Inject;",
47       "class PrimitiveInjectable {",
48       "  @Inject PrimitiveInjectable(int ignored) {}",
49       "}");
50 
51   JavaFileObject primitiveModule = JavaFileObjects.forSourceLines("test.PrimitiveModule",
52       "package test;",
53       "import dagger.Module;",
54       "import dagger.Provides;",
55       "@Module",
56       "class PrimitiveModule {",
57       "  @Provides int primitiveInt() { return Integer.MAX_VALUE; }",
58       "}");
59 
60   JavaFileObject component = JavaFileObjects.forSourceLines("test.PrimitiveComponent",
61       "package test;",
62       "import dagger.Component;",
63       "import dagger.Provides;",
64       "@Component(modules = PrimitiveModule.class)",
65       "interface PrimitiveComponent {",
66       "  int primitiveInt();",
67       "  PrimitiveInjectable primitiveInjectable();",
68       "}");
69 
70   JavaFileObject expectedComponent = JavaFileObjects.forSourceLines(
71       "test.DaggerPrimitiveComponent",
72       "package test;",
73       "",
74       "import javax.annotation.Generated;",
75       "import javax.inject.Provider;",
76       "",
77       "@Generated(\"dagger.internal.codegen.ComponentProcessor\")",
78       "public final class DaggerPrimitiveComponent implements PrimitiveComponent {",
79       "  private Provider<Integer> primitiveIntProvider;",
80       "  private Provider<PrimitiveInjectable> primitiveInjectableProvider;",
81       "",
82       "  private DaggerPrimitiveComponent(Builder builder) {",
83       "    assert builder != null;",
84       "    initialize(builder);",
85       "  }",
86       "",
87       "  public static Builder builder() {",
88       "    return new Builder();",
89       "  }",
90       "",
91       "  public static PrimitiveComponent create() {",
92       "    return builder().build();",
93       "  }",
94       "",
95       "  @SuppressWarnings(\"unchecked\")",
96       "  private void initialize(final Builder builder) {",
97       "    this.primitiveIntProvider =",
98       "        PrimitiveModule_PrimitiveIntFactory.create(builder.primitiveModule);",
99       "    this.primitiveInjectableProvider =",
100       "        PrimitiveInjectable_Factory.create(primitiveIntProvider);",
101       "  }",
102       "",
103       "  @Override",
104       "  public int primitiveInt() {",
105       "    return primitiveIntProvider.get();",
106       "  }",
107       "",
108       "  @Override",
109       "  public PrimitiveInjectable primitiveInjectable() {",
110       "    return primitiveInjectableProvider.get();",
111       "  }",
112       "",
113       "  public static final class Builder {",
114       "    private PrimitiveModule primitiveModule;",
115       "",
116       "    private Builder() {",
117       "    }",
118       "",
119       "    public PrimitiveComponent build() {",
120       "      if (primitiveModule == null) {",
121       "        this.primitiveModule = new PrimitiveModule();",
122       "      }",
123       "      return new DaggerPrimitiveComponent(this);",
124       "    }",
125       "",
126       "    public Builder primitiveModule(PrimitiveModule primitiveModule) {",
127       "      if (primitiveModule == null) {",
128       "        throw new NullPointerException();",
129       "      }",
130       "      this.primitiveModule = primitiveModule;",
131       "      return this;",
132       "    }",
133       "  }",
134       "}");
135 
primitiveArrayTypesAllInjected()136   @Test public void primitiveArrayTypesAllInjected() {
137     assert_().about(javaSources())
138         .that(asList(component, primitiveInjectable, primitiveModule))
139         .processedWith(new ComponentProcessor())
140         .compilesWithoutError()
141         .and().generatesSources(expectedComponent);
142   }
143 }
144