/* * Copyright 2005-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wamblee.inject; import static junit.framework.Assert.*; import org.junit.Test; public class InjectorCacheTest { private static class MyInjector implements Injector { private String classname; public MyInjector(String aClassname) { classname = aClassname; } @Override public void inject(Object aComponent) { throw new RuntimeException("not implemented"); } public String getClassname() { return classname; } } @Test public void testCache() { InjectorFactory factory = new InjectorFactory() { @Override public Injector create(Class aClass) { return new MyInjector(aClass.getName()); } }; InjectorCache cache = new InjectorCache(factory); // Unrealistic exmaple, but sufficient for test. Injector injector = cache.getInjector(String.class); assertEquals(String.class.getName(), ((MyInjector) injector) .getClassname()); Injector injector2 = cache.getInjector(String.class); assertSame(injector, injector2); // verify we get another one for another class. Injector injector3 = cache.getInjector(Long.class); assertEquals(Long.class.getName(), ((MyInjector) injector3) .getClassname()); } }