Source Code Cross Referenced for SitemapComponentTestCase.java in  » Web-Framework » cocoon » org » apache » cocoon » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » cocoon » org.apache.cocoon 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.cocoon;
019:
020:        import java.io.ByteArrayOutputStream;
021:        import java.io.IOException;
022:        import java.io.InputStream;
023:        import java.util.ArrayList;
024:        import java.util.HashMap;
025:        import java.util.Iterator;
026:        import java.util.Map;
027:
028:        import javax.xml.transform.TransformerException;
029:        import javax.xml.transform.TransformerFactory;
030:        import javax.xml.transform.dom.DOMSource;
031:        import javax.xml.transform.stream.StreamResult;
032:
033:        import org.apache.avalon.framework.context.DefaultContext;
034:        import org.apache.avalon.framework.parameters.Parameters;
035:        import org.apache.avalon.framework.service.ServiceException;
036:        import org.apache.avalon.framework.service.ServiceSelector;
037:        import org.apache.cocoon.acting.Action;
038:        import org.apache.cocoon.components.ContextHelper;
039:        import org.apache.cocoon.components.flow.AbstractInterpreter;
040:        import org.apache.cocoon.components.flow.FlowHelper;
041:        import org.apache.cocoon.components.flow.Interpreter;
042:        import org.apache.cocoon.components.source.SourceResolverAdapter;
043:        import org.apache.cocoon.core.container.ContainerTestCase;
044:        import org.apache.cocoon.environment.ObjectModelHelper;
045:        import org.apache.cocoon.environment.mock.MockContext;
046:        import org.apache.cocoon.environment.mock.MockRedirector;
047:        import org.apache.cocoon.environment.mock.MockRequest;
048:        import org.apache.cocoon.environment.mock.MockResponse;
049:        import org.apache.cocoon.generation.Generator;
050:        import org.apache.cocoon.matching.Matcher;
051:        import org.apache.cocoon.serialization.Serializer;
052:        import org.apache.cocoon.sitemap.PatternException;
053:        import org.apache.cocoon.transformation.Transformer;
054:        import org.apache.cocoon.xml.WhitespaceFilter;
055:        import org.apache.cocoon.xml.dom.DOMBuilder;
056:        import org.apache.cocoon.xml.dom.DOMStreamer;
057:        import org.apache.excalibur.source.Source;
058:        import org.apache.excalibur.source.SourceResolver;
059:        import org.apache.excalibur.xml.sax.SAXParser;
060:        import org.custommonkey.xmlunit.Diff;
061:        import org.w3c.dom.Document;
062:        import org.xml.sax.InputSource;
063:        import org.xml.sax.SAXException;
064:
065:        /**
066:         * Testcase for actions, generators, transformers and serializer components. 
067:         *
068:         * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
069:         * @author <a href="mailto:mark.leicester@energyintellect.com">Mark Leicester</a>
070:         * @version CVS $Id: SitemapComponentTestCase.java 433543 2006-08-22 06:22:54Z crossley $
071:         */
072:        public abstract class SitemapComponentTestCase extends
073:                ContainerTestCase {
074:
075:            public final static Parameters EMPTY_PARAMS = Parameters.EMPTY_PARAMETERS;
076:
077:            private MockRequest request = new MockRequest();
078:            private MockResponse response = new MockResponse();
079:            private MockContext context = new MockContext();
080:            private MockRedirector redirector = new MockRedirector();
081:            private Map objectmodel = new HashMap();
082:
083:            public final MockRequest getRequest() {
084:                return request;
085:            }
086:
087:            public final MockResponse getResponse() {
088:                return response;
089:            }
090:
091:            public final MockContext getContext() {
092:                return context;
093:            }
094:
095:            public final MockRedirector getRedirector() {
096:                return redirector;
097:            }
098:
099:            public final Map getObjectModel() {
100:                return objectmodel;
101:            }
102:
103:            protected void addContext(DefaultContext context) {
104:                context.put(ContextHelper.CONTEXT_REQUEST_OBJECT, request);
105:                context.put(ContextHelper.CONTEXT_RESPONSE_OBJECT, response);
106:                context.put(ContextHelper.CONTEXT_OBJECT_MODEL, objectmodel);
107:            }
108:
109:            public void setUp() throws Exception {
110:                super .setUp();
111:                objectmodel.clear();
112:
113:                request.reset();
114:                objectmodel.put(ObjectModelHelper.REQUEST_OBJECT, request);
115:
116:                response.reset();
117:                objectmodel.put(ObjectModelHelper.RESPONSE_OBJECT, response);
118:
119:                context.reset();
120:                objectmodel.put(ObjectModelHelper.CONTEXT_OBJECT, context);
121:
122:                redirector.reset();
123:            }
124:
125:            /**
126:             * Match with a pattern.
127:             *
128:             * @param type Hint of the matcher. 
129:             * @param pattern Pattern for the matcher.
130:             * @param parameters Matcher parameters.
131:             */
132:            public final Map match(String type, String pattern,
133:                    Parameters parameters) throws PatternException {
134:
135:                ServiceSelector selector = null;
136:                Matcher matcher = null;
137:                SourceResolver resolver = null;
138:
139:                Map result = null;
140:                try {
141:                    selector = (ServiceSelector) this .lookup(Matcher.ROLE
142:                            + "Selector");
143:                    assertNotNull("Test lookup of matcher selector", selector);
144:
145:                    resolver = (SourceResolver) this 
146:                            .lookup(SourceResolver.ROLE);
147:                    assertNotNull("Test lookup of source resolver", resolver);
148:
149:                    assertNotNull("Test if matcher name is not null", type);
150:                    matcher = (Matcher) selector.select(type);
151:                    assertNotNull("Test lookup of matcher", matcher);
152:
153:                    result = matcher.match(pattern, objectmodel, parameters);
154:
155:                } catch (ServiceException ce) {
156:                    getLogger().error("Could not retrieve matcher", ce);
157:                    fail("Could not retrieve matcher: " + ce.toString());
158:                } finally {
159:                    if (matcher != null) {
160:                        selector.release(matcher);
161:                    }
162:                    this .release(selector);
163:                    this .release(resolver);
164:                }
165:                return result;
166:            }
167:
168:            /**
169:             * Select with a pattern.
170:             *
171:             * @param type Hint of the matcher. 
172:             * @param expression Expression for the selector.
173:             * @param parameters Matcher parameters.
174:             */
175:            public final boolean select(String type, String expression,
176:                    Parameters parameters) {
177:
178:                ServiceSelector selector = null;
179:                org.apache.cocoon.selection.Selector sel = null;
180:                SourceResolver resolver = null;
181:
182:                boolean result = false;
183:                try {
184:                    selector = (ServiceSelector) this 
185:                            .lookup(org.apache.cocoon.selection.Selector.ROLE
186:                                    + "Selector");
187:                    assertNotNull("Test lookup of selector selector", selector);
188:
189:                    resolver = (SourceResolver) this 
190:                            .lookup(SourceResolver.ROLE);
191:                    assertNotNull("Test lookup of source resolver", resolver);
192:
193:                    assertNotNull("Test if selector name is not null", type);
194:                    sel = (org.apache.cocoon.selection.Selector) selector
195:                            .select(type);
196:                    assertNotNull("Test lookup of selector", sel);
197:
198:                    result = sel.select(expression, objectmodel, parameters);
199:
200:                } catch (ServiceException ce) {
201:                    getLogger().error("Could not retrieve selector", ce);
202:                    fail("Could not retrieve selector: " + ce.toString());
203:                } finally {
204:                    if (sel != null) {
205:                        selector.release(sel);
206:                    }
207:                    this .release(selector);
208:                    this .release(resolver);
209:                }
210:                return result;
211:            }
212:
213:            /**
214:             * Perform the action component.
215:             *
216:             * @param type Hint of the action. 
217:             * @param source Source for the action.
218:             * @param parameters Action parameters.
219:             */
220:            public final Map act(String type, String source,
221:                    Parameters parameters) throws Exception {
222:
223:                redirector.reset();
224:
225:                ServiceSelector selector = null;
226:                Action action = null;
227:                SourceResolver resolver = null;
228:
229:                Map result = null;
230:                try {
231:                    selector = (ServiceSelector) this .lookup(Action.ROLE
232:                            + "Selector");
233:                    assertNotNull("Test lookup of action selector", selector);
234:
235:                    resolver = (SourceResolver) this 
236:                            .lookup(SourceResolver.ROLE);
237:                    assertNotNull("Test lookup of source resolver", resolver);
238:
239:                    assertNotNull("Test if action name is not null", type);
240:                    action = (Action) selector.select(type);
241:                    assertNotNull("Test lookup of action", action);
242:
243:                    result = action.act(redirector, new SourceResolverAdapter(
244:                            resolver), objectmodel, source, parameters);
245:
246:                } catch (ServiceException ce) {
247:                    getLogger().error("Could not retrieve action", ce);
248:                    fail("Could not retrieve action: " + ce.toString());
249:                } finally {
250:                    if (action != null) {
251:                        selector.release(action);
252:                    }
253:                    this .release(selector);
254:                    this .release(resolver);
255:                }
256:                return result;
257:            }
258:
259:            /**
260:             * Generate the generator output.
261:             *
262:             * @param type Hint of the generator. 
263:             * @param source Source for the generator.
264:             * @param parameters Generator parameters.
265:             */
266:            public final Document generate(String type, String source,
267:                    Parameters parameters) throws IOException, SAXException,
268:                    ProcessingException {
269:
270:                ServiceSelector selector = null;
271:                Generator generator = null;
272:                SourceResolver resolver = null;
273:                SAXParser parser = null;
274:
275:                Document document = null;
276:                try {
277:                    selector = (ServiceSelector) this .lookup(Generator.ROLE
278:                            + "Selector");
279:                    assertNotNull("Test lookup of generator selector", selector);
280:
281:                    resolver = (SourceResolver) this 
282:                            .lookup(SourceResolver.ROLE);
283:                    assertNotNull("Test lookup of source resolver", resolver);
284:
285:                    parser = (SAXParser) this .lookup(SAXParser.ROLE);
286:                    assertNotNull("Test lookup of parser", parser);
287:
288:                    assertNotNull("Test if generator name is not null", type);
289:
290:                    generator = (Generator) selector.select(type);
291:                    assertNotNull("Test lookup of generator", generator);
292:
293:                    generator.setup(new SourceResolverAdapter(resolver),
294:                            objectmodel, source, parameters);
295:
296:                    DOMBuilder builder = new DOMBuilder();
297:                    generator.setConsumer(new WhitespaceFilter(builder));
298:
299:                    generator.generate();
300:
301:                    document = builder.getDocument();
302:
303:                    assertNotNull("Test for generator document", document);
304:
305:                } catch (ServiceException ce) {
306:                    getLogger().error("Could not retrieve generator", ce);
307:                    fail("Could not retrieve generator: " + ce.toString());
308:                } finally {
309:                    if (generator != null) {
310:                        selector.release(generator);
311:                    }
312:                    this .release(selector);
313:                    this .release(resolver);
314:                    this .release(parser);
315:                }
316:
317:                return document;
318:            }
319:
320:            /**     
321:             * Trannsform a document by a transformer
322:             *      
323:             * @param type Hint of the transformer. 
324:             * @param source Source for the transformer.
325:             * @param parameters Generator parameters.
326:             * @param input Input document.
327:             */
328:            public final Document transform(String type, String source,
329:                    Parameters parameters, Document input) throws SAXException,
330:                    ProcessingException, IOException {
331:
332:                ServiceSelector selector = null;
333:                Transformer transformer = null;
334:                SourceResolver resolver = null;
335:                SAXParser parser = null;
336:                Source inputsource = null;
337:
338:                assertNotNull("Test for component manager", this .getManager());
339:
340:                Document document = null;
341:                try {
342:                    selector = (ServiceSelector) this .lookup(Transformer.ROLE
343:                            + "Selector");
344:                    assertNotNull("Test lookup of transformer selector",
345:                            selector);
346:
347:                    resolver = (SourceResolver) this 
348:                            .lookup(SourceResolver.ROLE);
349:                    assertNotNull("Test lookup of source resolver", resolver);
350:
351:                    parser = (SAXParser) this .lookup(SAXParser.ROLE);
352:                    assertNotNull("Test lookup of parser", parser);
353:
354:                    assertNotNull("Test if transformer name is not null", type);
355:                    transformer = (Transformer) selector.select(type);
356:                    assertNotNull("Test lookup of transformer", transformer);
357:
358:                    transformer.setup(new SourceResolverAdapter(resolver),
359:                            objectmodel, source, parameters);
360:
361:                    DOMBuilder builder = new DOMBuilder();
362:                    transformer.setConsumer(new WhitespaceFilter(builder));
363:
364:                    assertNotNull("Test if input document is not null", input);
365:                    DOMStreamer streamer = new DOMStreamer(transformer);
366:                    streamer.stream(input);
367:
368:                    document = builder.getDocument();
369:                    assertNotNull("Test for transformer document", document);
370:
371:                } catch (ServiceException ce) {
372:                    getLogger().error("Could not retrieve transformer", ce);
373:                    ce.printStackTrace();
374:                    fail("Could not retrieve transformer:" + ce.toString());
375:                } finally {
376:                    if (transformer != null) {
377:                        selector.release(transformer);
378:                    }
379:
380:                    if (selector != null) {
381:                        this .release(selector);
382:                    }
383:
384:                    if (inputsource != null) {
385:                        resolver.release(inputsource);
386:                    }
387:
388:                    if (resolver != null) {
389:                        this .release(resolver);
390:                    }
391:
392:                    if (parser != null) {
393:                        this .release(parser);
394:                    }
395:                }
396:
397:                return document;
398:            }
399:
400:            /**
401:             * Serialize a document by a serializer
402:             *
403:             * @param type Hint of the serializer.
404:             * @param parameters Serializer parameters.
405:             * @param input Input document.
406:             *
407:             * @return Serialized data.
408:             */
409:            public final byte[] serialize(String type, Parameters parameters,
410:                    Document input) throws SAXException, IOException {
411:
412:                ServiceSelector selector = null;
413:                Serializer serializer = null;
414:                SourceResolver resolver = null;
415:                Source inputsource = null;
416:
417:                assertNotNull("Test for component manager", this .getManager());
418:
419:                ByteArrayOutputStream document = null;
420:
421:                try {
422:                    selector = (ServiceSelector) this .lookup(Serializer.ROLE
423:                            + "Selector");
424:                    assertNotNull("Test lookup of serializer selector",
425:                            selector);
426:
427:                    resolver = (SourceResolver) this 
428:                            .lookup(SourceResolver.ROLE);
429:                    assertNotNull("Test lookup of source resolver", resolver);
430:
431:                    assertNotNull("Test if serializer name is not null", type);
432:                    serializer = (Serializer) selector.select(type);
433:                    assertNotNull("Test lookup of serializer", serializer);
434:
435:                    document = new ByteArrayOutputStream();
436:                    serializer.setOutputStream(document);
437:
438:                    assertNotNull("Test if input document is not null", input);
439:                    DOMStreamer streamer = new DOMStreamer(serializer);
440:
441:                    streamer.stream(input);
442:                } catch (ServiceException ce) {
443:                    getLogger().error("Could not retrieve serializer", ce);
444:                    fail("Could not retrieve serializer:" + ce.toString());
445:                } finally {
446:                    if (serializer != null) {
447:                        selector.release(serializer);
448:                    }
449:
450:                    if (selector != null) {
451:                        this .release(selector);
452:                    }
453:
454:                    if (inputsource != null) {
455:                        resolver.release(inputsource);
456:                    }
457:
458:                    if (resolver != null) {
459:                        this .release(resolver);
460:                    }
461:                }
462:
463:                return document.toByteArray();
464:            }
465:
466:            public String callFunction(String type, String source,
467:                    String function, Map params) throws Exception {
468:
469:                redirector.reset();
470:
471:                ServiceSelector selector = null;
472:                Interpreter interpreter = null;
473:                SourceResolver resolver = null;
474:
475:                try {
476:                    selector = (ServiceSelector) this .lookup(Interpreter.ROLE);
477:                    assertNotNull("Test lookup of interpreter selector",
478:                            selector);
479:
480:                    resolver = (SourceResolver) this 
481:                            .lookup(SourceResolver.ROLE);
482:                    assertNotNull("Test lookup of source resolver", resolver);
483:
484:                    assertNotNull("Test if interpreter name is not null", type);
485:                    interpreter = (Interpreter) selector.select(type);
486:                    assertNotNull("Test lookup of interpreter", interpreter);
487:
488:                    ((AbstractInterpreter) interpreter).register(source);
489:
490:                    ArrayList parameters = new ArrayList();
491:                    for (Iterator i = params.entrySet().iterator(); i.hasNext();) {
492:                        Map.Entry me = (Map.Entry) i.next();
493:                        String name = (String) me.getKey();
494:                        String value = (String) me.getValue();
495:                        parameters.add(new Interpreter.Argument(name, value));
496:                    }
497:                    interpreter.callFunction(function, parameters,
498:                            getRedirector());
499:
500:                } catch (ServiceException ce) {
501:                    getLogger().error("Could not retrieve interpeter", ce);
502:                    fail("Could not retrieve interpreter: " + ce.toString());
503:                } finally {
504:                    if (interpreter != null) {
505:                        selector.release(interpreter);
506:                    }
507:                    this .release(selector);
508:                    this .release(resolver);
509:                }
510:                return FlowHelper.getWebContinuation(getObjectModel()).getId();
511:            }
512:
513:            public String callContinuation(String type, String source,
514:                    String id, Map params) throws Exception {
515:
516:                redirector.reset();
517:
518:                ServiceSelector selector = null;
519:                Interpreter interpreter = null;
520:                SourceResolver resolver = null;
521:
522:                try {
523:                    selector = (ServiceSelector) this .lookup(Interpreter.ROLE);
524:                    assertNotNull("Test lookup of interpreter selector",
525:                            selector);
526:
527:                    resolver = (SourceResolver) this 
528:                            .lookup(SourceResolver.ROLE);
529:                    assertNotNull("Test lookup of source resolver", resolver);
530:
531:                    assertNotNull("Test if interpreter name is not null", type);
532:                    interpreter = (Interpreter) selector.select(type);
533:                    assertNotNull("Test lookup of interpreter", interpreter);
534:
535:                    ((AbstractInterpreter) interpreter).register(source);
536:
537:                    ArrayList parameters = new ArrayList();
538:                    for (Iterator i = params.entrySet().iterator(); i.hasNext();) {
539:                        Map.Entry me = (Map.Entry) i.next();
540:                        String name = (String) me.getKey();
541:                        String value = (String) me.getValue();
542:                        parameters.add(new Interpreter.Argument(name, value));
543:                    }
544:                    interpreter.handleContinuation(id, parameters,
545:                            getRedirector());
546:
547:                } catch (ServiceException ce) {
548:                    getLogger().error("Could not retrieve interpreter", ce);
549:                    fail("Could not retrieve interpreter: " + ce.toString());
550:                } finally {
551:                    if (interpreter != null) {
552:                        selector.release(interpreter);
553:                    }
554:                    this .release(selector);
555:                    this .release(resolver);
556:                }
557:                return FlowHelper.getWebContinuation(getObjectModel()).getId();
558:            }
559:
560:            public Object getFlowContextObject() {
561:                return FlowHelper.getContextObject(getObjectModel());
562:            }
563:
564:            public final void print(Document document) {
565:                TransformerFactory factory = TransformerFactory.newInstance();
566:                try {
567:                    javax.xml.transform.Transformer serializer = factory
568:                            .newTransformer();
569:                    serializer.transform(new DOMSource(document),
570:                            new StreamResult(System.out));
571:                    System.out.println();
572:                } catch (TransformerException te) {
573:                    te.printStackTrace();
574:                }
575:            }
576:
577:            public final Document load(String source) {
578:
579:                SourceResolver resolver = null;
580:                SAXParser parser = null;
581:                Source assertionsource = null;
582:
583:                assertNotNull("Test for component manager", this .getManager());
584:
585:                Document assertiondocument = null;
586:                try {
587:                    resolver = (SourceResolver) this 
588:                            .lookup(SourceResolver.ROLE);
589:                    assertNotNull("Test lookup of source resolver", resolver);
590:
591:                    parser = (SAXParser) this .lookup(SAXParser.ROLE);
592:                    assertNotNull("Test lookup of parser", parser);
593:
594:                    assertNotNull("Test if assertion document is not null",
595:                            source);
596:                    assertionsource = resolver.resolveURI(source);
597:                    assertNotNull("Test lookup of assertion source",
598:                            assertionsource);
599:                    assertTrue("Test if source exist", assertionsource.exists());
600:
601:                    DOMBuilder builder = new DOMBuilder();
602:                    assertNotNull(
603:                            "Test if inputstream of the assertion source is not null",
604:                            assertionsource.getInputStream());
605:
606:                    parser.parse(new InputSource(assertionsource
607:                            .getInputStream()), new WhitespaceFilter(builder),
608:                            builder);
609:
610:                    assertiondocument = builder.getDocument();
611:                    assertNotNull("Test if assertion document exists",
612:                            assertiondocument);
613:
614:                } catch (ServiceException ce) {
615:                    getLogger().error("Could not retrieve generator", ce);
616:                    fail("Could not retrieve generator: " + ce.toString());
617:                } catch (Exception e) {
618:                    getLogger().error("Could not execute test", e);
619:                    fail("Could not execute test: " + e);
620:                } finally {
621:                    if (resolver != null) {
622:                        resolver.release(assertionsource);
623:                    }
624:                    this .release(resolver);
625:                    this .release(parser);
626:                }
627:
628:                return assertiondocument;
629:            }
630:
631:            /**
632:             * Load a binary document.
633:             *
634:             * @param source Source location.
635:             *
636:             * @return Binary data.
637:             */
638:            public final byte[] loadByteArray(String source) {
639:
640:                SourceResolver resolver = null;
641:                SAXParser parser = null;
642:                Source assertionsource = null;
643:
644:                assertNotNull("Test for component manager", this .getManager());
645:
646:                byte[] assertiondocument = null;
647:
648:                try {
649:                    resolver = (SourceResolver) this 
650:                            .lookup(SourceResolver.ROLE);
651:                    assertNotNull("Test lookup of source resolver", resolver);
652:
653:                    parser = (SAXParser) this .lookup(SAXParser.ROLE);
654:                    assertNotNull("Test lookup of parser", parser);
655:
656:                    assertNotNull("Test if assertion document is not null",
657:                            source);
658:                    assertionsource = resolver.resolveURI(source);
659:                    assertNotNull("Test lookup of assertion source",
660:                            assertionsource);
661:                    assertTrue("Test if source exist", assertionsource.exists());
662:
663:                    assertNotNull(
664:                            "Test if inputstream of the assertion source is not null",
665:                            assertionsource.getInputStream());
666:
667:                    InputStream input = assertionsource.getInputStream();
668:                    long size = assertionsource.getContentLength();
669:
670:                    assertiondocument = new byte[(int) size];
671:                    int i = 0;
672:                    int c;
673:
674:                    while ((c = input.read()) != -1) {
675:                        assertiondocument[i] = (byte) c;
676:                        i++;
677:                    }
678:
679:                } catch (ServiceException ce) {
680:                    getLogger().error("Could not retrieve generator", ce);
681:                    fail("Could not retrieve generator: " + ce.toString());
682:                } catch (Exception e) {
683:                    getLogger().error("Could not execute test", e);
684:                    fail("Could not execute test: " + e);
685:                } finally {
686:                    if (resolver != null) {
687:                        resolver.release(assertionsource);
688:                    }
689:                    this .release(resolver);
690:                    this .release(parser);
691:                }
692:
693:                return assertiondocument;
694:            }
695:
696:            /**
697:             * Compare two XML documents provided as strings
698:             * @param control Control document
699:             * @param test Document to test
700:             * @return Diff object describing differences in documents
701:             */
702:            public final Diff compareXML(Document control, Document test) {
703:                return new Diff(control, test);
704:            }
705:
706:            /**
707:             * Assert that the result of an XML comparison is similar.
708:             *
709:             * @param msg The assertion message
710:             * @param expected The expected XML document
711:             * @param actual The actual XML Document
712:             */
713:            public final void assertEqual(String msg, Document expected,
714:                    Document actual) {
715:
716:                expected.getDocumentElement().normalize();
717:                actual.getDocumentElement().normalize();
718:
719:                Diff diff = compareXML(expected, actual);
720:
721:                assertEquals(msg + ", " + diff.toString(), true, diff.similar());
722:            }
723:
724:            /**
725:             * Assert that the result of an XML comparison is similar.
726:             *
727:             * @param expected The expected XML document
728:             * @param actual The actual XML Document
729:             */
730:            public final void assertEqual(Document expected, Document actual) {
731:
732:                expected.getDocumentElement().normalize();
733:                actual.getDocumentElement().normalize();
734:
735:                Diff diff = compareXML(expected, actual);
736:
737:                assertEquals("Test if the assertion document is equal, "
738:                        + diff.toString(), true, diff.similar());
739:            }
740:
741:            /**
742:             * Assert that the result of an XML comparison is identical.
743:             *
744:             * @param msg The assertion message
745:             * @param expected The expected XML document
746:             * @param actual The actual XML Document
747:             */
748:            public final void assertIdentical(String msg, Document expected,
749:                    Document actual) {
750:
751:                expected.getDocumentElement().normalize();
752:                actual.getDocumentElement().normalize();
753:
754:                Diff diff = compareXML(expected, actual);
755:
756:                assertEquals(msg + ", " + diff.toString(), true, diff
757:                        .identical());
758:            }
759:
760:            /**
761:             * Assert that the result of an XML comparison is identical.
762:             *
763:             * @param expected The expected XML document
764:             * @param actual The actual XML Document
765:             */
766:            public final void assertIdentical(Document expected, Document actual) {
767:
768:                expected.getDocumentElement().normalize();
769:                actual.getDocumentElement().normalize();
770:
771:                Diff diff = compareXML(expected, actual);
772:
773:                assertEquals("Test if the assertion document is equal, "
774:                        + diff.toString(), true, diff.identical());
775:            }
776:
777:            /**
778:             * Assert that the result of a byte comparison is identical.
779:             *
780:             * @param expected The expected byte array
781:             * @param actual The actual byte array
782:             */
783:            public final void assertIdentical(byte[] expected, byte[] actual) {
784:                assertEquals("Byte arrays of differing sizes, ",
785:                        expected.length, actual.length);
786:
787:                if (expected.length > 0) {
788:                    for (int i = 0; i < expected.length; i++) {
789:                        assertEquals("Byte array differs at index " + i,
790:                                expected[i], actual[i]);
791:                    }
792:                }
793:
794:            }
795:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.