1/* GENERATED SOURCE. DO NOT MODIFY. */
2// © 2016 and later: Unicode, Inc. and others.
3// License & terms of use: http://www.unicode.org/copyright.html#License
4/**
5 *******************************************************************************
6 * Copyright (C) 2001-2010, International Business Machines Corporation and    *
7 * others. All Rights Reserved.                                                *
8 *******************************************************************************
9 */
10package android.icu.dev.test.translit;
11import org.junit.Test;
12
13import android.icu.dev.test.TestFmwk;
14import android.icu.text.ReplaceableString;
15import android.icu.text.Transliterator;
16import android.icu.text.UnicodeSet;
17
18/**
19 * @test
20 * @summary Error condition test of Transliterator
21 */
22public class ErrorTest extends TestFmwk {
23
24    @Test
25    public void TestTransliteratorErrors() {
26        String trans = "Latin-Greek";
27        String bogusID = "LATINGREEK-GREEKLATIN";
28        String newID = "Bogus-Latin";
29        String newIDRules = "zzz > Z; f <> ph";
30        String bogusRules = "a } [b-g m-p ";
31        ReplaceableString testString =
32            new ReplaceableString("A quick fox jumped over the lazy dog.");
33        String insertString = "cats and dogs";
34        int stoppedAt = 0, len;
35        Transliterator.Position pos = new Transliterator.Position();
36
37        Transliterator t =
38            Transliterator.getInstance(trans, Transliterator.FORWARD);
39        if (t == null) {
40            errln("FAIL: construction of Latin-Greek");
41            return;
42        }
43        len = testString.length();
44        stoppedAt = t.transliterate(testString, 0, 100);
45        if (stoppedAt != -1) {
46            errln("FAIL: Out of bounds check failed (1).");
47        } else if (testString.length() != len) {
48            testString =
49                new ReplaceableString("A quick fox jumped over the lazy dog.");
50            errln("FAIL: Transliterate fails and the target string was modified.");
51        }
52        stoppedAt = t.transliterate(testString, 100, testString.length() - 1);
53        if (stoppedAt != -1) {
54            errln("FAIL: Out of bounds check failed (2).");
55        } else if (testString.length() != len) {
56            testString =
57                new ReplaceableString("A quick fox jumped over the lazy dog.");
58            errln("FAIL: Transliterate fails and the target string was modified.");
59        }
60        pos.start = 100;
61        pos.limit = testString.length();
62        try {
63            t.transliterate(testString, pos);
64            errln("FAIL: Start offset is out of bounds, error not reported.");
65        } catch (IllegalArgumentException e) {
66            logln("Start offset is out of bounds and detected.");
67        }
68        pos.limit = 100;
69        pos.start = 0;
70
71        try {
72            t.transliterate(testString, pos);
73            errln("FAIL: Limit offset is out of bounds, error not reported.\n");
74        } catch (IllegalArgumentException e) {
75            logln("Start offset is out of bounds and detected.");
76        }
77        len = pos.contextLimit = testString.length();
78        pos.contextStart = 0;
79        pos.limit = len - 1;
80        pos.start = 5;
81        try {
82            t.transliterate(testString, pos, insertString);
83            if (len == pos.limit) {
84                errln("FAIL: Test insertion with string: the transliteration position limit didn't change as expected.");
85            }
86        } catch (IllegalArgumentException e) {
87            errln("Insertion test with string failed for some reason.");
88        }
89        pos.contextStart = 0;
90        pos.contextLimit = testString.length();
91        pos.limit = testString.length() - 1;
92        pos.start = 5;
93        try {
94            t.transliterate(testString, pos, 0x0061);
95            if (len == pos.limit) {
96                errln("FAIL: Test insertion with character: the transliteration position limit didn't change as expected.");
97            }
98        } catch (IllegalArgumentException e) {
99            errln("FAIL: Insertion test with UTF-16 code point failed for some reason.");
100        }
101        len = pos.limit = testString.length();
102        pos.contextStart = 0;
103        pos.contextLimit = testString.length() - 1;
104        pos.start = 5;
105        try {
106            t.transliterate(testString, pos, insertString);
107            errln("FAIL: Out of bounds check failed (3).");
108            if (testString.length() != len) {
109                errln("FAIL: The input string was modified though the offsets were out of bounds.");
110            }
111        } catch (IllegalArgumentException e) {
112            logln("Insertion test with out of bounds indexes.");
113        }
114        Transliterator t1 = null;
115        try {
116            t1 = Transliterator.getInstance(bogusID, Transliterator.FORWARD);
117            if (t1 != null) {
118                errln("FAIL: construction of bogus ID \"LATINGREEK-GREEKLATIN\"");
119            }
120        } catch (IllegalArgumentException e) {
121        }
122
123        //try { // unneeded - Exception cannot be thrown
124        Transliterator t2 =
125            Transliterator.createFromRules(
126                newID,
127                newIDRules,
128                Transliterator.FORWARD);
129        try {
130            Transliterator t3 = t2.getInverse();
131            errln("FAIL: The newID transliterator was not registered so createInverse should fail.");
132            if (t3 != null) {
133                errln("FAIL: The newID transliterator was not registered so createInverse should fail.");
134            }
135        } catch (Exception e) {
136        }
137        //} catch (Exception e) { }
138        try {
139            Transliterator t4 =
140                Transliterator.createFromRules(
141                    newID,
142                    bogusRules,
143                    Transliterator.FORWARD);
144            if (t4 != null) {
145                errln("FAIL: The rules is malformed but error was not reported.");
146            }
147        } catch (Exception e) {
148        }
149    }
150
151    @Test
152    public void TestUnicodeSetErrors() {
153        String badPattern = "[[:L:]-[0x0300-0x0400]";
154        UnicodeSet set = new UnicodeSet();
155        //String result;
156
157        if (!set.isEmpty()) {
158            errln("FAIL: The default ctor of UnicodeSet created a non-empty object.");
159        }
160        try {
161            set.applyPattern(badPattern);
162            errln("FAIL: Applied a bad pattern to the UnicodeSet object okay.");
163        } catch (IllegalArgumentException e) {
164            logln("Test applying with the bad pattern.");
165        }
166        try {
167            new UnicodeSet(badPattern);
168            errln("FAIL: Created a UnicodeSet based on bad patterns.");
169        } catch (IllegalArgumentException e) {
170            logln("Test constructing with the bad pattern.");
171        }
172    }
173
174//    public void TestUniToHexErrors() {
175//        Transliterator t = null;
176//        try {
177//            t = new UnicodeToHexTransliterator("", true, null);
178//            if (t != null) {
179//                errln("FAIL: Created a UnicodeToHexTransliterator with an empty pattern.");
180//            }
181//        } catch (IllegalArgumentException e) {
182//        }
183//        try {
184//            t = new UnicodeToHexTransliterator("\\x", true, null);
185//            if (t != null) {
186//                errln("FAIL: Created a UnicodeToHexTransliterator with a bad pattern.");
187//            }
188//        } catch (IllegalArgumentException e) {
189//        }
190//        t = new UnicodeToHexTransliterator();
191//        try {
192//            ((UnicodeToHexTransliterator) t).applyPattern("\\x");
193//            errln("FAIL: UnicodeToHexTransliterator::applyPattern succeeded with a bad pattern.");
194//        } catch (Exception e) {
195//        }
196//    }
197
198    @Test
199    public void TestRBTErrors() {
200
201        String rules = "ab>y";
202        String id = "MyRandom-YReverse";
203        String goodPattern = "[[:L:]&[\\u0000-\\uFFFF]]"; /* all BMP letters */
204        UnicodeSet set = null;
205        try {
206            set = new UnicodeSet(goodPattern);
207            try {
208                Transliterator t =
209                    Transliterator.createFromRules(id, rules, Transliterator.REVERSE);
210                t.setFilter(set);
211                Transliterator.registerClass(id, t.getClass(), null);
212                Transliterator.unregister(id);
213                try {
214                    Transliterator.getInstance(id, Transliterator.REVERSE);
215                    errln("FAIL: construction of unregistered ID should have failed.");
216                } catch (IllegalArgumentException e) {
217                }
218            } catch (IllegalArgumentException e) {
219                errln("FAIL: Was not able to create a good RBT to test registration.");
220            }
221        } catch (IllegalArgumentException e) {
222            errln("FAIL: Was not able to create a good UnicodeSet based on valid patterns.");
223            return;
224        }
225    }
226
227//    public void TestHexToUniErrors() {
228//        Transliterator t = null;
229//        //try { // unneeded - exception cannot be thrown
230//        t = new HexToUnicodeTransliterator("", null);
231//        //} catch (Exception e) {
232//        //    errln("FAIL: Could not create a HexToUnicodeTransliterator with an empty pattern.");
233//        //}
234//        try {
235//            t = new HexToUnicodeTransliterator("\\x", null);
236//            errln("FAIL: Created a HexToUnicodeTransliterator with a bad pattern.");
237//        } catch (IllegalArgumentException e) {
238//        }
239//
240//        t = new HexToUnicodeTransliterator();
241//        try {
242//            ((HexToUnicodeTransliterator) t).applyPattern("\\x");
243//            errln("FAIL: HexToUnicodeTransliterator::applyPattern succeeded with a bad pattern.");
244//        } catch (IllegalArgumentException e) {
245//        }
246//    }
247}
248