Source Code Cross Referenced for CollationAPITest.java in  » Internationalization-Localization » icu4j » com » ibm » icu » dev » test » collator » 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.collator 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         *******************************************************************************
0003:         * Copyright (C) 2002-2006, International Business Machines Corporation and         *
0004:         * others. All Rights Reserved.                                                *
0005:         *******************************************************************************
0006:         */
0007:
0008:        /** 
0009:         * Port From:   ICU4C v2.1 : collate/CollationAPITest
0010:         * Source File: $ICU4CRoot/source/test/intltest/apicoll.cpp
0011:         **/package com.ibm.icu.dev.test.collator;
0012:
0013:        import java.util.Locale;
0014:        import java.util.Arrays;
0015:        import java.text.CharacterIterator;
0016:        import java.text.StringCharacterIterator;
0017:        import com.ibm.icu.dev.test.*;
0018:        import com.ibm.icu.text.*;
0019:        import com.ibm.icu.util.ULocale;
0020:        import com.ibm.icu.util.VersionInfo;
0021:        import java.util.MissingResourceException;
0022:
0023:        public class CollationAPITest extends TestFmwk {
0024:            public static void main(String[] args) throws Exception {
0025:                new CollationAPITest().run(args);
0026:                //new CollationAPITest().TestGetTailoredSet();
0027:            }
0028:
0029:            /**
0030:             * This tests the collation key related APIs.
0031:             * - constructor/destructor
0032:             * - Collator.getCollationKey
0033:             * - == and != operators
0034:             * - comparison between collation keys
0035:             * - creating collation key with a byte array and vice versa
0036:             */
0037:            public void TestCollationKey() {
0038:                logln("testing CollationKey begins...");
0039:                Collator col = Collator.getInstance();
0040:                col.setStrength(Collator.TERTIARY);
0041:
0042:                String test1 = "Abcda";
0043:                String test2 = "abcda";
0044:
0045:                logln("Testing weird arguments");
0046:                CollationKey sortk1 = col.getCollationKey("");
0047:                // key gets reset here
0048:                byte[] bytes = sortk1.toByteArray();
0049:                doAssert(bytes.length == 3 && bytes[0] == 1 && bytes[1] == 1
0050:                        && bytes[2] == 0,
0051:                        "Empty string should return an empty collation key");
0052:                // bogus key returned here
0053:                sortk1 = col.getCollationKey(null);
0054:                doAssert(sortk1 == null,
0055:                        "Error code should return bogus collation key");
0056:
0057:                logln("Use tertiary comparison level testing ....");
0058:                sortk1 = col.getCollationKey(test1);
0059:                CollationKey sortk2 = col.getCollationKey(test2);
0060:                Object sortk3 = sortk2;
0061:                doAssert((sortk1.compareTo(sortk2)) > 0
0062:                        && (sortk1.compareTo(sortk3)) > 0,
0063:                        "Result should be \"Abcda\" >>> \"abcda\"");
0064:
0065:                CollationKey sortkNew;
0066:                sortkNew = sortk1;
0067:                doAssert(!(sortk1.equals(sortk2)) && !(sortk1.equals(sortk3)),
0068:                        "The sort keys should be different");
0069:                doAssert((sortk1.hashCode() != sortk2.hashCode()),
0070:                        "sort key hashCode() failed");
0071:                doAssert((sortk1.equals(sortkNew)),
0072:                        "The sort keys assignment failed");
0073:                doAssert((sortk1.hashCode() == sortkNew.hashCode()),
0074:                        "sort key hashCode() failed");
0075:
0076:                // check invaliad comparisons
0077:                Object fake = "fake";
0078:                try {
0079:                    sortk1.compareTo(fake);
0080:                    errln("Non-CollationKey comparison");
0081:                } catch (Exception e) {
0082:                    logln("PASS: Non-CollationKey comparison failed as expected");
0083:                }
0084:                if (sortk1.equals(fake)) {
0085:                    errln("Non-CollationKey comparison");
0086:                }
0087:
0088:                // port from apicoll
0089:                try {
0090:                    col = Collator.getInstance();
0091:                } catch (Exception e) {
0092:                    errln("Collator.getInstance() failed");
0093:                }
0094:                if (col.getStrength() != Collator.TERTIARY) {
0095:                    errln("Default collation did not have tertiary strength");
0096:                }
0097:
0098:                // Need to use identical strength
0099:                col.setStrength(Collator.IDENTICAL);
0100:
0101:                byte key2compat[] = {
0102:                        // 3.6 key, UCA 5.0
0103:                        (byte) 0x29, (byte) 0x2b, (byte) 0x2d, (byte) 0x2f,
0104:                        (byte) 0x29, (byte) 0x01, (byte) 0x09, (byte) 0x01,
0105:                        (byte) 0x09, (byte) 0x01, (byte) 0x28, (byte) 0x01,
0106:                        (byte) 0x92, (byte) 0x93, (byte) 0x94, (byte) 0x95,
0107:                        (byte) 0x92, (byte) 0x00
0108:
0109:                // 3.4 key UCA 4.1
0110:                /*
0111:                (byte) 0x28, (byte) 0x2a, (byte) 0x2c, 
0112:                (byte) 0x2e, (byte) 0x28, (byte) 0x01, 
0113:                (byte) 0x09, (byte) 0x01, (byte) 0x09, 
0114:                (byte) 0x01, (byte) 0x27, (byte) 0x01, 
0115:                (byte) 0x92, (byte) 0x93, (byte) 0x94, 
0116:                (byte) 0x95, (byte) 0x92, (byte) 0x00        
0117:                 */
0118:
0119:                //          2.6.1 key
0120:                /*
0121:                0x26, 0x28, 0x2A, 0x2C, 0x26, 0x01, 
0122:                0x09, 0x01, 0x09, 0x01, 0x25, 0x01, 
0123:                0x92, 0x93, 0x94, 0x95, 0x92, 0x00 
0124:                 */
0125:
0126:                // 2.2 key
0127:                /*
0128:                0x1D, 0x1F, 0x21, 0x23, 0x1D, 0x01,
0129:                0x09, 0x01, 0x09, 0x01, 0x1C, 0x01,
0130:                0x92, 0x93, 0x94, 0x95, 0x92, 0x00
0131:                 */
0132:
0133:                // 2.0 key
0134:                /*
0135:                0x19, 0x1B, 0x1D, 0x1F, 0x19,
0136:                0x01, 0x09, 0x01, 0x09, 0x01,
0137:                0x18, 0x01,
0138:                0x92, 0x93, 0x94, 0x95, 0x92,
0139:                0x00
0140:                 */
0141:
0142:                // 1.8.1 key.
0143:                /*
0144:                0x19, 0x1B, 0x1D, 0x1F, 0x19,
0145:                0x01, 0x0A, 0x01, 0x0A, 0x01,
0146:                0x92, 0x93, 0x94, 0x95, 0x92,
0147:                0x00 
0148:                 */
0149:                };
0150:
0151:                CollationKey key1 = col.getCollationKey(test1);
0152:                CollationKey key2 = col.getCollationKey(test2);
0153:                CollationKey key3 = col.getCollationKey(test2);
0154:
0155:                doAssert(key1.compareTo(key2) > 0,
0156:                        "Result should be \"Abcda\" > \"abcda\"");
0157:                doAssert(key2.compareTo(key1) < 0,
0158:                        "Result should be \"abcda\" < \"Abcda\"");
0159:                doAssert(key2.compareTo(key3) == 0,
0160:                        "Result should be \"abcda\" ==  \"abcda\"");
0161:
0162:                doAssert(Arrays.equals(key2.toByteArray(), key2compat),
0163:                        "Binary format for 'abcda' sortkey different for identical strength!");
0164:
0165:                logln("Use secondary comparision level testing ...");
0166:                col.setStrength(Collator.SECONDARY);
0167:
0168:                key1 = col.getCollationKey(test1);
0169:                key2 = col.getCollationKey(test2);
0170:                key3 = col.getCollationKey(test2);
0171:
0172:                doAssert(key1.compareTo(key2) == 0,
0173:                        "Result should be \"Abcda\" == \"abcda\"");
0174:                doAssert(key2.compareTo(key3) == 0,
0175:                        "Result should be \"abcda\" ==  \"abcda\"");
0176:
0177:                byte tempkey[] = key2.toByteArray();
0178:                byte subkey2compat[] = new byte[tempkey.length];
0179:                System.arraycopy(key2compat, 0, subkey2compat, 0,
0180:                        tempkey.length);
0181:                subkey2compat[subkey2compat.length - 1] = 0;
0182:                doAssert(Arrays.equals(tempkey, subkey2compat),
0183:                        "Binary format for 'abcda' sortkey different for secondary strength!");
0184:
0185:                logln("testing sortkey ends...");
0186:            }
0187:
0188:            public void TestRawCollationKey() {
0189:                // testing constructors
0190:                RawCollationKey key = new RawCollationKey();
0191:                if (key.bytes != null || key.size != 0) {
0192:                    errln("Empty default constructor expected to leave the bytes null "
0193:                            + "and size 0");
0194:                }
0195:                byte array[] = new byte[128];
0196:                key = new RawCollationKey(array);
0197:                if (key.bytes != array || key.size != 0) {
0198:                    errln("Constructor taking an array expected to adopt it and "
0199:                            + "retaining its size 0");
0200:                }
0201:                try {
0202:                    key = new RawCollationKey(array, 129);
0203:                    errln("Constructor taking an array and a size > array.length "
0204:                            + "expected to throw an exception");
0205:                } catch (IndexOutOfBoundsException e) {
0206:                    logln("PASS: Constructor failed as expected");
0207:                }
0208:                try {
0209:                    key = new RawCollationKey(array, -1);
0210:                    errln("Constructor taking an array and a size < 0 "
0211:                            + "expected to throw an exception");
0212:                } catch (IndexOutOfBoundsException e) {
0213:                    logln("PASS: Constructor failed as expected");
0214:                }
0215:                key = new RawCollationKey(array, array.length >> 1);
0216:                if (key.bytes != array || key.size != (array.length >> 1)) {
0217:                    errln("Constructor taking an array and a size, "
0218:                            + "expected to adopt it and take the size specified");
0219:                }
0220:                key = new RawCollationKey(10);
0221:                if (key.bytes == null || key.bytes.length != 10
0222:                        || key.size != 0) {
0223:                    errln("Constructor taking a specified capacity expected to "
0224:                            + "create a new internal byte array with length 10 and "
0225:                            + "retain size 0");
0226:                }
0227:            }
0228:
0229:            void doAssert(boolean conditions, String message) {
0230:                if (!conditions) {
0231:                    errln(message);
0232:                }
0233:            }
0234:
0235:            /**
0236:             * This tests the comparison convenience methods of a collator object.
0237:             * - greater than
0238:             * - greater than or equal to
0239:             * - equal to
0240:             */
0241:            public void TestCompare() {
0242:                logln("The compare tests begin : ");
0243:                Collator col = Collator.getInstance(Locale.ENGLISH);
0244:
0245:                String test1 = "Abcda";
0246:                String test2 = "abcda";
0247:                logln("Use tertiary comparison level testing ....");
0248:
0249:                Object obj1 = test1;
0250:                Object obj2 = test2;
0251:                doAssert((col.compare(obj1, obj2) > 0),
0252:                        "Result should be \"Abcda\" != \"abcda\"");
0253:                doAssert((!col.equals(test1, test2)),
0254:                        "Result should be \"Abcda\" != \"abcda\"");
0255:                doAssert((col.compare(test1, test2) > 0),
0256:                        "Result should be \"Abcda\" >>> \"abcda\"");
0257:
0258:                col.setStrength(Collator.SECONDARY);
0259:                logln("Use secondary comparison level testing ....");
0260:
0261:                doAssert((col.compare(obj1, obj2) == 0),
0262:                        "Result should be \"Abcda\" == \"abcda\"");
0263:                doAssert((col.equals(test1, test2)),
0264:                        "Result should be \"Abcda\" == \"abcda\"");
0265:                doAssert((col.compare(test1, test2) == 0),
0266:                        "Result should be \"Abcda\" == \"abcda\"");
0267:
0268:                col.setStrength(Collator.PRIMARY);
0269:                logln("Use primary comparison level testing ....");
0270:
0271:                doAssert((col.compare(obj1, obj2) == 0),
0272:                        "Result should be \"Abcda\" == \"abcda\"");
0273:                doAssert((col.equals(test1, test2)),
0274:                        "Result should be \"Abcda\" == \"abcda\"");
0275:                doAssert((col.compare(test1, test2) == 0),
0276:                        "Result should be \"Abcda\" == \"abcda\"");
0277:                logln("The compare tests end.");
0278:
0279:                Integer die = new Integer(1);
0280:                try {
0281:                    col.compare(die, test1);
0282:                    errln("Non-Strings should fail col.compare(Object, Object)");
0283:                } catch (Exception e) {
0284:                    logln("PASS: Non-Strings comparison failed as expected");
0285:                }
0286:            }
0287:
0288:            /**
0289:             * Tests decomposition setting
0290:             */
0291:            public void TestDecomposition() {
0292:                Collator en_US = null, el_GR = null, vi_VN = null;
0293:
0294:                en_US = Collator.getInstance(new Locale("en", "US"));
0295:                el_GR = Collator.getInstance(new Locale("el", "GR"));
0296:                vi_VN = Collator.getInstance(new Locale("vi", "VN"));
0297:
0298:                // there is no reason to have canonical decomposition in en_US OR default locale */
0299:                if (vi_VN.getDecomposition() != Collator.CANONICAL_DECOMPOSITION) {
0300:                    errln("vi_VN collation did not have cannonical decomposition for normalization!");
0301:                }
0302:
0303:                if (el_GR.getDecomposition() != Collator.CANONICAL_DECOMPOSITION) {
0304:                    errln("el_GR collation did not have cannonical decomposition for normalization!");
0305:                }
0306:
0307:                if (en_US.getDecomposition() != Collator.NO_DECOMPOSITION) {
0308:                    errln("en_US collation had cannonical decomposition for normalization!");
0309:                }
0310:            }
0311:
0312:            /**
0313:             * This tests the duplication of a collator object.
0314:             */
0315:            public void TestDuplicate() {
0316:                //Clone does not be implemented 
0317:                Collator col1 = Collator.getInstance(Locale.ENGLISH);
0318:
0319:                // Collator col2 = (Collator)col1.clone();
0320:                // doAssert(col1.equals(col2), "Cloned object is not equal to the orginal");
0321:                String ruleset = "< a, A < b, B < c, C < d, D, e, E";
0322:                RuleBasedCollator col3 = null;
0323:                try {
0324:                    col3 = new RuleBasedCollator(ruleset);
0325:                } catch (Exception e) {
0326:                    errln("Failure creating RuleBasedCollator with rule:"
0327:                            + ruleset);
0328:                    return;
0329:                }
0330:                doAssert(!col1.equals(col3),
0331:                        "Cloned object is equal to some dummy");
0332:                col3 = (RuleBasedCollator) col1;
0333:                doAssert(col1.equals(col3),
0334:                        "Copied object is not equal to the orginal");
0335:
0336:            }
0337:
0338:            /**
0339:             * This tests the CollationElementIterator related APIs.
0340:             * - creation of a CollationElementIterator object
0341:             * - == and != operators
0342:             * - iterating forward
0343:             * - reseting the iterator index
0344:             * - requesting the order properties(primary, secondary or tertiary)
0345:             */
0346:            public void TestElemIter() {
0347:                // logln("testing sortkey begins...");
0348:                Collator col = Collator.getInstance(Locale.ENGLISH);
0349:
0350:                String testString1 = "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?";
0351:                String testString2 = "Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?";
0352:                // logln("Constructors and comparison testing....");
0353:                CollationElementIterator iterator1 = ((RuleBasedCollator) col)
0354:                        .getCollationElementIterator(testString1);
0355:
0356:                CharacterIterator chariter = new StringCharacterIterator(
0357:                        testString1);
0358:                // copy ctor
0359:                CollationElementIterator iterator2 = ((RuleBasedCollator) col)
0360:                        .getCollationElementIterator(chariter);
0361:                UCharacterIterator uchariter = UCharacterIterator
0362:                        .getInstance(testString2);
0363:                CollationElementIterator iterator3 = ((RuleBasedCollator) col)
0364:                        .getCollationElementIterator(uchariter);
0365:
0366:                int offset = 0;
0367:                offset = iterator1.getOffset();
0368:                if (offset != 0) {
0369:                    errln("Error in getOffset for collation element iterator");
0370:                    return;
0371:                }
0372:                iterator1.setOffset(6);
0373:                iterator1.setOffset(0);
0374:                int order1, order2, order3;
0375:
0376:                order1 = iterator1.next();
0377:                doAssert(!(iterator1.equals(iterator2)),
0378:                        "The first iterator advance failed");
0379:                order2 = iterator2.next();
0380:
0381:                doAssert((iterator1.equals(iterator2)),
0382:                        "The second iterator advance failed");
0383:                doAssert((order1 == order2),
0384:                        "The order result should be the same");
0385:                order3 = iterator3.next();
0386:
0387:                doAssert(
0388:                        (CollationElementIterator.primaryOrder(order1) == CollationElementIterator
0389:                                .primaryOrder(order3)),
0390:                        "The primary orders should be the same");
0391:                doAssert(
0392:                        (CollationElementIterator.secondaryOrder(order1) == CollationElementIterator
0393:                                .secondaryOrder(order3)),
0394:                        "The secondary orders should be the same");
0395:                doAssert(
0396:                        (CollationElementIterator.tertiaryOrder(order1) == CollationElementIterator
0397:                                .tertiaryOrder(order3)),
0398:                        "The tertiary orders should be the same");
0399:
0400:                order1 = iterator1.next();
0401:                order3 = iterator3.next();
0402:
0403:                doAssert(
0404:                        (CollationElementIterator.primaryOrder(order1) == CollationElementIterator
0405:                                .primaryOrder(order3)),
0406:                        "The primary orders should be identical");
0407:                doAssert(
0408:                        (CollationElementIterator.tertiaryOrder(order1) != CollationElementIterator
0409:                                .tertiaryOrder(order3)),
0410:                        "The tertiary orders should be different");
0411:
0412:                order1 = iterator1.next();
0413:                order3 = iterator3.next();
0414:                // invalid test wrong in UCA
0415:                // doAssert((CollationElementIterator.secondaryOrder(order1) != 
0416:                //    CollationElementIterator.secondaryOrder(order3)), "The secondary orders should not be the same");
0417:
0418:                doAssert((order1 != CollationElementIterator.NULLORDER),
0419:                        "Unexpected end of iterator reached");
0420:
0421:                iterator1.reset();
0422:                iterator2.reset();
0423:                iterator3.reset();
0424:                order1 = iterator1.next();
0425:
0426:                doAssert(!(iterator1.equals(iterator2)),
0427:                        "The first iterator advance failed");
0428:
0429:                order2 = iterator2.next();
0430:
0431:                doAssert((iterator1.equals(iterator2)),
0432:                        "The second iterator advance failed");
0433:                doAssert((order1 == order2),
0434:                        "The order result should be the same");
0435:
0436:                order3 = iterator3.next();
0437:
0438:                doAssert(
0439:                        (CollationElementIterator.primaryOrder(order1) == CollationElementIterator
0440:                                .primaryOrder(order3)),
0441:                        "The primary orders should be the same");
0442:                doAssert(
0443:                        (CollationElementIterator.secondaryOrder(order1) == CollationElementIterator
0444:                                .secondaryOrder(order3)),
0445:                        "The secondary orders should be the same");
0446:                doAssert(
0447:                        (CollationElementIterator.tertiaryOrder(order1) == CollationElementIterator
0448:                                .tertiaryOrder(order3)),
0449:                        "The tertiary orders should be the same");
0450:
0451:                order1 = iterator1.next();
0452:                order2 = iterator2.next();
0453:                order3 = iterator3.next();
0454:
0455:                doAssert(
0456:                        (CollationElementIterator.primaryOrder(order1) == CollationElementIterator
0457:                                .primaryOrder(order3)),
0458:                        "The primary orders should be identical");
0459:                doAssert(
0460:                        (CollationElementIterator.tertiaryOrder(order1) != CollationElementIterator
0461:                                .tertiaryOrder(order3)),
0462:                        "The tertiary orders should be different");
0463:
0464:                order1 = iterator1.next();
0465:                order3 = iterator3.next();
0466:
0467:                // obsolete invalid test, removed
0468:                // doAssert((CollationElementIterator.secondaryOrder(order1) != 
0469:                //    CollationElementIterator.secondaryOrder(order3)), "The secondary orders should not be the same");
0470:                doAssert((order1 != CollationElementIterator.NULLORDER),
0471:                        "Unexpected end of iterator reached");
0472:                doAssert(!(iterator2.equals(iterator3)),
0473:                        "The iterators should be different");
0474:                logln("testing CollationElementIterator ends...");
0475:            }
0476:
0477:            /**
0478:             * This tests the hashCode method of a collator object.
0479:             */
0480:            public void TestHashCode() {
0481:                logln("hashCode tests begin.");
0482:                Collator col1 = Collator.getInstance(Locale.ENGLISH);
0483:
0484:                Collator col2 = null;
0485:                Locale dk = new Locale("da", "DK", "");
0486:                try {
0487:                    col2 = Collator.getInstance(dk);
0488:                } catch (Exception e) {
0489:                    errln("Danish collation creation failed.");
0490:                    return;
0491:                }
0492:
0493:                Collator col3 = null;
0494:                try {
0495:                    col3 = Collator.getInstance(Locale.ENGLISH);
0496:                } catch (Exception e) {
0497:                    errln("2nd default collation creation failed.");
0498:                    return;
0499:                }
0500:
0501:                logln("Collator.hashCode() testing ...");
0502:
0503:                doAssert(col1.hashCode() != col2.hashCode(),
0504:                        "Hash test1 result incorrect");
0505:                doAssert(!(col1.hashCode() == col2.hashCode()),
0506:                        "Hash test2 result incorrect");
0507:                doAssert(col1.hashCode() == col3.hashCode(),
0508:                        "Hash result not equal");
0509:
0510:                logln("hashCode tests end.");
0511:
0512:                String test1 = "Abcda";
0513:                String test2 = "abcda";
0514:
0515:                CollationKey sortk1, sortk2, sortk3;
0516:
0517:                sortk1 = col3.getCollationKey(test1);
0518:                sortk2 = col3.getCollationKey(test2);
0519:                sortk3 = col3.getCollationKey(test2);
0520:
0521:                doAssert(sortk1.hashCode() != sortk2.hashCode(),
0522:                        "Hash test1 result incorrect");
0523:                doAssert(sortk2.hashCode() == sortk3.hashCode(),
0524:                        "Hash result not equal");
0525:            }
0526:
0527:            /**
0528:             * This tests the properties of a collator object.
0529:             * - constructor
0530:             * - factory method getInstance
0531:             * - compare and getCollationKey
0532:             * - get/set decomposition mode and comparison level
0533:             */
0534:            public void TestProperty() {
0535:                /* 
0536:                  All the collations have the same version in an ICU
0537:                  version.
0538:                  ICU 2.0 currVersionArray = {0x18, 0xC0, 0x02, 0x02};
0539:                  ICU 2.1 currVersionArray = {0x19, 0x00, 0x03, 0x03};
0540:                  ICU 2.8 currVersionArray = {0x29, 0x80, 0x00, 0x04};          
0541:                 */
0542:                logln("The property tests begin : ");
0543:                logln("Test ctors : ");
0544:                Collator col = Collator.getInstance(Locale.ENGLISH);
0545:
0546:                logln("Test getVersion");
0547:                VersionInfo expectedVersion = VersionInfo.getInstance(0x31,
0548:                        0xC0, 0x00, 0x05);
0549:                doAssert(col.getVersion().equals(expectedVersion),
0550:                        "Expected version " + expectedVersion.toString()
0551:                                + " got " + col.getVersion().toString());
0552:
0553:                logln("Test getUCAVersion");
0554:                VersionInfo expectedUCAVersion = VersionInfo.getInstance(0x05,
0555:                        0, 0, 0);
0556:                doAssert(col.getUCAVersion().equals(expectedUCAVersion),
0557:                        "Expected UCA version " + expectedUCAVersion.toString()
0558:                                + " got " + col.getUCAVersion().toString());
0559:
0560:                doAssert((col.compare("ab", "abc") < 0),
0561:                        "ab < abc comparison failed");
0562:                doAssert((col.compare("ab", "AB") < 0),
0563:                        "ab < AB comparison failed");
0564:                doAssert((col.compare("blackbird", "black-bird") > 0),
0565:                        "black-bird > blackbird comparison failed");
0566:                doAssert((col.compare("black bird", "black-bird") < 0),
0567:                        "black bird > black-bird comparison failed");
0568:                doAssert((col.compare("Hello", "hello") > 0),
0569:                        "Hello > hello comparison failed");
0570:
0571:                logln("Test ctors ends.");
0572:
0573:                logln("testing Collator.getStrength() method ...");
0574:                doAssert((col.getStrength() == Collator.TERTIARY),
0575:                        "collation object has the wrong strength");
0576:                doAssert((col.getStrength() != Collator.PRIMARY),
0577:                        "collation object's strength is primary difference");
0578:
0579:                logln("testing Collator.setStrength() method ...");
0580:                col.setStrength(Collator.SECONDARY);
0581:                doAssert((col.getStrength() != Collator.TERTIARY),
0582:                        "collation object's strength is secondary difference");
0583:                doAssert((col.getStrength() != Collator.PRIMARY),
0584:                        "collation object's strength is primary difference");
0585:                doAssert((col.getStrength() == Collator.SECONDARY),
0586:                        "collation object has the wrong strength");
0587:
0588:                logln("testing Collator.setDecomposition() method ...");
0589:                col.setDecomposition(Collator.NO_DECOMPOSITION);
0590:                doAssert(
0591:                        (col.getDecomposition() != Collator.CANONICAL_DECOMPOSITION),
0592:                        "Decomposition mode != Collator.CANONICAL_DECOMPOSITION");
0593:                doAssert((col.getDecomposition() == Collator.NO_DECOMPOSITION),
0594:                        "Decomposition mode = Collator.NO_DECOMPOSITION");
0595:
0596:                RuleBasedCollator rcol = (RuleBasedCollator) Collator
0597:                        .getInstance(new Locale("da", "DK"));
0598:                doAssert(rcol.getRules().length() != 0,
0599:                        "da_DK rules does not have length 0");
0600:
0601:                try {
0602:                    col = Collator.getInstance(Locale.FRENCH);
0603:                } catch (Exception e) {
0604:                    errln("Creating French collation failed.");
0605:                    return;
0606:                }
0607:
0608:                col.setStrength(Collator.PRIMARY);
0609:                logln("testing Collator.getStrength() method again ...");
0610:                doAssert((col.getStrength() != Collator.TERTIARY),
0611:                        "collation object has the wrong strength");
0612:                doAssert((col.getStrength() == Collator.PRIMARY),
0613:                        "collation object's strength is not primary difference");
0614:
0615:                logln("testing French Collator.setStrength() method ...");
0616:                col.setStrength(Collator.TERTIARY);
0617:                doAssert((col.getStrength() == Collator.TERTIARY),
0618:                        "collation object's strength is not tertiary difference");
0619:                doAssert((col.getStrength() != Collator.PRIMARY),
0620:                        "collation object's strength is primary difference");
0621:                doAssert((col.getStrength() != Collator.SECONDARY),
0622:                        "collation object's strength is secondary difference");
0623:
0624:            }
0625:
0626:            public void TestJunkCollator() {
0627:                logln("Create junk collation: ");
0628:                Locale abcd = new Locale("ab", "CD", "");
0629:
0630:                Collator junk = Collator.getInstance(abcd);
0631:                Collator col = Collator.getInstance();
0632:
0633:                String colrules = ((RuleBasedCollator) col).getRules();
0634:                String junkrules = ((RuleBasedCollator) junk).getRules();
0635:                doAssert(colrules == junkrules || colrules.equals(junkrules),
0636:                        "The default collation should be returned.");
0637:                Collator frCol = null;
0638:                try {
0639:                    frCol = Collator.getInstance(Locale.FRANCE);
0640:                } catch (Exception e) {
0641:                    errln("Creating French collator failed.");
0642:                    return;
0643:                }
0644:
0645:                doAssert(!(frCol.equals(junk)),
0646:                        "The junk is the same as the French collator.");
0647:                logln("Collator property test ended.");
0648:
0649:            }
0650:
0651:            /**
0652:             * This tests the RuleBasedCollator
0653:             * - constructor/destructor
0654:             * - getRules
0655:             */
0656:            public void TestRuleBasedColl() {
0657:                RuleBasedCollator col1 = null, col2 = null, col3 = null, col4 = null;
0658:
0659:                String ruleset1 = "&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E";
0660:                String ruleset2 = "&9 < a, A < b, B < c, C < d, D, e, E";
0661:                String ruleset3 = "&";
0662:
0663:                try {
0664:                    col1 = new RuleBasedCollator(ruleset1);
0665:                } catch (Exception e) {
0666:                    // only first error needs to be a warning since we exit function 
0667:                    warnln("RuleBased Collator creation failed.");
0668:                    return;
0669:                }
0670:
0671:                try {
0672:                    col2 = new RuleBasedCollator(ruleset2);
0673:                } catch (Exception e) {
0674:                    errln("RuleBased Collator creation failed.");
0675:                    return;
0676:                }
0677:
0678:                try {
0679:                    // empty rules fail
0680:                    col3 = new RuleBasedCollator(ruleset3);
0681:                    errln("Failure: Empty rules for the collator should fail");
0682:                    return;
0683:                } catch (MissingResourceException e) {
0684:                    warnln(e.getMessage());
0685:                } catch (Exception e) {
0686:                    logln("PASS: Empty rules for the collator failed as expected");
0687:                }
0688:
0689:                Locale locale = new Locale("aa", "AA");
0690:                try {
0691:                    col3 = (RuleBasedCollator) Collator.getInstance(locale);
0692:                } catch (Exception e) {
0693:                    errln("Fallback Collator creation failed.: %s");
0694:                    return;
0695:                }
0696:
0697:                try {
0698:                    col3 = (RuleBasedCollator) Collator.getInstance();
0699:                } catch (Exception e) {
0700:                    errln("Default Collator creation failed.: %s");
0701:                    return;
0702:                }
0703:
0704:                String rule1 = col1.getRules();
0705:                String rule2 = col2.getRules();
0706:                String rule3 = col3.getRules();
0707:
0708:                doAssert(!rule1.equals(rule2),
0709:                        "Default collator getRules failed");
0710:                doAssert(!rule2.equals(rule3),
0711:                        "Default collator getRules failed");
0712:                doAssert(!rule1.equals(rule3),
0713:                        "Default collator getRules failed");
0714:
0715:                try {
0716:                    col4 = new RuleBasedCollator(rule2);
0717:                } catch (Exception e) {
0718:                    errln("RuleBased Collator creation failed.");
0719:                    return;
0720:                }
0721:
0722:                String rule4 = col4.getRules();
0723:                doAssert(rule2.equals(rule4),
0724:                        "Default collator getRules failed");
0725:                // tests that modifier ! is always ignored
0726:                String exclamationrules = "!&a<b";
0727:                // java does not allow ! to be the start of the rule
0728:                String thaistr = "\u0e40\u0e01\u0e2d";
0729:                try {
0730:                    RuleBasedCollator col5 = new RuleBasedCollator(
0731:                            exclamationrules);
0732:                    RuleBasedCollator encol = (RuleBasedCollator) Collator
0733:                            .getInstance(Locale.ENGLISH);
0734:                    CollationElementIterator col5iter = col5
0735:                            .getCollationElementIterator(thaistr);
0736:                    CollationElementIterator encoliter = encol
0737:                            .getCollationElementIterator(thaistr);
0738:                    while (true) {
0739:                        // testing with en since thai has its own tailoring
0740:                        int ce = col5iter.next();
0741:                        int ce2 = encoliter.next();
0742:                        if (ce2 != ce) {
0743:                            errln("! modifier test failed");
0744:                        }
0745:                        if (ce == CollationElementIterator.NULLORDER) {
0746:                            break;
0747:                        }
0748:                    }
0749:                } catch (Exception e) {
0750:                    errln("RuleBased Collator creation failed for ! modifier.");
0751:                    return;
0752:                }
0753:            }
0754:
0755:            /**
0756:             * This tests the RuleBasedCollator
0757:             * - getRules
0758:             */
0759:            public void TestRules() {
0760:                RuleBasedCollator coll = (RuleBasedCollator) Collator
0761:                        .getInstance(new Locale("", "", "")); //root
0762:                // logln("PASS: RuleBased Collator creation passed");
0763:
0764:                String rules = coll.getRules();
0765:                if (rules != null && rules.length() != 0) {
0766:                    errln("Root tailored rules failed");
0767:                }
0768:            }
0769:
0770:            public void TestSafeClone() {
0771:                String test1 = "abCda";
0772:                String test2 = "abcda";
0773:
0774:                // one default collator & two complex ones 
0775:                RuleBasedCollator someCollators[] = {
0776:                        (RuleBasedCollator) Collator
0777:                                .getInstance(Locale.ENGLISH),
0778:                        (RuleBasedCollator) Collator.getInstance(Locale.KOREA),
0779:                        (RuleBasedCollator) Collator.getInstance(Locale.JAPAN) };
0780:                RuleBasedCollator someClonedCollators[] = new RuleBasedCollator[3];
0781:
0782:                // change orig & clone & make sure they are independent 
0783:
0784:                for (int index = 0; index < someCollators.length; index++) {
0785:                    try {
0786:                        someClonedCollators[index] = (RuleBasedCollator) someCollators[index]
0787:                                .clone();
0788:                    } catch (CloneNotSupportedException e) {
0789:                        errln("Error cloning collator");
0790:                    }
0791:
0792:                    someClonedCollators[index].setStrength(Collator.TERTIARY);
0793:                    someCollators[index].setStrength(Collator.PRIMARY);
0794:                    someClonedCollators[index].setCaseLevel(false);
0795:                    someCollators[index].setCaseLevel(false);
0796:
0797:                    doAssert(
0798:                            someClonedCollators[index].compare(test1, test2) > 0,
0799:                            "Result should be \"abCda\" >>> \"abcda\" ");
0800:                    doAssert(someCollators[index].compare(test1, test2) == 0,
0801:                            "Result should be \"abCda\" == \"abcda\" ");
0802:                }
0803:            }
0804:
0805:            public void TestGetTailoredSet() {
0806:                logln("testing getTailoredSet...");
0807:                String rules[] = { "&a < \u212b", "& S < \u0161 <<< \u0160", };
0808:                String data[][] = { { "\u212b", "A\u030a", "\u00c5" },
0809:                        { "\u0161", "s\u030C", "\u0160", "S\u030C" } };
0810:
0811:                int i = 0, j = 0;
0812:
0813:                RuleBasedCollator coll;
0814:                UnicodeSet set;
0815:
0816:                for (i = 0; i < rules.length; i++) {
0817:                    try {
0818:                        logln("Instantiating a collator from " + rules[i]);
0819:                        coll = new RuleBasedCollator(rules[i]);
0820:                        set = coll.getTailoredSet();
0821:                        logln("Got set: " + set.toPattern(true));
0822:                        if (set.size() != data[i].length) {
0823:                            errln("Tailored set size different (" + set.size()
0824:                                    + ") than expected (" + data[i].length
0825:                                    + ")");
0826:                        }
0827:                        for (j = 0; j < data[i].length; j++) {
0828:                            logln("Checking to see whether " + data[i][j]
0829:                                    + " is in set");
0830:                            if (!set.contains(data[i][j])) {
0831:                                errln("Tailored set doesn't contain "
0832:                                        + data[i][j] + "... It should");
0833:                            }
0834:                        }
0835:                    } catch (Exception e) {
0836:                        warnln("Couldn't open collator with rules " + rules[i]);
0837:                    }
0838:                }
0839:            }
0840:
0841:            /** 
0842:             * Simple test to see if Collator is subclassable
0843:             */
0844:            public void TestSubClass() {
0845:                class TestCollator extends Collator {
0846:                    public boolean equals(Object that) {
0847:                        return this  == that;
0848:                    }
0849:
0850:                    public int hashCode() {
0851:                        return 0;
0852:                    }
0853:
0854:                    public int compare(String source, String target) {
0855:                        return source.compareTo(target);
0856:                    }
0857:
0858:                    public CollationKey getCollationKey(String source) {
0859:                        return new CollationKey(source, getRawCollationKey(
0860:                                source, new RawCollationKey()));
0861:                    }
0862:
0863:                    public RawCollationKey getRawCollationKey(String source,
0864:                            RawCollationKey key) {
0865:                        byte temp1[] = source.getBytes();
0866:                        byte temp2[] = new byte[temp1.length + 1];
0867:                        System.arraycopy(temp1, 0, temp2, 0, temp1.length);
0868:                        temp2[temp1.length] = 0;
0869:                        if (key == null) {
0870:                            key = new RawCollationKey();
0871:                        }
0872:                        key.bytes = temp2;
0873:                        key.size = temp2.length;
0874:                        return key;
0875:                    }
0876:
0877:                    public void setVariableTop(int ce) {
0878:                    }
0879:
0880:                    public int setVariableTop(String str) {
0881:                        return 0;
0882:                    }
0883:
0884:                    public int getVariableTop() {
0885:                        return 0;
0886:                    }
0887:
0888:                    public VersionInfo getVersion() {
0889:                        return VersionInfo.getInstance(0);
0890:                    }
0891:
0892:                    public VersionInfo getUCAVersion() {
0893:                        return VersionInfo.getInstance(0);
0894:                    }
0895:                }
0896:
0897:                Collator col1 = new TestCollator();
0898:                Collator col2 = new TestCollator();
0899:                if (col1.equals(col2)) {
0900:                    errln("2 different instance of TestCollator should fail");
0901:                }
0902:                if (col1.hashCode() != col2.hashCode()) {
0903:                    errln("Every TestCollator has the same hashcode");
0904:                }
0905:                String abc = "abc";
0906:                String bcd = "bcd";
0907:                if (col1.compare(abc, bcd) != abc.compareTo(bcd)) {
0908:                    errln("TestCollator compare should be the same as the default "
0909:                            + "string comparison");
0910:                }
0911:                CollationKey key = col1.getCollationKey(abc);
0912:                byte temp1[] = abc.getBytes();
0913:                byte temp2[] = new byte[temp1.length + 1];
0914:                System.arraycopy(temp1, 0, temp2, 0, temp1.length);
0915:                temp2[temp1.length] = 0;
0916:                if (!java.util.Arrays.equals(key.toByteArray(), temp2)
0917:                        || !key.getSourceString().equals(abc)) {
0918:                    errln("TestCollator collationkey API is returning wrong values");
0919:                }
0920:                UnicodeSet set = col1.getTailoredSet();
0921:                if (!set.equals(new UnicodeSet(0, 0x10FFFF))) {
0922:                    errln("Error getting default tailored set");
0923:                }
0924:            }
0925:
0926:            /** 
0927:             * Simple test the collator setter and getters
0928:             */
0929:            public void TestSetGet() {
0930:                RuleBasedCollator collator = (RuleBasedCollator) Collator
0931:                        .getInstance();
0932:                int decomp = collator.getDecomposition();
0933:                int strength = collator.getStrength();
0934:                boolean alt = collator.isAlternateHandlingShifted();
0935:                boolean caselevel = collator.isCaseLevel();
0936:                boolean french = collator.isFrenchCollation();
0937:                boolean hquart = collator.isHiraganaQuaternary();
0938:                boolean lowercase = collator.isLowerCaseFirst();
0939:                boolean uppercase = collator.isUpperCaseFirst();
0940:
0941:                collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
0942:                if (collator.getDecomposition() != Collator.CANONICAL_DECOMPOSITION) {
0943:                    errln("Setting decomposition failed");
0944:                }
0945:                collator.setStrength(Collator.QUATERNARY);
0946:                if (collator.getStrength() != Collator.QUATERNARY) {
0947:                    errln("Setting strength failed");
0948:                }
0949:                collator.setAlternateHandlingShifted(!alt);
0950:                if (collator.isAlternateHandlingShifted() == alt) {
0951:                    errln("Setting alternate handling failed");
0952:                }
0953:                collator.setCaseLevel(!caselevel);
0954:                if (collator.isCaseLevel() == caselevel) {
0955:                    errln("Setting case level failed");
0956:                }
0957:                collator.setFrenchCollation(!french);
0958:                if (collator.isFrenchCollation() == french) {
0959:                    errln("Setting french collation failed");
0960:                }
0961:                collator.setHiraganaQuaternary(!hquart);
0962:                if (collator.isHiraganaQuaternary() == hquart) {
0963:                    errln("Setting hiragana quartenary failed");
0964:                }
0965:                collator.setLowerCaseFirst(!lowercase);
0966:                if (collator.isLowerCaseFirst() == lowercase) {
0967:                    errln("Setting lower case first failed");
0968:                }
0969:                collator.setUpperCaseFirst(!uppercase);
0970:                if (collator.isUpperCaseFirst() == uppercase) {
0971:                    errln("Setting upper case first failed");
0972:                }
0973:                collator.setDecompositionDefault();
0974:                if (collator.getDecomposition() != decomp) {
0975:                    errln("Setting decomposition default failed");
0976:                }
0977:                collator.setStrengthDefault();
0978:                if (collator.getStrength() != strength) {
0979:                    errln("Setting strength default failed");
0980:                }
0981:                collator.setAlternateHandlingDefault();
0982:                if (collator.isAlternateHandlingShifted() != alt) {
0983:                    errln("Setting alternate handling default failed");
0984:                }
0985:                collator.setCaseLevelDefault();
0986:                if (collator.isCaseLevel() != caselevel) {
0987:                    errln("Setting case level default failed");
0988:                }
0989:                collator.setFrenchCollationDefault();
0990:                if (collator.isFrenchCollation() != french) {
0991:                    errln("Setting french handling default failed");
0992:                }
0993:                collator.setHiraganaQuaternaryDefault();
0994:                if (collator.isHiraganaQuaternary() != hquart) {
0995:                    errln("Setting Hiragana Quartenary default failed");
0996:                }
0997:                collator.setCaseFirstDefault();
0998:                if (collator.isLowerCaseFirst() != lowercase
0999:                        || collator.isUpperCaseFirst() != uppercase) {
1000:                    errln("Setting case first handling default failed");
1001:                }
1002:            }
1003:
1004:            public void TestBounds() {
1005:                Collator coll = Collator.getInstance(new Locale("sh", ""));
1006:
1007:                String test[] = { "John Smith", "JOHN SMITH", "john SMITH",
1008:                        "j\u00F6hn sm\u00EFth", "J\u00F6hn Sm\u00EFth",
1009:                        "J\u00D6HN SM\u00CFTH", "john smithsonian",
1010:                        "John Smithsonian", };
1011:
1012:                String testStr[] = { "\u010CAKI MIHALJ", "\u010CAKI MIHALJ",
1013:                        "\u010CAKI PIRO\u0160KA", "\u010CABAI ANDRIJA",
1014:                        "\u010CABAI LAJO\u0160", "\u010CABAI MARIJA",
1015:                        "\u010CABAI STEVAN", "\u010CABAI STEVAN",
1016:                        "\u010CABARKAPA BRANKO", "\u010CABARKAPA MILENKO",
1017:                        "\u010CABARKAPA MIROSLAV", "\u010CABARKAPA SIMO",
1018:                        "\u010CABARKAPA STANKO", "\u010CABARKAPA TAMARA",
1019:                        "\u010CABARKAPA TOMA\u0160",
1020:                        "\u010CABDARI\u0106 NIKOLA",
1021:                        "\u010CABDARI\u0106 ZORICA", "\u010CABI NANDOR",
1022:                        "\u010CABOVI\u0106 MILAN", "\u010CABRADI AGNEZIJA",
1023:                        "\u010CABRADI IVAN", "\u010CABRADI JELENA",
1024:                        "\u010CABRADI LJUBICA", "\u010CABRADI STEVAN",
1025:                        "\u010CABRDA MARTIN", "\u010CABRILO BOGDAN",
1026:                        "\u010CABRILO BRANISLAV", "\u010CABRILO LAZAR",
1027:                        "\u010CABRILO LJUBICA", "\u010CABRILO SPASOJA",
1028:                        "\u010CADE\u0160 ZDENKA", "\u010CADESKI BLAGOJE",
1029:                        "\u010CADOVSKI VLADIMIR", "\u010CAGLJEVI\u0106 TOMA",
1030:                        "\u010CAGOROVI\u0106 VLADIMIR", "\u010CAJA VANKA",
1031:                        "\u010CAJI\u0106 BOGOLJUB", "\u010CAJI\u0106 BORISLAV",
1032:                        "\u010CAJI\u0106 RADOSLAV",
1033:                        "\u010CAK\u0160IRAN MILADIN", "\u010CAKAN EUGEN",
1034:                        "\u010CAKAN EVGENIJE", "\u010CAKAN IVAN",
1035:                        "\u010CAKAN JULIJAN", "\u010CAKAN MIHAJLO",
1036:                        "\u010CAKAN STEVAN", "\u010CAKAN VLADIMIR",
1037:                        "\u010CAKAN VLADIMIR", "\u010CAKAN VLADIMIR",
1038:                        "\u010CAKARA ANA", "\u010CAKAREVI\u0106 MOMIR",
1039:                        "\u010CAKAREVI\u0106 NEDELJKO",
1040:                        "\u010CAKI \u0160ANDOR", "\u010CAKI AMALIJA",
1041:                        "\u010CAKI ANDRA\u0160", "\u010CAKI LADISLAV",
1042:                        "\u010CAKI LAJO\u0160", "\u010CAKI LASLO" };
1043:
1044:                CollationKey testKey[] = new CollationKey[testStr.length];
1045:                for (int i = 0; i < testStr.length; i++) {
1046:                    testKey[i] = coll.getCollationKey(testStr[i]);
1047:                }
1048:
1049:                Arrays.sort(testKey);
1050:                for (int i = 0; i < testKey.length - 1; i++) {
1051:                    CollationKey lower = testKey[i].getBound(
1052:                            CollationKey.BoundMode.LOWER, Collator.SECONDARY);
1053:                    for (int j = i + 1; j < testKey.length; j++) {
1054:                        CollationKey upper = testKey[j].getBound(
1055:                                CollationKey.BoundMode.UPPER,
1056:                                Collator.SECONDARY);
1057:                        for (int k = i; k <= j; k++) {
1058:                            if (lower.compareTo(testKey[k]) > 0) {
1059:                                errln("Problem with lower bound at i = " + i
1060:                                        + " j = " + j + " k = " + k);
1061:                            }
1062:                            if (upper.compareTo(testKey[k]) <= 0) {
1063:                                errln("Problem with upper bound at i = " + i
1064:                                        + " j = " + j + " k = " + k);
1065:                            }
1066:                        }
1067:                    }
1068:                }
1069:
1070:                for (int i = 0; i < test.length; i++) {
1071:                    CollationKey key = coll.getCollationKey(test[i]);
1072:                    CollationKey lower = key.getBound(
1073:                            CollationKey.BoundMode.LOWER, Collator.SECONDARY);
1074:                    CollationKey upper = key.getBound(
1075:                            CollationKey.BoundMode.UPPER_LONG,
1076:                            Collator.SECONDARY);
1077:                    for (int j = i + 1; j < test.length; j++) {
1078:                        key = coll.getCollationKey(test[j]);
1079:                        if (lower.compareTo(key) > 0) {
1080:                            errln("Problem with lower bound i = " + i + " j = "
1081:                                    + j);
1082:                        }
1083:                        if (upper.compareTo(key) <= 0) {
1084:                            errln("Problem with upper bound i = " + i + " j = "
1085:                                    + j);
1086:                        }
1087:                    }
1088:                }
1089:            }
1090:
1091:            public final void TestGetAll() {
1092:                Locale[] list = Collator.getAvailableLocales();
1093:                int errorCount = 0;
1094:                for (int i = 0; i < list.length; ++i) {
1095:                    log("Locale name: ");
1096:                    log(list[i].toString());
1097:                    log(" , the display name is : ");
1098:                    logln(list[i].getDisplayName());
1099:                    try {
1100:                        logln("     ...... Or display as: "
1101:                                + Collator.getDisplayName(list[i]));
1102:                        logln("     ...... and display in Chinese: "
1103:                                + Collator
1104:                                        .getDisplayName(list[i], Locale.CHINA));
1105:                    } catch (MissingResourceException ex) {
1106:                        errorCount++;
1107:                        logln("could not get displayName for " + list[i]);
1108:                    }
1109:                }
1110:                if (errorCount > 0) {
1111:                    warnln("Could not load the locale data.");
1112:                }
1113:            }
1114:
1115:            private void doSetsTest(UnicodeSet ref, UnicodeSet set,
1116:                    String inSet, String outSet) {
1117:
1118:                set.clear();
1119:                set.applyPattern(inSet);
1120:
1121:                if (!ref.containsAll(set)) {
1122:                    err("Some stuff from " + inSet
1123:                            + " is not present in the set\n");
1124:                }
1125:
1126:                set.clear();
1127:                set.applyPattern(outSet);
1128:                if (!ref.containsNone(set)) {
1129:                    err("Some stuff from " + outSet
1130:                            + " is present in the set\n");
1131:                }
1132:            }
1133:
1134:            public void TestGetContractions() throws Exception {
1135:                /*        static struct {
1136:                 const char* locale;
1137:                 const char* inConts;
1138:                 const char* outConts;
1139:                 const char* inExp;
1140:                 const char* outExp;
1141:                 const char* unsafeCodeUnits;
1142:                 const char* safeCodeUnits;
1143:                 }
1144:                 */
1145:                String tests[][] = {
1146:                        {
1147:                                "ru",
1148:                                "[{\u0474\u030F}{\u0475\u030F}{\u04D8\u0308}{\u04D9\u0308}{\u04E8\u0308}{\u04E9\u0308}]",
1149:                                "[{\u0430\u0306}{\u0410\u0306}{\u0430\u0308}{\u0410\u0306}{\u0433\u0301}{\u0413\u0301}]",
1150:                                "[\u00e6]", "[a]",
1151:                                "[\u0474\u0475\u04d8\u04d9\u04e8\u04e9]",
1152:                                "[aAbB\u0430\u0410\u0433\u0413]" },
1153:                        {
1154:                                "uk",
1155:                                "[{\u0474\u030F}{\u0475\u030F}{\u04D8\u0308}{\u04D9\u0308}{\u04E8\u0308}{\u04E9\u0308}"
1156:                                        + "{\u0430\u0306}{\u0410\u0306}{\u0430\u0308}{\u0410\u0306}{\u0433\u0301}{\u0413\u0301}]",
1157:                                "[]",
1158:                                "[\u00e6]",
1159:                                "[a]",
1160:                                "[\u0474\u0475\u04D8\u04D9\u04E8\u04E9\u0430\u0410\u0433\u0413]",
1161:                                "[aAbBxv]", },
1162:                        {
1163:                                "sh",
1164:                                "[{C\u0301}{C\u030C}{C\u0341}{DZ\u030C}{Dz\u030C}{D\u017D}{D\u017E}{lj}{nj}]",
1165:                                "[{\u309d\u3099}{\u30fd\u3099}]", "[\u00e6]",
1166:                                "[a]", "[nlcdzNLCDZ]", "[jabv]" },
1167:                        {
1168:                                "ja",
1169:                                "[{\u3053\u3099\u309D}{\u3053\u3099\u309D\u3099}{\u3053\u3099\u309E}{\u3053\u3099\u30FC}{\u3053\u309D}{\u3053\u309D\u3099}{\u3053\u309E}{\u3053\u30FC}{\u30B3\u3099\u30FC}{\u30B3\u3099\u30FD}{\u30B3\u3099\u30FD\u3099}{\u30B3\u3099\u30FE}{\u30B3\u30FC}{\u30B3\u30FD}{\u30B3\u30FD\u3099}{\u30B3\u30FE}]",
1170:                                "[{\u30FD\u3099}{\u309D\u3099}{\u3053\u3099}{\u30B3\u3099}{lj}{nj}]",
1171:                                "[\u30FE\u00e6]", "[a]", "[\u3099]", "[]" } };
1172:
1173:                RuleBasedCollator coll = null;
1174:                int i = 0;
1175:                UnicodeSet conts = new UnicodeSet();
1176:                UnicodeSet exp = new UnicodeSet();
1177:                UnicodeSet set = new UnicodeSet();
1178:
1179:                for (i = 0; i < tests.length; i++) {
1180:                    logln("Testing locale: " + tests[i][0]);
1181:                    coll = (RuleBasedCollator) Collator
1182:                            .getInstance(new ULocale(tests[i][0]));
1183:                    coll.getContractionsAndExpansions(conts, exp, true);
1184:                    logln("Contractions " + conts.size() + ":\n"
1185:                            + conts.toPattern(true));
1186:                    doSetsTest(conts, set, tests[i][1], tests[i][2]);
1187:                    logln("Expansions " + exp.size() + ":\n"
1188:                            + exp.toPattern(true));
1189:                    doSetsTest(exp, set, tests[i][3], tests[i][4]);
1190:
1191:                    // No unsafe set in ICU4J
1192:                    //noConts = ucol_getUnsafeSet(coll, conts, &status);
1193:                    //doSetsTest(conts, set, tests[i][5], tests[i][6]);
1194:                    //log_verbose("Unsafes "+conts.size()+":\n"+conts.toPattern(true)+"\n");
1195:                }
1196:            }
1197:
1198:            private static final String bigone = "One";
1199:            private static final String littleone = "one";
1200:
1201:            public void TestClone() {
1202:                logln("\ninit c0");
1203:                RuleBasedCollator c0 = (RuleBasedCollator) Collator
1204:                        .getInstance();
1205:                c0.setStrength(Collator.TERTIARY);
1206:                dump("c0", c0);
1207:
1208:                logln("\ninit c1");
1209:                RuleBasedCollator c1 = (RuleBasedCollator) Collator
1210:                        .getInstance();
1211:                c1.setStrength(Collator.TERTIARY);
1212:                c1.setUpperCaseFirst(!c1.isUpperCaseFirst());
1213:                dump("c0", c0);
1214:                dump("c1", c1);
1215:                try {
1216:                    logln("\ninit c2");
1217:                    RuleBasedCollator c2 = (RuleBasedCollator) c1.clone();
1218:                    c2.setUpperCaseFirst(!c2.isUpperCaseFirst());
1219:                    dump("c0", c0);
1220:                    dump("c1", c1);
1221:                    dump("c2", c2);
1222:                    if (c1.equals(c2)) {
1223:                        errln("The cloned objects refer to same data");
1224:                    }
1225:                } catch (CloneNotSupportedException ex) {
1226:                    errln("Could not clone the collator");
1227:                }
1228:            }
1229:
1230:            private void dump(String msg, RuleBasedCollator c) {
1231:                logln(msg + " " + c.compare(bigone, littleone) + " s: "
1232:                        + c.getStrength() + " u: " + c.isUpperCaseFirst());
1233:            }
1234:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.