001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package javax.microedition.global;
028:
029: import com.sun.midp.i3test.*;
030: import java.util.*;
031:
032: /**
033: * I3 test for StringComparator.
034: */
035: public class TestResourceManager extends TestCase {
036:
037: private ResourceManager rm;
038: private String message;
039:
040: private String[] locs_match_en = { "fi-FI", "en", "cs" };
041: private String[] locs_match_empty = { "de-DE", "fi-FI", "", "zh" };
042: private String[] locs_with_null = { "fi-FI", "en", null, "cs" };
043: private String[] locs_with_illegal = { "de-DE", "illegal", "ru-RU",
044: "it" };
045: private String[] locs_unsupported = { "de-DE", "fi", "ru-RU", "it" };
046: private String[] illegal = { "Cs", "cs-", "cs/CZ", "cs-cz",
047: "cs-CZ-", "cs-CZ:utf8" };
048:
049: private String[] res_cs = { "Konec", "Dal\u0161\u00ed", "Zp\u011bt" };
050: private String[] res_en = { "Exit", "Next", "Back" };
051: private String[] res_ja = { "\u7d42\u4e86", "\u6b21\u3078 ",
052: "\u623b\u308b" };
053: private String[] res_sk = { "Koniec", "\u010ealej",
054: "Sp\u00e4\u0165" };
055: private String[] res_common = res_en;
056:
057: private final int RES_BASE = 0x65;
058: private final int FORMAT_SYMBOLS = 0x7ffffffd;
059:
060: public void testValidConstruction() {
061: /* Retrieve JSR 238 version information */
062: assertEquals("1.0", System
063: .getProperty("microedition.global.version"));
064:
065: /* Create device resource manager for default locale */
066: rm = null;
067: rm = ResourceManager.getManager(ResourceManager.DEVICE);
068: assertNotNull(rm);
069: assertEquals(ResourceManager.DEVICE, rm.getBaseName());
070: assertEquals(System.getProperty("microedition.locale"), rm
071: .getLocale());
072:
073: /* Create device resource manager for common resources */
074: rm = null;
075: rm = ResourceManager.getManager(ResourceManager.DEVICE, "");
076: assertNotNull(rm);
077: assertEquals(ResourceManager.DEVICE, rm.getBaseName());
078: assertEquals("", rm.getLocale());
079:
080: /* Create device resource manager for an existing locale */
081: rm = null;
082: rm = ResourceManager
083: .getManager(ResourceManager.DEVICE, "en-US");
084: assertNotNull(rm);
085: assertEquals(ResourceManager.DEVICE, rm.getBaseName());
086: assertEquals("en-US", rm.getLocale());
087:
088: /* Create device resource manager for an unsupported locale */
089: rm = null;
090: rm = ResourceManager
091: .getManager(ResourceManager.DEVICE, "fi-FI");
092: assertNotNull(rm);
093: assertEquals(ResourceManager.DEVICE, rm.getBaseName());
094: assertEquals("", rm.getLocale());
095:
096: /*
097: * Create device resource manager for an unsupported locale with
098: * variant
099: */
100: rm = null;
101: rm = ResourceManager.getManager(ResourceManager.DEVICE,
102: "de-DE-utf8");
103: assertNotNull(rm);
104: assertEquals(ResourceManager.DEVICE, rm.getBaseName());
105: assertEquals("", rm.getLocale());
106:
107: /* Create device resource manager for a locale with underscore */
108: rm = null;
109: rm = ResourceManager
110: .getManager(ResourceManager.DEVICE, "sk_SK");
111: assertNotNull(rm);
112: assertEquals(ResourceManager.DEVICE, rm.getBaseName());
113: assertEquals("sk-SK", rm.getLocale());
114:
115: /*
116: * Create device resource manager for a locale with variant and
117: * underscore
118: */
119: rm = null;
120: rm = ResourceManager.getManager(ResourceManager.DEVICE,
121: "he-IL_utf8");
122: assertNotNull(rm);
123: assertEquals(ResourceManager.DEVICE, rm.getBaseName());
124: assertEquals("he-IL", rm.getLocale());
125:
126: /*
127: * Create device resource manager for a locale with variant and
128: * underscores
129: */
130: rm = null;
131: rm = ResourceManager.getManager(ResourceManager.DEVICE,
132: "cs_CZ_utf8");
133: assertNotNull(rm);
134: assertEquals(ResourceManager.DEVICE, rm.getBaseName());
135: assertEquals("cs-CZ", rm.getLocale());
136:
137: /*
138: * Device resource manager for a list of locales,
139: * "en" is the first supported locale in the list
140: */
141: rm = null;
142: rm = ResourceManager.getManager(ResourceManager.DEVICE,
143: locs_match_en);
144: assertNotNull(rm);
145: assertEquals(ResourceManager.DEVICE, rm.getBaseName());
146: assertEquals("en", rm.getLocale());
147:
148: /*
149: * Device resource manager for a list of locales,
150: * "" (common) is the first supported locale in the list
151: */
152: rm = null;
153: rm = ResourceManager.getManager(ResourceManager.DEVICE,
154: locs_match_empty);
155: assertNotNull(rm);
156: assertEquals(ResourceManager.DEVICE, rm.getBaseName());
157: assertEquals("", rm.getLocale());
158: }
159:
160: public void testInvalidConstruction() {
161: /* Test resource manager creation with baseName == null */
162: try {
163: rm = ResourceManager.getManager(null);
164: fail("NullPointerException expected");
165: } catch (NullPointerException e) {
166: assertTrue(true);
167: }
168:
169: /* Test resource manager creation with locale == null */
170: try {
171: rm = ResourceManager.getManager(ResourceManager.DEVICE,
172: (String) null);
173: fail("NullPointerException expected");
174: } catch (NullPointerException e) {
175: assertTrue(true);
176: }
177:
178: /* Test resource manager creation with baseName == null */
179: try {
180: rm = ResourceManager.getManager(null, "en-US");
181: fail("NullPointerException expected");
182: } catch (NullPointerException e) {
183: assertTrue(true);
184: }
185:
186: /* Test resource manager creation with illegal (wrong format) locale */
187: for (int i = 0; i < illegal.length; i++) {
188: try {
189: rm = ResourceManager.getManager(ResourceManager.DEVICE,
190: illegal[i]);
191: fail("IllegalArgumentException expected");
192: } catch (IllegalArgumentException e) {
193: assertTrue(true);
194: }
195: }
196:
197: /* Test resource manager creation with locales == null */
198: try {
199: rm = ResourceManager.getManager(ResourceManager.DEVICE,
200: (String[]) null);
201: fail("NullPointerException expected");
202: } catch (NullPointerException e) {
203: assertTrue(true);
204: }
205:
206: /* Test resource manager creation with baseName == null */
207: try {
208: rm = ResourceManager.getManager(null, locs_match_en);
209: fail("NullPointerException expected");
210: } catch (NullPointerException e) {
211: assertTrue(true);
212: }
213:
214: /* Test resource manager creation with locales containing null */
215: try {
216: rm = ResourceManager.getManager(ResourceManager.DEVICE,
217: locs_with_null);
218: fail("NullPointerException expected");
219: } catch (NullPointerException e) {
220: assertTrue(true);
221: }
222:
223: /* Test resource manager creation with empty list of locales */
224: try {
225: rm = ResourceManager.getManager(ResourceManager.DEVICE,
226: new String[0]);
227: fail("IllegalArgumentException expected");
228: } catch (IllegalArgumentException e) {
229: assertTrue(true);
230: }
231:
232: /* Test resource manager creation with list containing illegal locale */
233: for (int i = 0; i < illegal.length; i++) {
234: try {
235: locs_with_illegal[1] = illegal[i];
236: rm = ResourceManager.getManager(ResourceManager.DEVICE,
237: locs_with_illegal);
238: fail("IllegalArgumentException expected");
239: } catch (IllegalArgumentException e) {
240: assertTrue(true);
241: }
242: }
243:
244: /*
245: * Test resource manager creation with a list containing only
246: * unsupported locales
247: */
248: try {
249: rm = ResourceManager.getManager(ResourceManager.DEVICE,
250: locs_unsupported);
251: fail("UnsupportedLocaleException expected");
252: } catch (UnsupportedLocaleException e) {
253: assertTrue(true);
254: }
255: }
256:
257: /* Helper method for array comparison (element-by-element) */
258: private boolean equalArrays(byte[] a, byte[] b) {
259: if (a == null || b == null || a.length != b.length) {
260: return false;
261: }
262: for (int i = 0; i < a.length; i++) {
263: if (a[i] != b[i]) {
264: return false;
265: }
266: }
267: return true;
268: }
269:
270: public void testValidDeviceResources() {
271: /*
272: * Retrieve device resources for "cs-CZ" locale ("cs" resources are
273: * hierarchically matched)
274: */
275: rm = null;
276: rm = ResourceManager
277: .getManager(ResourceManager.DEVICE, "cs-CZ");
278: assertNotNull(rm);
279: for (int i = 0; i < 3; i++) {
280: assertTrue(rm.isValidResourceID(RES_BASE + i));
281: assertEquals(res_cs[i], rm.getString(RES_BASE + i));
282: assertEquals(res_cs[i], rm.getResource(RES_BASE + i));
283: }
284: assertNotNull(rm.getResource(FORMAT_SYMBOLS));
285:
286: /* Retrieve device resources for "cs" locale */
287: rm = null;
288: rm = ResourceManager.getManager(ResourceManager.DEVICE, "cs");
289: assertNotNull(rm);
290: for (int i = 0; i < 3; i++) {
291: assertTrue(rm.isValidResourceID(RES_BASE + i));
292: assertEquals(res_cs[i], rm.getString(RES_BASE + i));
293: assertEquals(res_cs[i], rm.getResource(RES_BASE + i));
294: }
295: assertNotNull(rm.getResource(FORMAT_SYMBOLS));
296:
297: /*
298: * Retrieve device resources for "en-US" locale ("en" resources are
299: * hierarchically matched)
300: */
301: rm = null;
302: rm = ResourceManager
303: .getManager(ResourceManager.DEVICE, "en-US");
304: assertNotNull(rm);
305: for (int i = 0; i < 3; i++) {
306: assertTrue(rm.isValidResourceID(RES_BASE + i));
307: assertEquals(res_en[i], rm.getString(RES_BASE + i));
308: assertEquals(res_en[i], rm.getResource(RES_BASE + i));
309: }
310: assertNotNull(rm.getResource(FORMAT_SYMBOLS));
311:
312: /* Retrieve device resources for "en" locale */
313: rm = null;
314: rm = ResourceManager.getManager(ResourceManager.DEVICE, "en");
315: assertNotNull(rm);
316: for (int i = 0; i < 3; i++) {
317: assertTrue(rm.isValidResourceID(RES_BASE + i));
318: assertEquals(res_en[i], rm.getString(RES_BASE + i));
319: assertEquals(res_en[i], rm.getResource(RES_BASE + i));
320: }
321: assertNotNull(rm.getResource(FORMAT_SYMBOLS));
322:
323: /*
324: * Retrieve device resources for "he-IL" locale (common resources are
325: * hierarchically matched)
326: */
327: rm = null;
328: rm = ResourceManager
329: .getManager(ResourceManager.DEVICE, "he-IL");
330: assertNotNull(rm);
331: for (int i = 0; i < 3; i++) {
332: assertTrue(rm.isValidResourceID(RES_BASE + i));
333: assertEquals(res_common[i], rm.getString(RES_BASE + i));
334: assertEquals(res_common[i], rm.getResource(RES_BASE + i));
335: }
336: assertNotNull(rm.getResource(FORMAT_SYMBOLS));
337:
338: /*
339: * Retrieve device resources for "he" locale (common resources are
340: * hierarchically matched)
341: */
342: rm = null;
343: rm = ResourceManager.getManager(ResourceManager.DEVICE, "he");
344: assertNotNull(rm);
345: for (int i = 0; i < 3; i++) {
346: assertTrue(rm.isValidResourceID(RES_BASE + i));
347: assertEquals(res_common[i], rm.getString(RES_BASE + i));
348: assertEquals(res_common[i], rm.getResource(RES_BASE + i));
349: }
350: assertNotNull(rm.getResource(FORMAT_SYMBOLS));
351:
352: /*
353: * Retrieve device resources for "ja-JP" locale ("ja" resources are
354: * hierarchically matched)
355: */
356: rm = null;
357: rm = ResourceManager
358: .getManager(ResourceManager.DEVICE, "ja-JP");
359: assertNotNull(rm);
360: for (int i = 0; i < 3; i++) {
361: assertTrue(rm.isValidResourceID(RES_BASE + i));
362: assertEquals(res_ja[i], rm.getString(RES_BASE + i));
363: assertEquals(res_ja[i], rm.getResource(RES_BASE + i));
364: }
365: assertNotNull(rm.getResource(FORMAT_SYMBOLS));
366:
367: /* Retrieve device resources for "ja" locale */
368: rm = null;
369: rm = ResourceManager.getManager(ResourceManager.DEVICE, "ja");
370: assertNotNull(rm);
371: for (int i = 0; i < 3; i++) {
372: assertTrue(rm.isValidResourceID(RES_BASE + i));
373: assertEquals(res_ja[i], rm.getString(RES_BASE + i));
374: assertEquals(res_ja[i], rm.getResource(RES_BASE + i));
375: }
376: assertNotNull(rm.getResource(FORMAT_SYMBOLS));
377:
378: /*
379: * Retrieve device resources for "sk-SK" locale ("sk" resources are
380: * hierarchically matched)
381: */
382: rm = null;
383: rm = ResourceManager
384: .getManager(ResourceManager.DEVICE, "sk-SK");
385: assertNotNull(rm);
386: for (int i = 0; i < 3; i++) {
387: assertTrue(rm.isValidResourceID(RES_BASE + i));
388: assertEquals(res_sk[i], rm.getString(RES_BASE + i));
389: assertEquals(res_sk[i], rm.getResource(RES_BASE + i));
390: }
391: assertNotNull(rm.getResource(FORMAT_SYMBOLS));
392:
393: /* Retrieve device resources for "sk" locale */
394: rm = null;
395: rm = ResourceManager.getManager(ResourceManager.DEVICE, "sk");
396: assertNotNull(rm);
397: for (int i = 0; i < 3; i++) {
398: assertTrue(rm.isValidResourceID(RES_BASE + i));
399: assertEquals(res_sk[i], rm.getString(RES_BASE + i));
400: assertEquals(res_sk[i], rm.getResource(RES_BASE + i));
401: }
402: assertNotNull(rm.getResource(FORMAT_SYMBOLS));
403:
404: /*
405: * Retrieve device resources for "zh-CN" locale (common resources are
406: * hierarchically matched)
407: */
408: rm = null;
409: rm = ResourceManager
410: .getManager(ResourceManager.DEVICE, "zh-CN");
411: assertNotNull(rm);
412: for (int i = 0; i < 3; i++) {
413: assertTrue(rm.isValidResourceID(RES_BASE + i));
414: assertEquals(res_common[i], rm.getString(RES_BASE + i));
415: assertEquals(res_common[i], rm.getResource(RES_BASE + i));
416: }
417: assertNotNull(rm.getResource(FORMAT_SYMBOLS));
418:
419: /*
420: * Retrieve device resources for "zh" locale (common resources are
421: * hierarchically matched)
422: */
423: rm = null;
424: rm = ResourceManager.getManager(ResourceManager.DEVICE, "zh");
425: assertNotNull(rm);
426: for (int i = 0; i < 3; i++) {
427: assertTrue(rm.isValidResourceID(RES_BASE + i));
428: assertEquals(res_common[i], rm.getString(RES_BASE + i));
429: assertEquals(res_common[i], rm.getResource(RES_BASE + i));
430: }
431: assertNotNull(rm.getResource(FORMAT_SYMBOLS));
432:
433: /* Retrieve common device resources explicitly */
434: rm = null;
435: rm = ResourceManager.getManager(ResourceManager.DEVICE, "");
436: assertNotNull(rm);
437: for (int i = 0; i < 3; i++) {
438: assertTrue(rm.isValidResourceID(RES_BASE + i));
439: assertEquals(res_common[i], rm.getString(RES_BASE + i));
440: assertEquals(res_common[i], rm.getResource(RES_BASE + i));
441: }
442: }
443:
444: public void testInvalidDeviceResources() {
445: /* Test reading of resource with illegal identifier */
446: rm = null;
447: rm = ResourceManager
448: .getManager(ResourceManager.DEVICE, "en-US");
449: try {
450: rm.getResource(0x80000000);
451: fail("IllegalArgumentException expected");
452: } catch (IllegalArgumentException e) {
453: assertTrue(true);
454: }
455:
456: /* Test reading of resource with illegal identifier as string */
457: rm = null;
458: rm = ResourceManager
459: .getManager(ResourceManager.DEVICE, "en-US");
460: try {
461: rm.getString(-1);
462: fail("IllegalArgumentException expected");
463: } catch (IllegalArgumentException e) {
464: assertTrue(true);
465: }
466:
467: /* Test reading of resource with illegal identifier as binary data */
468: rm = null;
469: rm = ResourceManager
470: .getManager(ResourceManager.DEVICE, "en-US");
471: try {
472: rm.getData(-12345);
473: fail("IllegalArgumentException expected");
474: } catch (IllegalArgumentException e) {
475: assertTrue(true);
476: }
477:
478: /* Test reading of non-existent resource */
479: rm = null;
480: rm = ResourceManager.getManager(ResourceManager.DEVICE, "zh");
481: try {
482: rm.getResource(0x12345678);
483: fail("ResourceException expected");
484: } catch (ResourceException e) {
485: assertEquals(ResourceException.RESOURCE_NOT_FOUND, e
486: .getErrorCode());
487: }
488:
489: /* Test reading of non-existent resource as string */
490: rm = null;
491: rm = ResourceManager.getManager(ResourceManager.DEVICE, "zh");
492: try {
493: rm.getString(0);
494: fail("ResourceException expected");
495: } catch (ResourceException e) {
496: assertEquals(ResourceException.RESOURCE_NOT_FOUND, e
497: .getErrorCode());
498: }
499:
500: /* Test reading of non-existent resource as binary data */
501: rm = null;
502: rm = ResourceManager.getManager(ResourceManager.DEVICE, "zh");
503: try {
504: rm.getData(0xabcdef);
505: fail("ResourceException expected");
506: } catch (ResourceException e) {
507: assertEquals(ResourceException.RESOURCE_NOT_FOUND, e
508: .getErrorCode());
509: }
510:
511: /* Test reading of string resource as binary data */
512: rm = null;
513: rm = ResourceManager
514: .getManager(ResourceManager.DEVICE, "cs-CZ");
515: try {
516: rm.getData(RES_BASE);
517: fail("ResourceException expected");
518: } catch (ResourceException e) {
519: assertEquals(ResourceException.WRONG_RESOURCE_TYPE, e
520: .getErrorCode());
521: }
522:
523: /* Test reading of user-defined resource as binary data */
524: rm = null;
525: rm = ResourceManager
526: .getManager(ResourceManager.DEVICE, "sk-SK");
527: try {
528: rm.getData(FORMAT_SYMBOLS);
529: fail("ResourceException expected");
530: } catch (ResourceException e) {
531: assertEquals(ResourceException.WRONG_RESOURCE_TYPE, e
532: .getErrorCode());
533: }
534:
535: /* Test reading of user-defined resource as string */
536: rm = null;
537: rm = ResourceManager
538: .getManager(ResourceManager.DEVICE, "sk-SK");
539: try {
540: rm.getString(FORMAT_SYMBOLS);
541: fail("ResourceException expected");
542: } catch (ResourceException e) {
543: assertEquals(ResourceException.WRONG_RESOURCE_TYPE, e
544: .getErrorCode());
545: }
546: }
547:
548: /**
549: * fill suite with test methods
550: */
551: public void runTests() {
552: declare("testValidConstruction");
553: testValidConstruction();
554: declare("testInvalidConstruction");
555: testInvalidConstruction();
556: declare("testValidDeviceResources");
557: testValidDeviceResources();
558: declare("testInvalidDeviceResources");
559: testInvalidDeviceResources();
560: }
561: }
|