1 /* 2 * Copyright (C) 2014 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 import static org.junit.Assert.fail; 21 22 import com.google.common.collect.ImmutableSet; 23 import com.google.common.util.concurrent.ListenableFuture; 24 import dagger.producers.Producer; 25 import dagger.producers.Producers; 26 import java.util.Collections; 27 import java.util.Set; 28 import java.util.concurrent.ExecutionException; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.junit.runners.JUnit4; 32 33 /** 34 * Tests {@link SetProducer}. 35 */ 36 @RunWith(JUnit4.class) 37 public class SetProducerTest { 38 @Test success()39 public void success() throws Exception { 40 Producer<Set<Integer>> producer = 41 SetProducer.<Integer>builder(1, 1) 42 .addProducer(Producers.immediateProducer(1)) 43 .addCollectionProducer(Producers.<Set<Integer>>immediateProducer(ImmutableSet.of(5, 7))) 44 .build(); 45 assertThat(producer.get().get()).containsExactly(1, 5, 7); 46 } 47 48 @Test delegateNpe()49 public void delegateNpe() throws Exception { 50 Producer<Set<Integer>> producer = 51 SetProducer.<Integer>builder(1, 0) 52 .addProducer(Producers.<Integer>immediateProducer(null)) 53 .build(); 54 ListenableFuture<Set<Integer>> future = producer.get(); 55 try { 56 future.get(); 57 fail(); 58 } catch (ExecutionException e) { 59 assertThat(e).hasCauseThat().isInstanceOf(NullPointerException.class); 60 } 61 } 62 63 @Test delegateSetNpe()64 public void delegateSetNpe() throws Exception { 65 Producer<Set<Integer>> producer = 66 SetProducer.<Integer>builder(0, 1) 67 .addCollectionProducer(Producers.<Set<Integer>>immediateProducer(null)) 68 .build(); 69 ListenableFuture<Set<Integer>> future = producer.get(); 70 try { 71 future.get(); 72 fail(); 73 } catch (ExecutionException e) { 74 assertThat(e).hasCauseThat().isInstanceOf(NullPointerException.class); 75 } 76 } 77 78 @Test delegateElementNpe()79 public void delegateElementNpe() throws Exception { 80 Producer<Set<Integer>> producer = 81 SetProducer.<Integer>builder(0, 2) 82 .addCollectionProducer(Producers.<Set<Integer>>immediateProducer(ImmutableSet.of(1, 2))) 83 .addCollectionProducer( 84 Producers.<Set<Integer>>immediateProducer(Collections.<Integer>singleton(null))) 85 .build(); 86 ListenableFuture<Set<Integer>> future = producer.get(); 87 try { 88 future.get(); 89 fail(); 90 } catch (ExecutionException e) { 91 assertThat(e).hasCauseThat().isInstanceOf(NullPointerException.class); 92 } 93 } 94 } 95