Java Code Examples for java.text.Collator#setDecomposition()

The following examples show how to use java.text.Collator#setDecomposition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CollatorTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * @tests java.text.Collator#setDecomposition(int)
 */
//FIXME This test fails on Harmony ClassLibrary
public void failing_test_setDecompositionI() {
	Collator c = Collator.getInstance(Locale.FRENCH);
	c.setStrength(Collator.IDENTICAL);
	c.setDecomposition(Collator.NO_DECOMPOSITION);
	assertTrue("Collator should not be using decomposition", !c.equals(
			"\u212B", "\u00C5")); // "ANGSTROM SIGN" and "LATIN CAPITAL
	// LETTER A WITH RING ABOVE"
	c.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
	assertTrue("Collator should be using decomposition", c.equals("\u212B",
			"\u00C5")); // "ANGSTROM SIGN" and "LATIN CAPITAL LETTER A WITH
	// RING ABOVE"
	assertTrue("Should not be equal under canonical decomposition", !c
			.equals("\u2163", "IV")); // roman number "IV"
	c.setDecomposition(Collator.FULL_DECOMPOSITION);
	assertTrue("Should be equal under full decomposition", c.equals(
			"\u2163", "IV")); // roman number "IV"
}
 
Example 2
Source File: NativeString.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.9 String.prototype.localeCompare (that)
 * @param self self reference
 * @param that comparison object
 * @return result of locale sensitive comparison operation between {@code self} and {@code that}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static double localeCompare(final Object self, final Object that) {

    final String   str      = checkObjectToString(self);
    final Collator collator = Collator.getInstance(Global.getEnv()._locale);

    collator.setStrength(Collator.IDENTICAL);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);

    return collator.compare(str, JSType.toString(that));
}
 
Example 3
Source File: Realm.java    From es6draft with MIT License 5 votes vote down vote up
/**
 * Returns a {@link Collator} for this realm's locale
 * 
 * @deprecated No longer used
 * @return the locale specific collator
 */
@Deprecated
public Collator getCollator() {
    Collator collator = Collator.getInstance(getLocale());
    // Use Normalized Form D for comparison (cf. 21.1.3.10, Note 2)
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    // `"\u0001".localeCompare("\u0002") == -1` should yield true
    collator.setStrength(Collator.IDENTICAL);
    return collator;
}
 
Example 4
Source File: NativeString.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.9 String.prototype.localeCompare (that)
 * @param self self reference
 * @param that comparison object
 * @return result of locale sensitive comparison operation between {@code self} and {@code that}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object localeCompare(final Object self, final Object that) {

    final String   str      = checkObjectToString(self);
    final Collator collator = Collator.getInstance(Global.getEnv()._locale);

    collator.setStrength(Collator.IDENTICAL);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);

    return (double)collator.compare(str, JSType.toString(that));
}
 
Example 5
Source File: NativeString.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.9 String.prototype.localeCompare (that)
 * @param self self reference
 * @param that comparison object
 * @return result of locale sensitive comparison operation between {@code self} and {@code that}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static double localeCompare(final Object self, final Object that) {

    final String   str      = checkObjectToString(self);
    final Collator collator = Collator.getInstance(Global.getEnv()._locale);

    collator.setStrength(Collator.IDENTICAL);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);

    return collator.compare(str, JSType.toString(that));
}
 
Example 6
Source File: NativeString.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.9 String.prototype.localeCompare (that)
 * @param self self reference
 * @param that comparison object
 * @return result of locale sensitive comparison operation between {@code self} and {@code that}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object localeCompare(final Object self, final Object that) {

    final String   str      = checkObjectToString(self);
    final Collator collator = Collator.getInstance(Global.getEnv()._locale);

    collator.setStrength(Collator.IDENTICAL);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);

    return (double)collator.compare(str, JSType.toString(that));
}
 
