(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
31         @Override
32         public void inject(Object aComponent) {
33             throw new RuntimeException("not implemented");
34         }
35
36         public String getClassname() {
37             return classname;
38         }
39     }
40
41     @Test
42     public void testCache() {
43         InjectorFactory factory = new InjectorFactory() {
44
45             @Override
46             public Injector create(Class aClass) {
47                 return new MyInjector(aClass.getName());
48             }
49         };
50         InjectorCache cache = new InjectorCache(factory);
51         // Unrealistic exmaple, but sufficient for test.
52
53         Injector injector = cache.getInjector(String.class);
54         assertEquals(String.class.getName(), ((MyInjector) injector)
55             .getClassname());
56
57         Injector injector2 = cache.getInjector(String.class);
58         assertSame(injector, injector2);
59
60         // verify we get another one for another class.
61         Injector injector3 = cache.getInjector(Long.class);
62         assertEquals(Long.class.getName(), ((MyInjector) injector3)
63             .getClassname());
64
65     }
66 }