FileDocCategorySizeDatePackage
TestRegReadWrite.javaAPI DocphoneME MR2 API (J2ME)11353Wed May 02 18:00:44 BST 2007com.sun.midp.content

TestRegReadWrite

public class TestRegReadWrite extends com.sun.midp.i3test.TestCase
Test RegistryStore functionality. Test write and read of ContentHandlerImpl.

Fields Summary
private static final int
suiteId
Constant application ID for testing.
private static final String
name
The name of the GraphicalInstaller.
private static final String
ID
Test ID.
private static final String
classname
The class of the GraphicalInstaller.
private String[]
types
The types registered.
private String[]
suffixes
The suffixes registered.
private String[]
actions
The actions registered.
private String[]
enActionNames
The english action names registered.
private String[]
frActionNames
The french action names registered.
private String[]
actionmaps
The action name maps to verify.
private String[]
actionlocales
The locale supported.
private String[]
accessRestricted
The restricted access.
private String[]
ZERO_STRINGS
A zero length array of strings.
private static final javax.microedition.content.ActionNameMap[]
ZERO_ACTIONNAMES
Empty ActionNameMap to return when needed.
Constructors Summary
Methods Summary
voidassertEquals(java.lang.String msg, ContentHandlerImpl expected, ContentHandlerImpl actual)
Compare two Content Handlers.

param
msg describing the test
param
expected the expected ContentHandler
param
actual the actual ContentHandler

        assertEquals("Verify storageId",
                     expected.storageId, actual.storageId);
        assertEquals("Verify classname",
                     expected.classname, actual.classname);
        assertEquals("Verify ID",
                     expected.ID, actual.ID);
        // Verify the dynamic flag
        assertTrue("Verify dynamic flag",
                     expected.registrationMethod == actual.registrationMethod);
    
voidassertEquals(java.lang.String msg, java.lang.String[] expected, java.lang.String[] actual)
Verify string arrays; asserting members are equals.

param
msg describing the test
param
expected the expected array of strings.
param
actual the actual array of strings.

        assertEquals(msg + " length",  expected.length, actual.length);
        int len = expected.length;
        if (len > actual.length) {
            len = actual.length;
        }
        for (int i = 0; i < len; i++) {
            assertEquals(msg + "[" + i + "]", expected[i], actual[i]);
        }

        // Report errors for any extra actuals
        for (int i = len; i < actual.length; i++) {
            assertEquals(msg + "[" + i + "]", null, actual[i]);
        }

        // Report errors for any extra expected
        for (int i = len; i < expected.length; i++) {
            assertEquals(msg + "[" + i + "]", actual[i], null);
        }
    
voidassertEquals(java.lang.String msg, javax.microedition.content.ActionNameMap expected, javax.microedition.content.ActionNameMap actual)
Verify that two ActionNameMaps are equal.

param
msg the message to print if a compare fails
param
expected the expected map
param
actual the actual map

        assertEquals("Verify locale",
                     expected.getLocale(), actual.getLocale());
        int size = expected.size();
        assertEquals(msg + " size ", size, actual.size());
        if (size == actual.size()) {
            for (int i = 0; i < size; i++) {
                assertEquals(msg + " action ",
                             expected.getAction(i),
                             actual.getAction(i));
                assertEquals(msg + " action name",
                             expected.getActionName(i),
                             actual.getActionName(i));
            }
        }
    
ContentHandlerImplmakeFull()
Make a contenthandlerImpl with everything set.

return
content handler object


        ActionNameMap [] actionnames = new ActionNameMap[2];
        actionnames[0] =
            new ActionNameMap(actions, enActionNames, "en_US");
        actionnames[1] =
            new ActionNameMap(actions, frActionNames, "fr_CH");

        ContentHandlerImpl ch = new ContentHandlerImpl(types, suffixes, actions,
                                                       actionnames, "ID-1",
                                                       accessRestricted,
                                                       "authority");
        ch.storageId = 61000;
        ch.appname = "Application Name";
        ch.version = "1.1.1";
        ch.classname = "classname";
        ch.registrationMethod = ContentHandlerImpl.REGISTERED_STATIC;
        return ch;
    
public voidrunTests()
Run the tests.


            
       
        test001();
    
