*/
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;
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);
+ }
+ }
+ }
}
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;
private PropertiesDao dao;
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
@Before
public void createDao() {
dao = new PropertiesDao(getMyBatis());
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) {