FileDocCategorySizeDatePackage
TestStringComparator.javaAPI DocphoneME MR2 API (J2ME)11635Wed May 02 18:00:46 BST 2007javax.microedition.global

TestStringComparator

public class TestStringComparator extends TestCase
I3 test for StringComparator.

Fields Summary
private StringComparator
sc
private String
message
private String[]
illegal
private static final String[]
words
private static final int[]
cmp_level1
private static final int[]
cmp_level2
private static final int[]
cmp_level3
private static final int[]
cmp_identical
Constructors Summary
Methods Summary
private intcompareWords(int i, int j)

        message = "compareWords(" + i + ", " + j + ") is wrong";
        return sc.compare(words[i], words[j]);
    
private booleanequalWords(int i, int j)

        message = "equalWords(" + i + ", " + j + ") is wrong";
        return sc.equals(words[i], words[j]);
    
public voidrunTests()
fill suite with test methods

        declare("testValidConstruction");
        testValidConstruction();
        declare("testInvalidConstruction");
        testInvalidConstruction();
        declare("testLevel1");
        testLevel1();
        declare("testLevel2");
        testLevel2();
        declare("testLevel3");
        testLevel3();
        declare("testIdentical");
        testIdentical();
        declare("testIllegalComparison");
        testIllegalComparison();
    
private intsign(int num)

        if (num > 0) {
            return 1;
        }
        if (num < 0) {
            return -1;
        }
        return 0;
    
public voidtestIdentical()

        sc = null;
        sc = new StringComparator(null, StringComparator.IDENTICAL);

        for (int i = 0; i < cmp_identical.length; i++) {
            for (int j = 0; j < cmp_identical[i].length; j++) {
                int res = compareWords(i, j);
                assertEquals(message, cmp_identical[i][j], sign(res));
            }
        }
        for (int i = 0; i < cmp_identical.length; i++) {
            for (int j = 0; j < cmp_identical[i].length; j++) {
                boolean eq = equalWords(i, j);
                assertTrue(message, (cmp_identical[i][j] == 0) == eq);
            }
        }
    
public voidtestIllegalComparison()

        sc = null;
        sc = new StringComparator(null);

        /* Test comparison of null strings */
        try {
            sc.compare(null, null);
            fail("NullPointerException expected");
        } catch (NullPointerException e) {
            assertTrue(true);
        }

        try {
            sc.compare(null, words[0]);
            fail("NullPointerException expected");
        } catch (NullPointerException e) {
            assertTrue(true);
        }

        try {
            sc.compare(words[0], null);
            fail("NullPointerException expected");
        } catch (NullPointerException e) {
            assertTrue(true);
        }

        /* Test equality testing of null strings */
        try {
            sc.equals(null, null);
            fail("NullPointerException expected");
        } catch (NullPointerException e) {
            assertTrue(true);
        }

        try {
            sc.equals(null, words[0]);
            fail("NullPointerException expected");
        } catch (NullPointerException e) {
            assertTrue(true);
        }

        try {
            sc.equals(words[0], null);
            fail("NullPointerException expected");
        } catch (NullPointerException e) {
            assertTrue(true);
        }
    
public voidtestInvalidConstruction()

        /* Test string comparator creation with illegal (wrong format) locale */
        for (int i = 0; i < illegal.length; i++) {
            try {
                sc = new StringComparator(illegal[i]);
                fail("IllegalArgumentException expected");
            } catch (IllegalArgumentException e) {
                assertTrue(true);
            }
        }

        /* Test string comparator creation with illegal level of comparison */
        try {
            sc = new StringComparator("en-US", 10);
            fail("IllegalArgumentException expected");
        } catch (IllegalArgumentException e) {
            assertTrue(true);
        }

        /* Test string comparator creation with unsupported locale */
        try {
            sc = new StringComparator("fi-FI");
            fail("UnsupportedException expected");
        } catch (UnsupportedLocaleException e) {
            assertTrue(true);
        }
    
