2 *******************************************************************************
\r
3 * Copyright (C) 2001-2010, 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.TestFmwk;
\r
9 import com.ibm.icu.text.ReplaceableString;
\r
10 import com.ibm.icu.text.Transliterator;
\r
11 import com.ibm.icu.text.UnicodeSet;
\r
15 * @summary Error condition test of Transliterator
\r
17 public class ErrorTest extends TestFmwk {
\r
19 public static void main(String[] args) throws Exception {
\r
20 new ErrorTest().run(args);
\r
23 public void TestTransliteratorErrors() {
\r
24 String trans = "Latin-Greek";
\r
25 String bogusID = "LATINGREEK-GREEKLATIN";
\r
26 String newID = "Bogus-Latin";
\r
27 String newIDRules = "zzz > Z; f <> ph";
\r
28 String bogusRules = "a } [b-g m-p ";
\r
29 ReplaceableString testString =
\r
30 new ReplaceableString("A quick fox jumped over the lazy dog.");
\r
31 String insertString = "cats and dogs";
\r
32 int stoppedAt = 0, len;
\r
33 Transliterator.Position pos = new Transliterator.Position();
\r
36 Transliterator.getInstance(trans, Transliterator.FORWARD);
\r
38 errln("FAIL: construction of Latin-Greek");
\r
41 len = testString.length();
\r
42 stoppedAt = t.transliterate(testString, 0, 100);
\r
43 if (stoppedAt != -1) {
\r
44 errln("FAIL: Out of bounds check failed (1).");
\r
45 } else if (testString.length() != len) {
\r
47 new ReplaceableString("A quick fox jumped over the lazy dog.");
\r
48 errln("FAIL: Transliterate fails and the target string was modified.");
\r
50 stoppedAt = t.transliterate(testString, 100, testString.length() - 1);
\r
51 if (stoppedAt != -1) {
\r
52 errln("FAIL: Out of bounds check failed (2).");
\r
53 } else if (testString.length() != len) {
\r
55 new ReplaceableString("A quick fox jumped over the lazy dog.");
\r
56 errln("FAIL: Transliterate fails and the target string was modified.");
\r
59 pos.limit = testString.length();
\r
61 t.transliterate(testString, pos);
\r
62 errln("FAIL: Start offset is out of bounds, error not reported.");
\r
63 } catch (IllegalArgumentException e) {
\r
64 logln("Start offset is out of bounds and detected.");
\r
70 t.transliterate(testString, pos);
\r
71 errln("FAIL: Limit offset is out of bounds, error not reported.\n");
\r
72 } catch (IllegalArgumentException e) {
\r
73 logln("Start offset is out of bounds and detected.");
\r
75 len = pos.contextLimit = testString.length();
\r
76 pos.contextStart = 0;
\r
77 pos.limit = len - 1;
\r
80 t.transliterate(testString, pos, insertString);
\r
81 if (len == pos.limit) {
\r
82 errln("FAIL: Test insertion with string: the transliteration position limit didn't change as expected.");
\r
84 } catch (IllegalArgumentException e) {
\r
85 errln("Insertion test with string failed for some reason.");
\r
87 pos.contextStart = 0;
\r
88 pos.contextLimit = testString.length();
\r
89 pos.limit = testString.length() - 1;
\r
92 t.transliterate(testString, pos, 0x0061);
\r
93 if (len == pos.limit) {
\r
94 errln("FAIL: Test insertion with character: the transliteration position limit didn't change as expected.");
\r
96 } catch (IllegalArgumentException e) {
\r
97 errln("FAIL: Insertion test with UTF-16 code point failed for some reason.");
\r
99 len = pos.limit = testString.length();
\r
100 pos.contextStart = 0;
\r
101 pos.contextLimit = testString.length() - 1;
\r
104 t.transliterate(testString, pos, insertString);
\r
105 errln("FAIL: Out of bounds check failed (3).");
\r
106 if (testString.length() != len) {
\r
107 errln("FAIL: The input string was modified though the offsets were out of bounds.");
\r
109 } catch (IllegalArgumentException e) {
\r
110 logln("Insertion test with out of bounds indexes.");
\r
112 Transliterator t1 = null;
\r
114 t1 = Transliterator.getInstance(bogusID, Transliterator.FORWARD);
\r
116 errln("FAIL: construction of bogus ID \"LATINGREEK-GREEKLATIN\"");
\r
118 } catch (IllegalArgumentException e) {
\r
121 //try { // unneeded - Exception cannot be thrown
\r
122 Transliterator t2 =
\r
123 Transliterator.createFromRules(
\r
126 Transliterator.FORWARD);
\r
128 Transliterator t3 = t2.getInverse();
\r
129 errln("FAIL: The newID transliterator was not registered so createInverse should fail.");
\r
131 errln("FAIL: The newID transliterator was not registered so createInverse should fail.");
\r
133 } catch (Exception e) {
\r
135 //} catch (Exception e) { }
\r
137 Transliterator t4 =
\r
138 Transliterator.createFromRules(
\r
141 Transliterator.FORWARD);
\r
143 errln("FAIL: The rules is malformed but error was not reported.");
\r
145 } catch (Exception e) {
\r
149 public void TestUnicodeSetErrors() {
\r
150 String badPattern = "[[:L:]-[0x0300-0x0400]";
\r
151 UnicodeSet set = new UnicodeSet();
\r
154 if (!set.isEmpty()) {
\r
155 errln("FAIL: The default ctor of UnicodeSet created a non-empty object.");
\r
158 set.applyPattern(badPattern);
\r
159 errln("FAIL: Applied a bad pattern to the UnicodeSet object okay.");
\r
160 } catch (IllegalArgumentException e) {
\r
161 logln("Test applying with the bad pattern.");
\r
164 new UnicodeSet(badPattern);
\r
165 errln("FAIL: Created a UnicodeSet based on bad patterns.");
\r
166 } catch (IllegalArgumentException e) {
\r
167 logln("Test constructing with the bad pattern.");
\r
171 // public void TestUniToHexErrors() {
\r
172 // Transliterator t = null;
\r
174 // t = new UnicodeToHexTransliterator("", true, null);
\r
175 // if (t != null) {
\r
176 // errln("FAIL: Created a UnicodeToHexTransliterator with an empty pattern.");
\r
178 // } catch (IllegalArgumentException e) {
\r
181 // t = new UnicodeToHexTransliterator("\\x", true, null);
\r
182 // if (t != null) {
\r
183 // errln("FAIL: Created a UnicodeToHexTransliterator with a bad pattern.");
\r
185 // } catch (IllegalArgumentException e) {
\r
187 // t = new UnicodeToHexTransliterator();
\r
189 // ((UnicodeToHexTransliterator) t).applyPattern("\\x");
\r
190 // errln("FAIL: UnicodeToHexTransliterator::applyPattern succeeded with a bad pattern.");
\r
191 // } catch (Exception e) {
\r
195 public void TestRBTErrors() {
\r
197 String rules = "ab>y";
\r
198 String id = "MyRandom-YReverse";
\r
199 String goodPattern = "[[:L:]&[\\u0000-\\uFFFF]]"; /* all BMP letters */
\r
200 UnicodeSet set = null;
\r
202 set = new UnicodeSet(goodPattern);
\r
205 Transliterator.createFromRules(id, rules, Transliterator.REVERSE);
\r
207 Transliterator.registerClass(id, t.getClass(), null);
\r
208 Transliterator.unregister(id);
\r
210 Transliterator.getInstance(id, Transliterator.REVERSE);
\r
211 errln("FAIL: construction of unregistered ID should have failed.");
\r
212 } catch (IllegalArgumentException e) {
\r
214 } catch (IllegalArgumentException e) {
\r
215 errln("FAIL: Was not able to create a good RBT to test registration.");
\r
217 } catch (IllegalArgumentException e) {
\r
218 errln("FAIL: Was not able to create a good UnicodeSet based on valid patterns.");
\r
223 // public void TestHexToUniErrors() {
\r
224 // Transliterator t = null;
\r
225 // //try { // unneeded - exception cannot be thrown
\r
226 // t = new HexToUnicodeTransliterator("", null);
\r
227 // //} catch (Exception e) {
\r
228 // // errln("FAIL: Could not create a HexToUnicodeTransliterator with an empty pattern.");
\r
231 // t = new HexToUnicodeTransliterator("\\x", null);
\r
232 // errln("FAIL: Created a HexToUnicodeTransliterator with a bad pattern.");
\r
233 // } catch (IllegalArgumentException e) {
\r
236 // t = new HexToUnicodeTransliterator();
\r
238 // ((HexToUnicodeTransliterator) t).applyPattern("\\x");
\r
239 // errln("FAIL: HexToUnicodeTransliterator::applyPattern succeeded with a bad pattern.");
\r
240 // } catch (IllegalArgumentException e) {
\r