ConfigInstanceUtil { public static<BUILDER extends ConfigBuilder> void setValues(BUILDER destination, BUILDER source) { try { Method setter = destination.getClass().getDeclaredMethod("override", destination.getClass()); setter.setAccessible(true); setter.invoke(destination, source); setter.setAccessible(false); } catch (Exception e) { throw new ConfigurationRuntimeException("Could not set values on config builder." + destination.getClass().getName(), e); } }  static void setValues(BUILDER destination, BUILDER source); static T getNewInstance(Class<T> type, String configId, ConfigPayload payload); static Object getField(ConfigBuilder builder, String fieldName);  }
@Test public void require_that_builder_values_can_be_overridden_by_another_builder() { FunctionTestConfig.Builder destination = createVariableAccessBuilder(); FunctionTestConfig.Builder source = new FunctionTestConfig.Builder() .int_val(-1) .intarr(0) .doublearr(0.0) .basicStruct(new FunctionTestConfig.BasicStruct.Builder() .bar(-1) .intArr(0)) .myarray(new FunctionTestConfig.Myarray.Builder() .intval(-1) .refval("") .fileVal("") .myStruct(new FunctionTestConfig.Myarray.MyStruct.Builder() .a(0) )); ConfigInstanceUtil.setValues(destination, source); FunctionTestConfig result = new FunctionTestConfig(destination); assertThat(result.int_val(), is(-1)); assertThat(result.string_val(), is("foo")); assertThat(result.intarr().size(), is(1)); assertThat(result.intarr(0), is(0)); assertThat(result.longarr().size(), is(2)); assertThat(result.doublearr().size(), is(3)); assertEquals(2344.0, result.doublearr(0), 0.01); assertEquals(123.0, result.doublearr(1), 0.01); assertEquals(0.0, result.doublearr(2), 0.01); assertThat(result.basicStruct().bar(), is(-1)); assertThat(result.basicStruct().foo(), is("basicFoo")); assertThat(result.basicStruct().intArr().size(), is(3)); assertThat(result.basicStruct().intArr(0), is(310)); assertThat(result.basicStruct().intArr(1), is(311)); assertThat(result.basicStruct().intArr(2), is(0)); assertThat(result.myarray().size(), is(3)); assertThat(result.myarray(2).intval(), is(-1)); assertThat(result.myarray(2).refval(), is("")); assertThat(result.myarray(2).fileVal().value(), is("")); assertThat(result.myarray(2).myStruct().a(), is(0)); }
@Test(expected = ConfigurationRuntimeException.class) public void require_that_invalid_builders_fail() { ConfigInstanceUtil.setValues(new FakeBuilder(), new FakeBuilder()); }