1*f585d8a3SJacky Wang /* 2*f585d8a3SJacky Wang * Copyright (C) 2014 The Dagger Authors. 3*f585d8a3SJacky Wang * 4*f585d8a3SJacky Wang * Licensed under the Apache License, Version 2.0 (the "License"); 5*f585d8a3SJacky Wang * you may not use this file except in compliance with the License. 6*f585d8a3SJacky Wang * You may obtain a copy of the License at 7*f585d8a3SJacky Wang * 8*f585d8a3SJacky Wang * http://www.apache.org/licenses/LICENSE-2.0 9*f585d8a3SJacky Wang * 10*f585d8a3SJacky Wang * Unless required by applicable law or agreed to in writing, software 11*f585d8a3SJacky Wang * distributed under the License is distributed on an "AS IS" BASIS, 12*f585d8a3SJacky Wang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*f585d8a3SJacky Wang * See the License for the specific language governing permissions and 14*f585d8a3SJacky Wang * limitations under the License. 15*f585d8a3SJacky Wang */ 16*f585d8a3SJacky Wang 17*f585d8a3SJacky Wang package dagger.producers.internal; 18*f585d8a3SJacky Wang 19*f585d8a3SJacky Wang import static com.google.common.truth.Truth.assertThat; 20*f585d8a3SJacky Wang import static org.junit.Assert.fail; 21*f585d8a3SJacky Wang 22*f585d8a3SJacky Wang import com.google.common.util.concurrent.Futures; 23*f585d8a3SJacky Wang import com.google.common.util.concurrent.ListenableFuture; 24*f585d8a3SJacky Wang import dagger.producers.Producer; 25*f585d8a3SJacky Wang import org.junit.Test; 26*f585d8a3SJacky Wang import org.junit.runner.RunWith; 27*f585d8a3SJacky Wang import org.junit.runners.JUnit4; 28*f585d8a3SJacky Wang 29*f585d8a3SJacky Wang /** 30*f585d8a3SJacky Wang * Tests {@link AbstractProducer}. 31*f585d8a3SJacky Wang */ 32*f585d8a3SJacky Wang @RunWith(JUnit4.class) 33*f585d8a3SJacky Wang public class AbstractProducerTest { 34*f585d8a3SJacky Wang @Test 35*f585d8a3SJacky Wang @SuppressWarnings("CheckReturnValue") get_nullPointerException()36*f585d8a3SJacky Wang public void get_nullPointerException() { 37*f585d8a3SJacky Wang Producer<Object> producer = new DelegateProducer<>(null); 38*f585d8a3SJacky Wang try { 39*f585d8a3SJacky Wang producer.get(); 40*f585d8a3SJacky Wang fail(); 41*f585d8a3SJacky Wang } catch (NullPointerException expected) { 42*f585d8a3SJacky Wang } 43*f585d8a3SJacky Wang } 44*f585d8a3SJacky Wang get()45*f585d8a3SJacky Wang @Test public void get() throws Exception { 46*f585d8a3SJacky Wang Producer<Integer> producer = 47*f585d8a3SJacky Wang new AbstractProducer<Integer>() { 48*f585d8a3SJacky Wang int i = 0; 49*f585d8a3SJacky Wang 50*f585d8a3SJacky Wang @Override 51*f585d8a3SJacky Wang public ListenableFuture<Integer> compute() { 52*f585d8a3SJacky Wang return Futures.immediateFuture(i++); 53*f585d8a3SJacky Wang } 54*f585d8a3SJacky Wang }; 55*f585d8a3SJacky Wang assertThat(producer.get().get()).isEqualTo(0); 56*f585d8a3SJacky Wang assertThat(producer.get().get()).isEqualTo(0); 57*f585d8a3SJacky Wang assertThat(producer.get().get()).isEqualTo(0); 58*f585d8a3SJacky Wang } 59*f585d8a3SJacky Wang 60*f585d8a3SJacky Wang static final class DelegateProducer<T> extends AbstractProducer<T> { 61*f585d8a3SJacky Wang private final ListenableFuture<T> delegate; 62*f585d8a3SJacky Wang DelegateProducer(ListenableFuture<T> delegate)63*f585d8a3SJacky Wang DelegateProducer(ListenableFuture<T> delegate) { 64*f585d8a3SJacky Wang this.delegate = delegate; 65*f585d8a3SJacky Wang } 66*f585d8a3SJacky Wang 67*f585d8a3SJacky Wang @Override compute()68*f585d8a3SJacky Wang public ListenableFuture<T> compute() { 69*f585d8a3SJacky Wang return delegate; 70*f585d8a3SJacky Wang } 71*f585d8a3SJacky Wang } 72*f585d8a3SJacky Wang } 73