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.

DeprecatedRulesDefinitionLoaderTest.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.server.rule;
  21. import java.io.Reader;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import org.junit.Test;
  25. import org.junit.runner.RunWith;
  26. import org.mockito.Mock;
  27. import org.mockito.runners.MockitoJUnitRunner;
  28. import org.sonar.api.rule.RuleKey;
  29. import org.sonar.api.rule.RuleStatus;
  30. import org.sonar.api.rule.Severity;
  31. import org.sonar.api.rules.Rule;
  32. import org.sonar.api.rules.RulePriority;
  33. import org.sonar.api.rules.RuleRepository;
  34. import org.sonar.api.server.debt.DebtRemediationFunction;
  35. import org.sonar.api.server.rule.RulesDefinition;
  36. import org.sonar.api.utils.ValidationMessages;
  37. import org.sonar.core.i18n.RuleI18nManager;
  38. import org.sonar.api.impl.server.RulesDefinitionContext;
  39. import org.sonar.server.debt.DebtModelPluginRepository;
  40. import org.sonar.server.debt.DebtModelXMLExporter;
  41. import org.sonar.server.debt.DebtRulesXMLImporter;
  42. import org.sonar.server.plugins.ServerPluginRepository;
  43. import static com.google.common.collect.Lists.newArrayList;
  44. import static org.assertj.core.api.Assertions.assertThat;
  45. import static org.junit.Assert.fail;
  46. import static org.mockito.ArgumentMatchers.any;
  47. import static org.mockito.ArgumentMatchers.eq;
  48. import static org.mockito.Mockito.mock;
  49. import static org.mockito.Mockito.when;
  50. @RunWith(MockitoJUnitRunner.class)
  51. public class DeprecatedRulesDefinitionLoaderTest {
  52. @Mock
  53. RuleI18nManager i18n;
  54. @Mock
  55. DebtModelPluginRepository debtModelRepository;
  56. @Mock
  57. DebtRulesXMLImporter importer;
  58. @Mock
  59. ServerPluginRepository pluginRepository;
  60. static class CheckstyleRules extends RuleRepository {
  61. public CheckstyleRules() {
  62. super("checkstyle", "java");
  63. setName("Checkstyle");
  64. }
  65. @Override
  66. public List<Rule> createRules() {
  67. Rule rule = Rule.create("checkstyle", "ConstantName", "Constant Name");
  68. rule.setDescription("Checks that constant names conform to the specified format");
  69. rule.setConfigKey("Checker/TreeWalker/ConstantName");
  70. rule.setSeverity(RulePriority.BLOCKER);
  71. rule.setStatus(Rule.STATUS_BETA);
  72. rule.setTags(new String[] {"style", "clumsy"});
  73. rule.createParameter("format").setDescription("Regular expression").setDefaultValue("A-Z").setType("REGULAR_EXPRESSION");
  74. return Arrays.asList(rule);
  75. }
  76. }
  77. static class UseBundles extends RuleRepository {
  78. public UseBundles() {
  79. super("checkstyle", "java");
  80. setName("Checkstyle");
  81. }
  82. @Override
  83. public List<Rule> createRules() {
  84. Rule rule = Rule.create("checkstyle", "ConstantName");
  85. rule.createParameter("format");
  86. return Arrays.asList(rule);
  87. }
  88. }
  89. @Test
  90. public void wrap_deprecated_rule_repositories() {
  91. RulesDefinition.Context context = new RulesDefinitionContext();
  92. CheckstyleRules checkstyleRules = new CheckstyleRules();
  93. when(pluginRepository.getPluginKey(checkstyleRules)).thenReturn("unittest");
  94. new DeprecatedRulesDefinitionLoader(i18n, debtModelRepository, importer, pluginRepository, new RuleRepository[] {checkstyleRules}).complete(context);
  95. assertThat(context.repositories()).hasSize(1);
  96. RulesDefinition.Repository checkstyle = context.repository("checkstyle");
  97. assertThat(checkstyle).isNotNull();
  98. assertThat(checkstyle.key()).isEqualTo("checkstyle");
  99. assertThat(checkstyle.name()).isEqualTo("Checkstyle");
  100. assertThat(checkstyle.language()).isEqualTo("java");
  101. assertThat(checkstyle.rules()).hasSize(1);
  102. RulesDefinition.Rule rule = checkstyle.rule("ConstantName");
  103. assertThat(rule).isNotNull();
  104. assertThat(rule.key()).isEqualTo("ConstantName");
  105. assertThat(rule.pluginKey()).isEqualTo("unittest");
  106. assertThat(rule.name()).isEqualTo("Constant Name");
  107. assertThat(rule.htmlDescription()).isEqualTo("Checks that constant names conform to the specified format");
  108. assertThat(rule.severity()).isEqualTo(Severity.BLOCKER);
  109. assertThat(rule.internalKey()).isEqualTo("Checker/TreeWalker/ConstantName");
  110. assertThat(rule.status()).isEqualTo(RuleStatus.BETA);
  111. assertThat(rule.tags()).containsOnly("style", "clumsy");
  112. assertThat(rule.params()).hasSize(1);
  113. RulesDefinition.Param param = rule.param("format");
  114. assertThat(param).isNotNull();
  115. assertThat(param.key()).isEqualTo("format");
  116. assertThat(param.name()).isEqualTo("format");
  117. assertThat(param.description()).isEqualTo("Regular expression");
  118. assertThat(param.defaultValue()).isEqualTo("A-Z");
  119. }
  120. @Test
  121. public void emulate_the_day_deprecated_api_can_be_dropped() {
  122. RulesDefinition.Context context = new RulesDefinitionContext();
  123. // no more RuleRepository !
  124. new DeprecatedRulesDefinitionLoader(i18n, debtModelRepository, importer, pluginRepository);
  125. assertThat(context.repositories()).isEmpty();
  126. }
  127. @Test
  128. public void use_l10n_bundles() {
  129. RulesDefinition.Context context = new RulesDefinitionContext();
  130. when(i18n.getName("checkstyle", "ConstantName")).thenReturn("Constant Name");
  131. when(i18n.getDescription("checkstyle", "ConstantName")).thenReturn("Checks that constant names conform to the specified format");
  132. when(i18n.getParamDescription("checkstyle", "ConstantName", "format")).thenReturn("Regular expression");
  133. new DeprecatedRulesDefinitionLoader(i18n, debtModelRepository, importer, pluginRepository, new RuleRepository[] {new UseBundles()}).complete(context);
  134. RulesDefinition.Repository checkstyle = context.repository("checkstyle");
  135. RulesDefinition.Rule rule = checkstyle.rule("ConstantName");
  136. assertThat(rule.key()).isEqualTo("ConstantName");
  137. assertThat(rule.name()).isEqualTo("Constant Name");
  138. assertThat(rule.htmlDescription()).isEqualTo("Checks that constant names conform to the specified format");
  139. RulesDefinition.Param param = rule.param("format");
  140. assertThat(param.key()).isEqualTo("format");
  141. assertThat(param.name()).isEqualTo("format");
  142. assertThat(param.description()).isEqualTo("Regular expression");
  143. }
  144. @Test
  145. public void define_rule_debt() {
  146. RulesDefinition.Context context = new RulesDefinitionContext();
  147. List<DebtModelXMLExporter.RuleDebt> ruleDebts = newArrayList(
  148. new DebtModelXMLExporter.RuleDebt()
  149. .setRuleKey(RuleKey.of("checkstyle", "ConstantName"))
  150. .setFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
  151. .setCoefficient("1d")
  152. .setOffset("10min"));
  153. Reader javaModelReader = mock(Reader.class);
  154. when(debtModelRepository.createReaderForXMLFile("java")).thenReturn(javaModelReader);
  155. when(debtModelRepository.getContributingPluginList()).thenReturn(newArrayList("java"));
  156. when(importer.importXML(eq(javaModelReader), any(ValidationMessages.class))).thenReturn(ruleDebts);
  157. new DeprecatedRulesDefinitionLoader(i18n, debtModelRepository, importer, pluginRepository, new RuleRepository[] {new CheckstyleRules()}).complete(context);
  158. assertThat(context.repositories()).hasSize(1);
  159. RulesDefinition.Repository checkstyle = context.repository("checkstyle");
  160. assertThat(checkstyle.rules()).hasSize(1);
  161. RulesDefinition.Rule rule = checkstyle.rule("ConstantName");
  162. assertThat(rule).isNotNull();
  163. assertThat(rule.key()).isEqualTo("ConstantName");
  164. assertThat(rule.debtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
  165. assertThat(rule.debtRemediationFunction().gapMultiplier()).isEqualTo("1d");
  166. assertThat(rule.debtRemediationFunction().baseEffort()).isEqualTo("10min");
  167. }
  168. @Test
  169. public void fail_on_invalid_rule_debt() {
  170. RulesDefinition.Context context = new RulesDefinitionContext();
  171. List<DebtModelXMLExporter.RuleDebt> ruleDebts = newArrayList(
  172. new DebtModelXMLExporter.RuleDebt()
  173. .setRuleKey(RuleKey.of("checkstyle", "ConstantName"))
  174. .setFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
  175. .setCoefficient("1d"));
  176. Reader javaModelReader = mock(Reader.class);
  177. when(debtModelRepository.createReaderForXMLFile("java")).thenReturn(javaModelReader);
  178. when(debtModelRepository.getContributingPluginList()).thenReturn(newArrayList("java"));
  179. when(importer.importXML(eq(javaModelReader), any(ValidationMessages.class))).thenReturn(ruleDebts);
  180. try {
  181. new DeprecatedRulesDefinitionLoader(i18n, debtModelRepository, importer, pluginRepository, new RuleRepository[] {new CheckstyleRules()}).complete(context);
  182. fail();
  183. } catch (Exception e) {
  184. assertThat(e).isInstanceOf(IllegalArgumentException.class);
  185. }
  186. assertThat(context.repositories()).isEmpty();
  187. }
  188. }