KickJava   Java API By Example, From Geeks To Geeks.

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


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.getResourceAsStream()</code> as well
29  * as <code>ClassLoader.getResourceAsStream()</code>. Operation is controlled
30  * by query 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 Resources04 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         InputStream is = null;
59         URL JavaDoc url = null;
60         try {
61             if ("context".equals(mode)) {
62                 is = getServletContext().getResourceAsStream(path);
63                 url = getServletContext().getResource(path);
64             } else {
65                 is = this.getClass().getResourceAsStream(path);
66                 url = this.getClass().getResource(path);
67             }
68             if (is == null) {
69                 if (url == null)
70                     writer.println("Resources04 PASSED");
71                 else
72                     writer.println("Resources04 FAILED - Stream is null but URL is " + url);
73             } else {
74                 if (url != null)
75                     writer.println("Resources04 FAILED - Stream is not null and URL is " + url);
76                 else
77                     writer.println("Resources04 FAILED - Stream is not null and URL is null");
78             }
79         } catch (MalformedURLException JavaDoc e) {
80             writer.println("Resources04 FAILED - MalformedURLException: "
81                            + e);
82         }
83
84         // Add wrapper messages as required
85
while (true) {
86             String JavaDoc message = StaticLogger.read();
87             if (message == null)
88                 break;
89             writer.println(message);
90         }
91         StaticLogger.reset();
92
93     }
94
95 }
96
Popular Tags