예제 #1
0
        public void TestLocale()
        {
            ICULocaleService service = new ICULocaleService("test locale");

            service.RegisterObject("root", UCultureInfo.InvariantCulture);
            service.RegisterObject("german", "de");
            service.RegisterObject("german_Germany", new UCultureInfo("de_DE"));
            service.RegisterObject("japanese", "ja");
            service.RegisterObject("japanese_Japan", new UCultureInfo("ja_JP"));

            object target = service.Get("de_US");

            confirmEqual("test de_US", "german", target);

            UCultureInfo de    = new UCultureInfo("de");
            UCultureInfo de_US = new UCultureInfo("de_US");

            target = service.Get(de_US);
            confirmEqual("test de_US 2", "german", target);

            target = service.Get(de_US, LocaleKey.KindAny);
            confirmEqual("test de_US 3", "german", target);

            target = service.Get(de_US, 1234);
            confirmEqual("test de_US 4", "german", target);

            target = service.Get(de_US, out UCultureInfo actualReturn); // ICU4N specific - refactored to out parameter
            confirmEqual("test de_US 5", "german", target);
            confirmEqual("test de_US 6", actualReturn, de);

            target = service.Get(de_US, LocaleKey.KindAny, out actualReturn); // ICU4N specific - refactored to out parameter
            confirmEqual("test de_US 7", actualReturn, de);

            target = service.Get(de_US, 1234, out actualReturn); // ICU4N specific - refactored to out parameter
            confirmEqual("test de_US 8", "german", target);
            confirmEqual("test de_US 9", actualReturn, de);

            service.RegisterObject("one/de_US", de_US, 1);
            service.RegisterObject("two/de_US", de_US, 2);

            target = service.Get(de_US, 1);
            confirmEqual("test de_US kind 1", "one/de_US", target);

            target = service.Get(de_US, 2);
            confirmEqual("test de_US kind 2", "two/de_US", target);

            target = service.Get(de_US);
            confirmEqual("test de_US kind 3", "german", target);

            LocaleKey lkey = LocaleKey.CreateWithCanonicalFallback("en", null, 1234);

            Logln("lkey prefix: " + lkey.Prefix);
            Logln("lkey descriptor: " + lkey.GetCurrentDescriptor());
            Logln("lkey current locale: " + lkey.GetCurrentCulture());

            lkey.Fallback();
            Logln("lkey descriptor 2: " + lkey.GetCurrentDescriptor());

            lkey.Fallback();
            Logln("lkey descriptor 3: " + lkey.GetCurrentDescriptor());

            target = service.Get("za_PPP");
            confirmEqual("test zappp", "root", target);

            ICollection <string> ids;

            using (new ThreadCultureChange("ja", "ja"))
            {
                target = service.Get("za_PPP");
                confirmEqual("test with ja locale", "japanese", target);

                ids = service.GetVisibleIDs();
                for (var iter = ids.GetEnumerator(); iter.MoveNext();)
                {
                    Logln("id: " + iter.Current);
                }
            }
            ids = service.GetVisibleIDs();
            for (var iter = ids.GetEnumerator(); iter.MoveNext();)
            {
                Logln("id: " + iter.Current);
            }

            target = service.Get("za_PPP");
            confirmEqual("test with en locale", "root", target);

            UCultureInfo[] locales = service.GetUCultures(UCultureTypes.AllCultures);
            confirmIdentical("test available locales", locales.Length, 6);
            Logln("locales: ");
            for (int i = 0; i < locales.Length; ++i)
            {
                Log("\n  [" + i + "] " + locales[i]);
            }
            Logln(" ");

            service.RegisterFactory(new ICUResourceBundleFactory());
            target = service.Get(new UCultureInfo("ja_JP"));

            {
                int n         = 0;
                var factories = service.Factories();
                var iter      = factories.GetEnumerator();
                while (iter.MoveNext())
                {
                    Logln("[" + n++ + "] " + iter.Current);
                }
            }

            // list only the english display names for es, in reverse order
            // since we're using locale keys, we should get all and only the es locales
            // hmmm, the default toString function doesn't print in sorted order for TreeMap
            {
                var map = service.GetDisplayNames(new UCultureInfo("en_US"),
                                                  new AnonymousComparer <object>(compare: (lhs, rhs) =>
                {
                    return(-StringComparer.OrdinalIgnoreCase.Compare((string)lhs, (string)rhs));
                }),
                                                  //                    new Comparator() {
                                                  //                            @Override
                                                  //                            public int compare(object lhs, object rhs)
                                                  //    {
                                                  //        return -string.CASE_INSENSITIVE_ORDER.compare((string)lhs, (string)rhs);
                                                  //    }
                                                  //},
                                                  "es");

                Logln("es display names in reverse order " + map);
            }
        }
