FileDocCategorySizeDatePackage
ResourceManagerTestMidlet.javaAPI DocphoneME MR2 API (J2ME)21978Wed May 02 18:00:46 BST 2007None

ResourceManagerTestMidlet

public class ResourceManagerTestMidlet extends MIDlet
version

Fields Summary
private ResourceManager
rm
private String
message
private String[]
locs_match_en
private String[]
locs_match_empty
private String[]
locs_with_null
private String[]
locs_with_illegal
private String[]
locs_unsupported
private String[]
illegal
private String[]
res_cs
private String[]
res_en
private String[]
res_ja
private String[]
res_sk
private String[]
res_common
private final int
RES_BASE
private final int
FORMAT_SYMBOLS
private int
assertions
private int
failures
private boolean
verbose
Constructors Summary
public ResourceManagerTestMidlet()



      
    
Methods Summary
private voidassertEquals(int a, int b)

        assertions++;
        if (verbose) {
            System.out.println("## " + assertions + ": null");
        }
        if (a != b) {
            fail();
        }
    
private voidassertEquals(java.lang.Object a, java.lang.Object b)

        assertions++;
        if (verbose) {
            System.out.println("## " + assertions + ": null");
        }
        if (!a.equals(b)) {
            fail();
        }
    
private voidassertNotNull(java.lang.Object a)

        assertions++;
        if (verbose) {
            System.out.println("## " + assertions + ": null");
        }
        if (a == null) {
            fail();
        }
    
private voidassertNull(java.lang.Object a)

        assertions++;
        if (verbose) {
            System.out.println("## " + assertions + ": null");
        }
        if (a != null) {
            fail();
        }
    
private voidassertTrue(boolean a)

        assertions++;
        if (verbose) {
            System.out.println("## " + assertions + ": null");
        }
        if (!a) {
            fail();
        }
    
public voiddestroyApp(boolean unconditional)

    
private booleanequalArrays(byte[] a, byte[] b)

        if (a == null || b == null || a.length != b.length) {
            return false;
        }
        for (int i = 0; i < a.length; i++) {
            if (a[i] != b[i]) {
                return false;
            }
        }
        return true;
    
private voidfail()

        System.out.println("Assertion failure: #" + assertions);
        failures++;
    
private voidfail(java.lang.String msg)

        System.out.println("Failure: " + msg);
        failures++;
    
public voidpauseApp()

    
public voidstartApp()

        System.out.println("Starting ResourceManager test midlet...\n");
        try {
            testValidConstruction();
            testInvalidConstruction();
            testValidApplicationResources();
            testInvalidApplicationResources();
            System.out.println("\nTest completed: " + assertions +
                               " assertions, " + failures + " failures");
        } catch (Exception e) {
            System.out.println("ERROR:");
            e.printStackTrace();
            System.out.println("\nTest completed: " + assertions +
                               " assertions, " + failures +
                               " failures, 1 ERROR");
        }
        notifyDestroyed();
    
