InstanceResolver { static void applyDef(ConfigBuilder builder, InnerCNode targetDef) throws Exception { for (Map.Entry<String, CNode> e: targetDef.children().entrySet()) { CNode node = e.getValue(); if (node instanceof LeafCNode) { setLeafValueIfUnset(targetDef, builder, (LeafCNode)node); } else if (node instanceof InnerCNode) { if (hasField(builder.getClass(), node.getName())) { Field innerField = builder.getClass().getDeclaredField(node.getName()); innerField.setAccessible(true); Object innerFieldVal = innerField.get(builder); if (innerFieldVal instanceof List) { List<?> innerList = (List<?>) innerFieldVal; for (Object b : innerList) { if (b instanceof ConfigBuilder) { applyDef((ConfigBuilder) b, (InnerCNode) node); } } } else if (innerFieldVal instanceof ConfigBuilder) { applyDef((ConfigBuilder) innerFieldVal, (InnerCNode) node); } else { } } } } }    }
@Test public void testApplyDefToBuilder() throws Exception { FunctionTestConfig.Builder builder = createVariableAccessBuilder(); InnerCNode targetDef = getDef(FunctionTestConfig.CONFIG_DEF_SCHEMA); ((LeafCNode) targetDef.getChild("stringwithdef")).setDefaultValue(new DefaultValue("newDef", new DefLine.Type("string"))); ((LeafCNode) targetDef.getChild("string_val")).setDefaultValue(new DefaultValue("newDefVal", new DefLine.Type("string"))); ((LeafCNode) targetDef.getChild("enumwithdef")).setDefaultValue(new DefaultValue(Enumwithdef.FOOBAR2.toString(), new DefLine.Type("enum"))); ((LeafCNode) targetDef.getChild("enum_val")).setDefaultValue(new DefaultValue(Enum_val.FOO.toString(), new DefLine.Type("enum"))); ((LeafCNode) targetDef.getChild("basicStruct").getChild("foo")).setDefaultValue(new DefaultValue("basicSchmasic", new DefLine.Type("string"))); ((LeafCNode) targetDef.getChild("basicStruct").getChild("bar")).setDefaultValue(new DefaultValue("89", new DefLine.Type("int"))); InnerCNode rootStruct = ((InnerCNode) targetDef.getChild("rootStruct")); InnerCNode innerArr = (InnerCNode) rootStruct.getChild("innerArr"); ((LeafCNode) innerArr.getChild("boolVal")).setDefaultValue(new DefaultValue("true", new DefLine.Type("bool"))); ((LeafCNode) innerArr.getChild("stringVal")).setDefaultValue(new DefaultValue("derp", new DefLine.Type("string"))); InnerCNode myArray = ((InnerCNode) targetDef.getChild("myarray")); myArray.children().put("intval", LeafCNode.newInstance(new DefLine.Type("int"), myArray, "intval", "-123424")); targetDef.children().put("myarray", myArray); InstanceResolver.applyDef(builder, targetDef); FunctionTestConfig c = new FunctionTestConfig(builder); assertEquals(c.string_val(), "foo"); assertEquals(c.stringwithdef(), "newDef"); assertEquals(c.enumwithdef(), Enumwithdef.FOOBAR2); assertEquals(c.enum_val(), Enum_val.FOOBAR); assertEquals(c.double_with_def(), -12, 0.0001); assertEquals(c.basicStruct().foo(), "basicSchmasic"); assertEquals(c.basicStruct().bar(), 3); assertTrue(c.rootStruct().innerArr(0).boolVal()); assertEquals(c.rootStruct().innerArr(0).stringVal(), "deep"); assertEquals(c.myarray(0).intval(), -123424); assertEquals(c.urlVal().toString(), "url"); assertEquals(c.urlArr(0).toString(), "url"); assertEquals(c.myarray(0).urlVal().toString(), "url1"); assertEquals(c.myarray(1).urlVal().toString(), "url2"); }
@Test public void testApplyDefToBuilderMismatches() throws Exception { FunctionTestConfig.Builder builder = createVariableAccessBuilderManyUnset(); InnerCNode targetDef = getDef(FunctionTestConfig.CONFIG_DEF_SCHEMA); targetDef.children().put("stringwithdef", LeafCNode.newInstance(new DefLine.Type("int"), targetDef, "stringwithdef", "1")); targetDef.children().put("int_val", LeafCNode.newInstance(new DefLine.Type("string"), targetDef, "int_val", "fooOO")); InstanceResolver.applyDef(builder, targetDef); try { FunctionTestConfig c = new FunctionTestConfig(builder); fail("No exception on incomplete builder"); } catch (Exception e) { } }
@Test public void testExtraFieldsAreIgnored() throws Exception { try { SimpletypesConfig.Builder builder = new SimpletypesConfig.Builder(); InnerCNode defWithExtra = new DefParser(SimpletypesConfig.CONFIG_DEF_NAME, new StringReader(StringUtilities.implode(SimpletypesConfig.CONFIG_DEF_SCHEMA, "\n") + "\nnewfield string default=\"foo\"\n")).getTree(); InstanceResolver.applyDef(builder, defWithExtra); } catch (NoSuchFieldException e) { fail("Should not fail on extra field"); } }