From ca4500b40b1d5d8b9a5a6f12e031c7783df4fd2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Tue, 6 Mar 2018 23:29:49 +0100 Subject: [PATCH] Delete multi property, add tests --- .../pmd/properties/RegexMultiProperty.java | 90 ----------------- .../AbstractPropertyDescriptorTester.java | 38 +++++-- .../pmd/properties/RegexPropertyTest.java | 98 +++++++++++++++++++ 3 files changed, 126 insertions(+), 100 deletions(-) delete mode 100644 pmd-core/src/main/java/net/sourceforge/pmd/properties/RegexMultiProperty.java create mode 100644 pmd-core/src/test/java/net/sourceforge/pmd/properties/RegexPropertyTest.java diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/properties/RegexMultiProperty.java b/pmd-core/src/main/java/net/sourceforge/pmd/properties/RegexMultiProperty.java deleted file mode 100644 index 4f19084310..0000000000 --- a/pmd-core/src/main/java/net/sourceforge/pmd/properties/RegexMultiProperty.java +++ /dev/null @@ -1,90 +0,0 @@ -/** - * BSD-style license; for more info see http://pmd.sourceforge.net/license.html - */ - -package net.sourceforge.pmd.properties; - -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Pattern; - -import net.sourceforge.pmd.properties.builders.MultiValuePropertyBuilder; -import net.sourceforge.pmd.properties.builders.PropertyDescriptorBuilderConversionWrapper.MultiValue; - - -/** - * Property which takes a collection of regex patterns as its value. - * - * @author Clément Fournier - * @since 6.1.0 - */ -public final class RegexMultiProperty extends AbstractMultiValueProperty { - RegexMultiProperty(String theName, String theDescription, List theDefault, float theUIOrder, char delimiter, boolean isDefinedExternally) { - super(theName, theDescription, theDefault, theUIOrder, delimiter, isDefinedExternally); - } - - - @Override - protected Pattern createFrom(String toParse) { - return Pattern.compile(toParse); - } - - - @Override - public Class type() { - return Pattern.class; - } - - - static MultiValue extractor() { - return new MultiValue(Pattern.class, ValueParserConstants.REGEX_PARSER) { - @Override - protected RegexMultiPBuilder newBuilder(String name) { - return new RegexMultiPBuilder(name); - } - }; - } - - - /** - * Creates a new builder for a regex multi property. - * - * @param name The name of the property - * - * @return A new builder - */ - public static RegexMultiPBuilder named(String name) { - return new RegexMultiPBuilder(name); - } - - - /** Builder for a {@link RegexMultiProperty}. */ - public static final class RegexMultiPBuilder extends MultiValuePropertyBuilder { - private RegexMultiPBuilder(String name) { - super(name); - } - - - /** - * Specify default patterns for the property. - * The arguments must be valid regex patterns. - * - * @param val Regex patterns - * - * @return The same builder - */ - public RegexMultiPBuilder defaultValues(String... val) { - List ps = new ArrayList<>(); - for (String s : val) { - ps.add(Pattern.compile(s)); - } - return super.defaultValues(ps); - } - - - @Override - public RegexMultiProperty build() { - return new RegexMultiProperty(this.name, this.description, this.defaultValues, this.uiOrder, this.multiValueDelimiter, isDefinedInXML); - } - } -} diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/properties/AbstractPropertyDescriptorTester.java b/pmd-core/src/test/java/net/sourceforge/pmd/properties/AbstractPropertyDescriptorTester.java index 4420ba926c..8cd9cfb06e 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/properties/AbstractPropertyDescriptorTester.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/properties/AbstractPropertyDescriptorTester.java @@ -16,6 +16,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; +import java.util.regex.Pattern; import org.junit.Test; @@ -55,11 +56,13 @@ public abstract class AbstractPropertyDescriptorTester { public void testFactorySingleValue() { PropertyDescriptor prop = getSingleFactory().build(getPropertyDescriptorValues()); T originalValue = createValue(); - T value = prop.valueFrom( - originalValue instanceof Class ? ((Class) originalValue).getName() : String.valueOf(originalValue)); - String asDelimitedString = prop.asDelimitedString(value); - Object value2 = prop.valueFrom(asDelimitedString); - assertEquals(value, value2); + T value = prop.valueFrom(originalValue instanceof Class ? ((Class) originalValue).getName() : String.valueOf(originalValue)); + T value2 = prop.valueFrom(prop.asDelimitedString(value)); + if (Pattern.class.equals(prop.type())) { + assertEquals(String.valueOf(value), String.valueOf(value2)); + } else { + assertEquals(value, value2); + } } @@ -188,7 +191,11 @@ public abstract class AbstractPropertyDescriptorTester { T returnedValue = pmdProp.valueFrom(storeValue); - assertEquals(returnedValue, testValue); + if (Pattern.class.equals(pmdProp.type())) { + assertEquals(String.valueOf(returnedValue), String.valueOf(testValue)); + } else { + assertEquals(returnedValue, testValue); + } } @@ -254,19 +261,26 @@ public abstract class AbstractPropertyDescriptorTester { @Test public void testIsMultiValue() { assertFalse(createProperty().isMultiValue()); - assertTrue(createMultiProperty().isMultiValue()); } + @Test + public void testIsMultiValueMulti() { + assertTrue(createMultiProperty().isMultiValue()); + } + @Test public void testAddAttributes() { Map atts = createProperty().attributeValuesById(); - Map multiAtts = createMultiProperty().attributeValuesById(); - assertTrue(atts.containsKey(PropertyDescriptorField.NAME)); assertTrue(atts.containsKey(PropertyDescriptorField.DESCRIPTION)); assertTrue(atts.containsKey(PropertyDescriptorField.DEFAULT_VALUE)); + } + + @Test + public void testAddAttributesMulti() { + Map multiAtts = createMultiProperty().attributeValuesById(); assertTrue(multiAtts.containsKey(PropertyDescriptorField.DELIMITER)); assertTrue(multiAtts.containsKey(PropertyDescriptorField.NAME)); assertTrue(multiAtts.containsKey(PropertyDescriptorField.DESCRIPTION)); @@ -276,11 +290,15 @@ public abstract class AbstractPropertyDescriptorTester { @Test public void testType() { - assertNotNull(createMultiProperty().type()); assertNotNull(createProperty().type()); } + @Test + public void testTypeMulti() { + assertNotNull(createMultiProperty().type()); + } + static boolean randomBool() { return RANDOM.nextBoolean(); } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/properties/RegexPropertyTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/properties/RegexPropertyTest.java new file mode 100644 index 0000000000..5aa8afe0c2 --- /dev/null +++ b/pmd-core/src/test/java/net/sourceforge/pmd/properties/RegexPropertyTest.java @@ -0,0 +1,98 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.properties; + +import java.util.List; +import java.util.regex.Pattern; + + +/** + * @author Clément Fournier + * @since 6.1.0 + */ +public class RegexPropertyTest extends AbstractPropertyDescriptorTester { + public RegexPropertyTest() { + super("Regex"); + } + + + @Override + protected Pattern createValue() { + return Pattern.compile("abc++"); + } + + + @Override + protected Pattern createBadValue() { + return null; + } + + + @Override + protected PropertyDescriptor createProperty() { + return RegexProperty.named("foo").defaultValue("(ec|sa)+").desc("the description").build(); + } + + + @Override + protected PropertyDescriptor createBadProperty() { + return RegexProperty.named("foo").defaultValue("(ec|sa").desc("the description").build(); + } + + + // The following are deliberately unimplemented, since they are only relevant to the tests of the multiproperty + + @Override + protected PropertyDescriptor> createMultiProperty() { + throw new UnsupportedOperationException(); + } + + + @Override + protected PropertyDescriptor> createBadMultiProperty() { + throw new UnsupportedOperationException(); + } + + @Override + public void testAddAttributesMulti() { + } + + + @Override + public void testAsDelimitedString() { + } + + + @Override + public void testErrorForBadMulti() { + } + + + @Override + public void testErrorForCorrectMulti() { + } + + + @Override + public void testFactoryMultiValueDefaultDelimiter() { + } + + + @Override + public void testFactoryMultiValueCustomDelimiter() { + } + + + @Override + public void testTypeMulti() { + } + + + @Override + public void testIsMultiValueMulti() { + } + + +}