1 /* 2 * Copyright (C) 2015 Google, Inc. 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 package producerstest; 17 18 import com.google.common.collect.Iterables; 19 import com.google.common.util.concurrent.MoreExecutors; 20 import dagger.producers.Produced; 21 import java.util.HashSet; 22 import java.util.Set; 23 import java.util.concurrent.ExecutionException; 24 import org.junit.Test; 25 import org.junit.runner.RunWith; 26 import org.junit.runners.JUnit4; 27 28 import static com.google.common.truth.Truth.assertThat; 29 30 @RunWith(JUnit4.class) 31 public class MultibindingTest { 32 @Test 33 public void setBinding() throws Exception { 34 MultibindingComponent multibindingComponent = 35 DaggerMultibindingComponent.builder().executor(MoreExecutors.directExecutor()).build(); 36 assertThat(multibindingComponent.strs().get()) 37 .containsExactly("foo", "foo1", "foo2", "bar", "bar1", "bar2"); 38 assertThat(multibindingComponent.strCount().get()).isEqualTo(6); 39 } 40 41 @Test 42 public void setBindingOfProduced() throws Exception { 43 MultibindingComponent multibindingComponent = 44 DaggerMultibindingComponent.builder().executor(MoreExecutors.directExecutor()).build(); 45 assertThat(multibindingComponent.successfulSet().get()) 46 .containsExactly( 47 Produced.successful("foo"), 48 Produced.successful("foo1"), 49 Produced.successful("foo2"), 50 Produced.successful("bar"), 51 Produced.successful("bar1"), 52 Produced.successful("bar2")); 53 } 54 55 @Test 56 public void setBindingOfProducedWithFailures() throws Exception { 57 MultibindingComponent multibindingComponent = 58 DaggerMultibindingComponent.builder().executor(MoreExecutors.directExecutor()).build(); 59 Set<Produced<String>> possiblyThrowingSet = multibindingComponent.possiblyThrowingSet().get(); 60 Set<String> successes = new HashSet<>(); 61 Set<ExecutionException> failures = new HashSet<>(); 62 for (Produced<String> str : possiblyThrowingSet) { 63 try { 64 successes.add(str.get()); 65 } catch (ExecutionException e) { 66 failures.add(e); 67 } 68 } 69 assertThat(successes).containsExactly("singleton", "double", "ton"); 70 assertThat(failures).hasSize(1); 71 assertThat(Iterables.getOnlyElement(failures).getCause()).hasMessage("monkey"); 72 } 73 } 74