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