mirror of
https://github.com/lightbend/config.git
synced 2025-03-29 21:51:10 +08:00
fix bug in locating resource includes relative to parent classpath resource
also add various tests related to the parseables
This commit is contained in:
parent
0684ffbea1
commit
2a635478dd
@ -439,7 +439,7 @@ public abstract class Parseable implements ConfigParseable {
|
|||||||
@Override
|
@Override
|
||||||
ConfigParseable relativeTo(String filename) {
|
ConfigParseable relativeTo(String filename) {
|
||||||
// not using File.isAbsolute because resource paths always use '/'
|
// not using File.isAbsolute because resource paths always use '/'
|
||||||
// (?)
|
// on all platforms
|
||||||
if (filename.startsWith("/"))
|
if (filename.startsWith("/"))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
@ -450,7 +450,7 @@ public abstract class Parseable implements ConfigParseable {
|
|||||||
// search a classpath.
|
// search a classpath.
|
||||||
File parent = new File(resource).getParentFile();
|
File parent = new File(resource).getParentFile();
|
||||||
if (parent == null)
|
if (parent == null)
|
||||||
return newResource(klass, "/" + filename, options()
|
return newResource(klass, filename, options()
|
||||||
.setOriginDescription(null));
|
.setOriginDescription(null));
|
||||||
else
|
else
|
||||||
return newResource(klass, new File(parent, filename).getPath(),
|
return newResource(klass, new File(parent, filename).getPath(),
|
||||||
|
@ -10,6 +10,10 @@ import java.io.StringReader
|
|||||||
import com.typesafe.config._
|
import com.typesafe.config._
|
||||||
import java.util.HashMap
|
import java.util.HashMap
|
||||||
import scala.collection.JavaConverters._
|
import scala.collection.JavaConverters._
|
||||||
|
import java.io.File
|
||||||
|
import java.net.URL
|
||||||
|
import java.util.Properties
|
||||||
|
import java.io.ByteArrayInputStream
|
||||||
|
|
||||||
class ConfParserTest extends TestUtils {
|
class ConfParserTest extends TestUtils {
|
||||||
|
|
||||||
@ -339,4 +343,15 @@ class ConfParserTest extends TestUtils {
|
|||||||
lineNumberTest(2, "\n1e\n")
|
lineNumberTest(2, "\n1e\n")
|
||||||
lineNumberTest(3, "\n\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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,8 @@ import java.util.Collections
|
|||||||
import java.util.TreeSet
|
import java.util.TreeSet
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import scala.collection.mutable
|
import scala.collection.mutable
|
||||||
|
import equiv03.SomethingInEquiv03
|
||||||
|
import java.io.StringReader
|
||||||
|
|
||||||
class PublicApiTest extends TestUtils {
|
class PublicApiTest extends TestUtils {
|
||||||
@Test
|
@Test
|
||||||
@ -318,13 +320,46 @@ class PublicApiTest extends TestUtils {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
def includersAreUsedRecursivelyWithClasspath() {
|
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", _))
|
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"),
|
assertEquals(List("letters/a.conf", "numbers/1.conf", "numbers/2", "letters/b.json", "letters/c"),
|
||||||
included.map(_.name))
|
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
|
@Test
|
||||||
def anySyntax() {
|
def anySyntax() {
|
||||||
// test01 has all three syntaxes; first load with basename
|
// test01 has all three syntaxes; first load with basename
|
||||||
|
7
src/test/scala/equiv03/SomethingInEquiv03.java
Normal file
7
src/test/scala/equiv03/SomethingInEquiv03.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package equiv03;
|
||||||
|
|
||||||
|
/** This is to test loading resources relative to this class */
|
||||||
|
|
||||||
|
public final class SomethingInEquiv03 {
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user