View Javadoc

1   package org.apache.onami.scopes;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertSame;
24  
25  import org.apache.onami.lifecycle.standard.AfterInjectionModule;
26  import org.apache.onami.test.OnamiRunner;
27  import org.junit.Before;
28  import org.junit.Test;
29  import org.junit.runner.RunWith;
30  
31  import com.google.inject.Binder;
32  import com.google.inject.Guice;
33  import com.google.inject.Inject;
34  import com.google.inject.Injector;
35  import com.google.inject.Module;
36  import com.google.inject.Provider;
37  
38  @RunWith(OnamiRunner.class)
39  public class TestLazySingleton
40  {
41      public static class InjectedProvider
42      {
43          public final Provider<LazySingletonObject> provider;
44  
45          @Inject
46          public InjectedProvider( Provider<LazySingletonObject> provider )
47          {
48              this.provider = provider;
49          }
50      }
51  
52      public static class InjectedAnnotatedProvider
53      {
54          public final Provider<AnnotatedLazySingletonObject> provider;
55  
56          @Inject
57          public InjectedAnnotatedProvider( Provider<AnnotatedLazySingletonObject> provider )
58          {
59              this.provider = provider;
60          }
61      }
62  
63      @Before
64      public void setup()
65      {
66          AnnotatedLazySingletonObject.constructorCount.set( 0 );
67          AnnotatedLazySingletonObject.postConstructCount.set( 0 );
68          LazySingletonObject.constructorCount.set( 0 );
69          LazySingletonObject.postConstructCount.set( 0 );
70      }
71  
72      @Test
73      public void testUsingAnnotation()
74      {
75          Injector injector = Guice.createInjector( new AfterInjectionModule(), new ScopesModule() );
76  
77          assertEquals( AnnotatedLazySingletonObject.constructorCount.get(), 0 );
78          assertEquals( AnnotatedLazySingletonObject.postConstructCount.get(), 0 );
79  
80          AnnotatedLazySingletonObject instance = injector.getInstance( AnnotatedLazySingletonObject.class );
81          assertEquals( AnnotatedLazySingletonObject.constructorCount.get(), 1 );
82          assertEquals( AnnotatedLazySingletonObject.postConstructCount.get(), 1 );
83  
84          AnnotatedLazySingletonObject instance2 = injector.getInstance( AnnotatedLazySingletonObject.class );
85          assertEquals( AnnotatedLazySingletonObject.constructorCount.get(), 1 );
86          assertEquals( AnnotatedLazySingletonObject.postConstructCount.get(), 1 );
87  
88          assertSame( instance, instance2 );
89      }
90  
91      @Test
92      public void testUsingInWithProviderAndAnnotation()
93      {
94          Injector injector = Guice.createInjector( new AfterInjectionModule(), new ScopesModule() );
95  
96          assertEquals( AnnotatedLazySingletonObject.constructorCount.get(), 0 );
97          assertEquals( AnnotatedLazySingletonObject.postConstructCount.get(), 0 );
98  
99          InjectedAnnotatedProvider injectedProvider = injector.getInstance( InjectedAnnotatedProvider.class );
100         assertEquals( AnnotatedLazySingletonObject.constructorCount.get(), 0 );
101         assertEquals( AnnotatedLazySingletonObject.postConstructCount.get(), 0 );
102 
103         AnnotatedLazySingletonObject instance = injectedProvider.provider.get();
104         assertEquals( AnnotatedLazySingletonObject.constructorCount.get(), 1 );
105         assertEquals( AnnotatedLazySingletonObject.postConstructCount.get(), 1 );
106 
107         AnnotatedLazySingletonObject instance2 = injectedProvider.provider.get();
108         assertEquals( AnnotatedLazySingletonObject.constructorCount.get(), 1 );
109         assertEquals( AnnotatedLazySingletonObject.postConstructCount.get(), 1 );
110 
111         assertSame( instance, instance2 );
112     }
113 
114     @Test
115     public void testUsingIn()
116     {
117         Module module = new Module()
118         {
119             public void configure( Binder binder )
120             {
121                 binder.bind( LazySingletonObject.class ).in( LazySingletonScope.get() );
122             }
123         };
124         Injector injector = Guice.createInjector( new AfterInjectionModule(), new ScopesModule(), module );
125 
126         assertEquals( LazySingletonObject.constructorCount.get(), 0 );
127         assertEquals( LazySingletonObject.postConstructCount.get(), 0 );
128 
129         LazySingletonObject instance = injector.getInstance( LazySingletonObject.class );
130         assertEquals( LazySingletonObject.constructorCount.get(), 1 );
131         assertEquals( LazySingletonObject.postConstructCount.get(), 1 );
132 
133         LazySingletonObject instance2 = injector.getInstance( LazySingletonObject.class );
134         assertEquals( LazySingletonObject.constructorCount.get(), 1 );
135         assertEquals( LazySingletonObject.postConstructCount.get(), 1 );
136 
137         assertSame( instance, instance2 );
138     }
139 
140     @Test
141     public void testUsingInWithProvider()
142     {
143         Module module = new Module()
144         {
145             public void configure( Binder binder )
146             {
147                 binder.bind( LazySingletonObject.class ).in( LazySingletonScope.get() );
148             }
149         };
150         Injector injector = Guice.createInjector( new AfterInjectionModule(), new ScopesModule(), module );
151 
152         assertEquals( LazySingletonObject.constructorCount.get(), 0 );
153         assertEquals( LazySingletonObject.postConstructCount.get(), 0 );
154 
155         InjectedProvider injectedProvider = injector.getInstance( InjectedProvider.class );
156         assertEquals( LazySingletonObject.constructorCount.get(), 0 );
157         assertEquals( LazySingletonObject.postConstructCount.get(), 0 );
158 
159         LazySingletonObject instance = injectedProvider.provider.get();
160         assertEquals( LazySingletonObject.constructorCount.get(), 1 );
161         assertEquals( LazySingletonObject.postConstructCount.get(), 1 );
162 
163         LazySingletonObject instance2 = injectedProvider.provider.get();
164         assertEquals( LazySingletonObject.constructorCount.get(), 1 );
165         assertEquals( LazySingletonObject.postConstructCount.get(), 1 );
166 
167         assertSame( instance, instance2 );
168     }
169 }