1 28 29 package com.idaremedia.antx.capture; 30 31 import java.util.ArrayList ; 32 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.Project; 35 import org.apache.tools.ant.taskdefs.condition.Condition; 36 import org.apache.tools.ant.types.RegularExpression; 37 import org.apache.tools.ant.util.regexp.RegexpMatcher; 38 39 import com.idaremedia.antx.AntX; 40 import com.idaremedia.antx.apis.BuildAssertionException; 41 import com.idaremedia.antx.apis.TestScriptComponent; 42 import com.idaremedia.antx.helpers.InnerString; 43 import com.idaremedia.antx.helpers.Empties; 44 import com.idaremedia.antx.helpers.Strings; 45 46 76 77 public final class AssertLoggedTask extends LogsUsingTask 78 implements Condition, TestScriptComponent 79 { 80 83 public AssertLoggedTask() 84 { 85 super(AntX.capture+"AssertLoggedTask:"); 86 } 87 88 89 93 97 public final void setMsg(String msg) 98 { 99 m_defaultMsg = msg; 100 } 101 102 103 107 public final String getDefaultMsg() 108 { 109 return m_defaultMsg; 110 } 111 112 113 120 public void setValue(String value) 121 { 122 require_(value!=null,"setValu- nonzro valu"); 123 checkLookupMechanism("value"); 124 m_requiredValue = value; 125 } 126 127 128 132 public final String getValue() 133 { 134 return m_requiredValue; 135 } 136 137 138 145 public void setOccurances(int Ntimes) 146 { 147 m_Ntimes = Ntimes; 148 } 149 150 154 public final void setOccurance(int Ntimes) 155 { 156 setOccurances(Ntimes); 157 } 158 159 160 167 public final int getOccurances() 168 { 169 return m_Ntimes; 170 } 171 172 173 181 public void setLike(String pattern) 182 { 183 require_(pattern!=null,"setLike- nonzro pattern"); 184 checkLookupMechanism("like"); 185 m_RE = new RegularExpression(); 186 m_RE.setPattern(pattern); 187 } 188 189 190 195 public final String getLike() 196 { 197 return (m_RE==null) ? null : m_RE.getPattern(getProject()); 198 } 199 200 201 206 public void setTrueProperty(String property) 207 { 208 require_(property!=null,"setTrueP- nonzro nam"); 209 m_trueProperty = property; 210 } 211 212 213 217 public final String getTrueProperty() 218 { 219 return m_trueProperty; 220 } 221 222 223 230 public void addConfiguredString(InnerString valueN) 231 { 232 require_(valueN!=null,"addString- nonzro valu"); 233 234 if (m_orderedValues.isEmpty()) { 235 checkLookupMechanism("orderset"); 236 } 237 m_orderedValues.add(valueN.toString(getProject())); 238 } 239 240 241 245 public final boolean willCheckOrder() 246 { 247 return !m_orderedValues.isEmpty(); 248 } 249 250 251 255 264 protected final void verifyExists(String log, final String string, int Nwant) 265 { 266 String ermsg = null; 267 int Nfound= -1; 268 int i = log.indexOf(string); 269 if (Nwant>0) { 270 if (i == -1) { 271 ermsg = "brul.logs.missing.value"; 272 Nfound = 0; 273 Nwant = 1; 274 } 275 } else if (i != -1) { 276 ermsg = "brul.logs.found.value"; 277 Nfound = 1; 278 Nwant = 0; 279 } 280 if (ermsg!=null) { if (getDefaultMsg()!=null) { 282 ermsg = getDefaultMsg(); 283 } else { 284 ermsg = getAntXMsg(ermsg,String.valueOf(Nwant), 285 String.valueOf(Nfound),string); 286 } 287 log(ermsg,Project.MSG_ERR); 288 throw new BuildAssertionException(ermsg,getLocation()); 289 } 290 } 291 292 293 300 protected final void verifyOccurances(String log, String string, int Nwant) 301 { 302 int Nfound= 0; 303 int i, from=0; 304 while ((i=log.indexOf(string,from))>=0) { 305 Nfound++; 306 from = i+string.length(); 307 } 308 if (Nfound!=Nwant) { 309 String ermsg; 310 if (getDefaultMsg()!=null) { 311 ermsg = getDefaultMsg(); 312 } else { 313 ermsg = getAntXMsg("brul.logs.mismatch.occurs", 314 String.valueOf(Nwant),String.valueOf(Nfound), 315 string); 316 } 317 log(ermsg,Project.MSG_ERR); 318 throw new BuildAssertionException(ermsg,getLocation()); 319 } 320 } 321 322 323 330 protected final void verifyOutputInOrder(String log, String [] strings) 331 { 332 String ermsg=null; 333 final int Nmax = log.length(); 334 int ilast=0; 335 for (int i=0;i<strings.length;i++) { 336 int at = log.indexOf(strings[i], ilast); 337 if (at<0) { 338 ermsg = getAntXMsg("brul.logs.outofplace",strings[i],String.valueOf(i)); 339 break; 340 } 341 ilast = at+strings[i].length(); 342 if ((ilast==Nmax) && (i!=strings.length-1)) { 343 ermsg = getAntXMsg("brul.logs.eof",strings[i+1],strings[i],String.valueOf(i)); 344 break; 345 } 346 } 347 if (ermsg!=null) { 348 if (getDefaultMsg()!=null) { 349 ermsg = getDefaultMsg(); 350 } 351 log(ermsg,Project.MSG_ERR); 352 throw new BuildAssertionException(ermsg,getLocation()); 353 } 354 } 355 356 357 364 protected final void verifyMatches(String log, RegexpMatcher re) 365 { 366 if (!re.matches(log)) { 367 String ermsg = getAntXMsg("brul.logs.no.match",re.getPattern()); 368 log(ermsg,Project.MSG_ERR); 369 throw new BuildAssertionException(ermsg,getLocation()); 370 } 371 } 372 373 374 378 public boolean eval() 379 { 380 verifyCanExecute_("eval"); 381 382 boolean evaluated = false; 383 384 String vutLog = getVUTLog(); 385 386 if (getLike()!=null) { 387 verifyMatches(vutLog, m_RE.getRegexp(getProject())); 388 evaluated = true; 389 } 390 else if (getValue()!=null) { 391 int N = getOccurances(); 392 if (N>0) { 393 verifyOccurances(vutLog,getValue(),N); 394 } else { 395 verifyExists(vutLog,getValue(),N==0 ? 0 : 1); 396 } 397 evaluated = true; 398 } 399 else if (!m_orderedValues.isEmpty()) { 400 String [] strings = (String [])m_orderedValues.toArray(Empties.EMPTY_STRING_ARRAY); 401 verifyOutputInOrder(vutLog,strings); 402 strings = null; 403 evaluated = true; 404 } 405 406 if (evaluated) { 407 if (getTrueProperty()!=null) { 408 String prop = getTrueProperty(); 409 log("AssertLogged was true; setting true-property '"+prop+"' property", 410 Project.MSG_DEBUG); 411 getProject().setNewProperty(prop,Strings.TRUE); 412 } 413 if (willReset()) { 414 LogsRecorder r = getRecorder(true); 415 if (r!=null) { 416 r.clearLogs(); 417 } 418 } 419 } 420 421 return true; 422 } 423 424 425 429 public void execute() 430 { 431 eval(); 432 } 433 434 435 440 protected void verifyCanExecute_(String calr) 441 { 442 super.verifyCanExecute_(calr); 443 444 if (m_numMechanisms==0) { 445 String ermsg = getAntXMsg("brul.logs.nothing.to.check"); 446 log(ermsg,Project.MSG_ERR); 447 throw new BuildException(ermsg,getLocation()); 448 } 449 } 450 451 452 456 private void checkLookupMechanism(String from) 457 { 458 if (m_numMechanisms>0) { 459 String ermsg = getAntXMsg("brul.logs.one.lookup.kind"); 460 log(ermsg,Project.MSG_ERR); 461 throw new BuildException(ermsg,getLocation()); 462 } 463 m_numMechanisms++; 464 } 465 466 467 468 private static final int ATLEAST_ONCE= -1; 469 470 private String m_requiredValue; private int m_Ntimes= ATLEAST_ONCE; private RegularExpression m_RE; private String m_defaultMsg; private String m_trueProperty; private ArrayList m_orderedValues= new ArrayList (5); 476 private int m_numMechanisms;} 478 479 480 | Popular Tags |