]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3940 property relocation
authorSimon Brandhof <simon.brandhof@gmail.com>
Wed, 7 Nov 2012 16:13:03 +0000 (17:13 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Wed, 7 Nov 2012 16:13:17 +0000 (17:13 +0100)
sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java
sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java
sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml
sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java

index a496d826fc68e535908417fb4968f1895193d25c..32f3b60fa4b338e33595abe9ad24f72a0ce73872 100644 (file)
@@ -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);
+      }
+    }
+  }
 }
index cb63e4bb2271d4ff723442fa86c8adb47ff12be6..c80a4d32c0f0d6c2089c42f309f297a3f1b535a5 100644 (file)
@@ -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);
 }
index d3d55c0008f0f8c04688d4d00e41a67483b67743..756892b24f06887c56311454db9cf594076f3ec1 100644 (file)
@@ -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>
index 6e7ad7fffa7f278e050129e8b724e2b64d57096e..876d5d33c7380231caa6c5357e403a819ca1fca1 100644 (file)
@@ -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) {