@Test(expected = ExecutionException.class) public void avoidsNullPointerIfNullPassed() throws Exception { FieldBinder unit = new FieldBinder(TestClass.class.getDeclaredField("uCantTouchThis")); TestClass target = new TestClass(); unit.bind(target, null); }
@Test(expected = ExecutionException.class) public void executionExceptionThrownIfWrongTypePassed() throws Exception { FieldBinder unit = new FieldBinder(TestClass.class.getDeclaredField("anInt")); TestClass target = new TestClass(); unit.bind(target, 4f); }
@Test(expected = ExecutionException.class) public void executionExceptionThrownIfFieldCantBeAccessed() throws Exception { FieldBinder unit = new FieldBinder(TestClass.class.getDeclaredField("uCantTouchThis")); TestClass target = new TestClass(); unit.bind(target, 4); }
@Test(expected = ExecutionException.class) public void executionExceptionThrownIfClassCannotBeInstantiatedWithNoArgs() throws Exception { FieldBinder unit = new FieldBinder(TestClass.class.getDeclaredField("argsRequired")); TestClass target = new TestClass(); unit.createAndBind(target); }
@Test public void staticFieldsCanBeSet() throws Exception { FieldBinder unit = new FieldBinder(TestClass.class.getDeclaredField("statics")); TestClass target = new TestClass(); unit.bind(target, 4); assertThat(target.statics, is(4)); }
@Test public void privateFieldsCanBeSet() throws Exception { FieldBinder unit = new FieldBinder(TestClass.class.getDeclaredField("privateDancer")); TestClass target = new TestClass(); unit.bind(target, 4); assertThat(target.privateDancer, is(4)); }
@Test public void valueIsSetOnField() throws Exception { FieldBinder unit = new FieldBinder(TestClass.class.getField("anInt")); TestClass target = new TestClass(); unit.bind(target, 4); assertThat(target.anInt, is(4)); }
@Test(expected = ExecutionException.class) public void executionExceptionThrownIfFieldReprivatised() throws Exception { Field f = TestClass.class.getDeclaredField("privateConstructor"); FieldBinder unit = new FieldBinder(f); TestClass target = new TestClass(); f.setAccessible(false); unit.createAndBind(target); }
/** * Constructs a component hierarchy from the design specified as an html tree. * * <p>If a component root is given, the component instances created during reading the design are * assigned to its member fields based on their id, local id, and caption * * @param doc the html tree * @param componentRoot optional component root instance. The type must match the type of the root * element in the design. * @param classWithFields a class (componentRoot class or a super class) with some member fields. * The member fields whose type is assignable from {@link Component} are bound to fields in * the design based on id/local id/caption */ private static DesignContext designToComponentTree( Document doc, Component componentRoot, Class<?> classWithFields) { DesignContext designContext = new DesignContext(doc); designContext.readPackageMappings(doc); // No special handling for a document without a body element - should be // taken care of by jsoup. Element root = doc.body(); Elements children = root.children(); if (children.size() > 1) { throw new DesignException( "The first level of a component hierarchy should contain at most one root component, but found " + children.size() + "."); } Element element = children.size() == 0 ? null : children.first(); if (componentRoot != null) { if (element == null) { throw new DesignException( "The root element cannot be null when the specified root Component is" + " not null."); } // user has specified root instance that may have member fields that // should be bound final FieldBinder binder; try { binder = new FieldBinder(componentRoot, classWithFields); } catch (IntrospectionException e) { throw new DesignException("Could not bind fields of the root component", e); } // create listener for component creations that binds the created // components to the componentRoot instance fields ComponentCreationListener creationListener = new ComponentCreationListener() { @Override public void componentCreated(ComponentCreatedEvent event) { binder.bindField(event.getComponent(), event.getLocalId()); } }; designContext.addComponentCreationListener(creationListener); // create subtree designContext.readDesign(element, componentRoot); // make sure that all the member fields are bound Collection<String> unboundFields = binder.getUnboundFields(); if (!unboundFields.isEmpty()) { throw new DesignException("Found unbound fields from component root " + unboundFields); } // no need to listen anymore designContext.removeComponentCreationListener(creationListener); } else { // createChild creates the entire component hierarchy componentRoot = element == null ? null : designContext.readDesign(element); } designContext.setRootComponent(componentRoot); return designContext; }
@Test public void createAndBindCreatesANewInstanceAndBindsItToTheField() throws Exception { FieldBinder unit = new FieldBinder(TestClass.class.getDeclaredField("another")); TestClass target = new TestClass(); Object binding = unit.createAndBind(target); assertThat(binding, instanceOf(AnotherClass.class)); assertThat(target.another, is(sameInstance(binding))); }