1 /** 2 * Copyright (C) 2008 Google Inc. 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 17 package com.google.inject; 18 19 import com.google.common.collect.ImmutableList; 20 import com.google.inject.spi.ElementSource; 21 import com.google.inject.util.Modules; 22 23 import junit.framework.TestCase; 24 25 import java.util.Arrays; 26 27 /** 28 * @author jessewilson@google.com (Jesse Wilson) 29 */ 30 public class ModulesTest extends TestCase { 31 testCombineVarargs()32 public void testCombineVarargs() { 33 Module combined = Modules.combine(newModule(1), newModule(2L), newModule((short) 3)); 34 Injector injector = Guice.createInjector(combined); 35 assertEquals(1, injector.getInstance(Integer.class).intValue()); 36 assertEquals(2L, injector.getInstance(Long.class).longValue()); 37 assertEquals(3, injector.getInstance(Short.class).shortValue()); 38 } 39 testCombineIterable()40 public void testCombineIterable() { 41 Iterable<Module> modules = Arrays.asList(newModule(1), newModule(2L), newModule((short) 3)); 42 Injector injector = Guice.createInjector(Modules.combine(modules)); 43 assertEquals(1, injector.getInstance(Integer.class).intValue()); 44 assertEquals(2, injector.getInstance(Long.class).longValue()); 45 assertEquals(3, injector.getInstance(Short.class).shortValue()); 46 } 47 48 /** 49 * The module returned by Modules.combine shouldn't show up in binder sources. 50 */ testCombineSources()51 public void testCombineSources() { 52 final Module m1 = newModule(1); 53 final Module m2 = newModule(2L); 54 final Module combined1 = Modules.combine(m1, m2); 55 Module skipSourcesModule = new AbstractModule() { 56 @Override protected void configure() { 57 install(combined1); 58 } 59 }; 60 final Module combined2 = Modules.combine(skipSourcesModule); 61 Injector injector = Guice.createInjector(combined2); 62 ElementSource source = (ElementSource) injector.getBinding(Integer.class).getSource(); 63 assertEquals(source.getModuleClassNames().size(), 4); 64 assertEquals(ImmutableList.of(m1.getClass().getName(), 65 combined1.getClass().getName(), skipSourcesModule.getClass().getName(), 66 combined2.getClass().getName()), source.getModuleClassNames()); 67 StackTraceElement stackTraceElement = (StackTraceElement) source.getDeclaringSource(); 68 assertEquals(skipSourcesModule.getClass().getName(), stackTraceElement.getClassName()); 69 } 70 newModule(final T toBind)71 private <T> Module newModule(final T toBind) { 72 return new AbstractModule() { 73 protected void configure() { 74 @SuppressWarnings("unchecked") // getClass always needs a cast 75 Class<T> tClass = (Class<T>) toBind.getClass(); 76 binder().skipSources(getClass()).bind(tClass).toInstance(toBind); 77 } 78 }; 79 } 80 } 81