Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

RuleDescriptionSectionsGeneratorResolverTest.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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.util.Set;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import org.junit.runner.RunWith;
  25. import org.mockito.Mock;
  26. import org.mockito.junit.MockitoJUnitRunner;
  27. import org.sonar.api.server.rule.RulesDefinition;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
  30. import static org.mockito.Mockito.when;
  31. @RunWith(MockitoJUnitRunner.class)
  32. public class RuleDescriptionSectionsGeneratorResolverTest {
  33. private static final String RULE_KEY = "RULE_KEY";
  34. @Mock
  35. private RuleDescriptionSectionsGenerator generator1;
  36. @Mock
  37. private RuleDescriptionSectionsGenerator generator2;
  38. @Mock
  39. private RulesDefinition.Rule rule;
  40. private RuleDescriptionSectionsGeneratorResolver resolver;
  41. @Before
  42. public void setUp() {
  43. resolver = new RuleDescriptionSectionsGeneratorResolver(Set.of(generator1, generator2));
  44. when(rule.key()).thenReturn(RULE_KEY);
  45. }
  46. @Test
  47. public void getRuleDescriptionSectionsGenerator_returnsTheCorrectGenerator() {
  48. when(generator2.isGeneratorForRule(rule)).thenReturn(true);
  49. assertThat(resolver.getRuleDescriptionSectionsGenerator(rule)).isEqualTo(generator2);
  50. }
  51. @Test
  52. public void getRuleDescriptionSectionsGenerator_whenNoGeneratorFound_throwsWithCorrectMessage() {
  53. assertThatIllegalStateException()
  54. .isThrownBy(() -> resolver.getRuleDescriptionSectionsGenerator(rule))
  55. .withMessage("No rule description section generator found for rule with key RULE_KEY");
  56. }
  57. @Test
  58. public void getRuleDescriptionSectionsGenerator_whenMoreThanOneGeneratorFound_throwsWithCorrectMessage() {
  59. when(generator1.isGeneratorForRule(rule)).thenReturn(true);
  60. when(generator2.isGeneratorForRule(rule)).thenReturn(true);
  61. assertThatIllegalStateException()
  62. .isThrownBy(() -> resolver.getRuleDescriptionSectionsGenerator(rule))
  63. .withMessage("More than one rule description section generator found for rule with key RULE_KEY");
  64. }
  65. }