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