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 java.util.LinkedHashMap; 22 import java.util.Map; 23 import java.util.concurrent.atomic.AtomicInteger; 24 import javax.inject.Provider; 25 import org.junit.Rule; 26 import org.junit.Test; 27 import org.junit.rules.ExpectedException; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 31 @RunWith(JUnit4.class) 32 @SuppressWarnings("unchecked") 33 public class MapProviderFactoryTest { 34 @Rule 35 public ExpectedException thrown = ExpectedException.none(); 36 37 @Test nullKey()38 public void nullKey() { 39 thrown.expect(NullPointerException.class); 40 MapProviderFactory.<String, Integer>builder(1).put(null, incrementingIntegerProvider(1)); 41 } 42 43 @Test nullValue()44 public void nullValue() { 45 thrown.expect(NullPointerException.class); 46 MapProviderFactory.<String, Integer>builder(1).put("Hello", null); 47 } 48 49 50 @Test iterationOrder()51 public void iterationOrder() { 52 Provider<Integer> p1 = incrementingIntegerProvider(10); 53 Provider<Integer> p2 = incrementingIntegerProvider(20); 54 Provider<Integer> p3 = incrementingIntegerProvider(30); 55 Provider<Integer> p4 = incrementingIntegerProvider(40); 56 Provider<Integer> p5 = incrementingIntegerProvider(50); 57 58 Factory<Map<String, Provider<Integer>>> factory = MapProviderFactory 59 .<String, Integer>builder(4) 60 .put("two", p2) 61 .put("one", p1) 62 .put("three", p3) 63 .put("one", p5) 64 .put("four", p4) 65 .build(); 66 67 Map<String, Provider<Integer>> expectedMap = new LinkedHashMap<>(); 68 expectedMap.put("two", p2); 69 expectedMap.put("one", p1); 70 expectedMap.put("three", p3); 71 expectedMap.put("one", p5); 72 expectedMap.put("four", p4); 73 assertThat(factory.get().entrySet()) 74 .containsExactlyElementsIn(expectedMap.entrySet()) 75 .inOrder(); 76 } 77 78 incrementingIntegerProvider(int seed)79 private static Provider<Integer> incrementingIntegerProvider(int seed) { 80 return new AtomicInteger(seed)::getAndIncrement; 81 } 82 } 83