Address PR feedback

Reduce cast noise in the `hasValue` method in the
ConfigNodeObject class. Cleanup exception testing in
ConfigDocument array tests.
This commit is contained in:
Preben Ingvaldsen 2015-03-30 13:09:05 -07:00
parent d6c9f632ab
commit e959e2c8a9
2 changed files with 12 additions and 33 deletions

View File

@ -13,13 +13,15 @@ final class ConfigNodeObject extends ConfigNodeComplexValue {
public boolean hasValue(Path desiredPath) {
for (AbstractConfigNode node : children) {
if (node instanceof ConfigNodeField) {
Path key = ((ConfigNodeField) node).path().value();
ConfigNodeField field = (ConfigNodeField) node;
Path key = field.path().value();
if (key.equals(desiredPath) || key.startsWith(desiredPath)) {
return true;
} else if (desiredPath.startsWith(key)) {
if (((ConfigNodeField) node).value() instanceof ConfigNodeObject) {
if (field.value() instanceof ConfigNodeObject) {
ConfigNodeObject obj = (ConfigNodeObject) field.value();
Path remainingPath = desiredPath.subPath(key.length());
if (((ConfigNodeObject) ((ConfigNodeField) node).value()).hasValue(remainingPath)) {
if (obj.hasValue(remainingPath)) {
return true;
}
}

View File

@ -231,38 +231,15 @@ class ConfigDocumentTest extends TestUtils {
// Attempting a replace on a ConfigDocument parsed from an array throws an error
val origText = "[1, 2, 3, 4, 5]"
val document = ConfigDocumentFactory.parseString(origText)
var exceptionThrown = false
try {
document.setValue("a", "1")
} catch {
case e: Exception =>
exceptionThrown = true
assertTrue(e.isInstanceOf[ConfigException])
assertTrue(e.getMessage.contains("ConfigDocument had an array at the root level"))
}
assertTrue(exceptionThrown)
exceptionThrown = false;
try {
document.hasValue("a")
} catch {
case e: Exception =>
exceptionThrown = true
assertTrue(e.isInstanceOf[ConfigException])
assertTrue(e.getMessage.contains("ConfigDocument had an array at the root level"))
}
assertTrue(exceptionThrown)
val e1 = intercept[ConfigException] { document.setValue("a", "1") }
assertTrue(e1.getMessage.contains("ConfigDocument had an array at the root level"))
exceptionThrown = false
try {
document.removeValue("a")
} catch {
case e: Exception =>
exceptionThrown = true
assertTrue(e.isInstanceOf[ConfigException])
assertTrue(e.getMessage.contains("ConfigDocument had an array at the root level"))
}
assertTrue(exceptionThrown)
val e2 = intercept[ConfigException] { document.hasValue("a") }
assertTrue(e2.getMessage.contains("ConfigDocument had an array at the root level"))
val e3 = intercept[ConfigException] { document.removeValue("a") }
assertTrue(e3.getMessage.contains("ConfigDocument had an array at the root level"))
}
@Test