KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > SitemapComponentTestCase


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.cocoon;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.xml.transform.TransformerException JavaDoc;
28 import javax.xml.transform.TransformerFactory JavaDoc;
29 import javax.xml.transform.dom.DOMSource JavaDoc;
30 import javax.xml.transform.stream.StreamResult JavaDoc;
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 JavaDoc;
61 import org.xml.sax.InputSource JavaDoc;
62 import org.xml.sax.SAXException JavaDoc;
63
64 /**
65  * Testcase for actions, generators, transformers and serializer components.
66  *
67  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
68  * @author <a HREF="mailto:mark.leicester@energyintellect.com">Mark Leicester</a>
69  * @version CVS $Id: SitemapComponentTestCase.java 124620 2005-01-08 04:02:22Z antonio $
70  */

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 JavaDoc objectmodel = new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc {
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     /**
124      * Match with a pattern.
125      *
126      * @param type Hint of the matcher.
127      * @param pattern Pattern for the matcher.
128      * @param parameters Matcher parameters.
129      */

130     public final Map JavaDoc match(String JavaDoc type, String JavaDoc pattern, Parameters parameters) throws PatternException {
131
132         ServiceSelector selector = null;
133         Matcher matcher = null;
134         SourceResolver resolver = null;
135
136         Map JavaDoc 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     /**
165      * Select with a pattern.
166      *
167      * @param type Hint of the matcher.
168      * @param expression Expression for the selector.
169      * @param parameters Matcher parameters.
170      */

171     public final boolean select(String JavaDoc type, String JavaDoc 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     /**
207      * Perform the action component.
208      *
209      * @param type Hint of the action.
210      * @param source Source for the action.
211      * @param parameters Action parameters.
212      */

213     public final Map JavaDoc act(String JavaDoc type, String JavaDoc source, Parameters parameters) throws Exception JavaDoc {
214         
215         redirector.reset();
216
217         ServiceSelector selector = null;
218         Action action = null;
219         SourceResolver resolver = null;
220
221         Map JavaDoc 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     /**
251      * Generate the generator output.
252      *
253      * @param type Hint of the generator.
254      * @param source Source for the generator.
255      * @param parameters Generator parameters.
256      */

257     public final Document JavaDoc generate(String JavaDoc type, String JavaDoc source, Parameters parameters)
258         throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
259
260         ServiceSelector selector = null;
261         Generator generator = null;
262         SourceResolver resolver = null;
263         SAXParser parser = null;
264
265         Document JavaDoc 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     /**
310      * Trannsform a document by a transformer
311      *
312      * @param type Hint of the transformer.
313      * @param source Source for the transformer.
314      * @param parameters Generator parameters.
315      * @param input Input document.
316      */

317     public final Document JavaDoc transform(String JavaDoc type, String JavaDoc source, Parameters parameters, Document JavaDoc input)
318         throws SAXException JavaDoc, ProcessingException, IOException JavaDoc {
319
320         ServiceSelector selector = null;
321         Transformer transformer = null;
322         SourceResolver resolver = null;
323         SAXParser parser = null;
324         Source JavaDoc inputsource = null;
325
326         assertNotNull("Test for component manager", this.getManager());
327
328         Document JavaDoc 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     /**
388      * Serialize a document by a serializer
389      *
390      * @param type Hint of the serializer.
391      * @param parameters Serializer parameters.
392      * @param input Input document.
393      *
394      * @return Serialized data.
395      */

396     public final byte[] serialize(String JavaDoc type, Parameters parameters,
397                                   Document JavaDoc input) throws SAXException JavaDoc, IOException JavaDoc{
398
399         ServiceSelector selector = null;
400         Serializer serializer = null;
401         SourceResolver resolver = null;
402         Source JavaDoc inputsource = null;
403
404         assertNotNull("Test for component manager", this.getManager());
405
406         ByteArrayOutputStream JavaDoc 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 JavaDoc();
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 JavaDoc callFunction(String JavaDoc type, String JavaDoc source, String JavaDoc function, Map JavaDoc params) throws Exception JavaDoc {
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 JavaDoc parameters = new ArrayList JavaDoc();
473             for (Iterator JavaDoc i = params.entrySet().iterator(); i.hasNext();) {
474                 Map.Entry JavaDoc me = (Map.Entry JavaDoc)i.next();
475                 String JavaDoc name = (String JavaDoc)me.getKey();
476                 String JavaDoc value = (String JavaDoc)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 JavaDoc callContinuation(String JavaDoc type, String JavaDoc source, String JavaDoc id, Map JavaDoc params) throws Exception JavaDoc {
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 JavaDoc parameters = new ArrayList JavaDoc();
516             for (Iterator JavaDoc i = params.entrySet().iterator(); i.hasNext();) {
517                 Map.Entry JavaDoc me = (Map.Entry JavaDoc)i.next();
518                 String JavaDoc name = (String JavaDoc)me.getKey();
519                 String JavaDoc value = (String JavaDoc)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 JavaDoc getFlowContextObject() {
538         return FlowHelper.getContextObject(getObjectModel());
539     }
540
541     public final void print(Document JavaDoc document) {
542         TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
543         try
544         {
545           javax.xml.transform.Transformer JavaDoc serializer = factory.newTransformer();
546           serializer.transform(new DOMSource JavaDoc(document), new StreamResult JavaDoc(System.out));
547           System.out.println();
548         }
549         catch (TransformerException JavaDoc te)
550         {
551           te.printStackTrace();
552         }
553     }
554
555     public final Document JavaDoc load(String JavaDoc source) {
556
557         SourceResolver resolver = null;
558         SAXParser parser = null;
559         Source JavaDoc assertionsource = null;
560
561         assertNotNull("Test for component manager", this.getManager());
562
563         Document JavaDoc 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 JavaDoc(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 JavaDoc 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     /**
607      * Load a binary document.
608      *
609      * @param source Source location.
610      *
611      * @return Binary data.
612      */

613     public final byte[] loadByteArray(String JavaDoc source) {
614
615         SourceResolver resolver = null;
616         SAXParser parser = null;
617         Source JavaDoc 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 JavaDoc 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 JavaDoc 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     /**
668      * Compare two XML documents provided as strings
669      * @param control Control document
670      * @param test Document to test
671      * @return Diff object describing differences in documents
672      */

673     public final Diff compareXML(Document JavaDoc control, Document JavaDoc test) {
674         return new Diff(control, test);
675     }
676
677     /**
678      * Assert that the result of an XML comparison is similar.
679      *
680      * @param msg The assertion message
681      * @param expected The expected XML document
682      * @param actual The actual XML Document
683      */

684     public final void assertEqual(String JavaDoc msg, Document JavaDoc expected, Document JavaDoc 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     /**
695      * Assert that the result of an XML comparison is similar.
696      *
697      * @param expected The expected XML document
698      * @param actual The actual XML Document
699      */

700     public final void assertEqual(Document JavaDoc expected, Document JavaDoc 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     /**
711      * Assert that the result of an XML comparison is identical.
712      *
713      * @param msg The assertion message
714      * @param expected The expected XML document
715      * @param actual The actual XML Document
716      */

717     public final void assertIdentical(String JavaDoc msg, Document JavaDoc expected, Document JavaDoc 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     /**
728      * Assert that the result of an XML comparison is identical.
729      *
730      * @param expected The expected XML document
731      * @param actual The actual XML Document
732      */

733     public final void assertIdentical(Document JavaDoc expected, Document JavaDoc 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     /**
744      * Assert that the result of a byte comparison is identical.
745      *
746      * @param expected The expected byte array
747      * @param actual The actual byte array
748      */

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