]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/translit/ErrorTest.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / translit / ErrorTest.java
1 /**\r
2  *******************************************************************************\r
3  * Copyright (C) 2001-2007, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\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
10 \r
11 /**\r
12  * @test\r
13  * @summary Error condition test of Transliterator\r
14  */\r
15 public class ErrorTest extends TestFmwk {\r
16 \r
17     public static void main(String[] args) throws Exception {\r
18         new ErrorTest().run(args);\r
19     }\r
20 \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
32 \r
33         Transliterator t =\r
34             Transliterator.getInstance(trans, Transliterator.FORWARD);\r
35         if (t == null) {\r
36             errln("FAIL: construction of Latin-Greek");\r
37             return;\r
38         }\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
44             testString =\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
47         }\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
52             testString =\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
55         }\r
56         pos.start = 100;\r
57         pos.limit = testString.length();\r
58         try {\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
63         }\r
64         pos.limit = 100;\r
65         pos.start = 0;\r
66 \r
67         try {\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
72         }\r
73         len = pos.contextLimit = testString.length();\r
74         pos.contextStart = 0;\r
75         pos.limit = len - 1;\r
76         pos.start = 5;\r
77         try {\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
81             }\r
82         } catch (IllegalArgumentException e) {\r
83             errln("Insertion test with string failed for some reason.");\r
84         }\r
85         pos.contextStart = 0;\r
86         pos.contextLimit = testString.length();\r
87         pos.limit = testString.length() - 1;\r
88         pos.start = 5;\r
89         try {\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
93             }\r
94         } catch (IllegalArgumentException e) {\r
95             errln("FAIL: Insertion test with UTF-16 code point failed for some reason.");\r
96         }\r
97         len = pos.limit = testString.length();\r
98         pos.contextStart = 0;\r
99         pos.contextLimit = testString.length() - 1;\r
100         pos.start = 5;\r
101         try {\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
106             }\r
107         } catch (IllegalArgumentException e) {\r
108             logln("Insertion test with out of bounds indexes.");\r
109         }\r
110         Transliterator t1 = null;\r
111         try {\r
112             t1 = Transliterator.getInstance(bogusID, Transliterator.FORWARD);\r
113             if (t1 != null) {\r
114                 errln("FAIL: construction of bogus ID \"LATINGREEK-GREEKLATIN\"");\r
115             }\r
116         } catch (IllegalArgumentException e) {\r
117         }\r
118 \r
119         //try { // unneeded - Exception cannot be thrown\r
120         Transliterator t2 =\r
121             Transliterator.createFromRules(\r
122                 newID,\r
123                 newIDRules,\r
124                 Transliterator.FORWARD);\r
125         try {\r
126             Transliterator t3 = t2.getInverse();\r
127             errln("FAIL: The newID transliterator was not registered so createInverse should fail.");\r
128             if (t3 != null) {\r
129                 errln("FAIL: The newID transliterator was not registered so createInverse should fail.");\r
130             }\r
131         } catch (Exception e) {\r
132         }\r
133         //} catch (Exception e) { }\r
134         try {\r
135             Transliterator t4 =\r
136                 Transliterator.createFromRules(\r
137                     newID,\r
138                     bogusRules,\r
139                     Transliterator.FORWARD);\r
140             if (t4 != null) {\r
141                 errln("FAIL: The rules is malformed but error was not reported.");\r
142             }\r
143         } catch (Exception e) {\r
144         }\r
145     }\r
146 \r
147     public void TestUnicodeSetErrors() {\r
148         String badPattern = "[[:L:]-[0x0300-0x0400]";\r
149         UnicodeSet set = new UnicodeSet();\r
150         //String result;\r
151 \r
152         if (!set.isEmpty()) {\r
153             errln("FAIL: The default ctor of UnicodeSet created a non-empty object.");\r
154         }\r
155         try {\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
160         }\r
161         try {\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
166         }\r
167     }\r
168 \r
169 //    public void TestUniToHexErrors() {\r
170 //        Transliterator t = null;\r
171 //        try {\r
172 //            t = new UnicodeToHexTransliterator("", true, null);\r
173 //            if (t != null) {\r
174 //                errln("FAIL: Created a UnicodeToHexTransliterator with an empty pattern.");\r
175 //            }\r
176 //        } catch (IllegalArgumentException e) {\r
177 //        }\r
178 //        try {\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
182 //            }\r
183 //        } catch (IllegalArgumentException e) {\r
184 //        }\r
185 //        t = new UnicodeToHexTransliterator();\r
186 //        try {\r
187 //            ((UnicodeToHexTransliterator) t).applyPattern("\\x");\r
188 //            errln("FAIL: UnicodeToHexTransliterator::applyPattern succeeded with a bad pattern.");\r
189 //        } catch (Exception e) {\r
190 //        }\r
191 //    }\r
192 \r
193     public void TestRBTErrors() {\r
194 \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
199         try {\r
200             set = new UnicodeSet(goodPattern);\r
201             try {\r
202                 Transliterator t =\r
203                     Transliterator.createFromRules(id, rules, Transliterator.REVERSE);\r
204                 t.setFilter(set);\r
205                 Transliterator.registerClass(id, t.getClass(), null);\r
206                 Transliterator.unregister(id);\r
207                 try {\r
208                     Transliterator.getInstance(id, Transliterator.REVERSE);\r
209                     errln("FAIL: construction of unregistered ID should have failed.");\r
210                 } catch (IllegalArgumentException e) {\r
211                 }\r
212             } catch (IllegalArgumentException e) {\r
213                 errln("FAIL: Was not able to create a good RBT to test registration.");\r
214             }\r
215         } catch (IllegalArgumentException e) {\r
216             errln("FAIL: Was not able to create a good UnicodeSet based on valid patterns.");\r
217             return;\r
218         }\r
219     }\r
220 \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
227 //        //}\r
228 //        try {\r
229 //            t = new HexToUnicodeTransliterator("\\x", null);\r
230 //            errln("FAIL: Created a HexToUnicodeTransliterator with a bad pattern.");\r
231 //        } catch (IllegalArgumentException e) {\r
232 //        }\r
233 //\r
234 //        t = new HexToUnicodeTransliterator();\r
235 //        try {\r
236 //            ((HexToUnicodeTransliterator) t).applyPattern("\\x");\r
237 //            errln("FAIL: HexToUnicodeTransliterator::applyPattern succeeded with a bad pattern.");\r
238 //        } catch (IllegalArgumentException e) {\r
239 //        }\r
240 //    }\r
241 }\r