Example 7
Source File: NativeString.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.9 String.prototype.localeCompare (that)
 * @param self self reference
 * @param that comparison object
 * @return result of locale sensitive comparison operation between {@code self} and {@code that}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object localeCompare(final Object self, final Object that) {

    final String   str      = checkObjectToString(self);
    final Collator collator = Collator.getInstance(Global.getEnv()._locale);

    collator.setStrength(Collator.IDENTICAL);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);

    return (double)collator.compare(str, JSType.toString(that));
}
 
Example 8
Source File: NativeString.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.9 String.prototype.localeCompare (that)
 * @param self self reference
 * @param that comparison object
 * @return result of locale sensitive comparison operation between {@code self} and {@code that}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static double localeCompare(final Object self, final Object that) {

    final String   str      = checkObjectToString(self);
    final Collator collator = Collator.getInstance(Global.getEnv()._locale);

    collator.setStrength(Collator.IDENTICAL);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);

    return collator.compare(str, JSType.toString(that));
}
 
Example 9
Source File: NativeString.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.9 String.prototype.localeCompare (that)
 * @param self self reference
 * @param that comparison object
 * @return result of locale sensitive comparison operation between {@code self} and {@code that}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static double localeCompare(final Object self, final Object that) {

    final String   str      = checkObjectToString(self);
    final Collator collator = Collator.getInstance(Global.getEnv()._locale);

    collator.setStrength(Collator.IDENTICAL);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);

    return collator.compare(str, JSType.toString(that));
}
 
Example 10
Source File: NativeString.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.9 String.prototype.localeCompare (that)
 * @param self self reference
 * @param that comparison object
 * @return result of locale sensitive comparison operation between {@code self} and {@code that}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static double localeCompare(final Object self, final Object that) {

    final String   str      = checkObjectToString(self);
    final Collator collator = Collator.getInstance(Global.getEnv()._locale);

    collator.setStrength(Collator.IDENTICAL);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);

    return collator.compare(str, JSType.toString(that));
}
 
Example 11
Source File: ForBenefitOfUtil.java    From snowblossom with Apache License 2.0 5 votes vote down vote up
/**
 * This doesn't output the string, but actually the collator byte stream
 * which should be used as the uniqueness key. See ForBenefitOfUtilTest for examples
 * of things that should or should not match each other.
 */
public static ByteString normalize(String input)
{
  String n1 = Normalizer.normalize(input, Normalizer.Form.NFC);

  // Not trying to be US-English centric, but have to pick some Locale
  // so that this section will behave consistently for all nodes
  Collator collator = Collator.getInstance(Locale.US);
  collator.setStrength(Collator.PRIMARY);
  collator.setDecomposition(Collator.FULL_DECOMPOSITION);
  
  return ByteString.copyFrom(collator.getCollationKey(n1).toByteArray());

}
 
Example 12
Source File: NativeString.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.9 String.prototype.localeCompare (that)
 * @param self self reference
 * @param that comparison object
 * @return result of locale sensitive comparison operation between {@code self} and {@code that}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static double localeCompare(final Object self, final Object that) {

    final String   str      = checkObjectToString(self);
    final Collator collator = Collator.getInstance(Global.getEnv()._locale);

    collator.setStrength(Collator.IDENTICAL);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);

    return collator.compare(str, JSType.toString(that));
}
 
Example 13
Source File: NativeString.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.9 String.prototype.localeCompare (that)
 * @param self self reference
 * @param that comparison object
 * @return result of locale sensitive comparison operation between {@code self} and {@code that}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static double localeCompare(final Object self, final Object that) {

    final String   str      = checkObjectToString(self);
    final Collator collator = Collator.getInstance(Global.getEnv()._locale);

    collator.setStrength(Collator.IDENTICAL);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);

    return collator.compare(str, JSType.toString(that));
}
 
