private CoercingPropertyConduitWrapper createConduit(String propertyName) { return new CoercingPropertyConduitWrapper(propertyConduitSource.create(beanType, propertyName), typeCoercer); }
return new PropertyConduitSource() { public PropertyConduit create(Class rootType, String expression) { PropertyConduit conduit = old.create(rootType, expression);
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); } }
@Test public void lexer_error_in_property_expression() { try { source.create(IntegerHolder.class, "fred #"); unreachable(); } catch (RuntimeException ex) { assertEquals(ex.getMessage(), "Error parsing property expression 'fred #': Unable to parse input at character position 6."); } }
@Test public void annotation_of_public_field() { PropertyConduit pc = source.create(PublicFieldBean.class, "StringField"); assertNotNull(pc.getAnnotation(NonVisual.class)); }
@Test public void generic_types_are_determined() { PropertyConduit2 datesConduit = (PropertyConduit2) source.create(GenericBean.class, "dates"); PropertyConduit2 longsConduit = (PropertyConduit2) source.create(GenericBean.class, "longs"); PropertyConduit2 nestedDatesConduit = (PropertyConduit2) source.create(GenericBean.class, "genericBeans.get(0).dates"); PropertyConduit2 mapConduit = (PropertyConduit2) source.create(GenericBean.class, "map"); assertEquals(datesConduit.getPropertyGenericType().toString(), "java.util.List<java.util.Date>"); assertEquals(longsConduit.getPropertyGenericType().toString(), "java.util.List<java.lang.Long>"); assertEquals(nestedDatesConduit.getPropertyGenericType().toString(), "java.util.List<java.util.Date>"); assertEquals(mapConduit.getPropertyGenericType().toString(), "java.util.Map<java.lang.String, java.lang.Integer>"); }
@Test public void parse_error_in_property_expression() { try { source.create(IntegerHolder.class, "getValue("); unreachable(); } catch (RuntimeException ex) { //note that addition of map support changed how this expression was parsed such that the error is now //reported at character 8, (, rather than 0: getValue(. assertEquals(ex.getMessage(), "Error parsing property expression 'getValue(': line 1:8 no viable alternative at input '('."); } }
@Test public void empty_map() { PropertyConduit conduit = source.create(EchoBean.class, "{ }"); EchoBean bean = new EchoBean(); Map m = (Map) conduit.get(bean); assertTrue(m.isEmpty()); }
private void assertConduitPropertyType(Class<?> origin, String property, Class<?> expectedType) { assertEquals(expectedType, source.create(origin, property).getPropertyType()); } }
@Test public void null_root_object() { PropertyConduit conduit = source.create(StringHolderBean.class, "value.get()"); try { conduit.get(null); unreachable(); } catch (NullPointerException ex) { assertEquals(ex.getMessage(), "Root object of property expression 'value.get()' is null."); } }
/** * TAP5-330 */ @Test public void object_methods_can_be_invoked() { PropertyConduit conduit = source.create(Block.class, "toString()"); Block b = new Block() { @Override public String toString() { return "Do You Grok Ze Block?"; } }; assertEquals(conduit.get(b), "Do You Grok Ze Block?"); }
@Test public void top_level_list() { PropertyConduit conduit = source.create(EchoBean.class, "[ 1, 2.0, storedString ]"); EchoBean bean = new EchoBean(); bean.setStoredString("Lisa"); List l = (List) conduit.get(bean); assertListsEquals(l, new Long(1), new Double(2.0), "Lisa"); }
@Test public void boolean_constant_as_method_parameter() { Bedrock bedrock = new Bedrock(); PropertyConduit trueConduit = source.create(Bedrock.class, "toName(true)"); PropertyConduit falseConduit = source.create(Bedrock.class, "toName(false)"); assertEquals(trueConduit.get(bedrock), "Fred"); assertEquals(falseConduit.get(bedrock), "Barney"); }
@Test public void final_static_fields_are_read_only() { PropertyConduit pc = source.create(PublicStaticFieldBean.class, "read_only"); try { pc.set(null, "new-value"); unreachable(); } catch (RuntimeException ex) { assertEquals(ex.getMessage(), "Expression 'read_only' for class org.apache.tapestry5.internal.services.PublicStaticFieldBean is read-only."); } }
@Test public void generic_interface() { final WithRealizedGenericInterface bean = new WithRealizedGenericInterface(); bean.a = "Hello"; bean.b = 12345L; PropertyConduit conduit = source.create(WithRealizedGenericInterface.class, "genericA()"); assertSame(conduit.get(bean), "Hello"); conduit = source.create(WithRealizedGenericInterface.class, "genericB()"); assertEquals(conduit.get(bean), 12345L); }
@Test public void list_as_method_argument() { PropertyConduit conduit = source.create(EchoBean.class, "echoList([ 1, 2.0, storedString ])"); EchoBean bean = new EchoBean(); bean.setStoredString("Bart"); List l = (List) conduit.get(bean); assertListsEquals(l, new Long(1), new Double(2.0), "Bart"); }
@Test public void public_static_field_in_an_array() { PropertyConduit pc = source.create(PublicStaticFieldBean.class, "[read_only]"); // Need to instantiate it, or exception "Root object of property expression is null" List<String> actual = (List<String>) pc.get(new PublicStaticFieldBean()); assertListsEquals(actual, PublicStaticFieldBean.READ_ONLY); }
/** * TAP5-1555 */ @Test public void this_and_null_inside_array() { PropertyConduit pc = source.create(NonVisualBean.class, "[this, null]"); Object bean = new NonVisualBean(); List list = (List) pc.get(bean); assertEquals(list.size(), 2); assertSame(list.get(0), bean); assertNull(list.get(1)); }
@Test public void method_invocation_with_integer_arguments() { PropertyConduit conduit = source.create(EchoBean.class, "echoInt(storedInt, 3)"); EchoBean bean = new EchoBean(); for (int i = 0; i < 10; i++) { bean.setStoredInt(i); assertEquals(conduit.get(bean), new Integer(i * 3)); } }
@Test public void method_invocation_with_double_argument() { PropertyConduit conduit = source.create(EchoBean.class, "echoDouble(storedDouble, 2.0)"); EchoBean bean = new EchoBean(); double value = 22. / 7.; bean.setStoredDouble(value); assertEquals(conduit.get(bean), new Double(2. * value)); }