|
| 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 | +} |
0 commit comments