2 *******************************************************************************
\r
3 * Copyright (C) 2001-2007, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.translit;
\r
8 import com.ibm.icu.dev.test.*;
\r
9 import com.ibm.icu.text.*;
\r
13 * @summary Error condition test of Transliterator
\r
15 public class ErrorTest extends TestFmwk {
\r
17 public static void main(String[] args) throws Exception {
\r
18 new ErrorTest().run(args);
\r
21 public void TestTransliteratorErrors() {
\r
22 String trans = "Latin-Greek";
\r
23 String bogusID = "LATINGREEK-GREEKLATIN";
\r
24 String newID = "Bogus-Latin";
\r
25 String newIDRules = "zzz > Z; f <> ph";
\r
26 String bogusRules = "a } [b-g m-p ";
\r
27 ReplaceableString testString =
\r
28 new ReplaceableString("A quick fox jumped over the lazy dog.");
\r
29 String insertString = "cats and dogs";
\r
30 int stoppedAt = 0, len;
\r
31 Transliterator.Position pos = new Transliterator.Position();
\r
34 Transliterator.getInstance(trans, Transliterator.FORWARD);
\r
36 errln("FAIL: construction of Latin-Greek");
\r
39 len = testString.length();
\r
40 stoppedAt = t.transliterate(testString, 0, 100);
\r
41 if (stoppedAt != -1) {
\r
42 errln("FAIL: Out of bounds check failed (1).");
\r
43 } else if (testString.length() != len) {
\r
45 new ReplaceableString("A quick fox jumped over the lazy dog.");
\r
46 errln("FAIL: Transliterate fails and the target string was modified.");
\r
48 stoppedAt = t.transliterate(testString, 100, testString.length() - 1);
\r
49 if (stoppedAt != -1) {
\r
50 errln("FAIL: Out of bounds check failed (2).");
\r
51 } else if (testString.length() != len) {
\r
53 new ReplaceableString("A quick fox jumped over the lazy dog.");
\r
54 errln("FAIL: Transliterate fails and the target string was modified.");
\r
57 pos.limit = testString.length();
\r
59 t.transliterate(testString, pos);
\r
60 errln("FAIL: Start offset is out of bounds, error not reported.");
\r
61 } catch (IllegalArgumentException e) {
\r
62 logln("Start offset is out of bounds and detected.");
\r
68 t.transliterate(testString, pos);
\r
69 errln("FAIL: Limit offset is out of bounds, error not reported.\n");
\r
70 } catch (IllegalArgumentException e) {
\r
71 logln("Start offset is out of bounds and detected.");
\r
73 len = pos.contextLimit = testString.length();
\r
74 pos.contextStart = 0;
\r
75 pos.limit = len - 1;
\r
78 t.transliterate(testString, pos, insertString);
\r
79 if (len == pos.limit) {
\r
80 errln("FAIL: Test insertion with string: the transliteration position limit didn't change as expected.");
\r
82 } catch (IllegalArgumentException e) {
\r
83 errln("Insertion test with string failed for some reason.");
\r
85 pos.contextStart = 0;
\r
86 pos.contextLimit = testString.length();
\r
87 pos.limit = testString.length() - 1;
\r
90 t.transliterate(testString, pos, 0x0061);
\r
91 if (len == pos.limit) {
\r
92 errln("FAIL: Test insertion with character: the transliteration position limit didn't change as expected.");
\r
94 } catch (IllegalArgumentException e) {
\r
95 errln("FAIL: Insertion test with UTF-16 code point failed for some reason.");
\r
97 len = pos.limit = testString.length();
\r
98 pos.contextStart = 0;
\r
99 pos.contextLimit = testString.length() - 1;
\r
102 t.transliterate(testString, pos, insertString);
\r
103 errln("FAIL: Out of bounds check failed (3).");
\r
104 if (testString.length() != len) {
\r
105 errln("FAIL: The input string was modified though the offsets were out of bounds.");
\r
107 } catch (IllegalArgumentException e) {
\r
108 logln("Insertion test with out of bounds indexes.");
\r
110 Transliterator t1 = null;
\r
112 t1 = Transliterator.getInstance(bogusID, Transliterator.FORWARD);
\r
114 errln("FAIL: construction of bogus ID \"LATINGREEK-GREEKLATIN\"");
\r
116 } catch (IllegalArgumentException e) {
\r
119 //try { // unneeded - Exception cannot be thrown
\r
120 Transliterator t2 =
\r
121 Transliterator.createFromRules(
\r
124 Transliterator.FORWARD);
\r
126 Transliterator t3 = t2.getInverse();
\r
127 errln("FAIL: The newID transliterator was not registered so createInverse should fail.");
\r
129 errln("FAIL: The newID transliterator was not registered so createInverse should fail.");
\r
131 } catch (Exception e) {
\r
133 //} catch (Exception e) { }
\r
135 Transliterator t4 =
\r
136 Transliterator.createFromRules(
\r
139 Transliterator.FORWARD);
\r
141 errln("FAIL: The rules is malformed but error was not reported.");
\r
143 } catch (Exception e) {
\r
147 public void TestUnicodeSetErrors() {
\r
148 String badPattern = "[[:L:]-[0x0300-0x0400]";
\r
149 UnicodeSet set = new UnicodeSet();
\r
152 if (!set.isEmpty()) {
\r
153 errln("FAIL: The default ctor of UnicodeSet created a non-empty object.");
\r
156 set.applyPattern(badPattern);
\r
157 errln("FAIL: Applied a bad pattern to the UnicodeSet object okay.");
\r
158 } catch (IllegalArgumentException e) {
\r
159 logln("Test applying with the bad pattern.");
\r
162 new UnicodeSet(badPattern);
\r
163 errln("FAIL: Created a UnicodeSet based on bad patterns.");
\r
164 } catch (IllegalArgumentException e) {
\r
165 logln("Test constructing with the bad pattern.");
\r
169 // public void TestUniToHexErrors() {
\r
170 // Transliterator t = null;
\r
172 // t = new UnicodeToHexTransliterator("", true, null);
\r
173 // if (t != null) {
\r
174 // errln("FAIL: Created a UnicodeToHexTransliterator with an empty pattern.");
\r
176 // } catch (IllegalArgumentException e) {
\r
179 // t = new UnicodeToHexTransliterator("\\x", true, null);
\r
180 // if (t != null) {
\r
181 // errln("FAIL: Created a UnicodeToHexTransliterator with a bad pattern.");
\r
183 // } catch (IllegalArgumentException e) {
\r
185 // t = new UnicodeToHexTransliterator();
\r
187 // ((UnicodeToHexTransliterator) t).applyPattern("\\x");
\r
188 // errln("FAIL: UnicodeToHexTransliterator::applyPattern succeeded with a bad pattern.");
\r
189 // } catch (Exception e) {
\r
193 public void TestRBTErrors() {
\r
195 String rules = "ab>y";
\r
196 String id = "MyRandom-YReverse";
\r
197 String goodPattern = "[[:L:]&[\\u0000-\\uFFFF]]"; /* all BMP letters */
\r
198 UnicodeSet set = null;
\r
200 set = new UnicodeSet(goodPattern);
\r
203 Transliterator.createFromRules(id, rules, Transliterator.REVERSE);
\r
205 Transliterator.registerClass(id, t.getClass(), null);
\r
206 Transliterator.unregister(id);
\r
208 Transliterator.getInstance(id, Transliterator.REVERSE);
\r
209 errln("FAIL: construction of unregistered ID should have failed.");
\r
210 } catch (IllegalArgumentException e) {
\r
212 } catch (IllegalArgumentException e) {
\r
213 errln("FAIL: Was not able to create a good RBT to test registration.");
\r
215 } catch (IllegalArgumentException e) {
\r
216 errln("FAIL: Was not able to create a good UnicodeSet based on valid patterns.");
\r
221 // public void TestHexToUniErrors() {
\r
222 // Transliterator t = null;
\r
223 // //try { // unneeded - exception cannot be thrown
\r
224 // t = new HexToUnicodeTransliterator("", null);
\r
225 // //} catch (Exception e) {
\r
226 // // errln("FAIL: Could not create a HexToUnicodeTransliterator with an empty pattern.");
\r
229 // t = new HexToUnicodeTransliterator("\\x", null);
\r
230 // errln("FAIL: Created a HexToUnicodeTransliterator with a bad pattern.");
\r
231 // } catch (IllegalArgumentException e) {
\r
234 // t = new HexToUnicodeTransliterator();
\r
236 // ((HexToUnicodeTransliterator) t).applyPattern("\\x");
\r
237 // errln("FAIL: HexToUnicodeTransliterator::applyPattern succeeded with a bad pattern.");
\r
238 // } catch (IllegalArgumentException e) {
\r