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.

IssuePatternTest.java 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.filter;
  21. import org.junit.Test;
  22. import org.sonar.api.rule.RuleKey;
  23. import org.sonar.api.rules.Rule;
  24. import static org.assertj.core.api.Assertions.assertThat;
  25. public class IssuePatternTest {
  26. @Test
  27. public void match_file() {
  28. String javaFile = "org/foo/Bar.xoo";
  29. assertThat(new IssuePattern("org/foo/Bar*", "*").matchComponent(javaFile)).isTrue();
  30. assertThat(new IssuePattern("org/foo/*", "*").matchComponent(javaFile)).isTrue();
  31. assertThat(new IssuePattern("**/*ar*", "*").matchComponent(javaFile)).isTrue();
  32. assertThat(new IssuePattern("org/**/?ar.xoo", "*").matchComponent(javaFile)).isTrue();
  33. assertThat(new IssuePattern("**", "*").matchComponent(javaFile)).isTrue();
  34. assertThat(new IssuePattern("org/other/Hello", "*").matchComponent(javaFile)).isFalse();
  35. assertThat(new IssuePattern("org/foo/Hello", "*").matchComponent(javaFile)).isFalse();
  36. assertThat(new IssuePattern("org/**/??ar.xoo", "*").matchComponent(javaFile)).isFalse();
  37. assertThat(new IssuePattern("org/**/??ar.xoo", "*").matchComponent(null)).isFalse();
  38. assertThat(new IssuePattern("org/**/??ar.xoo", "*").matchComponent("plop")).isFalse();
  39. }
  40. @Test
  41. public void match_rule() {
  42. RuleKey rule = Rule.create("checkstyle", "IllegalRegexp", "").ruleKey();
  43. assertThat(new IssuePattern("*", "*").matchRule(rule)).isTrue();
  44. assertThat(new IssuePattern("*", "checkstyle:*").matchRule(rule)).isTrue();
  45. assertThat(new IssuePattern("*", "checkstyle:IllegalRegexp").matchRule(rule)).isTrue();
  46. assertThat(new IssuePattern("*", "checkstyle:Illegal*").matchRule(rule)).isTrue();
  47. assertThat(new IssuePattern("*", "*:*Illegal*").matchRule(rule)).isTrue();
  48. assertThat(new IssuePattern("*", "pmd:IllegalRegexp").matchRule(rule)).isFalse();
  49. assertThat(new IssuePattern("*", "pmd:*").matchRule(rule)).isFalse();
  50. assertThat(new IssuePattern("*", "*:Foo*IllegalRegexp").matchRule(rule)).isFalse();
  51. }
  52. @Test
  53. public void test_to_string() {
  54. IssuePattern pattern = new IssuePattern("*", "checkstyle:*");
  55. assertThat(pattern).hasToString(
  56. "IssuePattern{componentPattern=*, rulePattern=checkstyle:*}");
  57. }
  58. }