mooli tayer has uploaded a new change for review.

Change subject: utils: add getProperty with an optionalSuffix String.
......................................................................

utils: add getProperty with an optionalSuffix String.

public String getProperty(String key, String optionalSuffix, boolean 
allowMissing);
Returns the value associated with key_optionalSuffix if it is defined or the one
associated with key. If both are missing and allowMissing = false throws an
IllegalStateException.

Change-Id: Ic37b6aa5ce44600fc647915b9f71632c98bbbae3
Signed-off-by: Mooli Tayer <mta...@redhat.com>
---
M 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java
M 
backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/LocalConfigTest.java
2 files changed, 49 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/79/23979/1

diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java
index a6fd162..054a3fc 100644
--- 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java
@@ -269,6 +269,25 @@
     }
 
     /**
+     * Get the value of a property given its name and an optional underscore 
separated suffix.
+     * if key_optionalSuffix has a value return it. otherwise return the value 
of key.
+     *
+     * @param key            the name of the property
+     * @param optionalSuffix the suffix of the property, not including an 
underscore.
+     * @param allowMissing   define the behaviour if both key and 
key_optionalSuffix are not associated with a value
+     * @return the value associated with key_optionalSuffix if it is defined 
or the one associated with key otherwise.
+     * @throws java.lang.IllegalStateException if both key_optionalSuffix and 
key are not associated with a value and
+     *      allowMissing is false.
+     */
+    public String getProperty(String key, String optionalSuffix, boolean 
allowMissing) {
+        String property = getProperty(key + "_" + optionalSuffix, true);
+        if (StringUtils.isEmpty(property)) {
+            property = getProperty(key, allowMissing);
+        }
+        return property;
+    }
+
+    /**
      * Get the value of a property given its name.
      *
      * @param key the name of the property
diff --git 
a/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/LocalConfigTest.java
 
b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/LocalConfigTest.java
index 5268bc6..480b89f 100644
--- 
a/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/LocalConfigTest.java
+++ 
b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/LocalConfigTest.java
@@ -1,5 +1,6 @@
 package org.ovirt.engine.core.utils;
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 
 import java.io.FileInputStream;
@@ -12,17 +13,26 @@
 import java.util.List;
 import java.util.Map;
 
+import org.junit.BeforeClass;
 import org.junit.Test;
 
 public class LocalConfigTest {
 
+    private static LocalConfig config;
+
+    @BeforeClass
+    static public void beforeClass() throws Exception {
+        config = new LocalConfig();
+
+        config.loadConfig(
+                
URLDecoder.decode(ClassLoader.getSystemResource("localconfig.conf").getPath(), 
"UTF-8"),
+                "/dev/null"
+        );
+    }
+
     @Test
     public void testValid() throws Exception {
-        LocalConfig config = new LocalConfig();
-        config.loadConfig(
-            
URLDecoder.decode(ClassLoader.getSystemResource("localconfig.conf").getPath(), 
"UTF-8"),
-            "/dev/null"
-        );
+
         List<String> res = new LinkedList<String>();
         for (Map.Entry<String, String> e : config.getProperties().entrySet()) {
             res.add(String.format("%s=%s", e.getKey(), e.getValue()));
@@ -43,7 +53,21 @@
             }
             catch (IOException e) {}
         }
+        for (Object o:res.toArray()){
+            System.out.println(o);
+        }
+        for (Object o:reference.split("\n")){
+            System.out.println(o);
+        }
 
-        assertEquals(reference.split("\n"), res.toArray());
+        assertArrayEquals(reference.split("\n"), res.toArray());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testGetSuffixedProperty() throws Exception {
+
+        assertEquals(config.getProperty("key00", "non_existent", false), 
"value0");
+        assertEquals(config.getProperty("key01", "suffixed", false), "suffixed 
val");
+        assertEquals(config.getProperty("non_existent", "non_existent", 
false), "throws exception");
     }
 }


-- 
To view, visit http://gerrit.ovirt.org/23979
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic37b6aa5ce44600fc647915b9f71632c98bbbae3
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: mooli tayer <mta...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to