@ -181,7 +181,7 @@ public final class PropertyFactory {
|
||||
|
||||
|
||||
public static <T> GenericCollectionPropertyBuilder<T, List<T>> enumListProperty(String name, Map<String, T> nameToValue) {
|
||||
return enumProperty(name, nameToValue).toList();
|
||||
return enumProperty(name, nameToValue).toList().delim(MultiValuePropertyDescriptor.DEFAULT_DELIMITER);
|
||||
}
|
||||
|
||||
|
||||
|
@ -13,10 +13,11 @@ import static org.junit.Assert.assertThat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hamcrest.Matcher;
|
||||
@ -152,17 +153,30 @@ public class PropertyDescriptorTest {
|
||||
.desc("hello")
|
||||
.defaultValue(1)
|
||||
.build();
|
||||
assertEquals(Integer.valueOf(1), descriptor.defaultValue());
|
||||
assertEquals("intProp", descriptor.name());
|
||||
assertEquals("hello", descriptor.description());
|
||||
assertEquals(Integer.valueOf(1), descriptor.defaultValue());
|
||||
assertEquals(Integer.valueOf(5), descriptor.valueFrom("5"));
|
||||
|
||||
PropertyDescriptor<List<Integer>> listDescriptor = PropertyFactory.intListProperty("intListProp")
|
||||
.desc("hello")
|
||||
.defaultValues(1, 2)
|
||||
.build();
|
||||
assertEquals(Arrays.asList(1, 2), listDescriptor.defaultValue());
|
||||
assertEquals("intListProp", listDescriptor.name());
|
||||
assertEquals("hello", listDescriptor.description());
|
||||
assertEquals(Arrays.asList(1, 2), listDescriptor.defaultValue());
|
||||
assertEquals(Arrays.asList(5, 7), listDescriptor.valueFrom("5,7"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntPropertyInvalidValue() {
|
||||
PropertyDescriptor<Integer> descriptor = PropertyFactory.intProperty("intProp")
|
||||
.desc("hello")
|
||||
.defaultValue(1)
|
||||
.build();
|
||||
thrown.expect(NumberFormatException.class);
|
||||
thrown.expectMessage("not a number");
|
||||
descriptor.valueFrom("not a number");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -171,13 +185,30 @@ public class PropertyDescriptorTest {
|
||||
.desc("hello")
|
||||
.defaultValue(1.0)
|
||||
.build();
|
||||
assertEquals("doubleProp", descriptor.name());
|
||||
assertEquals("hello", descriptor.description());
|
||||
assertEquals(Double.valueOf(1.0), descriptor.defaultValue());
|
||||
assertEquals(Double.valueOf(2.0), descriptor.valueFrom("2.0"));
|
||||
|
||||
PropertyDescriptor<List<Double>> listDescriptor = PropertyFactory.doubleListProperty("doubleListProp")
|
||||
.desc("hello")
|
||||
.defaultValues(1.0, 2.0)
|
||||
.build();
|
||||
assertEquals("doubleListProp", listDescriptor.name());
|
||||
assertEquals("hello", listDescriptor.description());
|
||||
assertEquals(Arrays.asList(1.0, 2.0), listDescriptor.defaultValue());
|
||||
assertEquals(Arrays.asList(2.0, 3.0), listDescriptor.valueFrom("2.0,3.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoublePropertyInvalidValue() {
|
||||
PropertyDescriptor<Double> descriptor = PropertyFactory.doubleProperty("doubleProp")
|
||||
.desc("hello")
|
||||
.defaultValue(1.0)
|
||||
.build();
|
||||
thrown.expect(NumberFormatException.class);
|
||||
thrown.expectMessage("this is not a number");
|
||||
descriptor.valueFrom("this is not a number");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -186,35 +217,61 @@ public class PropertyDescriptorTest {
|
||||
.desc("hello")
|
||||
.defaultValue("default value")
|
||||
.build();
|
||||
assertEquals("stringProp", descriptor.name());
|
||||
assertEquals("hello", descriptor.description());
|
||||
assertEquals("default value", descriptor.defaultValue());
|
||||
assertEquals("foo", descriptor.valueFrom("foo"));
|
||||
|
||||
PropertyDescriptor<List<String>> listDescriptor = PropertyFactory.stringListProperty("stringListProp")
|
||||
.desc("hello")
|
||||
.defaultValues("v1", "v2")
|
||||
.build();
|
||||
assertEquals("stringListProp", listDescriptor.name());
|
||||
assertEquals("hello", listDescriptor.description());
|
||||
assertEquals(Arrays.asList("v1", "v2"), listDescriptor.defaultValue());
|
||||
assertEquals(Arrays.asList("foo", "bar"), listDescriptor.valueFrom("foo|bar"));
|
||||
}
|
||||
|
||||
private enum SampleEnum { A, B, C }
|
||||
|
||||
@Test
|
||||
public void testEnumProperty() {
|
||||
Map<String, SampleEnum> nameMap = new HashMap<>();
|
||||
private static Map<String, SampleEnum> nameMap = new LinkedHashMap<>();
|
||||
|
||||
static {
|
||||
nameMap.put("TEST_A", SampleEnum.A);
|
||||
nameMap.put("TEST_B", SampleEnum.B);
|
||||
nameMap.put("TEST_C", SampleEnum.C);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumProperty() {
|
||||
PropertyDescriptor<SampleEnum> descriptor = PropertyFactory.enumProperty("enumProp", nameMap)
|
||||
.desc("hello")
|
||||
.defaultValue(SampleEnum.B)
|
||||
.build();
|
||||
assertEquals("enumProp", descriptor.name());
|
||||
assertEquals("hello", descriptor.description());
|
||||
assertEquals(SampleEnum.B, descriptor.defaultValue());
|
||||
assertEquals(SampleEnum.C, descriptor.valueFrom("TEST_C"));
|
||||
|
||||
PropertyDescriptor<List<SampleEnum>> listDescriptor = PropertyFactory.enumListProperty("enumListProp", nameMap)
|
||||
.desc("hello")
|
||||
.defaultValues(SampleEnum.A, SampleEnum.B)
|
||||
.build();
|
||||
assertEquals("enumListProp", listDescriptor.name());
|
||||
assertEquals("hello", listDescriptor.description());
|
||||
assertEquals(Arrays.asList(SampleEnum.A, SampleEnum.B), listDescriptor.defaultValue());
|
||||
assertEquals(Arrays.asList(SampleEnum.B, SampleEnum.C), listDescriptor.valueFrom("TEST_B|TEST_C"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumPropertyInvalidValue() {
|
||||
PropertyDescriptor<SampleEnum> descriptor = PropertyFactory.enumProperty("enumProp", nameMap)
|
||||
.desc("hello")
|
||||
.defaultValue(SampleEnum.B)
|
||||
.build();
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("Value was not in the set [TEST_A, TEST_B, TEST_C]");
|
||||
descriptor.valueFrom("InvalidEnumValue");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -226,6 +283,28 @@ public class PropertyDescriptorTest {
|
||||
assertEquals("regexProp", descriptor.name());
|
||||
assertEquals("hello", descriptor.description());
|
||||
assertEquals("^[A-Z].*$", descriptor.defaultValue().toString());
|
||||
assertEquals("[0-9]+", descriptor.valueFrom("[0-9]+").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegexPropertyInvalidValue() {
|
||||
PropertyDescriptor<Pattern> descriptor = PropertyFactory.regexProperty("regexProp")
|
||||
.desc("hello")
|
||||
.defaultValue("^[A-Z].*$")
|
||||
.build();
|
||||
thrown.expect(PatternSyntaxException.class);
|
||||
thrown.expectMessage("Unclosed character class");
|
||||
descriptor.valueFrom("[open class");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegexPropertyInvalidDefaultValue() {
|
||||
thrown.expect(PatternSyntaxException.class);
|
||||
thrown.expectMessage("Unclosed character class");
|
||||
PropertyDescriptor<Pattern> descriptor = PropertyFactory.regexProperty("regexProp")
|
||||
.desc("hello")
|
||||
.defaultValue("[open class")
|
||||
.build();
|
||||
}
|
||||
|
||||
private static Matcher<String> containsIgnoreCase(final String substring) {
|
||||
|
Reference in New Issue
Block a user