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.

BuiltInQProfileRepositoryRule.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.qualityprofile;
  21. import com.google.common.base.Preconditions;
  22. import com.google.common.collect.ImmutableList;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.List;
  26. import java.util.Map;
  27. import org.junit.rules.ExternalResource;
  28. import org.sonar.api.resources.Language;
  29. import org.sonar.api.rule.RuleKey;
  30. import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
  31. import org.sonar.core.util.stream.MoreCollectors;
  32. import org.sonar.db.rule.RuleDefinitionDto;
  33. import static com.google.common.base.Preconditions.checkState;
  34. public class BuiltInQProfileRepositoryRule extends ExternalResource implements BuiltInQProfileRepository {
  35. private boolean initializeCalled = false;
  36. private List<BuiltInQProfile> profiles = new ArrayList<>();
  37. @Override
  38. protected void before() {
  39. this.initializeCalled = false;
  40. this.profiles.clear();
  41. }
  42. @Override
  43. public void initialize() {
  44. checkState(!initializeCalled, "initialize must be called only once");
  45. this.initializeCalled = true;
  46. }
  47. @Override
  48. public List<BuiltInQProfile> get() {
  49. checkState(initializeCalled, "initialize must be called first");
  50. return ImmutableList.copyOf(profiles);
  51. }
  52. public boolean isInitialized() {
  53. return initializeCalled;
  54. }
  55. public BuiltInQProfile add(Language language, String profileName) {
  56. return add(language, profileName, false);
  57. }
  58. public BuiltInQProfile add(Language language, String profileName, boolean isDefault) {
  59. return add(language, profileName, isDefault, new BuiltInQProfile.ActiveRule[0]);
  60. }
  61. public BuiltInQProfile add(Language language, String profileName, boolean isDefault, BuiltInQProfile.ActiveRule... rules) {
  62. BuiltInQProfile builtIn = create(language, profileName, isDefault, rules);
  63. profiles.add(builtIn);
  64. return builtIn;
  65. }
  66. public BuiltInQProfile create(Language language, String profileName, boolean isDefault, BuiltInQProfile.ActiveRule... rules) {
  67. BuiltInQProfile.Builder builder = new BuiltInQProfile.Builder()
  68. .setLanguage(language.getKey())
  69. .setName(profileName)
  70. .setDeclaredDefault(isDefault);
  71. Arrays.stream(rules).forEach(builder::addRule);
  72. return builder.build();
  73. }
  74. public BuiltInQProfile create(BuiltInQualityProfilesDefinition.BuiltInQualityProfile api, RuleDefinitionDto... rules) {
  75. BuiltInQProfile.Builder builder = new BuiltInQProfile.Builder()
  76. .setLanguage(api.language())
  77. .setName(api.name())
  78. .setDeclaredDefault(api.isDefault());
  79. Map<RuleKey, RuleDefinitionDto> rulesByRuleKey = Arrays.stream(rules)
  80. .collect(MoreCollectors.uniqueIndex(RuleDefinitionDto::getKey));
  81. api.rules().forEach(rule -> {
  82. RuleKey ruleKey = RuleKey.of(rule.repoKey(), rule.ruleKey());
  83. RuleDefinitionDto ruleDefinition = rulesByRuleKey.get(ruleKey);
  84. Preconditions.checkState(ruleDefinition != null, "Rule '%s' not found", ruleKey);
  85. builder.addRule(rule, ruleDefinition.getId());
  86. });
  87. return builder
  88. .build();
  89. }
  90. }