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.internal; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.collect.ImmutableSet; 22 import java.util.Arrays; 23 import java.util.LinkedHashSet; 24 import java.util.Set; 25 import java.util.concurrent.atomic.AtomicInteger; 26 import javax.inject.Provider; 27 import org.junit.Rule; 28 import org.junit.Test; 29 import org.junit.rules.ExpectedException; 30 import org.junit.runner.RunWith; 31 import org.junit.runners.JUnit4; 32 33 @RunWith(JUnit4.class) 34 @SuppressWarnings("unchecked") 35 public class SetFactoryTest { 36 @Rule public ExpectedException thrown = ExpectedException.none(); 37 38 @Test providerReturnsNull()39 public void providerReturnsNull() { 40 Factory<Set<Integer>> factory = 41 SetFactory.<Integer>builder(0, 1).addCollectionProvider(() -> null).build(); 42 thrown.expect(NullPointerException.class); 43 factory.get(); 44 } 45 46 @Test providerReturnsNullSet()47 public void providerReturnsNullSet() { 48 Factory<Set<Integer>> factory = 49 SetFactory.<Integer>builder(1, 0).addProvider(() -> null).build(); 50 thrown.expect(NullPointerException.class); 51 factory.get(); 52 } 53 54 @Test providerReturnsSetWithNullElement()55 public void providerReturnsSetWithNullElement() { 56 Set<Integer> set = new LinkedHashSet<>(Arrays.asList(1, null, 3)); 57 Factory<Set<Integer>> factory = 58 SetFactory.<Integer>builder(0, 1).addCollectionProvider(() -> set).build(); 59 thrown.expect(NullPointerException.class); 60 factory.get(); 61 } 62 63 @Test invokesProvidersEveryTime()64 public void invokesProvidersEveryTime() { 65 Factory<Set<Integer>> factory = 66 SetFactory.<Integer>builder(2, 2) 67 .addProvider(incrementingIntegerProvider(0)) 68 .addProvider(incrementingIntegerProvider(10)) 69 .addCollectionProvider(incrementingIntegerSetProvider(20)) 70 .addCollectionProvider(incrementingIntegerSetProvider(30)) 71 .build(); 72 assertThat(factory.get()).containsExactly(0, 10, 20, 21, 30, 31); 73 assertThat(factory.get()).containsExactly(1, 11, 22, 23, 32, 33); 74 assertThat(factory.get()).containsExactly(2, 12, 24, 25, 34, 35); 75 } 76 incrementingIntegerProvider(int seed)77 private static Provider<Integer> incrementingIntegerProvider(int seed) { 78 final AtomicInteger value = new AtomicInteger(seed); 79 return value::getAndIncrement; 80 } 81 incrementingIntegerSetProvider(int seed)82 private static Provider<Set<Integer>> incrementingIntegerSetProvider(int seed) { 83 final AtomicInteger value = new AtomicInteger(seed); 84 return () -> ImmutableSet.of(value.getAndIncrement(), value.getAndIncrement()); 85 } 86 } 87