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