FileDocCategorySizeDatePackage
TestTransactionStore.javaAPI DocphoneME MR2 API (J2ME)13334Wed May 02 18:00:44 BST 2007com.sun.j2me.payment

TestTransactionStore

public class TestTransactionStore extends TestCase
TestTransactionStore is a class for Transaction store testing
version
1.0

Fields Summary
private static final int
numOfApp
number of application to be simulated
private TransactionStore
store
Store to be tested
private TransactionProcessor
transProc
Dummy transaction processor
private TransactionModuleImpl
transMod
Dummy transaction module
private com.sun.midp.security.SecurityToken
token
SecurityToken for file operation
private static int
maxRecPerApp
Maximum records per application
private Transaction[]
transVector
Stored transaction for verification
private int
maxNumOfApp
maximum number of simulated application
private int[]
appIDArr
Array of application IDs
Constructors Summary
Methods Summary
private voidPrepareEnv()
Try to init CldcTransactionStore

throws
Exception if it is impossible to init CldcTransactionStore



                      
         
        token = getSecurityToken();

        MIDletStateHandler msHandler =
            MIDletStateHandler.getMidletStateHandler();
        MIDletSuite suite = msHandler.getMIDletSuite();
        suite.setTempProperty(token, MIDletSuiteImpl.SUITE_NAME_PROP,
                              "DummySuite");

        // add dummy payment info for CldcTransactionModuleImpl init
        suite.setTempProperty(token, "Pay-Version", "1.0");
        suite.setTempProperty(token, "Pay-Adapters", "PPSMS");
        suite.setTempProperty(token, "MIDlet-Permissions",
                              "javax.microedition.payment.process.jpp");
        suite.setTempProperty(token, "Pay-Update-Stamp",
                              "2004-11-15 02:00+01:00");
        suite.setTempProperty(token, "Pay-Update-URL",
                              "http://127.0.0.1/thisgame.manifest.jpp");
        suite.setTempProperty(token, "Pay-Providers", "SMS1");
        suite.setTempProperty(token, "Pay-Cache", "no");
        suite.setTempProperty(token, "Pay-Feature-0", "0");
        suite.setTempProperty(token, "Pay-SMS1-Info", "PPSMS, EUR,"
                              + System.getProperty("payment.mcc")
                              + ","
                              + System.getProperty("payment.mnc"));
        suite.setTempProperty(token, "Pay-SMS1-Tag-0",
                              "1.20, 9990000, 0x0cba98765400");

        // Start a new instance of DummyMIDlet
        msHandler.startMIDlet("com.sun.midp.util.DummyMIDlet",
                              "DummyMIDlet");

        // Wait for async request to be processed
        int i = 1000;
        while (DummyMIDlet.midlet == null && i > 0) {
            try {
                Thread.sleep(100);
                i--;
            } catch (InterruptedException ie) {}
        }

        if (DummyMIDlet.midlet == null)
            throw new Exception("Cann't create DummyMIDlet");

        // PPSMS adapter accepts null cinfiguration string
        transProc = PPSMSAdapter.getInstance(null);
        transMod = (TransactionModuleImpl)new CldcTransactionModuleImpl(
            DummyMIDlet.midlet);
        store = new CldcTransactionStoreImpl(token);
        store.cleanUp();
    
private voidVerification()
Written data verification

throws
Exception if something goes wrong with store

        store = null;

        // for future tests. For now it is not important
        System.gc();
        store = new CldcTransactionStoreImpl(token);
        TransactionRecord[] transRec;
        int transID = 0;
        int appID;
        Transaction trans = null;

        // checking for stored records and setDelivering functions
        for (int i = 0; i < maxNumOfApp; i++) {
            appID = appIDArr[i];
            transRec = store.getMissedTransactions(appID);
            assertEquals(
                "Number of stored records is different from expected one",
                transRec.length, maxRecPerApp);
            for (int j = 0; j < maxRecPerApp; j++) {
                trans = transVector[i][j];
                assertEquals(
                    "Critical error: FeatureId is different",
                    transRec[j].getFeatureID(),
                    trans.getFeatureID());
                assertEquals(
                    "Critical error: TransactionID is different",
                    transRec[j].getTransactionID(),
                    trans.getTransactionID());
                assertEquals("Critical error: State is different",
                             transRec[j].getState(),
                             trans.getState());

                // NOTE: max number of record to store is
                // PASSED_TRANSACTIONS_LIMIT
                store.setDelivered(transRec[j].getTransactionID());
            }
        }

        // should we notify about it?
        //  fail("Payload was not stored");

        // checking for passed records and functions
        int passedNum = 0;
        int appSize;
        for (int i = 0; i < maxNumOfApp; i++) {
            appID = appIDArr[i];
            assertNull("Problem with setDelivered",
                       store.getMissedTransactions(appID));
            transRec = store.getPassedTransactions(appID);
            if (transRec != null) {
                passedNum += transRec.length;
                for (int j = 0; j < transRec.length; j++) {
                    assertTrue(
                        "Passed transaction was not delivered",
                        transRec[j].wasMissed() != true);
                }
            }
            appSize = store.getSizeUsedByApplication(appID);
            store.removeApplicationRecords(appID);
            assertTrue("Problem with removeApplicationREcords",
                       appSize ==
                       store.getSizeUsedByApplication(appID));
        }

        if (passedNum > CldcTransactionStoreImpl.PASSED_TRANSACTIONS_LIMIT) {
            fail("passedNum is greater then " +
                 "CldcTransactionStoreImpl.PASSED_TRANSACTIONS_LIMIT");
        } else if (passedNum > maxNumOfApp * maxRecPerApp) {
            fail("passedNum is less then  maxNumOfApp * maxRecPerApp");
        }
        store.cleanUp();
    
private voidVerification2()
Written data verification. removeMissedTransaction test

throws
Exception if something goes wrong with store

        CldcTransactionStoreImpl store = new CldcTransactionStoreImpl(token);
        TransactionRecord[] transRec, transRec2;
        int transID = 0;
        int appID;
        Transaction trans = null;

        // checking for stored records and setDelivering functions
        for (int i = 0; i < maxNumOfApp; i++) {
            appID = appIDArr[i];
            transRec = store.getMissedTransactions(appID);
            assertEquals(
                "Number of stored records is different from expected one",
                transRec.length, maxRecPerApp);
            store.removeMissedTransaction(appID);
            transRec2 = store.getMissedTransactions(appID);
            assertNull("Problem with removeMissedTransaction()", transRec2);
        }
        store.cleanUp();
    
private voidWriteTest()
Starts N threads to test reserve and addTransaction functions simultaneously

throws
Exception if something goes wrong with store

        int i = 0;
	TestThread[] write2app = new TestThread[numOfApp];
        for (i = 0; i < numOfApp; i++) {
            /* Start thread to open Transaction store */
            write2app[i] = new TestThread(this, i);
            write2app[i].start();
        }

        // Make sure all thread are done before proceeding to the next test.
        for (i = 0; i < numOfApp; i++) {
            write2app[i].join();
        }
    
public voidrunTests()
Run tests

        boolean fail;
        try {
            declare("Prepare enviroment");
            PrepareEnv();
            assertTrue(true);

            declare("Write test");
            WriteTest();
            assertTrue(true);

            declare("Store verification");
            Verification();
            assertTrue(true);

            declare("Write test 2");
            WriteTest();
            assertTrue(true);

            declare("Store verification 2");
            Verification2();
            assertTrue(true);

        }
        catch (Exception e) {
            fail("test throws exception");
            e.printStackTrace();
        } finally {
            DummyMIDlet.midlet.notifyDestroyed();
        }