~ubuntu-branches/ubuntu/vivid/icu4j-4.4/vivid

« back to all changes in this revision

Viewing changes to main/tests/translit/src/com/ibm/icu/dev/test/translit/ErrorTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Niels Thykier
  • Date: 2011-08-02 15:50:33 UTC
  • Revision ID: james.westby@ubuntu.com-20110802155033-itjzsl21y2lqdonn
Tags: upstream-4.4.2
ImportĀ upstreamĀ versionĀ 4.4.2

Show diffs side-by-side

added added

removed removed

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