Skip to content

Commit 1632ce0

Browse files
authored
Allow some more File tests to run on Native (#408)
* Added PublicApiFileTest for JVM/Native * Add PublicApiFileTest
1 parent ffafcc0 commit 1632ce0

File tree

6 files changed

+218
-35
lines changed

6 files changed

+218
-35
lines changed

sconfig/jvm-native/src/main/scala/org/ekrich/config/ConfigFactoryJvmNative.scala

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@ package org.ekrich.config
33
/**
44
* [[ConfigFactory]] methods common to JVM and Native
55
*/
6-
abstract class ConfigFactoryJvmNative extends ConfigFactoryShared {}
6+
abstract class ConfigFactoryJvmNative extends ConfigFactoryShared {
7+
// parseFile and parseFileAnySyntax should be here but they
8+
// use shared so then it is very tangled so any refactor is big
9+
// TODO: first create a PublicApiFileTest for JVM native to test
10+
// this API on Native.
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* Copyright (C) 2011 Typesafe Inc. <http://typesafe.com>
3+
*/
4+
package org.ekrich.config.impl
5+
6+
import org.junit.Assert._
7+
import org.junit._
8+
9+
import org.ekrich.config._
10+
11+
import FileUtils._
12+
13+
/**
14+
* Most of the File tests should work but internally and externally they use URL
15+
* and Native doesn't support URL. Once the API use URI internally more test
16+
* support should be available.
17+
*/
18+
class PublicApiFileTest extends TestUtils {
19+
20+
// dupe in PublicApiTest
21+
private def assertNotFound(e: ConfigException): Unit = {
22+
assertTrue(
23+
"Message text: " + e.getMessage,
24+
e.getMessage.contains("No such") ||
25+
e.getMessage.contains("not found") ||
26+
e.getMessage.contains("were found") ||
27+
e.getMessage.contains("java.io.FileNotFoundException")
28+
)
29+
}
30+
31+
@Test
32+
def allowMissing(): Unit = {
33+
val e = intercept[ConfigException.IO] {
34+
ConfigFactory.parseFile(
35+
resourceFile("nonexistent.conf"),
36+
ConfigParseOptions.defaults.setAllowMissing(false)
37+
)
38+
}
39+
assertNotFound(e)
40+
41+
val conf = ConfigFactory.parseFile(
42+
resourceFile("nonexistent.conf"),
43+
ConfigParseOptions.defaults.setAllowMissing(true)
44+
)
45+
assertTrue("is empty", conf.isEmpty)
46+
}
47+
48+
@Test
49+
def allowMissingFileAnySyntax(): Unit = {
50+
val e = intercept[ConfigException.IO] {
51+
ConfigFactory.parseFileAnySyntax(
52+
resourceFile("nonexistent"),
53+
ConfigParseOptions.defaults.setAllowMissing(false)
54+
)
55+
}
56+
assertNotFound(e)
57+
58+
val conf = ConfigFactory.parseFileAnySyntax(
59+
resourceFile("nonexistent"),
60+
ConfigParseOptions.defaults.setAllowMissing(true)
61+
)
62+
assertTrue("is empty", conf.isEmpty)
63+
}
64+
65+
@Test
66+
def anySyntaxJvmNative(): Unit = {
67+
// Kept in JVM as anySyntax() this is only a partial test
68+
// as resource loading not supported yet in Native
69+
70+
// test01 has all three syntaxes; first load with basename
71+
val conf = ConfigFactory.parseFileAnySyntax(
72+
resourceFile("test01"),
73+
ConfigParseOptions.defaults
74+
)
75+
assertEquals(42, conf.getInt("ints.fortyTwo"))
76+
assertEquals("A", conf.getString("fromJsonA"))
77+
assertEquals("true", conf.getString("fromProps.bool"))
78+
79+
// now include a suffix, should only load one of them
80+
val onlyProps = ConfigFactory.parseFileAnySyntax(
81+
resourceFile("test01.properties"),
82+
ConfigParseOptions.defaults
83+
)
84+
assertFalse(onlyProps.hasPath("ints.fortyTwo"))
85+
assertFalse(onlyProps.hasPath("fromJsonA"))
86+
assertEquals("true", onlyProps.getString("fromProps.bool"))
87+
88+
// force only one syntax via options
89+
val onlyPropsViaOptions = ConfigFactory.parseFileAnySyntax(
90+
resourceFile("test01.properties"),
91+
ConfigParseOptions.defaults.setSyntax(ConfigSyntax.PROPERTIES)
92+
)
93+
assertFalse(onlyPropsViaOptions.hasPath("ints.fortyTwo"))
94+
assertFalse(onlyPropsViaOptions.hasPath("fromJsonA"))
95+
assertEquals("true", onlyPropsViaOptions.getString("fromProps.bool"))
96+
97+
// TODO: continue test when resourse work on native
98+
// val fromResources = ConfigFactory.parseResourcesAnySyntax(
99+
// classOf[PublicApiFileTest],
100+
// "/test01",
101+
// ConfigParseOptions.defaults
102+
// )
103+
// assertEquals(42, fromResources.getInt("ints.fortyTwo"))
104+
// assertEquals("A", fromResources.getString("fromJsonA"))
105+
// assertEquals("true", fromResources.getString("fromProps.bool"))
106+
}
107+
108+
}

sconfig/jvm/src/test/scala/org/ekrich/config/impl/PublicApiTest.scala

-34
Original file line numberDiff line numberDiff line change
@@ -401,40 +401,6 @@ class PublicApiTest extends TestUtils {
401401
)
402402
}
403403

404-
@Test
405-
def allowMissing(): Unit = {
406-
val e = intercept[ConfigException.IO] {
407-
ConfigFactory.parseFile(
408-
resourceFile("nonexistent.conf"),
409-
ConfigParseOptions.defaults.setAllowMissing(false)
410-
)
411-
}
412-
assertNotFound(e)
413-
414-
val conf = ConfigFactory.parseFile(
415-
resourceFile("nonexistent.conf"),
416-
ConfigParseOptions.defaults.setAllowMissing(true)
417-
)
418-
assertTrue("is empty", conf.isEmpty)
419-
}
420-
421-
@Test
422-
def allowMissingFileAnySyntax(): Unit = {
423-
val e = intercept[ConfigException.IO] {
424-
ConfigFactory.parseFileAnySyntax(
425-
resourceFile("nonexistent"),
426-
ConfigParseOptions.defaults.setAllowMissing(false)
427-
)
428-
}
429-
assertNotFound(e)
430-
431-
val conf = ConfigFactory.parseFileAnySyntax(
432-
resourceFile("nonexistent"),
433-
ConfigParseOptions.defaults.setAllowMissing(true)
434-
)
435-
assertTrue("is empty", conf.isEmpty)
436-
}
437-
438404
@Test
439405
def allowMissingResourcesAnySyntax(): Unit = {
440406
val e = intercept[ConfigException.IO] {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{
2+
"ints" : {
3+
"fortyTwo" : 42,
4+
"fortyTwoAgain" : ${ints.fortyTwo}
5+
},
6+
7+
"floats" : {
8+
"fortyTwoPointOne" : 42.1,
9+
"fortyTwoPointOneAgain" : ${floats.fortyTwoPointOne},
10+
"pointThirtyThree": .33
11+
"pointThirtyThreeAgain": ${floats.pointThirtyThree}
12+
},
13+
14+
"strings" : {
15+
"abcd" : "abcd",
16+
"abcdAgain" : ${strings.a}${strings.b}${strings.c}${strings.d},
17+
"a" : "a",
18+
"b" : "b",
19+
"c" : "c",
20+
"d" : "d",
21+
"concatenated" : null bar 42 baz true 3.14 hi,
22+
"double" : "3.14",
23+
"doubleStartingWithDot": ".33",
24+
"number" : "57",
25+
"null" : "null",
26+
"true" : "true",
27+
"yes" : "yes",
28+
"false" : "false",
29+
"no" : "no"
30+
},
31+
32+
"arrays" : {
33+
"empty" : [],
34+
"ofInt" : [1, 2, 3],
35+
"ofString" : [ ${strings.a}, ${strings.b}, ${strings.c} ],
36+
"ofDouble" : [3.14, 4.14, 5.14],
37+
"ofNull" : [null, null, null],
38+
"ofBoolean" : [true, false],
39+
"ofArray" : [${arrays.ofString}, ${arrays.ofString}, ${arrays.ofString}],
40+
"ofObject" : [${ints}, ${booleans}, ${strings}],
41+
"firstElementNotASubst" : [ "a", ${strings.b} ]
42+
},
43+
44+
"booleans" : {
45+
"true" : true,
46+
"trueAgain" : ${booleans.true},
47+
"false" : false,
48+
"falseAgain" : ${booleans.false}
49+
},
50+
51+
"nulls" : {
52+
"null" : null,
53+
"nullAgain" : ${nulls.null}
54+
},
55+
56+
"durations" : {
57+
"second" : 1s,
58+
"secondsList" : [1s,2seconds,3 s, 4000],
59+
"secondAsNumber" : 1000,
60+
"halfSecond" : 0.5s,
61+
"millis" : 1 milli,
62+
"micros" : 2000 micros,
63+
"largeNanos" : 4878955355435272204ns,
64+
"plusLargeNanos" : "+4878955355435272204ns",
65+
"minusLargeNanos" : -4878955355435272204ns
66+
},
67+
68+
"periods" : {
69+
"day" : 1d,
70+
"dayAsNumber": 2,
71+
"week": 3 weeks,
72+
"month": 5 mo,
73+
"year": 8y
74+
},
75+
76+
"memsizes" : {
77+
"meg" : 1M,
78+
"megsList" : [1M, 1024K, 1048576],
79+
"megAsNumber" : 1048576,
80+
"halfMeg" : 0.5M
81+
},
82+
83+
"system" : {
84+
"javaversion" : ${?java.version},
85+
"userhome" : ${?user.home},
86+
"home" : ${?HOME},
87+
"pwd" : ${?PWD},
88+
"shell" : ${?SHELL},
89+
"lang" : ${?LANG},
90+
"path" : ${?PATH},
91+
"not_here" : ${?NOT_HERE},
92+
"concatenated" : Your Java version is ${?system.javaversion} and your user.home is ${?system.userhome}
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"fromJson1" : 1,
3+
"fromJsonA" : "A"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# .properties file
2+
fromProps.abc=abc
3+
fromProps.one=1
4+
fromProps.bool=true
5+
fromProps.specialChars=hello^^

0 commit comments

Comments
 (0)