1 28 29 package com.idaremedia.antx.condition.solo; 30 31 import java.io.File ; 32 import java.text.MessageFormat ; 33 34 import org.apache.tools.ant.AntTypeDefinition; 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.ComponentHelper; 37 import org.apache.tools.ant.DynamicAttribute; 38 import org.apache.tools.ant.Project; 39 import org.apache.tools.ant.taskdefs.condition.Condition; 40 import org.apache.tools.ant.types.Path; 41 import org.apache.tools.ant.types.Reference; 42 43 import com.idaremedia.antx.AntX; 44 import com.idaremedia.antx.Iteration; 45 import com.idaremedia.antx.NoiseLevel; 46 import com.idaremedia.antx.StringEquality; 47 import com.idaremedia.antx.helpers.Tk; 48 import com.idaremedia.antx.apis.BuildAssertionException; 49 import com.idaremedia.antx.condition.FileNotEmpty; 50 import com.idaremedia.antx.condition.ShortHandConditions; 51 import com.idaremedia.antx.condition.URIable; 52 import com.idaremedia.antx.parameters.Handling; 53 import com.idaremedia.antx.parameters.IsA; 54 55 108 109 public class AssertTask extends CompoundRuleTask 110 implements ShortHandConditions, FreeformRule, DynamicAttribute 111 { 112 115 public AssertTask() 116 { 117 super(AntX.rules+"assert"); 118 } 119 120 121 125 public AssertTask(String iam) 126 { 127 super(iam); 128 } 129 130 131 137 protected AssertTask(String iam, boolean embedded, boolean droppable) 138 { 139 super(iam,embedded); 140 m_droppable = droppable; 141 } 142 143 144 148 public void setProject(Project project) 149 { 150 super.setProject(project); 151 m_shh.setProject(project); 152 } 153 154 155 161 public final boolean isDroppable() 162 { 163 return m_droppable; 164 } 165 166 170 174 public final void setFailProperty(String property) 175 { 176 require_(!Tk.isWhitespace(property),"setfailp- nonwspc"); 177 setUpdateProperty(property); 178 } 179 180 181 185 public final String getFailProperty() 186 { 187 return getUpdateProperty(); 188 } 189 190 191 195 public final void setFailValue(String failPropertyValue) 196 { 197 setUpdatePropertyValue(failPropertyValue); 198 } 199 200 201 205 public final String getFailValue() 206 { 207 return getUpdatePropertyValue(); 208 } 209 210 211 221 public final void setLogic(BooleanRule.Operator logic) 222 { 223 setDefaultCombineOperator(logic); 224 } 225 226 227 232 public final BooleanRule.Operator getLogic() 233 { 234 return getDefaultCombineOperator(); 235 } 236 237 238 245 public final void setRuleId(Reference ruleId) 246 { 247 setReferralReference(ruleId); 248 } 249 250 251 255 public final void setMsg(String msg) 256 { 257 if (!Tk.isWhitespace(msg)) { 258 m_defaultMsg = uistrs().get("brul.assert.failed.lead",msg); 259 } else { 260 m_defaultMsg = msg; 261 } 262 } 263 264 265 269 public final String getDefaultMsg() 270 { 271 return m_defaultMsg; 272 } 273 274 275 279 284 protected boolean isIncompatibleReferral(ShareableCondition rule) 285 { 286 NoiseLevel effect = rule.getFailureEffect(); 287 if (NoiseLevel.isAsBadAs(effect,NoiseLevel.ERROR)) { 288 return false; 289 } 290 return true; 291 } 292 293 294 295 300 protected final boolean isDropped() 301 { 302 return isDroppable() && 303 Iteration.defaultdefaults().isAssertionsDisabled(); 304 } 305 306 307 308 313 protected void setEvalResult(boolean istrue, final String listing) 314 throws BuildException 315 { 316 String prop = getFailProperty(); 317 318 if (!istrue) { 319 String what = getFailValue(); 320 321 if (prop!=null) { 322 checkIfProperty_(prop,true); 323 getProject().setNewProperty(prop, what); 324 325 log("Assertion false; setting failure property "+prop+ 326 " to "+what, Project.MSG_DEBUG); 327 } 328 329 String errmsg; 330 if (getMsgId()==null) { 331 String inlined = getDefaultMsg(); 332 if (inlined==null) { 334 errmsg = uistrs().get("brul.assert.failed",listing); 335 } else if (inlined.indexOf("${")>=0) { 336 errmsg = inlined; 337 } else { 338 errmsg = MessageFormat.format(inlined,new Object []{listing}); 339 } 340 } else { 341 errmsg = getMsg(newMsgGetter(listing)); 342 } 343 344 violationOccured(errmsg); 345 346 throw new BuildAssertionException(errmsg, getLocation()); 347 348 } else if (prop!=null) { 349 String state = "true"; 350 if (isDropped()) { 351 state += " (disabled)"; 352 } 353 log("Assertion "+state+"; not setting failure property "+prop, 354 Project.MSG_DEBUG); 355 } 356 } 357 358 359 365 public boolean eval() throws BuildException 366 { 367 if (runReferral()) { 368 return getReferral().eval(new Referee()); 369 } 370 371 if (isDropped()) { 372 return true; 373 } 374 375 verifyCanExecute_("eval"); 376 377 if (m_shh.isActivated()) { 378 return defaultSingleConditionEval(); 379 380 } 381 return defaultMultipleConditionEval(); 382 } 383 384 385 386 392 protected void violationOccured(String ermsg) 393 { 394 if (ermsg==null) { 395 ermsg= uistrs().get("brul.assert.failed.default"); 396 } 397 log(ermsg, Project.MSG_ERR); 398 } 399 400 401 405 411 public void addConfigured(Condition c) 412 { 413 require_(c!=null,"add- nonzro condition"); 414 xaddCondition(c); 415 } 416 417 418 419 429 public void setDynamicAttribute(String name, String value) 430 { 431 require_(name!=null, "setFreeform- nonzro condition name"); 432 require_(value!=null,"setFreeform- nonzro uri fragment"); 433 434 boolean ok = false; 435 Project P = getProject(); 436 437 AntTypeDefinition td = ComponentHelper.getComponentHelper(P).getDefinition(name); 438 if (td!=null) { 439 Class cls = td.getExposedClass(P); 440 if (cls!=null && URIable.class.isAssignableFrom(cls)) { 441 URIable c = (URIable)td.create(P); 442 c.xsetFromURI(value); 443 m_shh.activateCondition(c,"set"+name); 444 ok = true; 445 } 446 } 447 if (!ok) { 448 String err = getAntXMsg("task.unsupported.attr",getTaskName(),name); 449 log(err,Project.MSG_ERR); 450 throw new BuildException(err,getLocation()); 451 } 452 } 453 454 458 462 public void setIsTrue(String value) 463 { 464 m_shh.setIsTrue(value); 465 } 466 467 468 472 public void setIsFalse(String value) 473 { 474 m_shh.setIsFalse(value); 475 } 476 477 478 482 public void setIsSet(String property) 483 { 484 m_shh.setIsSet(property); 485 } 486 487 488 493 public void setIsSetTrue(String property) 494 { 495 m_shh.setIsSetTrue(property); 496 } 497 498 499 503 public void setIsNotSet(String property) 504 { 505 m_shh.setIsNotSet(property); 506 } 507 508 509 515 public void setIsA(IsA isa) 516 { 517 m_shh.setIsA(isa); 518 } 519 520 521 526 public void setVarSet(String variable) 527 { 528 m_shh.setVarSet(variable); 529 } 530 531 532 536 public void setVarNotSet(String variable) 537 { 538 m_shh.setVarNotSet(variable); 539 } 540 541 542 547 public void setVarSetTrue(String variable) 548 { 549 m_shh.setVarSetTrue(variable); 550 } 551 552 553 557 public void setIsRef(String refId) 558 { 559 m_shh.setIsRef(refId); 560 } 561 562 563 567 public void setIsNotRef(String refId) 568 { 569 m_shh.setIsNotRef(refId); 570 } 571 572 573 577 public void setClass(String clzId) 578 { 579 m_shh.setClassName(clzId); 580 } 581 582 583 588 public void setIsKindOf(boolean isKindOf) 589 { 590 m_shh.setIsKindOf(isKindOf); 591 } 592 593 594 599 public void setAnySet(String properties) 600 { 601 m_shh.setAnySet(properties); 602 } 603 604 605 610 public void setAllSet(String properties) 611 { 612 m_shh.setAllSet(properties); 613 } 614 615 616 625 public void setAllSetLike(String pattern) 626 { 627 m_shh.setAllSetLike(pattern); 628 } 629 630 631 636 public void setNoneSet(String properties) 637 { 638 m_shh.setNoneSet(properties); 639 } 640 641 643 648 public void setIsBoolean(String value) 649 { 650 m_shh.setIsBoolean(value); 651 } 652 653 654 658 public void setIsNumeric(String value) 659 { 660 m_shh.setIsNumeric(value); 661 } 662 663 664 668 public void setGT(String gt) 669 { 670 m_shh.setGT(gt); 671 } 672 673 674 678 public void setGTE(String gte) 679 { 680 m_shh.setGTE(gte); 681 } 682 683 684 688 public void setLT(String lt) 689 { 690 m_shh.setLT(lt); 691 } 692 693 694 698 public void setLTE(String lte) 699 { 700 m_shh.setLTE(lte); 701 } 702 703 704 706 710 public void setIsFile(File file) 711 { 712 m_shh.setIsFile(file); 713 } 714 715 716 720 public void setIsDirectory(File directory) 721 { 722 m_shh.setIsDirectory(directory); 723 } 724 725 726 731 public void setFileNotEmpty(File file) 732 { 733 FileNotEmpty t= new FileNotEmpty(file,getProject()); 734 m_shh.activateCondition(t,"setFileNotEmpty"); 735 } 736 737 738 742 public void setIsResource(String resource) 743 { 744 m_shh.setIsResource(resource); 745 } 746 747 748 753 public void setIsClass(String classname) 754 { 755 m_shh.setIsClass(classname); 756 } 757 758 759 763 public void setClasspath(Path clsp) 764 { 765 m_shh.setClasspath(clsp); 766 } 767 768 769 774 public void setFilepath(Path filp) 775 { 776 m_shh.setFilepath(filp); 777 } 778 779 780 784 public void setClasspathRef(Reference cpr) 785 { 786 m_shh.setClasspathRef(cpr); 787 } 788 789 790 797 public void setSystemClasses(boolean included) 798 { 799 m_shh.setSystemClasses(included); 800 } 801 802 803 808 public void setOSFamily(String family) 809 { 810 m_shh.setOSFamily(family); 811 } 812 813 814 819 public void setOS(String selector) 820 { 821 m_shh.setOS(selector); 822 } 823 824 825 830 public void setHttpAlive(String url) 831 { 832 m_shh.setHttpAlive(url); 833 } 834 835 836 838 843 public void setIsNotWhitespace(String value) 844 { 845 m_shh.setIsNotWhitespace(value); 846 } 847 848 849 855 public void setEquals(String arg1) 856 { 857 StringEquality t= m_shh.getEqualityCondition(); 858 t.setKnownArg(arg1); 859 m_shh.activateCondition(t,"setEquals"); 860 } 861 862 863 869 public void setNotEquals(String arg1) 870 { 871 StringEquality t= m_shh.getEqualityCondition(); 872 t.setKnownArg(arg1); 873 t.setNegate(true); 874 m_shh.activateCondition(t,"setNotEquals"); 875 } 876 877 878 883 public void setStartsWith(String arg1) 884 { 885 StringEquality t= m_shh.getEqualityCondition(); 886 t.setKnownArg(arg1); 887 t.setOperator(StringEquality.OP_STARTSWITH); 888 m_shh.activateCondition(t,"setStartsWith"); 889 } 890 891 892 897 public void setEndsWith(String arg1) 898 { 899 StringEquality t= m_shh.getEqualityCondition(); 900 t.setKnownArg(arg1); 901 t.setOperator(StringEquality.OP_ENDSWITH); 902 m_shh.activateCondition(t,"setEndsWith"); 903 } 904 905 906 911 public void setMatches(String pattern) 912 { 913 m_shh.setMatches(pattern); 914 } 915 916 917 922 public void setContains(String substring) 923 { 924 StringEquality t= m_shh.getEqualityCondition(); 925 t.setKnownArg(substring); 926 t.setOperator(StringEquality.OP_CONTAINS); 927 m_shh.activateCondition(t,"setContains"); 928 } 929 930 931 935 public void setValue(String value) 936 { 937 m_shh.setValue(value); 938 } 939 940 941 948 public void setVariable(String variable) 949 { 950 m_shh.setVariable(variable); 951 } 952 953 954 957 public final void setVar(String variable) 958 { 959 setVariable(variable); 960 } 961 962 963 969 public void setProperty(String property) 970 { 971 m_shh.setProperty(property); 972 } 973 974 975 981 public void setReference(String refid) 982 { 983 m_shh.setReference(refid); 984 } 985 986 988 992 public void setAntIs(String version) 993 { 994 m_shh.setAntIs(version); 995 } 996 997 998 1003 public void setAntLike(String pattern) 1004 { 1005 m_shh.setAntLike(pattern); 1006 } 1007 1008 1010 1014 public void setIgnoreCase(boolean ignore) 1015 { 1016 m_shh.setIgnoreCase(ignore); 1017 } 1018 1019 1020 1025 public boolean isIgnoreCase() 1026 { 1027 return m_shh.isIgnoreCase(); 1028 } 1029 1030 1031 1035 public void setTrim(boolean trim) 1036 { 1037 m_shh.setTrim(trim); 1038 } 1039 1040 1041 1046 public boolean willTrim() 1047 { 1048 return m_shh.willTrim(); 1049 } 1050 1051 1052 1057 public void setWhitespace(Handling response) 1058 { 1059 m_shh.setWhitespace(response); 1060 } 1061 1062 1063 1068 public boolean ignoreWhitespace() 1069 { 1070 return m_shh.ignoreWhitespace(); 1071 } 1072 1073 1074 1079 public void setSynonyms(boolean allowAll) 1080 { 1081 m_shh.setSynonyms(allowAll); 1082 } 1083 1084 1085 1090 public void setMalformed(Handling response) 1091 { 1092 m_shh.setMalformed(response); 1093 } 1094 1095 1096 1101 public Handling getMalformedHandling() 1102 { 1103 return m_shh.getMalformedHandling(); 1104 } 1105 1106 1107 1109 1116 public void setSameField(String fieldname) 1117 { 1118 require_("project".equals(fieldname),"setfield- only 'project'"); 1119 EqualFields t = (EqualFields)m_shh.getHalfBakedObject(EqualFields.class); 1120 m_shh.activateCondition(t,"setSameField"); 1121 } 1122 1123 1124 1129 public void setItem1(String refid) 1130 { 1131 EqualFields t = (EqualFields)m_shh.getHalfBakedObject(EqualFields.class); 1132 t.setItem1(refid); 1133 } 1134 1135 1136 1141 public void setItem2(String refid) 1142 { 1143 EqualFields t = (EqualFields)m_shh.getHalfBakedObject(EqualFields.class); 1144 t.setItem2(refid); 1145 } 1146 1147 1149 private String m_defaultMsg; 1150 private final ShortHandHelper m_shh = new ShortHandHelper(this); 1151 private boolean m_droppable=true; 1152} 1153 1154 1155 | Popular Tags |