1 /* 2 * Copyright (C) 2016 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.producers.multibindings; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.fail; 21 22 import com.google.common.collect.Iterables; 23 import com.google.common.util.concurrent.ListenableFuture; 24 import dagger.producers.Produced; 25 import dagger.producers.Producer; 26 import java.util.HashSet; 27 import java.util.Map; 28 import java.util.Set; 29 import java.util.concurrent.ExecutionException; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.junit.runners.JUnit4; 33 34 @RunWith(JUnit4.class) 35 public class MultibindingTest { 36 @Test setBinding()37 public void setBinding() throws Exception { 38 MultibindingComponent multibindingComponent = DaggerMultibindingComponent.create(); 39 assertThat(multibindingComponent.strs().get()) 40 .containsExactly( 41 "foo", 42 "foo1", 43 "foo2", 44 "baz1", 45 "baz2", 46 "bar", 47 "bar1", 48 "bar2", 49 "providedStr", 50 "providedStr1", 51 "providedStr2"); 52 assertThat(multibindingComponent.strCount().get()).isEqualTo(11); 53 } 54 55 @Test setBindingOfProduced()56 public void setBindingOfProduced() throws Exception { 57 MultibindingComponent multibindingComponent = DaggerMultibindingComponent.create(); 58 assertThat(multibindingComponent.successfulSet().get()) 59 .containsExactly( 60 Produced.successful("foo"), 61 Produced.successful("foo1"), 62 Produced.successful("foo2"), 63 Produced.successful("baz1"), 64 Produced.successful("baz2"), 65 Produced.successful("bar"), 66 Produced.successful("bar1"), 67 Produced.successful("bar2"), 68 Produced.successful("providedStr"), 69 Produced.successful("providedStr1"), 70 Produced.successful("providedStr2")); 71 } 72 73 @Test setBindingOfProducedWithFailures()74 public void setBindingOfProducedWithFailures() throws Exception { 75 MultibindingComponent multibindingComponent = DaggerMultibindingComponent.create(); 76 Set<Produced<String>> possiblyThrowingSet = multibindingComponent.possiblyThrowingSet().get(); 77 Set<String> successes = new HashSet<>(); 78 Set<ExecutionException> failures = new HashSet<>(); 79 for (Produced<String> str : possiblyThrowingSet) { 80 try { 81 successes.add(str.get()); 82 } catch (ExecutionException e) { 83 failures.add(e); 84 } 85 } 86 assertThat(successes).containsExactly("singleton", "double", "ton"); 87 assertThat(failures).hasSize(1); 88 assertThat(Iterables.getOnlyElement(failures).getCause()).hasMessageThat().isEqualTo("monkey"); 89 } 90 91 @Test mapBinding()92 public void mapBinding() throws Exception { 93 MultibindingComponent multibindingComponent = DaggerMultibindingComponent.create(); 94 Map<Integer, String> map = multibindingComponent.map().get(); 95 assertThat(map).hasSize(3); 96 assertThat(map).containsEntry(15, "fifteen"); 97 assertThat(map).containsEntry(42, "forty two"); 98 assertThat(map).containsEntry(3, "provided three"); 99 } 100 101 @Test mapOfProducerBinding()102 public void mapOfProducerBinding() throws Exception { 103 MultibindingComponent multibindingComponent = DaggerMultibindingComponent.create(); 104 Map<Integer, Producer<String>> map = multibindingComponent.mapOfProducer().get(); 105 assertThat(map).hasSize(3); 106 assertThat(map).containsKey(15); 107 assertThat(map.get(15).get().get()).isEqualTo("fifteen"); 108 assertThat(map).containsKey(42); 109 assertThat(map.get(42).get().get()).isEqualTo("forty two"); 110 assertThat(map).containsKey(3); 111 assertThat(map.get(3).get().get()).isEqualTo("provided three"); 112 } 113 114 @Test mapOfProducedBinding()115 public void mapOfProducedBinding() throws Exception { 116 MultibindingComponent multibindingComponent = DaggerMultibindingComponent.create(); 117 Map<Integer, Produced<String>> map = multibindingComponent.mapOfProduced().get(); 118 assertThat(map).hasSize(3); 119 assertThat(map).containsKey(15); 120 assertThat(map.get(15).get()).isEqualTo("fifteen"); 121 assertThat(map).containsKey(42); 122 assertThat(map.get(42).get()).isEqualTo("forty two"); 123 assertThat(map).containsKey(3); 124 assertThat(map.get(3).get()).isEqualTo("provided three"); 125 } 126 127 @Test mapBindingWithFailures()128 public void mapBindingWithFailures() throws Exception { 129 MultibindingComponent multibindingComponent = DaggerMultibindingComponent.create(); 130 try { 131 multibindingComponent.possiblyThrowingMap().get(); 132 fail(); 133 } catch (ExecutionException e) { 134 assertThat(e.getCause()).hasMessageThat().isEqualTo("monkey"); 135 } 136 } 137 138 @Test mapOfProducerBindingWithFailures()139 public void mapOfProducerBindingWithFailures() throws Exception { 140 MultibindingComponent multibindingComponent = DaggerMultibindingComponent.create(); 141 Map<Integer, Producer<String>> map = 142 multibindingComponent.possiblyThrowingMapOfProducer().get(); 143 assertThat(map).hasSize(2); 144 assertThat(map).containsKey(42); 145 assertThat(map.get(42).get().get()).isEqualTo("forty two"); 146 assertThat(map).containsKey(15); 147 ListenableFuture<String> future = map.get(15).get(); 148 try { 149 future.get(); 150 fail(); 151 } catch (ExecutionException e) { 152 assertThat(e.getCause()).hasMessageThat().isEqualTo("monkey"); 153 } 154 } 155 156 @Test mapOfProducedBindingWithFailures()157 public void mapOfProducedBindingWithFailures() throws Exception { 158 MultibindingComponent multibindingComponent = DaggerMultibindingComponent.create(); 159 Map<Integer, Produced<String>> map = 160 multibindingComponent.possiblyThrowingMapOfProduced().get(); 161 assertThat(map).hasSize(2); 162 assertThat(map).containsKey(42); 163 assertThat(map.get(42).get()).isEqualTo("forty two"); 164 assertThat(map).containsKey(15); 165 Produced<String> produced = map.get(15); 166 try { 167 produced.get(); 168 fail(); 169 } catch (ExecutionException e) { 170 assertThat(e.getCause()).hasMessageThat().isEqualTo("monkey"); 171 } 172 } 173 174 @Test emptySet()175 public void emptySet() throws Exception { 176 MultibindingComponent multibindingComponent = DaggerMultibindingComponent.create(); 177 assertThat(multibindingComponent.objs().get()).isEmpty(); 178 assertThat(multibindingComponent.producedObjs().get()).isEmpty(); 179 assertThat(multibindingComponent.objCount().get()).isEqualTo(0); 180 } 181 182 @Test emptyMap()183 public void emptyMap() throws Exception { 184 MultibindingComponent multibindingComponent = DaggerMultibindingComponent.create(); 185 assertThat(multibindingComponent.objMap().get()).isEmpty(); 186 assertThat(multibindingComponent.objMapOfProduced().get()).isEmpty(); 187 assertThat(multibindingComponent.objMapOfProducer().get()).isEmpty(); 188 } 189 } 190