예제 #2
0
        public void TestLocale()
        {
            ICULocaleService service = new ICULocaleService("test locale");

            service.RegisterObject("root", IBM.ICU.Util.ULocale.ROOT);
            service.RegisterObject("german", "de");
            service.RegisterObject("german_Germany", IBM.ICU.Util.ULocale.GERMANY);
            service.RegisterObject("japanese", "ja");
            service.RegisterObject("japanese_Japan", IBM.ICU.Util.ULocale.JAPAN);

            Object target = service.Get("de_US");

            ConfirmEqual("test de_US", "german", target);

            ULocale de    = new ULocale("de");
            ULocale de_US = new ULocale("de_US");

            target = service.Get(de_US);
            ConfirmEqual("test de_US 2", "german", target);

            target = service.Get(de_US, IBM.ICU.Impl.ICULocaleService.LocaleKey.KIND_ANY);
            ConfirmEqual("test de_US 3", "german", target);

            target = service.Get(de_US, 1234);
            ConfirmEqual("test de_US 4", "german", target);

            ULocale[] actualReturn = new ULocale[1];
            target = service.Get(de_US, actualReturn);
            ConfirmEqual("test de_US 5", "german", target);
            ConfirmEqual("test de_US 6", actualReturn[0], de);

            actualReturn[0] = null;
            target          = service.Get(de_US, IBM.ICU.Impl.ICULocaleService.LocaleKey.KIND_ANY, actualReturn);
            ConfirmEqual("test de_US 7", actualReturn[0], de);

            actualReturn[0] = null;
            target          = service.Get(de_US, 1234, actualReturn);
            ConfirmEqual("test de_US 8", "german", target);
            ConfirmEqual("test de_US 9", actualReturn[0], de);

            service.RegisterObject("one/de_US", de_US, 1);
            service.RegisterObject("two/de_US", de_US, 2);

            target = service.Get(de_US, 1);
            ConfirmEqual("test de_US kind 1", "one/de_US", target);

            target = service.Get(de_US, 2);
            ConfirmEqual("test de_US kind 2", "two/de_US", target);

            target = service.Get(de_US);
            ConfirmEqual("test de_US kind 3", "german", target);

            IBM.ICU.Impl.ICULocaleService.LocaleKey lkey = IBM.ICU.Impl.ICULocaleService.LocaleKey
                                                           .CreateWithCanonicalFallback("en", null, 1234);
            Logln("lkey prefix: " + lkey.Prefix());
            Logln("lkey descriptor: " + lkey.CurrentDescriptor());
            Logln("lkey current locale: " + lkey.CurrentLocale());

            lkey.Fallback();
            Logln("lkey descriptor 2: " + lkey.CurrentDescriptor());

            lkey.Fallback();
            Logln("lkey descriptor 3: " + lkey.CurrentDescriptor());

            target = service.Get("za_PPP");
            ConfirmEqual("test zappp", "root", target);

            ULocale loc = IBM.ICU.Util.ULocale.GetDefault();

            IBM.ICU.Util.ULocale.SetDefault(IBM.ICU.Util.ULocale.JAPANESE);
            target = service.Get("za_PPP");
            ConfirmEqual("test with ja locale", "japanese", target);

            ILOG.J2CsMapping.Collections.ISet ids_0 = service.GetVisibleIDs();
            for (IIterator iter = new ILOG.J2CsMapping.Collections.IteratorAdapter(ids_0.GetEnumerator()); iter.HasNext();)
            {
                Logln("id: " + iter.Next());
            }

            IBM.ICU.Util.ULocale.SetDefault(loc);
            ids_0 = service.GetVisibleIDs();
            for (IIterator iter_1 = new ILOG.J2CsMapping.Collections.IteratorAdapter(ids_0.GetEnumerator()); iter_1.HasNext();)
            {
                Logln("id: " + iter_1.Next());
            }

            target = service.Get("za_PPP");
            ConfirmEqual("test with en locale", "root", target);

            ULocale[] locales = service.GetAvailableULocales();
            ConfirmIdentical("test available locales", locales.Length, 6);
            Logln("locales: ");
            for (int i = 0; i < locales.Length; ++i)
            {
                Log("\n  [" + i + "] " + locales[i]);
            }
            Logln(" ");

            service.RegisterFactory(new IBM.ICU.Impl.ICULocaleService.ICUResourceBundleFactory());
            target = service.Get(IBM.ICU.Util.ULocale.JAPAN);

            {
                int       n_2       = 0;
                IList     factories = service.Factories();
                IIterator iter_3    = new ILOG.J2CsMapping.Collections.IteratorAdapter(factories.GetEnumerator());
                while (iter_3.HasNext())
                {
                    Logln("[" + n_2++ + "] " + iter_3.Next());
                }
            }

            // list only the english display names for es, in reverse order
            // since we're using locale keys, we should get all and only the es
            // locales
            // hmmm, the default toString function doesn't print in sorted order for
            // TreeMap
            {
                SortedList map = service.GetDisplayNames(IBM.ICU.Util.ULocale.US,
                                                         new ICUServiceTest.Anonymous_C0(), "es");

                Logln("es display names in reverse order " + map);
            }
        }
