KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jspparser > ParseTest


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.modules.web.jspparser;
21
22 import java.io.File JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.util.Map JavaDoc;
27 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.api.project.ProjectUtils;
30 import org.netbeans.api.project.Sources;
31 import org.netbeans.junit.*;
32 import org.netbeans.junit.ide.ProjectSupport;
33 import org.netbeans.modules.project.ui.OpenProjectList;
34 import org.netbeans.modules.web.api.webmodule.WebModule;
35 import org.netbeans.modules.web.core.jsploader.JspParserAccess;
36 import org.netbeans.modules.web.jsps.parserapi.JspParserAPI;
37 import org.netbeans.modules.web.jsps.parserapi.JspParserFactory;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.FileUtil;
40
41 import org.netbeans.api.project.libraries.Library;
42 import org.netbeans.api.project.libraries.LibraryManager;
43
44 /** JUnit test suite with Jemmy support
45  *
46  * @author pj97932
47  * @version 1.0
48  */

49 public class ParseTest extends NbTestCase {
50     
51     /** constructor required by JUnit
52      * @param testName method name to be used as testcase
53      */

54     public ParseTest(String JavaDoc testName) {
55         super(testName);
56     }
57     
58     public void testAnalysisMain() throws Exception JavaDoc {
59         parserTestInProject("project2", Manager.getWorkDirPath() + "/project2/web/main.jsp");
60     }
61     
62     public void testAnalysisBean() throws Exception JavaDoc {
63         parserTestInProject("project2", Manager.getWorkDirPath() + "/project2/web/more_for_test/bean.jsp");
64     }
65     
66     public void testAnalysisTagLinkList() throws Exception JavaDoc {
67         parserTestInProject("project2", Manager.getWorkDirPath() + "/project2/web/WEB-INF/tags/linklist.tag");
68     }
69     
70     public void testAnalysisFaulty() throws Exception JavaDoc {
71         parserTestInProject("project2", Manager.getWorkDirPath() + "/project2/web/faulty.jsp");
72     }
73     
74     public void testAnalysisOutsideWM() throws Exception JavaDoc {
75         parserTestInProject("project2", Manager.getWorkDirPath() + "/project2/outside/outsidewm.jsp");
76     }
77         
78     public void testAnalysisFunction() throws Exception JavaDoc {
79         parserTestInProject("project3", Manager.getWorkDirPath() + "/project3/web/jsp2/el/functions.jsp");
80     }
81         
82     public void testAnalysisXMLTextRotate_1_6() throws Exception JavaDoc {
83         String JavaDoc javaVersion = System.getProperty("java.version"); //NOI18N
84

85         if (javaVersion.startsWith("1.6")){ //NOI18N
86
parserTestInProject("project3", Manager.getWorkDirPath() + "/project3/web/jsp2/jspx/textRotate.jspx"); //NOI18N
87
}
88     }
89     
90     public void testAnalysisXMLTextRotate_1_5() throws Exception JavaDoc {
91         String JavaDoc javaVersion = System.getProperty("java.version"); //NOI18N
92

93         if (javaVersion.startsWith("1.5")){ //NOI18N
94
parserTestInProject("project3", Manager.getWorkDirPath() + "/project3/web/jsp2/jspx/textRotate.jspx"); //NOI18N
95
}
96         
97     }
98     
99     public void testAnalysisXMLTextRotate_1_4() throws Exception JavaDoc {
100         String JavaDoc javaVersion = System.getProperty("java.version"); //NOI18N
101

102         if (javaVersion.startsWith("1.4")){ //NOI18N
103
parserTestInProject("project3", Manager.getWorkDirPath() + "/project3/web/jsp2/jspx/textRotate.jspx"); //NOI18N
104
}
105     }
106     
107     public void testAnalysisTagLibFromTagFiles() throws Exception JavaDoc {
108         String JavaDoc javaVersion = System.getProperty("java.version"); //NOI18N
109
if (!javaVersion.startsWith("1.6")){ //NOI18N
110
parserTestInProject("project2", Manager.getWorkDirPath() + "/project2/web/testTagLibs.jsp");
111         }
112     }
113
114     public void testAnalysisTagLibFromTagFiles_1_6() throws Exception JavaDoc {
115         String JavaDoc javaVersion = System.getProperty("java.version"); //NOI18N
116
if (javaVersion.startsWith("1.6")){ //NOI18N
117
parserTestInProject("project2", Manager.getWorkDirPath() + "/project2/web/testTagLibs.jsp");
118         }
119     }
120     
121     // test for issue #70426
122
public void testGetTagLibMap70426() throws Exception JavaDoc{
123         Object JavaDoc o = ProjectSupport.openProject(Manager.getWorkDirPath()+"/emptyWebProject");
124         Project project = (Project)o;
125         File JavaDoc f = new File JavaDoc(Manager.getWorkDirPath() + "/emptyWebProject/web/index.jsp");
126         FileObject jspFo = FileUtil.fromFile(f)[0];
127         JspParserAPI.WebModule wm = TestUtil.getWebModule(jspFo);
128         Map JavaDoc library = JspParserFactory.getJspParser().getTaglibMap(wm);
129         System.out.println("map->" + library);
130         Library jstlLibrary = LibraryManager.getDefault().getLibrary("jstl11");
131         ProjectClassPathExtender cpExtender = (ProjectClassPathExtender) project.getLookup().lookup(ProjectClassPathExtender.class);
132         cpExtender.addLibrary(jstlLibrary);
133         library = JspParserFactory.getJspParser().getTaglibMap(wm);
134         System.out.println("map->" + library);
135         assertTrue("The JSTL/core library was not returned.", (library.get("http://java.sun.com/jsp/jstl/core")) != null);
136     }
137     
138     public void parserTestInProject(String JavaDoc projectFolderName, String JavaDoc pagePath) throws Exception JavaDoc{
139         log("Parsing test of page " + pagePath + " in project " + projectFolderName + " started.");
140         String JavaDoc projectPath = Manager.getWorkDirPath() + "/" + projectFolderName;
141         Object JavaDoc o = ProjectSupport.openProject(projectPath);
142         if ( o != null)
143             log("Project " + projectPath + " opened.");
144         else
145             log("Project " + projectPath + " was not opened.");
146         Project project = (Project)o;
147         File JavaDoc f = new File JavaDoc(pagePath);
148         FileObject jspFo = FileUtil.fromFile(f)[0];
149         if (jspFo == null)
150             log (pagePath + " not found.");
151         log("Parsing page " + pagePath);
152
153         JspParserAPI.ParseResult result = JspParserFactory.getJspParser()
154                     .analyzePage(jspFo, TestUtil.getWebModule(jspFo), JspParserAPI.ERROR_IGNORE);
155         if (ProjectSupport.closeProject(ProjectUtils.getInformation(project).getName()))
156             log ("Project closed.");
157         assertFalse("The result from the parser was not obtained.", result == null);
158         File JavaDoc goldenF = null;
159         File JavaDoc outFile = null;
160         try {
161             goldenF = getGoldenFile();
162         }
163         finally {
164             String JavaDoc fName = (goldenF == null) ? ("temp" + fileNr++ + ".result") : getBrotherFile(goldenF, "result");
165             outFile = new File JavaDoc(getWorkDir(), fName);
166             writeOutResult(result, outFile);
167         }
168         
169         assertNotNull(outFile);
170         assertFile(outFile, goldenF, getWorkDir());
171     }
172     
173     private static int fileNr = 1;
174     
175     /*private void analyzeIt(FileObject root, FileObject jspFile) throws Exception {
176         log("calling parseIt, root: " + root + " file: " + jspFile);
177         JspParserAPI api = JspParserFactory.getJspParser();
178         JspParserAPI.ParseResult result = api.analyzePage(jspFile, TestUtil.getWebModule(root, jspFile), JspParserAPI.ERROR_IGNORE);
179         
180         File goldenF = null;
181         File outFile = null;
182         try {
183             //log(convertNBFSURL(getClass().getResource("/org/netbeans/modules/web/jspparser/data/goldenfiles/ParseTest/testAnalysisMain.pass")));
184             goldenF = getGoldenFile();
185             log("golden file exists 1: " + goldenF.exists());
186         }
187         finally {
188             String fName = (goldenF == null) ? ("temp" + fileNr++ + ".result") : getBrotherFile(goldenF, "result");
189             outFile = new File(getWorkDir(), fName);
190             writeOutResult(result, outFile);
191         }
192         log("golden file: " + goldenF);
193         log("golden file exists 2: " + goldenF.exists());
194         assertNotNull(outFile);
195         assertFile(outFile, goldenF, getWorkDir());
196     }*/

197     
198     
199     private void writeOutResult(JspParserAPI.ParseResult result, File JavaDoc outFile) throws IOException JavaDoc {
200         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(new FileWriter JavaDoc(outFile));
201         pw.write(result.toString());
202         pw.close();
203     }
204     
205     private String JavaDoc getBrotherFile(File JavaDoc f, String JavaDoc ext) {
206         String JavaDoc goldenFile = f.getName();
207         int i = goldenFile.lastIndexOf('.');
208         if (i == -1) {
209             i = goldenFile.length();
210         }
211         return goldenFile.substring(0, i) + "." + ext;
212     }
213     
214 }
215
Popular Tags