001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2007 Sun Microsystems, Inc.
038: */
039: package org.netbeans.api.xml.lexer;
040:
041: import java.io.BufferedReader;
042: import java.io.InputStream;
043: import java.io.InputStreamReader;
044: import javax.swing.text.Document;
045: import junit.framework.*;
046: import org.netbeans.api.lexer.Language;
047: import org.netbeans.api.lexer.Token;
048: import org.netbeans.api.lexer.TokenHierarchy;
049: import org.netbeans.api.lexer.TokenSequence;
050: import org.netbeans.modules.editor.NbEditorKit;
051: import org.netbeans.modules.editor.NbEditorDocument;
052:
053: /**
054: * The XMLTokenIdTest tests the parsing algorithm of XMLLexer.
055: * Various tests include, sanity, regression, performance etc.
056: * @author Samaresh (samaresh.panda@sun.com)
057: */
058: public class XMLTokenIdTest extends TestCase {
059:
060: public XMLTokenIdTest(String testName) {
061: super (testName);
062: }
063:
064: @Override
065: public void setUp() throws Exception {
066: }
067:
068: @Override
069: public void tearDown() {
070: }
071:
072: public static Test suite() {
073: TestSuite suite = new TestSuite();
074: suite.addTest(new XMLTokenIdTest("testTokens"));
075: //two regression tests
076: suite.addTest(new XMLTokenIdTest("testParse1"));
077: suite.addTest(new XMLTokenIdTest("testParse2"));
078: //measure performace
079: suite.addTest(new XMLTokenIdTest("testParsePerformance"));
080: return suite;
081: }
082:
083: /**
084: * Parses a XML document using XMLLexer and loops through all tokens.
085: * @param document
086: * @throws java.lang.Exception
087: */
088: private void parse(javax.swing.text.Document document)
089: throws Exception {
090: TokenHierarchy th = TokenHierarchy.get(document);
091: TokenSequence ts = th.tokenSequence();
092: assert (true);
093: while (ts.moveNext()) {
094: Token token = ts.token();
095: assert (token.id() != null);
096: }
097: }
098:
099: /**
100: * This test parses a particular schema that was earlier failing.
101: * See http://www.netbeans.org/issues/show_bug.cgi?id=124731
102: * See http://hg.netbeans.org/main?cmd=changeset;node=34612be91839
103: */
104: public void testParse1() throws Exception {
105: javax.swing.text.Document document = getDocument("resources/UBL-CommonAggregateComponents-1.0.xsd");
106: parse(document);
107: }
108:
109: /**
110: * This test parses a particular schema that was earlier failing.
111: * See http://www.netbeans.org/issues/show_bug.cgi?id=125005
112: * See http://hg.netbeans.org/main?cmd=changeset;node=dcd138bddc6c
113: */
114: public void testParse2() throws Exception {
115: javax.swing.text.Document document = getDocument("resources/wsdl.xml");
116: parse(document);
117: }
118:
119: /**
120: * This test measures the performance of XMLLexer on healthcare schema.
121: */
122: public void testParsePerformance() throws Exception {
123: javax.swing.text.Document document = getDocument("resources/fields.xsd");
124: long start = System.currentTimeMillis();
125: parse(document);
126: long end = System.currentTimeMillis();
127: System.out.println("Time taken to parse healthcare schema: "
128: + (end - start) + "ms.");
129: }
130:
131: /**
132: * This test validates all tokens obtained by parsing test.xml against
133: * an array of expected tokens.
134: */
135: public void testTokens() throws Exception {
136: XMLTokenId[] expectedIds = { XMLTokenId.PI_START,
137: XMLTokenId.PI_TARGET, XMLTokenId.WS,
138: XMLTokenId.PI_CONTENT, XMLTokenId.PI_END,
139: XMLTokenId.TEXT, XMLTokenId.TAG, XMLTokenId.WS,
140: XMLTokenId.ARGUMENT, XMLTokenId.OPERATOR,
141: XMLTokenId.VALUE, XMLTokenId.TAG, XMLTokenId.TEXT,
142: XMLTokenId.TAG, XMLTokenId.WS, XMLTokenId.ARGUMENT,
143: XMLTokenId.OPERATOR, XMLTokenId.VALUE, XMLTokenId.WS,
144: XMLTokenId.ARGUMENT, XMLTokenId.OPERATOR,
145: XMLTokenId.VALUE, XMLTokenId.WS, XMLTokenId.ARGUMENT,
146: XMLTokenId.OPERATOR, XMLTokenId.VALUE, XMLTokenId.WS,
147: XMLTokenId.ARGUMENT, XMLTokenId.OPERATOR,
148: XMLTokenId.VALUE, XMLTokenId.WS, XMLTokenId.TAG,
149: XMLTokenId.TEXT, XMLTokenId.TAG, XMLTokenId.TAG,
150: XMLTokenId.TEXT, XMLTokenId.BLOCK_COMMENT,
151: XMLTokenId.BLOCK_COMMENT, XMLTokenId.BLOCK_COMMENT,
152: XMLTokenId.TEXT, XMLTokenId.TAG, XMLTokenId.TAG,
153: XMLTokenId.TEXT, XMLTokenId.TAG, XMLTokenId.TAG,
154: XMLTokenId.TEXT };
155:
156: javax.swing.text.Document document = getDocument("resources/test.xml");
157: TokenHierarchy th = TokenHierarchy.get(document);
158: TokenSequence ts = th.tokenSequence();
159: assert (ts.tokenCount() == expectedIds.length);
160: int index = 0;
161: while (ts.moveNext()) {
162: Token token = ts.token();
163: System.out.println("Id :[" + token.id().name()
164: + "] [Text :[" + token.text() + "]");
165: assert (token.id() == expectedIds[index]);
166: index++;
167: }
168: }
169:
170: private javax.swing.text.Document getDocument(String path)
171: throws Exception {
172: javax.swing.text.Document doc = getResourceAsDocument(path);
173: //must set the language inside unit tests
174: doc.putProperty(Language.class, XMLTokenId.language());
175: return doc;
176: }
177:
178: public static Document getResourceAsDocument(String path)
179: throws Exception {
180: InputStream in = XMLTokenIdTest.class.getResourceAsStream(path);
181: Document sd = (Document) new NbEditorDocument(NbEditorKit.class);
182: BufferedReader br = new BufferedReader(new InputStreamReader(
183: in, "UTF-8"));
184: StringBuffer sbuf = new StringBuffer();
185: try {
186: String line = null;
187: while ((line = br.readLine()) != null) {
188: sbuf.append(line);
189: sbuf.append(System.getProperty("line.separator"));
190: }
191: } finally {
192: br.close();
193: }
194: sd.insertString(0, sbuf.toString(), null);
195: return sd;
196: }
197: }
|