KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > xml > AbstractXMLTestCase


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 package org.apache.cocoon.xml;
17
18 import org.custommonkey.xmlunit.XMLTestCase;
19 import org.xml.sax.ContentHandler JavaDoc;
20 import org.xml.sax.SAXException JavaDoc;
21 import org.xml.sax.helpers.AttributesImpl JavaDoc;
22 import org.apache.cocoon.xml.dom.DOMBuilder;
23
24 import javax.xml.transform.TransformerFactory JavaDoc;
25 import javax.xml.transform.Transformer JavaDoc;
26 import javax.xml.transform.Source JavaDoc;
27 import javax.xml.transform.Result JavaDoc;
28 import javax.xml.transform.stream.StreamResult JavaDoc;
29 import javax.xml.transform.dom.DOMSource JavaDoc;
30 import java.io.ByteArrayOutputStream JavaDoc;
31
32
33 /**
34  * general functions for XML related Testcases
35  *
36  * @author <a HREF="mailto:tcurdt@apache.org">Torsten Curdt</a>
37  * @version
38  */

39
40 public abstract class AbstractXMLTestCase extends XMLTestCase {
41
42     public AbstractXMLTestCase(String JavaDoc s) {
43         super(s);
44     }
45
46     protected void generateLargeSAX( ContentHandler JavaDoc consumer ) throws SAXException JavaDoc {
47         AttributesImpl atts = new AttributesImpl();
48
49         final int size = 65000;
50         char[] large = new char[size];
51         for(int i=0;i<size;i++) {
52             large[i] = 'x';
53         }
54
55         consumer.startDocument();
56         consumer.startElement("", "root", "root", atts);
57         consumer.characters(large,0,size);
58         consumer.endElement("", "root", "root");
59         consumer.endDocument();
60     }
61
62     protected void generateSmallSAX( ContentHandler JavaDoc consumer ) throws SAXException JavaDoc {
63         AttributesImpl atts = new AttributesImpl();
64
65         consumer.startDocument();
66         consumer.startElement("", "root", "root", atts);
67         consumer.characters("test".toCharArray(),0,4);
68         consumer.endElement("", "root", "root");
69         consumer.endDocument();
70     }
71
72     protected byte[] generateByteArray() throws Exception JavaDoc {
73         DOMBuilder in = new DOMBuilder();
74         generateSmallSAX(in);
75         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
76         TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
77         Transformer JavaDoc t = tFactory.newTransformer();
78         Source JavaDoc input = new DOMSource JavaDoc(in.getDocument());
79         Result JavaDoc output = new StreamResult JavaDoc(bos);
80         t.transform(input, output);
81         bos.close();
82
83         return bos.toByteArray();
84     }
85 }
86
87
Popular Tags