From c4fbd418d181cb50bf4b45668d70cf2b58187210 Mon Sep 17 00:00:00 2001 From: S N Munendra Date: Fri, 12 Jan 2018 16:17:12 +0530 Subject: [PATCH 1/3] feat(settings): remove braces from start and end in getStrings --- .../java/ro/pippo/core/PippoSettings.java | 6 ++ .../java/ro/pippo/core/PippoSettingsTest.java | 64 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 pippo-core/src/test/java/ro/pippo/core/PippoSettingsTest.java diff --git a/pippo-core/src/main/java/ro/pippo/core/PippoSettings.java b/pippo-core/src/main/java/ro/pippo/core/PippoSettings.java index 59e7e3ee4..3ab159295 100644 --- a/pippo-core/src/main/java/ro/pippo/core/PippoSettings.java +++ b/pippo-core/src/main/java/ro/pippo/core/PippoSettings.java @@ -627,6 +627,12 @@ public List getStrings(String name, String delimiter) { if (StringUtils.isNullOrEmpty(value)) { return Collections.emptyList(); } + + value = value.trim(); + // to handles cases where value is specified like [a,b, c] + if (value.startsWith("[") && value.endsWith("]")) { + value = value.substring(1, value.length() - 1); + } return StringUtils.getList(value, delimiter); } diff --git a/pippo-core/src/test/java/ro/pippo/core/PippoSettingsTest.java b/pippo-core/src/test/java/ro/pippo/core/PippoSettingsTest.java new file mode 100644 index 000000000..e925eee11 --- /dev/null +++ b/pippo-core/src/test/java/ro/pippo/core/PippoSettingsTest.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package ro.pippo.core; + +import org.junit.Test; +import org.mockito.Mockito; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class PippoSettingsTest { + + private PippoSettings pippoSettings = Mockito.mock(PippoSettings.class); + + @Test + public void testGetStrings() { + Mockito.doReturn(" [value1, value2, value3]").when(pippoSettings) + .getString("key", null); + Mockito.doCallRealMethod().when(pippoSettings).getStrings("key", ","); + + // using default delimiter, since it is private pass same as delimiter + List values = pippoSettings.getStrings("key", ","); + assertEquals(Arrays.asList("value1","value2","value3"), values); + + // when only closing bracket specified + Mockito.doReturn(" value1/value2/value3]").when(pippoSettings) + .getString("key", null); + Mockito.doCallRealMethod().when(pippoSettings).getStrings("key", "/"); + values = pippoSettings.getStrings("key", "/"); + assertEquals(Arrays.asList("value1","value2","value3]"), values); + } + + @Test + public void testGetIntegers() { + Mockito.doReturn(" [1234, 123, value3]").when(pippoSettings) + .getString("key", null); + Mockito.doCallRealMethod().when(pippoSettings).getStrings("key", ","); + Mockito.doCallRealMethod().when(pippoSettings).getIntegers("key", ","); + + List values = pippoSettings.getIntegers("key", ","); + assertEquals(Arrays.asList(1234, 123), values); + + Mockito.doReturn(Collections.emptyList()).when(pippoSettings).getStrings("key", ","); + values = pippoSettings.getIntegers("key", ","); + assertTrue(values.isEmpty()); + } +} From 2ac18522b0fe495dc00c16a042fdaaf7b51b8c74 Mon Sep 17 00:00:00 2001 From: S N Munendra Date: Sat, 13 Jan 2018 15:56:34 +0530 Subject: [PATCH 2/3] fix(settings): remove split on value before converting to primitive --- .../java/ro/pippo/core/PippoSettings.java | 18 ++++------- .../java/ro/pippo/core/PippoSettingsTest.java | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/pippo-core/src/main/java/ro/pippo/core/PippoSettings.java b/pippo-core/src/main/java/ro/pippo/core/PippoSettings.java index 3ab159295..abf3b38d4 100644 --- a/pippo-core/src/main/java/ro/pippo/core/PippoSettings.java +++ b/pippo-core/src/main/java/ro/pippo/core/PippoSettings.java @@ -493,7 +493,7 @@ public int getInteger(String name, int defaultValue) { try { String value = getString(name, null); if (!StringUtils.isNullOrEmpty(value)) { - return Integer.parseInt(value.trim().split(" ")[0]); + return Integer.parseInt(value.trim()); } } catch (NumberFormatException e) { log.warn("Failed to parse integer for " + name + USING_DEFAULT_OF @@ -516,7 +516,7 @@ public long getLong(String name, long defaultValue) { try { String value = getString(name, null); if (!StringUtils.isNullOrEmpty(value)) { - return Long.parseLong(value.trim().split(" ")[0]); + return Long.parseLong(value.trim()); } } catch (NumberFormatException e) { log.warn("Failed to parse long for " + name + USING_DEFAULT_OF @@ -539,7 +539,7 @@ public float getFloat(String name, float defaultValue) { try { String value = getString(name, null); if (!StringUtils.isNullOrEmpty(value)) { - return Float.parseFloat(value.trim().split(" ")[0]); + return Float.parseFloat(value.trim()); } } catch (NumberFormatException e) { log.warn("Failed to parse float for " + name + USING_DEFAULT_OF @@ -562,7 +562,7 @@ public double getDouble(String name, double defaultValue) { try { String value = getString(name, null); if (!StringUtils.isNullOrEmpty(value)) { - return Double.parseDouble(value.trim().split(" ")[0]); + return Double.parseDouble(value.trim()); } } catch (NumberFormatException e) { log.warn("Failed to parse double for " + name + USING_DEFAULT_OF @@ -655,11 +655,8 @@ public List getIntegers(String name) { */ public List getIntegers(String name, String delimiter) { List strings = getStrings(name, delimiter); - if (strings.isEmpty()) { - return Collections.emptyList(); - } - List ints = new ArrayList<>(); + List ints = new ArrayList<>(strings.size()); for (String value : strings) { try { int i = Integer.parseInt(value); @@ -690,11 +687,8 @@ public List getLongs(String name) { */ public List getLongs(String name, String delimiter) { List strings = getStrings(name, delimiter); - if (strings.isEmpty()) { - return Collections.emptyList(); - } - List longs = new ArrayList<>(); + List longs = new ArrayList<>(strings.size()); for (String value : strings) { try { long i = Long.parseLong(value); diff --git a/pippo-core/src/test/java/ro/pippo/core/PippoSettingsTest.java b/pippo-core/src/test/java/ro/pippo/core/PippoSettingsTest.java index e925eee11..f6ba5deee 100644 --- a/pippo-core/src/test/java/ro/pippo/core/PippoSettingsTest.java +++ b/pippo-core/src/test/java/ro/pippo/core/PippoSettingsTest.java @@ -61,4 +61,36 @@ public void testGetIntegers() { values = pippoSettings.getIntegers("key", ","); assertTrue(values.isEmpty()); } + + @Test + public void testGetNumber() { + Mockito.doReturn(" 1234").when(pippoSettings).getString("key", null); + Mockito.doCallRealMethod().when(pippoSettings).getInteger("key", 0); + Mockito.doCallRealMethod().when(pippoSettings).getLong("key", 0); + Mockito.doCallRealMethod().when(pippoSettings).getFloat("key", 0.0f); + Mockito.doCallRealMethod().when(pippoSettings).getDouble("key", 2.4d); + + int valueInt = pippoSettings.getInteger("key", 0); + long valueLong = pippoSettings.getLong("key", 0); + float valueFloat = pippoSettings.getFloat("key", 0.0f); + double valueDouble = pippoSettings.getDouble("key", 2.4d); + + assertEquals(1234, valueInt); + assertEquals(1234L, valueLong); + assertEquals(Float.parseFloat("1234"), valueFloat, 0.0f); + assertEquals(Double.parseDouble("1234"), valueDouble, 0.0d); + + // case when number followed by some char sequence + Mockito.doReturn(" 1234 abc").when(pippoSettings).getString("key", null); + valueInt = pippoSettings.getInteger("key", 0); + valueLong = pippoSettings.getLong("key", 0); + valueFloat = pippoSettings.getFloat("key", 0.0f); + valueDouble = pippoSettings.getDouble("key", 2.4d); + + assertEquals(0, valueInt); + assertEquals(0L, valueLong); + assertEquals(0.0f, valueFloat, 0.0f); + assertEquals(2.4d, valueDouble, 0.0d); + } + } From a586b3da7ed2723b2b6687ca61b9061938094525 Mon Sep 17 00:00:00 2001 From: S N Munendra Date: Sat, 13 Jan 2018 16:11:12 +0530 Subject: [PATCH 3/3] feat(settings): add helper methods to get list of float or double --- .../java/ro/pippo/core/PippoSettings.java | 66 ++++++++++++++++++- .../java/ro/pippo/core/PippoSettingsTest.java | 16 +++++ 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/pippo-core/src/main/java/ro/pippo/core/PippoSettings.java b/pippo-core/src/main/java/ro/pippo/core/PippoSettings.java index abf3b38d4..005b1ef7d 100644 --- a/pippo-core/src/main/java/ro/pippo/core/PippoSettings.java +++ b/pippo-core/src/main/java/ro/pippo/core/PippoSettings.java @@ -640,7 +640,7 @@ public List getStrings(String name, String delimiter) { * Returns a list of comma-delimited integers from the specified name. * * @param name - * @return list of strings + * @return list of integers */ public List getIntegers(String name) { return getIntegers(name, DEFAULT_LIST_DELIMITER); @@ -672,7 +672,7 @@ public List getIntegers(String name, String delimiter) { * Returns a list of comma-delimited longs from the specified name. * * @param name - * @return list of strings + * @return list of longs */ public List getLongs(String name) { return getLongs(name, DEFAULT_LIST_DELIMITER); @@ -700,6 +700,68 @@ public List getLongs(String name, String delimiter) { return Collections.unmodifiableList(longs); } + /** + * Returns a list of comma-delimited floats from the specified name. + * + * @param name + * @return list of floats + */ + public List getFloats(String name) { + return getFloats(name, DEFAULT_LIST_DELIMITER); + } + + /** + * Returns a list of floats from the specified name using the specified delimiter. + * + * @param name + * @param delimiter + * @return list of floats + */ + public List getFloats(String name, String delimiter) { + List strings = getStrings(name, delimiter); + + List floats = new ArrayList<>(strings.size()); + for (String value : strings) { + try { + float i = Float.parseFloat(value); + floats.add(i); + } catch (NumberFormatException e) { + } + } + return Collections.unmodifiableList(floats); + } + + /** + * Returns a list of comma-delimited doubles from the specified name. + * + * @param name + * @return list of doubles + */ + public List getDoubles(String name) { + return getDoubles(name, DEFAULT_LIST_DELIMITER); + } + + /** + * Returns a list of doubles from the specified name using the specified delimiter. + * + * @param name + * @param delimiter + * @return list of doubles + */ + public List getDoubles(String name, String delimiter) { + List strings = getStrings(name, delimiter); + + List doubles = new ArrayList<>(strings.size()); + for (String value : strings) { + try { + double i = Double.parseDouble(value); + doubles.add(i); + } catch (NumberFormatException e) { + } + } + return Collections.unmodifiableList(doubles); + } + /** * Gets the duration setting and converts it to milliseconds. *

diff --git a/pippo-core/src/test/java/ro/pippo/core/PippoSettingsTest.java b/pippo-core/src/test/java/ro/pippo/core/PippoSettingsTest.java index f6ba5deee..bf0dccdaf 100644 --- a/pippo-core/src/test/java/ro/pippo/core/PippoSettingsTest.java +++ b/pippo-core/src/test/java/ro/pippo/core/PippoSettingsTest.java @@ -62,6 +62,22 @@ public void testGetIntegers() { assertTrue(values.isEmpty()); } + @Test + public void testGetDoubles() { + // tests would similar for getFloats(String, String) + Mockito.doReturn(" [1234, 123, value3]").when(pippoSettings) + .getString("key", null); + Mockito.doCallRealMethod().when(pippoSettings).getStrings("key", ","); + Mockito.doCallRealMethod().when(pippoSettings).getDoubles("key", ","); + + List values = pippoSettings.getDoubles("key", ","); + assertEquals(Arrays.asList(1234d, 123d), values); + + Mockito.doReturn(Collections.emptyList()).when(pippoSettings).getStrings("key", ","); + values = pippoSettings.getDoubles("key", ","); + assertTrue(values.isEmpty()); + } + @Test public void testGetNumber() { Mockito.doReturn(" 1234").when(pippoSettings).getString("key", null);