1 15 package org.apache.tapestry.binding; 16 17 import java.util.Date ; 18 19 import org.apache.hivemind.Location; 20 import org.apache.tapestry.BindingException; 21 import org.apache.tapestry.coerce.ValueConverter; 22 import org.easymock.MockControl; 23 24 31 public class TestLiteralBinding extends BindingTestCase 32 { 33 public void testGetObject() 34 { 35 Location l = fabricateLocation(22); 36 ValueConverter vc = newValueConverter(); 37 38 replayControls(); 39 40 LiteralBinding b = new LiteralBinding("parameter foo", vc, l, "literal-value"); 41 42 assertSame(l, b.getLocation()); 43 assertEquals("parameter foo", b.getDescription()); 44 assertEquals("literal-value", b.getObject()); 45 46 assertEquals(true, b.isInvariant()); 47 assertNull(b.getComponent()); 48 49 verifyControls(); 50 } 51 52 public void testToString() 53 { 54 Location l = fabricateLocation(22); 55 ValueConverter vc = newValueConverter(); 56 57 replayControls(); 58 59 LiteralBinding b = new LiteralBinding("parameter foo", vc, l, "literal-value"); 60 61 assertEquals("StaticBinding[literal-value]", b.toString()); 62 63 verifyControls(); 64 } 65 66 public void testGetObjectWithClass() 67 { 68 MockControl control = newControl(ValueConverter.class); 69 ValueConverter vc = (ValueConverter) control.getMock(); 70 71 Date date = new Date (); 72 73 vc.coerceValue("my-literal", Date .class); 74 control.setReturnValue(date); 75 76 replayControls(); 77 78 LiteralBinding b = new LiteralBinding("parameter foo", vc, fabricateLocation(99), 79 "my-literal"); 80 81 assertSame(date, b.getObject(Date .class)); 82 83 verifyControls(); 84 } 85 86 public void testGetObjectException() 87 { 88 MockControl control = newControl(ValueConverter.class); 89 ValueConverter vc = (ValueConverter) control.getMock(); 90 91 Exception innerException = new RuntimeException ("Failure"); 92 93 vc.coerceValue("my-literal", Date .class); 94 control.setThrowable(innerException); 95 96 replayControls(); 97 98 Location location = fabricateLocation(99); 99 LiteralBinding b = new LiteralBinding("parameter foo", vc, location, "my-literal"); 100 101 try 102 { 103 b.getObject(Date .class); 104 unreachable(); 105 } 106 catch (BindingException ex) 107 { 108 assertEquals("Error converting value for parameter foo: Failure", ex.getMessage()); 109 assertSame(innerException, ex.getRootCause()); 110 assertSame(location, ex.getLocation()); 111 assertSame(b, ex.getBinding()); 112 } 113 114 verifyControls(); 115 } 116 117 public void testSetObject() 118 { 119 Location l = fabricateLocation(22); 120 ValueConverter vc = newValueConverter(); 121 122 replayControls(); 123 124 LiteralBinding b = new LiteralBinding("parameter foo", vc, l, "literal-value"); 125 126 try 127 { 128 b.setObject("fred"); 129 unreachable(); 130 } 131 catch (BindingException ex) 132 { 133 assertEquals( 134 "Binding for parameter foo (StaticBinding[literal-value]) may not be updated.", 135 ex.getMessage()); 136 assertSame(b, ex.getBinding()); 137 assertSame(l, ex.getLocation()); 138 } 139 140 } 141 } | Popular Tags |