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: package org.apache.cocoon.components.sax;
018:
019: import org.xml.sax.helpers.DefaultHandler;
020: import org.xml.sax.ContentHandler;
021: import org.apache.cocoon.xml.dom.DOMBuilder;
022: import org.apache.cocoon.xml.AbstractXMLTestCase;
023: import org.apache.cocoon.xml.DefaultHandlerWrapper;
024: import javax.xml.parsers.SAXParser;
025: import javax.xml.parsers.SAXParserFactory;
026: import java.io.ByteArrayInputStream;
027:
028: /**
029: * Testcase for XMLByteStreamCompiler and Interpreter
030: *
031: * @author <a href="mailto:tcurdt@apache.org">Torsten Curdt</a>
032: * @version
033: */
034:
035: public final class XMLByteStreamCompilerInterpreterTestCase extends
036: AbstractXMLTestCase {
037: public XMLByteStreamCompilerInterpreterTestCase(String s) {
038: super (s);
039: }
040:
041: public void testCompareDOM() throws Exception {
042: // reference
043: DOMBuilder in = new DOMBuilder();
044: generateLargeSAX(in);
045:
046: // capture events
047: XMLByteStreamCompiler xmlc = new XMLByteStreamCompiler();
048: generateLargeSAX(xmlc);
049:
050: // recall events and build a DOM from it
051: XMLByteStreamInterpreter xmli = new XMLByteStreamInterpreter();
052: DOMBuilder out = new DOMBuilder();
053: xmli.setConsumer(out);
054: xmli.deserialize(xmlc.getSAXFragment());
055:
056: // compare DOMs
057: assertXMLEqual(in.getDocument(), out.getDocument());
058: }
059:
060: public void testCompareByteArray() throws Exception {
061: // capture events
062: XMLByteStreamCompiler sa = new XMLByteStreamCompiler();
063: generateLargeSAX(sa);
064:
065: // serialize events
066: byte[] aa = (byte[]) sa.getSAXFragment();
067:
068: // deserialize and capture
069: XMLByteStreamCompiler sb = new XMLByteStreamCompiler();
070: XMLByteStreamInterpreter xmli = new XMLByteStreamInterpreter();
071: xmli.setConsumer(sb);
072: xmli.deserialize(aa);
073:
074: // serialize again
075: byte[] ab = (byte[]) sb.getSAXFragment();
076:
077: assertTrue(aa.length == ab.length);
078:
079: for (int i = 0; i < aa.length; i++) {
080: assertEquals(aa[i], ab[i]);
081: }
082: }
083:
084: public void testStressLoop() throws Exception {
085: XMLByteStreamCompiler xmlc = new XMLByteStreamCompiler();
086:
087: long loop = 10000;
088:
089: // simply consume documents
090: long start = System.currentTimeMillis();
091: for (int i = 0; i < loop; i++) {
092: generateSmallSAX(xmlc);
093: xmlc.recycle();
094: }
095: long stop = System.currentTimeMillis();
096:
097: double r = 1000 * loop / (stop - start);
098: System.out.println("consuming: " + r + " documents per second");
099: }
100:
101: public void testCompareToParsing() throws Exception {
102: DOMBuilder in = new DOMBuilder();
103: generateSmallSAX(in);
104:
105: SAXParserFactory pfactory = SAXParserFactory.newInstance();
106: SAXParser p = pfactory.newSAXParser();
107:
108: XMLByteStreamCompiler xmlc = new XMLByteStreamCompiler();
109: DefaultHandlerWrapper wrapper = new DefaultHandlerWrapper(xmlc);
110:
111: ByteArrayInputStream bis = new ByteArrayInputStream(
112: generateByteArray());
113:
114: long loop = 10000;
115:
116: // parse documents
117: long start = System.currentTimeMillis();
118: for (int i = 0; i < loop; i++) {
119: xmlc.recycle();
120: bis.reset();
121: p.parse(bis, wrapper);
122: }
123: long stop = System.currentTimeMillis();
124:
125: double r = 1000 * loop / (stop - start);
126: System.out.println("parsed: " + r + " documents per second");
127:
128: XMLByteStreamInterpreter xmli = new XMLByteStreamInterpreter();
129: ContentHandler ch = new DefaultHandler();
130:
131: // recall documents
132: start = System.currentTimeMillis();
133: for (int i = 0; i < loop; i++) {
134: xmli.setContentHandler(ch);
135: xmli.deserialize(xmlc.getSAXFragment());
136: }
137: stop = System.currentTimeMillis();
138:
139: r = 1000 * loop / (stop - start);
140: System.out.println("recalling: " + r + " documents per second");
141: }
142: }
|