diff --git a/sconfig/jvm-native/src/main/scala/org/ekrich/config/ConfigFactoryJvmNative.scala b/sconfig/jvm-native/src/main/scala/org/ekrich/config/ConfigFactoryJvmNative.scala index 3085e9ea..e3e9bfdc 100644 --- a/sconfig/jvm-native/src/main/scala/org/ekrich/config/ConfigFactoryJvmNative.scala +++ b/sconfig/jvm-native/src/main/scala/org/ekrich/config/ConfigFactoryJvmNative.scala @@ -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. + +} diff --git a/sconfig/jvm-native/src/test/scala/org/ekrich/config/impl/PublicApiFileTest.scala b/sconfig/jvm-native/src/test/scala/org/ekrich/config/impl/PublicApiFileTest.scala new file mode 100644 index 00000000..0592b25a --- /dev/null +++ b/sconfig/jvm-native/src/test/scala/org/ekrich/config/impl/PublicApiFileTest.scala @@ -0,0 +1,108 @@ +/** + * Copyright (C) 2011 Typesafe Inc. + */ +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")) + } + +} diff --git a/sconfig/jvm/src/test/scala/org/ekrich/config/impl/PublicApiTest.scala b/sconfig/jvm/src/test/scala/org/ekrich/config/impl/PublicApiTest.scala index c81415e0..9813e541 100644 --- a/sconfig/jvm/src/test/scala/org/ekrich/config/impl/PublicApiTest.scala +++ b/sconfig/jvm/src/test/scala/org/ekrich/config/impl/PublicApiTest.scala @@ -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] { diff --git a/sconfig/native/src/test/resources/test01.conf b/sconfig/native/src/test/resources/test01.conf new file mode 100644 index 00000000..e40eb7f5 --- /dev/null +++ b/sconfig/native/src/test/resources/test01.conf @@ -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} + } +} diff --git a/sconfig/native/src/test/resources/test01.json b/sconfig/native/src/test/resources/test01.json new file mode 100644 index 00000000..13809157 --- /dev/null +++ b/sconfig/native/src/test/resources/test01.json @@ -0,0 +1,4 @@ +{ + "fromJson1" : 1, + "fromJsonA" : "A" +} \ No newline at end of file diff --git a/sconfig/native/src/test/resources/test01.properties b/sconfig/native/src/test/resources/test01.properties new file mode 100644 index 00000000..71de4e0b --- /dev/null +++ b/sconfig/native/src/test/resources/test01.properties @@ -0,0 +1,5 @@ +# .properties file +fromProps.abc=abc +fromProps.one=1 +fromProps.bool=true +fromProps.specialChars=hello^^