Hello mooli tayer,
I'd like you to do a code review. Please visit
http://gerrit.ovirt.org/24690
to review the following change.
Change subject: utils: LocalConfig: add getProperty with an optionalSuffix
String.
......................................................................
utils: LocalConfig: 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 <[email protected]>
---
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
M backend/manager/modules/utils/src/test/resources/localconfig.conf
M backend/manager/modules/utils/src/test/resources/localconfig.conf.ref
4 files changed, 53 insertions(+), 6 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/90/24690/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..ad1627a 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.IllegalArgumentException
+ * 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");
}
}
diff --git a/backend/manager/modules/utils/src/test/resources/localconfig.conf
b/backend/manager/modules/utils/src/test/resources/localconfig.conf
index 3fd4698..65590d4 100644
--- a/backend/manager/modules/utils/src/test/resources/localconfig.conf
+++ b/backend/manager/modules/utils/src/test/resources/localconfig.conf
@@ -7,7 +7,9 @@
# blank line with spaces and comment:
# comment
+key00=value0
key01=
+key01_suffixed="suffixed val"
key02=value2
key03=value31 value32
key04="value41 value42"
diff --git
a/backend/manager/modules/utils/src/test/resources/localconfig.conf.ref
b/backend/manager/modules/utils/src/test/resources/localconfig.conf.ref
index b2d5bf7..7a9b40e 100644
--- a/backend/manager/modules/utils/src/test/resources/localconfig.conf.ref
+++ b/backend/manager/modules/utils/src/test/resources/localconfig.conf.ref
@@ -1,4 +1,6 @@
+key00=value0
key01=
+key01_suffixed=suffixed val
key02=value2
key03=value31
key04=value41 value42
--
To view, visit http://gerrit.ovirt.org/24690
To unsubscribe, visit http://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic37b6aa5ce44600fc647915b9f71632c98bbbae3
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: ovirt-engine-3.4
Gerrit-Owner: Alon Bar-Lev <[email protected]>
Gerrit-Reviewer: mooli tayer <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches