PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/metamorphiccode/guice
Java | 73 lines | 55 code | 13 blank | 5 comment | 0 complexity | 9db20c6f32314b94a33ec396f86719a3 MD5 | raw file
  1. package com.google.inject;
  2. import static com.google.inject.Asserts.assertContains;
  3. import com.google.inject.name.Names;
  4. import junit.framework.TestCase;
  5. import java.util.logging.Logger;
  6. /**
  7. * Test built-in injection of loggers.
  8. *
  9. * @author jessewilson
  10. */
  11. public class LoggerInjectionTest extends TestCase {
  12. @Inject Logger logger;
  13. public void testLoggerWithMember() {
  14. Injector injector = Guice.createInjector();
  15. injector.injectMembers(this);
  16. assertEquals("com.google.inject.LoggerInjectionTest", logger.getName());
  17. }
  18. public void testLoggerInConstructor() {
  19. Injector injector = Guice.createInjector();
  20. Foo foo = injector.getInstance(Foo.class);
  21. assertEquals("com.google.inject.LoggerInjectionTest$Foo", foo.logger.getName());
  22. }
  23. private static class Foo {
  24. Logger logger;
  25. @SuppressWarnings("unused")
  26. @Inject Foo(Logger logger) {
  27. this.logger = logger;
  28. }
  29. }
  30. public void testLoggerWithoutMember() {
  31. Injector injector = Guice.createInjector();
  32. assertNull(injector.getInstance(Logger.class).getName());
  33. assertNull(injector.getProvider(Logger.class).get().getName());
  34. assertNull(injector.getBinding(Logger.class).getProvider().get().getName());
  35. assertEquals("Provider<Logger>", injector.getProvider(Logger.class).toString());
  36. }
  37. public void testCanBindAnnotatedLogger() {
  38. Injector injector = Guice.createInjector(new AbstractModule() {
  39. protected void configure() {
  40. bind(Logger.class)
  41. .annotatedWith(Names.named("anonymous"))
  42. .toInstance(Logger.getAnonymousLogger());
  43. }
  44. });
  45. assertNull(injector.getInstance(Key.get(Logger.class, Names.named("anonymous"))).getName());
  46. }
  47. public void testCannotBindLogger() {
  48. try {
  49. Guice.createInjector(new AbstractModule() {
  50. protected void configure() {
  51. bind(Logger.class).toInstance(Logger.getAnonymousLogger());
  52. }
  53. });
  54. fail();
  55. } catch (CreationException expected) {
  56. assertContains(expected.getMessage(),
  57. "A binding to java.util.logging.Logger was already configured");
  58. }
  59. }
  60. }