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.

RuleDefinitionsLoaderTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 org.junit.Test;
  22. import org.sonar.api.server.rule.RulesDefinition;
  23. import org.sonar.server.plugins.ServerPluginRepository;
  24. import static org.assertj.core.api.Assertions.assertThat;
  25. import static org.mockito.Mockito.mock;
  26. public class RuleDefinitionsLoaderTest {
  27. @Test
  28. public void no_definitions() {
  29. CommonRuleDefinitions commonRulesDefinitions = mock(CommonRuleDefinitions.class);
  30. RulesDefinition.Context context = new RuleDefinitionsLoader(commonRulesDefinitions, mock(ServerPluginRepository.class)).load();
  31. assertThat(context.repositories()).isEmpty();
  32. }
  33. @Test
  34. public void load_definitions() {
  35. CommonRuleDefinitions commonRulesDefinitions = mock(CommonRuleDefinitions.class);
  36. RulesDefinition.Context context = new RuleDefinitionsLoader(commonRulesDefinitions, mock(ServerPluginRepository.class),
  37. new RulesDefinition[] {
  38. new FindbugsDefinitions(), new SquidDefinitions()
  39. }).load();
  40. assertThat(context.repositories()).hasSize(2);
  41. assertThat(context.repository("findbugs")).isNotNull();
  42. assertThat(context.repository("squid")).isNotNull();
  43. }
  44. @Test
  45. public void define_common_rules() {
  46. CommonRuleDefinitions commonRulesDefinitions = new FakeCommonRuleDefinitions();
  47. RulesDefinition.Context context = new RuleDefinitionsLoader(commonRulesDefinitions, mock(ServerPluginRepository.class),
  48. new RulesDefinition[] {
  49. new SquidDefinitions()
  50. }).load();
  51. assertThat(context.repositories()).extracting("key").containsOnly("squid", "common-java");
  52. assertThat(context.repository("common-java").rules()).extracting("key").containsOnly("InsufficientBranchCoverage");
  53. }
  54. /**
  55. * "common-rules" are merged into core 5.2. Previously they were embedded by some plugins. Only the core definition
  56. * is taken into account. Others are ignored.
  57. */
  58. @Test
  59. public void plugin_common_rules_are_overridden() {
  60. CommonRuleDefinitions commonRulesDefinitions = new FakeCommonRuleDefinitions();
  61. RulesDefinition.Context context = new RuleDefinitionsLoader(commonRulesDefinitions, mock(ServerPluginRepository.class),
  62. new RulesDefinition[] {
  63. new PluginCommonRuleDefinitions()
  64. }).load();
  65. assertThat(context.repositories()).extracting("key").containsOnly("common-java");
  66. assertThat(context.repository("common-java").rules()).extracting("key").containsOnly("InsufficientBranchCoverage");
  67. assertThat(context.repository("common-java").rule("InsufficientBranchCoverage").name()).isEqualTo("The name as defined by core");
  68. }
  69. static class FindbugsDefinitions implements RulesDefinition {
  70. @Override
  71. public void define(Context context) {
  72. NewRepository repo = context.createRepository("findbugs", "java");
  73. repo.setName("Findbugs");
  74. repo.createRule("ABC")
  75. .setName("ABC")
  76. .setHtmlDescription("Description of ABC");
  77. repo.done();
  78. }
  79. }
  80. static class SquidDefinitions implements RulesDefinition {
  81. @Override
  82. public void define(Context context) {
  83. NewRepository repo = context.createRepository("squid", "java");
  84. repo.setName("Squid");
  85. repo.createRule("DEF")
  86. .setName("DEF")
  87. .setHtmlDescription("Description of DEF");
  88. repo.done();
  89. }
  90. }
  91. static class PluginCommonRuleDefinitions implements RulesDefinition {
  92. @Override
  93. public void define(RulesDefinition.Context context) {
  94. RulesDefinition.NewRepository repo = context.createRepository("common-java", "java");
  95. repo.createRule("InsufficientBranchCoverage")
  96. .setName("The name as defined by plugin")
  97. .setHtmlDescription("The description as defined by plugin");
  98. repo.done();
  99. }
  100. }
  101. static class FakeCommonRuleDefinitions implements CommonRuleDefinitions {
  102. @Override
  103. public void define(RulesDefinition.Context context) {
  104. RulesDefinition.NewRepository repo = context.createRepository("common-java", "java");
  105. repo.createRule("InsufficientBranchCoverage")
  106. .setName("The name as defined by core")
  107. .setHtmlDescription("The description as defined by core");
  108. repo.done();
  109. }
  110. }
  111. }