ConfigUtils { public static Config configFromPath(String path) { File configFile = new File(path); Config config; try { config = ConfigFactory.parseFile(configFile); } catch (ConfigException.Parse e) { if (e.getMessage().contains("Key '\"PK")) { throw new RuntimeException(JAR_FILE_EXCEPTION_MESSAGE); } else { throw e; } } return config; }  static Config configFromPath(String path); static Config configFromResource(String resource); static Config applySubstitutions(Config config); static Config applySubstitutions(Config config, String substitutionsString); static Config getApplicationConfig(Config config); static Config mergeLoadedConfiguration(Config config); static Config findReplaceStringValues(Config config, String findRegex, Object replace); static boolean canBeCoerced(Config config, String path, ConfigValueType type); static T getOrElse(Config config, String path, T orElse); static T getOrNull(Config config, String path); static final String JAR_FILE_EXCEPTION_MESSAGE; }
@Test public void testConfigFromPath() throws Exception { String configString = "a=1,b.c=X,d.e.f=Y"; PrintWriter writer = new PrintWriter("testconf.conf", "UTF-8"); writer.println(configString); writer.close(); Config config = ConfigUtils.configFromPath("testconf.conf"); Files.delete(Paths.get("testconf.conf")); assertEquals(config.getInt("a"), 1); assertEquals(config.getString("b.c"), "X"); assertEquals(config.getString("d.e.f"), "Y"); }
@Test public void testConfigFromPathButJarFile() throws Exception { byte[] testJarContents = DatatypeConverter.parseBase64Binary( "UEsDBAoAAAAAAPJrbU4AAAAAAAAAAAAAAAAFABAAdGVzdC9VWAwAfD6JXHc+iVz2ARQAUEsBAhUDCgAAAAAA8mtt" + "TgAAAAAAAAAAAAAAAAUADAAAAAAAAAAAQO1BAAAAAHRlc3QvVVgIAHw+iVx3PolcUEsFBgAAAAABAAEAPwAA" + "ADMAAAAAAA=="); try { File jarFile = folder.newFile("test.jar"); Files.write(jarFile.toPath(), testJarContents); ConfigUtils.configFromPath(jarFile.getAbsolutePath()); } catch (RuntimeException e) { if (e.getMessage().equals(ConfigUtils.JAR_FILE_EXCEPTION_MESSAGE)) { return; } } fail(); }