001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.acting;
019:
020: import java.util.Map;
021:
022: import org.apache.avalon.framework.parameters.Parameters;
023: import org.apache.cocoon.SitemapComponentTestCase;
024: import org.apache.cocoon.environment.Cookie;
025: import org.apache.cocoon.environment.Session;
026: import org.apache.cocoon.environment.mock.MockCookie;
027: import org.apache.cocoon.environment.mock.MockSession;
028:
029: /**
030: * JUnit-based tests for {@link LocaleAction} class.
031: *
032: * @author Andrew Stevens
033: */
034: public class LocaleActionTestCase extends SitemapComponentTestCase {
035: /*
036: * Locales are looked for in following order:
037: * Locale provided as a request parameter
038: * Locale provided as a session attribute
039: * Locale provided as a cookie
040: * Locale provided using a sitemap parameter (<map:parameter name="locale" value="{1}">
041: * style parameter within the <map:act> node)
042: * Locale provided by the user agent, or server default, if use-locale == true
043: * The default locale, if specified in the action's configuration
044: * First found locale will be returned. The returned map will contain
045: * {locale}, {language}, {country} & {variant}.
046: */
047:
048: /**
049: * Test of act method, of class org.apache.cocoon.acting.LocaleAction.
050: */
051: public void testFindLocale() throws Exception {
052: // Test different locations for locale info in reverse order
053: Parameters parameters = new Parameters();
054: Map result;
055:
056: // 0. When no configuration, expect action to fail
057: result = act("locale0", null, parameters);
058: assertNull("Action should have failed", result);
059:
060: // 1. When nothing specified, use action's default constants
061: result = act("locale1", null, parameters);
062: assertNotNull("Action should always succeed", result);
063: assertEquals("Test for locale", "en_US", result.get("locale"));
064: assertEquals("Test for language", "en", result.get("language"));
065: assertEquals("Test for country", "US", result.get("country"));
066: assertEquals("Test for variant", "", result.get("variant"));
067:
068: // 2. Configuration
069: result = act("locale2", null, parameters);
070: assertNotNull("Action should always succeed", result);
071: assertEquals("Test for locale", "de_DE_EURO", result
072: .get("locale"));
073: assertEquals("Test for language", "de", result.get("language"));
074: assertEquals("Test for country", "DE", result.get("country"));
075: assertEquals("Test for variant", "EURO", result.get("variant"));
076:
077: // 3. User Agent or server default
078: getRequest().setLocale(new java.util.Locale("fr", "FR", "MAC")); // only if use-locale == true in configuration
079: // getRequest().setHeader("Accept-Language", "fr-FR,fr;q=0.75,en;q=0.5");
080: result = act("locale3", null, parameters);
081: assertNotNull("Action should always succeed", result);
082: assertEquals("Test for locale", "fr_FR_MAC", result
083: .get("locale"));
084: assertEquals("Test for language", "fr", result.get("language"));
085: assertEquals("Test for country", "FR", result.get("country"));
086: assertEquals("Test for variant", "MAC", result.get("variant"));
087:
088: // 4. Sitemap parameter
089: parameters.setParameter("locale", "zh_CN_WIN");
090: result = act("locale3", null, parameters);
091: assertNotNull("Action should always succeed", result);
092: assertEquals("Test for locale", "zh_CN_WIN", result
093: .get("locale"));
094: assertEquals("Test for language", "zh", result.get("language"));
095: assertEquals("Test for country", "CN", result.get("country"));
096: assertEquals("Test for variant", "WIN", result.get("variant"));
097:
098: // 5. Cookie
099: Map cookies = getRequest().getCookieMap();
100: MockCookie mockCookie = new MockCookie();
101: mockCookie.setName("locale");
102: mockCookie.setValue("no_NO_B");
103: cookies.put("locale", mockCookie);
104: result = act("locale3", null, parameters);
105: assertNotNull("Action should always succeed", result);
106: assertEquals("Test for locale", "no_NO_B", result.get("locale"));
107: assertEquals("Test for language", "no", result.get("language"));
108: assertEquals("Test for country", "NO", result.get("country"));
109: assertEquals("Test for variant", "B", result.get("variant"));
110:
111: // 6. Session attribute
112: MockSession session = (MockSession) getRequest().getSession();
113: session.setAttribute("locale", "th_TH_TH");
114: result = act("locale3", null, parameters);
115: assertNotNull("Action should always succeed", result);
116: assertEquals("Test for locale", "th_TH_TH", result
117: .get("locale"));
118: assertEquals("Test for language", "th", result.get("language"));
119: assertEquals("Test for country", "TH", result.get("country"));
120: assertEquals("Test for variant", "TH", result.get("variant"));
121:
122: // 7. Request parameter
123: getRequest().addParameter("locale", "es_MX_POSIX");
124: result = act("locale3", null, parameters);
125: assertNotNull("Action should always succeed", result);
126: assertEquals("Test for locale", "es_MX_POSIX", result
127: .get("locale"));
128: assertEquals("Test for language", "es", result.get("language"));
129: assertEquals("Test for country", "MX", result.get("country"));
130: assertEquals("Test for variant", "POSIX", result.get("variant"));
131: }
132:
133: /**
134: * Test of act method, of class org.apache.cocoon.acting.LocaleAction.
135: */
136: public void testStoreLocale() throws Exception {
137: // Test different locations for storing locale
138: Parameters parameters = new Parameters();
139: Map result;
140: Session session;
141: Cookie cookie;
142:
143: // 1. Don't store
144: result = act("locale2", null, parameters);
145: assertNotNull("Action should always succeed", result);
146: assertNull("Test for request attribute", getRequest()
147: .getAttribute("locale"));
148: assertNull("Test for session", getRequest().getSession(false));
149: assertTrue("Test for cookie", getResponse().getCookies()
150: .isEmpty());
151:
152: // 2. Store, but don't create session
153: result = act("locale4", null, parameters);
154: assertNotNull("Action should always succeed", result);
155: assertEquals("Test for request attribute", "no_NO_B",
156: getRequest().getAttribute("locale"));
157: assertNull("Test for session", getRequest().getSession(false));
158: assertEquals("Test for cookie", 1, getResponse().getCookies()
159: .size());
160: cookie = (Cookie) getResponse().getCookies().toArray()[0];
161: assertEquals("Check cookie name", "locale", cookie.getName());
162: assertEquals("Check cookie value", "no_NO_B", cookie.getValue());
163:
164: // 3. Store, creating session
165: getRequest().reset();
166: getRequest().clearSession();
167: getResponse().reset();
168: result = act("locale5", null, parameters);
169: assertNotNull("Action should always succeed", result);
170: assertEquals("Test for request attribute", "en_GB_SCOUSE",
171: getRequest().getAttribute("locale"));
172: session = getRequest().getSession(false);
173: assertNotNull("Test for session", session);
174: assertEquals("Test session attribute", "en_GB_SCOUSE", session
175: .getAttribute("locale"));
176: assertEquals("Test for cookie", 1, getResponse().getCookies()
177: .size());
178: cookie = (Cookie) getResponse().getCookies().toArray()[0];
179: assertEquals("Check cookie name", "locale", cookie.getName());
180: assertEquals("Check cookie value", "en_GB_SCOUSE", cookie
181: .getValue());
182:
183: // 4. Store, with existing session
184: getRequest().reset();
185: getRequest().clearSession();
186: getResponse().reset();
187: session = getRequest().getSession(true);
188: result = act("locale4", null, parameters);
189: assertNotNull("Action should always succeed", result);
190: assertEquals("Test for request attribute", "no_NO_B",
191: getRequest().getAttribute("locale"));
192: session = getRequest().getSession(false);
193: assertNotNull("Test for session", session);
194: assertEquals("Test session attribute", "no_NO_B", session
195: .getAttribute("locale"));
196: assertEquals("Test for cookie", 1, getResponse().getCookies()
197: .size());
198: cookie = (Cookie) getResponse().getCookies().toArray()[0];
199: assertEquals("Check cookie name", "locale", cookie.getName());
200: assertEquals("Check cookie value", "no_NO_B", cookie.getValue());
201: }
202:
203: }
|