fix bug in locating resource includes relative to parent classpath resource

also add various tests related to the parseables
This commit is contained in:
Havoc Pennington 2011-11-21 09:08:43 -05:00
parent 0684ffbea1
commit 2a635478dd
4 changed files with 60 additions and 3 deletions

View File

@ -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(),

View File

@ -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
}
}

View File

@ -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

View File

@ -0,0 +1,7 @@
package equiv03;
/** This is to test loading resources relative to this class */
public final class SomethingInEquiv03 {
}