001: /*
002: * $Id: TestPropertyMessageResources.java 480549 2006-11-29 12:16:15Z niallp $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.util;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025: import junit.framework.TestCase;
026:
027: import java.util.Locale;
028: import org.apache.struts.config.MessageResourcesConfig;
029:
030: /**
031: * Unit tests for PropertyMessageResources.
032: *
033: * @version $Revision: 480549 $
034: */
035: public class TestPropertyMessageResources extends TestCase {
036:
037: private static final String FOO_RESOURCES = "org.apache.struts.util.Foo";
038:
039: private Locale defaultLocale;
040:
041: // ----------------------------------------------------------------- Basics
042: public TestPropertyMessageResources(String name) {
043: super (name);
044: }
045:
046: public static void main(String[] args) {
047: junit.awtui.TestRunner
048: .main(new String[] { TestPropertyMessageResources.class
049: .getName() });
050: }
051:
052: public static Test suite() {
053: return (new TestSuite(TestPropertyMessageResources.class));
054: }
055:
056: // ----------------------------------------------------- Setup and Teardown
057: public void setUp() {
058: // cache the default locale
059: defaultLocale = Locale.getDefault();
060: }
061:
062: public void tearDown() {
063: // restore the default locale
064: Locale.setDefault(defaultLocale);
065: }
066:
067: // ------------------------------------------------------- Individual Tests
068:
069: /**
070: * Test Struts default PropertyMessageResources behaviour
071: */
072: public void testDefaultMode() {
073:
074: Locale.setDefault(Locale.US);
075:
076: // Create message resources - default Struts Behaviour
077: // MessageResources resources = createMessageResources(FOO_RESOURCES, true, "DEFAULT");
078: MessageResources resources = createMessageResources(
079: FOO_RESOURCES, true, null);
080:
081: // Test language (& default) only keys
082: assertEquals("key.lang FRANCE", "LANG default", resources
083: .getMessage(Locale.FRANCE, "key.lang")); // no cached en_US
084: assertEquals("key.lang English", "LANG en", resources
085: .getMessage(Locale.ENGLISH, "key.lang"));
086: assertEquals("key.lang US", "LANG en", resources.getMessage(
087: Locale.US, "key.lang"));
088: assertEquals("key.lang ITALY", "LANG en", resources.getMessage(
089: Locale.ITALY, "key.lang")); // cached en_US
090: assertEquals("key.lang German", "LANG de", resources
091: .getMessage(Locale.GERMAN, "key.lang"));
092: assertEquals("key.lang GERMANY", "LANG de", resources
093: .getMessage(Locale.GERMANY, "key.lang"));
094:
095: // Test country (& default) only keys
096: assertEquals("key.country FRANCE", "COUNTRY en_US", resources
097: .getMessage(Locale.FRANCE, "key.country"));
098: assertEquals("key.country English", "COUNTRY en_US", resources
099: .getMessage(Locale.ENGLISH, "key.country"));
100: assertEquals("key.country US", "COUNTRY en_US", resources
101: .getMessage(Locale.US, "key.country"));
102: assertEquals("key.country ITALY", "COUNTRY en_US", resources
103: .getMessage(Locale.ITALY, "key.country"));
104: assertEquals("key.country German", "COUNTRY en_US", resources
105: .getMessage(Locale.GERMAN, "key.country"));
106: assertEquals("key.country GERMANY", "COUNTRY de_DE", resources
107: .getMessage(Locale.GERMANY, "key.country"));
108:
109: // Test Unique Keys with wrong Locale
110: assertEquals("Wrong Locale en only", null, resources
111: .getMessage(Locale.GERMAN, "key.en"));
112: assertEquals("Wrong Locale en_US only", "en_US only", resources
113: .getMessage(Locale.GERMANY, "key.en_US"));
114:
115: // Run tests with common expected results
116: commonTests(resources);
117: }
118:
119: /**
120: * Test JSTL compatible PropertyMessageResources behaviour
121: */
122: public void testJstlMode() {
123:
124: Locale.setDefault(Locale.US);
125:
126: // Create message resources - default Struts Behaviour
127: MessageResources resources = createMessageResources(
128: FOO_RESOURCES, true, "JSTL");
129:
130: // Test language (& default) only keys
131: assertEquals("key.lang FRANCE", "LANG default", resources
132: .getMessage(Locale.FRANCE, "key.lang"));
133: assertEquals("key.lang English", "LANG en", resources
134: .getMessage(Locale.ENGLISH, "key.lang"));
135: assertEquals("key.lang US", "LANG en", resources.getMessage(
136: Locale.US, "key.lang"));
137: assertEquals("key.lang ITALY", "LANG default", resources
138: .getMessage(Locale.ITALY, "key.lang"));
139: assertEquals("key.lang German", "LANG de", resources
140: .getMessage(Locale.GERMAN, "key.lang"));
141: assertEquals("key.lang GERMANY", "LANG de", resources
142: .getMessage(Locale.GERMANY, "key.lang"));
143:
144: // Test country (& default) only keys
145: assertEquals("key.country FRANCE", "COUNTRY default", resources
146: .getMessage(Locale.FRANCE, "key.country"));
147: assertEquals("key.country English", "COUNTRY default",
148: resources.getMessage(Locale.ENGLISH, "key.country"));
149: assertEquals("key.country US", "COUNTRY en_US", resources
150: .getMessage(Locale.US, "key.country"));
151: assertEquals("key.country ITALY", "COUNTRY default", resources
152: .getMessage(Locale.ITALY, "key.country"));
153: assertEquals("key.country German", "COUNTRY default", resources
154: .getMessage(Locale.GERMAN, "key.country"));
155: assertEquals("key.country GERMANY", "COUNTRY de_DE", resources
156: .getMessage(Locale.GERMANY, "key.country"));
157:
158: // Test Unique Keys with wrong Locale
159: assertEquals("Wrong Locale en only", null, resources
160: .getMessage(Locale.GERMAN, "key.en"));
161: assertEquals("Wrong Locale en_US only", null, resources
162: .getMessage(Locale.GERMANY, "key.en_US"));
163:
164: // Run tests with common expected results
165: commonTests(resources);
166:
167: }
168:
169: /**
170: * Test "PropertyResourceBundle" compatible PropertyMessageResources behaviour
171: */
172: public void testResourceBundleMode() {
173:
174: Locale.setDefault(Locale.US);
175:
176: // Create message resources - default Struts Behaviour
177: MessageResources resources = createMessageResources(
178: FOO_RESOURCES, true, "RESOURCE");
179:
180: // Test language (& default) only keys
181: assertEquals("key.lang FRANCE", "LANG en", resources
182: .getMessage(Locale.FRANCE, "key.lang"));
183: assertEquals("key.lang English", "LANG en", resources
184: .getMessage(Locale.ENGLISH, "key.lang"));
185: assertEquals("key.lang US", "LANG en", resources.getMessage(
186: Locale.US, "key.lang"));
187: assertEquals("key.lang ITALY", "LANG en", resources.getMessage(
188: Locale.ITALY, "key.lang"));
189: assertEquals("key.lang German", "LANG de", resources
190: .getMessage(Locale.GERMAN, "key.lang"));
191: assertEquals("key.lang GERMANY", "LANG de", resources
192: .getMessage(Locale.GERMANY, "key.lang"));
193:
194: // Test country (& default) only keys
195: assertEquals("key.country FRANCE", "COUNTRY en_US", resources
196: .getMessage(Locale.FRANCE, "key.country"));
197: assertEquals("key.country English", "COUNTRY en_US", resources
198: .getMessage(Locale.ENGLISH, "key.country"));
199: assertEquals("key.country US", "COUNTRY en_US", resources
200: .getMessage(Locale.US, "key.country"));
201: assertEquals("key.country ITALY", "COUNTRY en_US", resources
202: .getMessage(Locale.ITALY, "key.country"));
203: assertEquals("key.country German", "COUNTRY en_US", resources
204: .getMessage(Locale.GERMAN, "key.country"));
205: assertEquals("key.country GERMANY", "COUNTRY de_DE", resources
206: .getMessage(Locale.GERMANY, "key.country"));
207:
208: // Test Unique Keys with wrong Locale
209: assertEquals("Wrong Locale en only", "en only", resources
210: .getMessage(Locale.GERMAN, "key.en"));
211: assertEquals("Wrong Locale en_US only", "en_US only", resources
212: .getMessage(Locale.GERMANY, "key.en_US"));
213:
214: // Run tests with common expected results
215: commonTests(resources);
216: }
217:
218: /**
219: * Tests with common expected results
220: */
221: public void commonTests(MessageResources resources) {
222:
223: // Test "null" Locale
224: assertEquals("null Locale", "ALL default", resources
225: .getMessage((Locale) null, "key.all"));
226:
227: // Test Default only key with all Locales
228: assertEquals("Check default en", "default only", resources
229: .getMessage(Locale.ENGLISH, "key.default"));
230: assertEquals("Check default en_US", "default only", resources
231: .getMessage(Locale.US, "key.default"));
232: assertEquals("Check default de", "default only", resources
233: .getMessage(Locale.GERMAN, "key.default"));
234: assertEquals("Check default de_DE", "default only", resources
235: .getMessage(Locale.GERMANY, "key.default"));
236:
237: // Test key in all locales
238: assertEquals("Check ALL en", "ALL en", resources.getMessage(
239: Locale.ENGLISH, "key.all"));
240: assertEquals("Check ALL en_US", "ALL en_US", resources
241: .getMessage(Locale.US, "key.all"));
242: assertEquals("Check ALL de", "ALL de", resources.getMessage(
243: Locale.GERMAN, "key.all"));
244: assertEquals("Check ALL de_DE", "ALL de_DE", resources
245: .getMessage(Locale.GERMANY, "key.all"));
246:
247: // Test key unique to each locale
248: assertEquals("Check en only", "en only", resources.getMessage(
249: Locale.ENGLISH, "key.en"));
250: assertEquals("Check en_US only", "en_US only", resources
251: .getMessage(Locale.US, "key.en_US"));
252: assertEquals("Check de only", "de only", resources.getMessage(
253: Locale.GERMAN, "key.de"));
254: assertEquals("Check de_DE only", "de_DE only", resources
255: .getMessage(Locale.GERMANY, "key.de_DE"));
256:
257: // Test unique keys with incorrect Locale
258: assertEquals("Missing default", null, resources.getMessage(
259: Locale.ENGLISH, "missing"));
260: assertEquals("Missing de only", null, resources.getMessage(
261: Locale.US, "key.de"));
262: assertEquals("Missing de_DE only", null, resources.getMessage(
263: Locale.US, "key.de_DE"));
264: }
265:
266: /**
267: * Create the PropertyMessageResources.
268: */
269: private MessageResources createMessageResources(String file,
270: boolean returnNull, String mode) {
271: MessageResourcesConfig config = new MessageResourcesConfig();
272: config.setNull(returnNull);
273: if (mode != null) {
274: config.setProperty("mode", mode);
275: }
276: PropertyMessageResourcesFactory factory = new PropertyMessageResourcesFactory();
277: factory.setConfig(config);
278: factory.setReturnNull(returnNull);
279: return factory.createResources(file);
280: }
281: }
|