aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core/src
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-08-12 10:39:32 +0200
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-08-12 10:39:37 +0200
commit105831419ee487a9d1ab1f8a9338d275e5be82fd (patch)
treedbcc07f6bb5c6d6bead39582b1a72cea917b523b /sonar-core/src
parentafdf3ba619eb80c7475afb0b69c1cabfcdd08833 (diff)
downloadsonarqube-105831419ee487a9d1ab1f8a9338d275e5be82fd.tar.gz
sonarqube-105831419ee487a9d1ab1f8a9338d275e5be82fd.zip
SONAR-7930 WS api/components/bulk_update_key dry run fails when invalid key
Diffstat (limited to 'sonar-core/src')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java11
-rw-r--r--sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java40
2 files changed, 50 insertions, 1 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java b/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java
index defab7e80a0..c018516ffe3 100644
--- a/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java
+++ b/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java
@@ -26,6 +26,8 @@ import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.resources.Scopes;
+import static com.google.common.base.Preconditions.checkArgument;
+
public final class ComponentKeys {
public static final int MAX_COMPONENT_KEY_LENGTH = 400;
@@ -97,6 +99,15 @@ public final class ComponentKeys {
}
/**
+ * Checks if given parameter is valid for a project/module following {@link #isValidModuleKey(String)} contract.
+ *
+ * @throws IllegalArgumentException if the format is incorrect
+ */
+ public static void checkModuleKey(String keyCandidate) {
+ checkArgument(isValidModuleKey(keyCandidate), "Malformed key for '%s'. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.", keyCandidate);
+ }
+
+ /**
* Same as {@link #isValidModuleKey(String)}, but allows additionally '/'.
*/
public static boolean isValidModuleKeyIssuesMode(String keyCandidate) {
diff --git a/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java b/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java
index 56048b1c97e..e9818a55d37 100644
--- a/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java
@@ -19,7 +19,9 @@
*/
package org.sonar.core.component;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.resources.Directory;
@@ -28,6 +30,8 @@ import org.sonar.api.resources.Project;
import static org.assertj.core.api.Assertions.assertThat;
public class ComponentKeysTest {
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
@Test
public void create_effective_key() {
@@ -57,7 +61,7 @@ public class ComponentKeysTest {
assertThat(ComponentKeys.isValidModuleKey("ab_12")).isTrue();
assertThat(ComponentKeys.isValidModuleKey("ab/12")).isFalse();
}
-
+
@Test
public void isValidModuleKeyIssuesMode() {
assertThat(ComponentKeys.isValidModuleKeyIssuesMode("")).isFalse();
@@ -80,4 +84,38 @@ public class ComponentKeysTest {
assertThat(ComponentKeys.isValidBranch("ab\n")).isFalse();
}
+ @Test
+ public void checkModuleKey_with_correct_keys() {
+ ComponentKeys.checkModuleKey("abc");
+ ComponentKeys.checkModuleKey("a-b_1.:2");
+ }
+
+ @Test
+ public void checkModuleKey_fail_if_only_digit() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Malformed key for '0123'. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.");
+
+ ComponentKeys.checkModuleKey("0123");
+ }
+
+ @Test
+ public void checkModuleKey_fail_if_key_is_empty() {
+ expectedException.expect(IllegalArgumentException.class);
+
+ ComponentKeys.checkModuleKey("");
+ }
+
+ @Test
+ public void checkModuleKey_fail_if_space() {
+ expectedException.expect(IllegalArgumentException.class);
+
+ ComponentKeys.checkModuleKey("ab 12");
+ }
+
+ @Test
+ public void checkModuleKey_fail_if_special_characters_not_allowed() {
+ expectedException.expect(IllegalArgumentException.class);
+
+ ComponentKeys.checkModuleKey("ab/12");
+ }
}