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.functional; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.assertEquals; 21 22 import dagger.functional.sub.Exposed; 23 import dagger.functional.sub.PublicSubclass; 24 import java.util.ArrayList; 25 import java.util.LinkedList; 26 import java.util.List; 27 import java.util.Set; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.junit.runners.JUnit4; 31 32 @RunWith(JUnit4.class) 33 public class GenericTest { 34 testGenericComponentCreate()35 @Test public void testGenericComponentCreate() { 36 GenericComponent component = DaggerGenericComponent.create(); 37 assertThat(component).isNotNull(); 38 } 39 testGenericSimpleReferences()40 @Test public void testGenericSimpleReferences() { 41 GenericComponent component = DaggerGenericComponent.create(); 42 assertThat(component.referencesGeneric().genericA.t).isNotNull(); 43 } 44 testGenericDoubleReferences()45 @Test public void testGenericDoubleReferences() { 46 GenericComponent component = DaggerGenericComponent.create(); 47 GenericDoubleReferences<A> doubleA = component.doubleGenericA(); 48 assertThat(doubleA.a).isNotNull(); 49 assertThat(doubleA.a2).isNotNull(); 50 assertThat(doubleA.t).isNotNull(); 51 assertThat(doubleA.t2).isNotNull(); 52 53 GenericDoubleReferences<B> doubleB = component.doubleGenericB(); 54 assertThat(doubleB.a).isNotNull(); 55 assertThat(doubleB.a2).isNotNull(); 56 assertThat(doubleB.t).isNotNull(); 57 assertThat(doubleB.t2).isNotNull(); 58 } 59 complexGenerics()60 @Test public void complexGenerics() { 61 GenericComponent component = DaggerGenericComponent.create(); 62 // validate these can be called w/o exceptions. 63 component.complexGenerics(); 64 } 65 noDepsGenerics()66 @Test public void noDepsGenerics() { 67 GenericComponent component = DaggerGenericComponent.create(); 68 // validate these can be called w/o exceptions. 69 component.noDepsA(); 70 component.noDepsB(); 71 } 72 boundedGenerics()73 @Test public void boundedGenerics() { 74 BoundedGenericModule expected = new BoundedGenericModule(); 75 BoundedGenericComponent component = DaggerBoundedGenericComponent.create(); 76 BoundedGenerics<Integer, ArrayList<String>, LinkedList<CharSequence>, Integer, List<Integer>> 77 b1 = component.bounds1(); 78 assertEquals(expected.provideInteger(), b1.a); 79 assertEquals(expected.provideArrayListString(), b1.b); 80 assertEquals(expected.provideLinkedListCharSeq(), b1.c); 81 assertEquals(expected.provideInteger(), b1.d); 82 assertEquals(expected.provideListOfInteger(), b1.e); 83 84 BoundedGenerics<Double, LinkedList<String>, LinkedList<Comparable<String>>, Double, Set<Double>> 85 b2 = component.bounds2(); 86 assertEquals(expected.provideDouble(), b2.a); 87 assertEquals(expected.provideLinkedListString(), b2.b); 88 assertEquals(expected.provideArrayListOfComparableString(), b2.c); 89 assertEquals(expected.provideDouble(), b2.d); 90 assertEquals(expected.provideSetOfDouble(), b2.e); 91 } 92 membersInjections()93 @Test public void membersInjections() { 94 GenericComponent component = DaggerGenericComponent.create(); 95 GenericChild<A> childA = new GenericChild<A>(); 96 component.injectA(childA); 97 assertThat(childA.a).isNotNull(); 98 assertThat(childA.b).isNotNull(); 99 assertThat(childA.registeredA).isNotNull(); 100 assertThat(childA.registeredB).isNotNull(); 101 assertThat(childA.registeredT).isNotNull(); 102 assertThat(childA.registeredX).isNotNull(); 103 assertThat(childA.registeredY).isNotNull(); 104 105 GenericChild<B> childB = new GenericChild<B>(); 106 component.injectB(childB); 107 assertThat(childB.a).isNotNull(); 108 assertThat(childB.b).isNotNull(); 109 assertThat(childB.registeredA).isNotNull(); 110 assertThat(childB.registeredB).isNotNull(); 111 assertThat(childB.registeredT).isNotNull(); 112 assertThat(childB.registeredX).isNotNull(); 113 assertThat(childB.registeredY).isNotNull(); 114 } 115 packagePrivateTypeParameterDependencies()116 @Test public void packagePrivateTypeParameterDependencies() { 117 GenericComponent component = DaggerGenericComponent.create(); 118 Exposed exposed = component.exposed(); 119 assertThat(exposed.gpp.t).isNotNull(); 120 assertThat(exposed.gpp2).isNotNull(); 121 } 122 123 @SuppressWarnings("rawtypes") publicSubclassWithPackagePrivateTypeParameterOfSuperclass()124 @Test public void publicSubclassWithPackagePrivateTypeParameterOfSuperclass() { 125 GenericComponent component = DaggerGenericComponent.create(); 126 PublicSubclass publicSubclass = component.publicSubclass(); 127 assertThat(((Generic)publicSubclass).t).isNotNull(); 128 } 129 singletonScopesAppliesToEachResolvedType()130 @Test public void singletonScopesAppliesToEachResolvedType() { 131 SingletonGenericComponent component = DaggerSingletonGenericComponent.create(); 132 ScopedGeneric<A> a = component.scopedGenericA(); 133 assertThat(a).isSameInstanceAs(component.scopedGenericA()); 134 assertThat(a.t).isNotNull(); 135 136 ScopedGeneric<B> b = component.scopedGenericB(); 137 assertThat(b).isSameInstanceAs(component.scopedGenericB()); 138 assertThat(b.t).isNotNull(); 139 140 assertThat(a).isNotSameInstanceAs(b); 141 } 142 143 @Test // See https://github.com/google/dagger/issues/671 scopedSimpleGenerics()144 public void scopedSimpleGenerics() { 145 SingletonGenericComponent component = DaggerSingletonGenericComponent.create(); 146 ScopedSimpleGeneric<A> a = component.scopedSimpleGenericA(); 147 assertThat(a).isSameInstanceAs(component.scopedSimpleGenericA()); 148 149 ScopedSimpleGeneric<B> b = component.scopedSimpleGenericB(); 150 assertThat(b).isSameInstanceAs(component.scopedSimpleGenericB()); 151 152 assertThat(a).isNotSameInstanceAs(b); 153 } 154 genericModules()155 @Test public void genericModules() { 156 GenericComponent component = DaggerGenericComponent.create(); 157 assertThat(component.iterableInt()).containsExactly(1, 2).inOrder(); 158 assertThat(component.iterableDouble()).containsExactly(3d, 4d).inOrder(); 159 } 160 } 161