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