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