KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > TestRequestLocaleManager


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.services.impl;
16
17 import java.util.Locale JavaDoc;
18
19 import org.apache.hivemind.service.ThreadLocale;
20 import org.apache.hivemind.test.HiveMindTestCase;
21 import org.apache.tapestry.TapestryConstants;
22 import org.apache.tapestry.services.CookieSource;
23 import org.apache.tapestry.web.WebRequest;
24 import org.easymock.MockControl;
25
26 /**
27  * Tests for {@link org.apache.tapestry.services.impl.RequestLocaleManagerImpl}.
28  *
29  * @author Howard Lewis Ship
30  * @since 4.0
31  */

32 public class TestRequestLocaleManager extends HiveMindTestCase
33 {
34     private ThreadLocale newThreadLocale()
35     {
36         return (ThreadLocale) newMock(ThreadLocale.class);
37     }
38
39     private ThreadLocale newThreadLocale(Locale JavaDoc locale)
40     {
41         MockControl control = newControl(ThreadLocale.class);
42         ThreadLocale threadLocale = (ThreadLocale) control.getMock();
43
44         threadLocale.getLocale();
45         control.setReturnValue(locale);
46
47         return threadLocale;
48     }
49
50     public void testSuppliedByRequest()
51     {
52         MockControl sourceControl = newControl(CookieSource.class);
53         CookieSource source = (CookieSource) sourceControl.getMock();
54
55         MockControl requestControl = newControl(WebRequest.class);
56         WebRequest request = (WebRequest) requestControl.getMock();
57
58         ThreadLocale tl = newThreadLocale();
59
60         // Training
61

62         source.readCookieValue(TapestryConstants.LOCALE_COOKIE_NAME);
63         sourceControl.setReturnValue(null);
64
65         request.getLocale();
66         requestControl.setReturnValue(Locale.JAPANESE);
67
68         tl.setLocale(Locale.JAPANESE);
69
70         replayControls();
71
72         RequestLocaleManagerImpl manager = new RequestLocaleManagerImpl();
73         manager.setCookieSource(source);
74         manager.setRequest(request);
75         manager.setThreadLocale(tl);
76
77         Locale JavaDoc actual = manager.extractLocaleForCurrentRequest();
78
79         assertEquals(Locale.JAPANESE, actual);
80
81         verifyControls();
82     }
83
84     private void attempt(String JavaDoc localeName, Locale JavaDoc expectedLocale)
85     {
86         MockControl sourceControl = newControl(CookieSource.class);
87         CookieSource source = (CookieSource) sourceControl.getMock();
88
89         ThreadLocale tl = newThreadLocale();
90
91         // Training
92

93         source.readCookieValue(TapestryConstants.LOCALE_COOKIE_NAME);
94         sourceControl.setReturnValue(localeName);
95
96         tl.setLocale(expectedLocale);
97
98         replayControls();
99
100         RequestLocaleManagerImpl manager = new RequestLocaleManagerImpl();
101         manager.setCookieSource(source);
102         manager.setThreadLocale(tl);
103
104         Locale JavaDoc actual = manager.extractLocaleForCurrentRequest();
105
106         assertEquals(expectedLocale, actual);
107
108         verifyControls();
109     }
110
111     public void testJustLanguage()
112     {
113         attempt("en", Locale.ENGLISH);
114     }
115
116     public void testLanguageAndCountry()
117     {
118         attempt("fr_FR", Locale.FRANCE);
119     }
120
121     public void testWithVariant()
122     {
123         attempt("en_US_Bahstohn", new Locale JavaDoc("en", "US", "Bahstohn"));
124     }
125
126     public void testPersist()
127     {
128         Locale JavaDoc locale = Locale.SIMPLIFIED_CHINESE;
129
130         CookieSource source = (CookieSource) newMock(CookieSource.class);
131         ThreadLocale threadLocale = newThreadLocale(locale);
132
133         // Training
134

135         source.writeCookieValue(TapestryConstants.LOCALE_COOKIE_NAME, locale.toString());
136
137         replayControls();
138
139         RequestLocaleManagerImpl m = new RequestLocaleManagerImpl();
140         m.setCookieSource(source);
141         m.setThreadLocale(threadLocale);
142
143         m.persistLocale();
144
145         verifyControls();
146     }
147
148     public void testPersistNoChange()
149     {
150         MockControl sourceControl = newControl(CookieSource.class);
151         CookieSource source = (CookieSource) sourceControl.getMock();
152
153         MockControl requestControl = newControl(WebRequest.class);
154         WebRequest request = (WebRequest) requestControl.getMock();
155
156         MockControl tlc = newControl(ThreadLocale.class);
157         ThreadLocale tl = (ThreadLocale) tlc.getMock();
158
159         // Training
160

161         source.readCookieValue(TapestryConstants.LOCALE_COOKIE_NAME);
162         sourceControl.setReturnValue(null);
163
164         request.getLocale();
165         requestControl.setReturnValue(Locale.JAPANESE);
166
167         tl.setLocale(Locale.JAPANESE);
168
169         replayControls();
170
171         RequestLocaleManagerImpl manager = new RequestLocaleManagerImpl();
172         manager.setCookieSource(source);
173         manager.setRequest(request);
174         manager.setThreadLocale(tl);
175
176         Locale JavaDoc actual = manager.extractLocaleForCurrentRequest();
177
178         assertEquals(Locale.JAPANESE, actual);
179
180         verifyControls();
181
182         tl.getLocale();
183         tlc.setReturnValue(Locale.JAPANESE);
184
185         replayControls();
186
187         // Should do nothing, beacuse it isn't a change.
188

189         manager.persistLocale();
190
191         verifyControls();
192     }
193
194     public void testGetLocaleValuesAreCached()
195     {
196         RequestLocaleManagerImpl manager = new RequestLocaleManagerImpl();
197
198         Locale JavaDoc l1 = manager.getLocale("en");
199         Locale JavaDoc l2 = manager.getLocale("en");
200
201         assertSame(l1, l2);
202     }
203
204     /**
205      * Test when filtering of incoming locales is disabled.
206      */

207
208     public void testFilterDisabled()
209     {
210         RequestLocaleManagerImpl manager = new RequestLocaleManagerImpl();
211
212         Locale JavaDoc l = manager.filterRequestedLocale("en");
213
214         assertEquals(Locale.ENGLISH, l);
215     }
216
217     /**
218      * Test with filtering enabled.
219      */

220
221     public void testFilterEnabled()
222     {
223         RequestLocaleManagerImpl manager = new RequestLocaleManagerImpl();
224         manager.setAcceptedLocales("en,fr");
225         manager.initializeService();
226
227         assertEquals(Locale.ENGLISH, manager.filterRequestedLocale("en"));
228         assertEquals(Locale.ENGLISH, manager.filterRequestedLocale("en_US"));
229         assertEquals(Locale.FRENCH, manager.filterRequestedLocale("fr"));
230         assertEquals(Locale.FRENCH, manager.filterRequestedLocale("fr_FR"));
231
232         // Unrecognized locales filter to the first accepted locale.
233

234         assertEquals(Locale.ENGLISH, manager.filterRequestedLocale("foo_bar_BAZ"));
235     }
236 }
Popular Tags