PropBindingFactory implements BindingFactory { public Binding newBinding(String description, ComponentResources container, ComponentResources component, String expression, Location location) { Object target = container.getComponent(); Class targetClass = target.getClass(); PropertyConduit conduit = source.create(targetClass, expression); String toString = interner.format("PropBinding[%s %s(%s)]", description, container .getCompleteId(), expression); return new PropBinding(location, target, conduit, expression, toString); } PropBindingFactory(PropertyConduitSource propertyConduitSource, StringInterner interner); Binding newBinding(String description, ComponentResources container,
                              ComponentResources component, String expression, Location location);  }
@Test public void object_property() { TargetBean bean = new TargetBean(); ComponentResources resources = newComponentResources(bean); Location l = mockLocation(); replay(); Binding binding = factory.newBinding("test binding", resources, null, "objectValue", l); assertSame(binding.getBindingType(), String.class); bean.setObjectValue("first"); assertEquals(binding.get(), "first"); binding.set("second"); assertEquals(bean.getObjectValue(), "second"); assertEquals(InternalUtils.locationOf(binding), l); assertEquals(binding.toString(), "PropBinding[test binding foo.Bar:baz(objectValue)]"); verify(); }
@Test public void annotation_from_read_only_property() { TargetBean bean = new TargetBean(); ComponentResources resources = newComponentResources(bean); Location l = mockLocation(); replay(); Binding binding = factory.newBinding("test binding", resources, null, "readOnly", l); assertEquals(binding.getAnnotation(Validate.class).value(), "readonly"); verify(); }
@Test public void annotation_from_write_only_property() { TargetBean bean = new TargetBean(); ComponentResources resources = newComponentResources(bean); Location l = mockLocation(); replay(); Binding binding = factory.newBinding("test binding", resources, null, "writeOnly", l); assertEquals(binding.getAnnotation(Validate.class).value(), "writeonly"); verify(); }
@Test public void annotation_does_not_exist() { TargetBean bean = new TargetBean(); ComponentResources resources = newComponentResources(bean); Location l = mockLocation(); replay(); Binding binding = factory.newBinding("test binding", resources, null, "intValue", l); assertNull(binding.getAnnotation(Validate.class)); verify(); }
@Test public void annotation_on_named_method() { TargetBean bean = new TargetBean(); ComponentResources resources = newComponentResources(bean); Location l = mockLocation(); replay(); Binding binding = factory.newBinding("test binding", resources, null, "stringHolderMethod()", l); assertNotNull(binding.getAnnotation(BeforeRenderBody.class)); verify(); }
@Test public void annnotation_on_read_method_takes_precedence_over_write_method() { TargetBean bean = new TargetBean(); ComponentResources resources = newComponentResources(bean); Location l = mockLocation(); replay(); Binding binding = factory.newBinding("test binding", resources, null, "objectValue", l); assertEquals(binding.getAnnotation(Validate.class).value(), "getObjectValue"); verify(); }
@Test public void property_path() { TargetBean bean = new TargetBean(); ComponentResources resources = newComponentResources(bean); Location l = mockLocation(); replay(); Binding binding = factory.newBinding("test binding", resources, null, "stringHolder.value", l); assertSame(binding.getBindingType(), String.class); bean.getStringHolder().setValue("first"); assertEquals(binding.get(), "first"); binding.set("second"); assertEquals(bean.getStringHolder().getValue(), "second"); assertEquals(binding.toString(), "PropBinding[test binding foo.Bar:baz(stringHolder.value)]"); verify(); }
@Test public void property_path_with_explicit_method_in_preamble() { TargetBean bean = new TargetBean(); ComponentResources resources = newComponentResources(bean); Location l = mockLocation(); replay(); Binding binding = factory.newBinding("test binding", resources, null, "stringHolderMethod().value", l); assertSame(binding.getBindingType(), String.class); bean.getStringHolder().setValue("first"); assertEquals(binding.get(), "first"); assertEquals(binding.toString(), "PropBinding[test binding foo.Bar:baz(stringHolderMethod().value)]"); verify(); }
@Test public void method_call_as_terminal() { TargetBean bean = new TargetBean(); ComponentResources resources = newComponentResources(bean); Location l = mockLocation(); replay(); Binding binding = factory.newBinding("test binding", resources, null, "stringHolderMethod().stringValue()", l); assertSame(binding.getBindingType(), String.class); bean.getStringHolder().setValue("first"); assertEquals(binding.get(), "first"); try { binding.set("read-only"); unreachable(); } catch (TapestryException ex) { assertEquals( ex.getMessage(), "Expression 'stringHolderMethod().stringValue()' for class org.apache.tapestry5.internal.bindings.TargetBean is read-only."); assertSame(ex.getLocation(), l); } verify(); }
@Test public void method_not_found_in_preamble() { TargetBean bean = new TargetBean(); ComponentResources resources = mockComponentResources(); Location l = mockLocation(); train_getComponent(resources, bean); replay(); try { factory.newBinding("test binding", resources, null, "isThatRealBlood().value", l); unreachable(); } catch (RuntimeException ex) { assertMessageContains(ex, "Class org.apache.tapestry5.internal.bindings.TargetBean does not contain a public method named 'isThatRealBlood()'"); } verify(); }
@Test public void method_not_found_in_terminal() { TargetBean bean = new TargetBean(); ComponentResources resources = mockComponentResources(); Location l = mockLocation(); train_getComponent(resources, bean); replay(); try { factory.newBinding("test binding", resources, null, "stringHolder.isThatRealBlood()", l); unreachable(); } catch (RuntimeException ex) { assertMessageContains(ex, "StringHolder", "does not contain a public method", "isThatRealBlood()"); } verify(); }
@Test public void void_method_in_preamble() { TargetBean bean = new TargetBean(); ComponentResources resources = mockComponentResources(); Location l = mockLocation(); train_getComponent(resources, bean); replay(); try { factory.newBinding("test binding", resources, null, "voidMethod().value", l); unreachable(); } catch (RuntimeException ex) { assertMessageContains(ex, "Method org.apache.tapestry5.internal.bindings.TargetBean.voidMethod() returns void"); } verify(); }
@Test public void void_method_as_terminal() { TargetBean bean = new TargetBean(); ComponentResources resources = mockComponentResources(); Location l = mockLocation(); train_getComponent(resources, bean); replay(); try { factory.newBinding("test binding", resources, null, "stringHolder.voidMethod()", l); unreachable(); } catch (RuntimeException ex) { assertMessageContains(ex, "Method org.apache.tapestry5.internal.bindings.StringHolder.voidMethod() returns void"); } verify(); }
@Test public void property_path_through_missing_property() { TargetBean bean = new TargetBean(); ComponentResources resources = mockComponentResources(); Location l = mockLocation(); train_getComponent(resources, bean); replay(); String propertyPath = "stringHolder.missingProperty.terminalProperty"; try { factory.newBinding("test binding", resources, null, propertyPath, l); unreachable(); } catch (RuntimeException ex) { assertMessageContains(ex, "Class org.apache.tapestry5.internal.bindings.StringHolder does not contain a property", "\'missingProperty\'"); } verify(); }
@Test public void property_path_through_write_only_property() { TargetBean bean = new TargetBean(); ComponentResources resources = mockComponentResources(); Location l = mockLocation(); train_getComponent(resources, bean); replay(); String propertyPath = "writeOnly.terminalProperty"; try { factory.newBinding("test binding", resources, null, propertyPath, l); unreachable(); } catch (RuntimeException ex) { assertMessageContains(ex, "Property \'writeOnly\' of class org.apache.tapestry5.internal.bindings.TargetBean", "is not readable (it has no read accessor method)."); } verify(); }
@Test public void primitive_property() { TargetBean bean = new TargetBean(); ComponentResources resources = newComponentResources(bean); Location l = mockLocation(); replay(); Binding binding = factory.newBinding("test binding", resources, null, "intValue", l); assertSame(binding.getBindingType(), int.class); bean.setIntValue(1); assertEquals(binding.get(), 1); binding.set(2); assertEquals(bean.getIntValue(), 2); verify(); }
@Test public void read_only_property() { TargetBean bean = new TargetBean(); ComponentResources resources = newComponentResources(bean); Location l = mockLocation(); replay(); Binding binding = factory.newBinding("test binding", resources, null, "readOnly", l); assertEquals(binding.get(), "ReadOnly"); try { binding.set("fail"); unreachable(); } catch (TapestryException ex) { assertEquals(ex.getMessage(), "Expression 'readOnly' for class org.apache.tapestry5.internal.bindings.TargetBean is read-only."); assertEquals(ex.getLocation(), l); } verify(); }
@Test public void write_only_property() { TargetBean bean = new TargetBean(); ComponentResources resources = newComponentResources(bean); Location l = mockLocation(); replay(); Binding binding = factory.newBinding("test binding", resources, null, "writeOnly", l); binding.set("updated"); assertEquals(bean.writeOnly, "updated"); try { assertEquals(binding.get(), "ReadOnly"); unreachable(); } catch (TapestryException ex) { assertEquals(ex.getMessage(), "Expression 'writeOnly' for class org.apache.tapestry5.internal.bindings.TargetBean is write-only."); assertEquals(ex.getLocation(), l); } verify(); }
@Test public void unknown_property() { TargetBean bean = new TargetBean(); ComponentResources resources = mockComponentResources(); Location l = mockLocation(); train_getComponent(resources, bean); replay(); try { factory.newBinding("test binding", resources, null, "missingProperty", l); unreachable(); } catch (RuntimeException ex) { assertMessageContains(ex, "Class org.apache.tapestry5.internal.bindings.TargetBean does not contain a property", "\'missingProperty\'"); } verify(); }
@Test(dataProvider = "values") public void special_prop_binding_values(String expression, Object expected) { Location l = mockLocation(); String description = "my description"; ComponentResources resources = mockComponentResources(); Component component = mockComponent(); train_getComponent(resources, component); train_getCompleteId(resources, "Does.not.matter"); replay(); Binding binding = factory.newBinding(description, resources, null, expression, l); assertEquals(binding.get(), expected); assertTrue(binding.isInvariant()); verify(); }