Config { public static Boolean getSysConfBoolean(String key, Boolean def, JsonObject conf) { final String v = System.getProperty(key); if (v == null || v.isEmpty()) { return conf.getBoolean(key, def); } return Boolean.parseBoolean(v); } private  Config(); static String getSysConf(String key, String def, JsonObject conf); static Boolean getSysConfBoolean(String key, Boolean def, JsonObject conf);  }
@Test public void testBoolean() { JsonObject conf = new JsonObject(); final String varName = "foo-bar92304238"; Assert.assertEquals(true, Config.getSysConfBoolean(varName, true, conf)); Assert.assertEquals(null, Config.getSysConfBoolean(varName, null, conf)); conf.put(varName, false); Assert.assertEquals(false, Config.getSysConfBoolean(varName, true, conf)); boolean thrown = false; try { conf.put(varName, "xx"); Assert.assertEquals(false, Config.getSysConfBoolean(varName, true, conf)); } catch (ClassCastException ex) { thrown = true; } Assert.assertTrue(thrown); System.setProperty(varName, "xx"); Assert.assertEquals(false, Config.getSysConfBoolean(varName, null, conf)); System.setProperty(varName, "true"); Assert.assertEquals(true, Config.getSysConfBoolean(varName, false, conf)); System.setProperty(varName, "false"); Assert.assertEquals(false, Config.getSysConfBoolean(varName, true, conf)); conf.put(varName, false); System.setProperty(varName, ""); Assert.assertEquals(false, Config.getSysConfBoolean(varName, true, conf)); conf.remove(varName); Assert.assertEquals(true, Config.getSysConfBoolean(varName, true, conf)); Assert.assertEquals(null, Config.getSysConfBoolean(varName, null, conf)); }