PageRenderTime 124ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/core/test/com/google/inject/ModulesTest.java

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