Exemple #1
0
        public void TestRegisterFactory()
        {
            ULocale fu_FU     = new ULocale("fu_FU");
            ULocale fu_FU_FOO = new ULocale("fu_FU_FOO");

            IDictionary <object, object> fuFUNames = new Dictionary <object, object>
            {
                { fu_FU, "ze leetle bunny Fu-Fu" },
                { fu_FU_FOO, "zee leetel bunny Foo-Foo" },
                { 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 (MissingManifestResourceException ex)
            {
                Warnln("Could not load locale data.");
            }
            // coverage
            {
                TestFactoryWrapper wrapper = new TestFactoryWrapper(factory); // in java, gc lets us easily multiply reference!
                Object             key     = Collator.RegisterFactory(wrapper);
                String             name    = null;
                try
                {
                    name = Collator.GetDisplayName(fu_FU, fu_FU_FOO);
                }
                catch (MissingManifestResourceException 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();
                bool      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");
                }
            }
        }
Exemple #2
0
        public void TestRegister()
        {
            // register a singleton
            Collator frcol = Collator.GetInstance(ULocale.FRANCE);
            Collator uscol = Collator.GetInstance(ULocale.US);

            { // try override en_US collator
                Object   key  = Collator.RegisterInstance(frcol, ULocale.US);
                Collator ncol = Collator.GetInstance(ULocale.US);
                if (!frcol.Equals(ncol))
                {
                    Errln("register of french collator for en_US failed");
                }

                // coverage
                Collator test = Collator.GetInstance(ULocale.GERMANY); // CollatorFactory.handleCreate
                if (!test.GetLocale(ULocale.VALID_LOCALE).Equals(ULocale.GERMAN))
                {
                    Errln("Collation from Germany is really " + test.GetLocale(ULocale.VALID_LOCALE));
                }

                if (!Collator.Unregister(key))
                {
                    Errln("failed to unregister french collator");
                }
                ncol = Collator.GetInstance(ULocale.US);
                if (!uscol.Equals(ncol))
                {
                    Errln("collator after unregister does not match original");
                }
            }

            ULocale fu_FU = new ULocale("fu_FU_FOO");

            { // try create collator for new locale
                Collator fucol = Collator.GetInstance(fu_FU);
                Object   key   = Collator.RegisterInstance(frcol, fu_FU);
                Collator ncol  = Collator.GetInstance(fu_FU);
                if (!frcol.Equals(ncol))
                {
                    Errln("register of fr collator for fu_FU failed");
                }

                ULocale[] locales = Collator.GetAvailableULocales();
                bool      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");
                }
                try
                {
                    String name = Collator.GetDisplayName(fu_FU);
                    if (!"fu (FU, FOO)".Equals(name) &&
                        !"fu_FU_FOO".Equals(name) /* no LocaleDisplayNamesImpl */)
                    {
                        Errln("found " + name + " for fu_FU");
                    }
                }
                catch (MissingManifestResourceException ex)
                {
                    Warnln("Could not load locale data.");
                }
                try
                {
                    String name = Collator.GetDisplayName(fu_FU, fu_FU);
                    if (!"fu (FU, FOO)".Equals(name) &&
                        !"fu_FU_FOO".Equals(name) /* no LocaleDisplayNamesImpl */)
                    {
                        Errln("found " + name + " for fu_FU");
                    }
                }
                catch (MissingManifestResourceException ex)
                {
                    Warnln("Could not load locale data.");
                }

                if (!Collator.Unregister(key))
                {
                    Errln("failed to unregister french collator");
                }
                ncol = Collator.GetInstance(fu_FU);
                if (!fucol.Equals(ncol))
                {
                    Errln("collator after unregister does not match original fu_FU");
                }
            }

            {
                // coverage after return to default
                ULocale[] locales = Collator.GetAvailableULocales();

                for (int i = 0; i < locales.Length; ++i)
                {
                    if (locales[i].Equals(fu_FU))
                    {
                        Errln("new locale fu_FU not reported as supported locale");
                        break;
                    }
                }

                Collator ncol = Collator.GetInstance(ULocale.US);
                if (!ncol.GetLocale(ULocale.VALID_LOCALE).Equals(ULocale.US))
                {
                    Errln("Collation from US is really " + ncol.GetLocale(ULocale.VALID_LOCALE));
                }
            }
        }