Skip to content

Commit

Permalink
SONARGROOV-39 Escape value of CodeNarc properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Wohops committed Aug 3, 2015
1 parent d07a93a commit 7442229
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonar.plugins.groovy.codenarc;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.rules.ActiveRule;
Expand Down Expand Up @@ -82,7 +83,7 @@ private void appendRule(ActiveRule activeRule) throws IOException {
writer.append("<property name=\"")
.append(activeRuleParam.getKey())
.append("\" value=\"")
.append(value)
.append(StringEscapeUtils.escapeXml(value))
.append(AUTO_CLOSING_TAG);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ public void shouldNotExportParametersWithDefaultValue() throws Exception {
writer.toString());
}

@Test
public void shouldEscapeExportedParameters() throws Exception {
Rule rule = Rule.create(CodeNarcRulesDefinition.REPOSITORY_KEY, "org.codenarc.rule.naming.ClassNameRule", "Class Name");
rule.createParameter("regex").setDefaultValue("([A-Z]\\w*\\$?)*");
profile.activateRule(rule, RulePriority.MAJOR).setParameter("regex", "[A-Z]+[a-z&&[^bc]]");

exporter.exportProfile(profile);

assertSimilarXml(
TestUtils.getResource("/org/sonar/plugins/groovy/codenarc/exportProfile/exportEscapedParameters.xml"),
writer.toString());
}

private void assertSimilarXml(File expectedFile, String xml) throws Exception {
XMLUnit.setIgnoreWhitespace(true);
Reader reader = new FileReader(expectedFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns="http://codenarc.org/ruleset/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://codenarc.org/ruleset/1.0 http://codenarc.org/ruleset-schema.xsd"
xsi:noNamespaceSchemaLocation="http://codenarc.org/ruleset-schema.xsd">
<rule class="org.codenarc.rule.naming.ClassNameRule">
<property name="regex" value="[A-Z]+[a-z&amp;&amp;[^bc]]"/>
</rule>
</ruleset>

0 comments on commit 7442229

Please sign in to comment.