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.producers.internal; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.collect.ImmutableSet; 22 import com.google.common.collect.Iterables; 23 import com.google.common.collect.Sets; 24 import dagger.producers.Produced; 25 import dagger.producers.Producer; 26 import dagger.producers.Producers; 27 import java.util.Arrays; 28 import java.util.Collections; 29 import java.util.Set; 30 import java.util.concurrent.ExecutionException; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.junit.runners.JUnit4; 34 35 /** 36 * Tests {@link SetOfProducedProducer}. 37 */ 38 @RunWith(JUnit4.class) 39 public class SetOfProducedProducerTest { 40 @Test success()41 public void success() throws Exception { 42 Producer<Set<Produced<Integer>>> producer = 43 SetOfProducedProducer.<Integer>builder(1, 1) 44 .addProducer(Producers.immediateProducer(1)) 45 .addCollectionProducer(Producers.<Set<Integer>>immediateProducer(ImmutableSet.of(5, 7))) 46 .build(); 47 assertThat(producer.get().get()) 48 .containsExactly( 49 Produced.successful(1), 50 Produced.successful(5), 51 Produced.successful(7)); 52 } 53 54 @Test failure()55 public void failure() throws Exception { 56 RuntimeException e = new RuntimeException("monkey"); 57 Producer<Set<Produced<Integer>>> producer = 58 SetOfProducedProducer.<Integer>builder(1, 1) 59 .addCollectionProducer(Producers.<Set<Integer>>immediateProducer(ImmutableSet.of(1, 2))) 60 .addProducer(Producers.<Integer>immediateFailedProducer(e)) 61 .build(); 62 assertThat(producer.get().get()) 63 .containsExactly( 64 Produced.successful(1), Produced.successful(2), Produced.<Integer>failed(e)); 65 } 66 67 @Test delegateNpe()68 public void delegateNpe() throws Exception { 69 Producer<Set<Produced<Integer>>> producer = 70 SetOfProducedProducer.<Integer>builder(1, 0) 71 .addProducer(Producers.<Integer>immediateProducer(null)) 72 .build(); 73 Results<Integer> results = Results.create(producer.get().get()); 74 assertThat(results.successes).isEmpty(); 75 assertThat(results.failures).hasSize(1); 76 assertThat(Iterables.getOnlyElement(results.failures)) 77 .hasCauseThat() 78 .isInstanceOf(NullPointerException.class); 79 } 80 81 @Test delegateSetNpe()82 public void delegateSetNpe() throws Exception { 83 Producer<Set<Produced<Integer>>> producer = 84 SetOfProducedProducer.<Integer>builder(0, 1) 85 .addCollectionProducer(Producers.<Set<Integer>>immediateProducer(null)) 86 .build(); 87 Results<Integer> results = Results.create(producer.get().get()); 88 assertThat(results.successes).isEmpty(); 89 assertThat(results.failures).hasSize(1); 90 assertThat(Iterables.getOnlyElement(results.failures)) 91 .hasCauseThat() 92 .isInstanceOf(NullPointerException.class); 93 } 94 95 @Test delegateElementNpe()96 public void delegateElementNpe() throws Exception { 97 Producer<Set<Produced<Integer>>> producer = 98 SetOfProducedProducer.<Integer>builder(0, 1) 99 .addCollectionProducer( 100 Producers.<Set<Integer>>immediateProducer(Collections.<Integer>singleton(null))) 101 .build(); 102 Results<Integer> results = Results.create(producer.get().get()); 103 assertThat(results.successes).isEmpty(); 104 assertThat(results.failures).hasSize(1); 105 assertThat(Iterables.getOnlyElement(results.failures)) 106 .hasCauseThat() 107 .isInstanceOf(NullPointerException.class); 108 } 109 110 @Test oneOfDelegateElementNpe()111 public void oneOfDelegateElementNpe() throws Exception { 112 Producer<Set<Produced<Integer>>> producer = 113 SetOfProducedProducer.<Integer>builder(0, 1) 114 .addCollectionProducer( 115 Producers.<Set<Integer>>immediateProducer( 116 Sets.newHashSet(Arrays.asList(5, 2, null)))) 117 .build(); 118 Results<Integer> results = Results.create(producer.get().get()); 119 assertThat(results.successes).containsExactly(2, 5); 120 assertThat(results.failures).hasSize(1); 121 assertThat(Iterables.getOnlyElement(results.failures)) 122 .hasCauseThat() 123 .isInstanceOf(NullPointerException.class); 124 } 125 126 static final class Results<T> { 127 final ImmutableSet<T> successes; 128 final ImmutableSet<ExecutionException> failures; 129 Results(ImmutableSet<T> successes, ImmutableSet<ExecutionException> failures)130 private Results(ImmutableSet<T> successes, ImmutableSet<ExecutionException> failures) { 131 this.successes = successes; 132 this.failures = failures; 133 } 134 create(Set<Produced<T>> setOfProduced)135 static <T> Results<T> create(Set<Produced<T>> setOfProduced) { 136 ImmutableSet.Builder<T> successes = ImmutableSet.builder(); 137 ImmutableSet.Builder<ExecutionException> failures = ImmutableSet.builder(); 138 for (Produced<T> produced : setOfProduced) { 139 try { 140 successes.add(produced.get()); 141 } catch (ExecutionException e) { 142 failures.add(e); 143 } 144 } 145 return new Results<T>(successes.build(), failures.build()); 146 } 147 } 148 } 149