KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > sax > XMLByteStreamCompilerInterpreterTestCase


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.components.sax;
17
18 import org.xml.sax.helpers.DefaultHandler JavaDoc;
19 import org.xml.sax.ContentHandler JavaDoc;
20 import org.apache.cocoon.xml.dom.DOMBuilder;
21 import org.apache.cocoon.xml.AbstractXMLTestCase;
22 import org.apache.cocoon.xml.DefaultHandlerWrapper;
23 import javax.xml.parsers.SAXParser JavaDoc;
24 import javax.xml.parsers.SAXParserFactory JavaDoc;
25 import java.io.ByteArrayInputStream JavaDoc;
26
27 /**
28  * Testcase for XMLByteStreamCompiler and Interpreter
29  *
30  * @author <a HREF="mailto:tcurdt@apache.org">Torsten Curdt</a>
31  * @version
32  */

33
34 public final class XMLByteStreamCompilerInterpreterTestCase extends AbstractXMLTestCase {
35     public XMLByteStreamCompilerInterpreterTestCase(String JavaDoc s) {
36         super(s);
37     }
38
39     public void testCompareDOM() throws Exception JavaDoc {
40         // reference
41
DOMBuilder in = new DOMBuilder();
42         generateLargeSAX(in);
43
44         // capture events
45
XMLByteStreamCompiler xmlc = new XMLByteStreamCompiler();
46         generateLargeSAX(xmlc);
47
48         // recall events and build a DOM from it
49
XMLByteStreamInterpreter xmli = new XMLByteStreamInterpreter();
50         DOMBuilder out = new DOMBuilder();
51         xmli.setConsumer(out);
52         xmli.deserialize(xmlc.getSAXFragment());
53
54         // compare DOMs
55
assertXMLEqual(in.getDocument(), out.getDocument());
56     }
57
58     public void testCompareByteArray() throws Exception JavaDoc {
59         // capture events
60
XMLByteStreamCompiler sa = new XMLByteStreamCompiler();
61         generateLargeSAX(sa);
62
63         // serialize events
64
byte[] aa = (byte[]) sa.getSAXFragment();
65
66         // deserialize and capture
67
XMLByteStreamCompiler sb = new XMLByteStreamCompiler();
68         XMLByteStreamInterpreter xmli = new XMLByteStreamInterpreter();
69         xmli.setConsumer(sb);
70         xmli.deserialize(aa);
71
72         // serialize again
73
byte[] ab = (byte[]) sb.getSAXFragment();
74
75         assertTrue(aa.length == ab.length);
76
77         for (int i=0;i<aa.length;i++) {
78             assertEquals(aa[i],ab[i]);
79         }
80     }
81
82     public void testStressLoop() throws Exception JavaDoc {
83         XMLByteStreamCompiler xmlc = new XMLByteStreamCompiler();
84
85         long loop = 10000;
86
87         // simply consume documents
88
long start = System.currentTimeMillis();
89         for(int i=0;i<loop;i++) {
90             generateSmallSAX(xmlc);
91             xmlc.recycle();
92         }
93         long stop = System.currentTimeMillis();
94
95         double r = 1000*loop/(stop-start);
96         System.out.println("consuming: "+ r + " documents per second");
97     }
98
99     public void testCompareToParsing() throws Exception JavaDoc {
100         DOMBuilder in = new DOMBuilder();
101         generateSmallSAX(in);
102
103         SAXParserFactory JavaDoc pfactory = SAXParserFactory.newInstance();
104         SAXParser JavaDoc p = pfactory.newSAXParser();
105
106         XMLByteStreamCompiler xmlc = new XMLByteStreamCompiler();
107         DefaultHandlerWrapper wrapper = new DefaultHandlerWrapper(xmlc);
108
109         ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(generateByteArray());
110
111         long loop = 10000;
112
113         // parse documents
114
long start = System.currentTimeMillis();
115         for(int i=0;i<loop;i++) {
116             xmlc.recycle();
117             bis.reset();
118             p.parse(bis,wrapper);
119         }
120         long stop = System.currentTimeMillis();
121
122         double r = 1000*loop/(stop-start);
123         System.out.println("parsed: " + r + " documents per second");
124
125
126         XMLByteStreamInterpreter xmli = new XMLByteStreamInterpreter();
127         ContentHandler JavaDoc ch = new DefaultHandler JavaDoc();
128
129         // recall documents
130
start = System.currentTimeMillis();
131         for(int i=0;i<loop;i++) {
132             xmli.setContentHandler(ch);
133             xmli.deserialize(xmlc.getSAXFragment());
134         }
135         stop = System.currentTimeMillis();
136
137         r = 1000*loop/(stop-start);
138         System.out.println("recalling: " + r + " documents per second");
139     }
140 }
141
Popular Tags