Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright (C) 2014 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  */
     16 package dagger.internal;
     17 
     18 import javax.inject.Provider;
     19 import org.junit.Ignore;
     20 import org.junit.Test;
     21 import org.junit.runner.RunWith;
     22 import org.junit.runners.JUnit4;
     23 
     24 import static com.google.common.truth.Truth.assert_;
     25 import static org.junit.Assert.fail;
     26 
     27 /**
     28  * Tests {@link ScopedProvider}.
     29  */
     30 @RunWith(JUnit4.class)
     31 public class ScopedProviderTest {
     32   @Test public void create_nullPointerException() {
     33     try {
     34       ScopedProvider.create(null);
     35       fail();
     36     } catch (NullPointerException expected) { }
     37   }
     38 
     39   // TODO(gak): reenable this test once we can ensure that factories are no longer providing null
     40   @Ignore @Test public void get_nullPointerException() {
     41     Provider<Object> scopedProvider = ScopedProvider.create(new Factory<Object>() {
     42       @Override public Object get() {
     43         return null;
     44       }
     45     });
     46     try {
     47       scopedProvider.get();
     48       fail();
     49     } catch (NullPointerException expected) {
     50     }
     51   }
     52 
     53   @Test public void get() {
     54     Provider<Integer> scopedProvider = ScopedProvider.create(new Factory<Integer>() {
     55       int i = 0;
     56 
     57       @Override public Integer get() {
     58         return i++;
     59       }
     60     });
     61     assert_().that(scopedProvider.get()).isEqualTo(0);
     62     assert_().that(scopedProvider.get()).isEqualTo(0);
     63     assert_().that(scopedProvider.get()).isEqualTo(0);
     64   }
     65 }
     66