예제 #3
0
        public void TestAPI()
        {
            // create a service using locale keys,
            ICUService service = new TestService();

            Logln("service name:" + service.Name);

            // register an object with one locale,
            // search for an object with a more specific locale
            // should return the original object
            Integer singleton0 = new Integer(0);

            service.RegisterObject(singleton0, "en_US");
            object result = service.Get("en_US_FOO");

            confirmIdentical("1) en_US_FOO -> en_US", result, singleton0);

            // register a new object with the more specific locale
            // search for an object with that locale
            // should return the new object
            Integer singleton1 = new Integer(1);

            service.RegisterObject(singleton1, "en_US_FOO");
            result = service.Get("en_US_FOO");
            confirmIdentical("2) en_US_FOO -> en_US_FOO", result, singleton1);

            // search for an object that falls back to the first registered locale
            result = service.Get("en_US_BAR");
            confirmIdentical("3) en_US_BAR -> en_US", result, singleton0);

            // get a list of the factories, should be two
            IList <IServiceFactory> factories = service.Factories();

            confirmIdentical("4) factory size", factories.Count, 2);

            // register a new object with yet another locale
            // original factory list is unchanged
            Integer singleton2 = new Integer(2);

            service.RegisterObject(singleton2, "en");
            confirmIdentical("5) factory size", factories.Count, 2);

            // search for an object with the new locale
            // stack of factories is now en, en_US_FOO, en_US
            // search for en_US should still find en_US object
            result = service.Get("en_US_BAR");
            confirmIdentical("6) en_US_BAR -> en_US", result, singleton0);

            // register a new object with an old id, should hide earlier factory using this id, but leave it there
            Integer singleton3 = new Integer(3);

            service.RegisterObject(singleton3, "en_US");
            factories = service.Factories();
            confirmIdentical("9) factory size", factories.Count, 4);

            // should get data from that new factory
            result = service.Get("en_US_BAR");
            confirmIdentical("10) en_US_BAR -> (3)", result, singleton3);

            // remove new factory
            // should have fewer factories again
            service.UnregisterFactory((IServiceFactory)factories[0]);
            factories = service.Factories();
            confirmIdentical("11) factory size", factories.Count, 3);

            // should get original data again after remove factory
            result = service.Get("en_US_BAR");
            confirmIdentical("12) en_US_BAR -> 0", result, singleton0);

            // shouldn't find unregistered ids
            result = service.Get("foo");
            confirmIdentical("13) foo -> null", result, null);

            // should find non-canonical strings
            result = service.Get("EN_us_fOo", out string resultID);
            confirmEqual("14) find non-canonical", resultID, "en_US_FOO");

            // should be able to register non-canonical strings and get them canonicalized
            service.RegisterObject(singleton3, "eN_ca_dUde");
            result = service.Get("En_Ca_DuDe", out resultID);
            confirmEqual("15) register non-canonical", resultID, "en_CA_DUDE");

            // should be able to register invisible factories, these will not
            // be visible by default, but if you know the secret password you
            // can still access these services...
            Integer singleton4 = new Integer(4);

            service.RegisterObject(singleton4, "en_US_BAR", false);
            result = service.Get("en_US_BAR");
            confirmIdentical("17) get invisible", result, singleton4);

            // should not be able to locate invisible services
            var ids = service.GetVisibleIDs();

            confirmBoolean("18) find invisible", !ids.Contains("en_US_BAR"));

            service.Reset();

            // an anonymous factory than handles all ids
            //{
            //    Factory factory = new Factory() {
            //    @Override
            //    public object create(Key key, ICUService unusedService)
            //    {
            //        return new UCultureInfo(key.currentID());
            //    }

            //    @Override
            //        public void updateVisibleIDs(Map unusedResult)
            //    {
            //    }

            //    @Override
            //        public string getDisplayName(string id, UCultureInfo l)
            //    {
            //        return null;
            //    }
            //};

            {
                // an anonymous factory than handles all ids
                IServiceFactory factory = new AnonymousFactory(null, (Func <string, UCultureInfo, string>)null, null);
                service.RegisterFactory(factory);

                // anonymous factory will still handle the id
                result = service.Get(new UCultureInfo("en_US").ToString());
                confirmEqual("21) locale", result, new UCultureInfo("en_US"));

                // still normalizes id
                result = service.Get("EN_US_BAR");
                confirmEqual("22) locale", result, new UCultureInfo("en_US_BAR"));

                // we can override for particular ids
                service.RegisterObject(singleton3, "en_US_BAR");
                result = service.Get("en_US_BAR");
                confirmIdentical("23) override super", result, singleton3);
            }

            // empty service should not recognize anything
            service.Reset();
            result = service.Get("en_US");
            confirmIdentical("24) empty", result, null);

            // create a custom multiple key factory
            {
                string[] xids = { "en_US_VALLEY_GIRL",
                                  "en_US_VALLEY_BOY",
                                  "en_US_SURFER_GAL",
                                  "en_US_SURFER_DUDE" };
                service.RegisterFactory(new TestLocaleKeyFactory(xids, "Later"));
            }

            // iterate over the visual ids returned by the multiple factory
            {
                ICollection <string> vids = service.GetVisibleIDs();
                var iter  = vids.GetEnumerator();
                int count = 0;
                while (iter.MoveNext())
                {
                    ++count;
                    string id = (string)iter.Current;
                    Logln("  " + id + " --> " + service.Get(id));
                }
                // four visible ids
                confirmIdentical("25) visible ids", count, 4);
            }

            // iterate over the display names
            {
                var dids  = GetDisplayNames(service, new UCultureInfo("de"));
                var iter  = dids.GetEnumerator();
                int count = 0;
                while (iter.MoveNext())
                {
                    ++count;
                    var e = iter.Current;
                    Logln("  " + e.Key + " -- > " + e.Value);
                }
                // four display names, in german
                confirmIdentical("26) display names", count, 4);
            }

            // no valid display name
            confirmIdentical("27) get display name", service.GetDisplayName("en_US_VALLEY_GEEK"), null);

            {
                string name = service.GetDisplayName("en_US_SURFER_DUDE", new UCultureInfo("en_US"));
                confirmEqual("28) get display name", name, "English (United States, SURFER_DUDE)");
            }

            // register another multiple factory
            {
                string[] xids =
                {
                    "en_US_SURFER", "en_US_SURFER_GAL", "en_US_SILICON", "en_US_SILICON_GEEK"
                };
                service.RegisterFactory(new TestLocaleKeyFactory(xids, "Rad dude"));
            }

            // this time, we have seven display names
            // Rad dude's surfer gal 'replaces' later's surfer gal
            {
                var dids  = GetDisplayNames(service);
                var iter  = dids.GetEnumerator();
                int count = 0;
                while (iter.MoveNext())
                {
                    ++count;
                    var e = iter.Current;
                    Logln("  " + e.Key + " --> " + e.Value);
                }
                // seven display names, in spanish
                confirmIdentical("29) display names", count, 7);
            }

            // we should get the display name corresponding to the actual id
            // returned by the id we used.
            {
                string id  = "en_us_surfer_gal";
                string gal = (string)service.Get(id, out string actualID);
                if (gal != null)
                {
                    Logln("actual id: " + actualID);
                    string displayName = service.GetDisplayName(actualID, new UCultureInfo("en_US"));
                    Logln("found actual: " + gal + " with display name: " + displayName);
                    confirmBoolean("30) found display name for actual", displayName != null);

                    displayName = service.GetDisplayName(id, new UCultureInfo("en_US"));
                    Logln("found query: " + gal + " with display name: " + displayName);
                    // this is no longer a bug, we want to return display names for anything
                    // that a factory handles.  since we handle it, we should return a display
                    // name.  see jb3549
                    // confirmBoolean("31) found display name for query", displayName == null);
                }
                else
                {
                    Errln("30) service could not find entry for " + id);
                }

                // this should be handled by the 'dude' factory, since it overrides en_US_SURFER.
                id = "en_US_SURFER_BOZO";
                string bozo = (string)service.Get(id, out actualID);
                if (bozo != null)
                {
                    string displayName = service.GetDisplayName(actualID, new UCultureInfo("en_US"));
                    Logln("found actual: " + bozo + " with display name: " + displayName);
                    confirmBoolean("32) found display name for actual", displayName != null);

                    displayName = service.GetDisplayName(id, new UCultureInfo("en_US"));
                    Logln("found actual: " + bozo + " with display name: " + displayName);
                    // see above and jb3549
                    // confirmBoolean("33) found display name for query", displayName == null);
                }
                else
                {
                    Errln("32) service could not find entry for " + id);
                }

                confirmBoolean("34) is default ", !service.IsDefault);
            }

            /*
             * // disallow hiding for now
             *
             * // hiding factory should obscure 'sublocales'
             * {
             * string[] xids = {
             * "en_US_VALLEY", "en_US_SILICON"
             * };
             * service.registerFactory(new TestHidingFactory(xids, "hiding"));
             * }
             *
             * {
             * Map dids = service.getDisplayNames();
             * Iterator iter = dids.entrySet().iterator();
             * int count = 0;
             * while (iter.hasNext()) {
             ++count;
             * Entry e = (Entry)iter.next();
             * Logln("  " + e.getKey() + " -- > " + e.getValue());
             * }
             * confirmIdentical("35) hiding factory", count, 5);
             * }
             */

            {
                var xids = service.GetVisibleIDs();
                var iter = xids.GetEnumerator();
                while (iter.MoveNext())
                {
                    string xid = (string)iter.Current;
                    Logln(xid + "?  " + service.Get(xid));
                }

                Logln("valleygirl?  " + service.Get("en_US_VALLEY_GIRL"));
                Logln("valleyboy?   " + service.Get("en_US_VALLEY_BOY"));
                Logln("valleydude?  " + service.Get("en_US_VALLEY_DUDE"));
                Logln("surfergirl?  " + service.Get("en_US_SURFER_GIRL"));
            }

            // resource bundle factory.
            service.Reset();
            service.RegisterFactory(new ICUResourceBundleFactory());

            // list all of the resources
            {
                Logln("all visible ids: " + service.GetVisibleIDs());

                /*
                 * Set xids = service.GetVisibleIDs();
                 * StringBuffer buf = new StringBuffer("{");
                 * bool notfirst = false;
                 * Iterator iter = xids.iterator();
                 * while (iter.hasNext()) {
                 * string xid = (string)iter.next();
                 * if (notfirst) {
                 * buf.append(", ");
                 * } else {
                 * notfirst = true;
                 * }
                 * buf.append(xid);
                 * }
                 * buf.append("}");
                 * Logln(buf.ToString());
                 */
            }

            // list only the resources for es, default locale
            // since we're using the default Key, only "es" is matched
            {
                Logln("visible ids for es locale: " + service.GetVisibleIDs("es"));
            }

            // list only the spanish display names for es, spanish collation order
            // since we're using the default Key, only "es" is matched
            {
                Logln("display names: " + GetDisplayNames(service, new UCultureInfo("es"), "es"));
            }

            // list the display names in reverse order
            {
                Logln("display names in reverse order: " +
                      service.GetDisplayNames(new UCultureInfo("en_US"), new AnonymousComparer <object>(compare: (lhs, rhs) =>
                {
                    return(-StringComparer.OrdinalIgnoreCase.Compare((string)lhs, (string)rhs));
                })));
            }

            // get all the display names of these resources
            // this should be fast since the display names were cached.
            {
                Logln("service display names for de_DE");
                var          names = GetDisplayNames(service, new UCultureInfo("de_DE"));
                StringBuffer buf   = new StringBuffer("{");
                var          iter  = names.GetEnumerator();
                while (iter.MoveNext())
                {
                    var    e    = iter.Current;
                    string name = (string)e.Key;
                    string id   = (string)e.Value;
                    buf.Append("\n   " + name + " --> " + id);
                }
                buf.Append("\n}");
                Logln(buf.ToString());
            }

            CalifornioLanguageFactory califactory = new CalifornioLanguageFactory();

            service.RegisterFactory(califactory);
            // get all the display names of these resources
            {
                Logln("californio language factory");
                StringBuffer buf     = new StringBuffer("{");
                string[]     idNames =
                {
                    CalifornioLanguageFactory.californio,
                    CalifornioLanguageFactory.valley,
                    CalifornioLanguageFactory.surfer,
                    CalifornioLanguageFactory.geek
                };
                for (int i = 0; i < idNames.Length; ++i)
                {
                    string idName = idNames[i];
                    buf.Append("\n  --- " + idName + " ---");
                    var names = GetDisplayNames(service, new UCultureInfo(idName));
                    var iter  = names.GetEnumerator();
                    while (iter.MoveNext())
                    {
                        var    e    = iter.Current;
                        string name = (string)e.Key;
                        string id   = (string)e.Value;
                        buf.Append("\n    " + name + " --> " + id);
                    }
                }
                buf.Append("\n}");
                Logln(buf.ToString());
            }

            // test notification
            // simple registration
            {
                Logln("simple registration notification");
                ICULocaleService ls = new ICULocaleService();
                IServiceListener l1 = new AnonymousServiceListener(serviceChanged: (s, me) =>
                {
                    Logln("listener 1 report " + me.n++ + " service changed: " + s);
                });
                //ServiceListener l1 = new ServiceListener()
                //{
                //            private int n;

                //            public void serviceChanged(ICUService s)
                //{
                //    Logln("listener 1 report " + n++ + " service changed: " + s);
                //}
                //        };
                ls.AddListener(l1);
                IServiceListener l2 = new AnonymousServiceListener(serviceChanged: (s, me) =>
                {
                    Logln("listener 2 report " + me.n++ + " service changed: " + s);
                });
                //        ServiceListener l2 = new ServiceListener()
                //        {
                //            private int n;

                //            public void serviceChanged(ICUService s)
                //{
                //    Logln("listener 2 report " + n++ + " service changed: " + s);
                //}
                //        };
                ls.AddListener(l2);
                Logln("registering foo... ");
                ls.RegisterObject("Foo", "en_FOO");
                Logln("registering bar... ");
                ls.RegisterObject("Bar", "en_BAR");
                Logln("getting foo...");
                Logln((string)ls.Get("en_FOO"));
                Logln("removing listener 2...");
                ls.RemoveListener(l2);
                Logln("registering baz...");
                ls.RegisterObject("Baz", "en_BAZ");
                Logln("removing listener 1");
                ls.RemoveListener(l1);
                Logln("registering burp...");
                ls.RegisterObject("Burp", "en_BURP");

                // should only get one notification even if register multiple times
                Logln("... trying multiple registration");
                ls.AddListener(l1);
                ls.AddListener(l1);
                ls.AddListener(l1);
                ls.AddListener(l2);
                ls.RegisterObject("Foo", "en_FOO");
                Logln("... registered foo");

                // since in a separate thread, we can callback and not deadlock
                IServiceListener l3 = new AnonymousServiceListener(serviceChanged: (s, me) =>
                {
                    Logln("listener 3 report " + me.n++ + " service changed...");
                    if (s.Get("en_BOINK") == null)
                    { // don't recurse on ourselves!!!
                        Logln("registering boink...");
                        s.RegisterObject("boink", "en_BOINK");
                    }
                });


                //ServiceListener l3 = new ServiceListener()
                //{
                //            private int n;
                //@Override
                //            public void serviceChanged(ICUService s)
                //{
                //    Logln("listener 3 report " + n++ + " service changed...");
                //    if (s.Get("en_BOINK") == null)
                //    { // don't recurse on ourselves!!!
                //        Logln("registering boink...");
                //        s.RegisterObject("boink", "en_BOINK");
                //    }
                //}
                //        };
                ls.AddListener(l3);
                Logln("registering boo...");
                ls.RegisterObject("Boo", "en_BOO");
                Logln("...done");

#if FEATURE_THREADINTERRUPT
                try
                {
#endif
                Thread.Sleep(100);
#if FEATURE_THREADINTERRUPT
            }
            catch (ThreadInterruptedException e)
            {
            }
#endif
            }
        }
