KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > ext > html > HTMLSyntaxTest


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.netbeans.editor.ext.html;
21
22 import java.io.PrintStream JavaDoc;
23 import java.net.URL JavaDoc;
24 import org.netbeans.editor.TokenContext;
25 import org.netbeans.editor.TokenContextPath;
26 import org.netbeans.editor.TokenID;
27 import org.netbeans.editor.TokenItem;
28 import org.netbeans.editor.ext.html.HTMLSyntax;
29 import org.netbeans.junit.NbTestCase;
30
31 import org.openide.ErrorManager;
32
33 /** Basic html syntax parser tests.
34  *
35  * @author Marek Fukala
36  */

37 public class HTMLSyntaxTest extends NbTestCase {
38
39     //it's static since the junit creates a new instance of this class for each test method
40
private static HTMLSyntax syntax = new HTMLSyntax();
41     
42     public HTMLSyntaxTest() {
43         super("htmlsyntaxtest");
44     }
45     
46     public void setUp() {
47         //print out a header to the ref file
48
getRef().println("'token image' [offset, length]; tokenID name; tokenID id; token category name; <list of token context names>\n--------------------\n");
49     }
50     
51     public void tearDown() {
52         compareReferenceFiles();
53     }
54     
55     //test methods -----------
56

57     public void testHtml() {
58         dumpTokensForContent("<html>\n<body>\n<h1>hello</h1>\n</body>\n</html>");
59     }
60     
61     public void testErrorInTag() {
62         dumpTokensForContent("<html@><head #/></html>");
63     }
64     
65     public void testAttrs() {
66         dumpTokensForContent("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
67     }
68     
69     public void testErrorInAttrs() {
70         dumpTokensForContent("<meta 2 http-equiv=\"Content-Type\" content@=\"text/html; charset=UTF-8\">");
71     }
72     
73     public void testMultilineAttrs() {
74         dumpTokensForContent("<meta \n http-equiv=\"Content-Type\" \n content=\"text/html; charset=UTF-8\">");
75     }
76     
77     public void testSGMLEscape() {
78         dumpTokensForContent("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
79     }
80     
81     public void testSGMLEscapeOnMoreLines() {
82         dumpTokensForContent("<!DOCTYPE HTML \nPUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n>");
83     }
84
85     public void testHTMLComment() {
86         dumpTokensForContent("<html>\n<!-- this is a commnent -->\n</html>");
87     }
88     
89     public void testHTMLMulitilineComment() {
90         dumpTokensForContent("<html>\n<!-- this \nis \na \ncommnent \n-->\n</html>");
91     }
92     
93     public void testEmbededCSS() {
94          dumpTokensForContent("<style type=\"text/css\">\n"+
95             "#search {height:100%;}\n"+
96             "#topmodule {align:right;}\n"+
97             "</style>\n");
98     }
99     
100     public void testBug53102() {
101         dumpTokensForContent("<html>\n<head />\n<he");
102     }
103      
104     public void testBugWrongJsptagType() {
105         dumpTokensForContent("\n<a >\n");
106     }
107     
108     public void testPlainText() {
109         dumpTokensForContent("this is just a text");
110     }
111     
112     public void testEntityReference() {
113         dumpTokensForContent("<html>\n &amp; \n </html>");
114     }
115     
116     public void testNumericReference() {
117         dumpTokensForContent("<html>\n &#345; \n </html>");
118     }
119     
120     //helper methods -----------
121

122     private void dumpTokensForContent(String JavaDoc content) {
123         loadContentToSyntax(content);
124         dumpTokensData(getRef()); //print output to reference stream
125
}
126     
127     private void dumpTokensData(PrintStream JavaDoc out) {
128         TokenID tokenID = null;
129         char[] buffer = syntax.getBuffer();
130         String JavaDoc tokenImage = null;
131         TokenContextPath tcp = null;
132         do {
133             //acquire all token relevant data
134
tokenID = syntax.nextToken();
135             
136             if( tokenID == null ) break;
137             
138             tokenImage = new String JavaDoc(buffer, syntax.getTokenOffset(), syntax.getTokenLength());
139             tcp = syntax.getTokenContextPath();
140             
141             //print it
142
out.print("'" + SyntaxUtils.normalize(tokenImage) + "' ["+syntax.getTokenOffset() + ", " + syntax.getTokenLength() + "]; " + tokenID.getName() + "; " + tokenID.getNumericID() + "; "+ (tokenID.getCategory() != null ? tokenID.getCategory().getName() : "-") + "; ");
143             SyntaxUtils.dumpTokenContextPath(tcp, out);
144             out.println();
145             
146         }
147         while(true);
148     }
149     
150     private void loadContentToSyntax(String JavaDoc content) {
151         //load syntax - scan the whole buffer - the buffer is last one
152
char[] buffer = content.toCharArray();
153         syntax.load(null, buffer, 0, buffer.length, true, -1);
154     }
155     
156 }
157
Popular Tags