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.

OrganizationValidationImplTest.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.organization;
  21. import com.google.common.base.Strings;
  22. import java.util.Random;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.ExpectedException;
  26. import static org.assertj.core.api.Assertions.assertThat;
  27. import static org.junit.Assert.fail;
  28. public class OrganizationValidationImplTest {
  29. private static final String STRING_32_CHARS = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  30. private static final String STRING_64_CHARS = STRING_32_CHARS + STRING_32_CHARS;
  31. private static final String STRING_256_CHARS = STRING_64_CHARS + STRING_64_CHARS + STRING_64_CHARS + STRING_64_CHARS;
  32. private static final String STRING_255_CHARS = Strings.repeat("a", 255);
  33. @Rule
  34. public ExpectedException expectedException = ExpectedException.none();
  35. private OrganizationValidationImpl underTest = new OrganizationValidationImpl();
  36. @Test
  37. public void checkValidKey_throws_NPE_if_arg_is_null() {
  38. expectedException.expect(NullPointerException.class);
  39. expectedException.expectMessage("key can't be null");
  40. underTest.checkKey(null);
  41. }
  42. @Test
  43. public void checkValidKey_throws_IAE_if_arg_is_empty() {
  44. expectedException.expect(IllegalArgumentException.class);
  45. expectedException.expectMessage("Key must not be empty");
  46. underTest.checkKey("");
  47. }
  48. @Test
  49. public void checkValidKey_throws_IAE_if_key_is_empty() {
  50. expectedException.expect(IllegalArgumentException.class);
  51. expectedException.expectMessage("Key must not be empty");
  52. underTest.checkKey("");
  53. }
  54. @Test
  55. public void checkValidKey_does_not_fail_if_arg_is_1_to_255_chars_long() {
  56. String str = "a";
  57. for (int i = 0; i < 254; i++) {
  58. underTest.checkKey(str);
  59. str += "a";
  60. }
  61. }
  62. @Test
  63. public void checkValidKey_throws_IAE_when_more_than_300_characters() {
  64. String key = STRING_255_CHARS + "b";
  65. expectedException.expect(IllegalArgumentException.class);
  66. expectedException.expectMessage("Key '" + key + "' must be at most 255 chars long");
  67. underTest.checkKey(key);
  68. }
  69. @Test
  70. public void checkValidKey_throws_IAE_if_arg_contains_invalid_chars() {
  71. char[] invalidChars = {'é', '<', '@'};
  72. for (char invalidChar : invalidChars) {
  73. String str = "aa" + invalidChar;
  74. try {
  75. underTest.checkKey(str);
  76. fail("A IllegalArgumentException should have been thrown");
  77. } catch (IllegalArgumentException e) {
  78. assertThat(e).hasMessage("Key '" + str + "' contains at least one invalid char");
  79. }
  80. }
  81. }
  82. @Test
  83. public void checkValidName_throws_NPE_if_arg_is_null() {
  84. expectedException.expect(NullPointerException.class);
  85. expectedException.expectMessage("name can't be null");
  86. underTest.checkName(null);
  87. }
  88. @Test
  89. public void checkValidName_throws_IAE_if_empty() {
  90. expectedException.expect(IllegalArgumentException.class);
  91. expectedException.expectMessage("Name must not be empty");
  92. underTest.checkName("");
  93. }
  94. @Test
  95. public void checkValidName_does_not_fail_if_arg_is_1_to_255_chars_long() {
  96. String str = "a";
  97. for (int i = 0; i < 254; i++) {
  98. underTest.checkName(str);
  99. str += "a";
  100. }
  101. }
  102. @Test
  103. public void checkValidName_throws_IAE_when_more_than_255_characters() {
  104. String str = STRING_255_CHARS + "b";
  105. expectedException.expect(IllegalArgumentException.class);
  106. expectedException.expectMessage("Name '" + str + "' must be at most 255 chars long");
  107. underTest.checkName(str);
  108. }
  109. @Test
  110. public void checkValidDescription_does_not_fail_if_arg_is_null() {
  111. underTest.checkDescription(null);
  112. }
  113. @Test
  114. public void checkValidDescription_does_not_fail_if_arg_is_empty() {
  115. underTest.checkDescription("");
  116. }
  117. @Test
  118. public void checkValidDescription_does_not_fail_if_arg_is_1_to_256_chars_long() {
  119. String str = "1";
  120. for (int i = 0; i < 256; i++) {
  121. underTest.checkDescription(str);
  122. str += "a";
  123. }
  124. }
  125. @Test
  126. public void checkValidDescription_throws_IAE_if_arg_is_more_than_256_chars_long() {
  127. String str = STRING_256_CHARS;
  128. underTest.checkDescription(str);
  129. for (int i = 0; i < 5 + Math.abs(new Random().nextInt(10)); i++) {
  130. str += "c";
  131. try {
  132. underTest.checkDescription(str);
  133. fail("A IllegalArgumentException should have been thrown");
  134. } catch (IllegalArgumentException e) {
  135. assertThat(e).hasMessage("Description '" + str + "' must be at most 256 chars long");
  136. }
  137. }
  138. }
  139. @Test
  140. public void checkValidUrl_does_not_fail_if_arg_is_null() {
  141. underTest.checkUrl(null);
  142. }
  143. @Test
  144. public void checkValidUrl_does_not_fail_if_arg_is_1_to_256_chars_long() {
  145. String str = "1";
  146. for (int i = 0; i < 256; i++) {
  147. underTest.checkUrl(str);
  148. str += "a";
  149. }
  150. }
  151. @Test
  152. public void checkValidUrl_throws_IAE_if_arg_is_more_than_256_chars_long() {
  153. String str = STRING_256_CHARS;
  154. underTest.checkUrl(str);
  155. for (int i = 0; i < 5 + Math.abs(new Random().nextInt(10)); i++) {
  156. str += "c";
  157. try {
  158. underTest.checkUrl(str);
  159. fail("A IllegalArgumentException should have been thrown");
  160. } catch (IllegalArgumentException e) {
  161. assertThat(e).hasMessage("Url '" + str + "' must be at most 256 chars long");
  162. }
  163. }
  164. }
  165. @Test
  166. public void checkValidAvatar_does_not_fail_if_arg_is_null() {
  167. underTest.checkAvatar(null);
  168. }
  169. @Test
  170. public void checkValidAvatar_does_not_fail_if_arg_is_1_to_256_chars_long() {
  171. String str = "1";
  172. for (int i = 0; i < 256; i++) {
  173. underTest.checkAvatar(str);
  174. str += "a";
  175. }
  176. }
  177. @Test
  178. public void checkValidAvatar_throws_IAE_if_arg_is_more_than_256_chars_long() {
  179. String str = STRING_256_CHARS;
  180. underTest.checkAvatar(str);
  181. for (int i = 0; i < 5 + Math.abs(new Random().nextInt(10)); i++) {
  182. str += "c";
  183. try {
  184. underTest.checkAvatar(str);
  185. fail("A IllegalArgumentException should have been thrown");
  186. } catch (IllegalArgumentException e) {
  187. assertThat(e).hasMessage("Avatar '" + str + "' must be at most 256 chars long");
  188. }
  189. }
  190. }
  191. @Test
  192. public void generateKeyFrom_returns_slug_of_arg() {
  193. assertThat(underTest.generateKeyFrom("foo")).isEqualTo("foo");
  194. assertThat(underTest.generateKeyFrom(" FOO ")).isEqualTo("foo");
  195. assertThat(underTest.generateKeyFrom("he's here")).isEqualTo("he-s-here");
  196. assertThat(underTest.generateKeyFrom("foo-bar")).isEqualTo("foo-bar");
  197. assertThat(underTest.generateKeyFrom("foo_bar")).isEqualTo("foo_bar");
  198. assertThat(underTest.generateKeyFrom("accents éà")).isEqualTo("accents-ea");
  199. assertThat(underTest.generateKeyFrom("<foo>")).isEqualTo("foo");
  200. assertThat(underTest.generateKeyFrom("<\"foo:\">")).isEqualTo("foo");
  201. }
  202. }