1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17package org.apache.harmony.text.tests.java.text; 18 19import dalvik.annotation.TestLevel; 20import dalvik.annotation.TestTargetClass; 21import dalvik.annotation.TestTargetNew; 22 23import java.text.CollationKey; 24import java.text.Collator; 25import java.text.ParseException; 26import java.text.RuleBasedCollator; 27import java.util.Arrays; 28 29 30@TestTargetClass(CollationKey.class) 31public class CollationKeyTest extends junit.framework.TestCase { 32 33 /** 34 * @tests java.text.CollationKey#compareTo(java.text.CollationKey) 35 */ 36 @TestTargetNew( 37 level = TestLevel.COMPLETE, 38 notes = "", 39 method = "compareTo", 40 args = {java.text.CollationKey.class} 41 ) 42 public void test_compareToLjava_text_CollationKey() { 43 Collator collator = Collator.getInstance(); 44 collator.setStrength(Collator.PRIMARY); 45 CollationKey key1 = collator.getCollationKey("abc"); 46 CollationKey key2 = collator.getCollationKey("ABC"); 47 assertEquals("Should be equal", 0, key1.compareTo(key2)); 48 } 49 50 /** 51 * @tests java.text.CollationKey#compareTo(java.lang.Object) 52 */ 53 @TestTargetNew( 54 level = TestLevel.COMPLETE, 55 notes = "", 56 method = "compareTo", 57 args = {java.lang.Object.class} 58 ) 59 public void test_compareToLjava_lang_Object() { 60 // Test for method int 61 // java.text.CollationKey.compareTo(java.lang.Object) 62 Collator collator = Collator.getInstance(); 63 collator.setStrength(Collator.PRIMARY); 64 CollationKey key1 = collator.getCollationKey("abc"); 65 CollationKey key2 = collator.getCollationKey("ABC"); 66 assertEquals("Should be equal", 0, key1.compareTo(key2)); 67 } 68 69 /** 70 * @tests java.text.CollationKey#equals(java.lang.Object) 71 */ 72 @TestTargetNew( 73 level = TestLevel.COMPLETE, 74 notes = "", 75 method = "equals", 76 args = {java.lang.Object.class} 77 ) 78 public void test_equalsLjava_lang_Object() { 79 Collator collator = Collator.getInstance(); 80 collator.setStrength(Collator.PRIMARY); 81 CollationKey key1 = collator.getCollationKey("abc"); 82 CollationKey key2 = collator.getCollationKey("ABC"); 83 assertTrue("Should be equal", key1.equals(key2)); 84 } 85 86 /** 87 * @tests java.text.CollationKey#getSourceString() 88 */ 89 @TestTargetNew( 90 level = TestLevel.COMPLETE, 91 notes = "", 92 method = "getSourceString", 93 args = {} 94 ) 95 public void test_getSourceString() { 96 Collator collator = Collator.getInstance(); 97 collator.setStrength(Collator.PRIMARY); 98 assertTrue("Wrong source string1", collator.getCollationKey("abc") 99 .getSourceString() == "abc"); 100 assertTrue("Wrong source string2", collator.getCollationKey("ABC") 101 .getSourceString() == "ABC"); 102 } 103 104 /** 105 * @tests java.text.CollationKey#hashCode() 106 */ 107 @TestTargetNew( 108 level = TestLevel.COMPLETE, 109 notes = "", 110 method = "hashCode", 111 args = {} 112 ) 113 public void test_hashCode() { 114 Collator collator = Collator.getInstance(); 115 collator.setStrength(Collator.PRIMARY); 116 CollationKey key1 = collator.getCollationKey("abc"); 117 CollationKey key2 = collator.getCollationKey("ABC"); 118 assertTrue("Should be equal", key1.hashCode() == key2.hashCode()); 119 } 120 121 /** 122 * @tests java.text.CollationKey#toByteArray() 123 */ 124 @TestTargetNew( 125 level = TestLevel.COMPLETE, 126 method = "toByteArray", 127 args = {} 128 ) 129 public void test_toByteArray() { 130 // Test for method byte [] java.text.CollationKey.toByteArray() 131 Collator collator = Collator.getInstance(); 132 collator.setStrength(Collator.PRIMARY); 133 CollationKey key1 = collator.getCollationKey("abc"); 134 byte[] bytes = key1.toByteArray(); 135 assertTrue("Not enough bytes", bytes.length >= 3); 136 137 try { 138 collator = new RuleBasedCollator("= 1 , 2 ; 3 , 4 < 5 ; 6 , 7"); 139 } catch (ParseException e) { 140 fail("ParseException"); 141 return; 142 } 143 /* 144 * CollationElementIterator it = 145 * ((RuleBasedCollator)collator).getCollationElementIterator("1234567"); 146 * int order; while ((order = it.next()) != 147 * CollationElementIterator.NULLORDER) { 148 * System.out.println(Integer.toHexString(order)); } for (int i=0; i<bytes.length; 149 * i+=2) { System.out.print(Integer.toHexString(bytes[i]) + 150 * Integer.toHexString(bytes[i+1]) + " "); } System.out.println(); 151 */ 152 153 // The RI has a different algorithm to generate the collation keys. 154 // bytes = collator.getCollationKey("1234567").toByteArray(); 155 // byte[] result = new byte[] { 0, 2, 0, 2, 0, 2, 0, 0, 0, 3, 0, 3, 0, 1, 156 // 0, 2, 0, 2, 0, 0, 0, 4, 0, 4, 0, 1, 0, 1, 0, 2 }; 157 byte[] bytes1 = collator.getCollationKey("12").toByteArray(); 158 byte[] bytes2 = collator.getCollationKey("123").toByteArray(); 159 byte[] bytes3 = collator.getCollationKey("124").toByteArray(); 160 byte[] bytes4 = collator.getCollationKey("1245").toByteArray(); 161 byte[] bytes5 = collator.getCollationKey("1245").toByteArray(); 162 163 assertTrue("returned collation key does not sort correctly", 164 compareUnsignedByteArrays(bytes1, bytes2) < 0); 165 166 assertTrue("returned collation key does not sort correctly", 167 compareUnsignedByteArrays(bytes2, bytes3) < 0); 168 169 assertTrue("returned collation key does not sort correctly", 170 compareUnsignedByteArrays(bytes3, bytes4) < 0); 171 172 assertTrue("returned collation key does not sort correctly", 173 compareUnsignedByteArrays(bytes4, bytes5) == 0); 174 175 } 176 177 private int compareUnsignedByteArrays(byte[] bytes1, byte[] bytes2) { 178 int commonLength = Math.min(bytes1.length, bytes2.length); 179 180 for (int i = 0; i < commonLength; i++) { 181 int keyA = bytes1[i] & 0xFF; 182 int keyB = bytes2[i] & 0xFF; 183 if (keyA < keyB) { 184 return -1; 185 } 186 if (keyA > keyB) { 187 return 1; 188 } 189 } 190 191 if (bytes1.length < bytes2.length) { 192 return -1; 193 } else if (bytes1.length > bytes2.length) { 194 return 1; 195 } else { 196 return 0; 197 } 198 } 199} 200