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.multibindings; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.auto.value.AutoAnnotation; 22 import dagger.MembersInjector; 23 import dagger.multibindings.ClassKey; 24 import dagger.multibindings.StringKey; 25 import java.math.BigDecimal; 26 import java.math.BigInteger; 27 import java.util.Map; 28 import java.util.Set; 29 import javax.inject.Provider; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.junit.runners.JUnit4; 33 34 /** Tests for {@link MultibindingComponent}. */ 35 @RunWith(JUnit4.class) 36 public class MultibindingTest { 37 38 private final MultibindingComponent multibindingComponent = 39 DaggerMultibindingComponent.builder().multibindingDependency(() -> 0.0).build(); 40 map()41 @Test public void map() { 42 Map<String, String> map = multibindingComponent.map(); 43 assertThat(map).hasSize(2); 44 assertThat(map).containsEntry("foo", "foo value"); 45 assertThat(map).containsEntry("bar", "bar value"); 46 } 47 mapOfArrays()48 @Test public void mapOfArrays() { 49 Map<String, String[]> map = multibindingComponent.mapOfArrays(); 50 assertThat(map).hasSize(2); 51 assertThat(map).containsKey("foo"); 52 assertThat(map.get("foo")).asList().containsExactly("foo1", "foo2").inOrder(); 53 assertThat(map).containsKey("bar"); 54 assertThat(map.get("bar")).asList().containsExactly("bar1", "bar2").inOrder(); 55 } 56 mapOfProviders()57 @Test public void mapOfProviders() { 58 Map<String, Provider<String>> mapOfProviders = multibindingComponent.mapOfProviders(); 59 assertThat(mapOfProviders).hasSize(2); 60 assertThat(mapOfProviders.get("foo").get()).isEqualTo("foo value"); 61 assertThat(mapOfProviders.get("bar").get()).isEqualTo("bar value"); 62 } 63 mapKeysAndValues()64 @Test public void mapKeysAndValues() { 65 assertThat(multibindingComponent.mapKeys()) 66 .containsExactly("foo", "bar"); 67 assertThat(multibindingComponent.mapValues()) 68 .containsExactly("foo value", "bar value"); 69 } 70 nestedKeyMap()71 @Test public void nestedKeyMap() { 72 assertThat(multibindingComponent.nestedKeyMap()) 73 .containsExactly( 74 nestedWrappedKey(Integer.class), "integer", nestedWrappedKey(Long.class), "long"); 75 } 76 77 @Test unwrappedAnnotationKeyMap()78 public void unwrappedAnnotationKeyMap() { 79 assertThat(multibindingComponent.unwrappedAnnotationKeyMap()) 80 .containsExactly(testStringKey("foo\n"), "foo annotation"); 81 } 82 83 @Test wrappedAnnotationKeyMap()84 public void wrappedAnnotationKeyMap() { 85 @SuppressWarnings({"unchecked", "rawtypes"}) 86 Class<? extends Number>[] classes = new Class[] {Long.class, Integer.class}; 87 assertThat(multibindingComponent.wrappedAnnotationKeyMap()) 88 .containsExactly( 89 testWrappedAnnotationKey( 90 testStringKey("foo"), new int[] {1, 2, 3}, new ClassKey[] {}, classes), 91 "wrapped foo annotation"); 92 } 93 94 @Test booleanKeyMap()95 public void booleanKeyMap() { 96 assertThat(multibindingComponent.booleanKeyMap()).containsExactly(true, "true"); 97 } 98 99 @Test byteKeyMap()100 public void byteKeyMap() { 101 assertThat(multibindingComponent.byteKeyMap()).containsExactly((byte) 100, "100 byte"); 102 } 103 104 @Test charKeyMap()105 public void charKeyMap() { 106 assertThat(multibindingComponent.characterKeyMap()) 107 .containsExactly('a', "a char", '\n', "newline char"); 108 } 109 110 @Test classKeyMap()111 public void classKeyMap() { 112 assertThat(multibindingComponent.classKeyMap()) 113 .containsExactly(Integer.class, "integer", Long.class, "long"); 114 } 115 116 @Test numberClassKeyMap()117 public void numberClassKeyMap() { 118 assertThat(multibindingComponent.numberClassKeyMap()) 119 .containsExactly(BigDecimal.class, "bigdecimal", BigInteger.class, "biginteger"); 120 } 121 122 @Test intKeyMap()123 public void intKeyMap() { 124 assertThat(multibindingComponent.integerKeyMap()).containsExactly(100, "100 int"); 125 } 126 127 @Test longKeyMap()128 public void longKeyMap() { 129 assertThat(multibindingComponent.longKeyMap()).containsExactly((long) 100, "100 long"); 130 } 131 132 @Test shortKeyMap()133 public void shortKeyMap() { 134 assertThat(multibindingComponent.shortKeyMap()).containsExactly((short) 100, "100 short"); 135 } 136 setBindings()137 @Test public void setBindings() { 138 assertThat(multibindingComponent.set()) 139 .containsExactly(-90, -17, -1, 5, 6, 832, 1742, -101, -102); 140 } 141 142 @Test complexQualifierSet()143 public void complexQualifierSet() { 144 assertThat(multibindingComponent.complexQualifierStringSet()).containsExactly("foo"); 145 } 146 147 @Test emptySet()148 public void emptySet() { 149 assertThat(multibindingComponent.emptySet()).isEmpty(); 150 } 151 152 @Test emptyQualifiedSet()153 public void emptyQualifiedSet() { 154 assertThat(multibindingComponent.emptyQualifiedSet()).isEmpty(); 155 } 156 157 @Test emptyMap()158 public void emptyMap() { 159 assertThat(multibindingComponent.emptyMap()).isEmpty(); 160 } 161 162 @Test emptyQualifiedMap()163 public void emptyQualifiedMap() { 164 assertThat(multibindingComponent.emptyQualifiedMap()).isEmpty(); 165 } 166 167 @Test maybeEmptySet()168 public void maybeEmptySet() { 169 assertThat(multibindingComponent.maybeEmptySet()).containsExactly("foo"); 170 } 171 172 @Test maybeEmptyQualifiedSet()173 public void maybeEmptyQualifiedSet() { 174 assertThat(multibindingComponent.maybeEmptyQualifiedSet()).containsExactly("qualified foo"); 175 } 176 177 @Test maybeEmptyMap()178 public void maybeEmptyMap() { 179 assertThat(multibindingComponent.maybeEmptyMap()).containsEntry("key", "foo value"); 180 } 181 182 @Test maybeEmptyQualifiedMap()183 public void maybeEmptyQualifiedMap() { 184 assertThat(multibindingComponent.maybeEmptyQualifiedMap()) 185 .containsEntry("key", "qualified foo value"); 186 } 187 188 @SuppressWarnings("unchecked") // We know the single type in the set 189 @Test membersInjectorSet()190 public void membersInjectorSet() { 191 Set<MembersInjector<?>> injectors = multibindingComponent.membersInjectorSet(); 192 assertThat(injectors).hasSize(1); 193 RequiresFieldInjection injected = new RequiresFieldInjection(); 194 assertThat(injected.value).isNull(); 195 ((MembersInjector<RequiresFieldInjection>) injectors.iterator().next()).injectMembers(injected); 196 assertThat(injected.value).isEqualTo("fieldInjectedValue"); 197 } 198 199 @SuppressWarnings("unchecked") // We know the single type in the map 200 @Test membersInjectorMap()201 public void membersInjectorMap() { 202 Map<Class<?>, MembersInjector<?>> injectors = multibindingComponent.membersInjectorMap(); 203 assertThat(injectors).hasSize(1); 204 RequiresFieldInjection injected = new RequiresFieldInjection(); 205 assertThat(injected.value).isNull(); 206 ((MembersInjector<RequiresFieldInjection>) injectors.get(RequiresFieldInjection.class)) 207 .injectMembers(injected); 208 assertThat(injected.value).isEqualTo("fieldInjectedValue"); 209 } 210 211 @AutoAnnotation testStringKey(String value)212 static StringKey testStringKey(String value) { 213 return new AutoAnnotation_MultibindingTest_testStringKey(value); 214 } 215 216 @AutoAnnotation nestedWrappedKey(Class<?> value)217 static NestedAnnotationContainer.NestedWrappedKey nestedWrappedKey(Class<?> value) { 218 return new AutoAnnotation_MultibindingTest_nestedWrappedKey(value); 219 } 220 221 @AutoAnnotation testWrappedAnnotationKey( StringKey value, int[] integers, ClassKey[] annotations, Class<? extends Number>[] classes)222 static WrappedAnnotationKey testWrappedAnnotationKey( 223 StringKey value, int[] integers, ClassKey[] annotations, Class<? extends Number>[] classes) { 224 return new AutoAnnotation_MultibindingTest_testWrappedAnnotationKey( 225 value, integers, annotations, classes); 226 } 227 } 228