]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3903 Error when a duplication is detected on a filename containing XML entities...
authorSimon Brandhof <simon.brandhof@gmail.com>
Tue, 23 Oct 2012 08:38:21 +0000 (10:38 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Tue, 23 Oct 2012 08:38:21 +0000 (10:38 +0200)
plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java
plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarEngineTest.java

index 88c2c90656ae23c21d44a91f7976760018b022ed..7187746e5ad4b9f77e3a85e3f1d72b111b8aff9a 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.plugins.cpd;
 
 import com.google.common.collect.Iterables;
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringEscapeUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.batch.SensorContext;
@@ -210,7 +211,7 @@ public class SonarEngine extends CpdEngine {
       for (ClonePart part : duplication.getCloneParts()) {
         xml.append("<b s=\"").append(part.getStartLine())
             .append("\" l=\"").append(part.getLines())
-            .append("\" r=\"").append(part.getResourceId())
+            .append("\" r=\"").append(StringEscapeUtils.escapeXml(part.getResourceId()))
             .append("\"/>");
       }
       xml.append("</g>");
index 434049fdbd6af82dbd2b052411688c9deb8e0f86..c5511962b370a79dff4c982fbcc126a186930786 100644 (file)
@@ -23,6 +23,7 @@ import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.batch.SensorContext;
 import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.resources.File;
 import org.sonar.api.resources.JavaFile;
 import org.sonar.api.resources.Resource;
 import org.sonar.api.test.IsMeasure;
@@ -129,6 +130,22 @@ public class SonarEngineTest {
             + "</duplications>")));
   }
 
+  @Test
+  public void shouldEscapeXmlEntities() {
+    File csharpFile = new File("Loads/File Loads/Subs & Reds/SubsRedsDelivery.cs");
+    List<CloneGroup> groups = Arrays.asList(newCloneGroup(
+      new ClonePart("Loads/File Loads/Subs & Reds/SubsRedsDelivery.cs", 0, 5, 204),
+      new ClonePart("Loads/File Loads/Subs & Reds/SubsRedsDelivery2.cs", 0, 15, 214)));
+    SonarEngine.save(context, csharpFile, groups);
+
+    verify(context).saveMeasure(
+        eq(csharpFile),
+        argThat(new IsMeasure(CoreMetrics.DUPLICATIONS_DATA, "<duplications><g>"
+            + "<b s=\"5\" l=\"200\" r=\"Loads/File Loads/Subs &amp; Reds/SubsRedsDelivery.cs\"/>"
+            + "<b s=\"15\" l=\"200\" r=\"Loads/File Loads/Subs &amp; Reds/SubsRedsDelivery2.cs\"/>"
+            + "</g></duplications>")));
+  }
+
   private CloneGroup newCloneGroup(ClonePart... parts) {
     return CloneGroup.builder().setLength(0).setOrigin(parts[0]).setParts(Arrays.asList(parts)).build();
   }