FileDocCategorySizeDatePackage
AutoTester.javaAPI DocphoneME MR2 API (J2ME)10712Wed May 02 18:00:04 BST 2007com.sun.midp.installer

AutoTester

public class AutoTester extends AutoTesterBase implements AutoTesterInterface
Installs/Updates a test suite, runs the first MIDlet in the suite in a loop specified number of iterations or until the new version of the suite is not found, then removes the suite.

The MIDlet uses these application properties as arguments:

  1. arg-0: URL for the test suite
  2. arg-1: Used to override the default domain used when installing an unsigned suite. The default is maximum to allow the runtime API tests be performed automatically without tester interaction. The domain name may be followed by a colon and a list of permissions that must be allowed even if they are not listed in the MIDlet-Permissions attribute in the application descriptor file. Instead of the list a keyword "all" can be specified indicating that all permissions must be allowed, for example: operator:all.
  3. arg-2: Integer number, specifying how many iterations to run the suite. If argument is not given or less then zero, then suite will be run until the new version of the suite is not found.

If arg-0 is not given then a form will be used to query the tester for the arguments.

Fields Summary
private static final String
AUTOTEST_STORE
Settings database name.
private static final int
URL_RECORD_ID
Record ID of URL.
private static final int
DOMAIN_RECORD_ID
Record ID of the security domain for unsigned suites.
private static final int
SUITE_ID_RECORD_ID
Record ID of suite ID.
private static final int
LOOP_COUNT_RECORD_ID
Record ID of loopCount
int
suiteId
ID of installed test suite.
Constructors Summary
public AutoTester()
Create and initialize a new auto tester MIDlet.


                 
      
        super();

        if (url != null) {
            startBackgroundTester();
        } else if (restoreSession()) {
            // continuation of a previous session
            startBackgroundTester();
        } else {
            /**
             * No URL has been provided, ask the user.
             *
             * commandAction will subsequently call startBackgroundTester.
             */
            getUrl();
        }
    
Methods Summary
private voidendSession()
End the testing session.

        try {
            RecordStore.deleteRecordStore(AUTOTEST_STORE);
        } catch (Throwable ex) {
            // ignore
        }

        notifyDestroyed();
    
public voidinstallAndPerformTests(com.sun.midp.midletsuite.MIDletSuiteStorage midletSuiteStorage, Installer inp_installer, java.lang.String inp_url)
Installs and performs the tests.

param
midletSuiteStorage MIDletSuiteStorage object
param
inp_installer Installer object
param
inp_url URL of the test suite


        MIDletInfo midletInfo;
        String message = null;

        if (loopCount != 0) {
            try {
                // force an overwrite and remove the RMS data
                suiteId = inp_installer.installJad(inp_url,
                    Constants.INTERNAL_STORAGE_ID, true, true, installListener);

                midletInfo = getFirstMIDletOfSuite(suiteId,
                        midletSuiteStorage);
                MIDletSuiteUtils.execute(suiteId,
                        midletInfo.classname, midletInfo.name);

                // We want auto tester MIDlet to run after the test is run.
                MIDletSuiteUtils.setLastSuiteToRun(
                    MIDletStateHandler.getMidletStateHandler().
                    getMIDletSuite().getID(),
                    getClass().getName(), null, null);

                if (loopCount > 0) {
                    loopCount -= 1;
                }
                saveSession();
                notifyDestroyed();
                return;
            } catch (Throwable t) {
                handleInstallerException(suiteId, t);
            }
        }

        if (midletSuiteStorage != null &&
                suiteId != MIDletSuite.UNUSED_SUITE_ID) {
            try {
                midletSuiteStorage.remove(suiteId);
            } catch (Throwable ex) {
                if (Logging.REPORT_LEVEL <= Logging.WARNING) {
                    Logging.report(Logging.WARNING, LogChannels.LC_AMS,
                                   "Throwable in remove");
                }
            }
        }

        endSession();
    
