Home | History | Annotate | Download | only in inject
      1 /*
      2  * Copyright (C) 2006 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 /** @author crazybob (at) google.com (Bob Lee) */
     22 public class BoundProviderTest extends TestCase {
     23 
     24   public void testFooProvider() throws CreationException {
     25     Injector injector =
     26         Guice.createInjector(
     27             new AbstractModule() {
     28               @Override
     29               protected void configure() {
     30                 bind(Foo.class).toProvider(FooProvider.class);
     31               }
     32             });
     33 
     34     Foo a = injector.getInstance(Foo.class);
     35     Foo b = injector.getInstance(Foo.class);
     36 
     37     assertEquals(0, a.i);
     38     assertEquals(0, b.i);
     39     assertNotNull(a.bar);
     40     assertNotNull(b.bar);
     41     assertNotSame(a.bar, b.bar);
     42   }
     43 
     44   public void testSingletonFooProvider() throws CreationException {
     45     Injector injector =
     46         Guice.createInjector(
     47             new AbstractModule() {
     48               @Override
     49               protected void configure() {
     50                 bind(Foo.class).toProvider(SingletonFooProvider.class);
     51               }
     52             });
     53 
     54     Foo a = injector.getInstance(Foo.class);
     55     Foo b = injector.getInstance(Foo.class);
     56 
     57     assertEquals(0, a.i);
     58     assertEquals(1, b.i);
     59     assertNotNull(a.bar);
     60     assertNotNull(b.bar);
     61     assertSame(a.bar, b.bar);
     62   }
     63 
     64   static class Bar {}
     65 
     66   static class Foo {
     67     final Bar bar;
     68     final int i;
     69 
     70     Foo(Bar bar, int i) {
     71       this.bar = bar;
     72       this.i = i;
     73     }
     74   }
     75 
     76   static class FooProvider implements Provider<Foo> {
     77 
     78     final Bar bar;
     79     int count = 0;
     80 
     81     @Inject
     82     public FooProvider(Bar bar) {
     83       this.bar = bar;
     84     }
     85 
     86     @Override
     87     public Foo get() {
     88       return new Foo(this.bar, count++);
     89     }
     90   }
     91 
     92   @Singleton
     93   static class SingletonFooProvider implements Provider<Foo> {
     94 
     95     final Bar bar;
     96     int count = 0;
     97 
     98     @Inject
     99     public SingletonFooProvider(Bar bar) {
    100       this.bar = bar;
    101     }
    102 
    103     @Override
    104     public Foo get() {
    105       return new Foo(this.bar, count++);
    106     }
    107   }
    108 }
    109