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.functional;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static dagger.functional.PrimitivesModule.BOUND_BOOLEAN;
21 import static dagger.functional.PrimitivesModule.BOUND_BOOLEAN_ARRAY;
22 import static dagger.functional.PrimitivesModule.BOUND_BYTE;
23 import static dagger.functional.PrimitivesModule.BOUND_BYTE_ARRAY;
24 import static dagger.functional.PrimitivesModule.BOUND_CHAR;
25 import static dagger.functional.PrimitivesModule.BOUND_CHAR_ARRAY;
26 import static dagger.functional.PrimitivesModule.BOUND_DOUBLE;
27 import static dagger.functional.PrimitivesModule.BOUND_DOUBLE_ARRAY;
28 import static dagger.functional.PrimitivesModule.BOUND_FLOAT;
29 import static dagger.functional.PrimitivesModule.BOUND_FLOAT_ARRAY;
30 import static dagger.functional.PrimitivesModule.BOUND_INT;
31 import static dagger.functional.PrimitivesModule.BOUND_INT_ARRAY;
32 import static dagger.functional.PrimitivesModule.BOUND_LONG;
33 import static dagger.functional.PrimitivesModule.BOUND_LONG_ARRAY;
34 import static dagger.functional.PrimitivesModule.BOUND_SHORT;
35 import static dagger.functional.PrimitivesModule.BOUND_SHORT_ARRAY;
36 
37 import dagger.Lazy;
38 import javax.inject.Provider;
39 import org.junit.experimental.theories.DataPoint;
40 import org.junit.experimental.theories.Theories;
41 import org.junit.experimental.theories.Theory;
42 import org.junit.runner.RunWith;
43 
44 @RunWith(Theories.class)
45 public class BasicTest {
46   @DataPoint
47   public static final BasicComponent basicComponent = DaggerBasicComponent.create();
48   @DataPoint
49   public static final BasicComponent abstractClassBasicComponent =
50       DaggerBasicAbstractClassComponent.create();
51 
primitives(BasicComponent basicComponent)52   @Theory public void primitives(BasicComponent basicComponent) {
53     assertThat(basicComponent.getByte()).isEqualTo(BOUND_BYTE);
54     assertThat(basicComponent.getChar()).isEqualTo(BOUND_CHAR);
55     assertThat(basicComponent.getShort()).isEqualTo(BOUND_SHORT);
56     assertThat(basicComponent.getInt()).isEqualTo(BOUND_INT);
57     assertThat(basicComponent.getLong()).isEqualTo(BOUND_LONG);
58     assertThat(basicComponent.getBoolean()).isEqualTo(BOUND_BOOLEAN);
59     assertThat(basicComponent.getFloat()).isEqualTo(BOUND_FLOAT);
60     assertThat(basicComponent.getDouble()).isEqualTo(BOUND_DOUBLE);
61   }
62 
boxedPrimitives(BasicComponent basicComponent)63   @Theory public void boxedPrimitives(BasicComponent basicComponent) {
64     assertThat(basicComponent.getBoxedByte()).isEqualTo(new Byte(BOUND_BYTE));
65     assertThat(basicComponent.getBoxedChar()).isEqualTo(new Character(BOUND_CHAR));
66     assertThat(basicComponent.getBoxedShort()).isEqualTo(new Short(BOUND_SHORT));
67     assertThat(basicComponent.getBoxedInt()).isEqualTo(new Integer(BOUND_INT));
68     assertThat(basicComponent.getBoxedLong()).isEqualTo(new Long(BOUND_LONG));
69     assertThat(basicComponent.getBoxedBoolean()).isEqualTo(new Boolean(BOUND_BOOLEAN));
70     assertThat(basicComponent.getBoxedFloat()).isEqualTo(BOUND_FLOAT);
71     assertThat(basicComponent.getBoxedDouble()).isEqualTo(BOUND_DOUBLE);
72   }
73 
boxedPrimitiveProviders(BasicComponent basicComponent)74   @Theory public void boxedPrimitiveProviders(BasicComponent basicComponent) {
75     assertThat(basicComponent.getByteProvider().get()).isEqualTo(new Byte(BOUND_BYTE));
76     assertThat(basicComponent.getCharProvider().get()).isEqualTo(new Character(BOUND_CHAR));
77     assertThat(basicComponent.getShortProvider().get()).isEqualTo(new Short(BOUND_SHORT));
78     assertThat(basicComponent.getIntProvider().get()).isEqualTo(new Integer(BOUND_INT));
79     assertThat(basicComponent.getLongProvider().get()).isEqualTo(new Long(BOUND_LONG));
80     assertThat(basicComponent.getBooleanProvider().get()).isEqualTo(new Boolean(BOUND_BOOLEAN));
81     assertThat(basicComponent.getFloatProvider().get()).isEqualTo(BOUND_FLOAT);
82     assertThat(basicComponent.getDoubleProvider().get()).isEqualTo(BOUND_DOUBLE);
83   }
84 
primitiveArrays(BasicComponent basicComponent)85   @Theory public void primitiveArrays(BasicComponent basicComponent) {
86     assertThat(basicComponent.getByteArray()).isSameInstanceAs(BOUND_BYTE_ARRAY);
87     assertThat(basicComponent.getCharArray()).isSameInstanceAs(BOUND_CHAR_ARRAY);
88     assertThat(basicComponent.getShortArray()).isSameInstanceAs(BOUND_SHORT_ARRAY);
89     assertThat(basicComponent.getIntArray()).isSameInstanceAs(BOUND_INT_ARRAY);
90     assertThat(basicComponent.getLongArray()).isSameInstanceAs(BOUND_LONG_ARRAY);
91     assertThat(basicComponent.getBooleanArray()).isSameInstanceAs(BOUND_BOOLEAN_ARRAY);
92     assertThat(basicComponent.getFloatArray()).isSameInstanceAs(BOUND_FLOAT_ARRAY);
93     assertThat(basicComponent.getDoubleArray()).isSameInstanceAs(BOUND_DOUBLE_ARRAY);
94   }
95 
primitiveArrayProviders(BasicComponent basicComponent)96   @Theory public void primitiveArrayProviders(BasicComponent basicComponent) {
97     assertThat(basicComponent.getByteArrayProvider().get()).isSameInstanceAs(BOUND_BYTE_ARRAY);
98     assertThat(basicComponent.getCharArrayProvider().get()).isSameInstanceAs(BOUND_CHAR_ARRAY);
99     assertThat(basicComponent.getShortArrayProvider().get()).isSameInstanceAs(BOUND_SHORT_ARRAY);
100     assertThat(basicComponent.getIntArrayProvider().get()).isSameInstanceAs(BOUND_INT_ARRAY);
101     assertThat(basicComponent.getLongArrayProvider().get()).isSameInstanceAs(BOUND_LONG_ARRAY);
102     assertThat(basicComponent.getBooleanArrayProvider().get())
103         .isSameInstanceAs(BOUND_BOOLEAN_ARRAY);
104     assertThat(basicComponent.getFloatArrayProvider().get()).isSameInstanceAs(BOUND_FLOAT_ARRAY);
105     assertThat(basicComponent.getDoubleArrayProvider().get()).isSameInstanceAs(BOUND_DOUBLE_ARRAY);
106   }
107 
noOpMembersInjection(BasicComponent basicComponent)108   @Theory public void noOpMembersInjection(BasicComponent basicComponent) {
109     Object object = new Object();
110     assertThat(basicComponent.noOpMembersInjection(object)).isSameInstanceAs(object);
111   }
112 
basicObject_noDeps(BasicComponent basicComponent)113   @Theory public void basicObject_noDeps(BasicComponent basicComponent) {
114     assertThat(basicComponent.thing()).isNotNull();
115   }
116 
inheritedMembersInjection(BasicComponent basicComponent)117   @Theory public void inheritedMembersInjection(BasicComponent basicComponent) {
118     assertThat(basicComponent.typeWithInheritedMembersInjection().thing).isNotNull();
119   }
120 
121   @Theory
nullableInjection(BasicComponent basicComponent)122   public void nullableInjection(BasicComponent basicComponent) {
123     assertThat(basicComponent.nullObject()).isNull();
124     assertThat(basicComponent.nullObjectProvider().get()).isNull();
125     assertThat(basicComponent.lazyNullObject().get()).isNull();
126   }
127 
128   @Theory
providerOfLazy(BasicComponent basicComponent)129   public void providerOfLazy(BasicComponent basicComponent) {
130     Provider<Lazy<InjectedThing>> lazyInjectedThingProvider =
131         basicComponent.lazyInjectedThingProvider();
132     Lazy<InjectedThing> lazyInjectedThing1 = lazyInjectedThingProvider.get();
133     Lazy<InjectedThing> lazyInjectedThing2 = lazyInjectedThingProvider.get();
134     assertThat(lazyInjectedThing2).isNotSameInstanceAs(lazyInjectedThing1);
135     assertThat(lazyInjectedThing1.get()).isSameInstanceAs(lazyInjectedThing1.get());
136     assertThat(lazyInjectedThing2.get()).isSameInstanceAs(lazyInjectedThing2.get());
137     assertThat(lazyInjectedThing2.get()).isNotSameInstanceAs(lazyInjectedThing1.get());
138   }
139 }
140