1 16 17 package org.springframework.web.servlet.i18n; 18 19 import java.util.Locale ; 20 21 import javax.servlet.http.Cookie ; 22 23 import junit.framework.TestCase; 24 25 import org.springframework.mock.web.MockHttpServletRequest; 26 import org.springframework.mock.web.MockHttpServletResponse; 27 28 31 public class CookieLocaleResolverTests extends TestCase { 32 33 public void testResolveLocale() { 34 MockHttpServletRequest request = new MockHttpServletRequest(); 35 Cookie cookie = new Cookie ("LanguageKoekje", "nl"); 36 request.setCookies(new Cookie [] {cookie}); 37 38 CookieLocaleResolver resolver = new CookieLocaleResolver(); 39 resolver.setCookieName("LanguageKoekje"); 41 Locale 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 ("nl", "")); 51 52 Cookie 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 [] {cookie}); 61 62 resolver = new CookieLocaleResolver(); 63 Locale 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 ("nl", "")); 77 78 Cookie 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 [] {cookie}); 87 88 resolver = new CookieLocaleResolver(); 89 resolver.setCookieName("LanguageKoek"); 90 Locale loc = resolver.resolveLocale(request); 91 assertEquals(loc.getLanguage(), "nl"); 92 } 93 94 } 95 | Popular Tags |