001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.test.rbbi;
008:
009: import com.ibm.icu.dev.test.TestFmwk;
010: import com.ibm.icu.text.BreakIterator;
011: import java.text.CharacterIterator;
012: import java.util.Arrays;
013: import java.util.Locale;
014:
015: public class BreakIteratorRegTest extends TestFmwk {
016: public static void main(String[] args) throws Exception {
017: new BreakIteratorRegTest().run(args);
018: }
019:
020: public void TestRegUnreg() {
021: Locale thailand_locale = new Locale("th", "TH", "");
022: Locale foo_locale = new Locale("fu", "FU", "FOO");
023: BreakIterator jwbi = BreakIterator
024: .getWordInstance(Locale.JAPAN);
025: BreakIterator uwbi = BreakIterator.getWordInstance(Locale.US);
026: BreakIterator usbi = BreakIterator
027: .getSentenceInstance(Locale.US);
028: BreakIterator twbi = BreakIterator
029: .getWordInstance(thailand_locale);
030: BreakIterator rwbi = BreakIterator.getWordInstance(new Locale(
031: "", "", ""));
032:
033: BreakIterator sbi = (BreakIterator) usbi.clone();
034: // todo: this will cause the test to fail, no way to set a breakiterator to null text so can't fix yet.
035: // String text = "This is some test, by golly. Boy, they don't make tests like they used to, do they? This here test ain't worth $2.50. Nope.";
036: // sbi.setText(text);
037:
038: assertTrue(!BreakIterator.unregister(""),
039: "unregister before register"); // coverage
040:
041: Object key0 = BreakIterator.registerInstance(
042: (BreakIterator) twbi.clone(), foo_locale,
043: BreakIterator.KIND_WORD);
044: Object key1 = BreakIterator.registerInstance(sbi, Locale.US,
045: BreakIterator.KIND_WORD);
046: Object key2 = BreakIterator.registerInstance(
047: (BreakIterator) twbi.clone(), Locale.US,
048: BreakIterator.KIND_WORD);
049:
050: {
051: BreakIterator test0 = BreakIterator
052: .getWordInstance(Locale.JAPAN);
053: BreakIterator test1 = BreakIterator
054: .getWordInstance(Locale.US);
055: BreakIterator test2 = BreakIterator
056: .getSentenceInstance(Locale.US);
057: BreakIterator test3 = BreakIterator
058: .getWordInstance(thailand_locale);
059: BreakIterator test4 = BreakIterator
060: .getWordInstance(foo_locale);
061:
062: assertEqual(test0, jwbi, "japan word == japan word");
063: assertEqual(test1, twbi, "us word == thai word");
064: assertEqual(test2, usbi, "us sentence == us sentence");
065: assertEqual(test3, twbi, "thai word == thai word");
066: assertEqual(test4, twbi, "foo word == thai word");
067: }
068:
069: Locale[] locales = BreakIterator.getAvailableLocales();
070:
071: assertTrue(BreakIterator.unregister(key2),
072: "unregister us word (thai word)");
073: assertTrue(!BreakIterator.unregister(key2),
074: "unregister second time");
075: boolean error = false;
076: try {
077: BreakIterator.unregister(null);
078: } catch (IllegalArgumentException e) {
079: error = true;
080: }
081:
082: assertTrue(error, "unregister null");
083:
084: {
085: CharacterIterator sci = BreakIterator.getWordInstance(
086: Locale.US).getText();
087: int len = sci.getEndIndex() - sci.getBeginIndex();
088: assertEqual(len, 0, "us word text: " + getString(sci));
089: }
090:
091: assertTrue(Arrays.asList(BreakIterator.getAvailableLocales())
092: .contains(foo_locale), "foo_locale");
093: assertTrue(BreakIterator.unregister(key0),
094: "unregister foo word (thai word)");
095: assertTrue(!Arrays.asList(BreakIterator.getAvailableLocales())
096: .contains(foo_locale), "no foo_locale");
097: assertEqual(BreakIterator.getWordInstance(Locale.US), usbi,
098: "us word == us sentence");
099:
100: assertTrue(BreakIterator.unregister(key1),
101: "unregister us word (us sentence)");
102: {
103: BreakIterator test0 = BreakIterator
104: .getWordInstance(Locale.JAPAN);
105: BreakIterator test1 = BreakIterator
106: .getWordInstance(Locale.US);
107: BreakIterator test2 = BreakIterator
108: .getSentenceInstance(Locale.US);
109: BreakIterator test3 = BreakIterator
110: .getWordInstance(thailand_locale);
111: BreakIterator test4 = BreakIterator
112: .getWordInstance(foo_locale);
113:
114: assertEqual(test0, jwbi, "japanese word break");
115: assertEqual(test1, uwbi, "us sentence-word break");
116: assertEqual(test2, usbi, "us sentence break");
117: assertEqual(test3, twbi, "thai word break");
118: assertEqual(test4, rwbi, "root word break");
119:
120: CharacterIterator sci = test1.getText();
121: int len = sci.getEndIndex() - sci.getBeginIndex();
122: assertEqual(len, 0, "us sentence-word break text: "
123: + getString(sci));
124: }
125: }
126:
127: private void assertEqual(Object lhs, Object rhs, String msg) {
128: msg(msg, lhs.equals(rhs) ? LOG : ERR, true, true);
129: }
130:
131: private void assertEqual(int lhs, int rhs, String msg) {
132: msg(msg, lhs == rhs ? LOG : ERR, true, true);
133: }
134:
135: private void assertTrue(boolean arg, String msg) {
136: msg(msg, arg ? LOG : ERR, true, true);
137: }
138:
139: private static String getString(CharacterIterator ci) {
140: StringBuffer buf = new StringBuffer(ci.getEndIndex()
141: - ci.getBeginIndex() + 2);
142: buf.append("'");
143: for (char c = ci.first(); c != CharacterIterator.DONE; c = ci
144: .next()) {
145: buf.append(c);
146: }
147: buf.append("'");
148: return buf.toString();
149: }
150: }
|