Skip to content

Commit

Permalink
Allow some more File tests to run on Native (#408)
Browse files Browse the repository at this point in the history
* Added PublicApiFileTest for JVM/Native

* Add PublicApiFileTest
  • Loading branch information
ekrich authored Nov 11, 2024
1 parent ffafcc0 commit 1632ce0
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ package org.ekrich.config
/**
* [[ConfigFactory]] methods common to JVM and Native
*/
abstract class ConfigFactoryJvmNative extends ConfigFactoryShared {}
abstract class ConfigFactoryJvmNative extends ConfigFactoryShared {
// parseFile and parseFileAnySyntax should be here but they
// use shared so then it is very tangled so any refactor is big
// TODO: first create a PublicApiFileTest for JVM native to test
// this API on Native.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Copyright (C) 2011 Typesafe Inc. <http://typesafe.com>
*/
package org.ekrich.config.impl

import org.junit.Assert._
import org.junit._

import org.ekrich.config._

import FileUtils._

/**
* Most of the File tests should work but internally and externally they use URL
* and Native doesn't support URL. Once the API use URI internally more test
* support should be available.
*/
class PublicApiFileTest extends TestUtils {

// dupe in PublicApiTest
private def assertNotFound(e: ConfigException): Unit = {
assertTrue(
"Message text: " + e.getMessage,
e.getMessage.contains("No such") ||
e.getMessage.contains("not found") ||
e.getMessage.contains("were found") ||
e.getMessage.contains("java.io.FileNotFoundException")
)
}

@Test
def allowMissing(): Unit = {
val e = intercept[ConfigException.IO] {
ConfigFactory.parseFile(
resourceFile("nonexistent.conf"),
ConfigParseOptions.defaults.setAllowMissing(false)
)
}
assertNotFound(e)

val conf = ConfigFactory.parseFile(
resourceFile("nonexistent.conf"),
ConfigParseOptions.defaults.setAllowMissing(true)
)
assertTrue("is empty", conf.isEmpty)
}

@Test
def allowMissingFileAnySyntax(): Unit = {
val e = intercept[ConfigException.IO] {
ConfigFactory.parseFileAnySyntax(
resourceFile("nonexistent"),
ConfigParseOptions.defaults.setAllowMissing(false)
)
}
assertNotFound(e)

val conf = ConfigFactory.parseFileAnySyntax(
resourceFile("nonexistent"),
ConfigParseOptions.defaults.setAllowMissing(true)
)
assertTrue("is empty", conf.isEmpty)
}

@Test
def anySyntaxJvmNative(): Unit = {
// Kept in JVM as anySyntax() this is only a partial test
// as resource loading not supported yet in Native

// test01 has all three syntaxes; first load with basename
val conf = ConfigFactory.parseFileAnySyntax(
resourceFile("test01"),
ConfigParseOptions.defaults
)
assertEquals(42, conf.getInt("ints.fortyTwo"))
assertEquals("A", conf.getString("fromJsonA"))
assertEquals("true", conf.getString("fromProps.bool"))

// now include a suffix, should only load one of them
val onlyProps = ConfigFactory.parseFileAnySyntax(
resourceFile("test01.properties"),
ConfigParseOptions.defaults
)
assertFalse(onlyProps.hasPath("ints.fortyTwo"))
assertFalse(onlyProps.hasPath("fromJsonA"))
assertEquals("true", onlyProps.getString("fromProps.bool"))

// force only one syntax via options
val onlyPropsViaOptions = ConfigFactory.parseFileAnySyntax(
resourceFile("test01.properties"),
ConfigParseOptions.defaults.setSyntax(ConfigSyntax.PROPERTIES)
)
assertFalse(onlyPropsViaOptions.hasPath("ints.fortyTwo"))
assertFalse(onlyPropsViaOptions.hasPath("fromJsonA"))
assertEquals("true", onlyPropsViaOptions.getString("fromProps.bool"))

// TODO: continue test when resourse work on native
// val fromResources = ConfigFactory.parseResourcesAnySyntax(
// classOf[PublicApiFileTest],
// "/test01",
// ConfigParseOptions.defaults
// )
// assertEquals(42, fromResources.getInt("ints.fortyTwo"))
// assertEquals("A", fromResources.getString("fromJsonA"))
// assertEquals("true", fromResources.getString("fromProps.bool"))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -401,40 +401,6 @@ class PublicApiTest extends TestUtils {
)
}

@Test
def allowMissing(): Unit = {
val e = intercept[ConfigException.IO] {
ConfigFactory.parseFile(
resourceFile("nonexistent.conf"),
ConfigParseOptions.defaults.setAllowMissing(false)
)
}
assertNotFound(e)

val conf = ConfigFactory.parseFile(
resourceFile("nonexistent.conf"),
ConfigParseOptions.defaults.setAllowMissing(true)
)
assertTrue("is empty", conf.isEmpty)
}

@Test
def allowMissingFileAnySyntax(): Unit = {
val e = intercept[ConfigException.IO] {
ConfigFactory.parseFileAnySyntax(
resourceFile("nonexistent"),
ConfigParseOptions.defaults.setAllowMissing(false)
)
}
assertNotFound(e)

val conf = ConfigFactory.parseFileAnySyntax(
resourceFile("nonexistent"),
ConfigParseOptions.defaults.setAllowMissing(true)
)
assertTrue("is empty", conf.isEmpty)
}

@Test
def allowMissingResourcesAnySyntax(): Unit = {
val e = intercept[ConfigException.IO] {
Expand Down
94 changes: 94 additions & 0 deletions sconfig/native/src/test/resources/test01.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"ints" : {
"fortyTwo" : 42,
"fortyTwoAgain" : ${ints.fortyTwo}
},

"floats" : {
"fortyTwoPointOne" : 42.1,
"fortyTwoPointOneAgain" : ${floats.fortyTwoPointOne},
"pointThirtyThree": .33
"pointThirtyThreeAgain": ${floats.pointThirtyThree}
},

"strings" : {
"abcd" : "abcd",
"abcdAgain" : ${strings.a}${strings.b}${strings.c}${strings.d},
"a" : "a",
"b" : "b",
"c" : "c",
"d" : "d",
"concatenated" : null bar 42 baz true 3.14 hi,
"double" : "3.14",
"doubleStartingWithDot": ".33",
"number" : "57",
"null" : "null",
"true" : "true",
"yes" : "yes",
"false" : "false",
"no" : "no"
},

"arrays" : {
"empty" : [],
"ofInt" : [1, 2, 3],
"ofString" : [ ${strings.a}, ${strings.b}, ${strings.c} ],
"ofDouble" : [3.14, 4.14, 5.14],
"ofNull" : [null, null, null],
"ofBoolean" : [true, false],
"ofArray" : [${arrays.ofString}, ${arrays.ofString}, ${arrays.ofString}],
"ofObject" : [${ints}, ${booleans}, ${strings}],
"firstElementNotASubst" : [ "a", ${strings.b} ]
},

"booleans" : {
"true" : true,
"trueAgain" : ${booleans.true},
"false" : false,
"falseAgain" : ${booleans.false}
},

"nulls" : {
"null" : null,
"nullAgain" : ${nulls.null}
},

"durations" : {
"second" : 1s,
"secondsList" : [1s,2seconds,3 s, 4000],
"secondAsNumber" : 1000,
"halfSecond" : 0.5s,
"millis" : 1 milli,
"micros" : 2000 micros,
"largeNanos" : 4878955355435272204ns,
"plusLargeNanos" : "+4878955355435272204ns",
"minusLargeNanos" : -4878955355435272204ns
},

"periods" : {
"day" : 1d,
"dayAsNumber": 2,
"week": 3 weeks,
"month": 5 mo,
"year": 8y
},

"memsizes" : {
"meg" : 1M,
"megsList" : [1M, 1024K, 1048576],
"megAsNumber" : 1048576,
"halfMeg" : 0.5M
},

"system" : {
"javaversion" : ${?java.version},
"userhome" : ${?user.home},
"home" : ${?HOME},
"pwd" : ${?PWD},
"shell" : ${?SHELL},
"lang" : ${?LANG},
"path" : ${?PATH},
"not_here" : ${?NOT_HERE},
"concatenated" : Your Java version is ${?system.javaversion} and your user.home is ${?system.userhome}
}
}
4 changes: 4 additions & 0 deletions sconfig/native/src/test/resources/test01.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"fromJson1" : 1,
"fromJsonA" : "A"
}
5 changes: 5 additions & 0 deletions sconfig/native/src/test/resources/test01.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# .properties file
fromProps.abc=abc
fromProps.one=1
fromProps.bool=true
fromProps.specialChars=hello^^

0 comments on commit 1632ce0

Please sign in to comment.