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.

ComponentKeysTest.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.core.component;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.junit.rules.ExpectedException;
  24. import static org.assertj.core.api.Assertions.assertThat;
  25. public class ComponentKeysTest {
  26. @Rule
  27. public ExpectedException expectedException = ExpectedException.none();
  28. @Test
  29. public void create_key_from_module_key_path_and_branch() {
  30. assertThat(ComponentKeys.createKey("module_key", "file", "origin/master")).isEqualTo("module_key:origin/master:file");
  31. assertThat(ComponentKeys.createKey("module_key", "file", null)).isEqualTo("module_key:file");
  32. assertThat(ComponentKeys.createKey("module_key", null, null)).isEqualTo("module_key");
  33. }
  34. @Test
  35. public void isValidProjectKey() {
  36. assertThat(ComponentKeys.isValidProjectKey("abc")).isTrue();
  37. assertThat(ComponentKeys.isValidProjectKey("0123")).isTrue();
  38. assertThat(ComponentKeys.isValidProjectKey("ab_12")).isTrue();
  39. assertThat(ComponentKeys.isValidProjectKey("ab/12")).isTrue();
  40. assertThat(ComponentKeys.isValidProjectKey("코드품질")).isTrue();
  41. assertThat(ComponentKeys.isValidProjectKey("")).isFalse();
  42. assertThat(ComponentKeys.isValidProjectKey(" ")).isFalse();
  43. assertThat(ComponentKeys.isValidProjectKey("ab 12")).isFalse();
  44. assertThat(ComponentKeys.isValidProjectKey(" ab")).isFalse();
  45. assertThat(ComponentKeys.isValidProjectKey("ab ")).isFalse();
  46. }
  47. @Test
  48. public void checkProjectKey_with_correct_keys() {
  49. ComponentKeys.checkProjectKey("abc");
  50. ComponentKeys.checkProjectKey("a-b_1.:2");
  51. }
  52. @Test
  53. public void checkProjectKey_fail_if_key_is_empty() {
  54. expectedException.expect(IllegalArgumentException.class);
  55. ComponentKeys.checkProjectKey("");
  56. }
  57. @Test
  58. public void checkProjectKey_fail_if_space() {
  59. expectedException.expect(IllegalArgumentException.class);
  60. ComponentKeys.checkProjectKey("ab 12");
  61. }
  62. }