]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7299 Fail when project name is empty
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 30 Jan 2017 13:48:10 +0000 (14:48 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 31 Jan 2017 07:31:36 +0000 (08:31 +0100)
server/sonar-server/src/main/java/org/sonar/server/component/NewComponent.java
server/sonar-server/src/main/java/org/sonar/server/project/ws/CreateAction.java
server/sonar-server/src/test/java/org/sonar/server/component/NewComponentTest.java
sonar-db/src/main/java/org/sonar/db/component/ComponentValidator.java

index 29da48b50c2d9820d611fd369975edb68dde37be..89c384850332f228486e1c8f9cb9004ac95ee1ee 100644 (file)
@@ -22,9 +22,9 @@ package org.sonar.server.component;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import javax.annotation.concurrent.Immutable;
-import org.sonar.api.resources.Qualifiers;
 
 import static java.util.Objects.requireNonNull;
+import static org.sonar.api.resources.Qualifiers.PROJECT;
 import static org.sonar.db.component.ComponentValidator.checkComponentKey;
 import static org.sonar.db.component.ComponentValidator.checkComponentName;
 import static org.sonar.db.component.ComponentValidator.checkComponentQualifier;
@@ -41,7 +41,7 @@ public class NewComponent {
     this.organizationUuid = builder.organizationUuid;
     this.key = builder.key;
     this.branch = builder.branch;
-    this.qualifier = builder.qualifier == null ? Qualifiers.PROJECT : checkComponentQualifier(builder.qualifier);
+    this.qualifier = builder.qualifier;
     this.name = builder.name;
   }
 
@@ -74,7 +74,7 @@ public class NewComponent {
     private String organizationUuid;
     private String key;
     private String branch;
-    private String qualifier;
+    private String qualifier = PROJECT;
     private String name;
 
     private Builder() {
@@ -108,8 +108,9 @@ public class NewComponent {
 
     public NewComponent build() {
       requireNonNull(organizationUuid, "organization uuid can't be null");
-      checkComponentKey(requireNonNull(key, "key can't be null"));
-      checkComponentName(requireNonNull(name, "name can't be null"));
+      checkComponentKey(key);
+      checkComponentName(name);
+      checkComponentQualifier(qualifier);
       return new NewComponent(this);
     }
   }
index b3eaa57b7f7c04ac72fbb07942ad07a602abdc6e..4267b349a9a692e66d35bc8e5ef7890293649013 100644 (file)
@@ -62,7 +62,8 @@ public class CreateAction implements ProjectsWsAction {
     WebService.NewAction action = context.createAction(ACTION_CREATE)
       .setDescription("Create a project.<br/>" +
         "Requires 'Create Projects' permission<br/>" +
-        "Since 6.3, the response has been updated and does not contain the database ID anymore")
+        "Since 6.3, the response format has been updated and does not contain the database ID anymore<br/>" +
+        "Since 6.3, the 'key' parameter has been renamed 'project'")
       .setSince("4.0")
       .setPost(true)
       .setResponseExample(getClass().getResource("create-example.json"))
index 637286daf07cef60bec51eaff0c0fced353bc880..785b12513ff9da200e317737a9097c33a6a6cdef 100644 (file)
@@ -22,10 +22,10 @@ package org.sonar.server.component;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
-import org.sonar.api.resources.Qualifiers;
 
 import static com.google.common.base.Strings.repeat;
 import static org.assertj.core.api.Java6Assertions.assertThat;
+import static org.sonar.api.resources.Qualifiers.PROJECT;
 import static org.sonar.server.component.NewComponent.newComponentBuilder;
 
 public class NewComponentTest {
@@ -44,10 +44,19 @@ public class NewComponentTest {
   }
 
   @Test
-  public void build_throws_NPE_when_key_is_null() {
+  public void build_throws_IAE_when_key_is_null() {
     underTest.setOrganizationUuid(ORGANIZATION_UUID);
 
-    expectBuildException(NullPointerException.class, "key can't be null");
+    expectBuildException(IllegalArgumentException.class, "Component key can't be empty");
+  }
+
+  @Test
+  public void build_throws_IAE_when_key_is_empty() {
+    underTest
+      .setKey("")
+      .setOrganizationUuid(ORGANIZATION_UUID);
+
+    expectBuildException(IllegalArgumentException.class, "Component key can't be empty");
   }
 
   @Test
@@ -61,11 +70,20 @@ public class NewComponentTest {
   }
 
   @Test
-  public void build_fails_with_NPE_when_name_is_null() {
+  public void build_fails_with_IAE_when_name_is_null() {
     underTest.setOrganizationUuid(ORGANIZATION_UUID)
       .setKey(KEY);
 
-    expectBuildException(NullPointerException.class, "name can't be null");
+    expectBuildException(IllegalArgumentException.class, "Component name can't be empty");
+  }
+
+  @Test
+  public void build_fails_with_IAE_when_name_is_empty() {
+    underTest.setOrganizationUuid(ORGANIZATION_UUID)
+      .setKey(KEY)
+      .setName("");
+
+    expectBuildException(IllegalArgumentException.class, "Component name can't be empty");
   }
 
   @Test
@@ -79,6 +97,26 @@ public class NewComponentTest {
       "Component name length (2001) is longer than the maximum authorized (2000)");
   }
 
+  @Test
+  public void build_fails_with_IAE_when_qualifier_is_null() {
+    underTest.setOrganizationUuid(ORGANIZATION_UUID)
+      .setKey(KEY)
+      .setName(NAME)
+      .setQualifier(null);
+
+    expectBuildException(IllegalArgumentException.class, "Component qualifier can't be empty");
+  }
+
+  @Test
+  public void build_fails_with_IAE_when_qualifier_is_empty() {
+    underTest.setOrganizationUuid(ORGANIZATION_UUID)
+      .setKey(KEY)
+      .setName(NAME)
+      .setQualifier("");
+
+    expectBuildException(IllegalArgumentException.class, "Component qualifier can't be empty");
+  }
+
   @Test
   public void build_fails_with_IAE_when_qualifier_is_longer_than_10_characters() {
     underTest.setOrganizationUuid(ORGANIZATION_UUID)
@@ -94,11 +132,11 @@ public class NewComponentTest {
   @Test
   public void getQualifier_returns_PROJECT_when_no_set_in_builder() {
     NewComponent newComponent = underTest.setOrganizationUuid(ORGANIZATION_UUID)
-        .setKey(KEY)
-        .setName(NAME)
-        .build();
+      .setKey(KEY)
+      .setName(NAME)
+      .build();
 
-    assertThat(newComponent.qualifier()).isEqualTo(Qualifiers.PROJECT);
+    assertThat(newComponent.qualifier()).isEqualTo(PROJECT);
   }
 
   private void expectBuildException(Class<? extends Exception> expectedExceptionType, String expectedMessage) {
index c352c2471d4f6451bd5f89aec47ccac1806bfe0e..bfc0d9f7e59718df822b97ffee7744841665339b 100644 (file)
@@ -21,6 +21,7 @@
 package org.sonar.db.component;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Strings.isNullOrEmpty;
 import static org.sonar.core.component.ComponentKeys.MAX_COMPONENT_KEY_LENGTH;
 
 public class ComponentValidator {
@@ -32,18 +33,21 @@ public class ComponentValidator {
   }
 
   public static String checkComponentName(String name) {
+    checkArgument(!isNullOrEmpty(name), "Component name can't be empty");
     checkArgument(name.length() <= MAX_COMPONENT_NAME_LENGTH, "Component name length (%s) is longer than the maximum authorized (%s). '%s' was provided.",
       name.length(), MAX_COMPONENT_NAME_LENGTH, name);
     return name;
   }
 
   public static String checkComponentKey(String key) {
+    checkArgument(!isNullOrEmpty(key), "Component key can't be empty");
     checkArgument(key.length() <= MAX_COMPONENT_KEY_LENGTH, "Component key length (%s) is longer than the maximum authorized (%s). '%s' was provided.",
       key.length(), MAX_COMPONENT_KEY_LENGTH, key);
     return key;
   }
 
   public static String checkComponentQualifier(String qualifier) {
+    checkArgument(!isNullOrEmpty(qualifier), "Component qualifier can't be empty");
     checkArgument(qualifier.length() <= MAX_COMPONENT_QUALIFIER_LENGTH, "Component qualifier length (%s) is longer than the maximum authorized (%s). '%s' was provided.",
       qualifier.length(), MAX_COMPONENT_QUALIFIER_LENGTH, qualifier);
     return qualifier;