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