예제 #4
0
        public void TestAPI()
        {
            // create a service using locale keys,
            ICUService service = new ICUServiceTest.TestService();

            Logln("service name:" + service.GetName());

            // register an object with one locale,
            // search for an object with a more specific locale
            // should return the original object
            Int32 singleton0 = ((int)(0));

            service.RegisterObject(singleton0, "en_US");
            Object result = service.Get("en_US_FOO");

            ConfirmIdentical("1) en_US_FOO -> en_US", result, singleton0);

            // register a new object with the more specific locale
            // search for an object with that locale
            // should return the new object
            Int32 singleton1 = ((int)(1));

            service.RegisterObject(singleton1, "en_US_FOO");
            result = service.Get("en_US_FOO");
            ConfirmIdentical("2) en_US_FOO -> en_US_FOO", result, singleton1);

            // search for an object that falls back to the first registered locale
            result = service.Get("en_US_BAR");
            ConfirmIdentical("3) en_US_BAR -> en_US", result, singleton0);

            // get a list of the factories, should be two
            IList factories = service.Factories();

            ConfirmIdentical("4) factory size", factories.Count, 2);

            // register a new object with yet another locale
            // original factory list is unchanged
            Int32 singleton2 = ((int)(2));

            service.RegisterObject(singleton2, "en");
            ConfirmIdentical("5) factory size", factories.Count, 2);

            // search for an object with the new locale
            // stack of factories is now en, en_US_FOO, en_US
            // search for en_US should still find en_US object
            result = service.Get("en_US_BAR");
            ConfirmIdentical("6) en_US_BAR -> en_US", result, singleton0);

            // register a new object with an old id, should hide earlier factory
            // using this id, but leave it there
            Int32 singleton3 = ((int)(3));

            service.RegisterObject(singleton3, "en_US");
            factories = service.Factories();
            ConfirmIdentical("9) factory size", factories.Count, 4);

            // should get data from that new factory
            result = service.Get("en_US_BAR");
            ConfirmIdentical("10) en_US_BAR -> (3)", result, singleton3);

            // remove new factory
            // should have fewer factories again
            service.UnregisterFactory((IBM.ICU.Impl.ICUService.Factory)factories[0]);
            factories = service.Factories();
            ConfirmIdentical("11) factory size", factories.Count, 3);

            // should get original data again after remove factory
            result = service.Get("en_US_BAR");
            ConfirmIdentical("12) en_US_BAR -> 0", result, singleton0);

            // shouldn't find unregistered ids
            result = service.Get("foo");
            ConfirmIdentical("13) foo -> null", result, null);

            // should find non-canonical strings
            String[] resultID = new String[1];
            result = service.Get("EN_us_fOo", resultID);
            ConfirmEqual("14) find non-canonical", resultID[0], "en_US_FOO");

            // should be able to register non-canonical strings and get them
            // canonicalized
            service.RegisterObject(singleton3, "eN_ca_dUde");
            result = service.Get("En_Ca_DuDe", resultID);
            ConfirmEqual("15) register non-canonical", resultID[0], "en_CA_DUDE");

            // should be able to register invisible factories, these will not
            // be visible by default, but if you know the secret password you
            // can still access these services...
            Int32 singleton4 = ((int)(4));

            service.RegisterObject(singleton4, "en_US_BAR", false);
            result = service.Get("en_US_BAR");
            ConfirmIdentical("17) get invisible", result, singleton4);

            // should not be able to locate invisible services
            ILOG.J2CsMapping.Collections.ISet ids = service.GetVisibleIDs();
            ConfirmBoolean("18) find invisible", !ILOG.J2CsMapping.Collections.Collections.Contains("en_US_BAR", ids));

            service.Reset();
            // an anonymous factory than handles all ids
            {
                IBM.ICU.Impl.ICUService.Factory factory = new ICUServiceTest.Anonymous_C5();
                service.RegisterFactory(factory);

                // anonymous factory will still handle the id
                result = service.Get(IBM.ICU.Util.ULocale.US.ToString());
                ConfirmEqual("21) locale", result, IBM.ICU.Util.ULocale.US);

                // still normalizes id
                result = service.Get("EN_US_BAR");
                ConfirmEqual("22) locale", result, new ULocale("en_US_BAR"));

                // we can override for particular ids
                service.RegisterObject(singleton3, "en_US_BAR");
                result = service.Get("en_US_BAR");
                ConfirmIdentical("23) override super", result, singleton3);
            }

            // empty service should not recognize anything
            service.Reset();
            result = service.Get("en_US");
            ConfirmIdentical("24) empty", result, null);

            // create a custom multiple key factory
            {
                String[] xids = { "en_US_VALLEY_GIRL", "en_US_VALLEY_BOY",
                                  "en_US_SURFER_GAL",  "en_US_SURFER_DUDE" };
                service.RegisterFactory(new ICUServiceTest.TestLocaleKeyFactory(xids, "Later"));
            }

            // iterate over the visual ids returned by the multiple factory
            {
                ILOG.J2CsMapping.Collections.ISet vids = service.GetVisibleIDs();
                IIterator iter  = new ILOG.J2CsMapping.Collections.IteratorAdapter(vids.GetEnumerator());
                int       count = 0;
                while (iter.HasNext())
                {
                    ++count;
                    String id = (String)iter.Next();
                    Logln("  " + id + " --> " + service.Get(id));
                }
                // four visible ids
                ConfirmIdentical("25) visible ids", count, 4);
            }

            // iterate over the display names
            {
                IDictionary dids    = GetDisplayNames(service, IBM.ICU.Util.ULocale.GERMANY);
                IIterator   iter_0  = new ILOG.J2CsMapping.Collections.IteratorAdapter(dids.GetEnumerator());
                int         count_1 = 0;
                while (iter_0.HasNext())
                {
                    ++count_1;
                    DictionaryEntry e = (DictionaryEntry)iter_0.Next();
                    Logln("  " + ((DictionaryEntry)e).Key + " -- > " + ((DictionaryEntry)e).Value);
                }
                // four display names, in german
                ConfirmIdentical("26) display names", count_1, 4);
            }

            // no valid display name
            ConfirmIdentical("27) get display name",
                             service.GetDisplayName("en_US_VALLEY_GEEK"), null);

            {
                String name = service.GetDisplayName("en_US_SURFER_DUDE",
                                                     IBM.ICU.Util.ULocale.US);
                ConfirmEqual("28) get display name", name,
                             "English (United States, SURFER_DUDE)");
            }

            // register another multiple factory
            {
                String[] xids_2 = { "en_US_SURFER",  "en_US_SURFER_GAL",
                                    "en_US_SILICON", "en_US_SILICON_GEEK" };
                service.RegisterFactory(new ICUServiceTest.TestLocaleKeyFactory(xids_2, "Rad dude"));
            }

            // this time, we have seven display names
            // Rad dude's surfer gal 'replaces' later's surfer gal
            {
                IDictionary dids_3  = GetDisplayNames(service);
                IIterator   iter_4  = new ILOG.J2CsMapping.Collections.IteratorAdapter(dids_3.GetEnumerator());
                int         count_5 = 0;
                while (iter_4.HasNext())
                {
                    ++count_5;
                    DictionaryEntry e_6 = (DictionaryEntry)iter_4.Next();
                    Logln("  " + ((DictionaryEntry)e_6).Key + " --> " + ((DictionaryEntry)e_6).Value);
                }
                // seven display names, in spanish
                ConfirmIdentical("29) display names", count_5, 7);
            }

            // we should get the display name corresponding to the actual id
            // returned by the id we used.
            {
                String[] actualID = new String[1];
                String   id_7     = "en_us_surfer_gal";
                String   gal      = (String)service.Get(id_7, actualID);
                if (gal != null)
                {
                    Logln("actual id: " + actualID[0]);
                    String displayName = service.GetDisplayName(actualID[0],
                                                                IBM.ICU.Util.ULocale.US);
                    Logln("found actual: " + gal + " with display name: "
                          + displayName);
                    ConfirmBoolean("30) found display name for actual",
                                   displayName != null);

                    displayName = service.GetDisplayName(id_7, IBM.ICU.Util.ULocale.US);
                    Logln("found query: " + gal + " with display name: "
                          + displayName);
                    // this is no longer a bug, we want to return display names for
                    // anything
                    // that a factory handles. since we handle it, we should return
                    // a display
                    // name. see jb3549
                    // confirmBoolean("31) found display name for query",
                    // displayName == null);
                }
                else
                {
                    Errln("30) service could not find entry for " + id_7);
                }

                // this should be handled by the 'dude' factory, since it overrides
                // en_US_SURFER.
                id_7 = "en_US_SURFER_BOZO";
                String bozo = (String)service.Get(id_7, actualID);
                if (bozo != null)
                {
                    String displayName_8 = service.GetDisplayName(actualID[0],
                                                                  IBM.ICU.Util.ULocale.US);
                    Logln("found actual: " + bozo + " with display name: "
                          + displayName_8);
                    ConfirmBoolean("32) found display name for actual",
                                   displayName_8 != null);

                    displayName_8 = service.GetDisplayName(id_7, IBM.ICU.Util.ULocale.US);
                    Logln("found actual: " + bozo + " with display name: "
                          + displayName_8);
                    // see above and jb3549
                    // confirmBoolean("33) found display name for query",
                    // displayName == null);
                }
                else
                {
                    Errln("32) service could not find entry for " + id_7);
                }

                ConfirmBoolean("34) is default ", !service.IsDefault());
            }

            /*
             * // disallow hiding for now
             *
             * // hiding factory should obscure 'sublocales' { String[] xids = {
             * "en_US_VALLEY", "en_US_SILICON" }; service.registerFactory(new
             * TestHidingFactory(xids, "hiding")); }
             *
             * { Map dids = service.getDisplayNames(); Iterator iter =
             * dids.entrySet().iterator(); int count = 0; while (iter.hasNext()) {
             * ++count; Entry e = (Entry)iter.next(); logln("  " + e.getKey() +
             * " -- > " + e.getValue()); } confirmIdentical("35) hiding factory",
             * count, 5); }
             */

            {
                ILOG.J2CsMapping.Collections.ISet xids_9 = service.GetVisibleIDs();
                IIterator iter_10 = new ILOG.J2CsMapping.Collections.IteratorAdapter(xids_9.GetEnumerator());
                while (iter_10.HasNext())
                {
                    String xid = (String)iter_10.Next();
                    Logln(xid + "?  " + service.Get(xid));
                }

                Logln("valleygirl?  " + service.Get("en_US_VALLEY_GIRL"));
                Logln("valleyboy?   " + service.Get("en_US_VALLEY_BOY"));
                Logln("valleydude?  " + service.Get("en_US_VALLEY_DUDE"));
                Logln("surfergirl?  " + service.Get("en_US_SURFER_GIRL"));
            }

            // resource bundle factory.
            service.Reset();
            service.RegisterFactory(new IBM.ICU.Impl.ICULocaleService.ICUResourceBundleFactory());

            // list all of the resources
            {
                Logln("all visible ids: " + service.GetVisibleIDs());

                /*
                 * Set xids = service.getVisibleIDs(); StringBuffer buf = new
                 * StringBuffer("{"); boolean notfirst = false; Iterator iter =
                 * xids.iterator(); while (iter.hasNext()) { String xid =
                 * (String)iter.next(); if (notfirst) { buf.append(", "); } else {
                 * notfirst = true; } buf.append(xid); } buf.append("}");
                 * logln(buf.toString());
                 */
            }

            // list only the resources for es, default locale
            // since we're using the default Key, only "es" is matched
            {
                Logln("visible ids for es locale: " + service.GetVisibleIDs("es"));
            }

            // list only the spanish display names for es, spanish collation order
            // since we're using the default Key, only "es" is matched
            {
                Logln("display names: "
                      + GetDisplayNames(service, new ULocale("es"), "es"));
            }

            // list the display names in reverse order
            {
                Logln("display names in reverse order: "
                      + service.GetDisplayNames(IBM.ICU.Util.ULocale.US, new ICUServiceTest.Anonymous_C4()));
            }

            // get all the display names of these resources
            // this should be fast since the display names were cached.
            {
                Logln("service display names for de_DE");
                IDictionary   names   = GetDisplayNames(service, new ULocale("de_DE"));
                StringBuilder buf     = new StringBuilder("{");
                IIterator     iter_11 = new ILOG.J2CsMapping.Collections.IteratorAdapter(names.GetEnumerator());
                while (iter_11.HasNext())
                {
                    DictionaryEntry e_12    = (DictionaryEntry)iter_11.Next();
                    String          name_13 = (String)((DictionaryEntry)e_12).Key;
                    String          id_14   = (String)((DictionaryEntry)e_12).Value;
                    buf.Append("\n   " + name_13 + " --> " + id_14);
                }
                buf.Append("\n}");
                Logln(buf.ToString());
            }

            ICUServiceTest.CalifornioLanguageFactory califactory = new ICUServiceTest.CalifornioLanguageFactory();
            service.RegisterFactory(califactory);
            // get all the display names of these resources
            {
                Logln("californio language factory");
                StringBuilder buf_15  = new StringBuilder("{");
                String[]      idNames = { IBM.ICU.Charset.ICUServiceTest.CalifornioLanguageFactory.californio,
                                          IBM.ICU.Charset.ICUServiceTest.CalifornioLanguageFactory.valley,
                                          IBM.ICU.Charset.ICUServiceTest.CalifornioLanguageFactory.surfer,
                                          IBM.ICU.Charset.ICUServiceTest.CalifornioLanguageFactory.geek };
                for (int i = 0; i < idNames.Length; ++i)
                {
                    String idName = idNames[i];
                    buf_15.Append("\n  --- " + idName + " ---");
                    IDictionary names_16 = GetDisplayNames(service, new ULocale(idName));
                    IIterator   iter_17  = new ILOG.J2CsMapping.Collections.IteratorAdapter(names_16.GetEnumerator());
                    while (iter_17.HasNext())
                    {
                        DictionaryEntry e_18    = (DictionaryEntry)iter_17.Next();
                        String          name_19 = (String)((DictionaryEntry)e_18).Key;
                        String          id_20   = (String)((DictionaryEntry)e_18).Value;
                        buf_15.Append("\n    " + name_19 + " --> " + id_20);
                    }
                }
                buf_15.Append("\n}");
                Logln(buf_15.ToString());
            }

            // test notification
            // simple registration
            {
                Logln("simple registration notification");
                ICULocaleService ls = new ICULocaleService();
                IBM.ICU.Impl.ICUService.ServiceListener l1 = new ICUServiceTest.Anonymous_C3(this);
                ls.AddListener(l1);
                IBM.ICU.Impl.ICUService.ServiceListener l2 = new ICUServiceTest.Anonymous_C2(this);
                ls.AddListener(l2);
                Logln("registering foo... ");
                ls.RegisterObject("Foo", "en_FOO");
                Logln("registering bar... ");
                ls.RegisterObject("Bar", "en_BAR");
                Logln("getting foo...");
                Logln((String)ls.Get("en_FOO"));
                Logln("removing listener 2...");
                ls.RemoveListener(l2);
                Logln("registering baz...");
                ls.RegisterObject("Baz", "en_BAZ");
                Logln("removing listener 1");
                ls.RemoveListener(l1);
                Logln("registering burp...");
                ls.RegisterObject("Burp", "en_BURP");

                // should only get one notification even if register multiple times
                Logln("... trying multiple registration");
                ls.AddListener(l1);
                ls.AddListener(l1);
                ls.AddListener(l1);
                ls.AddListener(l2);
                ls.RegisterObject("Foo", "en_FOO");
                Logln("... registered foo");

                // since in a separate thread, we can callback and not deadlock
                IBM.ICU.Impl.ICUService.ServiceListener l3 = new ICUServiceTest.Anonymous_C1(this);
                ls.AddListener(l3);
                Logln("registering boo...");
                ls.RegisterObject("Boo", "en_BOO");
                Logln("...done");

                try
                {
                    ILOG.J2CsMapping.Threading.ThreadWrapper.Sleep(100);
                }
                catch (ThreadInterruptedException e_21)
                {
                }
            }
        }