Example 14
Source File: APITest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public final void TestProperty( )
{
    Collator col = null;
    try {
        col = Collator.getInstance(Locale.ROOT);
        logln("The property tests begin : ");
        logln("Test ctors : ");
        doAssert(col.compare("ab", "abc") < 0, "ab < abc comparison failed");
        doAssert(col.compare("ab", "AB") < 0, "ab < AB comparison failed");
        doAssert(col.compare("black-bird", "blackbird") > 0, "black-bird > blackbird comparison failed");
        doAssert(col.compare("black bird", "black-bird") < 0, "black bird < black-bird comparison failed");
        doAssert(col.compare("Hello", "hello") > 0, "Hello > hello comparison failed");

        logln("Test ctors ends.");
        logln("testing Collator.getStrength() method ...");
        doAssert(col.getStrength() == Collator.TERTIARY, "collation object has the wrong strength");
        doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");

        logln("testing Collator.setStrength() method ...");
        col.setStrength(Collator.SECONDARY);
        doAssert(col.getStrength() != Collator.TERTIARY, "collation object's strength is secondary difference");
        doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");
        doAssert(col.getStrength() == Collator.SECONDARY, "collation object has the wrong strength");

        logln("testing Collator.setDecomposition() method ...");
        col.setDecomposition(Collator.NO_DECOMPOSITION);
        doAssert(col.getDecomposition() != Collator.FULL_DECOMPOSITION, "collation object's strength is secondary difference");
        doAssert(col.getDecomposition() != Collator.CANONICAL_DECOMPOSITION, "collation object's strength is primary difference");
        doAssert(col.getDecomposition() == Collator.NO_DECOMPOSITION, "collation object has the wrong strength");
    } catch (Exception foo) {
        errln("Error : " + foo.getMessage());
        errln("Default Collator creation failed.");
    }
    logln("Default collation property test ended.");
    logln("Collator.getRules() testing ...");
    doAssert(((RuleBasedCollator)col).getRules().length() != 0, "getRules() result incorrect" );
    logln("getRules tests end.");
    try {
        col = Collator.getInstance(Locale.FRENCH);
        col.setStrength(Collator.PRIMARY);
        logln("testing Collator.getStrength() method again ...");
        doAssert(col.getStrength() != Collator.TERTIARY, "collation object has the wrong strength");
        doAssert(col.getStrength() == Collator.PRIMARY, "collation object's strength is not primary difference");

        logln("testing French Collator.setStrength() method ...");
        col.setStrength(Collator.TERTIARY);
        doAssert(col.getStrength() == Collator.TERTIARY, "collation object's strength is not tertiary difference");
        doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");
        doAssert(col.getStrength() != Collator.SECONDARY, "collation object's strength is secondary difference");

    } catch (Exception bar) {
        errln("Error :  " + bar.getMessage());
        errln("Creating French collation failed.");
    }

    logln("Create junk collation: ");
    Locale abcd = new Locale("ab", "CD", "");
    Collator junk = null;
    try {
        junk = Collator.getInstance(abcd);
    } catch (Exception err) {
        errln("Error : " + err.getMessage());
        errln("Junk collation creation failed, should at least return the collator for the base bundle.");
    }
    try {
        col = Collator.getInstance(Locale.ROOT);
        doAssert(col.equals(junk), "The base bundle's collation should be returned.");
    } catch (Exception exc) {
        errln("Error : " + exc.getMessage());
        errln("Default collation comparison, caching not working.");
    }

    logln("Collator property test ended.");
}
 