private voidtestInvalidApplicationResources()

        if (verbose) {
            System.out.println("## test testInvalidApplicationResources");
        }

        /* Test reading of resource with illegal identifier */
        rm = null;
        rm = ResourceManager.getManager("common", "en-US");
        try {
            rm.getResource(0x80000000);
            fail("IllegalArgumentException expected");
        } catch (IllegalArgumentException e) {
            assertTrue(true);
        }

        /* Test reading of resource with illegal identifier as string */
        rm = null;
        rm = ResourceManager.getManager("common", "en-US");
        try {
            rm.getString(-1);
            fail("IllegalArgumentException expected");
        } catch (IllegalArgumentException e) {
            assertTrue(true);
        }

        /* Test reading of resource with illegal identifier as binary data */
        rm = null;
        rm = ResourceManager.getManager("common", "en-US");
        try {
            rm.getData(-12345);
            fail("IllegalArgumentException expected");
        } catch (IllegalArgumentException e) {
            assertTrue(true);
        }

        /* Test reading of non-existent resource */
        rm = null;
        rm = ResourceManager.getManager("common", "zh");
        try {
            rm.getResource(0x12345678);
            fail("ResourceException expected");
        } catch (ResourceException e) {
            assertEquals(ResourceException.RESOURCE_NOT_FOUND,
                         e.getErrorCode());
        }

        /* Test reading of non-existent resource as string */
        rm = null;
        rm = ResourceManager.getManager("common", "zh");
        try {
            rm.getString(0);
            fail("ResourceException expected");
        } catch (ResourceException e) {
            assertEquals(ResourceException.RESOURCE_NOT_FOUND,
                         e.getErrorCode());
        }

        /* Test reading of non-existent resource as binary data */
        rm = null;
        rm = ResourceManager.getManager("common", "zh");
        try {
            rm.getData(0xabcdef);
            fail("ResourceException expected");
        } catch (ResourceException e) {
            assertEquals(ResourceException.RESOURCE_NOT_FOUND,
                         e.getErrorCode());
        }

        /* Test reading of string resource as binary data */
        rm = null;
        rm = ResourceManager.getManager("common", "cs-CZ");
        try {
            rm.getData(RES_BASE);
            fail("ResourceException expected");
        } catch (ResourceException e) {
            assertEquals(ResourceException.WRONG_RESOURCE_TYPE,
                         e.getErrorCode());
        }

        /* Test reading of user-defined resource */
        rm = null;
        rm = ResourceManager.getManager("common", "sk-SK");
        try {
            rm.getResource(FORMAT_SYMBOLS);
            fail("ResourceException expected");
        } catch (ResourceException e) {
            assertEquals(ResourceException.UNKNOWN_RESOURCE_TYPE,
                         e.getErrorCode());
        }

        /* Test reading of user-defined resource as binary data */
        rm = null;
        rm = ResourceManager.getManager("common", "sk-SK");
        try {
            rm.getData(FORMAT_SYMBOLS);
            fail("ResourceException expected");
        } catch (ResourceException e) {
            assertEquals(ResourceException.WRONG_RESOURCE_TYPE,
                         e.getErrorCode());
        }

        /* Test reading of user-defined resource as string */
        rm = null;
        rm = ResourceManager.getManager("common", "sk-SK");
        try {
            rm.getString(FORMAT_SYMBOLS);
            fail("ResourceException expected");
        } catch (ResourceException e) {
            assertEquals(ResourceException.WRONG_RESOURCE_TYPE,
                         e.getErrorCode());
        }
    
private voidtestInvalidConstruction()

        if (verbose) {
            System.out.println("## test testInvalidConstruction");
        }

        /* Test resource manager creation with baseName == null */
        try {
            rm = ResourceManager.getManager(null);
            fail("NullPointerException expected");
        } catch (NullPointerException e) {
            assertTrue(true);
        }

        /* Test resource manager creation with baseName that doesn't exist */
        try {
            rm = ResourceManager.getManager("nonexistent");
            fail("ResourceException expected");
        } catch (ResourceException e) {
            assertEquals(ResourceException.METAFILE_NOT_FOUND,
                         e.getErrorCode());
        }

        /* Test resource manager creation with locale == null */
        try {
            rm = ResourceManager.getManager("common", (String)null);
            fail("NullPointerException expected");
        } catch (NullPointerException e) {
            assertTrue(true);
        }

        /* Test resource manager creation with baseName == null */
        try {
            rm = ResourceManager.getManager(null, "en-US");
            fail("NullPointerException expected");
        } catch (NullPointerException e) {
            assertTrue(true);
        }

        /* Test resource manager creation with illegal (wrong format) locale */
        for (int i = 0; i < illegal.length; i++) {
            try {
                rm = ResourceManager.getManager("common", illegal[i]);
                fail("IllegalArgumentException expected");
            } catch (IllegalArgumentException e) {
                assertTrue(true);
            }
        }

        /* Test resource manager creation with locales == null */
        try {
            rm = ResourceManager.getManager("common", (String[])null);
            fail("NullPointerException expected");
        } catch (NullPointerException e) {
            assertTrue(true);
        }

        /* Test resource manager creation with baseName == null */
        try {
            rm = ResourceManager.getManager(null, locs_match_en);
            fail("NullPointerException expected");
        } catch (NullPointerException e) {
            assertTrue(true);
        }

        /* Test resource manager creation with locales containing null */
        try {
            rm = ResourceManager.getManager("common", locs_with_null);
            fail("NullPointerException expected");
        } catch (NullPointerException e) {
            assertTrue(true);
        }

        /* Test resource manager creation with empty list of locales */
        try {
            rm = ResourceManager.getManager("common", new String[0]);
            fail("IllegalArgumentException expected");
        } catch (IllegalArgumentException e) {
            assertTrue(true);
        }

        /* Test resource manager creation with list containing illegal locale */
        for (int i = 0; i < illegal.length; i++) {
            try {
                locs_with_illegal[1] = illegal[i];
                rm = ResourceManager.getManager("common", locs_with_illegal);
                fail("IllegalArgumentException expected");
            } catch (IllegalArgumentException e) {
                assertTrue(true);
            }
        }

        /*
         * Test resource manager creation with a list containing only
         * unsupported locales
         */
        try {
            rm = ResourceManager.getManager("common", locs_unsupported);
            fail("UnsupportedLocaleException expected");
        } catch (UnsupportedLocaleException e) {
            assertTrue(true);
        }
    
