protected void loadDefaultDevelopmentConfiguration(Config config) { Config server = config.config("server"); server.put("debugOutput", true); Config resources = server.config("resources"); Config resourcesCache = resources.config("cache"); resourcesCache.put("enabled", false); Config renderer = server.config("renderer"); renderer.put("viewCacheTtl", 1L); renderer.put("templateCacheTtl", 1L); }
@Override public void setContext(String context) { while (context != null && context.startsWith("/")) context = context.substring(1); if (context != null && context.isEmpty()) context = null; super.setContext(context); config.config("server").put("context", context); }
default Config getConfig() { String[] path = prefix().split("\\."); Config config = Holders.getConfig(); if (path.length == 0) { return config; } else { for (String entry : path) { config = config.config(entry); } return config; } }
protected void loadManifest(Config config) { Unchecked.ignore(() -> { Manifest manifest = new Manifest(WebApplication.class.getResourceAsStream("/META-INF/MANIFEST.MF")); Config subconfig = config.config("manifest"); manifest.getMainAttributes().forEach((k, v) -> putInto(subconfig, String.valueOf(k), String.valueOf(v))); }); }
@Override public void setPort(int port) { super.setPort(port); config.config("server").put("port", port); }
public WebApplication setDebugOutput(boolean enabled) { super.setDebugOutput(enabled); config.config("server").put("debugOutput", enabled); return this; }
@Override public void setTTL(long milliseconds) { super.setTTL(milliseconds); config.config("server").put("ttl", milliseconds); }
@Override public void setIoThreadsCount(int ioThreadsCount) { super.setIoThreadsCount(ioThreadsCount); config.config("server").put("ioWorkersCount", ioThreadsCount); }
@Override public void setWorkersCount(int count) { super.setWorkersCount(count); config.config("server").put("workersCount", count); }
@Override public void setHostname(String hostname) { super.setHostname(hostname); config.config("server").put("hostname", hostname); }
@Test public void test_profiles_4() throws IOException, NoSuchMethodException, NoSuchFieldException, ClassNotFoundException, InterruptedException { server.config.config("environments").config("dev").config("profiles").config("profile_A").put("key", "dev_A"); server.config.config("environments").config("dev").config("profiles").config("profile_B").put("key", "dev_B"); server.config.config("environments").config("prod").config("profiles").config("profile_A").put("key", "prod_A"); server.config.config("environments").config("prod").config("profiles").config("profile_B").put("key", "prod_B"); server.setEnvironment(Environment.DEVELOPMENT); server.addProfile("profile_B"); server.start(); Assert.assertEquals("dev_B", server.getConfig().get("key")); }
@Override public <T> Dependency<T> resolve(ConfigValue annotation, Class<T> clazz) { Config config = Holders.getConfig(); String[] path = annotation.value().split("\\."); for (int i = 0; i < path.length - 1; i++) { config = config.config(path[i]); } return new SingletonDependency<>((T) config.get(path[path.length - 1])); } });
@Test public void test_profiles_6() throws IOException, NoSuchMethodException, NoSuchFieldException, ClassNotFoundException, InterruptedException { server.setEnvironment(Environment.DEVELOPMENT); server.start(); Assert.assertEquals("foobar", server.getConfig().config("foo").get("v")); Assert.assertEquals("foobar", server.getConfig().config("bar").get("v").toString()); }
@Test public void test_comment_5() { String s = "a {\n" + "// comment\n" + "b = 1" + "}"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals(Integer.valueOf(1), config.config("a").get("b", 0)); } }
@Test public void test_bind__should_not_fail_2() { String s = "" + "a.string = \"${2*2}\"\n" + ""; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); TestClass v = config.config("a").bind(TestClass.class); Assert.assertEquals("4", v.string); }
@Test public void test_bind__should_not_fail() { String s = "" + "a.l = 1\n" + ""; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); TestClass v = config.config("a").bind(TestClass.class); Assert.assertEquals(1L, v.l); }
@Test public void test_profiles_5() throws IOException, NoSuchMethodException, NoSuchFieldException, ClassNotFoundException, InterruptedException { server.setEnvironment(Environment.TEST); server.start(); Assert.assertEquals("bar", server.getConfig().config("foo").get("v")); Assert.assertTrue(server.getConfig().get("bar") instanceof Config); Assert.assertTrue(((Config) server.getConfig().get("bar")).isEmpty()); }