Example 15
Source File: APITest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public final void TestProperty( )
{
    Collator col = null;
    try {
        col = Collator.getInstance(Locale.ROOT);
        logln("The property tests begin : ");
        logln("Test ctors : ");
        doAssert(col.compare("ab", "abc") < 0, "ab < abc comparison failed");
        doAssert(col.compare("ab", "AB") < 0, "ab < AB comparison failed");
        doAssert(col.compare("black-bird", "blackbird") > 0, "black-bird > blackbird comparison failed");
        doAssert(col.compare("black bird", "black-bird") < 0, "black bird < black-bird comparison failed");
        doAssert(col.compare("Hello", "hello") > 0, "Hello > hello comparison failed");

        logln("Test ctors ends.");
        logln("testing Collator.getStrength() method ...");
        doAssert(col.getStrength() == Collator.TERTIARY, "collation object has the wrong strength");
        doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");

        logln("testing Collator.setStrength() method ...");
        col.setStrength(Collator.SECONDARY);
        doAssert(col.getStrength() != Collator.TERTIARY, "collation object's strength is secondary difference");
        doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");
        doAssert(col.getStrength() == Collator.SECONDARY, "collation object has the wrong strength");

        logln("testing Collator.setDecomposition() method ...");
        col.setDecomposition(Collator.NO_DECOMPOSITION);
        doAssert(col.getDecomposition() != Collator.FULL_DECOMPOSITION, "collation object's strength is secondary difference");
        doAssert(col.getDecomposition() != Collator.CANONICAL_DECOMPOSITION, "collation object's strength is primary difference");
        doAssert(col.getDecomposition() == Collator.NO_DECOMPOSITION, "collation object has the wrong strength");
    } catch (Exception foo) {
        errln("Error : " + foo.getMessage());
        errln("Default Collator creation failed.");
    }
    logln("Default collation property test ended.");
    logln("Collator.getRules() testing ...");
    doAssert(((RuleBasedCollator)col).getRules().length() != 0, "getRules() result incorrect" );
    logln("getRules tests end.");
    try {
        col = Collator.getInstance(Locale.FRENCH);
        col.setStrength(Collator.PRIMARY);
        logln("testing Collator.getStrength() method again ...");
        doAssert(col.getStrength() != Collator.TERTIARY, "collation object has the wrong strength");
        doAssert(col.getStrength() == Collator.PRIMARY, "collation object's strength is not primary difference");

        logln("testing French Collator.setStrength() method ...");
        col.setStrength(Collator.TERTIARY);
        doAssert(col.getStrength() == Collator.TERTIARY, "collation object's strength is not tertiary difference");
        doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");
        doAssert(col.getStrength() != Collator.SECONDARY, "collation object's strength is secondary difference");

    } catch (Exception bar) {
        errln("Error :  " + bar.getMessage());
        errln("Creating French collation failed.");
    }

    logln("Create junk collation: ");
    Locale abcd = new Locale("ab", "CD", "");
    Collator junk = null;
    try {
        junk = Collator.getInstance(abcd);
    } catch (Exception err) {
        errln("Error : " + err.getMessage());
        errln("Junk collation creation failed, should at least return the collator for the base bundle.");
    }
    try {
        col = Collator.getInstance(Locale.ROOT);
        doAssert(col.equals(junk), "The base bundle's collation should be returned.");
    } catch (Exception exc) {
        errln("Error : " + exc.getMessage());
        errln("Default collation comparison, caching not working.");
    }

    logln("Collator property test ended.");
}
 
Example 16
Source File: CollationField.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Setup the field according to the provided parameters
 */
