KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Enumeration JavaDoc;
22 import javax.servlet.*;
23 import javax.servlet.http.*;
24
25 /**
26  * Test aggregation of query string and POST parameters. According to
27  * Servlet 2.4 PFD, Section 4.1, all such parameters should be aggregated,
28  * and if there are duplicate parameter names from both sources, the
29  * parameter value(s) from the query string should appear first in the
30  * values returned by request.getParameterValues().
31  *
32  * @author Craig R. McClanahan
33  * @version $Revision: 1.3 $ $Date: 2004/02/27 14:58:56 $
34  */

35
36 public class Aggregate01 extends HttpServlet {
37
38     public void doPost(HttpServletRequest request, HttpServletResponse response)
39         throws IOException, ServletException {
40
41         response.setContentType("text/plain");
42         PrintWriter writer = response.getWriter();
43
44         // Accumulate any errors that are noticed
45
StringBuffer JavaDoc errors = new StringBuffer JavaDoc();
46         String JavaDoc values[] = request.getParameterValues("a");
47         if (values == null)
48             errors.append(" Received no parameter values for 'a'.");
49         else if (values.length != 2)
50             errors.append(" Received " + values.length +
51                           " parameter values for 'a' instead of 2.");
52         else {
53             if (!"1".equals(values[0]))
54                 errors.append(" First value for 'a' was '" + values[0] +
55                               "' instead of '1'.");
56             if (!"2".equals(values[1]))
57                 errors.append(" Second value for 'a' was '" + values[1] +
58                               "' instead of '2'.");
59         }
60         values = request.getParameterValues("b");
61         if (values == null)
62             errors.append(" Received no parameter values for 'b'.");
63         else if (values.length != 1)
64             errors.append(" Received " + values.length +
65                           " parameter values for 'b' instead of 1.");
66         else {
67             if (!"3".equals(values[0]))
68                 errors.append(" Value for 'b' was '" + values[0] +
69                               "' instead of '3'.");
70         }
71         Enumeration JavaDoc names = request.getParameterNames();
72         while (names.hasMoreElements()) {
73             String JavaDoc name = (String JavaDoc) names.nextElement();
74             if ("a".equals(name))
75                 continue;
76             if ("b".equals(name))
77                 continue;
78             errors.append(" Received parameter '" + name + "'.");
79         }
80
81         // Report the results
82
if (errors.length() < 1)
83             writer.println("Aggregate01 PASSED");
84         else
85             writer.println("Aggregate01 FAILED -" + errors.toString());
86         while (true) {
87             String JavaDoc message = StaticLogger.read();
88             if (message == null)
89                 break;
90             writer.println(message);
91         }
92         StaticLogger.reset();
93
94     }
95
96 }
97
Popular Tags