1 /* 2 * Copyright (c) 1997, 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 * 26 * A part of tests on the bug 4106263. CollationKey became non-final extendable class. 27 * The implementation of CollationKey is moved to the new private class, 28 * RuleBasedCollationKey. This test basically tests on the two features: 29 * 1. Existing code using CollationKey works (backward compatiblility) 30 * 2. CollationKey can be extended by its subclass. 31 */ 32 33 import java.util.Locale; 34 import java.text.Collator; 35 import java.text.CollationKey; 36 import java.io.*; 37 38 import java.text.*; 39 40 41 public class CollationKeyTestImpl extends CollationKey { 42 43 private static String[] sourceData_ja = { 44 "\u3042\u3044\u3046\u3048\u3048", 45 "\u3041\u3043\u3045\u3047\u3049", 46 "\u3052\u3054\u3056\u3058\u3058", 47 "\u3051\u3053\u3055\u3057\u3059", 48 "\u3062\u3064\u3066\u3068\u3068", 49 "\u3061\u3063\u3065\u3067\u3069", 50 "\u3072\u3074\u3075\u3078\u3078", 51 "\u3071\u3073\u3075\u3077\u3079", 52 "\u3082\u3084\u3085\u3088\u3088", 53 "\u3081\u3083\u3085\u3087\u3089", 54 "\u30a2\u30a4\u30a6\u30a8\u30aa", 55 "\u30a1\u30a3\u30a5\u30a7\u30a9", 56 "\u30c2\u30c4\u30c6\u30c8\u30ca", 57 "\u30c1\u30c3\u30c5\u30c7\u30c9", 58 "\u30b2\u30b4\u30b6\u30b8\u30ba", 59 "\u30b1\u30b3\u30b5\u30b7\u30b9", 60 "\u30d2\u30d4\u30d6\u30d8\u30da", 61 "\u30d1\u30d3\u30d5\u30d7\u30d9", 62 "\u30e2\u30e4\u30e6\u30e8\u30ea", 63 "\u30e1\u30e3\u30e5\u30e7\u30e9" 64 }; 65 private static final String[] targetData_ja = { 66 "\u3042\u3044\u3046\u3048\u3048", 67 "\u3041\u3043\u3045\u3047\u3049", 68 "\u30a2\u30a4\u30a6\u30a8\u30aa", 69 "\u30a1\u30a3\u30a5\u30a7\u30a9", 70 "\u3052\u3054\u3056\u3058\u3058", 71 "\u3051\u3053\u3055\u3057\u3059", 72 "\u30b1\u30b3\u30b5\u30b7\u30b9", 73 "\u30b2\u30b4\u30b6\u30b8\u30ba", 74 "\u3061\u3063\u3065\u3067\u3069", 75 "\u30c1\u30c3\u30c5\u30c7\u30c9", 76 "\u3062\u3064\u3066\u3068\u3068", 77 "\u30c2\u30c4\u30c6\u30c8\u30ca", 78 "\u3071\u3073\u3075\u3077\u3079", 79 "\u30d1\u30d3\u30d5\u30d7\u30d9", 80 "\u3072\u3074\u3075\u3078\u3078", 81 "\u30d2\u30d4\u30d6\u30d8\u30da", 82 "\u3081\u3083\u3085\u3087\u3089", 83 "\u30e1\u30e3\u30e5\u30e7\u30e9", 84 "\u3082\u3084\u3085\u3088\u3088", 85 "\u30e2\u30e4\u30e6\u30e8\u30ea" 86 }; 87 88 public void run() { 89 /** debug: printout the test data 90 for (int i=0; i<sourceData_ja.length; i++){ 91 System.out.println(i+": "+sourceData_ja[i]); 92 } 93 **/ 94 /* 95 * 1. Test the backward compatibility 96 * note: targetData_ja.length is equal to sourceData_ja.length 97 */ 98 Collator myCollator = Collator.getInstance(Locale.JAPAN); 99 CollationKey[] keys = new CollationKey[sourceData_ja.length]; 100 CollationKey[] target_keys = new CollationKey[targetData_ja.length]; 101 for (int i=0; i<sourceData_ja.length; i++){ 102 keys[i] = myCollator.getCollationKey(sourceData_ja[i]); 103 target_keys[i] = myCollator.getCollationKey(targetData_ja[i]); //used later 104 } 105 /* Sort the string using CollationKey */ 106 InsertionSort(keys); 107 /** debug: printout the result after sort 108 System.out.println("--- After Sorting ---"); 109 for (int i=0; i<sourceData_ja.length; i++){ 110 System.out.println(i+" :"+keys[i].getSourceString()); 111 } 112 **/ 113 /* 114 * Compare the result using equals method and getSourceString method. 115 */ 116 boolean pass = true; 117 for (int i=0; i<sourceData_ja.length; i++){ 118 /* Comparing using String.equals: in order to use getStringSource() */ 119 if (! targetData_ja[i].equals(keys[i].getSourceString())){ 120 throw new RuntimeException("FAILED: CollationKeyTest backward compatibility " 121 +"while comparing" +targetData_ja[i]+" vs " 122 +keys[i].getSourceString()); 123 } 124 /* Comparing using CollaionKey.equals: in order to use equals() */ 125 if (! target_keys[i].equals(keys[i])){ 126 throw new RuntimeException("FAILED: CollationKeyTest backward compatibility." 127 +" Using CollationKey.equals " +targetData_ja[i] 128 +" vs " +keys[i].getSourceString()); 129 } 130 /* Comparing using CollaionKey.hashCode(): in order to use hashCode() */ 131 if (target_keys[i].hashCode() != keys[i].hashCode()){ 132 throw new RuntimeException("FAILED: CollationKeyTest backward compatibility." 133 +" Using CollationKey.hashCode " +targetData_ja[i] 134 +" vs " +keys[i].getSourceString()); 135 } 136 /* Comparing using CollaionKey.toByteArray(): in order to use toByteArray() */ 137 byte[] target_bytes = target_keys[i].toByteArray(); 138 byte[] source_bytes = keys[i].toByteArray(); 139 for (int j=0; j<target_bytes.length; j++){ 140 Byte targetByte = new Byte(target_bytes[j]); 141 Byte sourceByte = new Byte(source_bytes[j]); 142 if (targetByte.compareTo(sourceByte)!=0){ 143 throw new RuntimeException("FAILED: CollationKeyTest backward " 144 +"compatibility. Using Byte.compareTo from CollationKey.toByteArray " 145 +targetData_ja[i] 146 +" vs " +keys[i].getSourceString()); 147 } 148 } 149 } 150 testSubclassMethods(); 151 testConstructor(); 152 } 153 154 /* 155 * Sort the array of CollationKey using compareTo method in insertion sort. 156 */ 157 private void InsertionSort(CollationKey[] keys){ 158 int f, i; 159 CollationKey tmp; 160 161 for (f=1; f < keys.length; f++){ 162 if(keys[f].compareTo( keys[f-1]) > 0){ 163 continue; 164 } 165 tmp = keys[f]; 166 i = f-1; 167 while ( (i>=0) && (keys[i].compareTo(tmp) > 0) ) { 168 keys[i+1] = keys[i]; 169 i--; 170 } 171 keys[i+1]=tmp; 172 } 173 } 174 175 176 /* 177 * From here is the bogus methods to test the subclass of 178 * the CollationKey class. 179 */ 180 public CollationKeyTestImpl(String str){ 181 super (str); 182 // debug: System.out.println("CollationKeyTest extends CollationKey class: "+str); 183 } 184 /* abstract method: needs to be implemented */ 185 public byte[] toByteArray(){ 186 String foo= "Hello"; 187 return foo.getBytes(); 188 } 189 /* abstract method: needs to be implemented */ 190 public int compareTo(CollationKey target){ 191 return 0; 192 } 193 public boolean equals(Object target){ 194 return true; 195 } 196 public String getSourceString(){ 197 return "CollationKeyTestImpl"; 198 } 199 /* 200 * This method tests the collection of bugus methods from the extended 201 * subclass of CollationKey class. 202 */ 203 private void testSubclassMethods() { 204 CollationKeyTestImpl clt1 = new CollationKeyTestImpl("testSubclassMethods-1"); 205 CollationKeyTestImpl clt2 = new CollationKeyTestImpl("testSubclassMethods-2"); 206 // extended method, equals always returns true 207 if (!clt1.equals(clt2)){ 208 throw new RuntimeException("Failed: equals(CollationKeySubClass)"); 209 } 210 // extended method, compareTo always returns 0 211 if (clt1.compareTo(clt2)!=0){ 212 throw new RuntimeException("Failed: compareTo(CollationKeySubClass)"); 213 } 214 // overriding extended method, getSourceString always returns "CollationKeyTestImpl" 215 if (! clt1.getSourceString().equals("CollationKeyTestImpl")){ 216 throw new RuntimeException("Failed: CollationKey subclass overriding getSourceString()"); 217 } 218 // extended method, toByteArray always returns bytes from "Hello" 219 String str2 = new String( clt2.toByteArray()); 220 if (! clt2.equals("Hello")){ 221 throw new RuntimeException("Failed: CollationKey subclass toByteArray()"); 222 } 223 } 224 225 /* 226 * This method tests CollationKey constructor with null source string. 227 * It should throw NPE. 228 */ 229 private void testConstructor() { 230 boolean npe=false; 231 try{ 232 CollationKeyTestImpl cltNull = new CollationKeyTestImpl(null); 233 } catch (NullPointerException npException){ 234 npe=true; 235 // debug: System.out.println("--- NPE is thrown with NULL arguement: PASS ---"); 236 } 237 if(!npe){ 238 throw new RuntimeException("Failed: CollationKey Constructor with null source"+ 239 " didn't throw NPE!"); 240 } 241 } 242 243 }