private void setup(ResourceLoader loader, Map<String,String> args) {
  String custom = args.remove("custom");
  String language = args.remove("language");
  String country = args.remove("country");
  String variant = args.remove("variant");
  String strength = args.remove("strength");
  String decomposition = args.remove("decomposition");
  
  final Collator collator;

  if (custom == null && language == null)
    throw new SolrException(ErrorCode.SERVER_ERROR, "Either custom or language is required.");
  
  if (custom != null && 
      (language != null || country != null || variant != null))
    throw new SolrException(ErrorCode.SERVER_ERROR, "Cannot specify both language and custom. "
        + "To tailor rules for a built-in language, see the javadocs for RuleBasedCollator. "
        + "Then save the entire customized ruleset to a file, and use with the custom parameter");
  
  if (language != null) { 
    // create from a system collator, based on Locale.
    collator = createFromLocale(language, country, variant);
  } else { 
    // create from a custom ruleset
    collator = createFromRules(custom, loader);
  }
  
  // set the strength flag, otherwise it will be the default.
  if (strength != null) {
    if (strength.equalsIgnoreCase("primary"))
      collator.setStrength(Collator.PRIMARY);
    else if (strength.equalsIgnoreCase("secondary"))
      collator.setStrength(Collator.SECONDARY);
    else if (strength.equalsIgnoreCase("tertiary"))
      collator.setStrength(Collator.TERTIARY);
    else if (strength.equalsIgnoreCase("identical"))
      collator.setStrength(Collator.IDENTICAL);
    else
      throw new SolrException(ErrorCode.SERVER_ERROR, "Invalid strength: " + strength);
  }
  
  // set the decomposition flag, otherwise it will be the default.
  if (decomposition != null) {
    if (decomposition.equalsIgnoreCase("no"))
      collator.setDecomposition(Collator.NO_DECOMPOSITION);
    else if (decomposition.equalsIgnoreCase("canonical"))
      collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    else if (decomposition.equalsIgnoreCase("full"))
      collator.setDecomposition(Collator.FULL_DECOMPOSITION);
    else
      throw new SolrException(ErrorCode.SERVER_ERROR, "Invalid decomposition: " + decomposition);
  }
  analyzer = new CollationKeyAnalyzer(collator);
}
 
Example 17
Source File: APITest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public final void TestProperty( )
{
    Collator col = null;
    try {
        col = Collator.getInstance(Locale.ROOT);
        logln("The property tests begin : ");
        logln("Test ctors : ");
        doAssert(col.compare("ab", "abc") < 0, "ab < abc comparison failed");
        doAssert(col.compare("ab", "AB") < 0, "ab < AB comparison failed");
        doAssert(col.compare("black-bird", "blackbird") > 0, "black-bird > blackbird comparison failed");
        doAssert(col.compare("black bird", "black-bird") < 0, "black bird < black-bird comparison failed");
        doAssert(col.compare("Hello", "hello") > 0, "Hello > hello comparison failed");

        logln("Test ctors ends.");
        logln("testing Collator.getStrength() method ...");
        doAssert(col.getStrength() == Collator.TERTIARY, "collation object has the wrong strength");
        doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");

        logln("testing Collator.setStrength() method ...");
        col.setStrength(Collator.SECONDARY);
        doAssert(col.getStrength() != Collator.TERTIARY, "collation object's strength is secondary difference");
        doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");
        doAssert(col.getStrength() == Collator.SECONDARY, "collation object has the wrong strength");

        logln("testing Collator.setDecomposition() method ...");
        col.setDecomposition(Collator.NO_DECOMPOSITION);
        doAssert(col.getDecomposition() != Collator.FULL_DECOMPOSITION, "collation object's strength is secondary difference");
        doAssert(col.getDecomposition() != Collator.CANONICAL_DECOMPOSITION, "collation object's strength is primary difference");
        doAssert(col.getDecomposition() == Collator.NO_DECOMPOSITION, "collation object has the wrong strength");
    } catch (Exception foo) {
        errln("Error : " + foo.getMessage());
        errln("Default Collator creation failed.");
    }
    logln("Default collation property test ended.");
    logln("Collator.getRules() testing ...");
    doAssert(((RuleBasedCollator)col).getRules().length() != 0, "getRules() result incorrect" );
    logln("getRules tests end.");
    try {
        col = Collator.getInstance(Locale.FRENCH);
        col.setStrength(Collator.PRIMARY);
        logln("testing Collator.getStrength() method again ...");
        doAssert(col.getStrength() != Collator.TERTIARY, "collation object has the wrong strength");
        doAssert(col.getStrength() == Collator.PRIMARY, "collation object's strength is not primary difference");

        logln("testing French Collator.setStrength() method ...");
        col.setStrength(Collator.TERTIARY);
        doAssert(col.getStrength() == Collator.TERTIARY, "collation object's strength is not tertiary difference");
        doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");
        doAssert(col.getStrength() != Collator.SECONDARY, "collation object's strength is secondary difference");

    } catch (Exception bar) {
        errln("Error :  " + bar.getMessage());
        errln("Creating French collation failed.");
    }

    logln("Create junk collation: ");
    Locale abcd = new Locale("ab", "CD", "");
    Collator junk = null;
    try {
        junk = Collator.getInstance(abcd);
    } catch (Exception err) {
        errln("Error : " + err.getMessage());
        errln("Junk collation creation failed, should at least return the collator for the base bundle.");
    }
    try {
        col = Collator.getInstance(Locale.ROOT);
        doAssert(col.equals(junk), "The base bundle's collation should be returned.");
    } catch (Exception exc) {
        errln("Error : " + exc.getMessage());
        errln("Default collation comparison, caching not working.");
    }

    logln("Collator property test ended.");
}
 
