Home | History | Annotate | Download | only in inject
      1 /**
      2  * Copyright (C) 2008 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 
     17 package com.google.inject;
     18 
     19 import junit.framework.TestCase;
     20 
     21 /**
     22  * @author jessewilson (at) google.com (Jesse Wilson)
     23  */
     24 public class EagerSingletonTest extends TestCase {
     25 
     26   @Override public void setUp() {
     27     A.instanceCount = 0;
     28     B.instanceCount = 0;
     29     C.instanceCount = 0;
     30   }
     31 
     32   public void testJustInTimeEagerSingletons() {
     33     Guice.createInjector(Stage.PRODUCTION, new AbstractModule() {
     34       protected void configure() {
     35         // create a just-in-time binding for A
     36         getProvider(A.class);
     37 
     38         // create a just-in-time binding for C
     39         requestInjection(new Object() {
     40           @Inject void inject(Injector injector) {
     41             injector.getInstance(C.class);
     42           }
     43         });
     44       }
     45     });
     46 
     47     assertEquals(1, A.instanceCount);
     48     assertEquals("Singletons discovered when creating singletons should not be built eagerly",
     49         0, B.instanceCount);
     50     assertEquals(1, C.instanceCount);
     51   }
     52 
     53   public void testJustInTimeSingletonsAreNotEager() {
     54     Injector injector = Guice.createInjector(Stage.PRODUCTION);
     55     injector.getProvider(B.class);
     56     assertEquals(0, B.instanceCount);
     57   }
     58 
     59   public void testChildEagerSingletons() {
     60     Injector parent = Guice.createInjector(Stage.PRODUCTION);
     61     parent.createChildInjector(new AbstractModule() {
     62       @Override protected void configure() {
     63         bind(D.class).to(C.class);
     64       }
     65     });
     66 
     67     assertEquals(1, C.instanceCount);
     68   }
     69 
     70   @Singleton
     71   static class A {
     72     static int instanceCount = 0;
     73     int instanceId = instanceCount++;
     74 
     75     @Inject A(Injector injector) {
     76       injector.getProvider(B.class);
     77     }
     78   }
     79 
     80   @Singleton
     81   static class B {
     82     static int instanceCount = 0;
     83     int instanceId = instanceCount++;
     84   }
     85 
     86   @Singleton
     87   static class C implements D {
     88     static int instanceCount = 0;
     89     int instanceId = instanceCount++;
     90   }
     91 
     92   private static interface D {}
     93 }
     94