001: /* Copyright 2003, 2004 Elliotte Rusty Harold
002:
003: This library is free software; you can redistribute it and/or modify
004: it under the terms of version 2.1 of the GNU Lesser General Public
005: License as published by the Free Software Foundation.
006:
007: This library is distributed in the hope that it will be useful,
008: but WITHOUT ANY WARRANTY; without even the implied warranty of
009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: GNU Lesser General Public License for more details.
011:
012: You should have received a copy of the GNU Lesser General Public
013: License along with this library; if not, write to the
014: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
015: Boston, MA 02111-1307 USA
016:
017: You can contact Elliotte Rusty Harold by sending e-mail to
018: elharo@metalab.unc.edu. Please include the word "XOM" in the
019: subject line. The XOM home page is located at http://www.xom.nu/
020: */
021: package nu.xom.tests;
022:
023: import java.io.BufferedReader;
024: import java.io.IOException;
025: import java.io.PipedReader;
026: import java.io.PipedWriter;
027: import java.io.Reader;
028: import java.io.Writer;
029:
030: import nu.xom.Attribute;
031: import nu.xom.Builder;
032: import nu.xom.Element;
033: import nu.xom.NodeFactory;
034: import nu.xom.Nodes;
035: import nu.xom.ParsingException;
036:
037: /**
038: * <p>
039: * Test that XOM can handle really big files.
040: * </p>
041: *
042: * @author Elliotte Rusty Harold
043: * @version 1.0
044: *
045: */
046: public class MegaTest extends XOMTestCase {
047:
048: private Reader in;
049: private Writer out;
050: private Builder builder;
051: private final static int expectedResult = 200000000;
052: private int actualResult = 0;
053: private Thread generator;
054:
055: public MegaTest(String name) {
056: super (name);
057: }
058:
059: public static void main(String[] args) throws Exception {
060: MegaTest test = new MegaTest("MegaTest");
061: test.setUp();
062: test.testMegaFile();
063: }
064:
065: protected void setUp() throws IOException {
066: PipedReader pin = new PipedReader();
067: out = new PipedWriter(pin);
068: in = new BufferedReader(pin);
069: actualResult = 0;
070: builder = new Builder(new MinimizingFactory());
071: generator = new Generator();
072: generator.start();
073: }
074:
075: class Generator extends Thread {
076:
077: public void run() {
078: try {
079: out.write("<?xml version='1.0'?>\n");
080: out.write("<root>\n");
081: for (int i = 0; i < expectedResult; i++) {
082: out.write(" <data>1</data>\n");
083: // out.flush();
084: if (i % 10000 == 0) {
085: System.out.println(i / 10000);
086: }
087: }
088: out.write("</root>\n");
089: out.close();
090: } catch (IOException ex) {
091: fail("threw IOException " + ex);
092: }
093:
094: }
095:
096: }
097:
098: public void testMegaFile() throws IOException, ParsingException {
099:
100: builder.build(in);
101: assertEquals(expectedResult, actualResult);
102:
103: }
104:
105: private class MinimizingFactory extends NodeFactory {
106:
107: private Nodes empty = new Nodes();
108:
109: public Nodes makeComment(String data) {
110: return empty;
111: }
112:
113: public Nodes finishMakingElement(Element element) {
114: if (element.getQualifiedName().equals("data")) {
115: actualResult += Integer.parseInt(element.getValue());
116: return empty;
117: }
118: return new Nodes(element);
119: }
120:
121: public Nodes makeAttribute(String name, String URI,
122: String value, Attribute.Type type) {
123: return empty;
124: }
125:
126: public Nodes makeDocType(String rootElementName,
127: String publicID, String systemID) {
128: return empty;
129: }
130:
131: public Nodes makeText(String data) {
132: data = data.trim();
133: if ("".equals(data))
134: return empty;
135: return super .makeText(data);
136: }
137:
138: public Nodes makeProcessingInstruction(String target,
139: String data) {
140: return empty;
141: }
142:
143: }
144:
145: }
|