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;
32 public void inject(Object aComponent) {
33 throw new RuntimeException("not implemented");
36 public String getClassname() {
42 public void testCache() {
43 InjectorFactory factory = new InjectorFactory() {
46 public Injector create(Class aClass) {
47 return new MyInjector(aClass.getName());
50 InjectorCache cache = new InjectorCache(factory);
51 // Unrealistic exmaple, but sufficient for test.
53 Injector injector = cache.getInjector(String.class);
54 assertEquals(String.class.getName(), ((MyInjector) injector)
57 Injector injector2 = cache.getInjector(String.class);
58 assertSame(injector, injector2);
60 // verify we get another one for another class.
61 Injector injector3 = cache.getInjector(Long.class);
62 assertEquals(Long.class.getName(), ((MyInjector) injector3)