1 19 20 package org.netbeans.modules.web.jspparser; 21 22 import java.io.File ; 23 import java.io.FileWriter ; 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.util.Map ; 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 49 public class ParseTest extends NbTestCase { 50 51 54 public ParseTest(String testName) { 55 super(testName); 56 } 57 58 public void testAnalysisMain() throws Exception { 59 parserTestInProject("project2", Manager.getWorkDirPath() + "/project2/web/main.jsp"); 60 } 61 62 public void testAnalysisBean() throws Exception { 63 parserTestInProject("project2", Manager.getWorkDirPath() + "/project2/web/more_for_test/bean.jsp"); 64 } 65 66 public void testAnalysisTagLinkList() throws Exception { 67 parserTestInProject("project2", Manager.getWorkDirPath() + "/project2/web/WEB-INF/tags/linklist.tag"); 68 } 69 70 public void testAnalysisFaulty() throws Exception { 71 parserTestInProject("project2", Manager.getWorkDirPath() + "/project2/web/faulty.jsp"); 72 } 73 74 public void testAnalysisOutsideWM() throws Exception { 75 parserTestInProject("project2", Manager.getWorkDirPath() + "/project2/outside/outsidewm.jsp"); 76 } 77 78 public void testAnalysisFunction() throws Exception { 79 parserTestInProject("project3", Manager.getWorkDirPath() + "/project3/web/jsp2/el/functions.jsp"); 80 } 81 82 public void testAnalysisXMLTextRotate_1_6() throws Exception { 83 String javaVersion = System.getProperty("java.version"); 85 if (javaVersion.startsWith("1.6")){ parserTestInProject("project3", Manager.getWorkDirPath() + "/project3/web/jsp2/jspx/textRotate.jspx"); } 88 } 89 90 public void testAnalysisXMLTextRotate_1_5() throws Exception { 91 String javaVersion = System.getProperty("java.version"); 93 if (javaVersion.startsWith("1.5")){ parserTestInProject("project3", Manager.getWorkDirPath() + "/project3/web/jsp2/jspx/textRotate.jspx"); } 96 97 } 98 99 public void testAnalysisXMLTextRotate_1_4() throws Exception { 100 String javaVersion = System.getProperty("java.version"); 102 if (javaVersion.startsWith("1.4")){ parserTestInProject("project3", Manager.getWorkDirPath() + "/project3/web/jsp2/jspx/textRotate.jspx"); } 105 } 106 107 public void testAnalysisTagLibFromTagFiles() throws Exception { 108 String javaVersion = System.getProperty("java.version"); if (!javaVersion.startsWith("1.6")){ parserTestInProject("project2", Manager.getWorkDirPath() + "/project2/web/testTagLibs.jsp"); 111 } 112 } 113 114 public void testAnalysisTagLibFromTagFiles_1_6() throws Exception { 115 String javaVersion = System.getProperty("java.version"); if (javaVersion.startsWith("1.6")){ parserTestInProject("project2", Manager.getWorkDirPath() + "/project2/web/testTagLibs.jsp"); 118 } 119 } 120 121 public void testGetTagLibMap70426() throws Exception { 123 Object o = ProjectSupport.openProject(Manager.getWorkDirPath()+"/emptyWebProject"); 124 Project project = (Project)o; 125 File f = new File (Manager.getWorkDirPath() + "/emptyWebProject/web/index.jsp"); 126 FileObject jspFo = FileUtil.fromFile(f)[0]; 127 JspParserAPI.WebModule wm = TestUtil.getWebModule(jspFo); 128 Map 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 projectFolderName, String pagePath) throws Exception { 139 log("Parsing test of page " + pagePath + " in project " + projectFolderName + " started."); 140 String projectPath = Manager.getWorkDirPath() + "/" + projectFolderName; 141 Object 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 f = new File (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 goldenF = null; 159 File outFile = null; 160 try { 161 goldenF = getGoldenFile(); 162 } 163 finally { 164 String fName = (goldenF == null) ? ("temp" + fileNr++ + ".result") : getBrotherFile(goldenF, "result"); 165 outFile = new File (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 197 198 199 private void writeOutResult(JspParserAPI.ParseResult result, File outFile) throws IOException { 200 PrintWriter pw = new PrintWriter (new FileWriter (outFile)); 201 pw.write(result.toString()); 202 pw.close(); 203 } 204 205 private String getBrotherFile(File f, String ext) { 206 String 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 |