001: /*
002: *******************************************************************************
003: * Copyright (C) 2003-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.test.util;
008:
009: import com.ibm.icu.dev.test.TestFmwk;
010: import com.ibm.icu.impl.ICUResourceBundle;
011: import com.ibm.icu.lang.UScript;
012: import com.ibm.icu.text.UnicodeSet;
013: import com.ibm.icu.text.UnicodeSetIterator;
014: import com.ibm.icu.util.LocaleData;
015: import com.ibm.icu.util.ULocale;
016:
017: /**
018: * @author ram
019: *
020: * To change the template for this generated type comment go to
021: * Window>Preferences>Java>Code Generation>Code and Comments
022: */
023: public class LocaleDataTest extends TestFmwk {
024:
025: public static void main(String[] args) throws Exception {
026: new LocaleDataTest().run(args);
027: }
028:
029: private ULocale[] availableLocales = null;
030:
031: public LocaleDataTest() {
032: }
033:
034: protected void init() {
035: availableLocales = ICUResourceBundle
036: .getAvailableULocales(ICUResourceBundle.ICU_BASE_NAME);
037: }
038:
039: public void TestPaperSize() {
040: for (int i = 0; i < availableLocales.length; i++) {
041: ULocale locale = availableLocales[i];
042: LocaleData.PaperSize paperSize = LocaleData
043: .getPaperSize(locale);
044: // skip testing of "in" .. deprecated code for Indonesian
045: String lang = locale.getLanguage();
046: if (lang.equals("in")) {
047: continue;
048: }
049: if (locale.toString().indexOf("_US") >= 0) {
050: if (paperSize.getHeight() != 279
051: || paperSize.getWidth() != 216) {
052: errln("PaperSize did not return the expected value for locale "
053: + locale
054: + " Expected height: 279 width: 216."
055: + " Got height: "
056: + paperSize.getHeight()
057: + " width: " + paperSize.getWidth());
058: } else {
059: logln("PaperSize returned the expected values for locale "
060: + locale);
061: }
062: } else {
063: if (paperSize.getHeight() != 297
064: || paperSize.getWidth() != 210) {
065: errln("PaperSize did not return the expected value for locale "
066: + locale
067: + " Expected height: 297 width: 210."
068: + " Got height: "
069: + paperSize.getHeight()
070: + " width: " + paperSize.getWidth());
071: } else {
072: logln("PaperSize returned the expected values for locale "
073: + locale);
074: }
075: }
076: }
077: }
078:
079: public void TestMeasurementSystem() {
080: for (int i = 0; i < availableLocales.length; i++) {
081: ULocale locale = availableLocales[i];
082: LocaleData.MeasurementSystem ms = LocaleData
083: .getMeasurementSystem(locale);
084: // skip testing of "in" .. deprecated code for Indonesian
085: String lang = locale.getLanguage();
086: if (lang.equals("in")) {
087: continue;
088: }
089: if (locale.toString().indexOf("_US") >= 0) {
090: if (ms == LocaleData.MeasurementSystem.US) {
091: logln("Got the expected measurement system for locale: "
092: + locale);
093: } else {
094: errln("Did not get the expected measurement system for locale: "
095: + locale);
096: }
097: } else {
098: if (ms == LocaleData.MeasurementSystem.SI) {
099: logln("Got the expected measurement system for locale: "
100: + locale);
101: } else {
102: errln("Did not get the expected measurement system for locale: "
103: + locale);
104: }
105: }
106: }
107: }
108:
109: public void TestExemplarSet() {
110: int equalCount = 0;
111: for (int i = 0; i < availableLocales.length; i++) {
112: ULocale locale = availableLocales[i];
113: UnicodeSet exemplarSets[] = new UnicodeSet[2];
114: for (int k = 0; k < 2; ++k) {
115: int option = (k == 0) ? 0 : UnicodeSet.CASE;
116: UnicodeSet exemplarSet = LocaleData.getExemplarSet(
117: locale, option);
118: exemplarSets[k] = exemplarSet;
119: int[] code = UScript.getCode(locale);
120: if (code != null) {
121: UnicodeSet[] sets = new UnicodeSet[code.length];
122: // create the UnicodeSets for the script
123: for (int j = 0; j < code.length; j++) {
124: sets[j] = new UnicodeSet("[:"
125: + UScript.getShortName(code[j]) + ":]");
126: }
127: boolean existsInScript = false;
128: UnicodeSetIterator iter = new UnicodeSetIterator(
129: exemplarSet);
130: // iterate over the
131: while (!existsInScript && iter.nextRange()) {
132: if (iter.codepoint != UnicodeSetIterator.IS_STRING) {
133: for (int j = 0; j < sets.length; j++) {
134: if (sets[j].contains(iter.codepoint,
135: iter.codepointEnd)) {
136: existsInScript = true;
137: break;
138: }
139: }
140: } else {
141: for (int j = 0; j < sets.length; j++) {
142: if (sets[j].contains(iter.string)) {
143: existsInScript = true;
144: break;
145: }
146: }
147: }
148: }
149: if (existsInScript == false) {
150: errln("ExemplarSet containment failed for locale : "
151: + locale);
152: }
153: } else {
154: // I hate the JDK's solution for deprecated language codes.
155: // Why does the Locale constructor change the string I passed to it ?
156: // such a broken hack !!!!!
157: // so in effect I can never test the script code for Indonesian :(
158: if (locale.toString().indexOf(("in")) < 0) {
159: errln("UScript.getCode returned null for locale: "
160: + locale);
161: }
162: }
163: }
164: // This is expensive, so only do it if it will be visible
165: if (isVerbose()) {
166: logln(locale.toString() + " exemplar "
167: + exemplarSets[0]);
168: logln(locale.toString() + " exemplar(case-folded) "
169: + exemplarSets[1]);
170: }
171: assertTrue(
172: locale.toString() + " case-folded is a superset",
173: exemplarSets[1].containsAll(exemplarSets[0]));
174: if (exemplarSets[1].equals(exemplarSets[0])) {
175: ++equalCount;
176: }
177: }
178: // Note: The case-folded set should sometimes be a strict superset
179: // and sometimes be equal.
180: assertTrue(
181: "case-folded is sometimes a strict superset, and sometimes equal",
182: equalCount > 0 && equalCount < availableLocales.length);
183: }
184:
185: public void TestExemplarSet2() {
186: int equalCount = 0;
187: for (int i = 0; i < availableLocales.length; i++) {
188: ULocale locale = availableLocales[i];
189: LocaleData ld = LocaleData.getInstance(locale);
190: UnicodeSet exemplarSets[] = new UnicodeSet[4];
191:
192: for (int k = 0; k < 2; ++k) {
193: int option = (k == 0) ? 0 : UnicodeSet.CASE;
194: for (int h = 0; h < 2; ++h) {
195: int type = (h == 0) ? LocaleData.ES_STANDARD
196: : LocaleData.ES_AUXILIARY;
197:
198: UnicodeSet exemplarSet = ld.getExemplarSet(option,
199: type);
200: exemplarSets[k * 2 + h] = exemplarSet;
201:
202: int[] code = UScript.getCode(locale);
203: if (code != null) {
204: UnicodeSet[] sets = new UnicodeSet[code.length];
205: // create the UnicodeSets for the script
206: for (int j = 0; j < code.length; j++) {
207: sets[j] = new UnicodeSet("[:"
208: + UScript.getShortName(code[j])
209: + ":]");
210: }
211: boolean existsInScript = false;
212: UnicodeSetIterator iter = new UnicodeSetIterator(
213: exemplarSet);
214: // iterate over the
215: while (!existsInScript && iter.nextRange()) {
216: if (iter.codepoint != UnicodeSetIterator.IS_STRING) {
217: for (int j = 0; j < sets.length; j++) {
218: if (sets[j].contains(
219: iter.codepoint,
220: iter.codepointEnd)) {
221: existsInScript = true;
222: break;
223: }
224: }
225: } else {
226: for (int j = 0; j < sets.length; j++) {
227: if (sets[j].contains(iter.string)) {
228: existsInScript = true;
229: break;
230: }
231: }
232: }
233: }
234: // TODO: How to verify LocaleData.ES_AUXILIARY ???
235: if (existsInScript == false && h == 0) {
236: errln("ExemplarSet containment failed for locale,option,type : "
237: + locale
238: + ", "
239: + option
240: + ", "
241: + type);
242: }
243: } else {
244: if (locale.toString().indexOf(("in")) < 0) {
245: errln("UScript.getCode returned null for locale,option,type : "
246: + locale
247: + ", "
248: + option
249: + ", "
250: + type);
251: }
252: }
253: }
254: }
255: // This is expensive, so only do it if it will be visible
256: if (isVerbose()) {
257: logln(locale.toString() + " exemplar(ES_STANDARD)"
258: + exemplarSets[0]);
259: logln(locale.toString() + " exemplar(ES_AUXILIARY) "
260: + exemplarSets[1]);
261: logln(locale.toString()
262: + " exemplar(case-folded,ES_STANDARD) "
263: + exemplarSets[2]);
264: logln(locale.toString()
265: + " exemplar(case-folded,ES_AUXILIARY) "
266: + exemplarSets[3]);
267: }
268: assertTrue(
269: locale.toString() + " case-folded is a superset",
270: exemplarSets[2].containsAll(exemplarSets[0]));
271: assertTrue(
272: locale.toString() + " case-folded is a superset",
273: exemplarSets[3].containsAll(exemplarSets[1]));
274: if (exemplarSets[2].equals(exemplarSets[0])) {
275: ++equalCount;
276: }
277: if (exemplarSets[3].equals(exemplarSets[1])) {
278: ++equalCount;
279: }
280: }
281: // Note: The case-folded set should sometimes be a strict superset
282: // and sometimes be equal.
283: assertTrue(
284: "case-folded is sometimes a strict superset, and sometimes equal",
285: equalCount > 0
286: && equalCount < availableLocales.length * 2);
287: }
288:
289: public void TestCoverage() {
290: LocaleData ld = LocaleData.getInstance();
291: boolean t = ld.getNoSubstitute();
292: ld.setNoSubstitute(t);
293: assertEquals("LocaleData get/set NoSubstitute", t, ld
294: .getNoSubstitute());
295:
296: logln(ld.getDelimiter(LocaleData.QUOTATION_START));
297: logln(ld.getDelimiter(LocaleData.QUOTATION_END));
298: logln(ld.getDelimiter(LocaleData.ALT_QUOTATION_START));
299: logln(ld.getDelimiter(LocaleData.ALT_QUOTATION_END));
300: }
301: }
|