View Javadoc

1   /**
2    Licensed to the Apache Software Foundation (ASF) under one or more
3    contributor license agreements.  See the NOTICE file distributed with
4    this work for additional information regarding copyright ownership.
5    The ASF licenses this file to You under the Apache License, Version 2.0
6    (the "License"); you may not use this file except in compliance with
7    the License.  You may obtain a copy of the License at
8   
9         http://www.apache.org/licenses/LICENSE-2.0
10  
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16  */
17  package org.apache.commons.inject.impl;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.annotation.PostConstruct;
23  import javax.annotation.PreDestroy;
24  
25  import junit.framework.Assert;
26  
27  import org.apache.commons.inject.api.CommonsInject;
28  import org.apache.commons.inject.api.IInjector;
29  import org.apache.commons.inject.api.IKey;
30  import org.apache.commons.inject.api.ILifecycleController;
31  import org.apache.commons.inject.api.PostConstructModule;
32  import org.apache.commons.inject.api.bind.IBinder;
33  import org.apache.commons.inject.api.bind.IModule;
34  import org.apache.commons.inject.api.bind.Scopes;
35  import org.junit.Test;
36  
37  public class ListenerTest {
38  	private static class ListenerModule implements IModule {
39  		private boolean injectorBuilListenerCalled;
40  		private final List<Object> initializedObjects  = new ArrayList<Object>();
41  		@Override
42  		public void configure(IBinder pBinder) {
43  			pBinder.add(new IBinder.IInjectorBuildListener() {
44  				@Override
45  				public void created(IInjector pInjector) {
46  					injectorBuilListenerCalled = true;
47  				}
48  			});
49  			pBinder.add(new IBinder.IInjectionListener() {
50  				@Override
51  				public void initialized(IKey<?> pKey, Object pObject) {
52  					initializedObjects.add(pObject);
53  				}
54  			});
55  			pBinder.bind(TimeRecordingObject.class, "perCall").scope(Scopes.PER_CALL);
56  			pBinder.bind(TimeRecordingObject.class, "lazy").asLazySingleton();
57  			pBinder.bind(TimeRecordingObject.class, "eager").asEagerSingleton();
58  		}
59  
60  		boolean isInitialized() {
61  			return injectorBuilListenerCalled;
62  		}
63  		List<Object> getInitializedObjects(){
64  			return initializedObjects;
65  		}
66  	}
67  	public static class InitializableObject {
68  		private final long timeOfCreation = System.currentTimeMillis();
69  		private long timeOfInitialization, timeOfShutdown;
70  		private int state;
71  
72  		@PostConstruct
73  		public void start() {
74  			state = 1;
75  			timeOfInitialization = System.currentTimeMillis();
76  		}
77  
78  		@PreDestroy
79  		public void shutdown() {
80  			state = 2;
81  			timeOfShutdown = System.currentTimeMillis();
82  		}
83  
84  		public void assertStarted() {
85  			Assert.assertTrue(state > 0);
86  			Assert.assertTrue(timeOfInitialization >= timeOfCreation);
87  		}
88  
89  		public void assertTerminated() {
90  			assertStarted();
91  			Assert.assertTrue(state > 1);
92  			Assert.assertTrue(timeOfShutdown >= timeOfInitialization);
93  			
94  		}
95  	}
96  
97  	@Test
98  	public void testListeners() {
99  		final ListenerModule module = new ListenerModule();
100 		final IInjector injector = CommonsInject.build(module);
101 		Assert.assertTrue(module.isInitialized());
102 		final Object perCall1 = injector.requireInstance(TimeRecordingObject.class, "perCall");
103 		final Object perCall2 = injector.requireInstance(TimeRecordingObject.class, "perCall");
104 		final Object lazy1 = injector.requireInstance(TimeRecordingObject.class, "lazy");
105 		final Object lazy2 = injector.requireInstance(TimeRecordingObject.class, "lazy");
106 		final Object eager1 = injector.requireInstance(TimeRecordingObject.class, "eager");
107 		final Object eager2 = injector.requireInstance(TimeRecordingObject.class, "eager");
108 		Assert.assertSame(eager1, eager2);
109 		Assert.assertSame(lazy1, lazy2);
110 		Assert.assertNotSame(perCall1, perCall2);
111 		List<Object> initializedObjects = module.getInitializedObjects();
112 		Assert.assertEquals(4, initializedObjects.size());
113 		Assert.assertSame(eager1, initializedObjects.get(0));
114 		Assert.assertSame(perCall1, initializedObjects.get(1));
115 		Assert.assertSame(perCall2, initializedObjects.get(2));
116 		Assert.assertSame(lazy1, initializedObjects.get(3));
117 	}
118 
119 	@Test
120 	public void testPostConstruct() {
121 		final PostConstructModule module0 = new PostConstructModule();
122 		final IModule module1 = new IModule(){
123 			@Override
124 			public void configure(IBinder pBinder) {
125 				pBinder.bind(InitializableObject.class, "perCall").scope(Scopes.PER_CALL);
126 				pBinder.bind(InitializableObject.class, "lazy").asLazySingleton();
127 				pBinder.bind(InitializableObject.class, "eager").asEagerSingleton();
128 			}
129 		};
130 		final ILifecycleController controller = module0.getLifecycleController();
131 		controller.start();
132 		final IInjector injector = CommonsInject.build(module0, module1);
133 		final InitializableObject perCall1 = injector.requireInstance(InitializableObject.class, "perCall");
134 		final InitializableObject perCall2 = injector.requireInstance(InitializableObject.class, "perCall");
135 		final InitializableObject lazy1 = injector.requireInstance(InitializableObject.class, "lazy");
136 		final InitializableObject lazy2 = injector.requireInstance(InitializableObject.class, "lazy");
137 		final InitializableObject eager1 = injector.requireInstance(InitializableObject.class, "eager");
138 		final InitializableObject eager2 = injector.requireInstance(InitializableObject.class, "eager");
139 		Assert.assertSame(eager1, eager2);
140 		Assert.assertSame(lazy1, lazy2);
141 		Assert.assertNotSame(perCall1, perCall2);
142 		perCall1.assertStarted();
143 		perCall2.assertStarted();
144 		eager1.assertStarted();
145 		lazy1.assertStarted();
146 		controller.shutdown();
147 		perCall1.assertTerminated();
148 		perCall2.assertTerminated();
149 		eager1.assertTerminated();
150 		lazy1.assertTerminated();
151 	}
152 }