private voidtestValidApplicationResources()

        if (verbose) {
            System.out.println("## test testValidApplicationResources");
        }

        /*
         * Retrieve device resources for "cs-CZ" locale ("cs" resources are
         * hierarchically matched)
         */
        rm = null;
        rm = ResourceManager.getManager("common", "cs-CZ");
        assertNotNull(rm);
        for (int i = 0; i < 3; i++) {
            assertTrue(rm.isValidResourceID(RES_BASE + i));
            assertEquals(res_cs[i], rm.getString(RES_BASE + i));
            assertEquals(res_cs[i], rm.getResource(RES_BASE + i));
        }

        /* Retrieve device resources for "cs" locale */
        rm = null;
        rm = ResourceManager.getManager("common", "cs");
        assertNotNull(rm);
        for (int i = 0; i < 3; i++) {
            assertTrue(rm.isValidResourceID(RES_BASE + i));
            assertEquals(res_cs[i], rm.getString(RES_BASE + i));
            assertEquals(res_cs[i], rm.getResource(RES_BASE + i));
        }

        /*
         * Retrieve device resources for "en-US" locale ("en" resources are
         * hierarchically matched)
         */
        rm = null;
        rm = ResourceManager.getManager("common", "en-US");
        assertNotNull(rm);
        for (int i = 0; i < 3; i++) {
            assertTrue(rm.isValidResourceID(RES_BASE + i));
            assertEquals(res_en[i], rm.getString(RES_BASE + i));
            assertEquals(res_en[i], rm.getResource(RES_BASE + i));
        }

        /* Retrieve device resources for "en" locale */
        rm = null;
        rm = ResourceManager.getManager("common", "en");
        assertNotNull(rm);
        for (int i = 0; i < 3; i++) {
            assertTrue(rm.isValidResourceID(RES_BASE + i));
            assertEquals(res_en[i], rm.getString(RES_BASE + i));
            assertEquals(res_en[i], rm.getResource(RES_BASE + i));
        }

        /*
         * Retrieve device resources for "he-IL" locale (common resources are
         * hierarchically matched)
         */
        rm = null;
        rm = ResourceManager.getManager("common", "he-IL");
        assertNotNull(rm);
        for (int i = 0; i < 3; i++) {
            assertTrue(rm.isValidResourceID(RES_BASE + i));
            assertEquals(res_common[i], rm.getString(RES_BASE + i));
            assertEquals(res_common[i], rm.getResource(RES_BASE + i));
        }

        /*
         * Retrieve device resources for "he" locale (common resources are
         * hierarchically matched)
         */
        rm = null;
        rm = ResourceManager.getManager("common", "he");
        assertNotNull(rm);
        for (int i = 0; i < 3; i++) {
            assertTrue(rm.isValidResourceID(RES_BASE + i));
            assertEquals(res_common[i], rm.getString(RES_BASE + i));
            assertEquals(res_common[i], rm.getResource(RES_BASE + i));
        }

        /*
         * Retrieve device resources for "ja-JP" locale ("ja" resources are
         * hierarchically matched)
         */
        rm = null;
        rm = ResourceManager.getManager("common", "ja-JP");
        assertNotNull(rm);
        for (int i = 0; i < 3; i++) {
            assertTrue(rm.isValidResourceID(RES_BASE + i));
            assertEquals(res_ja[i], rm.getString(RES_BASE + i));
            assertEquals(res_ja[i], rm.getResource(RES_BASE + i));
        }

        /* Retrieve device resources for "ja" locale */
        rm = null;
        rm = ResourceManager.getManager("common", "ja");
        assertNotNull(rm);
        for (int i = 0; i < 3; i++) {
            assertTrue(rm.isValidResourceID(RES_BASE + i));
            assertEquals(res_ja[i], rm.getString(RES_BASE + i));
            assertEquals(res_ja[i], rm.getResource(RES_BASE + i));
        }

        /*
         * Retrieve device resources for "sk-SK" locale ("sk" resources are
         * hierarchically matched)
         */
        rm = null;
        rm = ResourceManager.getManager("common", "sk-SK");
        assertNotNull(rm);
        for (int i = 0; i < 3; i++) {
            assertTrue(rm.isValidResourceID(RES_BASE + i));
            assertEquals(res_sk[i], rm.getString(RES_BASE + i));
            assertEquals(res_sk[i], rm.getResource(RES_BASE + i));
        }

        /* Retrieve device resources for "sk" locale */
        rm = null;
        rm = ResourceManager.getManager("common", "sk");
        assertNotNull(rm);
        for (int i = 0; i < 3; i++) {
            assertTrue(rm.isValidResourceID(RES_BASE + i));
            assertEquals(res_sk[i], rm.getString(RES_BASE + i));
            assertEquals(res_sk[i], rm.getResource(RES_BASE + i));
        }

        /*
         * Retrieve device resources for "zh-CN" locale (common resources are
         * hierarchically matched)
         */
        rm = null;
        rm = ResourceManager.getManager("common", "zh-CN");
        assertNotNull(rm);
        for (int i = 0; i < 3; i++) {
            assertTrue(rm.isValidResourceID(RES_BASE + i));
            assertEquals(res_common[i], rm.getString(RES_BASE + i));
            assertEquals(res_common[i], rm.getResource(RES_BASE + i));
        }

        /*
         * Retrieve device resources for "zh" locale (common resources are
         * hierarchically matched)
         */
        rm = null;
        rm = ResourceManager.getManager("common", "zh");
        assertNotNull(rm);
        for (int i = 0; i < 3; i++) {
            assertTrue(rm.isValidResourceID(RES_BASE + i));
            assertEquals(res_common[i], rm.getString(RES_BASE + i));
            assertEquals(res_common[i], rm.getResource(RES_BASE + i));
        }

        /* Retrieve common device resources explicitly */
        rm = null;
        rm = ResourceManager.getManager("common", "");
        assertNotNull(rm);
        for (int i = 0; i < 3; i++) {
            assertTrue(rm.isValidResourceID(RES_BASE + i));
            assertEquals(res_common[i], rm.getString(RES_BASE + i));
            assertEquals(res_common[i], rm.getResource(RES_BASE + i));
        }
    
