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 =
122                 new StringCharacterIterator(str, begin, end, begin);
123             iterator.setText(source);
124 
125             offsets[9] = end + 1;
126             offsets[10] = end + 2;
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;
176 
177             for (int j = 0; j < offsets.length; j++) {
178                 int offset = offsets[j];
179 
180                 if (offset < begin || offset > end) {
181                     break;
182                 }
183 
184                 iterator.setOffset(offset);
185                 int newOffset = iterator.getOffset();
186 
187                 if (newOffset != offset) {
188                     throw new RuntimeException("setOffset() didn't set a correct offset. Got: " +
189                     newOffset + " Expected: " + offset + " for <" + str + ">.");
190                 }
191             }
192         }
193     }
194 }