From 50bc3d2db0ce814f4b1056d0a30122e41814266b Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Thu, 18 Oct 2012 12:57:26 +0000
Subject: [PATCH] SP-326, BIS-217: Add method getList() to PropertyUtils. Tests
 written.

SVN: 27240
---
 .../cisd/common/properties/PropertyUtils.java | 21 +++++++++++++++
 .../common/properties/PropertyUtilsTest.java  | 26 +++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/common/source/java/ch/systemsx/cisd/common/properties/PropertyUtils.java b/common/source/java/ch/systemsx/cisd/common/properties/PropertyUtils.java
index 0e493407ae6..c14c5879c45 100644
--- a/common/source/java/ch/systemsx/cisd/common/properties/PropertyUtils.java
+++ b/common/source/java/ch/systemsx/cisd/common/properties/PropertyUtils.java
@@ -16,6 +16,7 @@
 
 package ch.systemsx.cisd.common.properties;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Enumeration;
@@ -214,6 +215,26 @@ public final class PropertyUtils
         }
         return Arrays.asList(items);
     }
+    
+    /**
+     * Returns list of comma separated values at the specific property key. Each element is trimmed.
+     * 
+     *  @return an empty list if the property is undefined or an empty string.
+     */
+    public static List<String> getList(Properties properties, String propertyKey)
+    {
+        List<String> result = new ArrayList<String>();
+        String property = PropertyUtils.getProperty(properties, propertyKey);
+        if (StringUtils.isNotBlank(property))
+        {
+            String[] splittedProperty = property.split(",");
+            for (String term : splittedProperty)
+            {
+                result.add(term.trim());
+            }
+        }
+        return result;
+    }
 
     /**
      * Looks up given <var>propertyKey</var> in given <var>properties</var>.
diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/properties/PropertyUtilsTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/properties/PropertyUtilsTest.java
index 7d407291ab9..5ee52804ed9 100644
--- a/common/sourceTest/java/ch/systemsx/cisd/common/properties/PropertyUtilsTest.java
+++ b/common/sourceTest/java/ch/systemsx/cisd/common/properties/PropertyUtilsTest.java
@@ -234,4 +234,30 @@ public final class PropertyUtilsTest extends AbstractFileSystemTestCase
         assertEquals(4, PropertyUtils.getPosLong(properties, key, 4));
         assertEquals(-7, PropertyUtils.getLong(properties, key, 4));
     }
+    
+    @Test
+    public void testGetList()
+    {
+        Properties properties = new Properties();
+        properties.setProperty("key", "  a,  gamma, Einstein");
+        
+        assertEquals("[a, gamma, Einstein]", PropertyUtils.getList(properties, "key").toString());
+    }
+    
+    @Test
+    public void testGetListForUndefinedProperty()
+    {
+        Properties properties = new Properties();
+        
+        assertEquals("[]", PropertyUtils.getList(properties, "key").toString());
+    }
+    
+    @Test
+    public void testGetListForEmptyProperty()
+    {
+        Properties properties = new Properties();
+        properties.setProperty("key", "  ");
+        
+        assertEquals("[]", PropertyUtils.getList(properties, "key").toString());
+    }
 }
\ No newline at end of file
-- 
GitLab