]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1605: Correct backup should be created if value of property already contains...
authorGodin <mandrikov@gmail.com>
Tue, 7 Dec 2010 15:13:02 +0000 (15:13 +0000)
committerGodin <mandrikov@gmail.com>
Tue, 7 Dec 2010 15:13:02 +0000 (15:13 +0000)
sonar-server/src/main/java/org/sonar/server/configuration/Backup.java
sonar-server/src/test/java/org/sonar/server/configuration/BackupTest.java
sonar-server/src/test/resources/org/sonar/server/configuration/BackupTest/backup-with-splitted-cdata.xml [new file with mode: 0644]

index 1e88d02b3dbf2cf257a62baefdcb828e18b8b641..a8ed347761e8ceca39d438675736d51c2f80bc43 100644 (file)
@@ -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);
index 07d86fce8e6f4ff54c78107568bf0030981e0f48..4bcc6d92584aff54a86374407f45dc77685810dd 100644 (file)
@@ -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 (file)
index 0000000..cc60317
--- /dev/null
@@ -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>