KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25
26
27 /**
28  * Negative test for <code>ServletContext.getResource()</code> as well as
29  * <code>ClassLoader.getResource()</code>. Operation is controlled by query
30  * parameters:
31  * <ul>
32  * <li><strong>mode</strong> - Use <code>context</code> for servlet context
33  * test, or <code>class</code> for class loader test. [context]</li>
34  * <li><strong>path</strong> - Resource path to the requested resource,
35  * starting with a slash. [/WEB-INF/web.xml]</li>
36  * </ul>
37  *
38  * @author Craig R. McClanahan
39  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
40  */

41
42 public class Resources02 extends HttpServlet {
43
44     public void doGet(HttpServletRequest request, HttpServletResponse response)
45         throws IOException, ServletException {
46
47         // Identify our configuration parameters
48
String JavaDoc mode = request.getParameter("mode");
49         if (mode == null)
50             mode = "context";
51         String JavaDoc path = request.getParameter("path");
52         if (path == null)
53             path = "/WEB-INF/web.xml";
54
55         // Execute the desired test
56
response.setContentType("text/plain");
57         PrintWriter writer = response.getWriter();
58         URL JavaDoc url = null;
59         try {
60             if ("context".equals(mode))
61                 url = getServletContext().getResource(path);
62             else
63                 url = this.getClass().getResource(path);
64             if (url == null)
65                 writer.println("Resources02 PASSED");
66             else
67                 writer.println("Resources02 FAILED - Returned URL " +
68                                url.toString());
69         } catch (MalformedURLException JavaDoc e) {
70             writer.println("Resources02 FAILED - MalformedURLException: "
71                            + e);
72         }
73
74         // Add wrapper messages as required
75
while (true) {
76             String JavaDoc message = StaticLogger.read();
77             if (message == null)
78                 break;
79             writer.println(message);
80         }
81         StaticLogger.reset();
82
83     }
84
85 }
86
Popular Tags