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 annotation_of_public_field() { PropertyConduit pc = source.create(PublicFieldBean.class, "StringField"); assertNotNull(pc.getAnnotation(NonVisual.class)); }
@Test public void empty_map() { PropertyConduit conduit = source.create(EchoBean.class, "{ }"); EchoBean bean = new EchoBean(); Map m = (Map) conduit.get(bean); assertTrue(m.isEmpty()); }
@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 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); }
@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 literal_conduits_have_invariant_annotation() { PropertyConduit pc = source.create(CompositeBean.class, "12345"); Invariant annotation = pc.getAnnotation(Invariant.class); assertNotNull(annotation); assertSame(annotation.annotationType(), Invariant.class); }
/** * TAP5-1673 */ @Test public void public_static_fields_are_accessible() { PropertyConduit pc = source.create(PublicStaticFieldBean.class, "value"); assertSame(pc.get(null), PublicStaticFieldBean.VALUE); pc.set(null, "new-value"); assertEquals(PublicStaticFieldBean.VALUE, "new-value"); }
@Test public void field_annotations_are_visible() { PropertyConduit conduit = source.create(CompositeBean.class, "simple.firstName"); Validate annotation = conduit.getAnnotation(Validate.class); assertNotNull(annotation); assertEquals(annotation.value(), "required"); }
@Test public void public_primitive_field() { PublicFieldBean bean = new PublicFieldBean(); bean.intField = 99; // check out the case insensitiveness: PropertyConduit pc = source.create(PublicFieldBean.class, "IntField"); assertEquals(pc.get(bean), new Integer(99)); pc.set(bean, 37); assertEquals(bean.intField, 37); }
@Test public void map_as_method_argument() { PropertyConduit conduit = source.create(EchoBean.class, "echoMap({ 1: 'one', 2.0: 'two', storedString: stringSource.value })"); EchoBean bean = new EchoBean(); bean.setStoredString("3"); bean.setStringSource(new StringSource("three")); Map m = (Map) conduit.get(bean); assertEquals("one", m.get(1L)); assertEquals("two", m.get(2.0)); assertEquals("three", m.get("3")); }
@Test public void range_variable_to() { PropertyConduit pc = source.create(IntegerHolder.class, "10..value"); IntegerHolder h = new IntegerHolder(); h.setValue(5); IntegerRange ir = (IntegerRange) pc.get(h); assertEquals(ir, new IntegerRange(10, 5)); }
@Test public void public_object_field() { PublicFieldBean bean = new PublicFieldBean(); bean.stringField = "x"; PropertyConduit pc = source.create(PublicFieldBean.class, "stringField"); assertEquals(pc.get(bean), "x"); pc.set(bean, "y"); assertEquals(bean.stringField, "y"); }
@Test public void not_operator() { PropertyConduit conduit = source.create(IntegerHolder.class, "! value"); IntegerHolder holder = new IntegerHolder(); assertEquals(conduit.get(holder), Boolean.TRUE); holder.setValue(99); assertEquals(conduit.get(holder), Boolean.FALSE); }
/** * TAP5-747 */ @Test public void dereference_result_of_method_invocation() { ComplexObject co = new ComplexObject(); PropertyConduit pc = source.create(ComplexObject.class, "get(nestedIndex).name"); assertEquals(pc.get(co), "zero"); co.setNestedIndex(1); assertEquals(pc.get(co), "one"); }
@Test public void method_invocation_using_dereference() { PropertyConduit conduit = source.create(EchoBean.class, "echoString(storedString, stringSource.value, 'beta')"); EchoBean bean = new EchoBean(); StringSource source = new StringSource("alpha"); bean.setStringSource(source); bean.setStoredString("Barney"); assertEquals(conduit.get(bean), "alpha - Barney - beta"); }
@Test public void not_operator_in_subexpression() { PropertyConduit conduit = source.create(Switch.class, "label(! value)"); Switch sw = new Switch(); assertEquals(conduit.get(sw), "aye"); sw.setValue(true); assertEquals(conduit.get(sw), "nay"); }
@Test public void generic_nested() { Three bean = new Three(); bean.setA("hello"); bean.setB(123L); bean.setB2(1235L); bean.setX(54321L); PropertyConduit conduit = source.create(Three.class, "a"); assertSame(conduit.get(bean), "hello"); }
@Test public void method_names_are_matched_caselessly() { InternalPropertyConduit conduit = (InternalPropertyConduit) source.create(CompositeBean.class, "GETSIMPLE().firstName"); assertEquals(conduit.getPropertyName(), "firstName"); CompositeBean bean = new CompositeBean(); SimpleBean inner = new SimpleBean(); bean.setSimple(inner); conduit.set(bean, "Howard"); assertEquals(inner.getFirstName(), "Howard"); }
@Test public void generics() { String string = "surprise"; StringHolder stringHolder = new StringHolder(); stringHolder.put(string); StringHolderBean bean = new StringHolderBean(); bean.setValue(stringHolder); PropertyConduit conduit = source.create(StringHolderBean.class, "value.get()"); assertSame(conduit.get(bean), string); assertSame(conduit.getPropertyType(), String.class); }