001: /**
002: *******************************************************************************
003: * Copyright (C) 2001-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */package com.ibm.icu.dev.test.translit;
007:
008: import com.ibm.icu.dev.test.*;
009: import com.ibm.icu.text.*;
010:
011: /**
012: * @test
013: * @summary Error condition test of Transliterator
014: */
015: public class ErrorTest extends TestFmwk {
016:
017: public static void main(String[] args) throws Exception {
018: new ErrorTest().run(args);
019: }
020:
021: public void TestTransliteratorErrors() {
022: String trans = "Latin-Greek";
023: String bogusID = "LATINGREEK-GREEKLATIN";
024: String newID = "Bogus-Latin";
025: String newIDRules = "zzz > Z; f <> ph";
026: String bogusRules = "a } [b-g m-p ";
027: ReplaceableString testString = new ReplaceableString(
028: "A quick fox jumped over the lazy dog.");
029: String insertString = "cats and dogs";
030: int stoppedAt = 0, len;
031: Transliterator.Position pos = new Transliterator.Position();
032:
033: Transliterator t = Transliterator.getInstance(trans,
034: Transliterator.FORWARD);
035: if (t == null) {
036: errln("FAIL: construction of Latin-Greek");
037: return;
038: }
039: len = testString.length();
040: stoppedAt = t.transliterate(testString, 0, 100);
041: if (stoppedAt != -1) {
042: errln("FAIL: Out of bounds check failed (1).");
043: } else if (testString.length() != len) {
044: testString = new ReplaceableString(
045: "A quick fox jumped over the lazy dog.");
046: errln("FAIL: Transliterate fails and the target string was modified.");
047: }
048: stoppedAt = t.transliterate(testString, 100, testString
049: .length() - 1);
050: if (stoppedAt != -1) {
051: errln("FAIL: Out of bounds check failed (2).");
052: } else if (testString.length() != len) {
053: testString = new ReplaceableString(
054: "A quick fox jumped over the lazy dog.");
055: errln("FAIL: Transliterate fails and the target string was modified.");
056: }
057: pos.start = 100;
058: pos.limit = testString.length();
059: try {
060: t.transliterate(testString, pos);
061: errln("FAIL: Start offset is out of bounds, error not reported.");
062: } catch (IllegalArgumentException e) {
063: logln("Start offset is out of bounds and detected.");
064: }
065: pos.limit = 100;
066: pos.start = 0;
067:
068: try {
069: t.transliterate(testString, pos);
070: errln("FAIL: Limit offset is out of bounds, error not reported.\n");
071: } catch (IllegalArgumentException e) {
072: logln("Start offset is out of bounds and detected.");
073: }
074: len = pos.contextLimit = testString.length();
075: pos.contextStart = 0;
076: pos.limit = len - 1;
077: pos.start = 5;
078: try {
079: t.transliterate(testString, pos, insertString);
080: if (len == pos.limit) {
081: errln("FAIL: Test insertion with string: the transliteration position limit didn't change as expected.");
082: }
083: } catch (IllegalArgumentException e) {
084: errln("Insertion test with string failed for some reason.");
085: }
086: pos.contextStart = 0;
087: pos.contextLimit = testString.length();
088: pos.limit = testString.length() - 1;
089: pos.start = 5;
090: try {
091: t.transliterate(testString, pos, 0x0061);
092: if (len == pos.limit) {
093: errln("FAIL: Test insertion with character: the transliteration position limit didn't change as expected.");
094: }
095: } catch (IllegalArgumentException e) {
096: errln("FAIL: Insertion test with UTF-16 code point failed for some reason.");
097: }
098: len = pos.limit = testString.length();
099: pos.contextStart = 0;
100: pos.contextLimit = testString.length() - 1;
101: pos.start = 5;
102: try {
103: t.transliterate(testString, pos, insertString);
104: errln("FAIL: Out of bounds check failed (3).");
105: if (testString.length() != len) {
106: errln("FAIL: The input string was modified though the offsets were out of bounds.");
107: }
108: } catch (IllegalArgumentException e) {
109: logln("Insertion test with out of bounds indexes.");
110: }
111: Transliterator t1 = null;
112: try {
113: t1 = Transliterator.getInstance(bogusID,
114: Transliterator.FORWARD);
115: if (t1 != null) {
116: errln("FAIL: construction of bogus ID \"LATINGREEK-GREEKLATIN\"");
117: }
118: } catch (IllegalArgumentException e) {
119: }
120:
121: //try { // unneeded - Exception cannot be thrown
122: Transliterator t2 = Transliterator.createFromRules(newID,
123: newIDRules, Transliterator.FORWARD);
124: try {
125: Transliterator t3 = t2.getInverse();
126: errln("FAIL: The newID transliterator was not registered so createInverse should fail.");
127: if (t3 != null) {
128: errln("FAIL: The newID transliterator was not registered so createInverse should fail.");
129: }
130: } catch (Exception e) {
131: }
132: //} catch (Exception e) { }
133: try {
134: Transliterator t4 = Transliterator.createFromRules(newID,
135: bogusRules, Transliterator.FORWARD);
136: if (t4 != null) {
137: errln("FAIL: The rules is malformed but error was not reported.");
138: }
139: } catch (Exception e) {
140: }
141: }
142:
143: public void TestUnicodeSetErrors() {
144: String badPattern = "[[:L:]-[0x0300-0x0400]";
145: UnicodeSet set = new UnicodeSet();
146: //String result;
147:
148: if (!set.isEmpty()) {
149: errln("FAIL: The default ctor of UnicodeSet created a non-empty object.");
150: }
151: try {
152: set.applyPattern(badPattern);
153: errln("FAIL: Applied a bad pattern to the UnicodeSet object okay.");
154: } catch (IllegalArgumentException e) {
155: logln("Test applying with the bad pattern.");
156: }
157: try {
158: UnicodeSet set1 = new UnicodeSet(badPattern);
159: errln("FAIL: Created a UnicodeSet based on bad patterns.");
160: if (set1 != null) {
161: errln("FAIL: Created a UnicodeSet based on bad patterns.");
162: }
163: } catch (IllegalArgumentException e) {
164: logln("Test constructing with the bad pattern.");
165: }
166: }
167:
168: // public void TestUniToHexErrors() {
169: // Transliterator t = null;
170: // try {
171: // t = new UnicodeToHexTransliterator("", true, null);
172: // if (t != null) {
173: // errln("FAIL: Created a UnicodeToHexTransliterator with an empty pattern.");
174: // }
175: // } catch (IllegalArgumentException e) {
176: // }
177: // try {
178: // t = new UnicodeToHexTransliterator("\\x", true, null);
179: // if (t != null) {
180: // errln("FAIL: Created a UnicodeToHexTransliterator with a bad pattern.");
181: // }
182: // } catch (IllegalArgumentException e) {
183: // }
184: // t = new UnicodeToHexTransliterator();
185: // try {
186: // ((UnicodeToHexTransliterator) t).applyPattern("\\x");
187: // errln("FAIL: UnicodeToHexTransliterator::applyPattern succeeded with a bad pattern.");
188: // } catch (Exception e) {
189: // }
190: // }
191:
192: public void TestRBTErrors() {
193:
194: String rules = "ab>y";
195: String id = "MyRandom-YReverse";
196: String goodPattern = "[[:L:]&[\\u0000-\\uFFFF]]"; /* all BMP letters */
197: UnicodeSet set = null;
198: try {
199: set = new UnicodeSet(goodPattern);
200: try {
201: Transliterator t = Transliterator.createFromRules(id,
202: rules, Transliterator.REVERSE);
203: t.setFilter(set);
204: Transliterator.registerClass(id, t.getClass(), null);
205: Transliterator.unregister(id);
206: try {
207: Transliterator t1 = Transliterator.getInstance(id,
208: Transliterator.REVERSE);
209: errln("FAIL: construction of unregistered ID should have failed.");
210: } catch (IllegalArgumentException e) {
211: }
212: } catch (IllegalArgumentException e) {
213: errln("FAIL: Was not able to create a good RBT to test registration.");
214: }
215: } catch (IllegalArgumentException e) {
216: errln("FAIL: Was not able to create a good UnicodeSet based on valid patterns.");
217: return;
218: }
219: }
220:
221: // public void TestHexToUniErrors() {
222: // Transliterator t = null;
223: // //try { // unneeded - exception cannot be thrown
224: // t = new HexToUnicodeTransliterator("", null);
225: // //} catch (Exception e) {
226: // // errln("FAIL: Could not create a HexToUnicodeTransliterator with an empty pattern.");
227: // //}
228: // try {
229: // t = new HexToUnicodeTransliterator("\\x", null);
230: // errln("FAIL: Created a HexToUnicodeTransliterator with a bad pattern.");
231: // } catch (IllegalArgumentException e) {
232: // }
233: //
234: // t = new HexToUnicodeTransliterator();
235: // try {
236: // ((HexToUnicodeTransliterator) t).applyPattern("\\x");
237: // errln("FAIL: HexToUnicodeTransliterator::applyPattern succeeded with a bad pattern.");
238: // } catch (IllegalArgumentException e) {
239: // }
240: // }
241: }
|