private voidtestValidConstruction()

        if (verbose) {
            System.out.println("## test testValidConstruction");
        }

        /* Retrieve JSR 238 version information */
        assertEquals("1.0", System.getProperty("microedition.global.version"));

        /* Create device resource manager for default locale */
        rm = null;
        rm = ResourceManager.getManager("common");
        assertNotNull(rm);
        assertEquals("common", rm.getBaseName());
        assertEquals(System.getProperty("microedition.locale"), rm.getLocale());

        /* Create device resource manager for common resources */
        rm = null;
        rm = ResourceManager.getManager("common", "");
        assertNotNull(rm);
        assertEquals("common", rm.getBaseName());
        assertEquals("", rm.getLocale());

        /* Create device resource manager for an existing locale */
        rm = null;
        rm = ResourceManager.getManager("common", "en-US");
        assertNotNull(rm);
        assertEquals("common", rm.getBaseName());
        assertEquals("en-US", rm.getLocale());

        /* Create device resource manager for an unsupported locale */
        rm = null;
        rm = ResourceManager.getManager("common", "fi-FI");
        assertNotNull(rm);
        assertEquals("common", rm.getBaseName());
        assertEquals("", rm.getLocale());

        /*
         * Create device resource manager for an unsupported locale with
         * variant
         */
        rm = null;
        rm = ResourceManager.getManager("common", "de-DE-utf8");
        assertNotNull(rm);
        assertEquals("common", rm.getBaseName());
        assertEquals("", rm.getLocale());

        /* Create device resource manager for a locale with underscore */
        rm = null;
        rm = ResourceManager.getManager("common", "sk_SK");
        assertNotNull(rm);
        assertEquals("common", rm.getBaseName());
        assertEquals("sk-SK", rm.getLocale());

        /*
         * Create device resource manager for a locale with variant and
         * underscore
         */
        rm = null;
        rm = ResourceManager.getManager("common", "he-IL_utf8");
        assertNotNull(rm);
        assertEquals("common", rm.getBaseName());
        assertEquals("he-IL", rm.getLocale());

        /*
         * Create device resource manager for a locale with variant and
         * underscores
         */
        rm = null;
        rm = ResourceManager.getManager("common", "cs_CZ_utf8");
        assertNotNull(rm);
        assertEquals("common", rm.getBaseName());
        assertEquals("cs-CZ", rm.getLocale());

        /*
         * Device resource manager for a list of locales,
         * "en" is the first supported locale in the list
         */
        rm = null;
        rm = ResourceManager.getManager("common", locs_match_en);
        assertNotNull(rm);
        assertEquals("common", rm.getBaseName());
        assertEquals("en", rm.getLocale());

        /*
         * Device resource manager for a list of locales,
         * "" (common) is the first supported locale in the list
         */
        rm = null;
        rm = ResourceManager.getManager("common",
                                        locs_match_empty);
        assertNotNull(rm);
        assertEquals("common", rm.getBaseName());
        assertEquals("", rm.getLocale());