1561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes/* 2561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * Licensed to the Apache Software Foundation (ASF) under one or more 3561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * contributor license agreements. See the NOTICE file distributed with 4561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * this work for additional information regarding copyright ownership. 5561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * The ASF licenses this file to You under the Apache License, Version 2.0 6561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * (the "License"); you may not use this file except in compliance with 7561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * the License. You may obtain a copy of the License at 8561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * 9561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 10561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * 11561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * Unless required by applicable law or agreed to in writing, software 12561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 13561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * See the License for the specific language governing permissions and 15561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * limitations under the License. 16561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */ 17561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughespackage org.apache.harmony.text.tests.java.text; 18561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 19561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.text.CollationKey; 20561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.text.Collator; 21561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.text.ParseException; 22561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.text.RuleBasedCollator; 23561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.util.Arrays; 24561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 25561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughespublic class CollationKeyTest extends junit.framework.TestCase { 26561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 27561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes /** 28561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * @tests java.text.CollationKey#compareTo(java.text.CollationKey) 29561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */ 30561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes public void test_compareToLjava_text_CollationKey() { 31561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes Collator collator = Collator.getInstance(); 32561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes collator.setStrength(Collator.PRIMARY); 33561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes CollationKey key1 = collator.getCollationKey("abc"); 34561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes CollationKey key2 = collator.getCollationKey("ABC"); 35561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes assertEquals("Should be equal", 0, key1.compareTo(key2)); 36561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 37561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 38561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes /** 39561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * @tests java.text.CollationKey#compareTo(java.lang.Object) 40561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */ 41561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes public void test_compareToLjava_lang_Object() { 42561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // Test for method int 43561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // java.text.CollationKey.compareTo(java.lang.Object) 44561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes Collator collator = Collator.getInstance(); 45561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes collator.setStrength(Collator.PRIMARY); 46561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes CollationKey key1 = collator.getCollationKey("abc"); 47561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes CollationKey key2 = collator.getCollationKey("ABC"); 48561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes assertEquals("Should be equal", 0, key1.compareTo(key2)); 49561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 50561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 51561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes /** 52561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * @tests java.text.CollationKey#equals(java.lang.Object) 53561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */ 54561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes public void test_equalsLjava_lang_Object() { 55561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes Collator collator = Collator.getInstance(); 56561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes collator.setStrength(Collator.PRIMARY); 57561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes CollationKey key1 = collator.getCollationKey("abc"); 58561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes CollationKey key2 = collator.getCollationKey("ABC"); 59561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes assertTrue("Should be equal", key1.equals(key2)); 60561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 61561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 62561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes /** 63561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * @tests java.text.CollationKey#getSourceString() 64561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */ 65561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes public void test_getSourceString() { 66561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes Collator collator = Collator.getInstance(); 67561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes collator.setStrength(Collator.PRIMARY); 68561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes assertTrue("Wrong source string1", collator.getCollationKey("abc") 69561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes .getSourceString() == "abc"); 70561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes assertTrue("Wrong source string2", collator.getCollationKey("ABC") 71561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes .getSourceString() == "ABC"); 72561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 73561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 74561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes /** 75561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * @tests java.text.CollationKey#hashCode() 76561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */ 77561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes public void test_hashCode() { 78561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes Collator collator = Collator.getInstance(); 79561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes collator.setStrength(Collator.PRIMARY); 80561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes CollationKey key1 = collator.getCollationKey("abc"); 81561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes CollationKey key2 = collator.getCollationKey("ABC"); 82561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes assertTrue("Should be equal", key1.hashCode() == key2.hashCode()); 83561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 84561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 85561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes /** 86561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * @tests java.text.CollationKey#toByteArray() 87561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */ 88561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes //FIXME This test fails on Harmony ClassLibrary 89561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes public void failing_test_toByteArray() { 90561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes // Test for method byte [] java.text.CollationKey.toByteArray() 91561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes Collator collator = Collator.getInstance(); 92561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes collator.setStrength(Collator.PRIMARY); 93561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes CollationKey key1 = collator.getCollationKey("abc"); 94561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes byte[] bytes = key1.toByteArray(); 95561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes assertTrue("Not enough bytes", bytes.length >= 3); 96561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 97561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes try { 98561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes collator = new RuleBasedCollator("= 1 , 2 ; 3 , 4 < 5 ; 6 , 7"); 99561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } catch (ParseException e) { 100561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes fail("ParseException"); 101561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes return; 102561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 103561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes bytes = collator.getCollationKey("1234567").toByteArray(); 104561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes /* 105561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * CollationElementIterator it = 106561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * ((RuleBasedCollator)collator).getCollationElementIterator("1234567"); 107561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * int order; while ((order = it.next()) != 108561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * CollationElementIterator.NULLORDER) { 109561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * System.out.println(Integer.toHexString(order)); } for (int i=0; i<bytes.length; 110561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * i+=2) { System.out.print(Integer.toHexString(bytes[i]) + 111561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * Integer.toHexString(bytes[i+1]) + " "); } System.out.println(); 112561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */ 113561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes byte[] result = new byte[] { 0, 2, 0, 2, 0, 2, 0, 0, 0, 3, 0, 3, 0, 1, 114561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes 0, 2, 0, 2, 0, 0, 0, 4, 0, 4, 0, 1, 0, 1, 0, 2 }; 115561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes assertTrue("Wrong bytes", Arrays.equals(bytes, result)); 116561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes } 117561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes} 118