< prev index next >

test/jdk/java/text/Collator/Bug6271411.java

Print this page

  1 /*
  2  * Copyright (c) 2005, 2016, 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 /*
 25  * @test
 26  * @bug 6271411
 27  * @library /java/text/testlib
 28  * @summary Confirm that three JCK testcases for CollationElementIterator pass.

 29  */
 30 
 31 import java.text.*;
 32 




 33 /*
 34  * Based on JCK-runtime-15/tests/api/java_text/CollationElementIterator/ColltnElmtIterTests.java.
 35  */
 36 public class Bug6271411 extends IntlTest {
 37 
 38     public static void main(String argv[]) throws Exception {
 39         Bug6271411 test = new Bug6271411();
 40         test.run(argv);
 41     }
 42 
 43     /*
 44      * Rule for RuleBasedCollator
 45      */
 46     static final String rule = "< c, C < d; D";
 47 
 48     /*
 49      * Textdata
 50      */
 51     static final String[] values = {
 52         "", "c", "cH522Yd", "Hi, high school", "abcchCHidD"
 53     };
 54 
 55 
 56     /*
 57      * Confirm that setOffset() throws IllegalArgumentException
 58      * (not IndexOutOfBoundsException) if the given offset is invalid.
 59      * Use CollationElementIterator.setText(String).
 60      */

 61     public void Test_CollationElementIterator0007() throws Exception {
 62         int[] offsets = {
 63             Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -10000, -2, -1,
 64             100, 101, // These two are customized for every test data later.
 65             12345, Integer.MAX_VALUE - 1, Integer.MAX_VALUE
 66         };
 67         boolean err = false;
 68 
 69         RuleBasedCollator rbc = new RuleBasedCollator(rule);
 70         CollationElementIterator iterator = rbc.getCollationElementIterator("");
 71 
 72         for (int i = 0; i < values.length; i++) {
 73             String source = values[i];
 74             iterator.setText(source);
 75 
 76             int len = source.length();
 77             offsets[5] = len + 1;
 78             offsets[6] = len + 2;
 79 
 80             for (int j = 0; j < offsets.length; j++) {
 81                 try {
 82                     iterator.setOffset(offsets[j]);
 83                     System.out.println("IllegalArgumentException should be thrown for setOffset(" +
 84                                        offsets[j] + ") for <" + source + ">.");
 85                     err = true;
 86                 }
 87                 catch (IllegalArgumentException e) {
 88                 }
 89             }
 90         }
 91 
 92         if (err) {
 93             errln("CollationElementIterator.setOffset() didn't throw an expected IllegalArguemntException.");
 94         }
 95     }
 96 
 97     /*
 98      * Confirm that setText() doesn't throw an exception and setOffset() throws
 99      * IllegalArgumentException if the given offset is invalid.
100      * Use CollationElementIterator.setText(CharacterIterator).
101      */

102     public void Test_CollationElementIterator0010() throws Exception {
103         String prefix = "xyz abc";
104         String suffix = "1234567890";
105         int begin = prefix.length();
106         int[] offsets = {
107             Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -10000,
108             -2, -1, 0, 1, begin - 2, begin - 1, 9, 10, 11, 12, 13, 14,
109             15, 12345, Integer.MAX_VALUE - 1, Integer.MAX_VALUE
110         };
111         boolean err = false;
112 
113         RuleBasedCollator rbc = new RuleBasedCollator(rule);
114         CollationElementIterator iterator = rbc.getCollationElementIterator("");
115 
116         for (int i = 0; i < values.length; i++) {
117             String str = prefix + values[i] + suffix;
118             int len = str.length();
119             int end = len - suffix.length();
120 
121             CharacterIterator source =

127             offsets[11] = (end + len) / 2;
128             offsets[12] = len - 1;
129             offsets[13] = len;
130             offsets[14] = len + 1;
131             offsets[15] = len + 2;
132 
133             for (int j = 0; j < offsets.length; j++) {
134                 try {
135                     iterator.setOffset(offsets[j]);
136 
137                     System.out.println("IllegalArgumentException should be thrown for setOffset(" +
138                                        offsets[j] + ") for <" + str + ">.");
139                     err = true;
140                 }
141                 catch (IllegalArgumentException e) {
142                 }
143             }
144         }
145 
146         if (err) {
147             errln("CollationElementIterator.setOffset() didn't throw an expected IllegalArguemntException.");
148         }
149     }
150 
151     /*
152      * Confirm that setText() doesn't throw an exception and setOffset() sets
153      * an offset as expected.
154      * Use CollationElementIterator.setText(CharacterIterator).
155      */

156     public void Test_CollationElementIterator0011() throws Exception {
157         String prefix = "xyz abc";
158         String suffix = "1234567890";
159         int begin = prefix.length();
160         int[] offsets = { begin, begin + 1, 2, 3, 4 };
161 
162         RuleBasedCollator rbc = new RuleBasedCollator(rule);
163         CollationElementIterator iterator = rbc.getCollationElementIterator("");
164 
165         for (int i = 0; i < values.length; i++) {
166             String str = prefix + values[i] + suffix;
167             int len = str.length();
168             int end = len - suffix.length();
169             CharacterIterator source =
170                 new StringCharacterIterator(str, begin, end, begin);
171             iterator.setText(source);
172 
173             offsets[2] = (end + len) / 2;
174             offsets[3] = len - 1;
175             offsets[4] = len;

  1 /*
  2  * Copyright (c) 2005, 2023, 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 /*
 25  * @test
 26  * @bug 6271411

 27  * @summary Confirm that three JCK testcases for CollationElementIterator pass.
 28  * @run junit Bug6271411
 29  */
 30 
 31 import java.text.*;
 32 
 33 import org.junit.jupiter.api.Test;
 34 
 35 import static org.junit.jupiter.api.Assertions.fail;
 36 
 37 /*
 38  * Based on JCK-runtime-15/tests/api/java_text/CollationElementIterator/ColltnElmtIterTests.java.
 39  */
 40 public class Bug6271411 {





 41 
 42     /*
 43      * Rule for RuleBasedCollator
 44      */
 45     static final String rule = "< c, C < d; D";
 46 
 47     /*
 48      * Textdata
 49      */
 50     static final String[] values = {
 51         "", "c", "cH522Yd", "Hi, high school", "abcchCHidD"
 52     };
 53 
 54 
 55     /*
 56      * Confirm that setOffset() throws IllegalArgumentException
 57      * (not IndexOutOfBoundsException) if the given offset is invalid.
 58      * Use CollationElementIterator.setText(String).
 59      */
 60     @Test
 61     public void Test_CollationElementIterator0007() throws Exception {
 62         int[] offsets = {
 63             Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -10000, -2, -1,
 64             100, 101, // These two are customized for every test data later.
 65             12345, Integer.MAX_VALUE - 1, Integer.MAX_VALUE
 66         };
 67         boolean err = false;
 68 
 69         RuleBasedCollator rbc = new RuleBasedCollator(rule);
 70         CollationElementIterator iterator = rbc.getCollationElementIterator("");
 71 
 72         for (int i = 0; i < values.length; i++) {
 73             String source = values[i];
 74             iterator.setText(source);
 75 
 76             int len = source.length();
 77             offsets[5] = len + 1;
 78             offsets[6] = len + 2;
 79 
 80             for (int j = 0; j < offsets.length; j++) {
 81                 try {
 82                     iterator.setOffset(offsets[j]);
 83                     System.out.println("IllegalArgumentException should be thrown for setOffset(" +
 84                                        offsets[j] + ") for <" + source + ">.");
 85                     err = true;
 86                 }
 87                 catch (IllegalArgumentException e) {
 88                 }
 89             }
 90         }
 91 
 92         if (err) {
 93             fail("CollationElementIterator.setOffset() didn't throw an expected IllegalArguemntException.");
 94         }
 95     }
 96 
 97     /*
 98      * Confirm that setText() doesn't throw an exception and setOffset() throws
 99      * IllegalArgumentException if the given offset is invalid.
100      * Use CollationElementIterator.setText(CharacterIterator).
101      */
102     @Test
103     public void Test_CollationElementIterator0010() throws Exception {
104         String prefix = "xyz abc";
105         String suffix = "1234567890";
106         int begin = prefix.length();
107         int[] offsets = {
108             Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -10000,
109             -2, -1, 0, 1, begin - 2, begin - 1, 9, 10, 11, 12, 13, 14,
110             15, 12345, Integer.MAX_VALUE - 1, Integer.MAX_VALUE
111         };
112         boolean err = false;
113 
114         RuleBasedCollator rbc = new RuleBasedCollator(rule);
115         CollationElementIterator iterator = rbc.getCollationElementIterator("");
116 
117         for (int i = 0; i < values.length; i++) {
118             String str = prefix + values[i] + suffix;
119             int len = str.length();
120             int end = len - suffix.length();
121 
122             CharacterIterator source =

128             offsets[11] = (end + len) / 2;
129             offsets[12] = len - 1;
130             offsets[13] = len;
131             offsets[14] = len + 1;
132             offsets[15] = len + 2;
133 
134             for (int j = 0; j < offsets.length; j++) {
135                 try {
136                     iterator.setOffset(offsets[j]);
137 
138                     System.out.println("IllegalArgumentException should be thrown for setOffset(" +
139                                        offsets[j] + ") for <" + str + ">.");
140                     err = true;
141                 }
142                 catch (IllegalArgumentException e) {
143                 }
144             }
145         }
146 
147         if (err) {
148             fail("CollationElementIterator.setOffset() didn't throw an expected IllegalArguemntException.");
149         }
150     }
151 
152     /*
153      * Confirm that setText() doesn't throw an exception and setOffset() sets
154      * an offset as expected.
155      * Use CollationElementIterator.setText(CharacterIterator).
156      */
157     @Test
158     public void Test_CollationElementIterator0011() throws Exception {
159         String prefix = "xyz abc";
160         String suffix = "1234567890";
161         int begin = prefix.length();
162         int[] offsets = { begin, begin + 1, 2, 3, 4 };
163 
164         RuleBasedCollator rbc = new RuleBasedCollator(rule);
165         CollationElementIterator iterator = rbc.getCollationElementIterator("");
166 
167         for (int i = 0; i < values.length; i++) {
168             String str = prefix + values[i] + suffix;
169             int len = str.length();
170             int end = len - suffix.length();
171             CharacterIterator source =
172                 new StringCharacterIterator(str, begin, end, begin);
173             iterator.setText(source);
174 
175             offsets[2] = (end + len) / 2;
176             offsets[3] = len - 1;
177             offsets[4] = len;
< prev index next >