Fix bug with ConfigDocument.setValue

Fix a bug in the setValue method in SimpleConfigDocument wherein
a rendered ConfigValue could have leading or trailing whitespace,
leading to an issue with setting a value on a ConfigDocument.
This commit is contained in:
Preben Ingvaldsen 2015-04-09 16:34:08 -07:00
parent 924ee2e4e4
commit 087a00c7ab
2 changed files with 15 additions and 2 deletions

View File

@ -27,7 +27,7 @@ final class SimpleConfigDocument implements ConfigDocument {
}
public ConfigDocument setValue(String path, ConfigValue newValue) {
return setValue(path, newValue.render());
return setValue(path, newValue.render().trim());
}
public ConfigDocument removeValue(String path) {

View File

@ -1,7 +1,6 @@
package com.typesafe.config.impl
import java.io.{ BufferedReader, FileReader }
import java.nio.charset.StandardCharsets
import java.nio.file.{ Paths, Files }
import com.typesafe.config._
@ -9,6 +8,8 @@ import com.typesafe.config.parser._
import org.junit.Assert._
import org.junit.Test
import scala.collection.JavaConverters._
class ConfigDocumentTest extends TestUtils {
private def configDocumentReplaceJsonTest(origText: String, finalText: String, newValue: String, replacePath: String) {
val configDocument = ConfigDocumentFactory.parseString(origText, ConfigParseOptions.defaults().setSyntax(ConfigSyntax.JSON))
@ -437,4 +438,16 @@ class ConfigDocumentTest extends TestUtils {
assertEquals(" a : 1", configDocument.setValue("a", "1").render)
}
@Test
def configDocumentConfigObjectInsertion {
val origText = "{ a : b }"
val configDocument = ConfigDocumentFactory.parseString(origText)
val configVal = ConfigValueFactory.fromAnyRef(Map("a" -> 1, "b" -> 2).asJava)
assertEquals("{ a : {\n # hardcoded value\n \"a\" : 1,\n # hardcoded value\n \"b\" : 2\n } }",
configDocument.setValue("a", configVal).render)
}
}