public static int parse(final Properties properties, final String propertyKey, final int defaultValue) { return Integer.valueOf(parse(properties, propertyKey, Integer.toString(defaultValue))); }
public static boolean parse(final Properties properties, final String propertyKey, final boolean defaultValue) { return Boolean.valueOf(parse(properties, propertyKey, Boolean.toString(defaultValue))); }
public static long parse(final Properties properties, final String propertyKey, final long defaultValue) { return Long.valueOf(parse(properties, propertyKey, Long.toString(defaultValue))); }
public static <T extends Enum<T>> T parse(final Properties properties, final String propertyKey, final T defaultValue, final Class<T> clazz) { return Enum.valueOf(clazz, parse(properties, propertyKey, defaultValue.toString()).toUpperCase()); }
@Test public void test_parse_whenPropertiesDoesNotExists() throws Exception { final Properties properties = new Properties(); assertThat(PropertiesUtil.parse(properties, PROPERTY_KEY_HOST, "otherHost"), is("otherHost")); assertThat(PropertiesUtil.parse(properties, PROPERTY_KEY_POD, 2), is(2)); assertThat(PropertiesUtil.parse(properties, PROPERTY_KEY_DEVELOPER, false), is(false)); }
@Test public void test_parse_whenPropertiesExists() throws Exception { final Properties properties = new Properties(); properties.setProperty(PROPERTY_KEY_HOST, MY_HOST); properties.setProperty(PROPERTY_KEY_POD, Integer.toString(MY_PORT)); properties.setProperty(PROPERTY_KEY_DEVELOPER, Boolean.toString(true)); assertThat(PropertiesUtil.parse(properties, PROPERTY_KEY_HOST, "otherHost"), is(MY_HOST)); assertThat(PropertiesUtil.parse(properties, PROPERTY_KEY_POD, 2), is(MY_PORT)); assertThat(PropertiesUtil.parse(properties, PROPERTY_KEY_DEVELOPER, false), is(true)); }
@Test public void test_parse_whenStreamIsEmpty() throws Exception { final Properties properties = PropertiesUtil.parse(Stream.empty()); assertThat(properties.entrySet(), empty()); }
@Test public void test_parse_whenPropertyIsPresentAndIsAList() throws Exception { final Properties properties = new Properties(); properties.setProperty("key", "element1,element2,element3"); final List<String> defaultValue = Collections.emptyList(); final List<String> list = PropertiesUtil.parse(properties, "key", defaultValue, stringSplitDecomposer(",", String.class)); assertThat(list, containsInAnyOrder("element1", "element2", "element3")); }
@Test public void test_parse_whenPropertyIsPresentAndIsAListAndIsEmpty() throws Exception { final Properties properties = new Properties(); properties.setProperty("key", ""); final List<String> defaultValue = Arrays.asList(new String[]{"element1", "element2", "element3"}); assertThat(PropertiesUtil.parse(properties, "key", defaultValue, stringSplitDecomposer(",", String.class)), empty()); }
@Test public void test_parse_whenPropertyIsNotPresentAndIsAList() throws Exception { final Properties properties = new Properties(); properties.setProperty("key", "element1,element2,element3"); final List<String> defaultValue = Collections.emptyList(); assertThat(PropertiesUtil.parse(properties, "key1", defaultValue, stringSplitDecomposer(",", String.class)), empty()); }
@Test public void test_parse_whenValueIsOfCustomType() throws Exception { final Properties properties = new Properties(); properties.setProperty("key", MyCustomType.VALUE1.toString()); assertThat(PropertiesUtil.parse(properties, "key", MyCustomType.VALUE2, MyCustomType.class), is(MyCustomType.VALUE1)); }
@Test public void test_parse_whenStreamIsNotEmpty() throws Exception { final Stream<String> stream = Arrays.stream(new String[]{"key1=value1", "key2=value2", "key1=value3"}); final Properties properties = PropertiesUtil.parse(stream); assertThat(properties, allOf(hasEntry("key1", "value3"), hasEntry("key2", "value2"))); }