/eng/code-quality-reports/src/test/java/com/azure/tools/checkstyle/checks/GoodLoggerCheckTests.java

http://github.com/WindowsAzure/azure-sdk-for-java · Java · 79 lines · 62 code · 12 blank · 5 comment · 0 complexity · 40e6fe84c42b760c4a374269cb01ddda MD5 · raw file

  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License.
  3. package com.azure.tools.checkstyle.checks;
  4. import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
  5. import com.puppycrawl.tools.checkstyle.Checker;
  6. import org.junit.After;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. /**
  10. * Tests for good logging practices, GoodLoggerCheck.
  11. */
  12. public class GoodLoggerCheckTests extends AbstractModuleTestSupport {
  13. private static final String INCONSISTENCY_NAMING_MESSAGE = "ClientLogger instance naming: use 'logger'"
  14. + " instead of 'wrongLoggerName' for consistency.";
  15. private static final String EXTERNAL_LOGGER_USED_MESSAGE = "Do not use external logger class. "
  16. + "Use 'com.azure.core.util.logging.ClientLogger' as a logging mechanism instead of '%s'.";
  17. private static final String NOT_NEWING_MATCH_CLASS_NAME = "Not newing a ClientLogger with matching class name. "
  18. + "Use 'WrongClassInLoggerConstructorTestData.class' instead of 'XXXXXX.class'.";
  19. private static final String STATIC_LOGGER_MESSAGE = "ClientLogger should not be static. Remove static modifier.";
  20. private Checker checker;
  21. @Before
  22. public void prepare() throws Exception {
  23. checker = createChecker(createModuleConfig(GoodLoggingCheck.class));
  24. }
  25. @After
  26. public void cleanup() {
  27. checker.destroy();
  28. }
  29. @Override
  30. protected String getPackageLocation() {
  31. return "com/azure/tools/checkstyle/checks/GoodLoggerCheck";
  32. }
  33. @Test
  34. public void externalLoggerLibraryTestData() throws Exception {
  35. String[] expected = {
  36. expectedErrorMessage(3,1, String.format(EXTERNAL_LOGGER_USED_MESSAGE,
  37. "org.apache.logging.log4j")),
  38. expectedErrorMessage(4,1, String.format(EXTERNAL_LOGGER_USED_MESSAGE, "org.slf4j")),
  39. expectedErrorMessage(5,1, String.format(EXTERNAL_LOGGER_USED_MESSAGE, "java.util.logging"))
  40. };
  41. verify(checker, getPath("ExternalLoggerLibraryTestData.java"), expected);
  42. }
  43. @Test
  44. public void invalidLoggerNameTestData() throws Exception {
  45. String[] expected = {
  46. expectedErrorMessage(5,5, INCONSISTENCY_NAMING_MESSAGE),
  47. };
  48. verify(checker, getPath("InvalidLoggerNameTestData.java"), expected);
  49. }
  50. @Test
  51. public void nonStaticLoggerTestData() throws Exception {
  52. String[] expected = {
  53. expectedErrorMessage(5,5, STATIC_LOGGER_MESSAGE)
  54. };
  55. verify(checker, getPath("NonStaticLoggerTestData.java"), expected);
  56. }
  57. @Test
  58. public void wrongClassInLoggerConstructorTestData() throws Exception {
  59. String[] expected = {
  60. expectedErrorMessage(5,64, NOT_NEWING_MATCH_CLASS_NAME)
  61. };
  62. verify(checker, getPath("WrongClassInLoggerConstructorTestData.java"), expected);
  63. }
  64. private String expectedErrorMessage(int line, int column, String errorMessage) {
  65. return String.format("%d:%d: %s", line, column, errorMessage);
  66. }
  67. }