KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999, 2000, 2001 ,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  * Part 6 of the ErrorPage Tests. Should be mapped by the container when
26  * the ErrorPage05 servlet returns the appropriate exception.
27  *
28  * @author Craig R. McClanahan
29  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:56 $
30  */

31
32 public class ErrorPage06 extends HttpServlet {
33
34
35     public void doGet(HttpServletRequest request, HttpServletResponse response)
36         throws IOException, ServletException {
37
38         response.reset();
39         response.setContentType("text/plain");
40         PrintWriter writer = response.getWriter();
41
42         // Accumulate all the reasons this request might fail
43
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
44         Object JavaDoc value = null;
45
46         value = request.getAttribute("javax.servlet.error.exception");
47         StaticLogger.write("exception is '" + value + "'");
48         if (value == null) {
49             sb.append(" exception is missing/");
50         } else if (!(value instanceof java.lang.ArithmeticException JavaDoc)) {
51             sb.append(" exception class is ");
52             sb.append(value.getClass().getName());
53             sb.append("/");
54         }
55
56         value = request.getAttribute("javax.servlet.error.exception_type");
57         StaticLogger.write("exception_type is '" + value + "'");
58         if (value != null)
59             StaticLogger.write("exception_type class is '" +
60                                value.getClass().getName() + "'");
61         if (value == null)
62             sb.append(" exception_type is missing/");
63         else if (!(value instanceof Class JavaDoc)) {
64             sb.append(" exception_type class is ");
65             sb.append(value.getClass().getName());
66             sb.append("/");
67         } else {
68             Class JavaDoc clazz = (Class JavaDoc) value;
69             String JavaDoc name = clazz.getName();
70             if (!"java.lang.ArithmeticException".equals(name)) {
71                 sb.append(" exception_type is ");
72                 sb.append(name);
73                 sb.append("/");
74             }
75         }
76
77         value = request.getAttribute("javax.servlet.error.message");
78         StaticLogger.write("message is '" + value + "'");
79         if (value == null)
80             sb.append(" message is missing/");
81         else if (!(value instanceof String JavaDoc)) {
82             sb.append(" message class is ");
83             sb.append(value.getClass().getName());
84             sb.append("/");
85         } else if (!"ErrorPage05 Threw ArithmeticException".equals(value) &&
86                    !"ErrorPage08 Threw ArithmeticException".equals(value)) {
87             sb.append(" message is not correct");
88         }
89
90         value = request.getAttribute("javax.servlet.error.request_uri");
91         StaticLogger.write("request_uri is '" + value + "'");
92         if (value == null)
93             sb.append(" request_uri is missing/");
94         else if (!(value instanceof String JavaDoc)) {
95             sb.append(" request_uri class is ");
96             sb.append(value.getClass().getName());
97             sb.append("/");
98         } else {
99             String JavaDoc request_uri = (String JavaDoc) value;
100             String JavaDoc test1 = request.getContextPath() + "/ErrorPage05";
101             String JavaDoc test2 = request.getContextPath() + "/WrappedErrorPage05";
102             String JavaDoc test3 = request.getContextPath() + "/ErrorPage08";
103             String JavaDoc test4 = request.getContextPath() + "/WrappedErrorPage08";
104             if (!request_uri.equals(test1) && !request_uri.equals(test2) &&
105                 !request_uri.equals(test3) && !request_uri.equals(test4)) {
106                 sb.append(" request_uri is ");
107                 sb.append(request_uri);
108                 sb.append("/");
109             }
110         }
111
112         value = request.getAttribute("javax.servlet.error.servlet_name");
113         StaticLogger.write("servlet_name is '" + value + "'");
114         if (value == null)
115             sb.append(" servlet_name is missing/");
116         else if (!(value instanceof String JavaDoc)) {
117             sb.append(" servlet_name class is ");
118             sb.append(value.getClass().getName());
119             sb.append("/");
120         } else {
121             String JavaDoc servlet_name = (String JavaDoc) value;
122             if (!"ErrorPage05".equals(servlet_name) &&
123                 !"ErrorPage08".equals(servlet_name)) {
124                 sb.append(" servlet_name is ");
125                 sb.append(servlet_name);
126                 sb.append("/");
127             }
128         }
129
130         // Report ultimate success or failure
131
if (sb.length() < 1)
132             writer.println("ErrorPage06 PASSED - SERVLET");
133         else
134             writer.println("ErrorPage06 FAILED -" + sb.toString());
135
136         while (true) {
137             String JavaDoc message = StaticLogger.read();
138             if (message == null)
139                 break;
140             writer.println(message);
141         }
142         StaticLogger.reset();
143
144     }
145
146
147 }
148
Popular Tags