diff --git a/src/main/java/org/finra/jtaf/ewd/properties/GUIHierarchyConcatenationProperties.java b/src/main/java/org/finra/jtaf/ewd/properties/GUIHierarchyConcatenationProperties.java index f333bea..717bf25 100644 --- a/src/main/java/org/finra/jtaf/ewd/properties/GUIHierarchyConcatenationProperties.java +++ b/src/main/java/org/finra/jtaf/ewd/properties/GUIHierarchyConcatenationProperties.java @@ -59,19 +59,53 @@ public GUIHierarchyConcatenationProperties(String... files) { * Concatenation of property names is done from high index to low. That is * to say, for the array {@code ["a", "b", "c"]}, the names searched will be * {@code "cba"}, {@code "cb"}, {@code "c"} in that order. - * + * * @param propertyNames * names to be concatenated and searched for + * @param parameters + * instances of the {@code String} literal "{n}" in + * the retrieved value will be replaced by {@code params[n]} * @return the first property found associated with a concatenation of the * given names * @throws Exception */ - public String getPropertyValue(String[] propertyNames) throws Exception { - return getPropertyValue(propertyNames, (String[]) null); + public String getPropertyValue(String[] propertyNames, String... parameters) throws Exception { + // Create possible combinations of property names + String value; + List possiblePropertyName = new ArrayList(); + StringBuffer fullName = new StringBuffer(); + for (int i = 0; i < propertyNames.length; i++) { + fullName.append(propertyNames[propertyNames.length - i - 1]); + possiblePropertyName.add(fullName.toString()); + } + + // Try to find the property + for (int i = 0; i < possiblePropertyName.size(); i++) { + String propertyNameCurrent = possiblePropertyName.get(possiblePropertyName.size() - i + - 1); + + for (int y = 0; y < propertyFiles.size(); y++) { + try { + GUIProperties propertyFile = propertyFiles.get(y); + if (parameters != null && parameters.length > 0) { + value = propertyFile.getPropertyValue(propertyNameCurrent, parameters); + } else { + value = propertyFile.getPropertyValue(propertyNameCurrent); + } + return value; + } catch (MissingResourceException e) { + // Ignore and continue searching + } + } + } + + throw new Exception("Can't find '" + StringUtils.join(possiblePropertyName, ",") + + "' property(ies) in '" + StringUtils.join(propertyFilesNames, ",") + + "' property file(s)"); } /** - * Searches over the group of {@code GUIProperties} for a property list + * Searches over the group of {@code GUIProperties} for a property * corresponding to a hierarchical concatenation of the given names. *

* Concatenation of property names is done from high index to low. That is @@ -80,18 +114,18 @@ public String getPropertyValue(String[] propertyNames) throws Exception { * * @param propertyNames * names to be concatenated and searched for - * @return the first property list found associated with a concatenation of - * the given names + * @return the first property found associated with a concatenation of the + * given names * @throws Exception */ - public List getPropertyValueAsList(String[] propertyNames) throws Exception { - return getPropertyValueAsList(propertyNames, (String[]) null); + public String getPropertyValue(String[] propertyNames) throws Exception { + return getPropertyValue(propertyNames, (String[]) null); } /** * Searches over the group of {@code GUIProperties} for a property * corresponding to the given name. - * + * * @param propertyName * property name to be found * @return the first property found associated with a concatenation of the @@ -106,24 +140,53 @@ public String getPropertyValue(String propertyName) throws Exception { } /** - * Searches over the group of {@code GUIProperties} for a property list - * corresponding to the given name. - * - * @param propertyName - * property name to be found - * @return the first property list found associated with a concatenation of - * the given names + * Searches over the group of {@code GUIProperties} for a property + * corresponding to the given key. + * + * @param key + * key to be found + * @param parameters + * instances of the {@code String} literal "{n}" in + * the retrieved value will be replaced by the {@code String} + * representation of {@code params[n]} + * @return the first property found associated with a concatenation of the + * given names * @throws Exception */ - public List getPropertyValueAsList(String propertyName) throws Exception { - String[] propertyNames = new String[1]; - propertyNames[0] = propertyName; - - return getPropertyValueAsList(propertyNames); + public String getPropertyValue(String key, Object[] parameters) throws Exception { + if (parameters != null && parameters.length > 0) { + String parameters2[] = new String[parameters.length]; + for (int i = 0; i < parameters.length; i++) { + Object parameter = parameters[i]; + if (parameter != null) { + parameters2[i] = String.valueOf(parameter); + } + } + return getPropertyValue(new String[] { key }, parameters2); + } else { + return getPropertyValue(key); + } } /** * Searches over the group of {@code GUIProperties} for a property + * corresponding to the given key. + * + * @param key + * key to be found + * @param parameters + * instances of the {@code String} literal "{n}" in + * the retrieved value will be replaced by {@code params[n]} + * @return the first property found associated with a concatenation of the + * given names + * @throws Exception + */ + public String getPropertyValue(String key, String... parameters) throws Exception { + return getPropertyValue(new String[] { key }, parameters); + } + + /** + * Searches over the group of {@code GUIProperties} for a property list * corresponding to a hierarchical concatenation of the given names. *

* Concatenation of property names is done from high index to low. That is @@ -132,46 +195,29 @@ public List getPropertyValueAsList(String propertyName) throws Exception * * @param propertyNames * names to be concatenated and searched for - * @param parameters - * instances of the {@code String} literal "{n}" in - * the retrieved value will be replaced by {@code params[n]} - * @return the first property found associated with a concatenation of the - * given names + * @return the first property list found associated with a concatenation of + * the given names * @throws Exception */ - public String getPropertyValue(String[] propertyNames, String... parameters) throws Exception { - // Create possible combinations of property names - String value; - List possiblePropertyName = new ArrayList(); - StringBuffer fullName = new StringBuffer(); - for (int i = 0; i < propertyNames.length; i++) { - fullName.append(propertyNames[propertyNames.length - i - 1]); - possiblePropertyName.add(fullName.toString()); - } - - // Try to find the property - for (int i = 0; i < possiblePropertyName.size(); i++) { - String propertyNameCurrent = possiblePropertyName.get(possiblePropertyName.size() - i - - 1); + public List getPropertyValueAsList(String[] propertyNames, String delimiter) throws Exception { + return getPropertyValueAsList(propertyNames,delimiter, (String[]) null); + } - for (int y = 0; y < propertyFiles.size(); y++) { - try { - GUIProperties propertyFile = propertyFiles.get(y); - if (parameters != null && parameters.length > 0) { - value = propertyFile.getPropertyValue(propertyNameCurrent, parameters); - } else { - value = propertyFile.getPropertyValue(propertyNameCurrent); - } - return value; - } catch (MissingResourceException e) { - // Ignore and continue searching - } - } - } + /** + * Searches over the group of {@code GUIProperties} for a property list + * corresponding to the given name. + * + * @param propertyName + * property name to be found + * @return the first property list found associated with a concatenation of + * the given names + * @throws Exception + */ + public List getPropertyValueAsList(String propertyName, String delimiter) throws Exception { + String[] propertyNames = new String[1]; + propertyNames[0] = propertyName; - throw new Exception("Can't find '" + StringUtils.join(possiblePropertyName, ",") - + "' property(ies) in '" + StringUtils.join(propertyFilesNames, ",") - + "' property file(s)"); + return getPropertyValueAsList(propertyNames,delimiter); } /** @@ -191,7 +237,7 @@ public String getPropertyValue(String[] propertyNames, String... parameters) thr * the given names * @throws Exception */ - public List getPropertyValueAsList(String[] propertyNames, String... parameters) + public List getPropertyValueAsList(String[] propertyNames, String delimiter, String... parameters) throws Exception { // Create possible combinations of property names List value; @@ -212,9 +258,9 @@ public List getPropertyValueAsList(String[] propertyNames, String... par GUIProperties propertyFile = propertyFiles.get(y); if (parameters != null && parameters.length > 0) { value = propertyFile - .getPropertyValueAsList(propertyNameCurrent, parameters); + .getPropertyValueAsList(propertyNameCurrent,delimiter, parameters); } else { - value = propertyFile.getPropertyValueAsList(propertyNameCurrent); + value = propertyFile.getPropertyValueAsList(propertyNameCurrent,delimiter); } return value; } catch (MissingResourceException e) { @@ -228,23 +274,6 @@ public List getPropertyValueAsList(String[] propertyNames, String... par + "' property file(s)"); } - /** - * Searches over the group of {@code GUIProperties} for a property - * corresponding to the given key. - * - * @param key - * key to be found - * @param parameters - * instances of the {@code String} literal "{n}" in - * the retrieved value will be replaced by {@code params[n]} - * @return the first property found associated with a concatenation of the - * given names - * @throws Exception - */ - public String getPropertyValue(String key, String... parameters) throws Exception { - return getPropertyValue(new String[] { key }, parameters); - } - /** * Searches over the group of {@code GUIProperties} for a property list * corresponding to the given key. @@ -258,37 +287,8 @@ public String getPropertyValue(String key, String... parameters) throws Exceptio * the given names * @throws Exception */ - public List getPropertyValueAsList(String key, String... parameters) throws Exception { - return getPropertyValueAsList(new String[] { key }, parameters); - } - - /** - * Searches over the group of {@code GUIProperties} for a property - * corresponding to the given key. - * - * @param key - * key to be found - * @param parameters - * instances of the {@code String} literal "{n}" in - * the retrieved value will be replaced by the {@code String} - * representation of {@code params[n]} - * @return the first property found associated with a concatenation of the - * given names - * @throws Exception - */ - public String getPropertyValue(String key, Object[] parameters) throws Exception { - if (parameters != null && parameters.length > 0) { - String parameters2[] = new String[parameters.length]; - for (int i = 0; i < parameters.length; i++) { - Object parameter = parameters[i]; - if (parameter != null) { - parameters2[i] = String.valueOf(parameter); - } - } - return getPropertyValue(new String[] { key }, parameters2); - } else { - return getPropertyValue(key); - } + public List getPropertyValueAsList(String key, String delimiter, String... parameters) throws Exception { + return getPropertyValueAsList(new String[] { key }, delimiter, parameters); } /** @@ -305,7 +305,7 @@ public String getPropertyValue(String key, Object[] parameters) throws Exception * the given names * @throws Exception */ - public List getPropertyValueAsList(String key, Object[] parameters) throws Exception { + public List getPropertyValueAsList(String key,String delimiter, Object[] parameters) throws Exception { if (parameters != null && parameters.length > 0) { String parameters2[] = new String[parameters.length]; for (int i = 0; i < parameters.length; i++) { @@ -314,9 +314,9 @@ public List getPropertyValueAsList(String key, Object[] parameters) thro parameters2[i] = String.valueOf(parameter); } } - return getPropertyValueAsList(new String[] { key }, parameters2); + return getPropertyValueAsList(new String[] { key },delimiter, parameters2); } else { - return getPropertyValueAsList(key); + return getPropertyValueAsList(key, delimiter); } } } \ No newline at end of file diff --git a/src/main/java/org/finra/jtaf/ewd/properties/GUIProperties.java b/src/main/java/org/finra/jtaf/ewd/properties/GUIProperties.java index 69bdb2c..634696e 100644 --- a/src/main/java/org/finra/jtaf/ewd/properties/GUIProperties.java +++ b/src/main/java/org/finra/jtaf/ewd/properties/GUIProperties.java @@ -86,6 +86,24 @@ private String getString(String key, Object params[]) throws Exception { } } + private String getStringOrDefault(String key, Object params[]) throws Exception { + String defaultValue = "Default value"; + try { + if ((params != null) && (params.length > 0)) { + defaultValue = MessageFormat.format(RESOURCE_BUNDLE.getString(key), params).trim(); + } else { + defaultValue = RESOURCE_BUNDLE.getString(key).trim(); + } + } catch (MissingResourceException e) { + throw e; + } catch (IllegalArgumentException ie) { + throw ie; + } + finally { + return defaultValue; + } + } + /** * Retrieves the property associated with the given key as a {@code String}. * @@ -101,6 +119,22 @@ public String getPropertyValue(String key, String... params) throws Exception { return getString(key, params); } + /** + * Retrieves the property associated with the given key as a {@code String}. + * + * @param key + * the key to be retrieved + * @param params + * instances of the {@code String} literal "{n}" in + * the retrieved value will be replaced by {@code params[n]} + * @return the parameterized property associated with the given key, o + * If no value is found, it return "default value" string; + * @throws Exception + */ + public String getPropertyValueOrDefault(String key, String... params) throws Exception { + return getStringOrDefault(key, params); + } + /** * Retrieves the property associated with the given key as a {@code List} of * {@code String}s. @@ -113,18 +147,14 @@ public String getPropertyValue(String key, String... params) throws Exception { * @return the parameterized property list associated with the given key * @throws Exception */ - public List getPropertyValueAsList(String key, String... params) throws Exception { + public List getPropertyValueAsList(String key, String delimiter, String... params) throws Exception { try { - String properties[] = RESOURCE_BUNDLE.getStringArray(key); - if ((params != null) && (params.length > 0)) { - List returnProperties = new ArrayList(); - for (String property : properties) { - returnProperties.add(MessageFormat.format(property, (Object[]) params).trim()); - } - return returnProperties; - } else { + String properties[] = getString(key, params).split(delimiter); + if (properties.length > 0) { return Arrays.asList(properties); } + else + return null; } catch (MissingResourceException e) { throw e; } catch (IllegalArgumentException ie) { diff --git a/src/test/java/org/finra/jtaf/ewd/properties/GUIHierarchyConcatenationPropertiesTest.java b/src/test/java/org/finra/jtaf/ewd/properties/GUIHierarchyConcatenationPropertiesTest.java new file mode 100644 index 0000000..8666fbe --- /dev/null +++ b/src/test/java/org/finra/jtaf/ewd/properties/GUIHierarchyConcatenationPropertiesTest.java @@ -0,0 +1,111 @@ +package org.finra.jtaf.ewd.properties; + +import org.junit.Assert; +import org.junit.Test; +import java.util.List; + +/** + * Created by kuoc on 7/19/2014. + */ +public class GUIHierarchyConcatenationPropertiesTest { + + @Test + // testing function String getPropertyValue(String[] propertyNames, String... parameters) + public void getPropertyValueTest() throws Exception{ + GUIHierarchyConcatenationProperties testClass = new GUIHierarchyConcatenationProperties("gui.properties","gui1.properties"); + String[] keys = {"1","y","a","r","r","a"}; + Assert.assertTrue(testClass.getPropertyValue(keys).equals("list1,{0},{1},list4")); + Assert.assertTrue(testClass.getPropertyValue(keys,"aaa","bbb").equals("list1,aaa,bbb,list4")); + } + + @Test + // testing function String getPropertyValue(String[] propertyNames) + public void getPropertyValueTestWithPropertiesArrayOnly() throws Exception{ + GUIHierarchyConcatenationProperties testClass = new GUIHierarchyConcatenationProperties("gui.properties","gui1.properties"); + String[] keys2 = {"st2","te"}; + Assert.assertTrue(testClass.getPropertyValue(keys2).equals("value2")); + + } + + @Test + // testing function String getPropertyValue(String propertyName) + public void getPropertyValueTestWithOnePropertyOnly() throws Exception{ + GUIHierarchyConcatenationProperties testClass = new GUIHierarchyConcatenationProperties("gui.properties","gui1.properties"); + Assert.assertTrue(testClass.getPropertyValue("test2").equals("value2")); + } + + @Test + // testing String getPropertyValue(String key, Object[] parameters) + public void getPropertyValueTestWithParamObjectArray() throws Exception{ + GUIHierarchyConcatenationProperties testClass = new GUIHierarchyConcatenationProperties("gui.properties","gui1.properties"); + String[] params = {"hello","world"}; + Assert.assertTrue(testClass.getPropertyValue("array1",params).equals("list1,hello,world,list4")); + + } + + @Test + // testing String getPropertyValue(String key, String... parameters) + public void getPropertyValueTestWithSingleKeyAndParamOption() throws Exception{ + GUIHierarchyConcatenationProperties testClass = new GUIHierarchyConcatenationProperties("gui.properties","gui1.properties"); + Assert.assertTrue(testClass.getPropertyValue("array1","hello","world").equals("list1,hello,world,list4")); + + } + + @Test + //testing List getPropertyValueAsList(String[] propertyNames, String delimiter, String... parameters) + public void getPropertyValueAsListTest() throws Exception{ + GUIHierarchyConcatenationProperties testClass = new GUIHierarchyConcatenationProperties("gui.properties","gui1.properties"); + String[] keys = {"1","y","a","r","r","a"}; + List temp = testClass.getPropertyValueAsList(keys,",","list2","list3"); + Assert.assertTrue(temp.get(0).equals("list1")); + Assert.assertTrue(temp.get(1).equals("list2")); + Assert.assertTrue(temp.get(2).equals("list3")); + Assert.assertTrue(temp.get(3).equals("list4")); + } + + @Test + // testing List getPropertyValueAsList(String[] propertyNames, String delimiter) + public void getPropertyValueAsListTestWithPropertiesArrayOnly() throws Exception{ + GUIHierarchyConcatenationProperties testClass = new GUIHierarchyConcatenationProperties("gui.properties","gui1.properties"); + String[] keys = {"2","y","a","r","r","a"}; + List temp = testClass.getPropertyValueAsList(keys,","); + Assert.assertTrue(temp.get(0).equals("alist1")); + Assert.assertTrue(temp.get(1).equals("0")); + Assert.assertTrue(temp.get(2).equals("1")); + Assert.assertTrue(temp.get(3).equals("alist4")); + } + + @Test + // testing List getPropertyValueAsList(String propertyName, String delimiter) + public void getPropertyValueAsListTestWithOnePropertyOnly() throws Exception{ + GUIHierarchyConcatenationProperties testClass = new GUIHierarchyConcatenationProperties("gui.properties","gui1.properties"); + List temp = testClass.getPropertyValueAsList("array2",","); + Assert.assertTrue(temp.get(0).equals("alist1")); + Assert.assertTrue(temp.get(1).equals("0")); + Assert.assertTrue(temp.get(2).equals("1")); + Assert.assertTrue(temp.get(3).equals("alist4")); + } + + @Test + // testing List getPropertyValueAsList(String key,String delimiter, Object[] parameters) + public void getPropertyValueAsListTestWithParamObjectArray() throws Exception{ + GUIHierarchyConcatenationProperties testClass = new GUIHierarchyConcatenationProperties("gui.properties","gui1.properties"); + String[] params = {"hello","world"}; + List temp = testClass.getPropertyValueAsList("array1",",",params); + Assert.assertTrue(temp.get(0).equals("list1")); + Assert.assertTrue(temp.get(1).equals("hello")); + Assert.assertTrue(temp.get(2).equals("world")); + Assert.assertTrue(temp.get(3).equals("list4")); + } + + @Test + // testing List getPropertyValueAsList(String key, String delimiter, String... parameters) + public void getPropertyValueAsListTestWithSingleKeyAndParamOption() throws Exception{ + GUIHierarchyConcatenationProperties testClass = new GUIHierarchyConcatenationProperties("gui.properties","gui1.properties"); + List temp = testClass.getPropertyValueAsList("array1",",","aaa","bb"); + Assert.assertTrue(temp.get(0).equals("list1")); + Assert.assertTrue(temp.get(1).equals("aaa")); + Assert.assertTrue(temp.get(2).equals("bb")); + Assert.assertTrue(temp.get(3).equals("list4")); + } +} diff --git a/src/test/java/org/finra/jtaf/ewd/properties/GUIPropertiesTest.java b/src/test/java/org/finra/jtaf/ewd/properties/GUIPropertiesTest.java new file mode 100644 index 0000000..f323015 --- /dev/null +++ b/src/test/java/org/finra/jtaf/ewd/properties/GUIPropertiesTest.java @@ -0,0 +1,39 @@ +package org.finra.jtaf.ewd.properties; + +import org.junit.Assert; +import org.junit.Test; +import java.util.List; + +/** + * Created by kuoc on 7/19/2014. + */ +public class GUIPropertiesTest { + + @Test + public void getPropertyValueTest() throws Exception{ + GUIProperties testClass = new GUIProperties(); + Assert.assertTrue(testClass.getPropertyValue("param2").equals("value2")); + GUIProperties testClass2 = new GUIProperties("gui1.properties"); + Assert.assertTrue(testClass2.getPropertyValue("test2").equals("value2")); + Assert.assertTrue(testClass2.getPropertyValue("testparams1","4","5","6").equals("firstparamis4andsecondparamis5andthirdparamis6")); + } + + @Test + public void getPropertyValueOrDefaultTest() throws Exception{ + GUIProperties testClass = new GUIProperties(); + Assert.assertTrue(testClass.getPropertyValueOrDefault("test default").equals("Default value")); + Assert.assertTrue(testClass.getPropertyValueOrDefault("param1").equals("value1")); + Assert.assertTrue(testClass.getPropertyValue("testparams2","4","5","6").equals("firstparamis4andsecondparamis5andthirdparamis6")); + } + + @Test + public void getPropertyValueAsListTest() throws Exception{ + GUIProperties testClass = new GUIProperties(); + List temp = testClass.getPropertyValueAsList("array1",",","list2","list3"); + Assert.assertTrue(temp.get(0).equals("list1")); + Assert.assertTrue(temp.get(1).equals("list2")); + Assert.assertTrue(temp.get(2).equals("list3")); + Assert.assertTrue(temp.get(3).equals("list4")); + } + +} diff --git a/src/test/resources/gui.properties b/src/test/resources/gui.properties new file mode 100644 index 0000000..0b8cdff --- /dev/null +++ b/src/test/resources/gui.properties @@ -0,0 +1,4 @@ +param1=value1 +param2=value2 +testparams2=firstparamis{0}andsecondparamis{1}andthirdparamis{2} +array1=list1,{0},{1},list4 \ No newline at end of file diff --git a/src/test/resources/gui1.properties b/src/test/resources/gui1.properties new file mode 100644 index 0000000..59e8a53 --- /dev/null +++ b/src/test/resources/gui1.properties @@ -0,0 +1,3 @@ +test2=value2 +testparams1=firstparamis{0}andsecondparamis{1}andthirdparamis{2} +array2=alist1,0,1,alist4 \ No newline at end of file