2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.inject;
18 import static junit.framework.Assert.*;
20 import org.junit.Test;
22 public class InjectorCacheTest {
24 private static class MyInjector implements Injector {
25 private String classname;
27 public MyInjector(String aClassname) {
28 classname = aClassname;
31 public void inject(Object aComponent) {
32 throw new RuntimeException("not implemented");
34 public String getClassname() {
40 public void testCache() {
41 InjectorFactory factory = new InjectorFactory() {
44 public Injector create(Class aClass) {
45 return new MyInjector(aClass.getName());
48 InjectorCache cache = new InjectorCache(factory);
49 // Unrealistic exmaple, but sufficient for test.
51 Injector injector = cache.getInjector(String.class);
52 assertEquals(String.class.getName(), ((MyInjector)injector).getClassname());
54 Injector injector2 = cache.getInjector(String.class);
55 assertSame(injector, injector2);
57 // verify we get another one for another class.
58 Injector injector3 = cache.getInjector(Long.class);
59 assertEquals(Long.class.getName(), ((MyInjector)injector3).getClassname());