KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > tests > MegaTest


1 /* Copyright 2003, 2004 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21 package nu.xom.tests;
22
23 import java.io.BufferedReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.PipedReader JavaDoc;
26 import java.io.PipedWriter JavaDoc;
27 import java.io.Reader JavaDoc;
28 import java.io.Writer JavaDoc;
29
30 import nu.xom.Attribute;
31 import nu.xom.Builder;
32 import nu.xom.Element;
33 import nu.xom.NodeFactory;
34 import nu.xom.Nodes;
35 import nu.xom.ParsingException;
36
37
38 /**
39  * <p>
40  * Test that XOM can handle really big files.
41  * </p>
42  *
43  * @author Elliotte Rusty Harold
44  * @version 1.0
45  *
46  */

47 public class MegaTest extends XOMTestCase {
48
49     private Reader JavaDoc in;
50     private Writer JavaDoc out;
51     private Builder builder;
52     private final static int expectedResult = 200000000;
53     private int actualResult = 0;
54     private Thread JavaDoc generator;
55     
56     public MegaTest(String JavaDoc name) {
57         super(name);
58     }
59     
60     
61     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
62         MegaTest test = new MegaTest("MegaTest");
63         test.setUp();
64         test.testMegaFile();
65     }
66
67     
68     protected void setUp() throws IOException JavaDoc {
69         PipedReader JavaDoc pin = new PipedReader JavaDoc();
70         out = new PipedWriter JavaDoc(pin);
71         in = new BufferedReader JavaDoc(pin);
72         actualResult = 0;
73         builder = new Builder(new MinimizingFactory());
74         generator = new Generator();
75         generator.start();
76     }
77     
78     
79     class Generator extends Thread JavaDoc {
80         
81         public void run() {
82             try {
83             out.write("<?xml version='1.0'?>\n");
84             out.write("<root>\n");
85             for (int i = 0; i < expectedResult; i++) {
86                 out.write(" <data>1</data>\n");
87                 // out.flush();
88
if (i % 10000 == 0) {
89                     System.out.println(i / 10000);
90                 }
91             }
92             out.write("</root>\n");
93             out.close();
94             }
95             catch (IOException JavaDoc ex) {
96                 fail("threw IOException " + ex);
97             }
98                        
99         }
100         
101     }
102     
103     
104     public void testMegaFile()
105       throws IOException JavaDoc, ParsingException {
106
107         builder.build(in);
108         assertEquals(expectedResult, actualResult);
109
110     }
111
112     
113     private class MinimizingFactory extends NodeFactory {
114
115         private Nodes empty = new Nodes();
116
117         public Nodes makeComment(String JavaDoc data) {
118             return empty;
119         }
120     
121         public Nodes finishMakingElement(Element element) {
122             if (element.getQualifiedName().equals("data")) {
123                 actualResult += Integer.parseInt(element.getValue());
124                 return empty;
125             }
126             return new Nodes(element);
127         }
128     
129         public Nodes makeAttribute(String JavaDoc name, String JavaDoc URI,
130           String JavaDoc value, Attribute.Type type) {
131             return empty;
132         }
133     
134         public Nodes makeDocType(String JavaDoc rootElementName,
135           String JavaDoc publicID, String JavaDoc systemID) {
136             return empty;
137         }
138     
139         public Nodes makeText(String JavaDoc data) {
140             data = data.trim();
141             if ("".equals(data)) return empty;
142             return super.makeText(data);
143         }
144     
145         public Nodes makeProcessingInstruction(
146           String JavaDoc target, String JavaDoc data) {
147             return empty;
148         }
149         
150     }
151
152 }
153
Popular Tags