Search in sources :

Example 1 with CollatorFactory

use of android.icu.text.Collator.CollatorFactory in project j2objc by google.

the class CollationAPITest method TestCreateCollator.

/*
     * Tests the class CollatorFactory
     */
@Test
public void TestCreateCollator() {
    // The following class override public Collator createCollator(Locale loc)
    class TestCreateCollator extends CollatorFactory {

        @Override
        public Set<String> getSupportedLocaleIDs() {
            return new HashSet<String>();
        }

        public TestCreateCollator() {
            super();
        }

        @Override
        public Collator createCollator(ULocale c) {
            return null;
        }
    }
    // The following class override public Collator createCollator(ULocale loc)
    class TestCreateCollator1 extends CollatorFactory {

        @Override
        public Set<String> getSupportedLocaleIDs() {
            return new HashSet<String>();
        }

        public TestCreateCollator1() {
            super();
        }

        @Override
        public Collator createCollator(Locale c) {
            return null;
        }

        @Override
        public boolean visible() {
            return false;
        }
    }
    /*
         * Tests the method public Collator createCollator(Locale loc) using TestCreateCollator1 class
         */
    try {
        TestCreateCollator tcc = new TestCreateCollator();
        tcc.createCollator(new Locale("en_US"));
    } catch (Exception e) {
        errln("Collator.createCollator(Locale) was not suppose to " + "return an exception.");
    }
    /*
         * Tests the method public Collator createCollator(ULocale loc) using TestCreateCollator1 class
         */
    try {
        TestCreateCollator1 tcc = new TestCreateCollator1();
        tcc.createCollator(new ULocale("en_US"));
    } catch (Exception e) {
        errln("Collator.createCollator(ULocale) was not suppose to " + "return an exception.");
    }
    /*
         * Tests the method public String getDisplayName(Locale objectLocale, Locale displayLocale) using TestCreateCollator1 class
         */
    try {
        TestCreateCollator tcc = new TestCreateCollator();
        tcc.getDisplayName(new Locale("en_US"), new Locale("jp_JP"));
    } catch (Exception e) {
        errln("Collator.getDisplayName(Locale,Locale) was not suppose to return an exception.");
    }
    /*
         * Tests the method public String getDisplayName(ULocale objectLocale, ULocale displayLocale) using TestCreateCollator1 class
         */
    try {
        TestCreateCollator1 tcc = new TestCreateCollator1();
        tcc.getDisplayName(new ULocale("en_US"), new ULocale("jp_JP"));
    } catch (Exception e) {
        errln("Collator.getDisplayName(ULocale,ULocale) was not suppose to return an exception.");
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) ULocale(android.icu.util.ULocale) CollatorFactory(android.icu.text.Collator.CollatorFactory) MissingResourceException(java.util.MissingResourceException) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with CollatorFactory

use of android.icu.text.Collator.CollatorFactory in project j2objc by google.

the class CollationServiceTest method TestRegisterFactory.

@Test
public void TestRegisterFactory() {
    class CollatorInfo {

        ULocale locale;

        Collator collator;

        // locale -> string
        Map displayNames;

        CollatorInfo(ULocale locale, Collator collator, Map displayNames) {
            this.locale = locale;
            this.collator = collator;
            this.displayNames = displayNames;
        }

        String getDisplayName(ULocale displayLocale) {
            String name = null;
            if (displayNames != null) {
                name = (String) displayNames.get(displayLocale);
            }
            if (name == null) {
                name = locale.getDisplayName(displayLocale);
            }
            return name;
        }
    }
    class TestFactory extends CollatorFactory {

        private Map map;

        private Set ids;

        TestFactory(CollatorInfo[] info) {
            map = new HashMap();
            for (int i = 0; i < info.length; ++i) {
                CollatorInfo ci = info[i];
                map.put(ci.locale, ci);
            }
        }

        public Collator createCollator(ULocale loc) {
            CollatorInfo ci = (CollatorInfo) map.get(loc);
            if (ci != null) {
                return ci.collator;
            }
            return null;
        }

        public String getDisplayName(ULocale objectLocale, ULocale displayLocale) {
            CollatorInfo ci = (CollatorInfo) map.get(objectLocale);
            if (ci != null) {
                return ci.getDisplayName(displayLocale);
            }
            return null;
        }

        public Set getSupportedLocaleIDs() {
            if (ids == null) {
                HashSet set = new HashSet();
                Iterator iter = map.keySet().iterator();
                while (iter.hasNext()) {
                    ULocale locale = (ULocale) iter.next();
                    String id = locale.toString();
                    set.add(id);
                }
                ids = Collections.unmodifiableSet(set);
            }
            return ids;
        }
    }
    class TestFactoryWrapper extends CollatorFactory {

        CollatorFactory delegate;

        TestFactoryWrapper(CollatorFactory delegate) {
            this.delegate = delegate;
        }

        public Collator createCollator(ULocale loc) {
            return delegate.createCollator(loc);
        }

        // use CollatorFactory getDisplayName(ULocale, ULocale) for coverage
        public Set getSupportedLocaleIDs() {
            return delegate.getSupportedLocaleIDs();
        }
    }
    ULocale fu_FU = new ULocale("fu_FU");
    ULocale fu_FU_FOO = new ULocale("fu_FU_FOO");
    Map fuFUNames = new HashMap();
    fuFUNames.put(fu_FU, "ze leetle bunny Fu-Fu");
    fuFUNames.put(fu_FU_FOO, "zee leetel bunny Foo-Foo");
    fuFUNames.put(ULocale.US, "little bunny Foo Foo");
    Collator frcol = Collator.getInstance(ULocale.FRANCE);
    /* Collator uscol = */
    Collator.getInstance(ULocale.US);
    Collator gecol = Collator.getInstance(ULocale.GERMANY);
    Collator jpcol = Collator.getInstance(ULocale.JAPAN);
    Collator fucol = Collator.getInstance(fu_FU);
    CollatorInfo[] info = { new CollatorInfo(ULocale.US, frcol, null), new CollatorInfo(ULocale.FRANCE, gecol, null), new CollatorInfo(fu_FU, jpcol, fuFUNames) };
    TestFactory factory = null;
    try {
        factory = new TestFactory(info);
    } catch (MissingResourceException ex) {
        warnln("Could not load locale data.");
    }
    // coverage
    {
        // in java, gc lets us easily multiply reference!
        TestFactoryWrapper wrapper = new TestFactoryWrapper(factory);
        Object key = Collator.registerFactory(wrapper);
        String name = null;
        try {
            name = Collator.getDisplayName(fu_FU, fu_FU_FOO);
        } catch (MissingResourceException ex) {
            warnln("Could not load locale data.");
        }
        logln("*** default name: " + name);
        Collator.unregister(key);
        ULocale bar_BAR = new ULocale("bar_BAR");
        Collator col = Collator.getInstance(bar_BAR);
        ULocale valid = col.getLocale(ULocale.VALID_LOCALE);
        String validName = valid.getName();
        if (validName.length() != 0 && !validName.equals("root")) {
            errln("Collation from bar_BAR is really \"" + validName + "\" but should be root");
        }
    }
    int n1 = checkAvailable("before registerFactory");
    {
        Object key = Collator.registerFactory(factory);
        int n2 = checkAvailable("after registerFactory");
        Collator ncol = Collator.getInstance(ULocale.US);
        if (!frcol.equals(ncol)) {
            errln("frcoll for en_US failed");
        }
        ncol = Collator.getInstance(fu_FU_FOO);
        if (!jpcol.equals(ncol)) {
            errln("jpcol for fu_FU_FOO failed, got: " + ncol);
        }
        ULocale[] locales = Collator.getAvailableULocales();
        boolean found = false;
        for (int i = 0; i < locales.length; ++i) {
            if (locales[i].equals(fu_FU)) {
                found = true;
                break;
            }
        }
        if (!found) {
            errln("new locale fu_FU not reported as supported locale");
        }
        String name = Collator.getDisplayName(fu_FU);
        if (!"little bunny Foo Foo".equals(name)) {
            errln("found " + name + " for fu_FU");
        }
        name = Collator.getDisplayName(fu_FU, fu_FU_FOO);
        if (!"zee leetel bunny Foo-Foo".equals(name)) {
            errln("found " + name + " for fu_FU in fu_FU_FOO");
        }
        if (!Collator.unregister(key)) {
            errln("failed to unregister factory");
        }
        int n3 = checkAvailable("after unregister");
        assertTrue("register increases count", n2 > n1);
        assertTrue("unregister restores count", n3 == n1);
        ncol = Collator.getInstance(fu_FU);
        if (!fucol.equals(ncol)) {
            errln("collator after unregister does not match original fu_FU");
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) MissingResourceException(java.util.MissingResourceException) Collator(android.icu.text.Collator) Iterator(java.util.Iterator) CollatorFactory(android.icu.text.Collator.CollatorFactory) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

CollatorFactory (android.icu.text.Collator.CollatorFactory)2 ULocale (android.icu.util.ULocale)2 HashSet (java.util.HashSet)2 MissingResourceException (java.util.MissingResourceException)2 Test (org.junit.Test)2 Collator (android.icu.text.Collator)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Set (java.util.Set)1