From afdf3ba619eb80c7475afb0b69c1cabfcdd08833 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Fri, 12 Aug 2016 08:32:08 +0200 Subject: [PATCH] SONAR-7930 WS api/components/bulk_update_key handles special characters --- .../db/component/ComponentKeyUpdaterDao.java | 16 ++++++++-------- .../db/component/ComponentKeyUpdaterDaoTest.java | 7 +++++++ 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 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 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 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 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"); + } } -- 2.39.5