001: // Copyright 2006 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.services;
016:
017: import java.util.Locale;
018:
019: import org.apache.tapestry.ioc.services.ThreadLocale;
020: import org.apache.tapestry.services.PersistentLocale;
021: import org.testng.Assert;
022: import org.testng.annotations.Test;
023:
024: public class LocalizationSetterImplTest extends Assert {
025: private PersistentLocale _nullPersistentLocale = new PersistentLocale() {
026: public boolean isSet() {
027: return false;
028: }
029:
030: public Locale get() {
031: return null;
032: }
033:
034: public void set(Locale locale) {
035:
036: }
037:
038: };
039:
040: private PersistentLocale _frenchPersistentLocale = new PersistentLocale() {
041: public boolean isSet() {
042: return true;
043: }
044:
045: public Locale get() {
046: return Locale.FRENCH;
047: }
048:
049: public void set(Locale locale) {
050:
051: }
052:
053: };
054:
055: private static class ThreadLocaleImpl implements ThreadLocale {
056: public Locale _locale;
057:
058: public void setLocale(Locale locale) {
059: _locale = locale;
060: }
061:
062: public Locale getLocale() {
063: return _locale;
064: }
065:
066: }
067:
068: @Test
069: public void locale_split() {
070: assertEquals(LocalizationSetterImpl.stripTerm("foo_bar_Baz"),
071: "foo_bar");
072: assertEquals(LocalizationSetterImpl.stripTerm("foo_bar"), "foo");
073: assertEquals(LocalizationSetterImpl.stripTerm("foo"), "");
074: }
075:
076: @Test
077: public void to_locale_is_cached() {
078: LocalizationSetterImpl filter = new LocalizationSetterImpl(
079: _nullPersistentLocale, null, "en");
080:
081: Locale l1 = filter.toLocale("en");
082:
083: assertEquals(l1.toString(), "en");
084:
085: checkLocale(l1, "en", "", "");
086:
087: assertSame(filter.toLocale("en"), l1);
088: }
089:
090: private void checkLocale(Locale l, String expectedLanguage,
091: String expectedCountry, String expectedVariant) {
092: assertEquals(l.getLanguage(), expectedLanguage);
093: assertEquals(l.getCountry(), expectedCountry);
094: assertEquals(l.getVariant(), expectedVariant);
095: }
096:
097: @Test
098: public void to_locale() {
099: LocalizationSetterImpl filter = new LocalizationSetterImpl(
100: _nullPersistentLocale, null, "en");
101:
102: checkLocale(filter.toLocale("en"), "en", "", "");
103: checkLocale(filter.toLocale("klingon_Gach"), "klingon", "GACH",
104: "");
105: checkLocale(filter.toLocale("klingon_Gach_snuff"), "klingon",
106: "GACH", "snuff");
107: }
108:
109: @Test
110: public void known_locale() {
111: ThreadLocale threadLocale = new ThreadLocaleImpl();
112: threadLocale.setLocale(Locale.FRENCH);
113: LocalizationSetter setter = new LocalizationSetterImpl(
114: _nullPersistentLocale, threadLocale, "en,fr");
115: setter.setThreadLocale(Locale.CANADA_FRENCH);
116: assertEquals(threadLocale.getLocale(), Locale.FRENCH);
117:
118: }
119:
120: @Test
121: public void unknown_locale_uses_default_locale() {
122: ThreadLocale threadLocale = new ThreadLocaleImpl();
123: threadLocale.setLocale(Locale.FRENCH);
124: LocalizationSetter setter = new LocalizationSetterImpl(
125: _nullPersistentLocale, threadLocale, "en,fr");
126: setter.setThreadLocale(Locale.JAPANESE);
127: assertEquals(threadLocale.getLocale(), Locale.ENGLISH);
128: }
129:
130: @Test
131: public void use_persistent_locale() {
132: ThreadLocale threadLocale = new ThreadLocaleImpl();
133: LocalizationSetter setter = new LocalizationSetterImpl(
134: _frenchPersistentLocale, threadLocale, "en,fr");
135: setter.setThreadLocale(Locale.ENGLISH);
136: assertEquals(threadLocale.getLocale(), Locale.FRENCH);
137: }
138:
139: }
|