01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.xml;
18:
19: import org.custommonkey.xmlunit.XMLTestCase;
20: import org.xml.sax.ContentHandler;
21: import org.xml.sax.SAXException;
22: import org.xml.sax.helpers.AttributesImpl;
23: import org.apache.cocoon.xml.dom.DOMBuilder;
24:
25: import javax.xml.transform.TransformerFactory;
26: import javax.xml.transform.Transformer;
27: import javax.xml.transform.Source;
28: import javax.xml.transform.Result;
29: import javax.xml.transform.stream.StreamResult;
30: import javax.xml.transform.dom.DOMSource;
31: import java.io.ByteArrayOutputStream;
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 s) {
43: super (s);
44: }
45:
46: protected void generateLargeSAX(ContentHandler consumer)
47: throws SAXException {
48: AttributesImpl atts = new AttributesImpl();
49:
50: final int size = 65000;
51: char[] large = new char[size];
52: for (int i = 0; i < size; i++) {
53: large[i] = 'x';
54: }
55:
56: consumer.startDocument();
57: consumer.startElement("", "root", "root", atts);
58: consumer.characters(large, 0, size);
59: consumer.endElement("", "root", "root");
60: consumer.endDocument();
61: }
62:
63: protected void generateSmallSAX(ContentHandler consumer)
64: throws SAXException {
65: AttributesImpl atts = new AttributesImpl();
66:
67: consumer.startDocument();
68: consumer.startElement("", "root", "root", atts);
69: consumer.characters("test".toCharArray(), 0, 4);
70: consumer.endElement("", "root", "root");
71: consumer.endDocument();
72: }
73:
74: protected byte[] generateByteArray() throws Exception {
75: DOMBuilder in = new DOMBuilder();
76: generateSmallSAX(in);
77: ByteArrayOutputStream bos = new ByteArrayOutputStream();
78: TransformerFactory tFactory = TransformerFactory.newInstance();
79: Transformer t = tFactory.newTransformer();
80: Source input = new DOMSource(in.getDocument());
81: Result output = new StreamResult(bos);
82: t.transform(input, output);
83: bos.close();
84:
85: return bos.toByteArray();
86: }
87: }
|