Path: blob/master/test/jdk/java/text/Collator/Bug6271411.java
41149 views
/*1* Copyright (c) 2005, 2016, 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*/2223/*24* @test25* @bug 627141126* @library /java/text/testlib27* @summary Confirm that three JCK testcases for CollationElementIterator pass.28*/2930import java.text.*;3132/*33* Based on JCK-runtime-15/tests/api/java_text/CollationElementIterator/ColltnElmtIterTests.java.34*/35public class Bug6271411 extends IntlTest {3637public static void main(String argv[]) throws Exception {38Bug6271411 test = new Bug6271411();39test.run(argv);40}4142/*43* Rule for RuleBasedCollator44*/45static final String rule = "< c, C < d; D";4647/*48* Textdata49*/50static final String[] values = {51"", "c", "cH522Yd", "Hi, high school", "abcchCHidD"52};535455/*56* Confirm that setOffset() throws IllegalArgumentException57* (not IndexOutOfBoundsException) if the given offset is invalid.58* Use CollationElementIterator.setText(String).59*/60public void Test_CollationElementIterator0007() throws Exception {61int[] offsets = {62Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -10000, -2, -1,63100, 101, // These two are customized for every test data later.6412345, Integer.MAX_VALUE - 1, Integer.MAX_VALUE65};66boolean err = false;6768RuleBasedCollator rbc = new RuleBasedCollator(rule);69CollationElementIterator iterator = rbc.getCollationElementIterator("");7071for (int i = 0; i < values.length; i++) {72String source = values[i];73iterator.setText(source);7475int len = source.length();76offsets[5] = len + 1;77offsets[6] = len + 2;7879for (int j = 0; j < offsets.length; j++) {80try {81iterator.setOffset(offsets[j]);82System.out.println("IllegalArgumentException should be thrown for setOffset(" +83offsets[j] + ") for <" + source + ">.");84err = true;85}86catch (IllegalArgumentException e) {87}88}89}9091if (err) {92errln("CollationElementIterator.setOffset() didn't throw an expected IllegalArguemntException.");93}94}9596/*97* Confirm that setText() doesn't throw an exception and setOffset() throws98* IllegalArgumentException if the given offset is invalid.99* Use CollationElementIterator.setText(CharacterIterator).100*/101public void Test_CollationElementIterator0010() throws Exception {102String prefix = "xyz abc";103String suffix = "1234567890";104int begin = prefix.length();105int[] offsets = {106Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -10000,107-2, -1, 0, 1, begin - 2, begin - 1, 9, 10, 11, 12, 13, 14,10815, 12345, Integer.MAX_VALUE - 1, Integer.MAX_VALUE109};110boolean err = false;111112RuleBasedCollator rbc = new RuleBasedCollator(rule);113CollationElementIterator iterator = rbc.getCollationElementIterator("");114115for (int i = 0; i < values.length; i++) {116String str = prefix + values[i] + suffix;117int len = str.length();118int end = len - suffix.length();119120CharacterIterator source =121new StringCharacterIterator(str, begin, end, begin);122iterator.setText(source);123124offsets[9] = end + 1;125offsets[10] = end + 2;126offsets[11] = (end + len) / 2;127offsets[12] = len - 1;128offsets[13] = len;129offsets[14] = len + 1;130offsets[15] = len + 2;131132for (int j = 0; j < offsets.length; j++) {133try {134iterator.setOffset(offsets[j]);135136System.out.println("IllegalArgumentException should be thrown for setOffset(" +137offsets[j] + ") for <" + str + ">.");138err = true;139}140catch (IllegalArgumentException e) {141}142}143}144145if (err) {146errln("CollationElementIterator.setOffset() didn't throw an expected IllegalArguemntException.");147}148}149150/*151* Confirm that setText() doesn't throw an exception and setOffset() sets152* an offset as expected.153* Use CollationElementIterator.setText(CharacterIterator).154*/155public void Test_CollationElementIterator0011() throws Exception {156String prefix = "xyz abc";157String suffix = "1234567890";158int begin = prefix.length();159int[] offsets = { begin, begin + 1, 2, 3, 4 };160161RuleBasedCollator rbc = new RuleBasedCollator(rule);162CollationElementIterator iterator = rbc.getCollationElementIterator("");163164for (int i = 0; i < values.length; i++) {165String str = prefix + values[i] + suffix;166int len = str.length();167int end = len - suffix.length();168CharacterIterator source =169new StringCharacterIterator(str, begin, end, begin);170iterator.setText(source);171172offsets[2] = (end + len) / 2;173offsets[3] = len - 1;174offsets[4] = len;175176for (int j = 0; j < offsets.length; j++) {177int offset = offsets[j];178179if (offset < begin || offset > end) {180break;181}182183iterator.setOffset(offset);184int newOffset = iterator.getOffset();185186if (newOffset != offset) {187throw new RuntimeException("setOffset() didn't set a correct offset. Got: " +188newOffset + " Expected: " + offset + " for <" + str + ">.");189}190}191}192}193}194195196