summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-11-07 17:13:03 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2012-11-07 17:13:17 +0100
commit7264e00ff2f411e6dacd65e467968b1ff1134583 (patch)
tree877c5c66c3981aa60fa2ba7a4a329df7455a667c
parent0c410b971537947ff4900e015fe71dd3e81157f2 (diff)
downloadsonarqube-7264e00ff2f411e6dacd65e467968b1ff1134583.tar.gz
sonarqube-7264e00ff2f411e6dacd65e467968b1ff1134583.zip
SONAR-3940 property relocation
-rw-r--r--sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java19
-rw-r--r--sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java1
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml4
-rw-r--r--sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java35
4 files changed, 59 insertions, 0 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java
index a496d826fc6..32f3b60fa4b 100644
--- a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java
+++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java
@@ -19,6 +19,8 @@
*/
package org.sonar.core.properties;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.sonar.api.BatchComponent;
@@ -131,4 +133,21 @@ public class PropertiesDao implements BatchComponent, ServerComponent {
MyBatis.closeQuietly(session);
}
}
+
+ public void renamePropertyKey(String oldKey, String newKey) {
+ Preconditions.checkArgument(!Strings.isNullOrEmpty(oldKey), "Old property key must not be empty");
+ Preconditions.checkArgument(!Strings.isNullOrEmpty(newKey), "New property key must not be empty");
+
+ if (!newKey.equals(oldKey)) {
+ SqlSession session = mybatis.openSession();
+ PropertiesMapper mapper = session.getMapper(PropertiesMapper.class);
+ try {
+ mapper.renamePropertyKey(oldKey, newKey);
+ session.commit();
+
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+ }
}
diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java
index cb63e4bb227..c80a4d32c0f 100644
--- a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java
+++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java
@@ -34,4 +34,5 @@ public interface PropertiesMapper {
void insert(PropertyDto property);
void deleteGlobalProperty(String key);
void deleteGlobalProperties();
+ void renamePropertyKey(@Param("oldKey") String oldKey, @Param("newKey") String newKey);
}
diff --git a/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml
index d3d55c0008f..756892b24f0 100644
--- a/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml
+++ b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml
@@ -72,4 +72,8 @@
delete from properties where resource_id is null and user_id is null
</delete>
+ <update id="renamePropertyKey" parameterType="map">
+ update properties set prop_key = #{newKey} where prop_key=#{oldKey}
+ </update>
+
</mapper>
diff --git a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java
index 6e7ad7fffa7..876d5d33c73 100644
--- a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java
@@ -21,7 +21,9 @@ package org.sonar.core.properties;
import com.google.common.collect.ImmutableMap;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.core.persistence.AbstractDaoTestCase;
import java.util.List;
@@ -34,6 +36,9 @@ public class PropertiesDaoTest extends AbstractDaoTestCase {
private PropertiesDao dao;
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
@Before
public void createDao() {
dao = new PropertiesDao(getMyBatis());
@@ -132,6 +137,36 @@ public class PropertiesDaoTest extends AbstractDaoTestCase {
checkTable("updateGlobalProperties", "properties", "prop_key", "text_value", "resource_id", "user_id");
}
+ @Test
+ public void renamePropertyKey() {
+ setupData("renamePropertyKey");
+
+ dao.renamePropertyKey("sonar.license.secured", "sonar.license");
+
+ checkTable("renamePropertyKey", "properties", "prop_key", "text_value", "resource_id", "user_id");
+ }
+
+ @Test
+ public void should_not_rename_if_same_key() {
+ setupData("should_not_rename_if_same_key");
+
+ dao.renamePropertyKey("foo", "foo");
+
+ checkTable("should_not_rename_if_same_key", "properties", "prop_key", "text_value", "resource_id", "user_id");
+ }
+
+ @Test
+ public void should_not_rename_with_empty_key() {
+ thrown.expect(IllegalArgumentException.class);
+ dao.renamePropertyKey("foo", "");
+ }
+
+ @Test
+ public void should_not_rename_an_empty_key() {
+ thrown.expect(IllegalArgumentException.class);
+ dao.renamePropertyKey(null, "foo");
+ }
+
private PropertyDto findById(List<PropertyDto> properties, int id) {
for (PropertyDto property : properties) {
if (property.getId() == id) {