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 29 package test.java.text.BreakIterator; 30 31 import java.text.*; 32 import java.util.*; 33 import static java.text.BreakIterator.DONE; 34 35 public class ExceptionTest { 36 private static final String text = 37 "An ordered collection (also known as a sequence). " 38 + "The user of this interface has precise control over " 39 + "where in the list each element is inserted. " 40 + "The user can access elements by their integer index (position in the list), " 41 + "and search for elements in the list."; 42 main(String[] args)43 public static void main(String[] args) { 44 BreakIterator bi = BreakIterator.getWordInstance(); 45 bi.setText(text); 46 MirroredBreakIterator mirror = new MirroredBreakIterator(bi); 47 final int first = bi.first(); 48 if (first != 0) { 49 throw new RuntimeException("first != 0: " + first); 50 } 51 final int last = bi.last(); 52 bi = BreakIterator.getWordInstance(); 53 bi.setText(text); 54 int length = text.length(); 55 56 /* 57 * following(int) 58 */ 59 for (int i = 0; i <= length; i++) { 60 if (i == length) { 61 check(bi.following(i), DONE); 62 } 63 check(bi.following(i), mirror.following(i)); 64 check(bi.current(), mirror.current()); 65 } 66 for (int i = -length; i < 0; i++) { 67 checkFollowingException(bi, i); 68 checkFollowingException(mirror, i); 69 check(bi.current(), mirror.current()); 70 } 71 for (int i = 1; i < length; i++) { 72 checkFollowingException(bi, length + i); 73 checkFollowingException(mirror, length + i); 74 check(bi.current(), mirror.current()); 75 } 76 77 /* 78 * preceding(int) 79 */ 80 for (int i = length; i >= 0; i--) { 81 if (i == 0) { 82 check(bi.preceding(i), DONE); 83 } 84 check(bi.preceding(i), mirror.preceding(i)); 85 check(bi.current(), mirror.current()); 86 } 87 for (int i = -length; i < 0; i++) { 88 checkPrecedingException(bi, i); 89 checkPrecedingException(mirror, i); 90 check(bi.current(), mirror.current()); 91 } 92 for (int i = 1; i < length; i++) { 93 checkPrecedingException(bi, length + i); 94 checkPrecedingException(mirror, length + i); 95 check(bi.current(), mirror.current()); 96 } 97 98 /* 99 * isBoundary(int) 100 */ 101 for (int i = 0; i <= length; i++) { 102 check(bi.isBoundary(i), mirror.isBoundary(i)); 103 check(bi.current(), mirror.current()); 104 } 105 for (int i = -length; i < 0; i++) { 106 checkIsBoundaryException(bi, i); 107 checkIsBoundaryException(mirror, i); 108 } 109 for (int i = 1; i < length; i++) { 110 checkIsBoundaryException(bi, length + i); 111 checkIsBoundaryException(mirror, length + i); 112 } 113 } 114 check(int i1, int i2)115 private static void check(int i1, int i2) { 116 if (i1 != i2) { 117 throw new RuntimeException(i1 + " != " + i2); 118 } 119 } 120 check(boolean b1, boolean b2)121 private static void check(boolean b1, boolean b2) { 122 if (b1 != b2) { 123 throw new RuntimeException(b1 + " != " + b2); 124 } 125 } 126 checkFollowingException(BreakIterator bi, int offset)127 private static void checkFollowingException(BreakIterator bi, int offset) { 128 try { 129 bi.following(offset); 130 } catch (IllegalArgumentException e) { 131 return; // OK 132 } 133 throw new RuntimeException(bi + ": following() doesn't throw an IAE with offset " 134 + offset); 135 } 136 checkPrecedingException(BreakIterator bi, int offset)137 private static void checkPrecedingException(BreakIterator bi, int offset) { 138 try { 139 bi.preceding(offset); 140 } catch (IllegalArgumentException e) { 141 return; // OK 142 } 143 throw new RuntimeException(bi + ": preceding() doesn't throw an IAE with offset " 144 + offset); 145 } 146 checkIsBoundaryException(BreakIterator bi, int offset)147 private static void checkIsBoundaryException(BreakIterator bi, int offset) { 148 try { 149 bi.isBoundary(offset); 150 } catch (IllegalArgumentException e) { 151 return; // OK 152 } 153 throw new RuntimeException(bi + ": isBoundary() doesn't throw an IAE with offset " 154 + offset); 155 } 156 } 157