001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)TestStringTranslator.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.framework;
030:
031: import java.io.File;
032: import java.net.URL;
033: import java.net.URLClassLoader;
034: import java.util.Locale;
035:
036: /**
037: * Tests for the StringTranslator class.
038: *
039: * @author Sun Microsystems, Inc.
040: */
041: public class TestStringTranslator extends junit.framework.TestCase {
042: /**
043: * Value of the $SRCROOT environment variable
044: */
045: private String mSrcroot;
046:
047: /**
048: * Locale of the JVM
049: */
050: private Locale mLocale;
051:
052: /**
053: * Package name for the test resource bundles.
054: */
055: private static final String PACKAGE_NAME = "testI18n";
056:
057: /**
058: * Message key for message not found in any bundle
059: */
060: private static final String KEY_NOT_FOUND = "not_found";
061:
062: /**
063: * Message key for message in english bundle only
064: */
065: private static final String KEY_ENGLISH_ONLY = "english_only";
066:
067: /**
068: * Message key for message with no inserts
069: */
070: private static final String KEY_NO_INSERTS = "no_inserts";
071:
072: /**
073: * Message key for message with one insert
074: */
075: private static final String KEY_ONE_INSERT = "one_insert";
076:
077: /**
078: * Message key for message with two inserts
079: */
080: private static final String KEY_TWO_INSERTS = "two_inserts";
081:
082: /**
083: * Message key for message with three inserts
084: */
085: private static final String KEY_THREE_INSERTS = "three_inserts";
086:
087: /**
088: * Message key for message with four inserts
089: */
090: private static final String KEY_FOUR_INSERTS = "four_inserts";
091:
092: /**
093: * Message key for message with five inserts
094: */
095: private static final String KEY_FIVE_INSERTS = "five_inserts";
096:
097: /**
098: * Message text for message not found
099: */
100: private static final String MSG_NOT_FOUND = "No translation available for message with key=not_found "
101: + "and inserts=[x,y,z].";
102:
103: /**
104: * Message text for message not found
105: */
106: private static final String MSG_NO_TRANSLATION = "No translation available for message with key=english_only "
107: + "and inserts=[].";
108:
109: /**
110: * Message text for message in english only
111: */
112: private static final String MSG_ENGLISH_ONLY = "This message is found only in the English bundle.";
113:
114: /**
115: * Message text for message with no inserts
116: */
117: private static final String MSG_NO_INSERTS = "This message has no inserts.";
118:
119: /**
120: * Message text for message with no inserts in German
121: */
122: private static final String MSG_NO_INSERTS_DE = "Diese Nachricht haben kein Einlage.";
123:
124: /**
125: * Message text for message with one insert
126: */
127: private static final String MSG_ONE_INSERT = "This is a message with one insert here: insert1.";
128:
129: /**
130: * Message text for message with one insert in German
131: */
132:
133: private static final String MSG_ONE_INSERT_DE = "Diese ist ein Nachricht mit ein Einlage hier: insert1.";
134:
135: /**
136: * Message text for message with two inserts
137: */
138: private static final String MSG_TWO_INSERTS = "This message contains two inserts, one here: insert1 "
139: + "and one over there: insert2.";
140:
141: /**
142: * Message text for message with two inserts
143: */
144: private static final String MSG_TWO_INSERTS_BAD = "This message contains two inserts, one here: insert1 "
145: + "and one over there: {1}.";
146:
147: /**
148: * Message text for message with two inserts in German
149: */
150:
151: private static final String MSG_TWO_INSERTS_DE = "Diese Nachricht halten zwei Einlage, ein hier: insert1 "
152: + "und ein uber da: insert2.";
153:
154: /**
155: * Message text for message with two inserts in German
156: */
157:
158: private static final String MSG_TWO_INSERTS_DE_BAD = "Diese Nachricht halten zwei Einlage, ein hier: insert1 "
159: + "und ein uber da: {1}.";
160:
161: /**
162: * Message text for message with three inserts
163: */
164: private static final String MSG_THREE_INSERTS = "Here is a message with three inserts: insert1, insert2, and insert3.";
165:
166: /**
167: * Message text for message with three inserts in German
168: */
169: private static final String MSG_THREE_INSERTS_DE = "Hier ist ein Nachricht mit drei Einlage: insert1, insert2, und insert3.";
170:
171: /**
172: * Message text for message with four inserts
173: */
174: private static final String MSG_FOUR_INSERTS = "Some messages have four inserts, the first is insert1, the second is"
175: + " insert2, the third is insert3, and the fourth is insert4.";
176:
177: /**
178: * Message text for message with four inserts in German
179: */
180: private static final String MSG_FOUR_INSERTS_DE = "Etwas Nachricht haben vier Einlage, erste ist insert1, zweite "
181: + "ist insert2, dritte ist insert3, und vierte ist insert4.";
182:
183: /**
184: * Message text for message with five inserts
185: */
186: private static final String MSG_FIVE_INSERTS = "I have a message with five inserts displayed together: "
187: + "insert1+insert2+insert3+insert4+insert5.";
188:
189: /**
190: * Message text for message with five inserts in German
191: */
192: private static final String MSG_FIVE_INSERTS_DE = "Ich habe ein Nachricht mit funf Einlage entfalten zusammen: "
193: + "insert1+insert2+insert3+insert4+insert5.";
194:
195: /**
196: * The constructor for this testcase, forwards the test name to
197: * the jUnit TestCase base class.
198: * @param aTestName String with the name of this test.
199: */
200: public TestStringTranslator(String aTestName) {
201: super (aTestName);
202: }
203:
204: /**
205: * Setup for the test.
206: * @throws Exception when set up fails for any reason.
207: */
208: public void setUp() throws Exception {
209: super .setUp();
210: mSrcroot = System.getProperty("junit.srcroot") + "/";
211: mLocale = Locale.getDefault();
212: }
213:
214: /**
215: * Cleanup for the test.
216: * @throws Exception when tearDown fails for any reason.
217: */
218: public void tearDown() throws Exception {
219: Locale.setDefault(mLocale);
220: super .tearDown();
221: }
222:
223: // ============================= test methods ================================
224:
225: /**
226: * Test the constructor with a null class loader specified.
227: * @throws Exception if an unexpected error occurs.
228: */
229: public void testStringTranslatorNoClassLoader() throws Exception {
230: StringTranslator st;
231:
232: // Create an instance for a valid resource bundle. This should
233: // return a non-null value.
234:
235: st = new StringTranslator(PACKAGE_NAME, null);
236: assertNotNull("Failure on new StringTranslator(): "
237: + "expected a non-null value, got a null", st);
238:
239: // Create an instance for a non-existent resource bundle. This
240: // should return a non-null value.
241:
242: st = new StringTranslator(this .getClass().getPackage()
243: .getName(), null);
244: assertNotNull("Failure on new StringTranslator(): "
245: + "expected a non-null value, got a null", st);
246: }
247:
248: /**
249: * Test the constructor with a class loader specified.
250: * @throws Exception if an unexpected error occurs.
251: */
252: public void testStringTranslatorClassLoader() throws Exception {
253: // Create a class loader to pass to the constructor
254:
255: String path;
256: File dir;
257: URL[] urls;
258: URLClassLoader cl;
259: path = mSrcroot + "framework/regress/" + PACKAGE_NAME;
260: dir = new File(path);
261: urls = new URL[1];
262: urls[0] = dir.toURL();
263: cl = new URLClassLoader(urls);
264:
265: StringTranslator st;
266: String msg;
267:
268: // Create an instance for a valid resource bundle. This should
269: // return a non-null value.
270:
271: st = new StringTranslator(PACKAGE_NAME, cl);
272: assertNotNull("Failure on new StringTranslator(): "
273: + "expected a non-null value, got a null", st);
274:
275: // Verify that the instance is using the correct bundle.
276:
277: msg = st.getString(KEY_ENGLISH_ONLY);
278: assertTrue("Failure on getString(" + KEY_ENGLISH_ONLY + "): "
279: + "expected \"" + MSG_ENGLISH_ONLY + "\", got \"" + msg
280: + "\"", msg.equals(MSG_ENGLISH_ONLY));
281:
282: // Create an instance for a non-existent resource bundle. This
283: // should return a non-null value.
284:
285: st = new StringTranslator(this .getClass().getPackage()
286: .getName(), cl);
287: assertNotNull("Failure on new StringTranslator(): "
288: + "expected a non-null value, got a null", st);
289:
290: // Verify that the instance has no resource bundle.
291:
292: msg = st.getString(KEY_ENGLISH_ONLY);
293: assertTrue("Failure on getString(" + KEY_ENGLISH_ONLY + "): "
294: + "expected \"" + MSG_NO_TRANSLATION + "\", got \""
295: + msg + "\"", msg.equals(MSG_NO_TRANSLATION));
296:
297: }
298:
299: /**
300: * Test getString() with a message key that is not found in any resource
301: * bundle.
302: * @throws Exception if an unexpected error occurs.
303: */
304: public void testGetStringNotFound() throws Exception {
305: StringTranslator st;
306: String msg;
307:
308: // First test the English locale. There is no fallback resource
309: // bundle in this case.
310:
311: st = new StringTranslator(PACKAGE_NAME, null);
312:
313: msg = st.getString(KEY_NOT_FOUND, "x", "y", "z");
314: assertTrue("Failure on getString(" + KEY_NOT_FOUND + "): "
315: + "expected \"" + MSG_NOT_FOUND + "\", got \"" + msg
316: + "\"", msg.equals(MSG_NOT_FOUND));
317:
318: // Next test the German locale. There is a fallback resource bundle
319: // (English) in this case, but the message is not found there either.
320:
321: Locale.setDefault(Locale.GERMANY);
322: st = new StringTranslator(PACKAGE_NAME, null);
323:
324: msg = st.getString(KEY_NOT_FOUND, "x", "y", "z");
325: assertTrue("Failure on getString(" + KEY_NOT_FOUND + "): "
326: + "expected \"" + MSG_NOT_FOUND + "\", got \"" + msg
327: + "\"", msg.equals(MSG_NOT_FOUND));
328:
329: }
330:
331: /**
332: * Test getString() with a message key only.
333: * @throws Exception if an unexpected error occurs.
334: */
335: public void testGetStringNoInserts() throws Exception {
336: StringTranslator st;
337: String msg;
338:
339: // Test in English locale.
340:
341: st = new StringTranslator(PACKAGE_NAME, null);
342: msg = st.getString(KEY_NO_INSERTS);
343: assertTrue("Failure on getString(" + KEY_NO_INSERTS + "): "
344: + "expected \"" + MSG_NO_INSERTS + "\", got \"" + msg
345: + "\"", msg.equals(MSG_NO_INSERTS));
346:
347: // Test in German locale.
348:
349: Locale.setDefault(Locale.GERMANY);
350:
351: st = new StringTranslator(PACKAGE_NAME, null);
352: msg = st.getString(KEY_NO_INSERTS);
353: assertTrue("Failure on getString(" + KEY_NO_INSERTS + "): "
354: + "expected \"" + MSG_NO_INSERTS_DE + "\", got \""
355: + msg + "\"", msg.equals(MSG_NO_INSERTS_DE));
356:
357: }
358:
359: /**
360: * Test getString() with a single insert.
361: * @throws Exception if an unexpected error occurs.
362: */
363: public void testGetStringOneInsert() throws Exception {
364: StringTranslator st;
365: String msg;
366:
367: // Test in English locale.
368:
369: st = new StringTranslator(PACKAGE_NAME, null);
370: msg = st.getString(KEY_ONE_INSERT, "insert1");
371: assertTrue("Failure on getString(" + KEY_ONE_INSERT + "): "
372: + "expected \"" + MSG_ONE_INSERT + "\", got \"" + msg
373: + "\"", msg.equals(MSG_ONE_INSERT));
374:
375: // Test in German locale.
376:
377: Locale.setDefault(Locale.GERMANY);
378:
379: st = new StringTranslator(PACKAGE_NAME, null);
380: msg = st.getString(KEY_ONE_INSERT, "insert1");
381: assertTrue("Failure on getString(" + KEY_ONE_INSERT + "): "
382: + "expected \"" + MSG_ONE_INSERT_DE + "\", got \""
383: + msg + "\"", msg.equals(MSG_ONE_INSERT_DE));
384:
385: }
386:
387: /**
388: * Test getString() with two inserts.
389: * @throws Exception if an unexpected error occurs.
390: */
391: public void testGetStringTwoInserts() throws Exception {
392: StringTranslator st;
393: String msg;
394:
395: // Test in English locale.
396:
397: st = new StringTranslator(PACKAGE_NAME, null);
398: msg = st.getString(KEY_TWO_INSERTS, "insert1", "insert2");
399: assertTrue("Failure on getString(" + KEY_TWO_INSERTS + "): "
400: + "expected \"" + MSG_TWO_INSERTS + "\", got \"" + msg
401: + "\"", msg.equals(MSG_TWO_INSERTS));
402:
403: // Test in German locale.
404:
405: Locale.setDefault(Locale.GERMANY);
406:
407: st = new StringTranslator(PACKAGE_NAME, null);
408: msg = st.getString(KEY_TWO_INSERTS, "insert1", "insert2");
409: assertTrue("Failure on getString(" + KEY_TWO_INSERTS + "): "
410: + "expected \"" + MSG_TWO_INSERTS_DE + "\", got \""
411: + msg + "\"", msg.equals(MSG_TWO_INSERTS_DE));
412:
413: }
414:
415: /**
416: * Test getString() with two inserts.
417: * @throws Exception if an unexpected error occurs.
418: */
419: public void testGetStringTwoInsertsMissingOne() throws Exception {
420: StringTranslator st;
421: String msg;
422:
423: // Test in English locale.
424:
425: st = new StringTranslator(PACKAGE_NAME, null);
426: msg = st.getString(KEY_TWO_INSERTS, "insert1");
427: assertTrue("Failure on getString(" + KEY_TWO_INSERTS + "): "
428: + "expected \"" + MSG_TWO_INSERTS_BAD + "\", got \""
429: + msg + "\"", msg.equals(MSG_TWO_INSERTS_BAD));
430:
431: // Test in German locale.
432:
433: Locale.setDefault(Locale.GERMANY);
434:
435: st = new StringTranslator(PACKAGE_NAME, null);
436: msg = st.getString(KEY_TWO_INSERTS, "insert1");
437: assertTrue("Failure on getString(" + KEY_TWO_INSERTS + "): "
438: + "expected \"" + MSG_TWO_INSERTS_DE_BAD + "\", got \""
439: + msg + "\"", msg.equals(MSG_TWO_INSERTS_DE_BAD));
440:
441: }
442:
443: /**
444: * Test getString() with three inserts.
445: * @throws Exception if an unexpected error occurs.
446: */
447: public void testGetStringThreeInserts() throws Exception {
448: StringTranslator st;
449: String msg;
450:
451: // Test in English locale.
452:
453: st = new StringTranslator(PACKAGE_NAME, null);
454: msg = st.getString(KEY_THREE_INSERTS, "insert1", "insert2",
455: "insert3");
456: assertTrue("Failure on getString(" + KEY_THREE_INSERTS + "): "
457: + "expected \"" + MSG_THREE_INSERTS + "\", got \""
458: + msg + "\"", msg.equals(MSG_THREE_INSERTS));
459:
460: // Test in German locale.
461:
462: Locale.setDefault(Locale.GERMANY);
463:
464: st = new StringTranslator(PACKAGE_NAME, null);
465: msg = st.getString(KEY_THREE_INSERTS, "insert1", "insert2",
466: "insert3");
467: assertTrue("Failure on getString(" + KEY_THREE_INSERTS + "): "
468: + "expected \"" + MSG_THREE_INSERTS_DE + "\", got \""
469: + msg + "\"", msg.equals(MSG_THREE_INSERTS_DE));
470:
471: }
472:
473: /**
474: * Test getString() with four inserts.
475: * @throws Exception if an unexpected error occurs.
476: */
477: public void testGetStringFourInserts() throws Exception {
478: StringTranslator st;
479: String msg;
480:
481: // Test in English locale.
482:
483: st = new StringTranslator(PACKAGE_NAME, null);
484: msg = st.getString(KEY_FOUR_INSERTS, "insert1", "insert2",
485: "insert3", "insert4");
486: assertTrue("Failure on getString(" + KEY_FOUR_INSERTS + "): "
487: + "expected \"" + MSG_FOUR_INSERTS + "\", got \"" + msg
488: + "\"", msg.equals(MSG_FOUR_INSERTS));
489:
490: // Test in German locale.
491:
492: Locale.setDefault(Locale.GERMANY);
493:
494: st = new StringTranslator(PACKAGE_NAME, null);
495: msg = st.getString(KEY_FOUR_INSERTS, "insert1", "insert2",
496: "insert3", "insert4");
497: assertTrue("Failure on getString(" + KEY_FOUR_INSERTS + "): "
498: + "expected \"" + MSG_FOUR_INSERTS_DE + "\", got \""
499: + msg + "\"", msg.equals(MSG_FOUR_INSERTS_DE));
500:
501: }
502:
503: /**
504: * Test getString() with five inserts.
505: * @throws Exception if an unexpected error occurs.
506: */
507: public void testGetStringFiveInserts() throws Exception {
508: StringTranslator st;
509: String msg;
510: String inserts[] = { "insert1", "insert2", "insert3",
511: "insert4", "insert5" };
512:
513: // Test in English locale.
514:
515: st = new StringTranslator(PACKAGE_NAME, null);
516: msg = st.getString(KEY_FIVE_INSERTS, inserts);
517: assertTrue("Failure on getString(" + KEY_FIVE_INSERTS + "): "
518: + "expected \"" + MSG_FIVE_INSERTS + "\", got \"" + msg
519: + "\"", msg.equals(MSG_FIVE_INSERTS));
520:
521: // Test in German locale.
522:
523: Locale.setDefault(Locale.GERMANY);
524:
525: st = new StringTranslator(PACKAGE_NAME, null);
526: msg = st.getString(KEY_FIVE_INSERTS, inserts);
527: assertTrue("Failure on getString(" + KEY_FIVE_INSERTS + "): "
528: + "expected \"" + MSG_FIVE_INSERTS_DE + "\", got \""
529: + msg + "\"", msg.equals(MSG_FIVE_INSERTS_DE));
530:
531: }
532:
533: }
|