(no commit message)
[utils] / support / inject / src / test / java / org / wamblee / inject / InjectorCacheTest.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
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 package org.wamblee.inject;
17
18 import static junit.framework.Assert.*;
19
20 import org.junit.Test;
21
22 public class InjectorCacheTest {
23     
24     private static class MyInjector implements Injector { 
25         private String classname; 
26         
27         public MyInjector(String aClassname) { 
28             classname = aClassname; 
29         }
30         @Override
31         public void inject(Object aComponent) {
32             throw new RuntimeException("not implemented");
33         }
34         public String getClassname() {
35             return classname;
36         }
37     }
38
39     @Test
40     public void testCache() { 
41         InjectorFactory factory = new InjectorFactory() {
42             
43             @Override
44             public Injector create(Class aClass) {
45                 return new MyInjector(aClass.getName());
46             }
47         };
48         InjectorCache cache = new InjectorCache(factory); 
49         // Unrealistic exmaple, but sufficient for test.
50       
51         Injector injector = cache.getInjector(String.class);
52         assertEquals(String.class.getName(), ((MyInjector)injector).getClassname());
53         
54         Injector injector2 = cache.getInjector(String.class); 
55         assertSame(injector, injector2);
56         
57         // verify we get another one for another class. 
58         Injector injector3 = cache.getInjector(Long.class);
59         assertEquals(Long.class.getName(), ((MyInjector)injector3).getClassname());
60         
61     }
62 }