KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tester > Include02


1 /*
2  * Copyright 1999, 2000 ,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.tester;
18
19
20 import java.io.*;
21 import javax.servlet.*;
22 import javax.servlet.http.*;
23
24 /**
25  * Positive test for handling exceptions thrown by an included servlet.
26  * Request parameter <strong>exception</strong> is used to indicate the type
27  * of exception that should be thrown, which must be one of
28  * <code>IOException</code>, <code>ServletException</code>, or
29  * <code>ServletException</code>. According to the spec, any exceptions of
30  * these types should be propogated back to the caller unchanged.
31  *
32  * @author Craig R. McClanahan
33  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
34  */

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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc)) {
108             writer.println("Include02 FAILED - Thrown Exception was " +
109                    throwable.getClass().getName());
110             ok = false;
111         } else {
112             String JavaDoc 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