KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
22 import javax.servlet.*;
23 import javax.servlet.http.*;
24
25 /**
26  * Part 2 of the request locale tests. Should receive a Locale that
27  * corresponds to "en_CA" and then "en_GB" as sent by the client in
28  * "Accept-Language" headers.
29  *
30  * @author Craig R. McClanahan
31  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
32  */

33
34 public class GetLocales02 extends HttpServlet {
35
36
37     public void doGet(HttpServletRequest request, HttpServletResponse response)
38         throws IOException, ServletException {
39
40         response.reset();
41         response.setContentType("text/plain");
42         PrintWriter writer = response.getWriter();
43         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
44         boolean ok = true;
45
46         Enumeration locales = request.getLocales();
47         if (locales == null) {
48             sb.append(" No locales returned/");
49             ok = false;
50         }
51
52         if (ok) {
53             if (locales.hasMoreElements()) {
54                 Locale expected = new Locale("en", "CA");
55                 Locale received = (Locale) locales.nextElement();
56                 if (!expected.equals(received)) {
57                     sb.append(" Expected1='" +
58                            expected.toString() + "' Received1='" +
59                            received.toString() + "'");
60                 }
61             } else {
62                 sb.append(" Zero locales returned/");
63                 ok = false;
64             }
65         }
66
67         if (ok) {
68             if (locales.hasMoreElements()) {
69                 Locale expected = new Locale("en", "GB");
70                 Locale received = (Locale) locales.nextElement();
71                 if (!expected.equals(received)) {
72                     sb.append(" Expected2='" +
73                            expected.toString() + "' Received2='" +
74                            received.toString() + "'");
75                 }
76             } else {
77                 sb.append(" One locale returned/");
78                 ok = false;
79             }
80         }
81
82         if (ok) {
83             if (locales.hasMoreElements()) {
84                 sb.append(" More than two locales returned/");
85                 ok = false;
86             }
87         }
88
89         if (ok && (sb.length() < 1)) {
90             writer.println("GetLocales02 PASSED");
91         } else {
92             writer.print("GetLocales02 FAILED -");
93             writer.println(sb.toString());
94         }
95
96
97         while (true) {
98             String JavaDoc message = StaticLogger.read();
99             if (message == null)
100                 break;
101             writer.println(message);
102         }
103         StaticLogger.reset();
104
105     }
106
107
108 }
109
Popular Tags