diff options
author | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2013-10-14 11:23:06 +0200 |
---|---|---|
committer | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2013-10-14 14:09:03 +0200 |
commit | 2f58b5598d1e8f80b8586bb44bc88020e36fcaf6 (patch) | |
tree | 1c6e26f34e0a88d9a3a2d562ce5cd9934a5fd055 /sonar-server | |
parent | e703a47e39eda1149411f026c63123cd12793bd5 (diff) | |
download | sonarqube-2f58b5598d1e8f80b8586bb44bc88020e36fcaf6.tar.gz sonarqube-2f58b5598d1e8f80b8586bb44bc88020e36fcaf6.zip |
SONAR-3871 Check key format also on update
Diffstat (limited to 'sonar-server')
2 files changed, 31 insertions, 14 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/component/DefaultRubyComponentService.java b/sonar-server/src/main/java/org/sonar/server/component/DefaultRubyComponentService.java index b4b51b736f4..abee537dda9 100644 --- a/sonar-server/src/main/java/org/sonar/server/component/DefaultRubyComponentService.java +++ b/sonar-server/src/main/java/org/sonar/server/component/DefaultRubyComponentService.java @@ -19,12 +19,11 @@ */ package org.sonar.server.component; -import org.sonar.core.component.ComponentKeys; - import com.google.common.base.Strings; import org.sonar.api.component.Component; import org.sonar.api.component.RubyComponentService; import org.sonar.core.component.ComponentDto; +import org.sonar.core.component.ComponentKeys; import org.sonar.core.resource.ResourceDao; import org.sonar.core.resource.ResourceDto; import org.sonar.core.resource.ResourceIndexerDao; @@ -58,9 +57,7 @@ public class DefaultRubyComponentService implements RubyComponentService { if (component != null) { throw new BadRequestException("Could not create resource, key already exists: "+kee); } - if (!ComponentKeys.isValidModuleKey(kee)) { - throw new BadRequestException("Could not create resource, malformed key: "+kee); - } + checkKeyFormat(kee); resourceDao.insertOrUpdate( new ResourceDto() @@ -82,6 +79,8 @@ public class DefaultRubyComponentService implements RubyComponentService { if (resource == null) { throw new NotFoundException(); } + checkKeyFormat(key); + resourceDao.insertOrUpdate(resource.setKey(key).setName(name)); } @@ -123,4 +122,9 @@ public class DefaultRubyComponentService implements RubyComponentService { return builder.build(); } + private static void checkKeyFormat(String kee) { + if (!ComponentKeys.isValidModuleKey(kee)) { + throw new BadRequestException("Could not create resource, malformed key: "+kee); + } + } } diff --git a/sonar-server/src/test/java/org/sonar/server/component/DefaultRubyComponentServiceTest.java b/sonar-server/src/test/java/org/sonar/server/component/DefaultRubyComponentServiceTest.java index c6cb10de180..890ab13bc95 100644 --- a/sonar-server/src/test/java/org/sonar/server/component/DefaultRubyComponentServiceTest.java +++ b/sonar-server/src/test/java/org/sonar/server/component/DefaultRubyComponentServiceTest.java @@ -20,19 +20,18 @@ package org.sonar.server.component; -import org.sonar.server.exceptions.NotFoundException; - -import org.sonar.server.exceptions.BadRequestException; -import org.sonar.core.resource.ResourceDto; +import org.junit.Before; +import org.junit.Test; import org.mockito.ArgumentCaptor; +import org.sonar.api.component.Component; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Scopes; import org.sonar.core.component.ComponentDto; -import org.sonar.core.resource.ResourceIndexerDao; -import org.junit.Before; -import org.junit.Test; -import org.sonar.api.component.Component; import org.sonar.core.resource.ResourceDao; +import org.sonar.core.resource.ResourceDto; +import org.sonar.core.resource.ResourceIndexerDao; +import org.sonar.server.exceptions.BadRequestException; +import org.sonar.server.exceptions.NotFoundException; import java.util.List; import java.util.Map; @@ -41,7 +40,11 @@ import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Maps.newHashMap; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.*; +import static org.mockito.Matchers.anyListOf; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class DefaultRubyComponentServiceTest { @@ -140,6 +143,16 @@ public class DefaultRubyComponentServiceTest { verify(resourceDao).insertOrUpdate(resource); } + @Test(expected=BadRequestException.class) + public void should_throw_if_malformed_key_in_update() { + final long componentId = 1234l; + final String newKey = "new/key"; + final String newName = "newName"; + ResourceDto resource = mock(ResourceDto.class); + when(resourceDao.getResource(componentId)).thenReturn(resource); + componentService.updateComponent(componentId, newKey, newName); + } + @Test public void should_find() { List<String> qualifiers = newArrayList("TRK"); |