1/*
2 * Copyright (C) 2015 Google, Inc.
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 */
16package dagger.producers;
17
18import com.google.common.testing.EqualsTester;
19import java.util.concurrent.CancellationException;
20import java.util.concurrent.ExecutionException;
21import org.junit.Test;
22import org.junit.runner.RunWith;
23import org.junit.runners.JUnit4;
24
25import static com.google.common.truth.Truth.assertThat;
26import static org.junit.Assert.fail;
27
28/**
29 * Tests {@link Produced}.
30 */
31@RunWith(JUnit4.class)
32public class ProducedTest {
33  @Test public void successfulProduced() throws ExecutionException {
34    Object o = new Object();
35    assertThat(Produced.successful(5).get()).isEqualTo(5);
36    assertThat(Produced.successful("monkey").get()).isEqualTo("monkey");
37    assertThat(Produced.successful(o).get()).isSameAs(o);
38  }
39
40  @Test public void failedProduced() {
41    RuntimeException cause = new RuntimeException("monkey");
42    try {
43      Produced.failed(cause).get();
44      fail();
45    } catch (ExecutionException e) {
46      assertThat(e.getCause()).isSameAs(cause);
47    }
48  }
49
50  @Test public void producedEquivalence() {
51    RuntimeException e1 = new RuntimeException("monkey");
52    RuntimeException e2 = new CancellationException();
53    new EqualsTester()
54        .addEqualityGroup(Produced.successful(132435), Produced.successful(132435))
55        .addEqualityGroup(Produced.successful("hi"), Produced.successful("hi"))
56        .addEqualityGroup(Produced.failed(e1), Produced.failed(e1))
57        .addEqualityGroup(Produced.failed(e2), Produced.failed(e2))
58        .testEquals();
59  }
60}
61