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