#1316 Multi Rule Properties with delimiter not possible
This commit is contained in:
@ -22,7 +22,7 @@ public class StringMultiProperty extends AbstractDelimitedProperty<String[]> {
|
||||
public static final PropertyDescriptorFactory FACTORY = new BasicPropertyDescriptorFactory<StringMultiProperty>(String[].class) {
|
||||
|
||||
public StringMultiProperty createWith(Map<String, String> valuesById) {
|
||||
final char delimiter = delimiterIn(valuesById);
|
||||
final char delimiter = delimiterIn(valuesById, DEFAULT_DELIMITER);
|
||||
return new StringMultiProperty(
|
||||
nameIn(valuesById),
|
||||
descriptionIn(valuesById),
|
||||
|
@ -34,8 +34,8 @@ public class BasicPropertyDescriptorFactory<T> implements PropertyDescriptorFact
|
||||
private final Map<String, Boolean> fieldTypesByKey;
|
||||
|
||||
protected static final Map<String, Boolean> CORE_FIELD_TYPES_BY_KEY = CollectionUtil.mapFrom(
|
||||
new String[] { NAME, DESC, DEFAULT_VALUE},
|
||||
new Boolean[] { Boolean.TRUE, Boolean.TRUE, Boolean.TRUE}
|
||||
new String[] { NAME, DESC, DEFAULT_VALUE, DELIMITER},
|
||||
new Boolean[] { Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE}
|
||||
);
|
||||
|
||||
public BasicPropertyDescriptorFactory(Class<?> theValueType) {
|
||||
@ -187,8 +187,14 @@ public class BasicPropertyDescriptorFactory<T> implements PropertyDescriptorFact
|
||||
return null; // TODO
|
||||
}
|
||||
|
||||
protected static char delimiterIn(Map<String, String> valuesById) {
|
||||
String characterStr = valuesById.get(DELIMITER).trim();
|
||||
protected static char delimiterIn(Map<String, String> valuesById, char defaultDelimiter) {
|
||||
String characterStr = "";
|
||||
if (valuesById.containsKey(DELIMITER)) {
|
||||
characterStr = valuesById.get(DELIMITER).trim();
|
||||
}
|
||||
if (characterStr.isEmpty()) {
|
||||
return defaultDelimiter;
|
||||
}
|
||||
return characterStr.charAt(0);
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@ import net.sourceforge.pmd.lang.DummyLanguageModule;
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.rule.MockRule;
|
||||
import net.sourceforge.pmd.lang.rule.RuleReference;
|
||||
import net.sourceforge.pmd.lang.rule.properties.StringMultiProperty;
|
||||
import net.sourceforge.pmd.util.ResourceLoader;
|
||||
|
||||
import org.junit.Assert;
|
||||
@ -163,6 +164,50 @@ public class RuleSetFactoryTest {
|
||||
assertNotSame(r.getDescription().indexOf("testdesc2"), -1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringMultiPropertyDefaultDelimiter() throws Exception {
|
||||
Rule r = loadFirstRule("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
||||
"<ruleset>\n" +
|
||||
" <rule name=\"myRule\" message=\"Do not place to this package. Move to \n" +
|
||||
"{0} package/s instead.\" \n" +
|
||||
"class=\"net.sourceforge.pmd.lang.rule.XPathRule\" " +
|
||||
"language=\"dummy\">\n" +
|
||||
" <description>Please move your class to the right folder(rest \n" +
|
||||
"folder)</description>\n" +
|
||||
" <priority>2</priority>\n" +
|
||||
" <properties>\n" +
|
||||
" <property name=\"packageRegEx\" value=\"com.aptsssss|com.abc\" \n" +
|
||||
"type=\"String[]\" description=\"valid packages\"/>\n" +
|
||||
" </properties>"
|
||||
+ "</rule>"
|
||||
+ "</ruleset>");
|
||||
PropertyDescriptor<?> prop = r.getPropertyDescriptor("packageRegEx");
|
||||
String[] values = (String[])r.getProperty(prop);
|
||||
Assert.assertArrayEquals(new String[]{"com.aptsssss", "com.abc"}, values);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringMultiPropertyDelimiter() throws Exception {
|
||||
Rule r = loadFirstRule("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
||||
"<ruleset>\n" +
|
||||
" <rule name=\"myRule\" message=\"Do not place to this package. Move to \n" +
|
||||
"{0} package/s instead.\" \n" +
|
||||
"class=\"net.sourceforge.pmd.lang.rule.XPathRule\" " +
|
||||
"language=\"dummy\">\n" +
|
||||
" <description>Please move your class to the right folder(rest \n" +
|
||||
"folder)</description>\n" +
|
||||
" <priority>2</priority>\n" +
|
||||
" <properties>\n" +
|
||||
" <property name=\"packageRegEx\" value=\"com.aptsssss,com.abc\" \n" +
|
||||
"type=\"String[]\" delimiter=\",\" description=\"valid packages\"/>\n" +
|
||||
" </properties>"
|
||||
+ "</rule>"
|
||||
+ "</ruleset>");
|
||||
PropertyDescriptor<?> prop = r.getPropertyDescriptor("packageRegEx");
|
||||
String[] values = (String[])r.getProperty(prop);
|
||||
Assert.assertArrayEquals(new String[]{"com.aptsssss", "com.abc"}, values);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testXPath() throws RuleSetNotFoundException {
|
||||
|
@ -45,6 +45,7 @@
|
||||
* [#1308](https://sourceforge.net/p/pmd/bugs/1308/): PMD runs endlessly on some generated files
|
||||
* [#1312](https://sourceforge.net/p/pmd/bugs/1312/): Rule reference must not override rule name of referenced rule
|
||||
* [#1313](https://sourceforge.net/p/pmd/bugs/1313/): Missing assertion message in assertEquals with delta not detected
|
||||
* [#1316](https://sourceforge.net/p/pmd/bugs/1316/): Multi Rule Properties with delimiter not possible
|
||||
|
||||
**API Changes:**
|
||||
|
||||
|
Reference in New Issue
Block a user