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 java.text.CollationKey; 20import java.text.Collator; 21import java.text.ParseException; 22import java.text.RuleBasedCollator; 23import java.util.Arrays; 24 25public class CollationKeyTest extends junit.framework.TestCase { 26 27 /** 28 * @tests java.text.CollationKey#compareTo(java.text.CollationKey) 29 */ 30 public void test_compareToLjava_text_CollationKey() { 31 Collator collator = Collator.getInstance(); 32 collator.setStrength(Collator.PRIMARY); 33 CollationKey key1 = collator.getCollationKey("abc"); 34 CollationKey key2 = collator.getCollationKey("ABC"); 35 assertEquals("Should be equal", 0, key1.compareTo(key2)); 36 } 37 38 /** 39 * @tests java.text.CollationKey#compareTo(java.lang.Object) 40 */ 41 public void test_compareToLjava_lang_Object() { 42 // Test for method int 43 // java.text.CollationKey.compareTo(java.lang.Object) 44 Collator collator = Collator.getInstance(); 45 collator.setStrength(Collator.PRIMARY); 46 CollationKey key1 = collator.getCollationKey("abc"); 47 CollationKey key2 = collator.getCollationKey("ABC"); 48 assertEquals("Should be equal", 0, key1.compareTo(key2)); 49 } 50 51 /** 52 * @tests java.text.CollationKey#equals(java.lang.Object) 53 */ 54 public void test_equalsLjava_lang_Object() { 55 Collator collator = Collator.getInstance(); 56 collator.setStrength(Collator.PRIMARY); 57 CollationKey key1 = collator.getCollationKey("abc"); 58 CollationKey key2 = collator.getCollationKey("ABC"); 59 assertTrue("Should be equal", key1.equals(key2)); 60 } 61 62 /** 63 * @tests java.text.CollationKey#getSourceString() 64 */ 65 public void test_getSourceString() { 66 Collator collator = Collator.getInstance(); 67 collator.setStrength(Collator.PRIMARY); 68 assertTrue("Wrong source string1", collator.getCollationKey("abc") 69 .getSourceString() == "abc"); 70 assertTrue("Wrong source string2", collator.getCollationKey("ABC") 71 .getSourceString() == "ABC"); 72 } 73 74 /** 75 * @tests java.text.CollationKey#hashCode() 76 */ 77 public void test_hashCode() { 78 Collator collator = Collator.getInstance(); 79 collator.setStrength(Collator.PRIMARY); 80 CollationKey key1 = collator.getCollationKey("abc"); 81 CollationKey key2 = collator.getCollationKey("ABC"); 82 assertTrue("Should be equal", key1.hashCode() == key2.hashCode()); 83 } 84 85 /** 86 * @tests java.text.CollationKey#toByteArray() 87 */ 88 //FIXME This test fails on Harmony ClassLibrary 89 public void failing_test_toByteArray() { 90 // Test for method byte [] java.text.CollationKey.toByteArray() 91 Collator collator = Collator.getInstance(); 92 collator.setStrength(Collator.PRIMARY); 93 CollationKey key1 = collator.getCollationKey("abc"); 94 byte[] bytes = key1.toByteArray(); 95 assertTrue("Not enough bytes", bytes.length >= 3); 96 97 try { 98 collator = new RuleBasedCollator("= 1 , 2 ; 3 , 4 < 5 ; 6 , 7"); 99 } catch (ParseException e) { 100 fail("ParseException"); 101 return; 102 } 103 bytes = collator.getCollationKey("1234567").toByteArray(); 104 /* 105 * CollationElementIterator it = 106 * ((RuleBasedCollator)collator).getCollationElementIterator("1234567"); 107 * int order; while ((order = it.next()) != 108 * CollationElementIterator.NULLORDER) { 109 * System.out.println(Integer.toHexString(order)); } for (int i=0; i<bytes.length; 110 * i+=2) { System.out.print(Integer.toHexString(bytes[i]) + 111 * Integer.toHexString(bytes[i+1]) + " "); } System.out.println(); 112 */ 113 byte[] result = new byte[] { 0, 2, 0, 2, 0, 2, 0, 0, 0, 3, 0, 3, 0, 1, 114 0, 2, 0, 2, 0, 0, 0, 4, 0, 4, 0, 1, 0, 1, 0, 2 }; 115 assertTrue("Wrong bytes", Arrays.equals(bytes, result)); 116 } 117} 118