voidtest001()
Test that the built-in suite is registered.

        boolean b;
        ContentHandlerImpl[] chArr;
        ContentHandlerImpl chTst;
        String[] arr;
        String caller = accessRestricted[0];
        String badStr = "_";

        declare("Verify the write and read of RegistryStore");

        ContentHandlerImpl ch = new ContentHandlerImpl(ZERO_STRINGS,
                                                       ZERO_STRINGS,
                                                       ZERO_STRINGS,
                                                       ZERO_ACTIONNAMES,
                                                       null,
                                                       ZERO_STRINGS,
                                                       "");

        assertNotNull("Verify handler created", ch);

        b = RegistryStore.register(ch);
        assertFalse("Verify empty handler is not registered", b);

        ActionNameMap[] actionnames = new ActionNameMap[2];
        actionnames[0] =
            new ActionNameMap(actions, enActionNames, "en_US");
        actionnames[1] =
            new ActionNameMap(actions, frActionNames, "fr_CH");


        ch = new ContentHandlerImpl(types, suffixes, actions, actionnames,
                                    ID, accessRestricted, "authority");

        ch.storageId = suiteId;
        ch.classname = classname;
        ch.appname   = name;

    // static boolean register(ContentHandlerImpl contentHandler) {
        b = RegistryStore.register(ch);
        assertTrue("Verify right handler is registered", b);


    /**
     * @param searchBy:
     * <code>BY_TYPE</code>,
     * <code>BY_SUFFIX</code>,
     * <code>BY_ACTION</code>,
     * <code>BY_ID</code>,
     * <code>BY_ID_EXACT</code>
     * <code>BY_SUITE</code>
     */
    // static String[] findHandler(String callerId, int searchBy, String value)
        chArr = RegistryStore.findHandler(caller, RegistryStore.FIELD_TYPES,
                                        ch.getType(0));
        if (chArr != null && chArr.length == 1) {
            assertEquals("Verify handler ID after search by type",
                         ch.ID, chArr[0].ID);
        } else {
            fail("Verify search handler by type");
        }

        chArr = RegistryStore.findHandler(caller, RegistryStore.FIELD_TYPES,
                                        badStr);
        assertTrue("Verify empty search results by type",
                    chArr == null || chArr.length == 0);

    // search by partial ID
    chArr = RegistryStore.findHandler(caller, RegistryStore.FIELD_ID,
                                        ch.ID + ch.ID.substring(0, 3));
    if (chArr != null && chArr.length == 1) {
        assertEquals("Verify handler ID after partial search", ch.ID, chArr[0].ID);
    } else {
        fail("Verify handler ID after search by cut ID");
    }

    // search by exact ID
    chTst = RegistryStore.getHandler(caller, ch.ID, RegistryStore.SEARCH_EXACT);
    if (chTst != null) {
        assertEquals("Verify handler search by ID exact", ch.ID, chTst.ID);
    } else {
        fail("Verify handler search by ID exact");
    }

     // get values
    arr = RegistryStore.getValues(caller, RegistryStore.FIELD_TYPES);
    assertTrue("Verify getValues by type", arr != null && arr.length >= 2);


    // testId
    b = testId(ch.ID);
    assertFalse("Verify test equal ID", b);

    b = testId(ch.ID.substring(0, 3));
    assertFalse("Verify test prefixed ID", b);

    b = testId(ch.ID+"qqq");
    assertFalse("Verify test prefixing ID", b);

    b = testId("qqq"+ch.ID);
    assertTrue("Verify test good ID", b);

    // load handler
    ContentHandlerImpl ch2 = RegistryStore.getHandler(null, ch.ID, 
                                                RegistryStore.SEARCH_EXACT);
    assertNotNull("Verify loadHandler by ID", ch2);

    // version and appname are not storable
    if (ch2 != null) {
        ch2.appname = ch.appname;
        ch2.version = ch.version;
        assertEquals("Verify loadHandler by ID", ch, ch2);
    }

    // unregister
    b = RegistryStore.unregister(ch.ID);
    assertTrue("Verify right handler is unregistered "+
                "/CHECK: if JSR 211 database has the correct format!/", b);
private booleantestId(java.lang.String ID)

        ContentHandlerImpl[] arr = 
            RegistryStore.findHandler(null, RegistryStore.FIELD_ID, ID);
        return arr == null || arr.length == 0;