public booleanrestoreSession()
Restore the data from the last session.

return
true if there was data saved from the last session

        RecordStore settings = null;
        ByteArrayInputStream bas;
        DataInputStream dis;
        byte[] data;

        try {
            settings = RecordStore.openRecordStore(AUTOTEST_STORE, false);

            data = settings.getRecord(URL_RECORD_ID);
            if (data == null) {
                return false;
            }

            bas = new ByteArrayInputStream(data);
            dis = new DataInputStream(bas);
            url = dis.readUTF();

            data = settings.getRecord(DOMAIN_RECORD_ID);
            if (data != null && data.length > 0) {
                bas = new ByteArrayInputStream(data);
                dis = new DataInputStream(bas);
                domain = dis.readUTF();
            }

            data = settings.getRecord(SUITE_ID_RECORD_ID);
            if (data != null && data.length > 0) {
                bas = new ByteArrayInputStream(data);
                dis = new DataInputStream(bas);
                suiteId = dis.readInt();
            }

            data = settings.getRecord(LOOP_COUNT_RECORD_ID);
            if (data != null && data.length > 0) {
                bas = new ByteArrayInputStream(data);
                dis = new DataInputStream(bas);
                loopCount = dis.readInt();
            }

            return true;
        } catch (RecordStoreNotFoundException rsnfe) {
            // This normal when no initial args are given, ignore
        } catch (Exception ex) {
            displayException(Resource.getString
                             (ResourceConstants.EXCEPTION), ex.toString());
        } finally {
            if (settings != null) {
                try {
                    settings.closeRecordStore();
                } catch (Exception ex) {
                    if (Logging.REPORT_LEVEL <= Logging.WARNING) {
                        Logging.report(Logging.WARNING, LogChannels.LC_AMS,
                        "closeRecordStore threw an Exception");
                    }
                }
            }
        }

        return false;
    
public voidrun()
Run the installer.

        installAndPerformTests(midletSuiteStorage, installer, url);
    
private voidsaveSession()
Save session data for next time.

exception
if an exception occurs

        RecordStore settings = null;
        boolean newStore = false;
        ByteArrayOutputStream bas;
        DataOutputStream dos;
        byte[] data;

        if (url == null) {
            return;
        }

        try {
            settings = RecordStore.openRecordStore(AUTOTEST_STORE, true);

            if (settings.getNextRecordID() == URL_RECORD_ID) {
                newStore = true;
            }

            bas = new ByteArrayOutputStream();
            dos = new DataOutputStream(bas);
            dos.writeUTF(url);
            data = bas.toByteArray();

            if (newStore) {
                settings.addRecord(data, 0, data.length);
            } else {
                settings.setRecord(URL_RECORD_ID, data, 0, data.length);
            }

            bas.reset();
            dos.writeUTF(domain);
            data = bas.toByteArray();

            if (newStore) {
                settings.addRecord(data, 0, data.length);
            } else {
                settings.setRecord(DOMAIN_RECORD_ID, data, 0, data.length);
            }

            bas.reset();
            dos.writeInt(suiteId);
            data = bas.toByteArray();

            if (newStore) {
                settings.addRecord(data, 0, data.length);
            } else {
                settings.setRecord(SUITE_ID_RECORD_ID, data, 0, data.length);
            }

            bas.reset();
            dos.writeInt(loopCount);
            data = bas.toByteArray();

            if (newStore) {
                settings.addRecord(data, 0, data.length);
            } else {
                settings.setRecord(LOOP_COUNT_RECORD_ID, data, 0, data.length);
            }
        } finally {
            if (settings != null) {
                try {
                    settings.closeRecordStore();
                } catch (Exception ex) {
                    if (Logging.REPORT_LEVEL <= Logging.WARNING) {
                        Logging.report(Logging.WARNING, LogChannels.LC_AMS,
                        "closeRecordStore threw an exception");
                    }
                }
            }
        }