1 package org.jacorb.test.notification.node; 2 3 import junit.framework.Test; 4 5 import org.jacorb.notification.MessageFactory; 6 import org.jacorb.notification.filter.EvaluationContext; 7 import org.jacorb.notification.filter.EvaluationException; 8 import org.jacorb.notification.filter.EvaluationResult; 9 import org.jacorb.notification.filter.FilterConstraint; 10 import org.jacorb.notification.filter.ParseException; 11 import org.jacorb.notification.filter.etcl.AbstractTCLNode; 12 import org.jacorb.notification.filter.etcl.ETCLComponentName; 13 import org.jacorb.notification.filter.etcl.ETCLFilterConstraint; 14 import org.jacorb.notification.filter.etcl.StaticTypeChecker; 15 import org.jacorb.notification.filter.etcl.StaticTypeException; 16 import org.jacorb.notification.filter.etcl.TCLCleanUp; 17 import org.jacorb.notification.filter.etcl.TCLParser; 18 import org.jacorb.notification.impl.DefaultMessageFactory; 19 import org.jacorb.notification.interfaces.Message; 20 import org.jacorb.test.notification.Address; 21 import org.jacorb.test.notification.NamedValue; 22 import org.jacorb.test.notification.NamedValueSeqHelper; 23 import org.jacorb.test.notification.NotificationTestCase; 24 import org.jacorb.test.notification.NotificationTestCaseSetup; 25 import org.jacorb.test.notification.NotificationTestUtils; 26 import org.jacorb.test.notification.Person; 27 import org.jacorb.test.notification.PersonHelper; 28 import org.jacorb.test.notification.Profession; 29 import org.jacorb.test.notification.TestUnion; 30 import org.jacorb.test.notification.TestUnionHelper; 31 import org.omg.CORBA.Any ; 32 import org.omg.CosNotification.Property; 33 import org.omg.CosNotification.PropertySeqHelper; 34 import org.omg.CosNotification.StructuredEvent; 35 import org.omg.CosNotification.StructuredEventHelper; 36 import org.omg.TimeBase.UtcT; 37 import org.omg.TimeBase.UtcTHelper; 38 39 44 45 public class TCLTest extends NotificationTestCase 46 { 47 private NotificationTestUtils testUtils_; 48 49 52 private Any testPerson_; 53 54 private Any testUnion_; 55 56 private Any testUnion1_; 57 58 private Any testUnion2_; 59 60 private Any testUnion3_; 61 62 private Any testUnion4_; 63 64 private Any testUnion5_; 65 66 private final String [] visitorTestExpressions_ = new String [] { "$.value < 1", "$.value != 1", 67 "$.value <= 1", "$.value > 1", "$.value >= 1", "$.value == 1", "$.value + 1", 68 "$.value - 1", "$.value * 1", "$.value / 1", "$.value ~ $.x" }; 69 70 private void setUpTestUnion(Person person) 71 { 72 TestUnion _t1, _t2, _t3, _t4, _t5, _t; 73 74 _t = new TestUnion(); 75 _t.default_person(person); 76 77 _t1 = new TestUnion(); 78 _t1.long_(100); 79 80 _t2 = new TestUnion(); 81 _t2.string_("String"); 82 83 _t3 = new TestUnion(); 84 _t3.named_value_(new NamedValue("this is the name", "this is the value")); 85 86 _t4 = new TestUnion(); 87 _t4.person_(person); 88 89 _t5 = new TestUnion(); 90 NamedValue[] lonv = new NamedValue[1]; 91 lonv[0] = new NamedValue("name", "value"); 92 _t5.named_value_array(lonv); 93 94 testUnion_ = getORB().create_any(); 95 testUnion1_ = getORB().create_any(); 96 testUnion2_ = getORB().create_any(); 97 testUnion3_ = getORB().create_any(); 98 testUnion4_ = getORB().create_any(); 99 testUnion5_ = getORB().create_any(); 100 101 TestUnionHelper.insert(testUnion_, _t); 102 TestUnionHelper.insert(testUnion1_, _t1); 103 TestUnionHelper.insert(testUnion2_, _t2); 104 TestUnionHelper.insert(testUnion3_, _t3); 105 TestUnionHelper.insert(testUnion4_, _t4); 106 TestUnionHelper.insert(testUnion5_, _t5); 107 } 108 109 private Person setUpPerson() 110 { 111 Person _person = new Person(); 112 Address _address = new Address(); 113 NamedValue _nv1 = new NamedValue(); 114 NamedValue _nv2 = new NamedValue(); 115 Profession _p = Profession.STUDENT; 116 117 _person.first_name = "Firstname"; 118 _person.last_name = "Lastname"; 119 _person.age = 29; 120 _person.phone_numbers = new String [2]; 121 _person.phone_numbers[0] = "32132132"; 122 _person.phone_numbers[1] = ""; 123 _person.nv = new NamedValue[2]; 124 _person.person_profession = _p; 125 126 _address.street = "Takustr."; 127 _address.number = 9; 128 _address.city = "Berlin"; 129 130 _person.home_address = _address; 131 132 _nv1.name = "priority"; 133 _nv1.value = "Very High"; 134 _person.nv[0] = _nv1; 135 136 _nv2.name = "stuff"; 137 _nv2.value = "not important"; 138 _person.nv[1] = _nv2; 139 140 _person.aliases = new String [] { "Alias0", "Alias1", "Alias2" }; 141 _person.numbers = new int[] { 10, 20, 30, 40, 50 }; 142 143 testPerson_ = getORB().create_any(); 144 PersonHelper.insert(testPerson_, _person); 145 return (_person); 146 } 147 148 public void setUpTest() throws Exception 149 { 150 testUtils_ = new NotificationTestUtils(getORB()); 151 152 Person _person = setUpPerson(); 153 154 setUpTestUnion(_person); 155 } 156 157 public TCLTest(String name, NotificationTestCaseSetup setup) 158 { 159 super(name, setup); 160 } 161 162 165 private void runStaticTypeCheck(String expr) throws Exception 166 { 167 runStaticTypeCheck(expr, true); 168 } 169 170 private void runStaticTypeCheck(String expr, boolean shouldFail) throws Exception 171 { 172 try 173 { 174 AbstractTCLNode _root = TCLParser.parse(expr); 175 176 StaticTypeChecker _checker = new StaticTypeChecker(); 177 178 _checker.check(_root); 179 180 if (shouldFail) 181 { 182 fail("static type error should have occcured"); 183 } 184 } catch (StaticTypeException ste) 185 { 186 } 188 } 189 190 201 private void runEvaluation(String expect, String expression) throws Exception 202 { 203 AbstractTCLNode fstNode = TCLParser.parse(expect); 204 AbstractTCLNode sndNode = TCLParser.parse(expression); 205 206 EvaluationContext _context = new EvaluationContext(getEvaluator()); 207 208 EvaluationResult pre_res = fstNode.evaluate(_context); 209 EvaluationResult pst_res = sndNode.evaluate(_context); 210 211 assertEquals("expected " + fstNode.toStringTree() + " == " + sndNode.toStringTree(), 212 pre_res, pst_res); 213 } 214 215 218 public void testPLUS() throws Exception 219 { 220 runEvaluation("2", "1 + 1"); 221 runEvaluation("0", "1 + -1"); 222 runEvaluation("0", "-1 + 1"); 223 runEvaluation("2", "1 + 1.0"); 224 runEvaluation("2.0", "1 + 1"); 225 } 226 227 public void testMINUS() throws Exception 228 { 229 runEvaluation("0", "1 - 1"); 230 runEvaluation("2", "1 - -1"); 231 runEvaluation("-2", "-1 - 1"); 232 runEvaluation("0", "1 - 1.0"); 233 runEvaluation("0.0", "1 - 1"); 234 } 235 236 public void testDIV() throws Exception 237 { 238 runEvaluation("5", "10/2"); 239 runEvaluation("1/3", "10/30"); 240 runEvaluation("0.25", "1.0/4"); 241 } 242 243 public void testMULT() throws Exception 244 { 245 runEvaluation("100", "10*10"); 246 runEvaluation("1", "1 * 1"); 247 runEvaluation("1", "0.25 * 4"); 248 runEvaluation("-1", "1 * -1"); 249 } 250 251 public void testSimpleNumbers() throws Exception 252 { 253 runEvaluation("5", "5"); 254 runEvaluation("-1", "-1"); 255 runEvaluation("1", "+1"); 256 runEvaluation("1", "1.0"); 257 runEvaluation("1", "+1.0"); 258 runEvaluation("-1", "-1.0"); 259 runEvaluation(".1", "+.1"); 260 runEvaluation("-0.1", "-.1"); 261 } 262 263 public void testFloatNumbers() throws Exception 264 { 265 runEvaluation("1000", "10e+2"); 266 runEvaluation("100", "10e+1"); 267 runEvaluation("10", "10e+0"); 268 runEvaluation("1", "10e-1"); 269 runEvaluation(".1", "10e-2"); 270 runEvaluation(".01", "10e-3"); 271 runEvaluation("-.01", "-10e-3"); 272 } 273 274 public void testSimpleOperations() throws Exception 275 { 276 runEvaluation("0", "1-1"); 277 runEvaluation("2", "1+1"); 278 runEvaluation("1", "1*1"); 279 runEvaluation("1", "1/1"); 280 } 281 282 public void testParentheses() throws Exception 283 { 284 runEvaluation("7", "1+2*3"); 285 runEvaluation("7", "1+(2*3)"); 286 runEvaluation("1+2*3", "1+(2*3)"); 287 runEvaluation("9", "(1+2)*3"); 288 runEvaluation("1+(2+(3+(4+5)))", "(((1+2)+3)+4)+5"); 289 runEvaluation("1*(2*(3*(4*5)))", "(((1*2)*3)*4)*5"); 290 } 291 292 public void testGT() throws Exception 293 { 294 runEvaluation("TRUE", "1>0"); 295 runEvaluation("FALSE", "0>1"); 296 297 runEvaluation("TRUE", "TRUE > FALSE"); 298 runEvaluation("FALSE", "FALSE > TRUE"); 299 300 runEvaluation("TRUE", "'bbb' > 'aaa'"); 301 runEvaluation("FALSE", "'bbb' > 'ccc'"); 302 } 303 304 public void testLT() throws Exception 305 { 306 runEvaluation("FALSE", "1<0"); 307 runEvaluation("TRUE", "0<1"); 308 309 runEvaluation("FALSE", "TRUE < FALSE"); 310 runEvaluation("TRUE", "FALSE < TRUE"); 311 312 runEvaluation("FALSE", "'bbb' < 'aaa'"); 313 runEvaluation("TRUE", "'bbb' < 'ccc'"); 314 } 315 316 public void testLTE() throws Exception 317 { 318 runEvaluation("TRUE", "0<=1"); 319 runEvaluation("TRUE", "0<=0"); 320 runEvaluation("FALSE", "1<=0"); 321 runEvaluation("TRUE", "'abc'<='abc'"); 322 runEvaluation("TRUE", "'abc'<='dbc'"); 323 runEvaluation("FALSE", "'bbc'<='abc'"); 324 } 325 326 public void testGTE() throws Exception 327 { 328 runEvaluation("FALSE", "0>=1"); 329 runEvaluation("TRUE", "0>=0"); 330 runEvaluation("TRUE", "1>=0"); 331 runEvaluation("TRUE", "'abc'>='abc'"); 332 runEvaluation("FALSE", "'abc'>='dbc'"); 333 runEvaluation("TRUE", "'bbc'>='abc'"); 334 } 335 336 public void testEQ() throws Exception 337 { 338 runEvaluation("TRUE", "TRUE == TRUE"); 339 runEvaluation("TRUE", "FALSE == FALSE"); 340 runEvaluation("FALSE", "FALSE == TRUE"); 341 runEvaluation("FALSE", "TRUE == FALSE"); 342 } 343 344 public void testNEQ() throws Exception 345 { 346 runEvaluation("TRUE", "0!=1"); 347 runEvaluation("FALSE", "1!=1"); 348 349 runEvaluation("TRUE", "'bla'!='blubb'"); 350 runEvaluation("FALSE", "'bla'!='bla'"); 351 352 runEvaluation("TRUE", "TRUE!=FALSE"); 353 runEvaluation("TRUE", "FALSE!=TRUE"); 354 runEvaluation("FALSE", "TRUE!=TRUE"); 355 runEvaluation("FALSE", "FALSE!=FALSE"); 356 } 357 358 public void testAND() throws Exception 359 { 360 runEvaluation("TRUE", "TRUE and TRUE"); 361 runEvaluation("FALSE", "TRUE and FALSE"); 362 runEvaluation("FALSE", "FALSE and TRUE"); 363 runEvaluation("FALSE", "FALSE and FALSE"); 364 } 365 366 public void testOR() throws Exception 367 { 368 runEvaluation("TRUE", "TRUE or TRUE"); 369 runEvaluation("TRUE", "TRUE or FALSE"); 370 runEvaluation("TRUE", "FALSE or TRUE"); 371 runEvaluation("FALSE", "FALSE or FALSE"); 372 } 373 374 public void testNOT() throws Exception 375 { 376 runEvaluation("TRUE", "not FALSE"); 377 runEvaluation("FALSE", "not TRUE"); 378 } 379 380 public void testLazyEval() throws Exception 381 { 382 try 383 { 384 runEvaluation("TRUE", "1/0"); 385 fail("Division by zero should cause an exception"); 386 } catch (EvaluationException e) 387 { 388 } 390 391 runEvaluation("TRUE", "TRUE or (1/0)"); 392 393 runEvaluation("FALSE", "FALSE and (1/0)"); 394 } 395 396 public void testTwiddle() throws Exception 397 { 398 runEvaluation("TRUE", "'substr' ~ 'substring'"); 399 runEvaluation("FALSE", "'not' ~ 'substring'"); 400 } 401 402 public void testCast() throws Exception 403 { 404 runEvaluation("FALSE", "2/3 > 0"); 405 runEvaluation("TRUE", "(1.0 * 2/3) > 0"); 406 } 407 408 public void testTypeConversion() throws Exception 409 { 410 runEvaluation("TRUE", "'H' + 1 > 32"); 411 } 412 413 public void testStaticTypeCheck() throws Exception 414 { 415 runStaticTypeCheck("5 + 'al'"); 416 runStaticTypeCheck("5 + 'a'", false); 417 runStaticTypeCheck("5 ~ 'a'"); 418 runStaticTypeCheck("TRUE + 'a'"); 419 runStaticTypeCheck("'a' and 'b'"); 420 runStaticTypeCheck("1 * (TRUE and TRUE)", false); 421 } 422 423 public void testEnum() throws Exception 424 { 425 runEvaluation(testPerson_, "$.person_profession == STUDENT"); 426 } 427 428 public void testImplicit() throws Exception 429 { 430 runEvaluation(testPerson_, "$._type_id == 'Person'"); 431 432 runEvaluation(testPerson_, 433 "$._repos_id == 'IDL:jacorb.org/org/jacorb/test/notification/Person:1.0'"); 434 435 runEvaluation(testPerson_, "$.nv._length == 2"); 436 437 try 438 { 439 runEvaluation(testPerson_, "$.first_name._length == 2"); 440 fail(); 441 } catch (EvaluationException e) 442 { 443 } 445 446 runEvaluation(testPerson_, "$.phone_numbers._length == 2"); 447 448 runEvaluation(testPerson_, "$.4._length == 2"); 449 450 runEvaluation(testUnion5_, "$.named_value_array._length == 1"); 451 452 runEvaluation(testUnion5_, "$.(5)._length == 1"); 453 } 454 455 public void testDefault() throws Exception 456 { 457 runEvaluation(testUnion_, "default $._d and $.().first_name == 'Firstname'"); 458 } 459 460 public void testExist() throws Exception 461 { 462 runEvaluation(testPerson_, "exist $._type_id"); 463 runEvaluation(testPerson_, "exist $._type_id and $._type_id =='Person'"); 464 465 runEvaluation(testPerson_, "exist $._repos_id"); 466 467 runEvaluation(testUnion1_, "not exist $.1"); 468 runEvaluation(testUnion1_, "not exist $.0"); 469 runEvaluation(testUnion1_, "exist $.(0)"); 470 runEvaluation(testUnion1_, "exist $.(1)"); 471 472 runEvaluation(testPerson_, "exist $.first_name"); 473 runEvaluation(testPerson_, "not exist $.third_name"); 474 475 runEvaluation(testUnion1_, "exist $._d"); 476 runEvaluation(testPerson_, "not exist $._d"); 477 478 runEvaluation(testPerson_, "exist $.nv._length"); 479 runEvaluation(testPerson_, "exist $.5._length"); 480 runEvaluation(testPerson_, "not exist $.home_address._length"); 481 482 runEvaluation(testUnion1_, "exist $.long_"); 483 runEvaluation(testUnion1_, "exist $.(1)"); 484 485 runEvaluation(testUnion2_, "exist $.string_"); 486 runEvaluation(testUnion2_, "exist $.(2)"); 487 488 runEvaluation(testUnion3_, "exist $.named_value_"); 489 runEvaluation(testUnion3_, "exist $.(3)"); 490 491 runEvaluation(testUnion4_, "exist $.person_"); 492 runEvaluation(testUnion4_, "exist $.(4)"); 493 runEvaluation(testUnion4_, "exist $.(4).first_name"); 494 runEvaluation(testUnion4_, "exist $.(4).phone_numbers[0]"); 495 496 runEvaluation(testUnion_, "exist $.()"); 497 runEvaluation(testUnion_, "exist $.default_person"); 498 } 499 500 public void testUnion() throws Exception 501 { 502 runEvaluation(testUnion1_, "$.long_ > 54"); 503 runEvaluation(testUnion1_, "$._d == 1 and $.(0) == 100"); 504 runEvaluation(testUnion1_, "$._d == 1 and $.(1) == 100"); 505 506 try 507 { 508 runEvaluation(testUnion1_, "$.string_"); 509 fail(); 510 } catch (EvaluationException e) 511 { 512 } 514 515 runEvaluation(testUnion1_, "$.(1) > 54"); 516 517 runEvaluation(testUnion1_, "$.(0) > 54"); 518 519 runEvaluation(testUnion2_, "$.string_", "'String'"); 520 runEvaluation(testUnion2_, "$.(2)", "'String'"); 521 522 runEvaluation(testUnion3_, "$.named_value_.name", "'this is the name'"); 523 524 runEvaluation(testUnion3_, "$.(3).name", "'this is the name'"); 525 526 runEvaluation(testUnion4_, "$.person_.home_address.street == 'Takustr.'"); 527 528 runEvaluation(testUnion4_, "$.(4).home_address.street == 'Takustr.'"); 529 530 runEvaluation(testUnion4_, "$.(4).3.street == 'Takustr.'"); 531 runEvaluation(testUnion4_, "$.(4).3.0 == 'Takustr.'"); 532 runEvaluation(testUnion4_, "$.(4).home_address.0 == 'Takustr.'"); 533 534 runEvaluation(testUnion5_, "$.named_value_array(name)", "'value'"); 535 536 runEvaluation(testUnion5_, "$.(5)(name)", "'value'"); 537 538 runEvaluation(testUnion_, "$.().first_name", "'Firstname'"); 539 540 runEvaluation(testUnion_, "$.default_person.first_name", "'Firstname'"); 541 } 542 543 public void testComponent() throws Exception 544 { 545 runEvaluation(testPerson_, "$.first_name", "'Firstname'"); 546 547 try 548 { 549 runEvaluation(testPerson_, "$.third_name", "'does not exist'"); 550 fail(); 551 } catch (EvaluationException e) 552 { 553 } 555 556 runEvaluation(testPerson_, "$.0", "'Firstname'"); 557 558 560 runEvaluation(testPerson_, "$.home_address.street", "'Takustr.'"); 561 562 runEvaluation(testPerson_, "$.3.0", "'Takustr.'"); 563 564 566 runEvaluation(testPerson_, "$.first_name == 'Firstname'"); 567 568 runEvaluation(testPerson_, "$.age > 30", "FALSE"); 569 570 runEvaluation(testPerson_, "$.age > 20 and $.first_name =='Firstname'"); 571 572 runEvaluation(testPerson_, "$.age < 30 or $.first_name == 'Adalbert'"); 573 574 576 runEvaluation(testPerson_, "$.phone_numbers[0]", "'32132132'"); 577 578 runEvaluation(testPerson_, "$.4[0]", "'32132132'"); 579 580 582 runEvaluation(testPerson_, "'321' ~ $.phone_numbers[0]"); 583 584 586 runEvaluation(testPerson_, "$.nv(priority)", "'Very High'"); 587 588 runEvaluation(testPerson_, "$.5(priority)", "'Very High'"); 589 590 runEvaluation(testPerson_, "$.nv[0].name == 'priority' and $.nv[0].value == 'Very High'"); 591 592 594 runEvaluation(testPerson_, "$.nv(stuff) > 'aaa'"); 595 } 596 597 public void testDynamicTypeExceptions() throws Exception 598 { 599 601 try 602 { 603 runEvaluation(testPerson_, "$.first_name + 1 == 10"); 604 fail(); 605 } catch (EvaluationException e) 606 { 607 } 609 610 try 611 { 612 runEvaluation(testPerson_, "$.age == '29'"); 613 fail(); 614 } catch (EvaluationException e) 615 { 616 } 618 619 try 620 { 621 runEvaluation(testPerson_, "$.age and true"); 622 fail(); 623 } catch (EvaluationException e) 624 { 625 } 627 628 } 629 630 public void testParse() throws Exception 631 { 632 try 633 { 634 TCLParser.parse("$.."); 635 fail(); 636 } catch (ParseException e) 637 { 638 } 640 641 try 642 { 643 TCLParser.parse("ab +-/ abc"); 644 fail(); 645 } catch (ParseException e) 646 { 647 } 649 } 650 651 public void testShorthandNotation() throws Exception 652 { 653 Any _testData = testUtils_.getStructuredEventAny(); 654 655 runEvaluation(_testData, "$domain_name == 'TESTING'"); 656 runEvaluation(_testData, "$type_name == 'TESTING'"); 657 runEvaluation(_testData, "$event_name == 'ALARM'"); 658 659 StructuredEvent _structuredEvent = testUtils_.getStructuredEvent(); 660 661 runEvaluation(_structuredEvent, "$domain_name == 'TESTING'"); 662 runEvaluation(_structuredEvent, "$type_name == 'TESTING'"); 663 runEvaluation(_structuredEvent, "$event_name == 'ALARM'"); 664 } 665 666 public void testShorthandVariableHeader() throws Exception 667 { 668 StructuredEvent _structuredEvent = testUtils_.getStructuredEvent(); 669 670 Any _any = getORB().create_any(); 671 672 _structuredEvent.header.variable_header = new Property[2]; 673 674 _any.insert_long(10); 675 _structuredEvent.header.variable_header[0] = new Property("long", _any); 676 677 _any = getORB().create_any(); 678 _any.insert_string("text"); 679 _structuredEvent.header.variable_header[1] = new Property("string", _any); 680 681 runEvaluation(_structuredEvent, "$long == 10"); 682 runEvaluation(_structuredEvent, "$long == $.header.variable_header(long)"); 683 684 runEvaluation(_structuredEvent, "$string == 'text'"); 685 runEvaluation(_structuredEvent, "$string == $.header.variable_header(string)"); 686 } 687 688 public void testShorthandFilterableData() throws Exception 689 { 690 StructuredEvent _structuredEvent = testUtils_.getStructuredEvent(); 691 692 Any _any = getORB().create_any(); 693 694 Property[] _props = new Property[2]; 695 _any.insert_long(10); 696 _props[0] = new Property("long", _any); 697 698 _any = getORB().create_any(); 699 _any.insert_string("text"); 700 _props[1] = new Property("string", _any); 701 702 _structuredEvent.filterable_data = _props; 703 704 runEvaluation(_structuredEvent, "$long == 10"); 705 runEvaluation(_structuredEvent, "$long == $.filterable_data(long)"); 706 707 runEvaluation(_structuredEvent, "$string == 'text'"); 708 runEvaluation(_structuredEvent, "$string == $.filterable_data(string)"); 709 } 710 711 public void testShorthandDefault() throws Exception 712 { 713 runEvaluation(testPerson_, "$first_name == $.first_name"); 714 } 715 716 public void testShorthandDefaultAny() throws Exception 717 { 718 NamedValue[] _nv = new NamedValue[2]; 719 _nv[0] = new NamedValue("name1", "value1"); 720 _nv[1] = new NamedValue("name2", "value2"); 721 722 Any _a = getORB().create_any(); 723 NamedValueSeqHelper.insert(_a, _nv); 724 runEvaluation(_a, "$name1 == $(name1)"); 725 runEvaluation(_a, "$name2 == $(name2)"); 726 } 727 728 public void testInsertComponentName() throws Exception 729 { 730 ETCLComponentName _comp = (ETCLComponentName) TCLParser.parse("$.first_name.last_name"); 731 732 _comp.acceptInOrder(new TCLCleanUp()); 733 assertEquals("$.first_name.last_name", _comp.getComponentName()); 734 735 AbstractTCLNode _root = TCLParser.parse("$.first_name.value + 5"); 736 737 _root.acceptInOrder(new TCLCleanUp()); 738 _comp = (ETCLComponentName) _root.getFirstChild(); 739 assertEquals("$.first_name.value", _comp.getComponentName()); 740 741 _comp = (ETCLComponentName) TCLParser.parse("$domain_name"); 742 _comp.acceptInOrder(new TCLCleanUp()); 743 assertEquals("$domain_name", _comp.getComponentName()); 744 745 _comp = (ETCLComponentName) TCLParser.parse("$domain_name._type_id"); 746 _comp.acceptInOrder(new TCLCleanUp()); 747 assertEquals("$domain_name._type_id", _comp.getComponentName()); 748 749 _comp = (ETCLComponentName) TCLParser.parse("$.(1)"); 750 _comp.acceptInOrder(new TCLCleanUp()); 751 assertEquals("$.(1)", _comp.getComponentName()); 752 753 _comp = (ETCLComponentName) TCLParser.parse("$.()"); 754 _comp.acceptInOrder(new TCLCleanUp()); 755 assertEquals("$.(default)", _comp.getComponentName()); 756 } 757 758 public void testInOperator() throws Exception 759 { 760 runEvaluation(testPerson_, "'Alias0' in $.aliases"); 761 runEvaluation(testPerson_, "'Alias1' in $.aliases"); 762 runEvaluation(testPerson_, "'Alias2' in $.aliases"); 763 runEvaluation(testPerson_, "not ('AliasXYZ' in $.aliases)"); 764 runEvaluation(testPerson_, "10 in $.numbers"); 765 runEvaluation(testPerson_, "not (25 in $.numbers)"); 766 } 767 768 public void testPassOverUnnamedLayers() throws Exception 769 { 770 Any _any = getORB().create_any(); 771 _any.insert_any(testPerson_); 772 773 runEvaluation(_any, "$.first_name == 'Firstname'"); 774 775 Any _any2 = getORB().create_any(); 776 _any2.insert_any(_any); 777 778 runEvaluation(_any, "$.first_name == 'Firstname'"); 779 780 runEvaluation(_any, "$ == 'FirstName'", "FALSE"); 781 782 Any _any3 = getORB().create_any(); 783 _any3.insert_long(10); 784 785 runEvaluation(_any3, "$ == 10"); 786 787 try 788 { 789 runEvaluation(_any3, "$ == '10'"); 790 fail(); 791 } catch (EvaluationException e) 792 { 793 } 795 } 796 797 public void testVariableCurtime() throws Exception 798 { 799 runEvaluation(testPerson_, "$curtime._repos_id == 'IDL:omg.org/TimeBase/UtcT:1.0'"); 800 801 Any _timeAny = getORB().create_any(); 802 UtcT _time = org.jacorb.util.Time.corbaTime(); 803 UtcTHelper.insert(_timeAny, _time); 804 805 runEvaluation(_timeAny, "$curtime._repos_id == $._repos_id"); 806 runEvaluation(_timeAny, "$.time <= $curtime.time"); 807 runEvaluation(_timeAny, "$.time + 1 >= $.time"); 808 runEvaluation(_timeAny, "$.time - 1 <= $.time"); 809 } 810 811 public void testAccessNonExistingStructMember() throws Exception 812 { 813 try 814 { 815 runEvaluation(testPerson_, "$.not_exist"); 816 fail(); 817 } catch (EvaluationException e) 818 { 819 } 821 } 822 823 public void testAccessNonExistingRuntimeVariable() throws Exception 824 { 825 try 826 { 827 runEvaluation(testPerson_, "$not_exist"); 828 fail(); 829 } catch (EvaluationException e) 830 { 831 } 833 } 834 835 public void testAccessNonExistingProperty() throws Exception 836 { 837 StructuredEvent event = testUtils_.getStructuredEvent(); 838 839 try 840 { 841 runEvaluation(event, "$.filterable_data(PROPERTY) == 1"); 842 843 fail(); 844 } catch (EvaluationException e) 845 { 846 } 848 849 try 850 { 851 runEvaluation(event, "$.filterable_data(PROPERTY) < 5"); 852 853 fail(); 854 } catch (EvaluationException e) 855 { 856 } 858 } 859 860 public void testLTEforwardsVisitorBug() throws Exception 861 { 862 String _expr = "$.time <= 1"; 863 AbstractTCLNode _root = TCLParser.parse(_expr); 864 _root.acceptPostOrder(new TCLCleanUp()); 865 866 ETCLComponentName _n = (ETCLComponentName) _root.left(); 867 868 assertEquals("$.time", _n.getComponentName()); 869 } 870 871 public void testAcceptPostOrder() throws Exception 872 { 873 for (int x = 0; x < visitorTestExpressions_.length; ++x) 874 { 875 AbstractTCLNode _root = TCLParser.parse(visitorTestExpressions_[x]); 876 _root.acceptPostOrder(new TCLCleanUp()); 877 ETCLComponentName _n = (ETCLComponentName) _root.left(); 878 879 assertEquals(visitorTestExpressions_[x] + " failed", "$.value", _n.getComponentName()); 880 } 881 } 882 883 public void testAcceptInOrder() throws Exception 884 { 885 for (int x = 0; x < visitorTestExpressions_.length; ++x) 886 { 887 AbstractTCLNode _root = TCLParser.parse(visitorTestExpressions_[x]); 888 _root.acceptInOrder(new TCLCleanUp()); 889 ETCLComponentName _n = (ETCLComponentName) _root.left(); 890 891 assertEquals(visitorTestExpressions_[x] + " failed", "$.value", _n.getComponentName()); 892 } 893 } 894 895 public void testAcceptPreOrder() throws Exception 896 { 897 for (int x = 0; x < visitorTestExpressions_.length; ++x) 898 { 899 AbstractTCLNode _root = TCLParser.parse(visitorTestExpressions_[x]); 900 _root.acceptPreOrder(new TCLCleanUp()); 901 ETCLComponentName _n = (ETCLComponentName) _root.left(); 902 903 assertEquals(visitorTestExpressions_[x] + " failed", "$.value", _n.getComponentName()); 904 } 905 } 906 907 public void testWhiteboardExpr() throws Exception 908 { 909 Any _any = getORB().create_any(); 910 911 StructuredEvent _event = testUtils_.getStructuredEvent(); 912 _event.header.variable_header = new Property[1]; 913 914 Any _anyInt = getORB().create_any(); 915 _anyInt.insert_long(10); 916 917 _event.header.variable_header[0] = new Property("workgroup_id", _anyInt); 918 919 StructuredEventHelper.insert(_any, _event); 920 runEvaluation(_any, "$.header.variable_header(workgroup_id) != 20"); 921 } 922 923 public void testNewLexer() throws Exception 924 { 925 assertNotNull(TCLParser.parse(".1")); 926 927 assertNotNull(TCLParser.parse("$.1")); 928 } 929 930 public void testTypedEvent() throws Exception 931 { 932 Property[] _props = new Property[] { new Property("operation", toAny("operationName")), 933 new Property("value1", toAny(100)), new Property("value2", toAny(200)) }; 934 935 Any _any = getORB().create_any(); 936 937 PropertySeqHelper.insert(_any, _props); 938 939 runEvaluation(_any, "$operation == 'operationName'"); 940 runEvaluation(_any, "$value1 > 50 and $value2 > 50"); 941 } 942 943 public void testStringMayContainSpecialChars() throws Exception 944 { 945 TCLParser.parse("'noti*server'"); 946 TCLParser.parse("'noti{}server'"); 947 TCLParser.parse("'noti\"server'"); 948 TCLParser.parse("'noti|server'"); 949 TCLParser.parse("'noti;server'"); 950 TCLParser.parse("'noti^server'"); 951 TCLParser.parse("'noti~server'"); 952 TCLParser.parse("'noti=server'"); 953 TCLParser.parse("'noti,server'"); 954 TCLParser.parse("'noti$server'"); 955 TCLParser.parse("'noti#server'"); 956 TCLParser.parse("'noti%server'"); 957 } 958 959 public void testParserIgnoresWhitespace() throws Exception 960 { 961 TCLParser.parse("\t\n\r "); 962 963 runEvaluation("TRUE", "\t\n\r TRUE\n\r \t "); 964 runEvaluation("2", "\t1\r+\n\r1\t"); 965 } 966 967 969 public static Test suite() throws Exception 970 { 971 return NotificationTestCase.suite("TCL Parsing and Evaluation Tests", TCLTest.class 972 ); 974 } 975 976 private void runEvaluation(Any any, String expr) throws Exception 977 { 978 979 runEvaluation(any, expr, "TRUE"); 980 } 981 982 private void runEvaluation(Any any, String expr, String expect) throws Exception 983 { 984 MessageFactory _notificationEventFactory = new DefaultMessageFactory(getConfiguration()); 985 986 Message _event = _notificationEventFactory.newMessage(any); 987 try 988 { 989 runEvaluation(_event, expr, expect); 990 } finally 991 { 992 _event.dispose(); 993 } 994 } 995 996 private void runEvaluation(StructuredEvent event, String expr) throws Exception 997 { 998 runEvaluation(event, expr, "TRUE"); 999 } 1000 1001 private void runEvaluation(StructuredEvent event, String expr, String expect) throws Exception 1002 { 1003 MessageFactory _notificationEventFactory = new DefaultMessageFactory(getConfiguration()); 1005 1006 Message _event = _notificationEventFactory.newMessage(event); 1007 runEvaluation(_event, expr, expect); 1008 } 1009 1010 private void runEvaluation(Message event, String expr, String expect) throws Exception 1011 { 1012 AbstractTCLNode _root = TCLParser.parse(expr); 1013 AbstractTCLNode _expect = TCLParser.parse(expect); 1014 1015 FilterConstraint _evaluator = new ETCLFilterConstraint(_root); 1016 EvaluationResult _res; 1017 _root.acceptPostOrder(new TCLCleanUp()); 1018 1019 EvaluationContext _context = new EvaluationContext(getEvaluator()); 1020 1021 _res = _evaluator.evaluate(_context, event); 1022 1023 assertEquals("expected " + _root.toStringTree() + " == " + _expect.toStringTree(), _expect 1024 .evaluate(null), _res); 1025 } 1026} 1027 1028 | Popular Tags |