Search in sources :

Example 6 with CollationKey

use of java.text.CollationKey in project robovm by robovm.

the class OldCollationKeyTest method test_toByteArray.

public void test_toByteArray() {
    // Test for method byte [] java.text.CollationKey.toByteArray()
    Collator collator = Collator.getInstance();
    collator.setStrength(Collator.PRIMARY);
    CollationKey key1 = collator.getCollationKey("abc");
    byte[] bytes = key1.toByteArray();
    assertTrue("Not enough bytes", bytes.length >= 3);
    try {
        collator = new RuleBasedCollator("= 1 , 2 ; 3 , 4 < 5 ; 6 , 7");
    } catch (ParseException e) {
        fail("ParseException");
        return;
    }
    /*
         * CollationElementIterator it =
         * ((RuleBasedCollator)collator).getCollationElementIterator("1234567");
         * int order; while ((order = it.next()) !=
         * CollationElementIterator.NULLORDER) {
         * System.out.println(Integer.toHexString(order)); } for (int i=0; i<bytes.length;
         * i+=2) { System.out.print(Integer.toHexString(bytes[i]) +
         * Integer.toHexString(bytes[i+1]) + " "); } System.out.println();
         */
    // The RI has a different algorithm to generate the collation keys.
    // bytes = collator.getCollationKey("1234567").toByteArray();
    // byte[] result = new byte[] { 0, 2, 0, 2, 0, 2, 0, 0, 0, 3, 0, 3, 0, 1,
    //         0, 2, 0, 2, 0, 0, 0, 4, 0, 4, 0, 1, 0, 1, 0, 2 };
    byte[] bytes1 = collator.getCollationKey("12").toByteArray();
    byte[] bytes2 = collator.getCollationKey("123").toByteArray();
    byte[] bytes3 = collator.getCollationKey("124").toByteArray();
    byte[] bytes4 = collator.getCollationKey("1245").toByteArray();
    byte[] bytes5 = collator.getCollationKey("1245").toByteArray();
    assertTrue("returned collation key does not sort correctly", compareUnsignedByteArrays(bytes1, bytes2) < 0);
    assertTrue("returned collation key does not sort correctly", compareUnsignedByteArrays(bytes2, bytes3) < 0);
    assertTrue("returned collation key does not sort correctly", compareUnsignedByteArrays(bytes3, bytes4) < 0);
    assertTrue("returned collation key does not sort correctly", compareUnsignedByteArrays(bytes4, bytes5) == 0);
}
Also used : RuleBasedCollator(java.text.RuleBasedCollator) CollationKey(java.text.CollationKey) ParseException(java.text.ParseException) RuleBasedCollator(java.text.RuleBasedCollator) Collator(java.text.Collator)

Aggregations

CollationKey (java.text.CollationKey)6 Collator (java.text.Collator)2 DTM (org.apache.xml.dtm.DTM)2 XObject (org.apache.xpath.objects.XObject)2 ParseException (java.text.ParseException)1 RuleBasedCollator (java.text.RuleBasedCollator)1