diff options
author | Godin <mandrikov@gmail.com> | 2010-12-07 15:13:02 +0000 |
---|---|---|
committer | Godin <mandrikov@gmail.com> | 2010-12-07 15:13:02 +0000 |
commit | 8e9d7ff89a66bf987d7fcb7c298a53a992625375 (patch) | |
tree | 6e810a67b7d5ad7a4e9009cfbc4bc5502502bccb | |
parent | 2f65b14c5b45bf513cfb0a56d1087c4a49299744 (diff) | |
download | sonarqube-8e9d7ff89a66bf987d7fcb7c298a53a992625375.tar.gz sonarqube-8e9d7ff89a66bf987d7fcb7c298a53a992625375.zip |
SONAR-1605: Correct backup should be created if value of property already contains CDATA section
3 files changed, 55 insertions, 3 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/configuration/Backup.java b/sonar-server/src/main/java/org/sonar/server/configuration/Backup.java index 1e88d02b3db..a8ed347761e 100644 --- a/sonar-server/src/main/java/org/sonar/server/configuration/Backup.java +++ b/sonar-server/src/main/java/org/sonar/server/configuration/Backup.java @@ -27,6 +27,7 @@ import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; import com.thoughtworks.xstream.io.xml.XppDriver; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.CharEncoding; +import org.apache.commons.lang.StringUtils; import org.sonar.api.database.DatabaseSession; import org.sonar.jpa.entity.SchemaMigration; @@ -147,17 +148,24 @@ public class Backup { @Override protected void writeText(QuickWriter writer, String text) { writer.write("<![CDATA["); + /* + * See http://jira.codehaus.org/browse/SONAR-1605 + * According to XML specification ( http://www.w3.org/TR/REC-xml/#sec-cdata-sect ) + * CData section may contain everything except of sequence ']]>' + * so we will split all occurrences of this sequence into two CDATA + * first one would contain ']]' and second '>' + */ + text = StringUtils.replace(text, "]]>", "]]]]><![CDATA[>"); writer.write(text); writer.write("]]>"); } }; } - } - ); + }); xStream.processAnnotations(SonarConfig.class); xStream.addDefaultImplementation(ArrayList.class, Collection.class); - xStream.registerConverter(new DateConverter(DATE_FORMAT, new String[]{})); + xStream.registerConverter(new DateConverter(DATE_FORMAT, new String[] {})); for (Backupable backupable : backupables) { backupable.configure(xStream); diff --git a/sonar-server/src/test/java/org/sonar/server/configuration/BackupTest.java b/sonar-server/src/test/java/org/sonar/server/configuration/BackupTest.java index 07d86fce8e6..4bcc6d92584 100644 --- a/sonar-server/src/test/java/org/sonar/server/configuration/BackupTest.java +++ b/sonar-server/src/test/java/org/sonar/server/configuration/BackupTest.java @@ -181,6 +181,19 @@ public class BackupTest { assertThat(metric.getId(), nullValue()); } + @Test + public void shouldExportAndImportInnerCDATA() throws Exception { + SonarConfig sonarConfig = getSonarConfig(); + sonarConfig.setProperties(getPropertiesWithCDATA()); + + Backup backup = new Backup(Arrays.asList(new MetricsBackup(null), new PropertiesBackup(null))); + String xml = backup.getXmlFromSonarConfig(sonarConfig); + assertXmlAreSimilar(xml, "backup-with-splitted-cdata.xml"); + + sonarConfig = backup.getSonarConfigFromXml(xml); + assertTrue(CollectionUtils.isEqualCollection(sonarConfig.getProperties(), getPropertiesWithCDATA())); + } + private SonarConfig getSonarConfig() throws ParseException { DateFormat dateFormat = new SimpleDateFormat(Backup.DATE_FORMAT); Date date = dateFormat.parse("2008-11-18"); @@ -225,6 +238,15 @@ public class BackupTest { return properties; } + private List<Property> getPropertiesWithCDATA() { + List<Property> properties = new ArrayList<Property>(); + properties.add(new Property("key1", "<![CDATA[value1]]>")); + properties.add(new Property("key2", "]]>value2")); + properties.add(new Property("key3", "prefix]]>value3")); + properties.add(new Property("key4", "<name><![CDATA[Forges]]></name>")); + return properties; + } + private List<Property> getPropertiesWithXmlIlliciteCharacters() { List<Property> properties = new ArrayList<Property>(); properties.add(new Property("key", "<value>")); diff --git a/sonar-server/src/test/resources/org/sonar/server/configuration/BackupTest/backup-with-splitted-cdata.xml b/sonar-server/src/test/resources/org/sonar/server/configuration/BackupTest/backup-with-splitted-cdata.xml new file mode 100644 index 00000000000..cc6031719bc --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/configuration/BackupTest/backup-with-splitted-cdata.xml @@ -0,0 +1,22 @@ +<sonar-config> + <version><![CDATA[54]]></version> + <date><![CDATA[2008-11-18]]></date> + <properties> + <property> + <key><![CDATA[key1]]></key> + <value><![CDATA[<![CDATA[value1]]]]><![CDATA[>]]></value> + </property> + <property> + <key><![CDATA[key2]]></key> + <value><![CDATA[]]]]><![CDATA[>value2]]></value> + </property> + <property> + <key><![CDATA[key3]]></key> + <value><![CDATA[prefix]]]]><![CDATA[>value3]]></value> + </property> + <property> + <key><![CDATA[key4]]></key> + <value><![CDATA[<name><![CDATA[Forges]]]]><![CDATA[></name>]]></value> + </property> + </properties> +</sonar-config> |