1 15 package org.apache.tapestry.binding; 16 17 import org.apache.tapestry.BindingException; 18 import org.apache.tapestry.IComponent; 19 import org.apache.tapestry.coerce.ValueConverter; 20 import org.apache.tapestry.services.ExpressionCache; 21 import org.apache.tapestry.services.ExpressionEvaluator; 22 import org.easymock.MockControl; 23 24 30 public class TestExpressionBinding extends BindingTestCase 31 { 32 33 public void testInvariant() 34 { 35 MockControl evc = newControl(ExpressionEvaluator.class); 36 ExpressionEvaluator ev = (ExpressionEvaluator) evc.getMock(); 37 38 MockControl ecc = newControl(ExpressionCache.class); 39 ExpressionCache ec = (ExpressionCache) ecc.getMock(); 40 41 MockControl cc = newControl(IComponent.class); 42 IComponent component = (IComponent) cc.getMock(); 43 44 Object compiled = new Object (); 45 46 Object expressionValue = "EXPRESSION-VALUE"; 47 48 ValueConverter vc = newValueConverter(); 49 50 ec.getCompiledExpression("exp"); 51 ecc.setReturnValue(compiled); 52 53 ev.isConstant("exp"); 54 evc.setReturnValue(true); 55 56 ev.readCompiled(component, compiled); 57 evc.setReturnValue(expressionValue); 58 59 component.getExtendedId(); 60 cc.setReturnValue("Foo/bar.baz"); 61 62 replayControls(); 63 64 ExpressionBinding b = new ExpressionBinding("param", fabricateLocation(1), vc, component, 65 "exp", ev, ec); 66 67 assertEquals(true, b.isInvariant()); 68 69 72 assertEquals(true, b.isInvariant()); 73 74 76 assertSame(expressionValue, b.getObject()); 77 78 assertSame(component, b.getComponent()); 79 80 assertEquals("ExpressionBinding[Foo/bar.baz exp]", b.toString()); 81 82 verifyControls(); 83 } 84 85 public void testVariant() 86 { 87 MockControl evc = newControl(ExpressionEvaluator.class); 88 ExpressionEvaluator ev = (ExpressionEvaluator) evc.getMock(); 89 90 MockControl ecc = newControl(ExpressionCache.class); 91 ExpressionCache ec = (ExpressionCache) ecc.getMock(); 92 93 IComponent component = newComponent(); 94 Object compiled = new Object (); 95 96 Object expressionValue1 = new Object (); 97 Object expressionValue2 = new Object (); 98 99 ValueConverter vc = newValueConverter(); 100 101 ec.getCompiledExpression("exp"); 102 ecc.setReturnValue(compiled); 103 104 ev.isConstant("exp"); 105 evc.setReturnValue(false); 106 107 ev.readCompiled(component, compiled); 108 evc.setReturnValue(expressionValue1); 109 110 ev.readCompiled(component, compiled); 111 evc.setReturnValue(expressionValue2); 112 113 replayControls(); 114 115 ExpressionBinding b = new ExpressionBinding("param", fabricateLocation(1), vc, component, 116 "exp", ev, ec); 117 118 assertEquals(false, b.isInvariant()); 119 120 123 assertSame(expressionValue1, b.getObject()); 124 125 assertSame(expressionValue2, b.getObject()); 126 127 verifyControls(); 128 } 129 130 public void testSetObject() 131 { 132 MockControl evc = newControl(ExpressionEvaluator.class); 133 ExpressionEvaluator ev = (ExpressionEvaluator) evc.getMock(); 134 135 MockControl ecc = newControl(ExpressionCache.class); 136 ExpressionCache ec = (ExpressionCache) ecc.getMock(); 137 138 IComponent component = newComponent(); 139 Object compiled = new Object (); 140 141 ValueConverter vc = newValueConverter(); 142 143 ec.getCompiledExpression("exp"); 144 ecc.setReturnValue(compiled); 145 146 ev.isConstant("exp"); 147 evc.setReturnValue(false); 148 149 Object newValue = new Object (); 150 151 ev.writeCompiled(component, compiled, newValue); 152 153 replayControls(); 154 155 ExpressionBinding b = new ExpressionBinding("param", fabricateLocation(1), vc, component, 156 "exp", ev, ec); 157 158 b.setObject(newValue); 159 160 verifyControls(); 161 } 162 163 public void testSetObjectInvariant() 164 { 165 MockControl evc = newControl(ExpressionEvaluator.class); 166 ExpressionEvaluator ev = (ExpressionEvaluator) evc.getMock(); 167 168 MockControl ecc = newControl(ExpressionCache.class); 169 ExpressionCache ec = (ExpressionCache) ecc.getMock(); 170 171 IComponent component = newComponent("Foo/bar.baz"); 172 Object compiled = new Object (); 173 174 ValueConverter vc = newValueConverter(); 175 176 ec.getCompiledExpression("exp"); 177 ecc.setReturnValue(compiled); 178 179 ev.isConstant("exp"); 180 evc.setReturnValue(true); 181 182 replayControls(); 183 184 ExpressionBinding b = new ExpressionBinding("parameter foo", fabricateLocation(1), vc, component, 185 "exp", ev, ec); 186 187 try 188 { 189 b.setObject(new Object ()); 190 unreachable(); 191 } 192 catch (BindingException ex) 193 { 194 assertEquals( 195 "Binding for parameter foo (ExpressionBinding[Foo/bar.baz exp]) may not be updated.", 196 ex.getMessage()); 197 } 198 199 verifyControls(); 200 } 201 202 public void testSetObjectFailure() 203 { 204 MockControl evc = newControl(ExpressionEvaluator.class); 205 ExpressionEvaluator ev = (ExpressionEvaluator) evc.getMock(); 206 207 MockControl ecc = newControl(ExpressionCache.class); 208 ExpressionCache ec = (ExpressionCache) ecc.getMock(); 209 210 IComponent component = newComponent(); 211 Object compiled = new Object (); 212 213 ValueConverter vc = newValueConverter(); 214 215 ec.getCompiledExpression("exp"); 216 ecc.setReturnValue(compiled); 217 218 ev.isConstant("exp"); 219 evc.setReturnValue(false); 220 221 Object newValue = new Object (); 222 223 RuntimeException innerException = new RuntimeException ("Failure"); 224 225 ev.writeCompiled(component, compiled, newValue); 226 evc.setThrowable(innerException); 227 228 replayControls(); 229 230 ExpressionBinding b = new ExpressionBinding("param", fabricateLocation(1), vc, component, 231 "exp", ev, ec); 232 233 try 234 { 235 b.setObject(newValue); 236 unreachable(); 237 } 238 catch (BindingException ex) 239 { 240 assertEquals("Failure", ex.getMessage()); 241 assertSame(innerException, ex.getRootCause()); 242 } 243 244 verifyControls(); 245 } 246 247 public void testCompileExpressionFailure() 248 { 249 MockControl evc = newControl(ExpressionEvaluator.class); 250 ExpressionEvaluator ev = (ExpressionEvaluator) evc.getMock(); 251 252 MockControl ecc = newControl(ExpressionCache.class); 253 ExpressionCache ec = (ExpressionCache) ecc.getMock(); 254 255 IComponent component = newComponent(); 256 ValueConverter vc = newValueConverter(); 257 258 Throwable innerException = new RuntimeException ("Failure"); 259 260 ec.getCompiledExpression("exp"); 261 ecc.setThrowable(innerException); 262 263 replayControls(); 264 265 ExpressionBinding b = new ExpressionBinding("param", fabricateLocation(1), vc, component, 266 "exp", ev, ec); 267 268 try 269 { 270 b.isInvariant(); 271 unreachable(); 272 } 273 catch (BindingException ex) 274 { 275 assertEquals("Failure", ex.getMessage()); 276 assertSame(innerException, ex.getRootCause()); 277 } 278 279 verifyControls(); 280 } 281 282 public void testResolveExpressionFailure() 283 { 284 MockControl evc = newControl(ExpressionEvaluator.class); 285 ExpressionEvaluator ev = (ExpressionEvaluator) evc.getMock(); 286 287 MockControl ecc = newControl(ExpressionCache.class); 288 ExpressionCache ec = (ExpressionCache) ecc.getMock(); 289 290 IComponent component = newComponent(); 291 Object compiled = new Object (); 292 293 ValueConverter vc = newValueConverter(); 294 295 ec.getCompiledExpression("exp"); 296 ecc.setReturnValue(compiled); 297 298 ev.isConstant("exp"); 299 evc.setReturnValue(false); 300 301 Throwable innerException = new RuntimeException ("Failure"); 302 303 ev.readCompiled(component, compiled); 304 evc.setThrowable(innerException); 305 306 replayControls(); 307 308 ExpressionBinding b = new ExpressionBinding("param", fabricateLocation(1), vc, component, 309 "exp", ev, ec); 310 311 try 312 { 313 b.getObject(); 314 unreachable(); 315 } 316 catch (BindingException ex) 317 { 318 assertEquals("Failure", ex.getMessage()); 319 assertSame(innerException, ex.getRootCause()); 320 } 321 322 verifyControls(); 323 } 324 } | Popular Tags |