diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-10-23 10:38:21 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-10-23 10:38:21 +0200 |
commit | ca503010d27a1ccd712b5cefc37e068caeba0d0b (patch) | |
tree | 2cc78b6a2a6cde536618fba77084724e5df42544 /plugins | |
parent | a7839618efe0e85396e9d9fab7315e55ecbd6889 (diff) | |
download | sonarqube-ca503010d27a1ccd712b5cefc37e068caeba0d0b.tar.gz sonarqube-ca503010d27a1ccd712b5cefc37e068caeba0d0b.zip |
SONAR-3903 Error when a duplication is detected on a filename containing XML entities like ampersand
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java | 3 | ||||
-rw-r--r-- | plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarEngineTest.java | 17 |
2 files changed, 19 insertions, 1 deletions
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java index 88c2c90656a..7187746e5ad 100644 --- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java +++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java @@ -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>"); diff --git a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarEngineTest.java b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarEngineTest.java index 434049fdbd6..c5511962b37 100644 --- a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarEngineTest.java +++ b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/SonarEngineTest.java @@ -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 & Reds/SubsRedsDelivery.cs\"/>" + + "<b s=\"15\" l=\"200\" r=\"Loads/File Loads/Subs & Reds/SubsRedsDelivery2.cs\"/>" + + "</g></duplications>"))); + } + private CloneGroup newCloneGroup(ClonePart... parts) { return CloneGroup.builder().setLength(0).setOrigin(parts[0]).setParts(Arrays.asList(parts)).build(); } |