1 15 package org.apache.tapestry.test; 16 17 import java.io.BufferedInputStream ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.net.URL ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Properties ; 25 26 import javax.xml.parsers.SAXParser ; 27 import javax.xml.parsers.SAXParserFactory ; 28 29 import org.apache.hivemind.ApplicationRuntimeException; 30 import org.apache.hivemind.Resource; 31 import org.apache.hivemind.parse.AbstractParser; 32 import org.apache.hivemind.parse.ElementParseInfo; 33 import org.apache.tapestry.test.assertions.AssertOutput; 34 import org.apache.tapestry.test.assertions.AssertRegexp; 35 import org.apache.tapestry.test.assertions.RegexpMatch; 36 import org.apache.tapestry.util.xml.DocumentParseException; 37 38 45 public class IntegrationTestScriptParser extends AbstractParser 46 { 47 private ScriptDescriptor _scriptDescriptor; 48 private Map _attributes; 49 private String _elementName; 50 51 54 private final Map _elementParseInfo = new HashMap (); 55 56 public IntegrationTestScriptParser() 57 { 58 initializeFromPropertiesFile(); 59 } 60 61 private void initializeFromPropertiesFile() 62 { 63 Properties p = new Properties (); 64 InputStream stream = null; 65 66 try 67 { 68 69 InputStream rawStream = getClass().getResourceAsStream("ScriptParser.properties"); 70 stream = new BufferedInputStream (rawStream); 71 72 p.load(stream); 73 74 stream.close(); 75 stream = null; 76 } 77 catch (IOException ex) 78 { 79 throw new ApplicationRuntimeException(ex); 80 } 81 finally 82 { 83 close(stream); 84 } 85 86 initializeFromProperties(p); 87 } 88 89 private void initializeFromProperties(Properties p) 90 { 91 Iterator i = p.keySet().iterator(); 92 while (i.hasNext()) 93 { 94 String key = (String ) i.next(); 95 String value = p.getProperty(key); 96 97 initializeFromProperty(key, value); 98 } 99 } 100 101 private void initializeFromProperty(String key, String value) 102 { 103 105 if (!key.startsWith("required.")) 106 return; 107 108 int lastDotx = key.lastIndexOf('.'); 109 110 String elementName = key.substring(9, lastDotx); 111 String attributeName = key.substring(lastDotx + 1); 112 113 boolean required = value.equalsIgnoreCase("true"); 114 115 ElementParseInfo epi = getElementParseInfo(elementName); 116 117 epi.addAttribute(attributeName, required); 118 } 119 120 private void close(InputStream stream) 121 { 122 try 123 { 124 if (stream != null) 125 stream.close(); 126 } 127 catch (IOException ex) 128 { 129 } 131 } 132 133 public ScriptDescriptor parse(Resource script) 134 { 135 initializeParser(script, STATE_INITIAL); 136 137 try 138 { 139 startParse(); 140 141 return _scriptDescriptor; 142 } 143 finally 144 { 145 resetParser(); 146 } 147 } 148 149 private void startParse() 150 { 151 try 152 { 153 SAXParserFactory factory = SAXParserFactory.newInstance(); 154 155 SAXParser parser = factory.newSAXParser(); 156 157 Resource resource = getResource(); 158 159 URL url = resource.getResourceURL(); 160 161 InputStream is = url.openStream(); 162 163 parser.parse(is, this); 164 } 165 catch (Exception ex) 166 { 167 throw new DocumentParseException(ex.getMessage(), getResource(), ex); 168 } 169 } 170 171 protected void initializeParser(Resource resource, int startState) 172 { 173 super.initializeParser(resource, startState); 174 175 _attributes = new HashMap (); 176 } 177 178 protected void resetParser() 179 { 180 _attributes = null; 181 _elementName = null; 182 _scriptDescriptor = null; 183 184 super.resetParser(); 185 } 186 187 private static final int STATE_INITIAL = 0; 188 private static final int STATE_TEST_SCRIPT = 1; 189 private static final int STATE_SERVLET = 2; 190 private static final int STATE_INIT_PARAMETER = 3; 191 private static final int STATE_REQUEST = 4; 192 private static final int STATE_ASSERT_OUTPUT = 5; 193 private static final int STATE_ASSERT_REGEXP = 6; 194 private static final int STATE_MATCH = 7; 195 196 private static final int STATE_NO_CONTENT = 1000; 197 198 protected void begin(String elementName, Map attributes) 199 { 200 _elementName = elementName; 201 _attributes = attributes; 202 203 switch (getState()) 204 { 205 case STATE_INITIAL : 206 beginInitial(); 207 break; 208 209 case STATE_TEST_SCRIPT : 210 beginTestScript(); 211 break; 212 213 case STATE_SERVLET : 214 beginServlet(); 215 break; 216 217 case STATE_REQUEST : 218 beginRequest(); 219 break; 220 221 case STATE_ASSERT_REGEXP : 222 beginAssertRegexp(); 223 break; 224 225 default : 226 unexpectedElement(_elementName); 227 } 228 } 229 230 protected void end(String elementName) 231 { 232 _elementName = elementName; 233 234 switch (getState()) 235 { 236 case STATE_ASSERT_OUTPUT : 237 238 endAssertOutput(); 239 break; 240 241 case STATE_ASSERT_REGEXP : 242 endAssertRegexp(); 243 break; 244 245 case STATE_MATCH : 246 endMatch(); 247 break; 248 249 default : 250 break; 251 } 252 253 pop(); 254 255 } 256 257 private void beginInitial() 258 { 259 if (_elementName.equals("test-script")) 260 { 261 enterTestScript(); 262 return; 263 } 264 265 unexpectedElement(_elementName); 266 } 267 268 private void beginTestScript() 269 { 270 if (_elementName.equals("servlet")) 271 { 272 enterServlet(); 273 return; 274 } 275 276 if (_elementName.equals("request")) 277 { 278 enterRequest(); 279 return; 280 } 281 282 unexpectedElement(_elementName); 283 } 284 285 private void beginServlet() 286 { 287 if (_elementName.equals("init-parameter")) 288 { 289 enterInitParameter(); 290 return; 291 } 292 293 unexpectedElement(_elementName); 294 } 295 296 private void enterRequest() 297 { 298 validateAttributes(); 299 300 String servletName = getAttribute("servlet"); 301 String servletPath = getAttribute("servlet-path", "/app"); 302 303 RequestDescriptor rd = new RequestDescriptor(); 304 rd.setServletName(servletName); 305 rd.setServletPath(servletPath); 306 307 ScriptDescriptor sd = (ScriptDescriptor) peekObject(); 308 309 sd.addRequestDescriptor(rd); 310 311 push(_elementName, rd, STATE_REQUEST); 312 } 313 314 public void beginRequest() 315 { 316 if (_elementName.equals("parameter")) 317 { 318 enterParameter(); 319 return; 320 } 321 322 if (_elementName.equals("assert-output")) 323 { 324 enterAssertOutput(); 325 return; 326 } 327 328 if (_elementName.equals("assert-regexp")) 329 { 330 enterAssertRegexp(); 331 return; 332 } 333 334 unexpectedElement(_elementName); 335 } 336 337 private void enterAssertOutput() 338 { 339 validateAttributes(); 340 341 AssertOutput ao = new AssertOutput(); 342 RequestDescriptor rd = (RequestDescriptor) peekObject(); 343 344 rd.addAssertion(ao); 345 346 push(_elementName, ao, STATE_ASSERT_OUTPUT, false); 347 } 348 349 private void enterAssertRegexp() 350 { 351 validateAttributes(); 352 353 int subgroup = getIntAttribute("subgroup", 0); 354 355 AssertRegexp ar = new AssertRegexp(); 356 ar.setSubgroup(subgroup); 357 358 RequestDescriptor rd = (RequestDescriptor) peekObject(); 359 360 rd.addAssertion(ar); 361 362 push(_elementName, ar, STATE_ASSERT_REGEXP, false); 363 } 364 365 private void beginAssertRegexp() 366 { 367 if (_elementName.equals("match")) 368 { 369 enterMatch(); 370 return; 371 } 372 373 unexpectedElement(_elementName); 374 } 375 376 private void enterMatch() 377 { 378 validateAttributes(); 379 380 RegexpMatch m = new RegexpMatch(); 381 AssertRegexp ar = (AssertRegexp) peekObject(); 382 383 ar.addMatch(m); 384 385 push(_elementName, m, STATE_MATCH, false); 386 } 387 388 private void endAssertOutput() 389 { 390 String content = peekContent(); 391 AssertOutput ao = (AssertOutput) peekObject(); 392 393 ao.setExpectedSubstring(content); 394 } 395 396 private void endAssertRegexp() 397 { 398 String content = peekContent(); 399 400 AssertRegexp ar = (AssertRegexp) peekObject(); 401 402 ar.setRegexp(content); 403 } 404 405 private void endMatch() 406 { 407 String content = peekContent(); 408 409 RegexpMatch m = (RegexpMatch) peekObject(); 410 411 m.setExpectedString(content); 412 } 413 414 protected String peekContent() 415 { 416 String rawContent = super.peekContent(); 417 418 if (rawContent == null) 419 return null; 420 421 return rawContent.trim(); 422 } 423 424 private void enterParameter() 425 { 426 validateAttributes(); 427 428 String name = getAttribute("name"); 429 String value = getAttribute("value"); 430 431 RequestDescriptor rd = (RequestDescriptor) peekObject(); 432 433 rd.addParameter(name, value); 434 435 push(_elementName, null, STATE_NO_CONTENT); 436 } 437 438 private void enterInitParameter() 439 { 440 validateAttributes(); 441 442 String name = getAttribute("name"); 443 String value = getAttribute("value"); 444 445 ServletDescriptor sd = (ServletDescriptor) peekObject(); 446 447 sd.addInitParameter(name, value); 448 449 push(_elementName, null, STATE_NO_CONTENT); 450 } 451 452 private void enterTestScript() 453 { 454 validateAttributes(); 455 456 String contextName = getAttribute("context"); 457 String directory = getAttribute("directory"); 458 459 ScriptDescriptor sd = new ScriptDescriptor(); 460 sd.setContextName(contextName); 461 sd.setRootDirectory(directory); 462 463 _scriptDescriptor = sd; 464 465 push(_elementName, sd, STATE_TEST_SCRIPT); 466 } 467 468 private void enterServlet() 469 { 470 validateAttributes(); 471 472 String name = getAttribute("name"); 473 String className = getAttribute("class", "org.apache.tapestry.ApplicationServlet"); 474 475 ServletDescriptor sd = new ServletDescriptor(); 476 sd.setName(name); 477 sd.setClassName(className); 478 479 sd.setLocation(getLocation()); 482 483 ScriptDescriptor scriptDescriptor = (ScriptDescriptor) peekObject(); 484 485 scriptDescriptor.addServletDescriptor(sd); 486 487 push(_elementName, sd, STATE_SERVLET); 488 } 489 490 private String getAttribute(String name) 491 { 492 return (String ) _attributes.get(name); 493 } 494 495 private String getAttribute(String name, String defaultValue) 496 { 497 if (!_attributes.containsKey(name)) 498 return defaultValue; 499 500 return (String ) _attributes.get(name); 501 } 502 503 private void validateAttributes() 504 { 505 Iterator i = _attributes.keySet().iterator(); 506 507 ElementParseInfo epi = getElementParseInfo(_elementName); 508 509 511 while (i.hasNext()) 512 { 513 String name = (String ) i.next(); 514 515 if (!epi.isKnown(name)) 516 throw new DocumentParseException( 517 ScriptMessages.unexpectedAttributeInElement(name, _elementName), 518 getLocation(), 519 null); 520 } 521 522 524 i = epi.getRequiredNames(); 525 while (i.hasNext()) 526 { 527 String name = (String ) i.next(); 528 529 if (!_attributes.containsKey(name)) 530 throw new DocumentParseException( 531 ScriptMessages.missingRequiredAttribute(name, _elementName), 532 getLocation(), 533 null); 534 } 535 536 } 537 538 private ElementParseInfo getElementParseInfo(String elementName) 539 { 540 ElementParseInfo result = (ElementParseInfo) _elementParseInfo.get(elementName); 541 542 if (result == null) 543 { 544 result = new ElementParseInfo(); 545 _elementParseInfo.put(elementName, result); 546 } 547 548 return result; 549 } 550 551 private int getIntAttribute(String name, int defaultValue) 552 { 553 String attributeValue = getAttribute(name); 554 555 if (attributeValue == null) 556 return defaultValue; 557 558 try 559 { 560 return Integer.parseInt(attributeValue); 561 } 562 catch (NumberFormatException ex) 563 { 564 throw new ApplicationRuntimeException( 565 ScriptMessages.invalidIntAttribute( 566 name, 567 _elementName, 568 getLocation(), 569 attributeValue), 570 getLocation(), 571 ex); 572 } 573 } 574 } 575 | Popular Tags |