Source Code Cross Referenced for ICUServiceTest.java in  » Internationalization-Localization » icu4j » com » ibm » icu » dev » test » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Internationalization Localization » icu4j » com.ibm.icu.dev.test.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**
0002:         *******************************************************************************
0003:         * Copyright (C) 2001-2006, International Business Machines Corporation and    *
0004:         * others. All Rights Reserved.                                                *
0005:         *******************************************************************************
0006:         */package com.ibm.icu.dev.test.util;
0007:
0008:        import com.ibm.icu.dev.test.TestFmwk;
0009:        import com.ibm.icu.impl.ICUNotifier;
0010:        import com.ibm.icu.impl.ICURWLock;
0011:        import com.ibm.icu.impl.ICUResourceBundle;
0012:        import com.ibm.icu.impl.ICUService;
0013:        import com.ibm.icu.impl.ICUService.Factory;
0014:        import com.ibm.icu.impl.ICUService.Key;
0015:        import com.ibm.icu.impl.ICUService.ServiceListener;
0016:        import com.ibm.icu.impl.ICUService.SimpleFactory;
0017:        import com.ibm.icu.impl.LocaleUtility; //import com.ibm.icu.impl.ICULocaleData;
0018:        import com.ibm.icu.impl.ICULocaleService;
0019:        import com.ibm.icu.impl.ICULocaleService.LocaleKey;
0020:        import com.ibm.icu.impl.ICULocaleService.LocaleKeyFactory;
0021:        import com.ibm.icu.impl.ICULocaleService.ICUResourceBundleFactory;
0022:        import com.ibm.icu.text.Collator;
0023:        import com.ibm.icu.util.ULocale;
0024:
0025:        import java.util.Arrays;
0026:        import java.util.Collections;
0027:        import java.util.Comparator;
0028:        import java.util.EventListener;
0029:        import java.util.HashMap;
0030:        import java.util.HashSet;
0031:        import java.util.Iterator;
0032:        import java.util.List;
0033:        import java.util.Map;
0034:        import java.util.Map.Entry;
0035:        import java.util.Set;
0036:        import java.util.SortedMap;
0037:
0038:        public class ICUServiceTest extends TestFmwk {
0039:            public static void main(String[] args) throws Exception {
0040:                ICUServiceTest test = new ICUServiceTest();
0041:                test.run(args);
0042:            }
0043:
0044:            private String lrmsg(String message, Object lhs, Object rhs) {
0045:                return message + " lhs: " + lhs + " rhs: " + rhs;
0046:            }
0047:
0048:            public void confirmBoolean(String message, boolean val) {
0049:                msg(message, val ? LOG : ERR, !val, true);
0050:            }
0051:
0052:            public void confirmEqual(String message, Object lhs, Object rhs) {
0053:                msg(lrmsg(message, lhs, rhs), (lhs == null ? rhs == null : lhs
0054:                        .equals(rhs)) ? LOG : ERR, true, true);
0055:            }
0056:
0057:            public void confirmIdentical(String message, Object lhs, Object rhs) {
0058:                msg(lrmsg(message, lhs, rhs), lhs == rhs ? LOG : ERR, true,
0059:                        true);
0060:            }
0061:
0062:            public void confirmIdentical(String message, int lhs, int rhs) {
0063:                msg(message + " lhs: " + lhs + " rhs: " + rhs, lhs == rhs ? LOG
0064:                        : ERR, true, true);
0065:            }
0066:
0067:            /**
0068:             * Convenience override of getDisplayNames(ULocale, Comparator, String) that
0069:             * uses the current default ULocale as the locale, the default collator for
0070:             * the locale as the comparator to sort the display names, and null for
0071:             * the matchID.
0072:             */
0073:            public SortedMap getDisplayNames(ICUService service) {
0074:                ULocale locale = ULocale.getDefault();
0075:                Collator col = Collator.getInstance(locale);
0076:                return service.getDisplayNames(locale, col, null);
0077:            }
0078:
0079:            /**
0080:             * Convenience override of getDisplayNames(ULocale, Comparator, String) that
0081:             * uses the default collator for the locale as the comparator to
0082:             * sort the display names, and null for the matchID.
0083:             */
0084:            public SortedMap getDisplayNames(ICUService service, ULocale locale) {
0085:                Collator col = Collator.getInstance(locale);
0086:                return service.getDisplayNames(locale, col, null);
0087:            }
0088:
0089:            /**
0090:             * Convenience override of getDisplayNames(ULocale, Comparator, String) that
0091:             * uses the default collator for the locale as the comparator to
0092:             * sort the display names.
0093:             */
0094:            public SortedMap getDisplayNames(ICUService service,
0095:                    ULocale locale, String matchID) {
0096:                Collator col = Collator.getInstance(locale);
0097:                return service.getDisplayNames(locale, col, matchID);
0098:            }
0099:
0100:            // use locale keys
0101:            static final class TestService extends ICUService {
0102:                public TestService() {
0103:                    super ("Test Service");
0104:                }
0105:
0106:                public Key createKey(String id) {
0107:                    return LocaleKey.createWithCanonicalFallback(id, null); // no fallback locale
0108:                }
0109:            }
0110:
0111:            public void TestAPI() {
0112:                // create a service using locale keys,
0113:                ICUService service = new TestService();
0114:
0115:                logln("service name:" + service.getName());
0116:
0117:                // register an object with one locale,
0118:                // search for an object with a more specific locale
0119:                // should return the original object
0120:                Integer singleton0 = new Integer(0);
0121:                service.registerObject(singleton0, "en_US");
0122:                Object result = service.get("en_US_FOO");
0123:                confirmIdentical("1) en_US_FOO -> en_US", result, singleton0);
0124:
0125:                // register a new object with the more specific locale
0126:                // search for an object with that locale
0127:                // should return the new object
0128:                Integer singleton1 = new Integer(1);
0129:                service.registerObject(singleton1, "en_US_FOO");
0130:                result = service.get("en_US_FOO");
0131:                confirmIdentical("2) en_US_FOO -> en_US_FOO", result,
0132:                        singleton1);
0133:
0134:                // search for an object that falls back to the first registered locale
0135:                result = service.get("en_US_BAR");
0136:                confirmIdentical("3) en_US_BAR -> en_US", result, singleton0);
0137:
0138:                // get a list of the factories, should be two
0139:                List factories = service.factories();
0140:                confirmIdentical("4) factory size", factories.size(), 2);
0141:
0142:                // register a new object with yet another locale
0143:                // original factory list is unchanged
0144:                Integer singleton2 = new Integer(2);
0145:                service.registerObject(singleton2, "en");
0146:                confirmIdentical("5) factory size", factories.size(), 2);
0147:
0148:                // search for an object with the new locale
0149:                // stack of factories is now en, en_US_FOO, en_US
0150:                // search for en_US should still find en_US object
0151:                result = service.get("en_US_BAR");
0152:                confirmIdentical("6) en_US_BAR -> en_US", result, singleton0);
0153:
0154:                // register a new object with an old id, should hide earlier factory using this id, but leave it there
0155:                Integer singleton3 = new Integer(3);
0156:                service.registerObject(singleton3, "en_US");
0157:                factories = service.factories();
0158:                confirmIdentical("9) factory size", factories.size(), 4);
0159:
0160:                // should get data from that new factory
0161:                result = service.get("en_US_BAR");
0162:                confirmIdentical("10) en_US_BAR -> (3)", result, singleton3);
0163:
0164:                // remove new factory
0165:                // should have fewer factories again
0166:                service.unregisterFactory((Factory) factories.get(0));
0167:                factories = service.factories();
0168:                confirmIdentical("11) factory size", factories.size(), 3);
0169:
0170:                // should get original data again after remove factory
0171:                result = service.get("en_US_BAR");
0172:                confirmIdentical("12) en_US_BAR -> 0", result, singleton0);
0173:
0174:                // shouldn't find unregistered ids
0175:                result = service.get("foo");
0176:                confirmIdentical("13) foo -> null", result, null);
0177:
0178:                // should find non-canonical strings
0179:                String[] resultID = new String[1];
0180:                result = service.get("EN_us_fOo", resultID);
0181:                confirmEqual("14) find non-canonical", resultID[0], "en_US_FOO");
0182:
0183:                // should be able to register non-canonical strings and get them canonicalized
0184:                service.registerObject(singleton3, "eN_ca_dUde");
0185:                result = service.get("En_Ca_DuDe", resultID);
0186:                confirmEqual("15) register non-canonical", resultID[0],
0187:                        "en_CA_DUDE");
0188:
0189:                // should be able to register invisible factories, these will not
0190:                // be visible by default, but if you know the secret password you
0191:                // can still access these services...
0192:                Integer singleton4 = new Integer(4);
0193:                service.registerObject(singleton4, "en_US_BAR", false);
0194:                result = service.get("en_US_BAR");
0195:                confirmIdentical("17) get invisible", result, singleton4);
0196:
0197:                // should not be able to locate invisible services
0198:                Set ids = service.getVisibleIDs();
0199:                confirmBoolean("18) find invisible", !ids.contains("en_US_BAR"));
0200:
0201:                service.reset();
0202:                // an anonymous factory than handles all ids
0203:                {
0204:                    Factory factory = new Factory() {
0205:                        public Object create(Key key, ICUService service) {
0206:                            return new ULocale(key.currentID());
0207:                        }
0208:
0209:                        ///CLOVER:OFF
0210:                        public void updateVisibleIDs(Map result) {
0211:                        }
0212:
0213:                        ///CLOVER:ON
0214:
0215:                        ///CLOVER:OFF
0216:                        public String getDisplayName(String id, ULocale l) {
0217:                            return null;
0218:                        }
0219:                        ///CLOVER:ON
0220:                    };
0221:                    service.registerFactory(factory);
0222:
0223:                    // anonymous factory will still handle the id
0224:                    result = service.get(ULocale.US.toString());
0225:                    confirmEqual("21) locale", result, ULocale.US);
0226:
0227:                    // still normalizes id
0228:                    result = service.get("EN_US_BAR");
0229:                    confirmEqual("22) locale", result, new ULocale("en_US_BAR"));
0230:
0231:                    // we can override for particular ids
0232:                    service.registerObject(singleton3, "en_US_BAR");
0233:                    result = service.get("en_US_BAR");
0234:                    confirmIdentical("23) override super", result, singleton3);
0235:
0236:                }
0237:
0238:                // empty service should not recognize anything
0239:                service.reset();
0240:                result = service.get("en_US");
0241:                confirmIdentical("24) empty", result, null);
0242:
0243:                // create a custom multiple key factory
0244:                {
0245:                    String[] xids = { "en_US_VALLEY_GIRL", "en_US_VALLEY_BOY",
0246:                            "en_US_SURFER_GAL", "en_US_SURFER_DUDE" };
0247:                    service.registerFactory(new TestLocaleKeyFactory(xids,
0248:                            "Later"));
0249:                }
0250:
0251:                // iterate over the visual ids returned by the multiple factory
0252:                {
0253:                    Set vids = service.getVisibleIDs();
0254:                    Iterator iter = vids.iterator();
0255:                    int count = 0;
0256:                    while (iter.hasNext()) {
0257:                        ++count;
0258:                        String id = (String) iter.next();
0259:                        logln("  " + id + " --> " + service.get(id));
0260:                    }
0261:                    // four visible ids
0262:                    confirmIdentical("25) visible ids", count, 4);
0263:                }
0264:
0265:                // iterate over the display names
0266:                {
0267:                    Map dids = getDisplayNames(service, ULocale.GERMANY);
0268:                    Iterator iter = dids.entrySet().iterator();
0269:                    int count = 0;
0270:                    while (iter.hasNext()) {
0271:                        ++count;
0272:                        Entry e = (Entry) iter.next();
0273:                        logln("  " + e.getKey() + " -- > " + e.getValue());
0274:                    }
0275:                    // four display names, in german
0276:                    confirmIdentical("26) display names", count, 4);
0277:                }
0278:
0279:                // no valid display name
0280:                confirmIdentical("27) get display name", service
0281:                        .getDisplayName("en_US_VALLEY_GEEK"), null);
0282:
0283:                {
0284:                    String name = service.getDisplayName("en_US_SURFER_DUDE",
0285:                            ULocale.US);
0286:                    confirmEqual("28) get display name", name,
0287:                            "English (United States, SURFER_DUDE)");
0288:                }
0289:
0290:                // register another multiple factory
0291:                {
0292:                    String[] xids = { "en_US_SURFER", "en_US_SURFER_GAL",
0293:                            "en_US_SILICON", "en_US_SILICON_GEEK" };
0294:                    service.registerFactory(new TestLocaleKeyFactory(xids,
0295:                            "Rad dude"));
0296:                }
0297:
0298:                // this time, we have seven display names
0299:                // Rad dude's surfer gal 'replaces' later's surfer gal
0300:                {
0301:                    Map dids = getDisplayNames(service);
0302:                    Iterator iter = dids.entrySet().iterator();
0303:                    int count = 0;
0304:                    while (iter.hasNext()) {
0305:                        ++count;
0306:                        Entry e = (Entry) iter.next();
0307:                        logln("  " + e.getKey() + " --> " + e.getValue());
0308:                    }
0309:                    // seven display names, in spanish
0310:                    confirmIdentical("29) display names", count, 7);
0311:                }
0312:
0313:                // we should get the display name corresponding to the actual id
0314:                // returned by the id we used.
0315:                {
0316:                    String[] actualID = new String[1];
0317:                    String id = "en_us_surfer_gal";
0318:                    String gal = (String) service.get(id, actualID);
0319:                    if (gal != null) {
0320:                        logln("actual id: " + actualID[0]);
0321:                        String displayName = service.getDisplayName(
0322:                                actualID[0], ULocale.US);
0323:                        logln("found actual: " + gal + " with display name: "
0324:                                + displayName);
0325:                        confirmBoolean("30) found display name for actual",
0326:                                displayName != null);
0327:
0328:                        displayName = service.getDisplayName(id, ULocale.US);
0329:                        logln("found query: " + gal + " with display name: "
0330:                                + displayName);
0331:                        // this is no longer a bug, we want to return display names for anything
0332:                        // that a factory handles.  since we handle it, we should return a display
0333:                        // name.  see jb3549
0334:                        // confirmBoolean("31) found display name for query", displayName == null);
0335:                    } else {
0336:                        errln("30) service could not find entry for " + id);
0337:                    }
0338:
0339:                    // this should be handled by the 'dude' factory, since it overrides en_US_SURFER.
0340:                    id = "en_US_SURFER_BOZO";
0341:                    String bozo = (String) service.get(id, actualID);
0342:                    if (bozo != null) {
0343:                        String displayName = service.getDisplayName(
0344:                                actualID[0], ULocale.US);
0345:                        logln("found actual: " + bozo + " with display name: "
0346:                                + displayName);
0347:                        confirmBoolean("32) found display name for actual",
0348:                                displayName != null);
0349:
0350:                        displayName = service.getDisplayName(id, ULocale.US);
0351:                        logln("found actual: " + bozo + " with display name: "
0352:                                + displayName);
0353:                        // see above and jb3549
0354:                        // confirmBoolean("33) found display name for query", displayName == null);
0355:                    } else {
0356:                        errln("32) service could not find entry for " + id);
0357:                    }
0358:
0359:                    confirmBoolean("34) is default ", !service.isDefault());
0360:                }
0361:
0362:                /*
0363:                // disallow hiding for now
0364:
0365:                // hiding factory should obscure 'sublocales'
0366:                {
0367:                String[] xids = {
0368:                "en_US_VALLEY", "en_US_SILICON"
0369:                };
0370:                service.registerFactory(new TestHidingFactory(xids, "hiding"));
0371:                }
0372:
0373:                {
0374:                Map dids = service.getDisplayNames();
0375:                Iterator iter = dids.entrySet().iterator();
0376:                int count = 0;
0377:                while (iter.hasNext()) {
0378:                ++count;
0379:                Entry e = (Entry)iter.next();
0380:                logln("  " + e.getKey() + " -- > " + e.getValue());
0381:                }
0382:                confirmIdentical("35) hiding factory", count, 5);
0383:                }
0384:                 */
0385:
0386:                {
0387:                    Set xids = service.getVisibleIDs();
0388:                    Iterator iter = xids.iterator();
0389:                    while (iter.hasNext()) {
0390:                        String xid = (String) iter.next();
0391:                        logln(xid + "?  " + service.get(xid));
0392:                    }
0393:
0394:                    logln("valleygirl?  " + service.get("en_US_VALLEY_GIRL"));
0395:                    logln("valleyboy?   " + service.get("en_US_VALLEY_BOY"));
0396:                    logln("valleydude?  " + service.get("en_US_VALLEY_DUDE"));
0397:                    logln("surfergirl?  " + service.get("en_US_SURFER_GIRL"));
0398:                }
0399:
0400:                // resource bundle factory.
0401:                service.reset();
0402:                service.registerFactory(new ICUResourceBundleFactory());
0403:
0404:                // list all of the resources
0405:                {
0406:                    logln("all visible ids: " + service.getVisibleIDs());
0407:                    /*
0408:                    Set xids = service.getVisibleIDs();
0409:                    StringBuffer buf = new StringBuffer("{");
0410:                    boolean notfirst = false;
0411:                    Iterator iter = xids.iterator();
0412:                    while (iter.hasNext()) {
0413:                    String xid = (String)iter.next();
0414:                    if (notfirst) {
0415:                    buf.append(", ");
0416:                    } else {
0417:                    notfirst = true;
0418:                    }
0419:                    buf.append(xid);
0420:                    }
0421:                    buf.append("}");
0422:                    logln(buf.toString());
0423:                     */
0424:                }
0425:
0426:                // list only the resources for es, default locale
0427:                // since we're using the default Key, only "es" is matched
0428:                {
0429:                    logln("visible ids for es locale: "
0430:                            + service.getVisibleIDs("es"));
0431:                }
0432:
0433:                // list only the spanish display names for es, spanish collation order
0434:                // since we're using the default Key, only "es" is matched
0435:                {
0436:                    logln("display names: "
0437:                            + getDisplayNames(service, new ULocale("es"), "es"));
0438:                }
0439:
0440:                // list the display names in reverse order
0441:                {
0442:                    logln("display names in reverse order: "
0443:                            + service.getDisplayNames(ULocale.US,
0444:                                    new Comparator() {
0445:                                        public int compare(Object lhs,
0446:                                                Object rhs) {
0447:                                            return -String.CASE_INSENSITIVE_ORDER
0448:                                                    .compare((String) lhs,
0449:                                                            (String) rhs);
0450:                                        }
0451:                                    }));
0452:                }
0453:
0454:                // get all the display names of these resources
0455:                // this should be fast since the display names were cached.
0456:                {
0457:                    logln("service display names for de_DE");
0458:                    Map names = getDisplayNames(service, new ULocale("de_DE"));
0459:                    StringBuffer buf = new StringBuffer("{");
0460:                    Iterator iter = names.entrySet().iterator();
0461:                    while (iter.hasNext()) {
0462:                        Entry e = (Entry) iter.next();
0463:                        String name = (String) e.getKey();
0464:                        String id = (String) e.getValue();
0465:                        buf.append("\n   " + name + " --> " + id);
0466:                    }
0467:                    buf.append("\n}");
0468:                    logln(buf.toString());
0469:                }
0470:
0471:                CalifornioLanguageFactory califactory = new CalifornioLanguageFactory();
0472:                service.registerFactory(califactory);
0473:                // get all the display names of these resources
0474:                {
0475:                    logln("californio language factory");
0476:                    StringBuffer buf = new StringBuffer("{");
0477:                    String[] idNames = { CalifornioLanguageFactory.californio,
0478:                            CalifornioLanguageFactory.valley,
0479:                            CalifornioLanguageFactory.surfer,
0480:                            CalifornioLanguageFactory.geek };
0481:                    for (int i = 0; i < idNames.length; ++i) {
0482:                        String idName = idNames[i];
0483:                        buf.append("\n  --- " + idName + " ---");
0484:                        Map names = getDisplayNames(service,
0485:                                new ULocale(idName));
0486:                        Iterator iter = names.entrySet().iterator();
0487:                        while (iter.hasNext()) {
0488:                            Entry e = (Entry) iter.next();
0489:                            String name = (String) e.getKey();
0490:                            String id = (String) e.getValue();
0491:                            buf.append("\n    " + name + " --> " + id);
0492:                        }
0493:                    }
0494:                    buf.append("\n}");
0495:                    logln(buf.toString());
0496:                }
0497:
0498:                // test notification
0499:                // simple registration
0500:                {
0501:                    logln("simple registration notification");
0502:                    ICULocaleService ls = new ICULocaleService();
0503:                    ServiceListener l1 = new ServiceListener() {
0504:                        private int n;
0505:
0506:                        public void serviceChanged(ICUService s) {
0507:                            logln("listener 1 report " + n++
0508:                                    + " service changed: " + s);
0509:                        }
0510:                    };
0511:                    ls.addListener(l1);
0512:                    ServiceListener l2 = new ServiceListener() {
0513:                        private int n;
0514:
0515:                        public void serviceChanged(ICUService s) {
0516:                            logln("listener 2 report " + n++
0517:                                    + " service changed: " + s);
0518:                        }
0519:                    };
0520:                    ls.addListener(l2);
0521:                    logln("registering foo... ");
0522:                    ls.registerObject("Foo", "en_FOO");
0523:                    logln("registering bar... ");
0524:                    ls.registerObject("Bar", "en_BAR");
0525:                    logln("getting foo...");
0526:                    logln((String) ls.get("en_FOO"));
0527:                    logln("removing listener 2...");
0528:                    ls.removeListener(l2);
0529:                    logln("registering baz...");
0530:                    ls.registerObject("Baz", "en_BAZ");
0531:                    logln("removing listener 1");
0532:                    ls.removeListener(l1);
0533:                    logln("registering burp...");
0534:                    ls.registerObject("Burp", "en_BURP");
0535:
0536:                    // should only get one notification even if register multiple times
0537:                    logln("... trying multiple registration");
0538:                    ls.addListener(l1);
0539:                    ls.addListener(l1);
0540:                    ls.addListener(l1);
0541:                    ls.addListener(l2);
0542:                    ls.registerObject("Foo", "en_FOO");
0543:                    logln("... registered foo");
0544:
0545:                    // since in a separate thread, we can callback and not deadlock
0546:                    ServiceListener l3 = new ServiceListener() {
0547:                        private int n;
0548:
0549:                        public void serviceChanged(ICUService s) {
0550:                            logln("listener 3 report " + n++
0551:                                    + " service changed...");
0552:                            if (s.get("en_BOINK") == null) { // don't recurse on ourselves!!!
0553:                                logln("registering boink...");
0554:                                s.registerObject("boink", "en_BOINK");
0555:                            }
0556:                        }
0557:                    };
0558:                    ls.addListener(l3);
0559:                    logln("registering boo...");
0560:                    ls.registerObject("Boo", "en_BOO");
0561:                    logln("...done");
0562:
0563:                    try {
0564:                        Thread.sleep(100);
0565:                    } catch (InterruptedException e) {
0566:                    }
0567:                }
0568:            }
0569:
0570:            static class TestLocaleKeyFactory extends LocaleKeyFactory {
0571:                protected final Set ids;
0572:                protected final String factoryID;
0573:
0574:                public TestLocaleKeyFactory(String[] ids, String factoryID) {
0575:                    super (VISIBLE, factoryID);
0576:
0577:                    this .ids = Collections.unmodifiableSet(new HashSet(Arrays
0578:                            .asList(ids)));
0579:                    this .factoryID = factoryID + ": ";
0580:                }
0581:
0582:                protected Object handleCreate(ULocale loc, int kind,
0583:                        ICUService service) {
0584:                    return factoryID + loc.toString();
0585:                }
0586:
0587:                protected Set getSupportedIDs() {
0588:                    return ids;
0589:                }
0590:            }
0591:
0592:            /*
0593:              // Disallow hiding for now since it causes gnarly problems, like
0594:              // how do you localize the hidden (but still exported) names.
0595:
0596:              static class TestHidingFactory implements ICUService.Factory {
0597:              protected final String[] ids;
0598:              protected final String factoryID;
0599:
0600:              public TestHidingFactory(String[] ids) {
0601:              this(ids, "Hiding");
0602:              }
0603:
0604:              public TestHidingFactory(String[] ids, String factoryID) {
0605:              this.ids = (String[])ids.clone();
0606:
0607:              if (factoryID == null || factoryID.length() == 0) {
0608:              this.factoryID = "";
0609:              } else {
0610:              this.factoryID = factoryID + ": ";
0611:              }
0612:              }
0613:
0614:              public Object create(Key key, ICUService service) {
0615:              for (int i = 0; i < ids.length; ++i) {
0616:              if (LocaleUtility.isFallbackOf(ids[i], key.currentID())) {
0617:              return factoryID + key.canonicalID();
0618:              }
0619:              }
0620:              return null;
0621:              }
0622:
0623:              public void updateVisibleIDs(Map result) {
0624:              for (int i = 0; i < ids.length; ++i) {
0625:              String id = ids[i];
0626:              Iterator iter = result.keySet().iterator();
0627:              while (iter.hasNext()) {
0628:              if (LocaleUtility.isFallbackOf(id, (String)iter.next())) {
0629:              iter.remove();
0630:              }
0631:              }
0632:              result.put(id, this);
0633:              }
0634:              }
0635:
0636:              public String getDisplayName(String id, ULocale locale) {
0637:              return factoryID + new ULocale(id).getDisplayName(locale);
0638:              }
0639:              }
0640:             */
0641:
0642:            static class CalifornioLanguageFactory extends
0643:                    ICUResourceBundleFactory {
0644:                public static String californio = "en_US_CA";
0645:                public static String valley = californio + "_VALLEY";
0646:                public static String surfer = californio + "_SURFER";
0647:                public static String geek = californio + "_GEEK";
0648:                public static Set supportedIDs;
0649:                static {
0650:                    HashSet result = new HashSet();
0651:                    result
0652:                            .addAll(ICUResourceBundle
0653:                                    .getAvailableLocaleNameSet());
0654:                    result.add(californio);
0655:                    result.add(valley);
0656:                    result.add(surfer);
0657:                    result.add(geek);
0658:                    supportedIDs = Collections.unmodifiableSet(result);
0659:                }
0660:
0661:                public Set getSupportedIDs() {
0662:                    return supportedIDs;
0663:                }
0664:
0665:                public String getDisplayName(String id, ULocale locale) {
0666:                    String prefix = "";
0667:                    String suffix = "";
0668:                    String ls = locale.toString();
0669:                    if (LocaleUtility.isFallbackOf(californio, ls)) {
0670:                        if (ls.equalsIgnoreCase(valley)) {
0671:                            prefix = "Like, you know, it's so totally ";
0672:                        } else if (ls.equalsIgnoreCase(surfer)) {
0673:                            prefix = "Dude, its ";
0674:                        } else if (ls.equalsIgnoreCase(geek)) {
0675:                            prefix = "I'd estimate it's approximately ";
0676:                        } else {
0677:                            prefix = "Huh?  Maybe ";
0678:                        }
0679:                    }
0680:                    if (LocaleUtility.isFallbackOf(californio, id)) {
0681:                        if (id.equalsIgnoreCase(valley)) {
0682:                            suffix = "like the Valley, you know?  Let's go to the mall!";
0683:                        } else if (id.equalsIgnoreCase(surfer)) {
0684:                            suffix = "time to hit those gnarly waves, Dude!!!";
0685:                        } else if (id.equalsIgnoreCase(geek)) {
0686:                            suffix = "all systems go.  T-Minus 9, 8, 7...";
0687:                        } else {
0688:                            suffix = "No Habla Englais";
0689:                        }
0690:                    } else {
0691:                        suffix = super .getDisplayName(id, locale);
0692:                    }
0693:
0694:                    return prefix + suffix;
0695:                }
0696:            }
0697:
0698:            public void TestLocale() {
0699:                ICULocaleService service = new ICULocaleService("test locale");
0700:                service.registerObject("root", ULocale.ROOT);
0701:                service.registerObject("german", "de");
0702:                service.registerObject("german_Germany", ULocale.GERMANY);
0703:                service.registerObject("japanese", "ja");
0704:                service.registerObject("japanese_Japan", ULocale.JAPAN);
0705:
0706:                Object target = service.get("de_US");
0707:                confirmEqual("test de_US", "german", target);
0708:
0709:                ULocale de = new ULocale("de");
0710:                ULocale de_US = new ULocale("de_US");
0711:
0712:                target = service.get(de_US);
0713:                confirmEqual("test de_US 2", "german", target);
0714:
0715:                target = service.get(de_US, LocaleKey.KIND_ANY);
0716:                confirmEqual("test de_US 3", "german", target);
0717:
0718:                target = service.get(de_US, 1234);
0719:                confirmEqual("test de_US 4", "german", target);
0720:
0721:                ULocale[] actualReturn = new ULocale[1];
0722:                target = service.get(de_US, actualReturn);
0723:                confirmEqual("test de_US 5", "german", target);
0724:                confirmEqual("test de_US 6", actualReturn[0], de);
0725:
0726:                actualReturn[0] = null;
0727:                target = service.get(de_US, LocaleKey.KIND_ANY, actualReturn);
0728:                confirmEqual("test de_US 7", actualReturn[0], de);
0729:
0730:                actualReturn[0] = null;
0731:                target = service.get(de_US, 1234, actualReturn);
0732:                confirmEqual("test de_US 8", "german", target);
0733:                confirmEqual("test de_US 9", actualReturn[0], de);
0734:
0735:                service.registerObject("one/de_US", de_US, 1);
0736:                service.registerObject("two/de_US", de_US, 2);
0737:
0738:                target = service.get(de_US, 1);
0739:                confirmEqual("test de_US kind 1", "one/de_US", target);
0740:
0741:                target = service.get(de_US, 2);
0742:                confirmEqual("test de_US kind 2", "two/de_US", target);
0743:
0744:                target = service.get(de_US);
0745:                confirmEqual("test de_US kind 3", "german", target);
0746:
0747:                LocaleKey lkey = LocaleKey.createWithCanonicalFallback("en",
0748:                        null, 1234);
0749:                logln("lkey prefix: " + lkey.prefix());
0750:                logln("lkey descriptor: " + lkey.currentDescriptor());
0751:                logln("lkey current locale: " + lkey.currentLocale());
0752:
0753:                lkey.fallback();
0754:                logln("lkey descriptor 2: " + lkey.currentDescriptor());
0755:
0756:                lkey.fallback();
0757:                logln("lkey descriptor 3: " + lkey.currentDescriptor());
0758:
0759:                target = service.get("za_PPP");
0760:                confirmEqual("test zappp", "root", target);
0761:
0762:                ULocale loc = ULocale.getDefault();
0763:                ULocale.setDefault(ULocale.JAPANESE);
0764:                target = service.get("za_PPP");
0765:                confirmEqual("test with ja locale", "japanese", target);
0766:
0767:                Set ids = service.getVisibleIDs();
0768:                for (Iterator iter = ids.iterator(); iter.hasNext();) {
0769:                    logln("id: " + iter.next());
0770:                }
0771:
0772:                ULocale.setDefault(loc);
0773:                ids = service.getVisibleIDs();
0774:                for (Iterator iter = ids.iterator(); iter.hasNext();) {
0775:                    logln("id: " + iter.next());
0776:                }
0777:
0778:                target = service.get("za_PPP");
0779:                confirmEqual("test with en locale", "root", target);
0780:
0781:                ULocale[] locales = service.getAvailableULocales();
0782:                confirmIdentical("test available locales", locales.length, 6);
0783:                logln("locales: ");
0784:                for (int i = 0; i < locales.length; ++i) {
0785:                    log("\n  [" + i + "] " + locales[i]);
0786:                }
0787:                logln(" ");
0788:
0789:                service.registerFactory(new ICUResourceBundleFactory());
0790:                target = service.get(ULocale.JAPAN);
0791:
0792:                {
0793:                    int n = 0;
0794:                    List factories = service.factories();
0795:                    Iterator iter = factories.iterator();
0796:                    while (iter.hasNext()) {
0797:                        logln("[" + n++ + "] " + iter.next());
0798:                    }
0799:                }
0800:
0801:                // list only the english display names for es, in reverse order
0802:                // since we're using locale keys, we should get all and only the es locales
0803:                // hmmm, the default toString function doesn't print in sorted order for TreeMap
0804:                {
0805:                    SortedMap map = service.getDisplayNames(ULocale.US,
0806:                            new Comparator() {
0807:                                public int compare(Object lhs, Object rhs) {
0808:                                    return -String.CASE_INSENSITIVE_ORDER
0809:                                            .compare((String) lhs, (String) rhs);
0810:                                }
0811:                            }, "es");
0812:
0813:                    logln("es display names in reverse order " + map);
0814:                }
0815:            }
0816:
0817:            public void TestWrapFactory() {
0818:                final String greeting = "Hello There";
0819:                final String greetingID = "greeting";
0820:
0821:                ICUService service = new ICUService("wrap");
0822:                service.registerObject(greeting, greetingID);
0823:
0824:                logln("test one: " + service.get(greetingID));
0825:
0826:                class WrapFactory implements  Factory {
0827:                    public Object create(Key key, ICUService service) {
0828:                        if (key.currentID().equals(greetingID)) {
0829:                            Object previous = service.getKey(key, null, this );
0830:                            return "A different greeting: \"" + previous + "\"";
0831:                        }
0832:                        return null;
0833:                    }
0834:
0835:                    public void updateVisibleIDs(Map result) {
0836:                        result.put("greeting", this );
0837:                    }
0838:
0839:                    public String getDisplayName(String id, ULocale locale) {
0840:                        return "wrap '" + id + "'";
0841:                    }
0842:                }
0843:                service.registerFactory(new WrapFactory());
0844:
0845:                confirmEqual("wrap test: ", service.get(greetingID),
0846:                        "A different greeting: \"" + greeting + "\"");
0847:            }
0848:
0849:            // misc coverage tests
0850:            public void TestCoverage() {
0851:                // Key
0852:                Key key = new Key("foobar");
0853:                logln("ID: " + key.id());
0854:                logln("canonicalID: " + key.canonicalID());
0855:                logln("currentID: " + key.currentID());
0856:                logln("has fallback: " + key.fallback());
0857:
0858:                // SimpleFactory
0859:                Object obj = new Object();
0860:                SimpleFactory sf = new SimpleFactory(obj, "object");
0861:                try {
0862:                    sf = new SimpleFactory(null, null);
0863:                    errln("didn't throw exception");
0864:                } catch (IllegalArgumentException e) {
0865:                    logln("OK: " + e.getMessage());
0866:                } catch (Exception e) {
0867:                    errln("threw wrong exception" + e);
0868:                }
0869:                logln(sf.getDisplayName("object", null));
0870:
0871:                // ICUService
0872:                ICUService service = new ICUService();
0873:                service.registerFactory(sf);
0874:
0875:                try {
0876:                    service.get(null, null);
0877:                    errln("didn't throw exception");
0878:                } catch (NullPointerException e) {
0879:                    logln("OK: " + e.getMessage());
0880:                }
0881:                /*
0882:                catch (Exception e) {
0883:                errln("threw wrong exception" + e);
0884:                }
0885:                 */
0886:                try {
0887:                    service.registerFactory(null);
0888:                    errln("didn't throw exception");
0889:                } catch (NullPointerException e) {
0890:                    logln("OK: " + e.getMessage());
0891:                } catch (Exception e) {
0892:                    errln("threw wrong exception" + e);
0893:                }
0894:
0895:                try {
0896:                    service.unregisterFactory(null);
0897:                    errln("didn't throw exception");
0898:                } catch (NullPointerException e) {
0899:                    logln("OK: " + e.getMessage());
0900:                } catch (Exception e) {
0901:                    errln("threw wrong exception" + e);
0902:                }
0903:
0904:                logln("object is: " + service.get("object"));
0905:
0906:                logln("stats: " + service.stats());
0907:
0908:                // ICURWLock
0909:
0910:                ICURWLock rwlock = new ICURWLock();
0911:                rwlock.acquireRead();
0912:                rwlock.releaseRead();
0913:
0914:                rwlock.acquireWrite();
0915:                rwlock.releaseWrite();
0916:                logln("stats: " + rwlock.getStats());
0917:                logln("stats: " + rwlock.clearStats());
0918:                rwlock.acquireRead();
0919:                rwlock.releaseRead();
0920:                rwlock.acquireWrite();
0921:                rwlock.releaseWrite();
0922:                logln("stats: " + rwlock.getStats());
0923:
0924:                try {
0925:                    rwlock.releaseRead();
0926:                    errln("no error thrown");
0927:                } catch (IllegalStateException e) {
0928:                    logln("OK: " + e.getMessage());
0929:                }
0930:
0931:                try {
0932:                    rwlock.releaseWrite();
0933:                    errln("no error thrown");
0934:                } catch (IllegalStateException e) {
0935:                    logln("OK: " + e.getMessage());
0936:                }
0937:
0938:                // ICULocaleService
0939:
0940:                // LocaleKey
0941:
0942:                // LocaleKey lkey = LocaleKey.create("en_US", "ja_JP");
0943:                // lkey = LocaleKey.create(null, null);
0944:                LocaleKey lkey = LocaleKey.createWithCanonicalFallback("en_US",
0945:                        "ja_JP");
0946:                logln("lkey: " + lkey);
0947:
0948:                lkey = LocaleKey.createWithCanonicalFallback(null, null);
0949:                logln("lkey from null,null: " + lkey);
0950:
0951:                // LocaleKeyFactory
0952:                LocaleKeyFactory lkf = new LKFSubclass(false);
0953:                logln("lkf: " + lkf);
0954:                logln("obj: " + lkf.create(lkey, null));
0955:                logln(lkf.getDisplayName("foo", null));
0956:                logln(lkf.getDisplayName("bar", null));
0957:                lkf.updateVisibleIDs(new HashMap());
0958:
0959:                LocaleKeyFactory invisibleLKF = new LKFSubclass(false);
0960:                logln("obj: " + invisibleLKF.create(lkey, null));
0961:                logln(invisibleLKF.getDisplayName("foo", null));
0962:                logln(invisibleLKF.getDisplayName("bar", null));
0963:                invisibleLKF.updateVisibleIDs(new HashMap());
0964:
0965:                // ResourceBundleFactory
0966:                ICUResourceBundleFactory rbf = new ICUResourceBundleFactory();
0967:                logln("RB: " + rbf.create(lkey, null));
0968:
0969:                // ICUNotifier
0970:                ICUNotifier nf = new ICUNSubclass();
0971:                try {
0972:                    nf.addListener(null);
0973:                    errln("added null listener");
0974:                } catch (NullPointerException e) {
0975:                    logln(e.getMessage());
0976:                } catch (Exception e) {
0977:                    errln("got wrong exception");
0978:                }
0979:
0980:                try {
0981:                    nf.addListener(new WrongListener());
0982:                    errln("added wrong listener");
0983:                } catch (IllegalStateException e) {
0984:                    logln(e.getMessage());
0985:                } catch (Exception e) {
0986:                    errln("got wrong exception");
0987:                }
0988:
0989:                try {
0990:                    nf.removeListener(null);
0991:                    errln("removed null listener");
0992:                } catch (NullPointerException e) {
0993:                    logln(e.getMessage());
0994:                } catch (Exception e) {
0995:                    errln("got wrong exception");
0996:                }
0997:
0998:                nf.removeListener(new MyListener());
0999:                nf.notifyChanged();
1000:                nf.addListener(new MyListener());
1001:                nf.removeListener(new MyListener());
1002:            }
1003:
1004:            static class MyListener implements  EventListener {
1005:            }
1006:
1007:            static class WrongListener implements  EventListener {
1008:            }
1009:
1010:            static class ICUNSubclass extends ICUNotifier {
1011:                public boolean acceptsListener(EventListener l) {
1012:                    return l instanceof  MyListener;
1013:                }
1014:
1015:                // not used, just needed to implement abstract base
1016:                ///CLOVER:OFF
1017:                public void notifyListener(EventListener l) {
1018:                }
1019:                ///CLOVER:ON
1020:            }
1021:
1022:            static class LKFSubclass extends LocaleKeyFactory {
1023:                LKFSubclass(boolean visible) {
1024:                    super (visible ? VISIBLE : INVISIBLE);
1025:                }
1026:
1027:                protected Set getSupportedIDs() {
1028:                    return Collections.EMPTY_SET;
1029:                }
1030:            }
1031:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.