/* * TestTempDirServlet - Depend on setting of tempDir property in sun-web.xml * this web app checks the directory where compiled JSP * are generated. The generated Java files should match * to the expected list of files */ package servlet_tests; import java.io.*; import javax.servlet.http.*; import javax.servlet.*; import java.net.*; public class TestTempDirServlet extends HttpServlet { private PrintWriter out; private ServletContext context; private boolean debug = false; private final String COMPILED_JSP_DIR = "org/apache/jsp/jsp"; private final String[] EXPECTED_FILES = {"Test1_jsp.java", "RedirectedTo_jsp.java", "TestAllowLinking_jsp.java", "TestRelativeRedirectAllowed_jsp.java", "TestReuseSessionID_jsp.java"}; public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); out = response.getWriter(); out.println(""); out.println(""); if (request.getParameter("debug") != null) { debug = true; } String domainName = request.getParameter("domainName"); if (domainName == null) { domainName = "domain1"; } context = getServletContext(); File tempDir = (File) context.getAttribute("javax.servlet.context.tempdir"); File compiledJspDir = new File(tempDir, COMPILED_JSP_DIR); String [] listedJspFiles = compiledJspDir.list(); out.println("TestTempDirServlet
"); System.out.println("  javax.servlet.context.tempdir = " + tempDir + "
"); System.out.println("  compiledJspDir = " + compiledJspDir + "
"); System.out.println("  listedJspFiles.length = " + listedJspFiles.length + "
"); if (debug) { out.println("  javax.servlet.context.tempdir = " + tempDir + "
"); out.println("  compiledJspDir = " + compiledJspDir + "
"); out.println("  listedJspFiles.length = " + listedJspFiles.length + "
"); } // Print out the compiledJspDir to see if it matches to the expected // dir name in the golden file out.print("  The compiled JSP files are under directory -> "); int index = compiledJspDir.toString().indexOf(domainName) + domainName.length() + 1; out.println(compiledJspDir.toString().substring(index) + "
"); boolean testStatus = false; if ((listedJspFiles.length == 0) && (compiledJspDir.exists() == true) && (compiledJspDir.isDirectory() == true)) { testStatus = true; } // Verify that the generated Java files under compiledJspDir match to // one of the files in EXPECTED_FILES array for (int i = 0; i < listedJspFiles.length; i++) { if (listedJspFiles[i].endsWith("_jsp.java")) { testStatus = false; for (int j = 0; j < EXPECTED_FILES.length; j++) { if (listedJspFiles[i].equalsIgnoreCase(EXPECTED_FILES[j])) { testStatus = true; break; } } // Test fails if a converted-to Java file under compiledJspDir // does not match to any file in the EXPECTED_FILES array if (testStatus == false) { out.println("  Test FAILED - Unexpected file = " + listedJspFiles[i] + "
"); break; } } } if (testStatus == true) { out.println("  Test PASSED
"); } out.println("
"); } }