You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RuleI18nManagerTest.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.core.i18n;
  21. import java.util.Locale;
  22. import org.junit.Test;
  23. import static org.assertj.core.api.Assertions.assertThat;
  24. import static org.mockito.Mockito.mock;
  25. import static org.mockito.Mockito.verify;
  26. import static org.mockito.Mockito.verifyNoMoreInteractions;
  27. import static org.mockito.Mockito.when;
  28. public class RuleI18nManagerTest {
  29. @Test
  30. public void shouldGetName() {
  31. DefaultI18n i18n = mock(DefaultI18n.class);
  32. RuleI18nManager ruleI18n = new RuleI18nManager(i18n);
  33. ruleI18n.getName("checkstyle", "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck", Locale.ENGLISH);
  34. String propertyKey = "rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.name";
  35. verify(i18n).message(Locale.ENGLISH, propertyKey, null /* no default value */);
  36. verifyNoMoreInteractions(i18n);
  37. }
  38. @Test
  39. public void shouldGetParamDescription() {
  40. DefaultI18n i18n = mock(DefaultI18n.class);
  41. RuleI18nManager ruleI18n = new RuleI18nManager(i18n);
  42. ruleI18n.getParamDescription("checkstyle", "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck", "pattern", Locale.ENGLISH);
  43. String propertyKey = "rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.param.pattern";
  44. verify(i18n).message(Locale.ENGLISH, propertyKey, null /* no default value */);
  45. verifyNoMoreInteractions(i18n);
  46. }
  47. @Test
  48. public void shouldGetDescriptionFromFile() {
  49. String propertyKeyForName = "rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.name";
  50. DefaultI18n i18n = mock(DefaultI18n.class);
  51. when(i18n.messageFromFile(Locale.ENGLISH, "rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName)).thenReturn(
  52. "Description");
  53. RuleI18nManager ruleI18n = new RuleI18nManager(i18n);
  54. String description = ruleI18n.getDescription("checkstyle", "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck", Locale.ENGLISH);
  55. assertThat(description).isEqualTo("Description");
  56. verify(i18n).messageFromFile(Locale.ENGLISH, "rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName);
  57. verifyNoMoreInteractions(i18n);
  58. }
  59. // see http://jira.sonarsource.com/browse/SONAR-3319
  60. @Test
  61. public void shouldGetDescriptionFromFileWithBackwardCompatibility() {
  62. String propertyKeyForName = "rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.name";
  63. DefaultI18n i18n = mock(DefaultI18n.class);
  64. // this is the "old" way of storing HTML description files for rules (they are not in the "rules/<repo-key>" folder)
  65. when(i18n.messageFromFile(Locale.ENGLISH, "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName)).thenReturn("Description");
  66. RuleI18nManager ruleI18n = new RuleI18nManager(i18n);
  67. String description = ruleI18n.getDescription("checkstyle", "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck", Locale.ENGLISH);
  68. assertThat(description).isEqualTo("Description");
  69. verify(i18n).messageFromFile(Locale.ENGLISH, "rules/checkstyle/com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName);
  70. verify(i18n).messageFromFile(Locale.ENGLISH, "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.html", propertyKeyForName);
  71. verifyNoMoreInteractions(i18n);
  72. }
  73. @Test
  74. public void shoudlReturnNullIfMissingDescription() {
  75. DefaultI18n i18n = mock(DefaultI18n.class);
  76. RuleI18nManager ruleI18n = new RuleI18nManager(i18n);
  77. assertThat(ruleI18n.getDescription("checkstyle", "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck", Locale.ENGLISH)).isNull();
  78. }
  79. @Test
  80. public void shouldBeRuleKey() {
  81. assertThat(RuleI18nManager.isRuleProperty("rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.name")).isTrue();
  82. assertThat(RuleI18nManager.isRuleProperty("rule.pmd.Header.name")).isTrue();
  83. }
  84. @Test
  85. public void shouldNotBeRuleKey() {
  86. // this is the parameter "name"
  87. assertThat(RuleI18nManager.isRuleProperty("rule.checkstyle.com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.param.name")).isFalse();
  88. assertThat(RuleI18nManager.isRuleProperty("by")).isFalse();
  89. assertThat(RuleI18nManager.isRuleProperty("something.else")).isFalse();
  90. assertThat(RuleI18nManager.isRuleProperty("checkstyle.page.name")).isFalse();
  91. }
  92. }