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