Find problem with delimiter

This commit is contained in:
Clément Fournier
2023-02-05 19:59:17 +01:00
parent a586a9bf11
commit 552b9e4bc0
4 changed files with 50 additions and 21 deletions

View File

@ -4,6 +4,10 @@
package net.sourceforge.pmd;
import static net.sourceforge.pmd.util.CollectionUtil.listOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayOutputStream;
@ -87,4 +91,19 @@ class RuleSetWriterTest {
String written = out.toString("UTF-8");
assertTrue(written.contains("ref=\"rulesets/dummy/basic.xml/DummyBasicMockRule\""));
}
@Test
void testXmlPropertyWithDelimiter() throws Exception {
RuleSet rs = new RuleSetLoader().loadFromResource("net/sourceforge/pmd/TestRulesetProperties.xml");
Rule rule = rs.getRuleByName("MockRule4");
assertEquals(listOf("bar", "foo"), rule.getProperty(rule.getPropertyDescriptor("stringList")));
assertEquals(listOf("bar", "foo"), rule.getProperty(rule.getPropertyDescriptor("stringListWithDelim")));
writer.write(rs);
String written = out.toString("UTF-8");
assertThat(written, containsString("delimiter="));
}
}

View File

@ -0,0 +1,29 @@
<?xml version="1.0"?>
<ruleset name="Test Ruleset" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>
Ruleset used by test net.sourceforge.pmd.RuleSetWriter and RuleSetFactoryTest
</description>
<rule name="MockRule4" language="dummy" since="1.0" message="Test Rule"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/test/TestRuleset1.xml#MockRule">
<description>
Just for test
</description>
<priority>3</priority>
<properties>
<property name="test2" type="String" description="test 2" value="foo"/>
<property name="stringList" type="List[String]" description="test 3" value="bar|foo"/>
<property name="stringListWithDelim" type="List[String]" delimiter="," description="test 3" value="bar,foo"/>
<property name="xpath" value="//dummyNode" />
</properties>
<example>
<![CDATA[
]]>
</example>
</rule>
</ruleset>

View File

@ -4,31 +4,11 @@
package net.sourceforge.pmd.lang.java;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test;
import net.sourceforge.pmd.AbstractRuleSetFactoryTest;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.RuleSetLoader;
/**
* Test java's rulesets
*/
class RuleSetFactoryTest extends AbstractRuleSetFactoryTest {
@Test
void testExclusionOfUselessParantheses() {
RuleSet ruleset = new RuleSetLoader().loadFromString("",
"<?xml version=\"1.0\"?>\n" + "<ruleset name=\"Custom ruleset for tests\"\n"
+ " xmlns=\"http://pmd.sourceforge.net/ruleset/2.0.0\"\n"
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
+ " xsi:schemaLocation=\"http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd\">\n"
+ " <description>Custom ruleset for tests</description>\n"
+ " <rule ref=\"category/java/codestyle.xml\">\n"
+ " <exclude name=\"UselessParentheses\"/>\n" + " </rule>\n" + "</ruleset>\n");
Rule rule = ruleset.getRuleByName("UselessParentheses");
assertNull(rule);
}
}

View File

@ -503,7 +503,8 @@ public abstract class AbstractRuleSetFactoryTest {
value1 = ((Pattern) value1).pattern();
value2 = ((Pattern) value2).pattern();
}
assertEquals(value1, value2, message + ", Rule property value " + j);
assertEquals(value1, value2, message + ", Rule " + rule1.getName() + " property "
+ propertyDescriptors1.get(j).name());
}
assertEquals(propertyDescriptors1.size(), propertyDescriptors2.size(),
message + ", Rule property descriptor count");