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: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.jspparser;
043:
044: import java.io.File;
045: import java.io.FileWriter;
046: import java.io.IOException;
047: import java.io.PrintWriter;
048:
049: import org.netbeans.modules.web.jsps.parserapi.JspParserAPI;
050: import org.netbeans.modules.web.jsps.parserapi.JspParserFactory;
051: import org.openide.filesystems.FileObject;
052:
053: import org.netbeans.junit.NbTestCase;
054: import org.netbeans.modules.web.api.webmodule.WebModule;
055:
056: /**
057: * @author pj97932
058: * @version 1.0
059: */
060: public class ParseTest extends NbTestCase {
061:
062: /** constructor required by JUnit
063: * @param testName method name to be used as testcase
064: */
065: public ParseTest(String testName) {
066: super (testName);
067: }
068:
069: @Override
070: protected void setUp() throws Exception {
071: super .setUp();
072: TestUtil.setup(this );
073: }
074:
075: public void testAnalysisBasicJspx() throws Exception {
076: parserTestInProject("project2", "/web/basic.jspx");
077: }
078:
079: public void testAnalysisMain() throws Exception {
080: parserTestInProject("project2", "/web/main.jsp");
081: }
082:
083: public void testAnalysisBean() throws Exception {
084: parserTestInProject("project2", "/web/more_for_test/bean.jsp");
085: }
086:
087: public void testAnalysisTagLinkList() throws Exception {
088: parserTestInProject("project2",
089: "/web/WEB-INF/tags/linklist.tag");
090: }
091:
092: public void testAnalysisFaulty() throws Exception {
093: parserTestInProject("project2", "/web/faulty.jsp");
094: }
095:
096: public void testAnalysisOutsideWM() throws Exception {
097: parserTestInProject("project2", "/outside/outsidewm.jsp");
098: }
099:
100: public void testAnalysisFunction() throws Exception {
101: parserTestInProject("project3", "/web/jsp2/el/functions.jsp");
102: }
103:
104: public void testAnalysisXMLTextRotate_1_6() throws Exception {
105: String javaVersion = System.getProperty("java.version");
106:
107: if (javaVersion.startsWith("1.6")) {
108: parserTestInProject("project3",
109: "/web/jsp2/jspx/textRotate.jspx");
110: }
111: }
112:
113: public void testAnalysisXMLTextRotate_1_5() throws Exception {
114: String javaVersion = System.getProperty("java.version");
115:
116: if (javaVersion.startsWith("1.5")) {
117: parserTestInProject("project3",
118: "/web/jsp2/jspx/textRotate.jspx");
119: }
120:
121: }
122:
123: public void testAnalysisXMLTextRotate_1_4() throws Exception {
124: String javaVersion = System.getProperty("java.version");
125:
126: if (javaVersion.startsWith("1.4")) {
127: parserTestInProject("project3",
128: "/web/jsp2/jspx/textRotate.jspx");
129: }
130: }
131:
132: public void testAnalysisTagLibFromTagFiles() throws Exception {
133: String javaVersion = System.getProperty("java.version");
134: if (!javaVersion.startsWith("1.6")) {
135: parserTestInProject("project2", "/web/testTagLibs.jsp");
136: }
137: }
138:
139: public void testAnalysisTagLibFromTagFiles_1_6() throws Exception {
140: String javaVersion = System.getProperty("java.version");
141: if (javaVersion.startsWith("1.6")) {
142: parserTestInProject("project2", "/web/testTagLibs.jsp");
143: }
144: }
145:
146: public void testJSPInclude() throws Exception {
147: parserTestInProject("project2", "/web/jspInclude.jsp");
148: }
149:
150: public void testInclude() throws Exception {
151: parserTestInProject("project2", "/web/include.jsp");
152:
153: }
154:
155: public void testIncludePreludeCoda() throws Exception {
156: JspParserAPI.ParseResult result = parserTestInProject(
157: "project2", "/web/includePreludeCoda.jsp");
158: log("Prelude: " + result.getPageInfo().getIncludePrelude());
159: log("Coda: " + result.getPageInfo().getIncludeCoda());
160: }
161:
162: public void testTagFileAttribute() throws Exception {
163: parserTestInProject("project3",
164: "/web/WEB-INF/tags/displayProducts.tag");
165: parserTestInProject("project3",
166: "/web/WEB-INF/tags/displayProducts.tag");
167: }
168:
169: public JspParserAPI.ParseResult parserTestInProject(
170: String projectFolderName, String pagePath) throws Exception {
171: FileObject jspFo = TestUtil.getProjectFile(this ,
172: projectFolderName, pagePath);
173: WebModule webModule = TestUtil.getWebModule(jspFo);
174: JspParserAPI jspParser = JspParserFactory.getJspParser();
175: JspParserAPI.ParseResult result = jspParser.analyzePage(jspFo,
176: webModule, JspParserAPI.ERROR_IGNORE);
177: assertNotNull("The result from the parser was not obtained.",
178: result);
179:
180: File goldenF = null;
181: File outFile = null;
182: try {
183: goldenF = getGoldenFile();
184: } finally {
185: String fName = (goldenF == null) ? ("temp" + fileNr++ + ".result")
186: : getBrotherFile(goldenF, "result");
187: outFile = new File(getWorkDir(), fName);
188: writeOutResult(result, outFile);
189: }
190:
191: assertNotNull(outFile);
192: try {
193: assertFile(outFile, goldenF, getWorkDir());
194: } catch (Error e) {
195: System.out.println("diff -u " + goldenF + " " + outFile);
196: fail(e.getMessage());
197: }
198:
199: return result;
200: }
201:
202: private static int fileNr = 1;
203:
204: private void writeOutResult(JspParserAPI.ParseResult result,
205: File outFile) throws IOException {
206: PrintWriter pw = new PrintWriter(new FileWriter(outFile));
207: pw.write(result.toString());
208: pw.close();
209: }
210:
211: private String getBrotherFile(File f, String ext) {
212: String goldenFile = f.getName();
213: int i = goldenFile.lastIndexOf('.');
214: if (i == -1) {
215: i = goldenFile.length();
216: }
217: return goldenFile.substring(0, i) + "." + ext;
218: }
219: }
|