KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26
27 /**
28  * Test aggregation of query string and POST parameters. According to
29  * Servlet 2.4 PFD, Section 4.1, all such parameters should be aggregated,
30  * and if there are duplicate parameter names from both sources, the
31  * parameter value(s) from the query string should appear first in the
32  * values returned by request.getParameterValues().
33  * <p>
34  * This test is the same as Aggregate01, except that it uses the new
35  * <code>getParameterMap()</code> method to retrieve parameter values.
36  *
37  * @author Craig R. McClanahan
38  * @version $Revision: 1.3 $ $Date: 2004/02/27 14:58:56 $
39  */

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