Delete multi property, add tests

This commit is contained in:
Clément Fournier
2018-03-06 23:29:49 +01:00
parent 9fcf146241
commit ca4500b40b
3 changed files with 126 additions and 100 deletions

View File

@ -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<Pattern> {
RegexMultiProperty(String theName, String theDescription, List<Pattern> 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<Pattern> type() {
return Pattern.class;
}
static MultiValue<Pattern, RegexMultiPBuilder> extractor() {
return new MultiValue<Pattern, RegexMultiPBuilder>(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<Pattern, RegexMultiPBuilder> {
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<Pattern> 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);
}
}
}

View File

@ -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<T> {
public void testFactorySingleValue() {
PropertyDescriptor<T> 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> {
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<T> {
@Test
public void testIsMultiValue() {
assertFalse(createProperty().isMultiValue());
assertTrue(createMultiProperty().isMultiValue());
}
@Test
public void testIsMultiValueMulti() {
assertTrue(createMultiProperty().isMultiValue());
}
@Test
public void testAddAttributes() {
Map<PropertyDescriptorField, String> atts = createProperty().attributeValuesById();
Map<PropertyDescriptorField, String> multiAtts = createMultiProperty().attributeValuesById();
assertTrue(atts.containsKey(PropertyDescriptorField.NAME));
assertTrue(atts.containsKey(PropertyDescriptorField.DESCRIPTION));
assertTrue(atts.containsKey(PropertyDescriptorField.DEFAULT_VALUE));
}
@Test
public void testAddAttributesMulti() {
Map<PropertyDescriptorField, String> 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<T> {
@Test
public void testType() {
assertNotNull(createMultiProperty().type());
assertNotNull(createProperty().type());
}
@Test
public void testTypeMulti() {
assertNotNull(createMultiProperty().type());
}
static boolean randomBool() {
return RANDOM.nextBoolean();
}

View File

@ -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<Pattern> {
public RegexPropertyTest() {
super("Regex");
}
@Override
protected Pattern createValue() {
return Pattern.compile("abc++");
}
@Override
protected Pattern createBadValue() {
return null;
}
@Override
protected PropertyDescriptor<Pattern> createProperty() {
return RegexProperty.named("foo").defaultValue("(ec|sa)+").desc("the description").build();
}
@Override
protected PropertyDescriptor<Pattern> 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<List<Pattern>> createMultiProperty() {
throw new UnsupportedOperationException();
}
@Override
protected PropertyDescriptor<List<Pattern>> 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() {
}
}