1/*
2 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23/*
24 * @test
25 * @bug 6521742
26 * @summary test exceptions
27 */
28
29import java.text.*;
30import java.util.*;
31import static java.text.BreakIterator.DONE;
32
33public class ExceptionTest {
34    private static final String text =
35          "An ordered collection (also known as a sequence). "
36        + "The user of this interface has precise control over "
37        + "where in the list each element is inserted. "
38        + "The user can access elements by their integer index (position in the list), "
39        + "and search for elements in the list.";
40
41    public static void main(String[] args) {
42        BreakIterator bi = BreakIterator.getWordInstance();
43        bi.setText(text);
44        MirroredBreakIterator mirror = new MirroredBreakIterator(bi);
45        final int first = bi.first();
46        if (first != 0) {
47            throw new RuntimeException("first != 0: " + first);
48        }
49        final int last = bi.last();
50        bi = BreakIterator.getWordInstance();
51        bi.setText(text);
52        int length = text.length();
53
54        /*
55         * following(int)
56         */
57        for (int i = 0; i <= length; i++) {
58            if (i == length) {
59                check(bi.following(i), DONE);
60            }
61            check(bi.following(i), mirror.following(i));
62            check(bi.current(), mirror.current());
63        }
64        for (int i = -length; i < 0; i++) {
65            checkFollowingException(bi, i);
66            checkFollowingException(mirror, i);
67            check(bi.current(), mirror.current());
68        }
69        for (int i = 1; i < length; i++) {
70            checkFollowingException(bi, length + i);
71            checkFollowingException(mirror, length + i);
72            check(bi.current(), mirror.current());
73        }
74
75        /*
76         * preceding(int)
77         */
78        for (int i = length; i >= 0; i--) {
79            if (i == 0) {
80                check(bi.preceding(i), DONE);
81            }
82            check(bi.preceding(i), mirror.preceding(i));
83            check(bi.current(), mirror.current());
84        }
85        for (int i = -length; i < 0; i++) {
86            checkPrecedingException(bi, i);
87            checkPrecedingException(mirror, i);
88            check(bi.current(), mirror.current());
89        }
90        for (int i = 1; i < length; i++) {
91            checkPrecedingException(bi, length + i);
92            checkPrecedingException(mirror, length + i);
93            check(bi.current(), mirror.current());
94        }
95
96        /*
97         * isBoundary(int)
98         */
99        for (int i = 0; i <= length; i++) {
100            check(bi.isBoundary(i), mirror.isBoundary(i));
101            check(bi.current(), mirror.current());
102        }
103        for (int i = -length; i < 0; i++) {
104            checkIsBoundaryException(bi, i);
105            checkIsBoundaryException(mirror, i);
106        }
107        for (int i = 1; i < length; i++) {
108            checkIsBoundaryException(bi, length + i);
109            checkIsBoundaryException(mirror, length + i);
110        }
111    }
112
113    private static void check(int i1, int i2) {
114        if (i1 != i2) {
115            throw new RuntimeException(i1 + " != " + i2);
116        }
117    }
118
119    private static void check(boolean b1, boolean b2) {
120        if (b1 != b2) {
121            throw new RuntimeException(b1 + " != " + b2);
122        }
123    }
124
125    private static void checkFollowingException(BreakIterator bi, int offset) {
126        try {
127            bi.following(offset);
128        } catch (IllegalArgumentException e) {
129            return; // OK
130        }
131        throw new RuntimeException(bi + ": following() doesn't throw an IAE with offset "
132                                   + offset);
133    }
134
135    private static void checkPrecedingException(BreakIterator bi, int offset) {
136        try {
137            bi.preceding(offset);
138        } catch (IllegalArgumentException e) {
139            return; // OK
140        }
141        throw new RuntimeException(bi + ": preceding() doesn't throw an IAE with offset "
142                                   + offset);
143    }
144
145    private static void checkIsBoundaryException(BreakIterator bi, int offset) {
146        try {
147            bi.isBoundary(offset);
148        } catch (IllegalArgumentException e) {
149            return; // OK
150        }
151        throw new RuntimeException(bi + ": isBoundary() doesn't throw an IAE with offset "
152                                   + offset);
153    }
154}
155