/core/src/test/java/org/jclouds/logging/config/BindLoggersAnnotatedWithResourceTest.java

https://github.com/regularfry/jclouds · Java · 142 lines · 99 code · 25 blank · 18 comment · 0 complexity · 47e5d255adf0636138e53723dd8c61c6 MD5 · raw file

  1. /**
  2. * Licensed to jclouds, Inc. (jclouds) under one or more
  3. * contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. jclouds licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jclouds.logging.config;
  20. import static com.google.inject.matcher.Matchers.any;
  21. import static org.easymock.classextension.EasyMock.createMock;
  22. import static org.testng.Assert.assertEquals;
  23. import java.lang.reflect.Field;
  24. import java.util.Set;
  25. import javax.annotation.Resource;
  26. import javax.inject.Inject;
  27. import javax.inject.Named;
  28. import org.jclouds.logging.Logger;
  29. import org.jclouds.logging.config.BindLoggersAnnotatedWithResource.AssignLoggerToField;
  30. import org.jclouds.logging.config.BindLoggersAnnotatedWithResource.LoggerFieldsAnnotatedWithResource;
  31. import org.jclouds.logging.jdk.JDKLogger;
  32. import org.testng.annotations.BeforeMethod;
  33. import org.testng.annotations.Test;
  34. import com.google.inject.AbstractModule;
  35. import com.google.inject.Guice;
  36. import com.google.inject.Injector;
  37. @Test
  38. public class BindLoggersAnnotatedWithResourceTest {
  39. private BindLoggersAnnotatedWithResource blawr;
  40. public static class A {
  41. @Resource
  42. private Logger logger = Logger.NULL;
  43. }
  44. private static class B {
  45. @Resource
  46. private Logger logger = Logger.NULL;
  47. @Resource
  48. @Named("blogger")
  49. private Logger blogger = Logger.NULL;
  50. }
  51. @BeforeMethod
  52. void createBlawr() {
  53. blawr = new BindLoggersAnnotatedWithResource(new JDKLogger.JDKLoggerFactory());
  54. }
  55. @Test
  56. void testHear() {
  57. Injector i = Guice.createInjector(new AbstractModule() {
  58. @Override
  59. protected void configure() {
  60. bindListener(any(), blawr);
  61. }
  62. });
  63. assertEquals(i.getInstance(A.class).logger.getCategory(), getClass().getName() + "$A");
  64. assertEquals(i.getInstance(B.class).logger.getCategory(), getClass().getName() + "$B");
  65. assertEquals(i.getInstance(B.class).blogger.getCategory(), "blogger");
  66. }
  67. @Test
  68. public void testAssignLoggerToField() throws SecurityException, NoSuchFieldException,
  69. IllegalArgumentException, IllegalAccessException {
  70. Logger logger = createMock(Logger.class);
  71. A a = new A();
  72. Field field = A.class.getDeclaredField("logger");
  73. AssignLoggerToField<A> assigner = new AssignLoggerToField<A>(logger, field);
  74. assigner.afterInjection(a);
  75. assert field.get(a).equals(logger);
  76. }
  77. @Test
  78. public void testLoggerFieldsAnnotatedWithResource() throws SecurityException,
  79. NoSuchFieldException {
  80. LoggerFieldsAnnotatedWithResource predicate = new LoggerFieldsAnnotatedWithResource();
  81. assert predicate.apply(A.class.getDeclaredField("logger"));
  82. }
  83. public static class C {
  84. @SuppressWarnings("unused")
  85. @Inject
  86. private Logger logger = Logger.NULL;
  87. }
  88. @Test
  89. public void testLoggerFieldsAnnotatedWithInjectReturnsNull() throws SecurityException,
  90. NoSuchFieldException {
  91. LoggerFieldsAnnotatedWithResource predicate = new LoggerFieldsAnnotatedWithResource();
  92. assert !predicate.apply(C.class.getDeclaredField("logger"));
  93. }
  94. public static class D {
  95. @SuppressWarnings("unused")
  96. @Resource
  97. private Logger logger = Logger.NULL;
  98. @SuppressWarnings("unused")
  99. @Resource
  100. private Logger blogger;
  101. }
  102. @Test
  103. public void testGetLoggerFieldsAnnotatedWithResourceNoLogger() {
  104. Set<Field> fields = blawr.getLoggerFieldsAnnotatedWithResource(this.getClass());
  105. assertEquals(fields.size(), 0);
  106. }
  107. @Test
  108. public void testGetLoggerFieldsAnnotatedWithResourceOneLogger() {
  109. Set<Field> fields = blawr.getLoggerFieldsAnnotatedWithResource(A.class);
  110. assertEquals(fields.size(), 1);
  111. }
  112. @Test
  113. public void testGetLoggerFieldsAnnotatedWithResourceTwoLoggers() {
  114. Set<Field> fields = blawr.getLoggerFieldsAnnotatedWithResource(D.class);
  115. assertEquals(fields.size(), 2);
  116. }
  117. }