KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26
27
28 /**
29  * Positive test for <code>getResourcePaths()</code>, which will specify the
30  * directory path indicated by the <strong>path</strong> request parameter.
31  * For known paths, at least the known set of included resources must be
32  * found in order to pass.
33  *
34  * @author Craig R. McClanahan
35  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
36  */

37
38 public class Resources06 extends HttpServlet {
39
40     // Resource Lists (incomplete) for known directory paths
41
String JavaDoc rootPaths[] =
42     { "/ErrorPage06.html", "/ErrorPage06.jsp", "/Forward01.txt",
43       "/Include01.txt", "/WEB-INF/", "/Xerces00.jsp", "/Xerces01.xml",
44       "/Xerces02.jsp", "/golden/", "/includeme.txt", "/index.shtml",
45       "/ssidir/" };
46
47     String JavaDoc goldenPaths[] =
48     { "/golden/Golden01.txt", "/golden/SSIConfig01.txt",
49       "/golden/SSIConfig03.txt", "/golden/SSIFsize02.txt",
50       "/golden/SSIInclude01.txt", "/golden/SSIInclude02.txt",
51       "/golden/Session05.txt" };
52
53     String JavaDoc webinfPaths[] =
54     { "/WEB-INF/classes/", "/WEB-INF/lib/", "/WEB-INF/web.xml" };
55
56
57     public void doGet(HttpServletRequest request, HttpServletResponse response)
58         throws IOException, ServletException {
59
60         // Prepare our output writer
61
response.setContentType("text/plain");
62         PrintWriter writer = response.getWriter();
63
64         // Identify our configuration parameters
65
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
66         String JavaDoc path = request.getParameter("path");
67         if (path == null)
68             path = "/";
69         String JavaDoc paths[] = null;
70         if ("/".equals(path))
71             paths = rootPaths;
72         else if ("/golden".equals(path))
73             paths = goldenPaths;
74         else if ("/WEB-INF".equals(path))
75             paths = webinfPaths;
76         else {
77             sb.append(" Unknown path '");
78             sb.append(path);
79             sb.append("'/");
80         }
81         int counts[] = null;
82         if (paths != null)
83             counts = new int[paths.length];
84
85         // Request the set of resources in the specified path
86
StaticLogger.write("Processing path '" + path + "'");
87         String JavaDoc first = null;
88         Set set = getServletContext().getResourcePaths(path);
89         if (set == null) {
90             sb.append(" No resources returned/");
91             set = new HashSet();
92         }
93
94         // Count the occurrences of the resources we know about
95
Iterator resources = set.iterator();
96         while (resources.hasNext()) {
97             String JavaDoc resource = (String JavaDoc) resources.next();
98             if (first == null)
99                 first = resource;
100             StaticLogger.write("Found resource '" + resource + "'");
101             for (int i = 0; i < paths.length; i++) {
102                 if (paths[i].equals(resource)) {
103                     counts[i]++;
104                     break;
105                 }
106             }
107         }
108
109         // Report on any missing or duplicated resources
110
for (int i = 0; i < paths.length; i++) {
111             if (counts[i] < 1) {
112                 sb.append(" Missing resource '");
113                 sb.append(paths[i]);
114                 sb.append("'/");
115             } else if (counts[i] > 2) {
116                 sb.append(" Resource '");
117                 sb.append(paths[i]);
118                 sb.append("' occurred ");
119                 sb.append(counts[i]);
120                 sb.append(" times/");
121             }
122         }
123
124         // Verify that the returned set is immutable
125
try {
126             String JavaDoc newElement = "NEW FOO";
127             set.add(newElement);
128             if (set.contains(newElement))
129               sb.append(" Set allowed add()/");
130         } catch (Throwable JavaDoc t) {
131             ;
132         }
133         try {
134             if (first != null) {
135                 set.remove(first);
136                 if (!set.contains(first))
137                   sb.append(" Set allowed remove()/");
138             }
139         } catch (Throwable JavaDoc t) {
140             ;
141         }
142         try {
143             set.clear();
144             if (set.size() == 0)
145                 sb.append(" Set allowed clear()/");
146         } catch (Throwable JavaDoc t) {
147             ;
148         }
149
150
151         // Report any failures we have encountered
152
if (sb.length() > 0) {
153             writer.print("Resources06 FAILED -");
154             writer.println(sb.toString());
155         } else {
156             writer.println("Resources06 PASSED");
157         }
158
159         // Add wrapper messages as required
160
while (true) {
161             String JavaDoc message = StaticLogger.read();
162             if (message == null)
163                 break;
164             writer.println(message);
165         }
166         StaticLogger.reset();
167
168     }
169
170 }
171
Popular Tags