1 16 17 package org.apache.cocoon; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 27 import javax.xml.transform.TransformerException ; 28 import javax.xml.transform.TransformerFactory ; 29 import javax.xml.transform.dom.DOMSource ; 30 import javax.xml.transform.stream.StreamResult ; 31 32 import org.apache.avalon.framework.context.DefaultContext; 33 import org.apache.avalon.framework.parameters.Parameters; 34 import org.apache.avalon.framework.service.ServiceException; 35 import org.apache.avalon.framework.service.ServiceSelector; 36 import org.apache.cocoon.acting.Action; 37 import org.apache.cocoon.components.ContextHelper; 38 import org.apache.cocoon.components.flow.AbstractInterpreter; 39 import org.apache.cocoon.components.flow.FlowHelper; 40 import org.apache.cocoon.components.flow.Interpreter; 41 import org.apache.cocoon.components.source.SourceResolverAdapter; 42 import org.apache.cocoon.core.container.ContainerTestCase; 43 import org.apache.cocoon.environment.ObjectModelHelper; 44 import org.apache.cocoon.environment.mock.MockContext; 45 import org.apache.cocoon.environment.mock.MockRedirector; 46 import org.apache.cocoon.environment.mock.MockRequest; 47 import org.apache.cocoon.environment.mock.MockResponse; 48 import org.apache.cocoon.generation.Generator; 49 import org.apache.cocoon.matching.Matcher; 50 import org.apache.cocoon.serialization.Serializer; 51 import org.apache.cocoon.sitemap.PatternException; 52 import org.apache.cocoon.transformation.Transformer; 53 import org.apache.cocoon.xml.WhitespaceFilter; 54 import org.apache.cocoon.xml.dom.DOMBuilder; 55 import org.apache.cocoon.xml.dom.DOMStreamer; 56 import org.apache.excalibur.source.Source; 57 import org.apache.excalibur.source.SourceResolver; 58 import org.apache.excalibur.xml.sax.SAXParser; 59 import org.custommonkey.xmlunit.Diff; 60 import org.w3c.dom.Document ; 61 import org.xml.sax.InputSource ; 62 import org.xml.sax.SAXException ; 63 64 71 public abstract class SitemapComponentTestCase extends ContainerTestCase { 72 73 public final static Parameters EMPTY_PARAMS = Parameters.EMPTY_PARAMETERS; 74 75 private MockRequest request = new MockRequest(); 76 private MockResponse response = new MockResponse(); 77 private MockContext context = new MockContext(); 78 private MockRedirector redirector = new MockRedirector(); 79 private Map objectmodel = new HashMap (); 80 81 public final MockRequest getRequest() { 82 return request; 83 } 84 85 public final MockResponse getResponse() { 86 return response; 87 } 88 89 public final MockContext getContext() { 90 return context; 91 } 92 93 public final MockRedirector getRedirector() { 94 return redirector; 95 } 96 97 public final Map getObjectModel() { 98 return objectmodel; 99 } 100 101 protected void addContext(DefaultContext context) { 102 context.put(ContextHelper.CONTEXT_REQUEST_OBJECT, request); 103 context.put(ContextHelper.CONTEXT_RESPONSE_OBJECT, response); 104 context.put(ContextHelper.CONTEXT_OBJECT_MODEL, objectmodel); 105 } 106 107 public void setUp() throws Exception { 108 super.setUp(); 109 objectmodel.clear(); 110 111 request.reset(); 112 objectmodel.put(ObjectModelHelper.REQUEST_OBJECT, request); 113 114 response.reset(); 115 objectmodel.put(ObjectModelHelper.RESPONSE_OBJECT, response); 116 117 context.reset(); 118 objectmodel.put(ObjectModelHelper.CONTEXT_OBJECT, context); 119 120 redirector.reset(); 121 } 122 123 130 public final Map match(String type, String pattern, Parameters parameters) throws PatternException { 131 132 ServiceSelector selector = null; 133 Matcher matcher = null; 134 SourceResolver resolver = null; 135 136 Map result = null; 137 try { 138 selector = (ServiceSelector) this.lookup(Matcher.ROLE + 139 "Selector"); 140 assertNotNull("Test lookup of matcher selector", selector); 141 142 resolver = (SourceResolver) this.lookup(SourceResolver.ROLE); 143 assertNotNull("Test lookup of source resolver", resolver); 144 145 assertNotNull("Test if matcher name is not null", type); 146 matcher = (Matcher) selector.select(type); 147 assertNotNull("Test lookup of matcher", matcher); 148 149 result = matcher.match(pattern, objectmodel, parameters); 150 151 } catch (ServiceException ce) { 152 getLogger().error("Could not retrieve matcher", ce); 153 fail("Could not retrieve matcher: " + ce.toString()); 154 } finally { 155 if (matcher != null) { 156 selector.release(matcher); 157 } 158 this.release(selector); 159 this.release(resolver); 160 } 161 return result; 162 } 163 164 171 public final boolean select(String type, String expression, Parameters parameters) { 172 173 ServiceSelector selector = null; 174 org.apache.cocoon.selection.Selector sel = null; 175 SourceResolver resolver = null; 176 177 boolean result = false; 178 try { 179 selector = (ServiceSelector) this.lookup(org.apache.cocoon.selection.Selector.ROLE + 180 "Selector"); 181 assertNotNull("Test lookup of selector selector", selector); 182 183 resolver = (SourceResolver) this.lookup(SourceResolver.ROLE); 184 assertNotNull("Test lookup of source resolver", resolver); 185 186 assertNotNull("Test if selector name is not null", type); 187 sel = (org.apache.cocoon.selection.Selector) selector.select(type); 188 assertNotNull("Test lookup of selector", sel); 189 190 191 result = sel.select(expression, objectmodel, parameters); 192 193 } catch (ServiceException ce) { 194 getLogger().error("Could not retrieve selector", ce); 195 fail("Could not retrieve selector: " + ce.toString()); 196 } finally { 197 if (sel != null) { 198 selector.release(sel); 199 } 200 this.release(selector); 201 this.release(resolver); 202 } 203 return result; 204 } 205 206 213 public final Map act(String type, String source, Parameters parameters) throws Exception { 214 215 redirector.reset(); 216 217 ServiceSelector selector = null; 218 Action action = null; 219 SourceResolver resolver = null; 220 221 Map result = null; 222 try { 223 selector = (ServiceSelector) this.lookup(Action.ROLE + 224 "Selector"); 225 assertNotNull("Test lookup of action selector", selector); 226 227 resolver = (SourceResolver) this.lookup(SourceResolver.ROLE); 228 assertNotNull("Test lookup of source resolver", resolver); 229 230 assertNotNull("Test if action name is not null", type); 231 action = (Action) selector.select(type); 232 assertNotNull("Test lookup of action", action); 233 234 result = action.act(redirector, new SourceResolverAdapter(resolver), 235 objectmodel, source, parameters); 236 237 } catch (ServiceException ce) { 238 getLogger().error("Could not retrieve action", ce); 239 fail("Could not retrieve action: " + ce.toString()); 240 } finally { 241 if (action != null) { 242 selector.release(action); 243 } 244 this.release(selector); 245 this.release(resolver); 246 } 247 return result; 248 } 249 250 257 public final Document generate(String type, String source, Parameters parameters) 258 throws IOException , SAXException , ProcessingException { 259 260 ServiceSelector selector = null; 261 Generator generator = null; 262 SourceResolver resolver = null; 263 SAXParser parser = null; 264 265 Document document = null; 266 try { 267 selector = (ServiceSelector) this.lookup(Generator.ROLE + 268 "Selector"); 269 assertNotNull("Test lookup of generator selector", selector); 270 271 resolver = (SourceResolver) this.lookup(SourceResolver.ROLE); 272 assertNotNull("Test lookup of source resolver", resolver); 273 274 parser = (SAXParser) this.lookup(SAXParser.ROLE); 275 assertNotNull("Test lookup of parser", parser); 276 277 assertNotNull("Test if generator name is not null", type); 278 279 generator = (Generator) selector.select(type); 280 assertNotNull("Test lookup of generator", generator); 281 282 generator.setup(new SourceResolverAdapter(resolver), 283 objectmodel, source, parameters); 284 285 DOMBuilder builder = new DOMBuilder(); 286 generator.setConsumer(new WhitespaceFilter(builder)); 287 288 generator.generate(); 289 290 document = builder.getDocument(); 291 292 assertNotNull("Test for generator document", document); 293 294 } catch (ServiceException ce) { 295 getLogger().error("Could not retrieve generator", ce); 296 fail("Could not retrieve generator: " + ce.toString()); 297 } finally { 298 if (generator != null) { 299 selector.release(generator); 300 } 301 this.release(selector); 302 this.release(resolver); 303 this.release(parser); 304 } 305 306 return document; 307 } 308 309 317 public final Document transform(String type, String source, Parameters parameters, Document input) 318 throws SAXException , ProcessingException, IOException { 319 320 ServiceSelector selector = null; 321 Transformer transformer = null; 322 SourceResolver resolver = null; 323 SAXParser parser = null; 324 Source inputsource = null; 325 326 assertNotNull("Test for component manager", this.getManager()); 327 328 Document document = null; 329 try { 330 selector = (ServiceSelector) this.lookup(Transformer.ROLE+ 331 "Selector"); 332 assertNotNull("Test lookup of transformer selector", selector); 333 334 resolver = (SourceResolver) this.lookup(SourceResolver.ROLE); 335 assertNotNull("Test lookup of source resolver", resolver); 336 337 parser = (SAXParser) this.lookup(SAXParser.ROLE); 338 assertNotNull("Test lookup of parser", parser); 339 340 341 assertNotNull("Test if transformer name is not null", type); 342 transformer = (Transformer) selector.select(type); 343 assertNotNull("Test lookup of transformer", transformer); 344 345 transformer.setup(new SourceResolverAdapter(resolver), 346 objectmodel, source, parameters); 347 348 DOMBuilder builder = new DOMBuilder(); 349 transformer.setConsumer(new WhitespaceFilter(builder)); 350 351 assertNotNull("Test if input document is not null", input); 352 DOMStreamer streamer = new DOMStreamer(transformer); 353 streamer.stream(input); 354 355 document = builder.getDocument(); 356 assertNotNull("Test for transformer document", document); 357 358 } catch (ServiceException ce) { 359 getLogger().error("Could not retrieve transformer", ce); 360 ce.printStackTrace(); 361 fail("Could not retrieve transformer:"+ce.toString()); 362 } finally { 363 if (transformer!=null) { 364 selector.release(transformer); 365 } 366 367 if (selector!=null) { 368 this.release(selector); 369 } 370 371 if (inputsource!=null) { 372 resolver.release(inputsource); 373 } 374 375 if (resolver!=null) { 376 this.release(resolver); 377 } 378 379 if (parser!=null) { 380 this.release(parser); 381 } 382 } 383 384 return document; 385 } 386 387 396 public final byte[] serialize(String type, Parameters parameters, 397 Document input) throws SAXException , IOException { 398 399 ServiceSelector selector = null; 400 Serializer serializer = null; 401 SourceResolver resolver = null; 402 Source inputsource = null; 403 404 assertNotNull("Test for component manager", this.getManager()); 405 406 ByteArrayOutputStream document = null; 407 408 try { 409 selector = (ServiceSelector) this.lookup(Serializer.ROLE+ 410 "Selector"); 411 assertNotNull("Test lookup of serializer selector", selector); 412 413 resolver = (SourceResolver) this.lookup(SourceResolver.ROLE); 414 assertNotNull("Test lookup of source resolver", resolver); 415 416 assertNotNull("Test if serializer name is not null", type); 417 serializer = (Serializer) selector.select(type); 418 assertNotNull("Test lookup of serializer", serializer); 419 420 document = new ByteArrayOutputStream (); 421 serializer.setOutputStream(document); 422 423 assertNotNull("Test if input document is not null", input); 424 DOMStreamer streamer = new DOMStreamer(serializer); 425 426 streamer.stream(input); 427 } catch (ServiceException ce) { 428 getLogger().error("Could not retrieve serializer", ce); 429 fail("Could not retrieve serializer:"+ce.toString()); 430 } finally { 431 if (serializer!=null) { 432 selector.release(serializer); 433 } 434 435 if (selector!=null) { 436 this.release(selector); 437 } 438 439 if (inputsource!=null) { 440 resolver.release(inputsource); 441 } 442 443 if (resolver!=null) { 444 this.release(resolver); 445 } 446 } 447 448 return document.toByteArray(); 449 } 450 451 public String callFunction(String type, String source, String function, Map params) throws Exception { 452 453 redirector.reset(); 454 455 ServiceSelector selector = null; 456 Interpreter interpreter = null; 457 SourceResolver resolver = null; 458 459 try { 460 selector = (ServiceSelector) this.lookup(Interpreter.ROLE); 461 assertNotNull("Test lookup of interpreter selector", selector); 462 463 resolver = (SourceResolver) this.lookup(SourceResolver.ROLE); 464 assertNotNull("Test lookup of source resolver", resolver); 465 466 assertNotNull("Test if interpreter name is not null", type); 467 interpreter = (Interpreter) selector.select(type); 468 assertNotNull("Test lookup of interpreter", interpreter); 469 470 ((AbstractInterpreter)interpreter).register(source); 471 472 ArrayList parameters = new ArrayList (); 473 for (Iterator i = params.entrySet().iterator(); i.hasNext();) { 474 Map.Entry me = (Map.Entry )i.next(); 475 String name = (String )me.getKey(); 476 String value = (String )me.getValue(); 477 parameters.add(new Interpreter.Argument(name, value)); 478 } 479 interpreter.callFunction(function, parameters, getRedirector()); 480 481 } catch (ServiceException ce) { 482 getLogger().error("Could not retrieve interpeter", ce); 483 fail("Could not retrieve interpreter: " + ce.toString()); 484 } finally { 485 if (interpreter != null) { 486 selector.release(interpreter); 487 } 488 this.release(selector); 489 this.release(resolver); 490 } 491 return FlowHelper.getWebContinuation(getObjectModel()).getId(); 492 } 493 494 public String callContinuation(String type, String source, String id, Map params) throws Exception { 495 496 redirector.reset(); 497 498 ServiceSelector selector = null; 499 Interpreter interpreter = null; 500 SourceResolver resolver = null; 501 502 try { 503 selector = (ServiceSelector) this.lookup(Interpreter.ROLE); 504 assertNotNull("Test lookup of interpreter selector", selector); 505 506 resolver = (SourceResolver) this.lookup(SourceResolver.ROLE); 507 assertNotNull("Test lookup of source resolver", resolver); 508 509 assertNotNull("Test if interpreter name is not null", type); 510 interpreter = (Interpreter) selector.select(type); 511 assertNotNull("Test lookup of interpreter", interpreter); 512 513 ((AbstractInterpreter)interpreter).register(source); 514 515 ArrayList parameters = new ArrayList (); 516 for (Iterator i = params.entrySet().iterator(); i.hasNext();) { 517 Map.Entry me = (Map.Entry )i.next(); 518 String name = (String )me.getKey(); 519 String value = (String )me.getValue(); 520 parameters.add(new Interpreter.Argument(name, value)); 521 } 522 interpreter.handleContinuation(id, parameters, getRedirector()); 523 524 } catch (ServiceException ce) { 525 getLogger().error("Could not retrieve interpreter", ce); 526 fail("Could not retrieve interpreter: " + ce.toString()); 527 } finally { 528 if (interpreter != null) { 529 selector.release(interpreter); 530 } 531 this.release(selector); 532 this.release(resolver); 533 } 534 return FlowHelper.getWebContinuation(getObjectModel()).getId(); 535 } 536 537 public Object getFlowContextObject() { 538 return FlowHelper.getContextObject(getObjectModel()); 539 } 540 541 public final void print(Document document) { 542 TransformerFactory factory = TransformerFactory.newInstance(); 543 try 544 { 545 javax.xml.transform.Transformer serializer = factory.newTransformer(); 546 serializer.transform(new DOMSource (document), new StreamResult (System.out)); 547 System.out.println(); 548 } 549 catch (TransformerException te) 550 { 551 te.printStackTrace(); 552 } 553 } 554 555 public final Document load(String source) { 556 557 SourceResolver resolver = null; 558 SAXParser parser = null; 559 Source assertionsource = null; 560 561 assertNotNull("Test for component manager", this.getManager()); 562 563 Document assertiondocument = null; 564 try { 565 resolver = (SourceResolver) this.lookup(SourceResolver.ROLE); 566 assertNotNull("Test lookup of source resolver", resolver); 567 568 parser = (SAXParser) this.lookup(SAXParser.ROLE); 569 assertNotNull("Test lookup of parser", parser); 570 571 assertNotNull("Test if assertion document is not null", 572 source); 573 assertionsource = resolver.resolveURI(source); 574 assertNotNull("Test lookup of assertion source", 575 assertionsource); 576 assertTrue("Test if source exist", assertionsource.exists()); 577 578 DOMBuilder builder = new DOMBuilder(); 579 assertNotNull("Test if inputstream of the assertion source is not null", 580 assertionsource.getInputStream()); 581 582 parser.parse(new InputSource (assertionsource.getInputStream()), 583 new WhitespaceFilter(builder), 584 builder); 585 586 assertiondocument = builder.getDocument(); 587 assertNotNull("Test if assertion document exists", assertiondocument); 588 589 } catch (ServiceException ce) { 590 getLogger().error("Could not retrieve generator", ce); 591 fail("Could not retrieve generator: " + ce.toString()); 592 } catch (Exception e) { 593 getLogger().error("Could not execute test", e); 594 fail("Could not execute test: " + e); 595 } finally { 596 if (resolver != null) { 597 resolver.release(assertionsource); 598 } 599 this.release(resolver); 600 this.release(parser); 601 } 602 603 return assertiondocument; 604 } 605 606 613 public final byte[] loadByteArray(String source) { 614 615 SourceResolver resolver = null; 616 SAXParser parser = null; 617 Source assertionsource = null; 618 619 assertNotNull("Test for component manager", this.getManager()); 620 621 byte[] assertiondocument = null; 622 623 try { 624 resolver = (SourceResolver) this.lookup(SourceResolver.ROLE); 625 assertNotNull("Test lookup of source resolver", resolver); 626 627 parser = (SAXParser) this.lookup(SAXParser.ROLE); 628 assertNotNull("Test lookup of parser", parser); 629 630 assertNotNull("Test if assertion document is not null", source); 631 assertionsource = resolver.resolveURI(source); 632 assertNotNull("Test lookup of assertion source", assertionsource); 633 assertTrue("Test if source exist", assertionsource.exists()); 634 635 assertNotNull("Test if inputstream of the assertion source is not null", 636 assertionsource.getInputStream()); 637 638 InputStream input = assertionsource.getInputStream(); 639 long size = assertionsource.getContentLength(); 640 641 assertiondocument = new byte[(int) size]; 642 int i = 0; 643 int c; 644 645 while ((c = input.read())!=-1) { 646 assertiondocument[i] = (byte) c; 647 i++; 648 } 649 650 } catch (ServiceException ce) { 651 getLogger().error("Could not retrieve generator", ce); 652 fail("Could not retrieve generator: "+ce.toString()); 653 } catch (Exception e) { 654 getLogger().error("Could not execute test", e); 655 fail("Could not execute test: "+e); 656 } finally { 657 if (resolver!=null) { 658 resolver.release(assertionsource); 659 } 660 this.release(resolver); 661 this.release(parser); 662 } 663 664 return assertiondocument; 665 } 666 667 673 public final Diff compareXML(Document control, Document test) { 674 return new Diff(control, test); 675 } 676 677 684 public final void assertEqual(String msg, Document expected, Document actual) { 685 686 expected.getDocumentElement().normalize(); 687 actual.getDocumentElement().normalize(); 688 689 Diff diff = compareXML(expected, actual); 690 691 assertEquals(msg + ", " + diff.toString(), true, diff.similar()); 692 } 693 694 700 public final void assertEqual(Document expected, Document actual) { 701 702 expected.getDocumentElement().normalize(); 703 actual.getDocumentElement().normalize(); 704 705 Diff diff = compareXML(expected, actual); 706 707 assertEquals("Test if the assertion document is equal, " + diff.toString(), true, diff.similar()); 708 } 709 710 717 public final void assertIdentical(String msg, Document expected, Document actual) { 718 719 expected.getDocumentElement().normalize(); 720 actual.getDocumentElement().normalize(); 721 722 Diff diff = compareXML(expected, actual); 723 724 assertEquals(msg + ", " + diff.toString(), true, diff.identical()); 725 } 726 727 733 public final void assertIdentical(Document expected, Document actual) { 734 735 expected.getDocumentElement().normalize(); 736 actual.getDocumentElement().normalize(); 737 738 Diff diff = compareXML(expected, actual); 739 740 assertEquals("Test if the assertion document is equal, " + diff.toString(), true, diff.identical()); 741 } 742 743 749 public final void assertIdentical(byte[] expected, byte[] actual) { 750 assertEquals("Byte arrays of differing sizes, ", expected.length, 751 actual.length); 752 753 if (expected.length>0) { 754 for (int i = 0; i<expected.length; i++) { 755 assertEquals("Byte array differs at index "+i, expected[i], 756 actual[i]); 757 } 758 } 759 760 } 761 } 762 | Popular Tags |