KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.http.Cookie JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.springframework.mock.web.MockHttpServletRequest;
26 import org.springframework.mock.web.MockHttpServletResponse;
27
28 /**
29  * @author Alef Arendsen
30  */

31 public class CookieLocaleResolverTests extends TestCase {
32
33     public void testResolveLocale() {
34         MockHttpServletRequest request = new MockHttpServletRequest();
35         Cookie JavaDoc cookie = new Cookie JavaDoc("LanguageKoekje", "nl");
36         request.setCookies(new Cookie JavaDoc[] {cookie});
37
38         CookieLocaleResolver resolver = new CookieLocaleResolver();
39         // yup, koekje is the Dutch name for Cookie ;-)
40
resolver.setCookieName("LanguageKoekje");
41         Locale JavaDoc loc = resolver.resolveLocale(request);
42         assertEquals(loc.getLanguage(), "nl");
43     }
44
45     public void testSetAndResolveLocale() {
46         MockHttpServletRequest request = new MockHttpServletRequest();
47         MockHttpServletResponse response = new MockHttpServletResponse();
48
49         CookieLocaleResolver resolver = new CookieLocaleResolver();
50         resolver.setLocale(request, response, new Locale JavaDoc("nl", ""));
51
52         Cookie JavaDoc cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
53         assertNotNull(cookie);
54         assertEquals(CookieLocaleResolver.DEFAULT_COOKIE_NAME, cookie.getName());
55         assertEquals(null, cookie.getDomain());
56         assertEquals(CookieLocaleResolver.DEFAULT_COOKIE_PATH, cookie.getPath());
57         assertEquals(CookieLocaleResolver.DEFAULT_COOKIE_MAX_AGE, cookie.getMaxAge());
58
59         request = new MockHttpServletRequest();
60         request.setCookies(new Cookie JavaDoc[] {cookie});
61
62         resolver = new CookieLocaleResolver();
63         Locale JavaDoc loc = resolver.resolveLocale(request);
64         assertEquals(loc.getLanguage(), "nl");
65     }
66
67     public void testCustomCookie() {
68         MockHttpServletRequest request = new MockHttpServletRequest();
69         MockHttpServletResponse response = new MockHttpServletResponse();
70
71         CookieLocaleResolver resolver = new CookieLocaleResolver();
72         resolver.setCookieName("LanguageKoek");
73         resolver.setCookieDomain(".springframework.org");
74         resolver.setCookiePath("/mypath");
75         resolver.setCookieMaxAge(10000);
76         resolver.setLocale(request, response, new Locale JavaDoc("nl", ""));
77
78         Cookie JavaDoc cookie = response.getCookie("LanguageKoek");
79         assertNotNull(cookie);
80         assertEquals("LanguageKoek", cookie.getName());
81         assertEquals(".springframework.org", cookie.getDomain());
82         assertEquals("/mypath", cookie.getPath());
83         assertEquals(10000, cookie.getMaxAge());
84
85         request = new MockHttpServletRequest();
86         request.setCookies(new Cookie JavaDoc[] {cookie});
87
88         resolver = new CookieLocaleResolver();
89         resolver.setCookieName("LanguageKoek");
90         Locale JavaDoc loc = resolver.resolveLocale(request);
91         assertEquals(loc.getLanguage(), "nl");
92     }
93
94 }
95
Popular Tags