Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

RuleRepositoryRule.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.ce.task.projectanalysis.issue;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.Optional;
  24. import java.util.function.Supplier;
  25. import org.junit.jupiter.api.extension.AfterEachCallback;
  26. import org.junit.jupiter.api.extension.ExtensionContext;
  27. import org.junit.rules.ExternalResource;
  28. import org.sonar.api.rule.RuleKey;
  29. import org.sonar.db.DbSession;
  30. import static com.google.common.base.Preconditions.checkArgument;
  31. import static java.util.Objects.requireNonNull;
  32. public class RuleRepositoryRule extends ExternalResource implements RuleRepository, AfterEachCallback {
  33. private final Map<RuleKey, Rule> rulesByKey = new HashMap<>();
  34. private final Map<String, Rule> rulesByUuid = new HashMap<>();
  35. private final Map<RuleKey, NewAdHocRule> newExternalRulesById = new HashMap<>();
  36. @Override
  37. protected void after() {
  38. rulesByKey.clear();
  39. rulesByUuid.clear();
  40. }
  41. @Override
  42. public Rule getByKey(RuleKey key) {
  43. Rule rule = rulesByKey.get(requireNonNull(key));
  44. checkArgument(rule != null);
  45. return rule;
  46. }
  47. @Override
  48. public Rule getByUuid(String uuid) {
  49. Rule rule = rulesByUuid.get(uuid);
  50. checkArgument(rule != null);
  51. return rule;
  52. }
  53. @Override
  54. public Optional<Rule> findByKey(RuleKey key) {
  55. return Optional.ofNullable(rulesByKey.get(requireNonNull(key)));
  56. }
  57. @Override
  58. public Optional<Rule> findByUuid(String uuid) {
  59. return Optional.ofNullable(rulesByUuid.get(uuid));
  60. }
  61. @Override
  62. public void saveOrUpdateAddHocRules(DbSession dbSession) {
  63. throw new UnsupportedOperationException();
  64. }
  65. public DumbRule add(RuleKey key) {
  66. DumbRule rule = new DumbRule(key);
  67. rule.setUuid(key.rule());
  68. rulesByKey.put(key, rule);
  69. rulesByUuid.put(rule.getUuid(), rule);
  70. return rule;
  71. }
  72. public RuleRepositoryRule add(DumbRule rule) {
  73. rulesByKey.put(requireNonNull(rule.getKey()), rule);
  74. rulesByUuid.put(rule.getUuid(), rule);
  75. return this;
  76. }
  77. @Override
  78. public void addOrUpdateAddHocRuleIfNeeded(RuleKey ruleKey, Supplier<NewAdHocRule> ruleSupplier) {
  79. newExternalRulesById.computeIfAbsent(ruleKey, k -> ruleSupplier.get());
  80. }
  81. @Override
  82. public void afterEach(ExtensionContext context) {
  83. after();
  84. }
  85. }