001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package javax.microedition.global;
028:
029: import com.sun.midp.i3test.*;
030: import java.util.*;
031:
032: /**
033: * I3 test for StringComparator.
034: */
035: public class TestStringComparator extends TestCase {
036:
037: private StringComparator sc;
038: private String message;
039:
040: private String[] illegal = { "Cs", "cs-", "cs/CZ", "cs-cz",
041: "cs-CZ-", "cs-CZ:utf8" };
042:
043: private static final String[] words = { "role", "ro\u0083le",
044: "Role", "r\u00f4le", "roles", "rule" };
045: private static final int[][] cmp_level1 = { { 0, 0, 0, 0, -1, -1 },
046: { 0, 0, 0, 0, -1, -1 }, { 0, 0, 0, 0, -1, -1 },
047: { 0, 0, 0, 0, -1, -1 }, { 1, 1, 1, 1, 0, -1 },
048: { 1, 1, 1, 1, 1, 0 } };
049: private static final int[][] cmp_level2 = {
050: { 0, 0, 0, -1, -1, -1 }, { 0, 0, 0, -1, -1, -1 },
051: { 0, 0, 0, -1, -1, -1 }, { 1, 1, 1, 0, -1, -1 },
052: { 1, 1, 1, 1, 0, -1 }, { 1, 1, 1, 1, 1, 0 } };
053: private static final int[][] cmp_level3 = {
054: { 0, 0, -1, -1, -1, -1 }, { 0, 0, -1, -1, -1, -1 },
055: { 1, 1, 0, -1, -1, -1 }, { 1, 1, 1, 0, -1, -1 },
056: { 1, 1, 1, 1, 0, -1 }, { 1, 1, 1, 1, 1, 0 } };
057: private static final int[][] cmp_identical = {
058: { 0, -1, -1, -1, -1, -1 }, { 1, 0, -1, -1, -1, -1 },
059: { 1, 1, 0, -1, -1, -1 }, { 1, 1, 1, 0, -1, -1 },
060: { 1, 1, 1, 1, 0, -1 }, { 1, 1, 1, 1, 1, 0 } };
061:
062: /* Test cases of valid StringComparator creation */
063: public void testValidConstruction() {
064: /* Create string comparator for default locale */
065: sc = null;
066: sc = new StringComparator();
067: assertNotNull(sc);
068: assertEquals(StringComparator.LEVEL1, sc.getLevel());
069: assertEquals(System.getProperty("microedition.locale"), sc
070: .getLocale());
071:
072: /* Create string comparator for generic collation */
073: sc = null;
074: sc = new StringComparator(null);
075: assertNotNull(sc);
076: assertEquals(StringComparator.LEVEL1, sc.getLevel());
077: assertNull(sc.getLocale());
078:
079: /* Create string comparator for generic collation */
080: sc = null;
081: sc = new StringComparator("");
082: assertNotNull(sc);
083: assertEquals(StringComparator.LEVEL1, sc.getLevel());
084: assertNull(sc.getLocale());
085:
086: /* Create string comparator for "zh" locale */
087: sc = null;
088: sc = new StringComparator("zh");
089: assertNotNull(sc);
090: assertEquals(StringComparator.LEVEL1, sc.getLevel());
091: assertEquals("zh", sc.getLocale());
092:
093: /* Create string comparator for "sk-SK" locale */
094: sc = null;
095: sc = new StringComparator("sk-SK");
096: assertNotNull(sc);
097: assertEquals(StringComparator.LEVEL1, sc.getLevel());
098: assertEquals("sk-SK", sc.getLocale());
099:
100: /* Create string comparator with comparison level LEVEL1 */
101: sc = null;
102: sc = new StringComparator("sk-SK", StringComparator.LEVEL1);
103: assertNotNull(sc);
104: assertEquals(StringComparator.LEVEL1, sc.getLevel());
105: assertEquals("sk-SK", sc.getLocale());
106:
107: /* Create string comparator with comparison level LEVEL2 */
108: sc = null;
109: sc = new StringComparator("sk-SK", StringComparator.LEVEL2);
110: assertNotNull(sc);
111: assertEquals(StringComparator.LEVEL2, sc.getLevel());
112: assertEquals("sk-SK", sc.getLocale());
113:
114: /* Create string comparator with comparison level LEVEL3 */
115: sc = null;
116: sc = new StringComparator("sk-SK", StringComparator.LEVEL3);
117: assertNotNull(sc);
118: assertEquals(StringComparator.LEVEL3, sc.getLevel());
119: assertEquals("sk-SK", sc.getLocale());
120:
121: /* Create string comparator with comparison level IDENTICAL */
122: sc = null;
123: sc = new StringComparator("sk-SK", StringComparator.IDENTICAL);
124: assertNotNull(sc);
125: assertEquals(StringComparator.IDENTICAL, sc.getLevel());
126: assertEquals("sk-SK", sc.getLocale());
127: }
128:
129: /* Test cases of invalid StringComparator creation */
130: public void testInvalidConstruction() {
131: /* Test string comparator creation with illegal (wrong format) locale */
132: for (int i = 0; i < illegal.length; i++) {
133: try {
134: sc = new StringComparator(illegal[i]);
135: fail("IllegalArgumentException expected");
136: } catch (IllegalArgumentException e) {
137: assertTrue(true);
138: }
139: }
140:
141: /* Test string comparator creation with illegal level of comparison */
142: try {
143: sc = new StringComparator("en-US", 10);
144: fail("IllegalArgumentException expected");
145: } catch (IllegalArgumentException e) {
146: assertTrue(true);
147: }
148:
149: /* Test string comparator creation with unsupported locale */
150: try {
151: sc = new StringComparator("fi-FI");
152: fail("UnsupportedException expected");
153: } catch (UnsupportedLocaleException e) {
154: assertTrue(true);
155: }
156: }
157:
158: /* Compare 2 entries of words[] array, prepare failure message */
159: private int compareWords(int i, int j) {
160: message = "compareWords(" + i + ", " + j + ") is wrong";
161: return sc.compare(words[i], words[j]);
162: }
163:
164: /* Test 2 entries of words[] array for equality, prepare failure message */
165: private boolean equalWords(int i, int j) {
166: message = "equalWords(" + i + ", " + j + ") is wrong";
167: return sc.equals(words[i], words[j]);
168: }
169:
170: /* Signum of integer argument */
171: private int sign(int num) {
172: if (num > 0) {
173: return 1;
174: }
175: if (num < 0) {
176: return -1;
177: }
178: return 0;
179: }
180:
181: /* Test LEVEL1 generic collation: base character differences only */
182: public void testLevel1() {
183: sc = null;
184: sc = new StringComparator(null);
185:
186: for (int i = 0; i < cmp_level1.length; i++) {
187: for (int j = 0; j < cmp_level1[i].length; j++) {
188: int res = compareWords(i, j);
189: assertEquals(message, cmp_level1[i][j], sign(res));
190: }
191: }
192: for (int i = 0; i < cmp_level1.length; i++) {
193: for (int j = 0; j < cmp_level1[i].length; j++) {
194: boolean eq = equalWords(i, j);
195: assertTrue(message, (cmp_level1[i][j] == 0) == eq);
196: }
197: }
198: }
199:
200: /* Test LEVEL2 generic collation: take character accents into account */
201: public void testLevel2() {
202: sc = null;
203: sc = new StringComparator(null, StringComparator.LEVEL2);
204:
205: for (int i = 0; i < cmp_level2.length; i++) {
206: for (int j = 0; j < cmp_level2[i].length; j++) {
207: int res = compareWords(i, j);
208: assertEquals(message, cmp_level2[i][j], sign(res));
209: }
210: }
211: for (int i = 0; i < cmp_level2.length; i++) {
212: for (int j = 0; j < cmp_level2[i].length; j++) {
213: boolean eq = equalWords(i, j);
214: assertTrue(message, (cmp_level2[i][j] == 0) == eq);
215: }
216: }
217: }
218:
219: /* Test LEVEL3 generic collation: take character case into account */
220: public void testLevel3() {
221: sc = null;
222: sc = new StringComparator(null, StringComparator.LEVEL3);
223:
224: for (int i = 0; i < cmp_level3.length; i++) {
225: for (int j = 0; j < cmp_level3[i].length; j++) {
226: int res = compareWords(i, j);
227: assertEquals(message, cmp_level3[i][j], sign(res));
228: }
229: }
230: for (int i = 0; i < cmp_level3.length; i++) {
231: for (int j = 0; j < cmp_level3[i].length; j++) {
232: boolean eq = equalWords(i, j);
233: assertTrue(message, (cmp_level3[i][j] == 0) == eq);
234: }
235: }
236: }
237:
238: /* Test IDENTICAL generic collation: take all differences into account */
239: public void testIdentical() {
240: sc = null;
241: sc = new StringComparator(null, StringComparator.IDENTICAL);
242:
243: for (int i = 0; i < cmp_identical.length; i++) {
244: for (int j = 0; j < cmp_identical[i].length; j++) {
245: int res = compareWords(i, j);
246: assertEquals(message, cmp_identical[i][j], sign(res));
247: }
248: }
249: for (int i = 0; i < cmp_identical.length; i++) {
250: for (int j = 0; j < cmp_identical[i].length; j++) {
251: boolean eq = equalWords(i, j);
252: assertTrue(message, (cmp_identical[i][j] == 0) == eq);
253: }
254: }
255: }
256:
257: /* Test illegal comparison attempts */
258: public void testIllegalComparison() {
259: sc = null;
260: sc = new StringComparator(null);
261:
262: /* Test comparison of null strings */
263: try {
264: sc.compare(null, null);
265: fail("NullPointerException expected");
266: } catch (NullPointerException e) {
267: assertTrue(true);
268: }
269:
270: try {
271: sc.compare(null, words[0]);
272: fail("NullPointerException expected");
273: } catch (NullPointerException e) {
274: assertTrue(true);
275: }
276:
277: try {
278: sc.compare(words[0], null);
279: fail("NullPointerException expected");
280: } catch (NullPointerException e) {
281: assertTrue(true);
282: }
283:
284: /* Test equality testing of null strings */
285: try {
286: sc.equals(null, null);
287: fail("NullPointerException expected");
288: } catch (NullPointerException e) {
289: assertTrue(true);
290: }
291:
292: try {
293: sc.equals(null, words[0]);
294: fail("NullPointerException expected");
295: } catch (NullPointerException e) {
296: assertTrue(true);
297: }
298:
299: try {
300: sc.equals(words[0], null);
301: fail("NullPointerException expected");
302: } catch (NullPointerException e) {
303: assertTrue(true);
304: }
305: }
306:
307: /**
308: * fill suite with test methods
309: */
310: public void runTests() {
311: declare("testValidConstruction");
312: testValidConstruction();
313: declare("testInvalidConstruction");
314: testInvalidConstruction();
315: declare("testLevel1");
316: testLevel1();
317: declare("testLevel2");
318: testLevel2();
319: declare("testLevel3");
320: testLevel3();
321: declare("testIdentical");
322: testIdentical();
323: declare("testIllegalComparison");
324: testIllegalComparison();
325: }
326: }
|