KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > xmlfs > XMLFSvsParserTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.filesystems.xmlfs;
21
22 import java.io.*;
23 import java.net.URL JavaDoc;
24
25 import org.xml.sax.*;
26
27 import org.openide.filesystems.XMLFileSystem;
28
29
30
31 /** A test that should measure and compare the speed of parsing an
32  * XML document to creation of new XMLFileSystem
33  *
34  * @author Jaroslav Tulach
35  */

36 public class XMLFSvsParserTest extends org.netbeans.performance.Benchmark
37 implements EntityResolver {
38     private static final String JavaDoc PUBLIC_ID = "-//NetBeans//DTD Filesystem 1.0//EN";// NOI18N
39
private static final String JavaDoc DTD_PATH = "/org/openide/filesystems/filesystem.dtd";// NOI18N
40
/** URL of a document to parse */
41     private URL JavaDoc url;
42     
43     /** Constructor
44      */

45     public XMLFSvsParserTest (String JavaDoc name) {
46         super (name, new Object JavaDoc[] {
47             new Integer JavaDoc (100),
48             new Integer JavaDoc (1000),
49             new Integer JavaDoc (10000)
50         });
51     }
52     
53     /** A main method.
54      */

55     public static void main(java.lang.String JavaDoc[] args) {
56         junit.textui.TestRunner.run(new junit.framework.TestSuite (XMLFSvsParserTest.class));
57     }
58     
59     /** Setups the test.
60      */

61     protected void setUp () throws Exception JavaDoc {
62         File file = File.createTempFile ("afile", null);
63         file.deleteOnExit ();
64         
65         Integer JavaDoc count = (Integer JavaDoc)getArgument ();
66         
67         prepareXmlFilesystem (file, count.intValue (), "anyfilewithanyvalues");
68         
69         url = file.toURL ();
70     }
71         
72     
73     /** Tests how quickly a parser can parse the file.
74      */

75     public void testXMLParser () throws Exception JavaDoc {
76         int cnt = getIterationCount ();
77         
78         while (cnt-- > 0) {
79             org.xml.sax.HandlerBase JavaDoc base = new org.xml.sax.HandlerBase JavaDoc ();
80             javax.xml.parsers.SAXParserFactory JavaDoc factory = javax.xml.parsers.SAXParserFactory.newInstance ();
81             factory.setValidating (false);
82             org.xml.sax.Parser JavaDoc parser = factory.newSAXParser ().getParser ();
83             parser.setEntityResolver(this);
84             parser.setDocumentHandler (base);
85             
86             parser.parse (new InputSource (url.openStream ()));
87         }
88     }
89         
90
91     /** Tests how quickly an XML fs can be created.
92      */

93     public void testXMLFS () throws Exception JavaDoc {
94         int cnt = getIterationCount ();
95         while (cnt-- > 0) {
96             XMLFileSystem fs = new XMLFileSystem (url);
97         }
98     }
99
100     /** Implements entity resolver.
101      */

102     public InputSource resolveEntity(java.lang.String JavaDoc pid,java.lang.String JavaDoc sid) throws SAXException {
103         if (pid != null && pid.equals(PUBLIC_ID))
104             return new InputSource (getClass ().getResourceAsStream (DTD_PATH));
105
106         return new InputSource (sid);
107     }
108     
109         
110     /** Writes the content of a file
111      */

112     private static void prepareXmlFilesystem( File jar, int count, String JavaDoc prefix ) throws IOException {
113         FileOutputStream stream = new FileOutputStream( jar );
114         OutputStreamWriter writer = new OutputStreamWriter( stream, "UTF8" );
115         PrintWriter print = new PrintWriter( writer );
116         print.println(
117             "<?xml version=\"1.0\"?>" +
118             "<!DOCTYPE filesystem PUBLIC " +
119             "\"-//NetBeans//DTD Filesystem 1.0//EN\" " +
120             "\"http://www.netbeans.org/dtds/filesystem-1_0.dtd\">\n" +
121             "<filesystem>"
122         );
123
124         for( int i=0; i<count; i++ ) {
125             print.println( "<file name=\"" + prefix + i + "\"/>" );
126         }
127         print.println( "</filesystem>" );
128         print.flush();
129         print.close();
130     }
131         
132 }
133
Popular Tags