KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > web > core > syntax > JspMultiSyntaxTest


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.test.web.core.syntax;
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.editor.ext.java.JavaSyntax;
30 import org.netbeans.junit.NbTestCase;
31
32 import org.netbeans.modules.web.core.syntax.deprecated.Jsp11Syntax;
33 import org.openide.ErrorManager;
34
35 /** Basic jsp multisyntax parser tests.
36  *
37  * @author mf100882
38  */

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

59     public void testHtml() {
60         dumpTokensForContent("<html>\n<body>\n<h1>hello</h1>\n</body>\n</html>");
61     }
62     
63     public void testJavaScripting() {
64         dumpTokensForContent("<html><%! int a = 1; %>\n\n<br>\n<%=\"hello\"%>\n<br>\n<% String s = \"world\"; %>\n</html>");
65     }
66
67     public void testJspDeclaration() {
68         dumpTokensForContent("<%@page contentType=\"text/html\"%>\n"+
69                              "<%@page pageEncoding=\"UTF-8\"%>" +
70                              "<%@taglib uri=\"http://java.sun.com/jsp/jstl/core\" prefix=\"c\"%>");
71     }
72     
73     public void testExpressionLanguage() {
74         dumpTokensForContent("<html>${pageContext.request.contextPath}\n<br>" +
75                              "${pageContext.request.contextPath}\n " +
76                              "${header[\"host\"]} <br>\n" +
77                              "${requestScope['javax.servlet.forward.servlet_path']}\n</html>");
78     }
79     
80     public void testBug53102() {
81         dumpTokensForContent("<html>\n<head />\n<he");
82     }
83      
84     public void testBug52942() {
85         dumpTokensForContent("<a HREF=\"<%= 1 %>\" >Destination</a>");
86     }
87     
88     public void testBugWrongJsptagType() {
89         dumpTokensForContent("\n<a >\n");
90     }
91     
92     public void test50283_1() {
93         dumpTokensForContent("< /jsp:element >"); //should be marked as an error
94
}
95      
96     public void test50283_2() {
97         dumpTokensForContent("</ jsp:element >"); //should be marked as an error
98
}
99     
100     public void testJspComment() {
101         dumpTokensForContent("<html><%-- text \n new line --%></html>\n");
102     }
103     
104     public void testSimpleJspTag() {
105         dumpTokensForContent("</jsp:useBean id=\"sss\">");
106     }
107     
108     //helper methods -----------
109

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