Example 18
Source File: APITest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public final void TestProperty( )
{
    Collator col = null;
    try {
        col = Collator.getInstance(Locale.ROOT);
        logln("The property tests begin : ");
        logln("Test ctors : ");
        doAssert(col.compare("ab", "abc") < 0, "ab < abc comparison failed");
        doAssert(col.compare("ab", "AB") < 0, "ab < AB comparison failed");
        doAssert(col.compare("black-bird", "blackbird") > 0, "black-bird > blackbird comparison failed");
        doAssert(col.compare("black bird", "black-bird") < 0, "black bird < black-bird comparison failed");
        doAssert(col.compare("Hello", "hello") > 0, "Hello > hello comparison failed");

        logln("Test ctors ends.");
        logln("testing Collator.getStrength() method ...");
        doAssert(col.getStrength() == Collator.TERTIARY, "collation object has the wrong strength");
        doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");

        logln("testing Collator.setStrength() method ...");
        col.setStrength(Collator.SECONDARY);
        doAssert(col.getStrength() != Collator.TERTIARY, "collation object's strength is secondary difference");
        doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");
        doAssert(col.getStrength() == Collator.SECONDARY, "collation object has the wrong strength");

        logln("testing Collator.setDecomposition() method ...");
        col.setDecomposition(Collator.NO_DECOMPOSITION);
        doAssert(col.getDecomposition() != Collator.FULL_DECOMPOSITION, "collation object's strength is secondary difference");
        doAssert(col.getDecomposition() != Collator.CANONICAL_DECOMPOSITION, "collation object's strength is primary difference");
        doAssert(col.getDecomposition() == Collator.NO_DECOMPOSITION, "collation object has the wrong strength");
    } catch (Exception foo) {
        errln("Error : " + foo.getMessage());
        errln("Default Collator creation failed.");
    }
    logln("Default collation property test ended.");
    logln("Collator.getRules() testing ...");
    doAssert(((RuleBasedCollator)col).getRules().length() != 0, "getRules() result incorrect" );
    logln("getRules tests end.");
    try {
        col = Collator.getInstance(Locale.FRENCH);
        col.setStrength(Collator.PRIMARY);
        logln("testing Collator.getStrength() method again ...");
        doAssert(col.getStrength() != Collator.TERTIARY, "collation object has the wrong strength");
        doAssert(col.getStrength() == Collator.PRIMARY, "collation object's strength is not primary difference");

        logln("testing French Collator.setStrength() method ...");
        col.setStrength(Collator.TERTIARY);
        doAssert(col.getStrength() == Collator.TERTIARY, "collation object's strength is not tertiary difference");
        doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");
        doAssert(col.getStrength() != Collator.SECONDARY, "collation object's strength is secondary difference");

    } catch (Exception bar) {
        errln("Error :  " + bar.getMessage());
        errln("Creating French collation failed.");
    }

    logln("Create junk collation: ");
    Locale abcd = new Locale("ab", "CD", "");
    Collator junk = null;
    try {
        junk = Collator.getInstance(abcd);
    } catch (Exception err) {
        errln("Error : " + err.getMessage());
        errln("Junk collation creation failed, should at least return the collator for the base bundle.");
    }
    try {
        col = Collator.getInstance(Locale.ROOT);
        doAssert(col.equals(junk), "The base bundle's collation should be returned.");
    } catch (Exception exc) {
        errln("Error : " + exc.getMessage());
        errln("Default collation comparison, caching not working.");
    }

    logln("Collator property test ended.");
}
 
