From 2a635478dd17773e69d31540218b130aa7e14e6c Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Mon, 21 Nov 2011 09:08:43 -0500 Subject: [PATCH] fix bug in locating resource includes relative to parent classpath resource also add various tests related to the parseables --- .../com/typesafe/config/impl/Parseable.java | 4 +- .../typesafe/config/impl/ConfParserTest.scala | 15 ++++++++ .../typesafe/config/impl/PublicApiTest.scala | 37 ++++++++++++++++++- .../scala/equiv03/SomethingInEquiv03.java | 7 ++++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 src/test/scala/equiv03/SomethingInEquiv03.java diff --git a/src/main/java/com/typesafe/config/impl/Parseable.java b/src/main/java/com/typesafe/config/impl/Parseable.java index eef9e75b..ad9712be 100644 --- a/src/main/java/com/typesafe/config/impl/Parseable.java +++ b/src/main/java/com/typesafe/config/impl/Parseable.java @@ -439,7 +439,7 @@ public abstract class Parseable implements ConfigParseable { @Override ConfigParseable relativeTo(String filename) { // not using File.isAbsolute because resource paths always use '/' - // (?) + // on all platforms if (filename.startsWith("/")) return null; @@ -450,7 +450,7 @@ public abstract class Parseable implements ConfigParseable { // search a classpath. File parent = new File(resource).getParentFile(); if (parent == null) - return newResource(klass, "/" + filename, options() + return newResource(klass, filename, options() .setOriginDescription(null)); else return newResource(klass, new File(parent, filename).getPath(), diff --git a/src/test/scala/com/typesafe/config/impl/ConfParserTest.scala b/src/test/scala/com/typesafe/config/impl/ConfParserTest.scala index 51fb8c2e..9c2ad696 100644 --- a/src/test/scala/com/typesafe/config/impl/ConfParserTest.scala +++ b/src/test/scala/com/typesafe/config/impl/ConfParserTest.scala @@ -10,6 +10,10 @@ import java.io.StringReader import com.typesafe.config._ import java.util.HashMap import scala.collection.JavaConverters._ +import java.io.File +import java.net.URL +import java.util.Properties +import java.io.ByteArrayInputStream class ConfParserTest extends TestUtils { @@ -339,4 +343,15 @@ class ConfParserTest extends TestUtils { lineNumberTest(2, "\n1e\n") lineNumberTest(3, "\n\n1e\n") } + + @Test + def toStringForParseables() { + // just be sure the toString don't throw, to get test coverage + val options = ConfigParseOptions.defaults() + Parseable.newFile(new File("foo"), options).toString + Parseable.newResource(classOf[ConfParserTest], "foo", options).toString + Parseable.newURL(new URL("file:///foo"), options).toString + Parseable.newProperties(new Properties(), options).toString + Parseable.newReader(new StringReader("{}"), options).toString + } } diff --git a/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala b/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala index 2ff06335..4408a6ae 100644 --- a/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala +++ b/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala @@ -11,6 +11,8 @@ import java.util.Collections import java.util.TreeSet import java.io.File import scala.collection.mutable +import equiv03.SomethingInEquiv03 +import java.io.StringReader class PublicApiTest extends TestUtils { @Test @@ -318,13 +320,46 @@ class PublicApiTest extends TestUtils { @Test def includersAreUsedRecursivelyWithClasspath() { - // includes.conf has recursive includes in it + // includes.conf has recursive includes in it; here we look it up + // with an "absolute" class path resource. val included = whatWasIncluded(ConfigFactory.parseResource(classOf[PublicApiTest], "/equiv03/includes.conf", _)) assertEquals(List("letters/a.conf", "numbers/1.conf", "numbers/2", "letters/b.json", "letters/c"), included.map(_.name)) } + @Test + def includersAreUsedRecursivelyWithClasspathRelativeResource() { + // includes.conf has recursive includes in it; here we look it up + // with a "class-relative" class path resource + val included = whatWasIncluded(ConfigFactory.parseResource(classOf[SomethingInEquiv03], "includes.conf", _)) + + assertEquals(List("letters/a.conf", "numbers/1.conf", "numbers/2", "letters/b.json", "letters/c"), + included.map(_.name)) + } + + @Test + def includersAreUsedRecursivelyWithURL() { + // includes.conf has recursive includes in it; here we look it up + // with a URL + val included = whatWasIncluded(ConfigFactory.parseURL(resourceFile("/equiv03/includes.conf").toURI.toURL, _)) + + assertEquals(List("letters/a.conf", "numbers/1.conf", "numbers/2", "letters/b.json", "letters/c"), + included.map(_.name)) + } + + @Test + def stringParsing() { + val conf = ConfigFactory.parseString("{ a : b }", ConfigParseOptions.defaults()) + assertEquals("b", conf.getString("a")) + } + + @Test + def readerParsing() { + val conf = ConfigFactory.parseReader(new StringReader("{ a : b }"), ConfigParseOptions.defaults()) + assertEquals("b", conf.getString("a")) + } + @Test def anySyntax() { // test01 has all three syntaxes; first load with basename diff --git a/src/test/scala/equiv03/SomethingInEquiv03.java b/src/test/scala/equiv03/SomethingInEquiv03.java new file mode 100644 index 00000000..439dd7e0 --- /dev/null +++ b/src/test/scala/equiv03/SomethingInEquiv03.java @@ -0,0 +1,7 @@ +package equiv03; + +/** This is to test loading resources relative to this class */ + +public final class SomethingInEquiv03 { + +}