aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-08-12 08:32:08 +0200
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-08-12 10:39:37 +0200
commitafdf3ba619eb80c7475afb0b69c1cabfcdd08833 (patch)
tree8ebf3363aa41c4d25b73576f7ebde5a850016c25
parent9ae5317b5e30245dd85b88723af06c995816d406 (diff)
downloadsonarqube-afdf3ba619eb80c7475afb0b69c1cabfcdd08833.tar.gz
sonarqube-afdf3ba619eb80c7475afb0b69c1cabfcdd08833.zip
SONAR-7930 WS api/components/bulk_update_key handles special characters
-rw-r--r--sonar-db/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java16
-rw-r--r--sonar-db/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java7
2 files changed, 15 insertions, 8 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java b/sonar-db/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java
index a57b6734c76..1eb408af1f1 100644
--- a/sonar-db/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/component/ComponentKeyUpdaterDao.java
@@ -83,7 +83,7 @@ public class ComponentKeyUpdaterDao implements Dao {
try {
Set<ResourceDto> modules = collectAllModules(projectUuid, stringToReplace, mapper);
for (ResourceDto module : modules) {
- String newKey = computeNewKey(module, stringToReplace, replacementString);
+ String newKey = computeNewKey(module.getKey(), stringToReplace, replacementString);
if (mapper.countResourceByKey(newKey) > 0) {
result.put(module.getKey(), "#duplicate_key#");
} else {
@@ -109,7 +109,7 @@ public class ComponentKeyUpdaterDao implements Dao {
.stream()
.collect(Collectors.toMap(
ResourceDto::getKey,
- component -> computeNewKey(component, stringToReplace, replacementString)));
+ component -> computeNewKey(component.getKey(), stringToReplace, replacementString)));
}
/**
@@ -119,6 +119,10 @@ public class ComponentKeyUpdaterDao implements Dao {
return newComponentKeys.stream().collect(Collectors.toMap(Function.identity(), key -> mapper(dbSession).countResourceByKey(key) > 0));
}
+ public static String computeNewKey(String key, String stringToReplace, String replacementString) {
+ return key.replace(stringToReplace, replacementString);
+ }
+
public void bulkUpdateKey(DbSession session, String projectUuid, String stringToReplace, String replacementString) {
ComponentKeyUpdaterMapper mapper = session.getMapper(ComponentKeyUpdaterMapper.class);
// must SELECT first everything
@@ -132,17 +136,13 @@ public class ComponentKeyUpdaterDao implements Dao {
// and then proceed with the batch UPDATE at once
for (ResourceDto module : modules) {
String oldModuleKey = module.getKey();
- String newModuleKey = computeNewKey(module, stringToReplace, replacementString);
+ String newModuleKey = computeNewKey(module.getKey(), stringToReplace, replacementString);
Collection<ResourceDto> resources = Lists.newArrayList(module);
resources.addAll(allResourcesByModuleMap.get(module));
runBatchUpdateForAllResources(resources, oldModuleKey, newModuleKey, mapper);
}
}
- private static String computeNewKey(ResourceDto resource, String stringToReplace, String replacementString) {
- return resource.getKey().replaceAll(stringToReplace, replacementString);
- }
-
private static void runBatchUpdateForAllResources(Collection<ResourceDto> resources, String oldKey, String newKey, ComponentKeyUpdaterMapper mapper) {
for (ResourceDto resource : resources) {
String oldResourceKey = resource.getKey();
@@ -171,7 +171,7 @@ public class ComponentKeyUpdaterDao implements Dao {
private static void checkNewNameOfAllModules(Set<ResourceDto> modules, String stringToReplace, String replacementString, ComponentKeyUpdaterMapper mapper) {
for (ResourceDto module : modules) {
- String newKey = computeNewKey(module, stringToReplace, replacementString);
+ String newKey = computeNewKey(module.getKey(), stringToReplace, replacementString);
checkArgument(isValidModuleKey(newKey), "Malformed key for '%s'. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.", newKey);
if (mapper.countResourceByKey(newKey) > 0) {
throw new IllegalArgumentException("Impossible to update key: a component with key \"" + newKey + "\" already exists.");
diff --git a/sonar-db/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java b/sonar-db/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java
index 25b86210b58..eb74073d6b7 100644
--- a/sonar-db/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java
@@ -33,6 +33,7 @@ import org.sonar.db.DbTester;
import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
+import static org.sonar.db.component.ComponentKeyUpdaterDao.computeNewKey;
import static org.sonar.db.component.ComponentTesting.newDirectory;
import static org.sonar.db.component.ComponentTesting.newFileDto;
import static org.sonar.db.component.ComponentTesting.newModuleDto;
@@ -219,4 +220,10 @@ public class ComponentKeyUpdaterDaoTest {
.hasSize(2)
.containsOnly(entry("project", "new-project"), entry("project:enabled-module", "new-project:enabled-module"));
}
+
+ @Test
+ public void compute_new_key() {
+ assertThat(computeNewKey("my_project", "my_", "your_")).isEqualTo("your_project");
+ assertThat(computeNewKey("my_project", "my_", "$()_")).isEqualTo("$()_project");
+ }
}