Example 19
Source File: APITest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public final void TestProperty( )
{
    Collator col = null;
    try {
        col = Collator.getInstance(Locale.ROOT);
        logln("The property tests begin : ");
        logln("Test ctors : ");
        doAssert(col.compare("ab", "abc") < 0, "ab < abc comparison failed");
        doAssert(col.compare("ab", "AB") < 0, "ab < AB comparison failed");
        doAssert(col.compare("black-bird", "blackbird") > 0, "black-bird > blackbird comparison failed");
        doAssert(col.compare("black bird", "black-bird") < 0, "black bird < black-bird comparison failed");
        doAssert(col.compare("Hello", "hello") > 0, "Hello > hello comparison failed");

        logln("Test ctors ends.");
        logln("testing Collator.getStrength() method ...");
        doAssert(col.getStrength() == Collator.TERTIARY, "collation object has the wrong strength");
        doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");

        logln("testing Collator.setStrength() method ...");
        col.setStrength(Collator.SECONDARY);
        doAssert(col.getStrength() != Collator.TERTIARY, "collation object's strength is secondary difference");
        doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");
        doAssert(col.getStrength() == Collator.SECONDARY, "collation object has the wrong strength");

        logln("testing Collator.setDecomposition() method ...");
        col.setDecomposition(Collator.NO_DECOMPOSITION);
        doAssert(col.getDecomposition() != Collator.FULL_DECOMPOSITION, "collation object's strength is secondary difference");
        doAssert(col.getDecomposition() != Collator.CANONICAL_DECOMPOSITION, "collation object's strength is primary difference");
        doAssert(col.getDecomposition() == Collator.NO_DECOMPOSITION, "collation object has the wrong strength");
    } catch (Exception foo) {
        errln("Error : " + foo.getMessage());
        errln("Default Collator creation failed.");
    }
    logln("Default collation property test ended.");
    logln("Collator.getRules() testing ...");
    doAssert(((RuleBasedCollator)col).getRules().length() != 0, "getRules() result incorrect" );
    logln("getRules tests end.");
    try {
        col = Collator.getInstance(Locale.FRENCH);
        col.setStrength(Collator.PRIMARY);
        logln("testing Collator.getStrength() method again ...");
        doAssert(col.getStrength() != Collator.TERTIARY, "collation object has the wrong strength");
        doAssert(col.getStrength() == Collator.PRIMARY, "collation object's strength is not primary difference");

        logln("testing French Collator.setStrength() method ...");
        col.setStrength(Collator.TERTIARY);
        doAssert(col.getStrength() == Collator.TERTIARY, "collation object's strength is not tertiary difference");
        doAssert(col.getStrength() != Collator.PRIMARY, "collation object's strength is primary difference");
        doAssert(col.getStrength() != Collator.SECONDARY, "collation object's strength is secondary difference");

    } catch (Exception bar) {
        errln("Error :  " + bar.getMessage());
        errln("Creating French collation failed.");
    }

    logln("Create junk collation: ");
    Locale abcd = new Locale("ab", "CD", "");
    Collator junk = null;
    try {
        junk = Collator.getInstance(abcd);
    } catch (Exception err) {
        errln("Error : " + err.getMessage());
        errln("Junk collation creation failed, should at least return the collator for the base bundle.");
    }
    try {
        col = Collator.getInstance(Locale.ROOT);
        doAssert(col.equals(junk), "The base bundle's collation should be returned.");
    } catch (Exception exc) {
        errln("Error : " + exc.getMessage());
        errln("Default collation comparison, caching not working.");
    }

    logln("Collator property test ended.");
}