xref: /aosp_15_r20/external/dagger2/javatests/dagger/producers/internal/MapProducerTest.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2016 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 dagger.producers.Producer;
23 import dagger.producers.Producers;
24 import java.util.Map;
25 import java.util.concurrent.ExecutionException;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.JUnit4;
29 
30 @RunWith(JUnit4.class)
31 public final class MapProducerTest {
32   @Test
success()33   public void success() throws Exception {
34     Producer<Map<Integer, String>> mapProducer =
35         MapProducer.<Integer, String>builder(2)
36             .put(15, Producers.immediateProducer("fifteen"))
37             .put(42, Producers.immediateProducer("forty two"))
38             .build();
39     Map<Integer, String> map = mapProducer.get().get();
40     assertThat(map).hasSize(2);
41     assertThat(map).containsEntry(15, "fifteen");
42     assertThat(map).containsEntry(42, "forty two");
43   }
44 
45   @Test
failingContribution()46   public void failingContribution() throws Exception {
47     RuntimeException cause = new RuntimeException("monkey");
48     Producer<Map<Integer, String>> mapProducer =
49         MapProducer.<Integer, String>builder(2)
50             .put(15, Producers.immediateProducer("fifteen"))
51             // TODO(ronshapiro): remove the type parameter when we drop java7 support
52             .put(42, Producers.<String>immediateFailedProducer(cause))
53             .build();
54     try {
55       mapProducer.get().get();
56       fail();
57     } catch (ExecutionException e) {
58       assertThat(e).hasCauseThat().isSameInstanceAs(cause);
59     }
60   }
61 }
62