public voidtestLevel1()

        sc = null;
        sc = new StringComparator(null);

        for (int i = 0; i < cmp_level1.length; i++) {
            for (int j = 0; j < cmp_level1[i].length; j++) {
                int res = compareWords(i, j);
                assertEquals(message, cmp_level1[i][j], sign(res));
            }
        }
        for (int i = 0; i < cmp_level1.length; i++) {
            for (int j = 0; j < cmp_level1[i].length; j++) {
                boolean eq = equalWords(i, j);
                assertTrue(message, (cmp_level1[i][j] == 0) == eq);
            }
        }
    
public voidtestLevel2()

        sc = null;
        sc = new StringComparator(null, StringComparator.LEVEL2);

        for (int i = 0; i < cmp_level2.length; i++) {
            for (int j = 0; j < cmp_level2[i].length; j++) {
                int res = compareWords(i, j);
                assertEquals(message, cmp_level2[i][j], sign(res));
            }
        }
        for (int i = 0; i < cmp_level2.length; i++) {
            for (int j = 0; j < cmp_level2[i].length; j++) {
                boolean eq = equalWords(i, j);
                assertTrue(message, (cmp_level2[i][j] == 0) == eq);
            }
        }
    
public voidtestLevel3()

        sc = null;
        sc = new StringComparator(null, StringComparator.LEVEL3);

        for (int i = 0; i < cmp_level3.length; i++) {
            for (int j = 0; j < cmp_level3[i].length; j++) {
                int res = compareWords(i, j);
                assertEquals(message, cmp_level3[i][j], sign(res));
            }
        }
        for (int i = 0; i < cmp_level3.length; i++) {
            for (int j = 0; j < cmp_level3[i].length; j++) {
                boolean eq = equalWords(i, j);
                assertTrue(message, (cmp_level3[i][j] == 0) == eq);
            }
        }
    
public voidtestValidConstruction()



    /* Test cases of valid StringComparator creation */
       
        /* Create string comparator for default locale */
        sc = null;
        sc = new StringComparator();
        assertNotNull(sc);
        assertEquals(StringComparator.LEVEL1, sc.getLevel());
        assertEquals(System.getProperty("microedition.locale"), sc.getLocale());

        /* Create string comparator for generic collation */
        sc = null;
        sc = new StringComparator(null);
        assertNotNull(sc);
        assertEquals(StringComparator.LEVEL1, sc.getLevel());
        assertNull(sc.getLocale());

        /* Create string comparator for generic collation */
        sc = null;
        sc = new StringComparator("");
        assertNotNull(sc);
        assertEquals(StringComparator.LEVEL1, sc.getLevel());
        assertNull(sc.getLocale());

        /* Create string comparator for "zh" locale */
        sc = null;
        sc = new StringComparator("zh");
        assertNotNull(sc);
        assertEquals(StringComparator.LEVEL1, sc.getLevel());
        assertEquals("zh", sc.getLocale());

        /* Create string comparator for "sk-SK" locale */
        sc = null;
        sc = new StringComparator("sk-SK");
        assertNotNull(sc);
        assertEquals(StringComparator.LEVEL1, sc.getLevel());
        assertEquals("sk-SK", sc.getLocale());

        /* Create string comparator with comparison level LEVEL1 */
        sc = null;
        sc = new StringComparator("sk-SK", StringComparator.LEVEL1);
        assertNotNull(sc);
        assertEquals(StringComparator.LEVEL1, sc.getLevel());
        assertEquals("sk-SK", sc.getLocale());

        /* Create string comparator with comparison level LEVEL2 */
        sc = null;
        sc = new StringComparator("sk-SK", StringComparator.LEVEL2);
        assertNotNull(sc);
        assertEquals(StringComparator.LEVEL2, sc.getLevel());
        assertEquals("sk-SK", sc.getLocale());

        /* Create string comparator with comparison level LEVEL3 */
        sc = null;
        sc = new StringComparator("sk-SK", StringComparator.LEVEL3);
        assertNotNull(sc);
        assertEquals(StringComparator.LEVEL3, sc.getLevel());
        assertEquals("sk-SK", sc.getLocale());

        /* Create string comparator with comparison level IDENTICAL */
        sc = null;
        sc = new StringComparator("sk-SK", StringComparator.IDENTICAL);
        assertNotNull(sc);
        assertEquals(StringComparator.IDENTICAL, sc.getLevel());
        assertEquals("sk-SK", sc.getLocale());