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