1 16 package org.apache.myfaces.el; 17 18 import java.util.Map ; 19 20 import javax.faces.el.ReferenceSyntaxException; 21 import javax.faces.el.ValueBinding; 22 23 24 72 public class ValueBindingTest extends ELBaseTest 73 { 74 76 public ValueBindingTest(String name) 77 { 78 super(name); 79 } 80 81 83 public void testGetValueWithLongName1() throws Exception 84 { 85 Object v; 86 ValueBinding vb = _application.createValueBinding("#{theA}"); 87 v = vb.getValue(_facesContext); 88 assertTrue(v == _theA); 89 } 90 91 public void testGetValueWithLongName2() throws Exception 92 { 93 Object v; 94 v = _application.createValueBinding("#{theA.name}").getValue( 95 _facesContext); 96 assertEquals(A.NAME, v); 97 } 98 99 public void testGetValueWithLongName3() throws Exception 100 { 101 Object v; 102 v = _application.createValueBinding("#{theA.theB.name}").getValue( 103 _facesContext); 104 assertEquals(B.NAME, v); 105 } 106 107 public void testGetValueWithLongName4() throws Exception 108 { 109 Object v; 110 v = _application.createValueBinding("#{theA.theB.theC.name}").getValue( 111 _facesContext); 112 assertEquals(C.DEFAULT_NAME, v); 113 } 114 115 public void testGetValueWithShortName1() throws Exception 116 { 117 Object v; 118 v = _application.createValueBinding("#{a}").getValue(_facesContext); 119 assertTrue(v == _a); 120 } 121 122 public void testGetValueWithShortName2() throws Exception 123 { 124 Object v; 125 v = _application.createValueBinding("#{a.name}") 126 .getValue(_facesContext); 127 assertEquals(A.NAME, v); 128 } 129 130 public void testGetValueWithShortName3() throws Exception 131 { 132 Object v; 133 v = _application.createValueBinding("#{a.b.name}").getValue( 134 _facesContext); 135 assertEquals(B.NAME, v); 136 } 137 138 public void testGetValueWithShortName4() throws Exception 139 { 140 Object v; 141 v = _application.createValueBinding("#{a.b.c.name}").getValue( 142 _facesContext); 143 assertEquals(C.DEFAULT_NAME, v); 144 } 145 146 public void testGetValue() throws Exception 147 { 148 ValueBinding vb; 149 Object r; 150 151 vb = _application.createValueBinding("#{testmap}"); 152 r = vb.getValue(_facesContext); 153 assertEquals(_m, r); 154 155 vb = _application.createValueBinding("#{true ? testmap : testmap.f}"); 156 r = vb.getValue(_facesContext); 157 assertEquals(_m, r); 158 159 vb = _application.createValueBinding("#{testmap.f}"); 160 r = vb.getValue(_facesContext); 161 assertFalse(((Boolean ) r).booleanValue()); 162 163 vb = _application.createValueBinding("#{false ? testmap : testmap.f}"); 164 r = vb.getValue(_facesContext); 165 assertFalse(((Boolean ) r).booleanValue()); 166 167 vb = _application.createValueBinding("#{testmap.t}"); 168 r = vb.getValue(_facesContext); 169 assertTrue(((Boolean ) r).booleanValue()); 170 171 vb = _application.createValueBinding("#{testmap[\"o\"]['obj']}"); 172 r = vb.getValue(_facesContext); 173 assertEquals("OBJECT", r); 174 175 vb = _application 176 .createValueBinding("#{ testmap [ \"o\" ] [ 'obj' ] }"); 177 r = vb.getValue(_facesContext); 178 assertEquals("OBJECT", r); 179 180 vb = _application.createValueBinding("#{testmap.true_}"); 181 r = vb.getValue(_facesContext); 182 assertEquals("TRUE_", r); 183 184 vb = _application.createValueBinding("#{ testmap . true_ }"); 185 r = vb.getValue(_facesContext); 186 assertEquals("TRUE_", r); 187 188 vb = _application.createValueBinding("#{testmap[\"true_\"]}"); 189 r = vb.getValue(_facesContext); 190 assertEquals("TRUE_", r); 191 192 vb = _application.createValueBinding("#{testmap['true_']}"); 193 r = vb.getValue(_facesContext); 194 assertEquals("TRUE_", r); 195 196 vb = _application.createValueBinding("#{testmap[testmap.t]}"); 197 r = vb.getValue(_facesContext); 198 assertEquals("TRUE", r); 199 200 vb = _application 201 .createValueBinding("#{ testmap [ testmap . t ] }"); 202 r = vb.getValue(_facesContext); 203 assertEquals("TRUE", r); 204 205 vb = _application.createValueBinding("#{testmap.false_}"); 206 r = vb.getValue(_facesContext); 207 assertEquals("FALSE_", r); 208 209 vb = _application.createValueBinding("#{testmap[testmap.f]}"); 210 r = vb.getValue(_facesContext); 211 assertEquals("FALSE", r); 212 213 vb = _application.createValueBinding("#{testmap['map']}"); 214 r = vb.getValue(_facesContext); 215 assertEquals(_m, r); 216 217 vb = _application.createValueBinding("#{testmap[\"David's\"]}"); 218 r = vb.getValue(_facesContext); 219 assertEquals(_m, r); 220 221 vb = _application.createValueBinding("#{testmap['David\\'s']}"); 222 r = vb.getValue(_facesContext); 223 assertEquals(_m, r); 224 225 vb = _application.createValueBinding("#{testmap['my]bracket[']}"); 226 r = vb.getValue(_facesContext); 227 assertEquals(_m, r); 228 229 vb = _application.createValueBinding("#{testmap[\"my]bracket[\"]}"); 230 r = vb.getValue(_facesContext); 231 assertEquals(_m, r); 232 233 vb = _application.createValueBinding("#{testmap[\"my\\\\]bracket[\"]}"); 234 r = vb.getValue(_facesContext); 235 assertEquals(_m, r); 236 237 vb = _application.createValueBinding("#{testmap[0][0]}"); 238 r = vb.getValue(_facesContext); 239 assertEquals(new Integer (0), r); 240 241 vb = _application.createValueBinding("#{ testmap [ 0 ] [ 0 ]}"); 242 r = vb.getValue(_facesContext); 243 assertEquals(new Integer (0), r); 244 245 vb = _application.createValueBinding("#{testmap.o0.obj[0]}"); 246 r = vb.getValue(_facesContext); 247 assertEquals(new Integer (0), r); 248 249 vb = _application.createValueBinding("#{testmap.o1.obj[0][0]}"); 250 r = vb.getValue(_facesContext); 251 assertEquals(new Integer (0), r); 252 253 vb = _application.createValueBinding("#{testmap.list[0][0]}"); 254 r = vb.getValue(_facesContext); 255 assertEquals(new Integer (0), r); 256 257 vb = _application.createValueBinding("#{testmap.list[1][0]}"); 258 r = vb.getValue(_facesContext); 259 assertEquals(_a0, r); 260 261 vb = _application.createValueBinding("#{testmap.list[4][0][0]}"); 262 r = vb.getValue(_facesContext); 263 assertEquals(new Integer (0), r); 264 265 vb = _application.createValueBinding("#{testmap.list[4][1][0][0]}"); 266 r = vb.getValue(_facesContext); 267 assertEquals(new Integer (0), r); 268 269 vb = _application 270 .createValueBinding("#{testmap.list[4].list[1][0][0]}"); 271 r = vb.getValue(_facesContext); 272 assertEquals(new Integer (0), r); 273 274 vb = _application 275 .createValueBinding("#{testmap.map.map.list[4].list[4][1][0][0]}"); 276 r = vb.getValue(_facesContext); 277 assertEquals(new Integer (0), r); 278 279 vb = _application 280 .createValueBinding("#{testmap.map.list[4].map.list[4][1][0][0]}"); 281 r = vb.getValue(_facesContext); 282 assertEquals(new Integer (0), r); 283 284 vb = _application 285 .createValueBinding("#{testmap.list[4][testmap.list[4][0][0]][0]}"); 286 r = vb.getValue(_facesContext); 287 assertEquals(new Integer (0), r); 288 289 vb = _application 290 .createValueBinding("#{testmap.list[4][1][0][testmap.list[4][0][0]]}"); 291 r = vb.getValue(_facesContext); 292 assertEquals(new Integer (0), r); 293 294 vb = _application 295 .createValueBinding("#{testmap.list[4].list[testmap.list[4][1][0][testmap.list[4][0][1]]][0][0]}"); 296 r = vb.getValue(_facesContext); 297 assertEquals(new Integer (0), r); 298 299 vb = _application 300 .createValueBinding("#{testmap.map.map.list[4].list[4][1][testmap.list[4].list[testmap.list[4][1][0][testmap.list[4][0][testmap.t]]][0][0]][0]}"); 301 r = vb.getValue(_facesContext); 302 assertEquals(new Integer (0), r); 303 304 vb = _application 305 .createValueBinding("#{testmap['my]bracket[']['map'].list[4]['map'][\"list\"][4][1][testmap.map.map.list[4].list[4][1][testmap.list[4].list[testmap.list[4][1][testmap.f][testmap['list'][4][0][1]]][0][0]][0]][0]}"); 306 r = vb.getValue(_facesContext); 307 assertEquals(new Integer (0), r); 308 309 vb = _application 310 .createValueBinding("#{testmap['my]bracket[']['map'].list[4]['map'][\"list\"][4][1][testmap.map.map.list[4].list[4][1][testmap.list[4].list[testmap.list[4][1][testmap.f][testmap['list'][4][0][1]]][0][0]][0]][0]}"); 311 r = vb.getValue(_facesContext); 312 assertEquals(new Integer (0), r); 313 314 vb = _application 315 .createValueBinding("#{testmap['my\\\\]bracket[']['map'].list[4]['map'][\"list\"][4][1][testmap.map.map.list[4].list[4][1][testmap.list[4].list[testmap.list[4][1][testmap.f][testmap['list'][4][0][1]]][0][0]][0]][0]}"); 316 r = vb.getValue(_facesContext); 317 assertEquals(new Integer (0), r); 318 319 vb = _application.createValueBinding("#{testmap[\"\\\\]true[\"]}"); 320 r = vb.getValue(_facesContext); 321 assertEquals("_TRUE_", r); 322 323 vb = _application.createValueBinding("#{testmap['\\\\]false[']}"); 324 r = vb.getValue(_facesContext); 325 assertEquals("_FALSE_", r); 326 327 try 329 { 330 vb = _application 331 .createValueBinding("#{testmap['my\\\\]bracket[']['map'].list[4]['map'][\"list\"][4][1][testmap.map.map.list[4].list[4][1][testmap.list[4].list[testmap.list[4][1][testmap.f][testmap['list'][4][0][1]]][0][0]][0][0]}"); 332 assertTrue(false); 333 } 334 catch (ReferenceSyntaxException e) 335 { 336 } 339 340 try 341 { 342 vb = _application 343 .createValueBinding("#{testmap.map.list[4].map.list[][1][0][0]}"); 344 assertTrue(false); 345 } 346 catch (ReferenceSyntaxException e) 347 { 348 } 351 352 try 353 { 354 vb = _application 355 .createValueBinding("#{testmap.map.list[4].map.list.[4][1][0][0]}"); 356 assertTrue(false); 357 } 358 catch (ReferenceSyntaxException e) 359 { 360 } 363 364 try 365 { 366 vb = _application 367 .createValueBinding("#{testmap.map.list[4].map..list[4][1][0][0]}"); 368 assertTrue(false); 369 } 370 catch (ReferenceSyntaxException e) 371 { 372 } 375 376 try 377 { 378 vb = _application 379 .createValueBinding("#{.testmap.map.list[4].map.list[4][1][0][0]}"); 380 assertTrue(false); 381 } 382 catch (ReferenceSyntaxException e) 383 { 384 } 387 388 try 389 { 390 vb = _application 391 .createValueBinding("#{testmap.map.list[4].map.list[4[1][0]['0']}"); 392 assertTrue(false); 393 } 394 catch (ReferenceSyntaxException e) 395 { 396 } 399 400 try 401 { 402 vb = _application 403 .createValueBinding("#{testmap.map.list[4].map.list[4][1][0].[0]}"); 404 assertTrue(false); 405 } 406 catch (ReferenceSyntaxException e) 407 { 408 } 411 } 412 413 public void testMixedExpressions() 414 { 415 ValueBinding vb; 416 Object r; 417 418 vb = _application 419 .createValueBinding("This is one #{testmap[\"o\"]['obj']}"); 420 r = vb.getValue(_facesContext); 421 assertEquals("This is one OBJECT", r); 422 423 vb = _application 424 .createValueBinding("#{ testmap [ \"o\" ] [ 'obj' ] } is the One."); 425 r = vb.getValue(_facesContext); 426 assertEquals("OBJECT is the One.", r); 427 428 vb = _application.createValueBinding("Is it really #{testmap.true_}?"); 429 r = vb.getValue(_facesContext); 430 assertEquals("Is it really TRUE_?", r); 431 432 vb = _application 433 .createValueBinding("#{testmap . true_} or #{testmap.false_}, that's the question!"); 434 r = vb.getValue(_facesContext); 435 assertEquals("TRUE_ or FALSE_, that's the question!", r); 436 437 vb = _application 438 .createValueBinding("What? #{testmap . true_} or #{testmap.false_}, that's the question!"); 439 r = vb.getValue(_facesContext); 440 assertEquals("What? TRUE_ or FALSE_, that's the question!", r); 441 442 vb = _application 443 .createValueBinding("What? ${ #{testmap . true_} or #{testmap.false_}, that's the question! }"); 444 r = vb.getValue(_facesContext); 445 assertEquals("What? ${ TRUE_ or FALSE_, that's the question! }", r); 446 447 vb = _application 448 .createValueBinding("#{ '#{' } What? ${ #{true ? '${' : \"#{\\\\\"} #{testmap . true_} or #{testmap.false_}, that's the question! }"); 449 r = vb.getValue(_facesContext); 450 assertEquals("#{ What? ${ ${ TRUE_ or FALSE_, that's the question! }", 451 r); 452 453 } 458 459 public void testIsReadOnly() throws Exception 460 { 461 ValueBinding vb; 462 463 vb = _application.createValueBinding("#{'constant literal'}"); 464 assertTrue(vb.isReadOnly(_facesContext)); 465 466 vb = _application.createValueBinding("#{cookie}"); 467 assertTrue(vb.isReadOnly(_facesContext)); 468 469 vb = _application.createValueBinding("#{requestScope}"); 470 assertTrue(vb.isReadOnly(_facesContext)); 471 472 vb = _application.createValueBinding("#{a.name}"); 473 assertTrue(vb.isReadOnly(_facesContext)); 474 475 vb = _application.createValueBinding("#{theA.theB.name}"); 476 assertFalse(vb.isReadOnly(_facesContext)); 477 478 vb = _application.createValueBinding("#{testmap}"); 479 assertFalse(vb.isReadOnly(_facesContext)); 480 481 vb = _application.createValueBinding("#{testmap.f}"); 482 assertFalse(vb.isReadOnly(_facesContext)); 483 484 vb = _application.createValueBinding("#{ testmap [ 0 ] [ 0 ]}"); 485 assertFalse(vb.isReadOnly(_facesContext)); 486 487 vb = _application.createValueBinding("#{true ? cookie : max}"); 488 assertTrue(vb.isReadOnly(_facesContext)); 489 490 vb = _application.createValueBinding("#{false ? cookie : max}"); 491 assertFalse(vb.isReadOnly(_facesContext)); 492 493 496 vb = _application.createValueBinding("#{nonExistentValueBlahBlahBlah}"); 497 assertFalse(vb.isReadOnly(_facesContext)); 498 499 vb = _application 500 .createValueBinding("#{nonExistingValueBlahBlahBlah.blah}"); 501 assertFalse(vb.isReadOnly(_facesContext)); 502 503 vb = _application 504 .createValueBinding("#{nonExistingValueBlahBlahBlah.blah.blah}"); 505 assertFalse(vb.isReadOnly(_facesContext)); 506 } 507 508 public void testGetType() throws Exception 509 { 510 ValueBinding vb; 511 512 515 vb = _application.createValueBinding("#{'constant literal'}"); 516 assertSame(String .class, vb.getType(_facesContext)); 517 518 vb = _application.createValueBinding("#{false && true}"); 519 assertSame(Boolean .class, vb.getType(_facesContext)); 520 521 vb = _application 522 .createValueBinding("#{applicationScope.bean == null}"); 523 assertSame(Boolean .class, vb.getType(_facesContext)); 524 525 vb = _application.createValueBinding("#{cookie}"); 528 assertTrue(Map .class.isAssignableFrom(vb.getType(_facesContext))); 529 530 vb = _application.createValueBinding("#{requestScope}"); 531 assertTrue(Map .class.isAssignableFrom(vb.getType(_facesContext))); 532 533 vb = _application.createValueBinding("#{true ? cookie : max}"); 534 assertTrue(Map .class.isAssignableFrom(vb.getType(_facesContext))); 535 536 vb = _application.createValueBinding("#{false ? cookie : max}"); 538 assertSame(Object .class, vb.getType(_facesContext)); 539 540 543 vb = _application.createValueBinding("#{a.name}"); 544 assertSame(A.NAME.getClass(), vb.getType(_facesContext)); 545 546 vb = _application.createValueBinding("#{theA.theB.name}"); 547 assertSame(B.NAME.getClass(), vb.getType(_facesContext)); 548 549 vb = _application.createValueBinding("#{testmap}"); 550 assertSame(_m.getClass(), vb.getType(_facesContext)); 551 552 vb = _application.createValueBinding("#{testmap.f}"); 553 assertSame(Boolean .class, vb.getType(_facesContext)); 554 555 vb = _application.createValueBinding("#{ testmap [ 0 ] [ 1 ]}"); 556 assertSame(Object .class, vb.getType(_facesContext)); 558 559 562 vb = _application.createValueBinding("#{nonExistentValueBlahBlahBlah}"); 563 assertSame(Object .class, vb.getType(_facesContext)); 564 565 vb = _application 566 .createValueBinding("#{nonExistingValueBlahBlahBlah.blah}"); 567 assertSame(null, vb.getType(_facesContext)); 568 569 vb = _application 570 .createValueBinding("#{nonExistingValueBlahBlahBlah.blah.blah}"); 571 assertSame(null, vb.getType(_facesContext)); 572 } 573 574 public void testManagedBean() throws Exception 575 { 576 ValueBinding vb; 577 578 vb = _application.createValueBinding("#{testBean_B.name}"); 579 assertEquals("testName", vb.getValue(_facesContext)); 580 581 vb = _application.createValueBinding("#{testBean_B.int}"); 582 assertEquals(new Integer (1), vb.getValue(_facesContext)); 583 584 vb = _application.createValueBinding("#{testBean_B.double}"); 585 assertEquals(new Double (1.1), vb.getValue(_facesContext)); 586 587 vb = _application.createValueBinding("#{testBean_B.integer}"); 588 assertEquals(new Integer (2), vb.getValue(_facesContext)); 589 590 vb = _application.createValueBinding("#{testBean_B.double2}"); 591 assertEquals(new Double (2.2), vb.getValue(_facesContext)); 592 593 vb = _application.createValueBinding("#{testBean_B1.double2}"); 594 assertEquals(new Double (2.2), vb.getValue(_facesContext)); 595 596 vb = _application.createValueBinding("#{testBean_B2.name}"); 597 assertEquals(B.NAME, vb.getValue(_facesContext)); 598 599 vb = _application.createValueBinding("#{testBean_B2.int}"); 600 assertEquals(new Integer (-1), vb.getValue(_facesContext)); 601 602 vb = _application.createValueBinding("#{testBean_B2.double}"); 603 assertEquals(new Double (-1.1), vb.getValue(_facesContext)); 604 605 vb = _application.createValueBinding("#{testBean_B2.integer}"); 606 assertEquals(new Integer (-2), vb.getValue(_facesContext)); 607 608 vb = _application.createValueBinding("#{testBean_B2.double2}"); 609 assertEquals(new Double (-2.2), vb.getValue(_facesContext)); 610 611 vb = _application.createValueBinding("#{testBean_E.a}"); 612 assertEquals("foo", vb.getValue(_facesContext)); 613 614 vb = _application.createValueBinding("#{testBean_E.b}"); 615 assertEquals("bar", vb.getValue(_facesContext)); 616 617 vb = _application.createValueBinding("#{testBean_E.a}#{testBean_E.b}"); 618 assertEquals("foobar", vb.getValue(_facesContext)); 619 620 vb = _application.createValueBinding("#{testBean_E.c['foo']}"); 621 assertEquals("bar", vb.getValue(_facesContext)); 622 623 vb = _application.createValueBinding("#{!empty testBean_E.d}"); 624 assertFalse("false", Boolean.getBoolean(vb.getValue(_facesContext) 625 .toString())); 626 627 } 628 629 public void testEL() throws Exception 630 { 631 ValueBinding vb; 632 633 vb = _application 634 .createValueBinding("#{facesContext.application.defaultRenderKitId}"); 635 assertEquals("HTML_BASIC", vb.getValue(_facesContext)); 636 637 638 641 } 644 } | Popular Tags |