• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 MapOfProducerProducerTest {
32   @Test
success()33   public void success() throws Exception {
34     MapOfProducerProducer<Integer, String> mapOfProducerProducer =
35         MapOfProducerProducer.<Integer, String>builder(2)
36             .put(15, Producers.<String>immediateProducer("fifteen"))
37             .put(42, Producers.<String>immediateProducer("forty two"))
38             .build();
39     Map<Integer, Producer<String>> map = mapOfProducerProducer.get().get();
40     assertThat(map).hasSize(2);
41     assertThat(map).containsKey(15);
42     assertThat(map.get(15).get().get()).isEqualTo("fifteen");
43     assertThat(map).containsKey(42);
44     assertThat(map.get(42).get().get()).isEqualTo("forty two");
45   }
46 
47   @Test
failingContributionDoesNotFailMap()48   public void failingContributionDoesNotFailMap() throws Exception {
49     RuntimeException cause = new RuntimeException("monkey");
50     MapOfProducerProducer<Integer, String> mapOfProducerProducer =
51         MapOfProducerProducer.<Integer, String>builder(2)
52             .put(15, Producers.<String>immediateProducer("fifteen"))
53             .put(42, Producers.<String>immediateFailedProducer(cause))
54             .build();
55     Map<Integer, Producer<String>> map = mapOfProducerProducer.get().get();
56     assertThat(map).hasSize(2);
57     assertThat(map).containsKey(15);
58     assertThat(map.get(15).get().get()).isEqualTo("fifteen");
59     assertThat(map).containsKey(42);
60     try {
61       map.get(42).get().get();
62       fail();
63     } catch (ExecutionException e) {
64       assertThat(e).hasCauseThat().isSameInstanceAs(cause);
65     }
66   }
67 }
68