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