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.producers.internal; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.fail; 21 22 import com.google.common.util.concurrent.Futures; 23 import com.google.common.util.concurrent.ListenableFuture; 24 import dagger.producers.Producer; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.junit.runners.JUnit4; 28 29 /** 30 * Tests {@link AbstractProducer}. 31 */ 32 @RunWith(JUnit4.class) 33 public class AbstractProducerTest { 34 @Test 35 @SuppressWarnings("CheckReturnValue") get_nullPointerException()36 public void get_nullPointerException() { 37 Producer<Object> producer = new DelegateProducer<>(null); 38 try { 39 producer.get(); 40 fail(); 41 } catch (NullPointerException expected) { 42 } 43 } 44 get()45 @Test public void get() throws Exception { 46 Producer<Integer> producer = 47 new AbstractProducer<Integer>() { 48 int i = 0; 49 50 @Override 51 public ListenableFuture<Integer> compute() { 52 return Futures.immediateFuture(i++); 53 } 54 }; 55 assertThat(producer.get().get()).isEqualTo(0); 56 assertThat(producer.get().get()).isEqualTo(0); 57 assertThat(producer.get().get()).isEqualTo(0); 58 } 59 60 static final class DelegateProducer<T> extends AbstractProducer<T> { 61 private final ListenableFuture<T> delegate; 62 DelegateProducer(ListenableFuture<T> delegate)63 DelegateProducer(ListenableFuture<T> delegate) { 64 this.delegate = delegate; 65 } 66 67 @Override compute()68 public ListenableFuture<T> compute() { 69 return delegate; 70 } 71 } 72 } 73