KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > i18n > LocaleResolverTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
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.springframework.web.servlet.i18n;
18
19 import java.util.Locale JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.springframework.mock.web.MockHttpServletRequest;
24 import org.springframework.mock.web.MockHttpServletResponse;
25 import org.springframework.mock.web.MockServletContext;
26 import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
27 import org.springframework.web.servlet.i18n.CookieLocaleResolver;
28 import org.springframework.web.servlet.i18n.SessionLocaleResolver;
29 import org.springframework.web.servlet.LocaleResolver;
30
31 /**
32  * @author Juergen Hoeller
33  * @since 20.03.2003
34  */

35 public class LocaleResolverTests extends TestCase {
36
37     private void internalTest(LocaleResolver localeResolver, boolean shouldSet) {
38         // create mocks
39
MockServletContext context = new MockServletContext();
40         MockHttpServletRequest request = new MockHttpServletRequest(context);
41         request.addPreferredLocale(Locale.UK);
42         MockHttpServletResponse response = new MockHttpServletResponse();
43         // check original locale
44
Locale JavaDoc locale = localeResolver.resolveLocale(request);
45         assertEquals(locale, Locale.UK);
46         // set new locale
47
try {
48             localeResolver.setLocale(request, response, Locale.GERMANY);
49             if (!shouldSet)
50                 fail("should not be able to set Locale");
51             // check new locale
52
locale = localeResolver.resolveLocale(request);
53             assertEquals(locale, Locale.GERMANY);
54         } catch (IllegalArgumentException JavaDoc ex) {
55             if (shouldSet)
56                 fail("should be able to set Locale");
57         }
58     }
59
60     public void testAcceptHeaderLocaleResolver() {
61         internalTest(new AcceptHeaderLocaleResolver(), false);
62     }
63
64     public void testCookieLocaleResolver() {
65         internalTest(new CookieLocaleResolver(), true);
66     }
67
68     public void testSessionLocaleResolver() {
69         internalTest(new SessionLocaleResolver(), true);
70     }
71
72 }
73
Popular Tags