1 16 17 package org.apache.tester; 18 19 20 import java.io.*; 21 import javax.servlet.*; 22 import javax.servlet.http.*; 23 24 35 36 public class Include02 extends HttpServlet { 37 38 public void doGet(HttpServletRequest request, HttpServletResponse response) 39 throws IOException, ServletException { 40 41 boolean ok = true; 42 response.setContentType("text/plain"); 43 PrintWriter writer = response.getWriter(); 44 RequestDispatcher rd = 45 getServletContext().getRequestDispatcher("/Include02a"); 46 if (rd == null) { 47 writer.println("Include02 FAILED - No RequestDispatcher returned"); 48 ok = false; 49 } 50 String type = request.getParameter("exception"); 51 if (ok) { 52 if (type == null) { 53 writer.println("Include02 FAILED - No exception type specified"); 54 ok = false; 55 } else if (!type.equals("IOException") && 56 !type.equals("ServletException") && 57 !type.equals("NullPointerException")) { 58 writer.println("Include02 FAILED - Invalid exception type " + 59 type + " requested"); 60 ok = false; 61 } 62 } 63 64 IOException ioException = null; 65 ServletException servletException = null; 66 Throwable throwable = null; 67 try { 68 if (ok) 69 rd.include(request, response); 70 } catch (IOException e) { 71 ioException = e; 72 } catch (ServletException e) { 73 servletException = e; 74 } catch (Throwable e) { 75 throwable = e; 76 } 77 78 if (ok) { 79 if (type.equals("IOException")) { 80 if (ioException == null) { 81 writer.println("Include02 FAILED - No IOException thrown"); 82 ok = false; 83 } else { 84 String message = ioException.getMessage(); 85 if (!"Include02 IOException".equals(message)) { 86 writer.println("Include02 FAILED - IOException was " + 87 message); 88 ok = false; 89 } 90 } 91 } else if (type.equals("ServletException")) { 92 if (servletException == null) { 93 writer.println("Include02 FAILED - No ServletException thrown"); 94 ok = false; 95 } else { 96 String message = servletException.getMessage(); 97 if (!"Include02 ServletException".equals(message)) { 98 writer.println("Include02 FAILED - ServletException was " + 99 message); 100 ok = false; 101 } 102 } 103 } else if (type.equals("NullPointerException")) { 104 if (throwable == null) { 105 writer.println("Include02 FAILED - No NullPointerException thrown"); 106 ok = false; 107 } else if (!(throwable instanceof NullPointerException )) { 108 writer.println("Include02 FAILED - Thrown Exception was " + 109 throwable.getClass().getName()); 110 ok = false; 111 } else { 112 String message = throwable.getMessage(); 113 if (!"Include02 NullPointerException".equals(message)) { 114 writer.println("Include02 FAILED - NullPointerException was " + 115 message); 116 ok = false; 117 } 118 } 119 } 120 } 121 122 if (ok) 123 writer.println("Include02 PASSED"); 124 StaticLogger.reset(); 125 126 } 127 128 } 129 | Popular Tags |