javamanager/javainstaller/installer/tsrc/javasrc/com/nokia/mj/impl/installer/storagehandler/StorageHandlerTest.java
branchRCL_3
changeset 19 04becd199f91
child 67 63b81d807542
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.mj.impl.installer.storagehandler;
       
    20 
       
    21 import com.nokia.mj.impl.installer.utils.InstallerMain;
       
    22 import com.nokia.mj.impl.installer.utils.Log;
       
    23 import com.nokia.mj.impl.storage.StorageAttribute;
       
    24 import com.nokia.mj.impl.storage.StorageEntry;
       
    25 import com.nokia.mj.impl.storage.StorageNames;
       
    26 import com.nokia.mj.impl.storage.StorageSession;
       
    27 import com.nokia.mj.impl.utils.Attribute;
       
    28 import com.nokia.mj.impl.utils.Uid;
       
    29 import com.nokia.mj.impl.utils.Version;
       
    30 
       
    31 import java.util.Enumeration;
       
    32 import java.util.Hashtable;
       
    33 import java.util.Vector;
       
    34 
       
    35 import j2meunit.framework.Test;
       
    36 import j2meunit.framework.TestCase;
       
    37 import j2meunit.framework.TestMethod;
       
    38 import j2meunit.framework.TestSuite;
       
    39 
       
    40 /**
       
    41  * StorageHandler unit tests.
       
    42  */
       
    43 public class StorageHandlerTest extends TestCase implements InstallerMain
       
    44 {
       
    45     private static final int MAX_SUITE_COUNT = 8;
       
    46     private StorageHandler iStorageHandler = null;
       
    47 
       
    48     // Begin j2meunit test framework setup
       
    49 
       
    50     public void installerMain(String[] args)
       
    51     {
       
    52         TestSuite suite = new TestSuite(this.getClass().getName());
       
    53 
       
    54         suite.addTest(new StorageHandlerTest("testSuiteComparison", new TestMethod()
       
    55         {
       
    56             public void run(TestCase tc)
       
    57             {
       
    58                 ((StorageHandlerTest)tc).testSuiteComparison();
       
    59             }
       
    60         }));
       
    61 
       
    62         suite.addTest(new StorageHandlerTest("testCommitTwice", new TestMethod()
       
    63         {
       
    64             public void run(TestCase tc)
       
    65             {
       
    66                 ((StorageHandlerTest)tc).testCommitTwice();
       
    67             }
       
    68         }));
       
    69 
       
    70         suite.addTest(new StorageHandlerTest("testRollbackTwice", new TestMethod()
       
    71         {
       
    72             public void run(TestCase tc)
       
    73             {
       
    74                 ((StorageHandlerTest)tc).testRollbackTwice();
       
    75             }
       
    76         }));
       
    77 
       
    78         suite.addTest(new StorageHandlerTest("testCommitRollback", new TestMethod()
       
    79         {
       
    80             public void run(TestCase tc)
       
    81             {
       
    82                 ((StorageHandlerTest)tc).testCommitRollback();
       
    83             }
       
    84         }));
       
    85 
       
    86         suite.addTest(new StorageHandlerTest("testRollbackCommit", new TestMethod()
       
    87         {
       
    88             public void run(TestCase tc)
       
    89             {
       
    90                 ((StorageHandlerTest)tc).testRollbackCommit();
       
    91             }
       
    92         }));
       
    93 
       
    94         suite.addTest(new StorageHandlerTest("testWriteReadByUidRemove", new TestMethod()
       
    95         {
       
    96             public void run(TestCase tc)
       
    97             {
       
    98                 ((StorageHandlerTest)tc).testWriteReadByUidRemove();
       
    99             }
       
   100         }));
       
   101 
       
   102         suite.addTest(new StorageHandlerTest("testWriteReadByNameAndVendorRemove", new TestMethod()
       
   103         {
       
   104             public void run(TestCase tc)
       
   105             {
       
   106                 ((StorageHandlerTest)tc).testWriteReadByNameAndVendorRemove();
       
   107             }
       
   108         }));
       
   109 
       
   110         suite.addTest(new StorageHandlerTest("testWriteCommitReadByUidRemoveCommit", new TestMethod()
       
   111         {
       
   112             public void run(TestCase tc)
       
   113             {
       
   114                 ((StorageHandlerTest)tc).testWriteCommitReadByUidRemoveCommit();
       
   115             }
       
   116         }));
       
   117 
       
   118         suite.addTest(new StorageHandlerTest("testWriteCommitReadByNameAndVendorRemoveCommit", new TestMethod()
       
   119         {
       
   120             public void run(TestCase tc)
       
   121             {
       
   122                 ((StorageHandlerTest)tc).testWriteCommitReadByNameAndVendorRemoveCommit();
       
   123             }
       
   124         }));
       
   125 
       
   126         suite.addTest(new StorageHandlerTest("testWriteRollbackReadByUid", new TestMethod()
       
   127         {
       
   128             public void run(TestCase tc)
       
   129             {
       
   130                 ((StorageHandlerTest)tc).testWriteRollbackReadByUid();
       
   131             }
       
   132         }));
       
   133 
       
   134         suite.addTest(new StorageHandlerTest("testWriteRollbackReadByNameAndVendor", new TestMethod()
       
   135         {
       
   136             public void run(TestCase tc)
       
   137             {
       
   138                 ((StorageHandlerTest)tc).testWriteRollbackReadByNameAndVendor();
       
   139             }
       
   140         }));
       
   141 
       
   142         suite.addTest(new StorageHandlerTest("testAllocateUid", new TestMethod()
       
   143         {
       
   144             public void run(TestCase tc)
       
   145             {
       
   146                 ((StorageHandlerTest)tc).testAllocateUid();
       
   147             }
       
   148         }));
       
   149 
       
   150         suite.addTest(new StorageHandlerTest("testGetSuiteUids", new TestMethod()
       
   151         {
       
   152             public void run(TestCase tc)
       
   153             {
       
   154                 ((StorageHandlerTest)tc).testGetSuiteUids();
       
   155             }
       
   156         }));
       
   157 
       
   158         suite.addTest(new StorageHandlerTest("testGetSuiteUidFromAppUid", new TestMethod()
       
   159         {
       
   160             public void run(TestCase tc)
       
   161             {
       
   162                 ((StorageHandlerTest)tc).testGetSuiteUidFromAppUid();
       
   163             }
       
   164         }));
       
   165 
       
   166         suite.addTest(new StorageHandlerTest("testGetSuiteUidFromSuiteUid", new TestMethod()
       
   167         {
       
   168             public void run(TestCase tc)
       
   169             {
       
   170                 ((StorageHandlerTest)tc).testGetSuiteUidFromSuiteUid();
       
   171             }
       
   172         }));
       
   173 
       
   174         suite.addTest(new StorageHandlerTest("testGetAppUids", new TestMethod()
       
   175         {
       
   176             public void run(TestCase tc)
       
   177             {
       
   178                 ((StorageHandlerTest)tc).testGetAppUids();
       
   179             }
       
   180         }));
       
   181 
       
   182         suite.addTest(new StorageHandlerTest("testInstallPreinstall", new TestMethod()
       
   183         {
       
   184             public void run(TestCase tc)
       
   185             {
       
   186                 ((StorageHandlerTest)tc).testInstallPreinstall();
       
   187             }
       
   188         }));
       
   189 
       
   190         suite.addTest(new StorageHandlerTest("testPreinstallInstall", new TestMethod()
       
   191         {
       
   192             public void run(TestCase tc)
       
   193             {
       
   194                 ((StorageHandlerTest)tc).testPreinstallInstall();
       
   195             }
       
   196         }));
       
   197 
       
   198         com.nokia.mj.impl.utils.OmjTestRunner.run(suite);
       
   199     }
       
   200 
       
   201     public StorageHandlerTest()
       
   202     {
       
   203     }
       
   204 
       
   205     public StorageHandlerTest(String aTestName, TestMethod aTestMethod)
       
   206     {
       
   207         super(aTestName, aTestMethod);
       
   208     }
       
   209 
       
   210     public void assertFalse(String aMsg, boolean aCondition)
       
   211     {
       
   212         assertTrue(aMsg, !aCondition);
       
   213     }
       
   214 
       
   215     // End j2meunit test framework setup
       
   216 
       
   217     protected void setUp()
       
   218     {
       
   219         //System.out.println("----" + getName() + " setUp----");
       
   220         SuiteInfo[] suites = new SuiteInfo[MAX_SUITE_COUNT];
       
   221         for (int i = 0; i < suites.length; i++)
       
   222         {
       
   223             suites[i] = createSuiteInfo(i);
       
   224             cleanSuiteFromStorage(suites[i]);
       
   225         }
       
   226         iStorageHandler = new StorageHandler();
       
   227     }
       
   228 
       
   229     protected void tearDown()
       
   230     {
       
   231         iStorageHandler.close();
       
   232         iStorageHandler = null;
       
   233         //System.out.println("----" + getName() + " tearDown----");
       
   234     }
       
   235 
       
   236     public void testSuiteComparison()
       
   237     {
       
   238         SuiteInfo suite = createSuiteInfo();
       
   239         SuiteInfo suite2 = createSuiteInfo();
       
   240         // Test code for verifying that suite comparison works.
       
   241         assertTrue("expected suite to be equal", equals(suite, suite));
       
   242         assertTrue("expected suite2 to be equal", equals(suite, suite2));
       
   243         suite2.setAttribute("MIDlet-Version", "1.1");
       
   244         assertFalse("expected suite2 to not be equal", equals(suite, suite2));
       
   245     }
       
   246 
       
   247     public void testCommitTwice()
       
   248     {
       
   249         tryStartSession();
       
   250         SuiteInfo suite = createSuiteInfo();
       
   251         SuiteInfo suiteOrig = suite;
       
   252         tryWriteSuite(suite);
       
   253         tryCommitSession();
       
   254         tryCommitSessionFails();
       
   255     }
       
   256 
       
   257     public void testRollbackTwice()
       
   258     {
       
   259         tryStartSession();
       
   260         SuiteInfo suite = createSuiteInfo();
       
   261         SuiteInfo suiteOrig = suite;
       
   262         tryWriteSuite(suite);
       
   263         tryRollbackSession();
       
   264         tryRollbackSessionFails();
       
   265     }
       
   266 
       
   267     public void testCommitRollback()
       
   268     {
       
   269         tryStartSession();
       
   270         SuiteInfo suite = createSuiteInfo();
       
   271         SuiteInfo suiteOrig = suite;
       
   272         tryWriteSuite(suite);
       
   273         tryCommitSession();
       
   274         tryRollbackSessionFails();
       
   275     }
       
   276 
       
   277     public void testRollbackCommit()
       
   278     {
       
   279         tryStartSession();
       
   280         SuiteInfo suite = createSuiteInfo();
       
   281         SuiteInfo suiteOrig = suite;
       
   282         tryWriteSuite(suite);
       
   283         tryRollbackSession();
       
   284         tryCommitSessionFails();
       
   285     }
       
   286 
       
   287     public void testWriteReadByUidRemove()
       
   288     {
       
   289         tryStartSession();
       
   290         SuiteInfo suite = createSuiteInfo();
       
   291         SuiteInfo suiteOrig = suite;
       
   292         tryWriteSuite(suite);
       
   293         suite = tryReadSuite(suiteOrig.getUid());
       
   294         assertTrue("expected suite from storage to be equal with original",
       
   295                    equals(suite, suiteOrig));
       
   296 
       
   297         tryRemoveSuite(suiteOrig);
       
   298         tryReadNoSuite(suiteOrig.getUid());
       
   299 
       
   300         tryRollbackSession();
       
   301 
       
   302         // After rollback we have to get a new StorageHandler instance.
       
   303         iStorageHandler = new StorageHandler();
       
   304         tryReadNoSuite(suiteOrig.getUid());
       
   305         iStorageHandler.close();
       
   306     }
       
   307 
       
   308     public void testWriteReadByNameAndVendorRemove()
       
   309     {
       
   310         tryStartSession();
       
   311         SuiteInfo suite = createSuiteInfo();
       
   312         SuiteInfo suiteOrig = suite;
       
   313         tryWriteSuite(suite);
       
   314         suite = tryReadSuite(suiteOrig.getName(), suiteOrig.getVendor());
       
   315         assertTrue("expected suite from storage to be equal with original",
       
   316                    equals(suite, suiteOrig));
       
   317 
       
   318         tryRemoveSuite(suiteOrig);
       
   319         tryReadNoSuite(suiteOrig.getUid());
       
   320 
       
   321         tryRollbackSession();
       
   322 
       
   323         // After rollback we have to get a new StorageHandler instance.
       
   324         iStorageHandler = new StorageHandler();
       
   325         tryReadNoSuite(suiteOrig.getUid());
       
   326         iStorageHandler.close();
       
   327     }
       
   328 
       
   329     public void testWriteCommitReadByUidRemoveCommit()
       
   330     {
       
   331         tryStartSession();
       
   332         SuiteInfo suite = createSuiteInfo();
       
   333         SuiteInfo suiteOrig = suite;
       
   334         tryWriteSuite(suite);
       
   335         suite = tryReadSuite(suiteOrig.getUid());
       
   336         assertTrue("expected suite from storage to be equal with original",
       
   337                    equals(suite, suiteOrig));
       
   338 
       
   339         tryCommitSession();
       
   340 
       
   341         // After commit we have to get a new StorageHandler instance.
       
   342         iStorageHandler = new StorageHandler();
       
   343         suite = tryReadSuite(suiteOrig.getUid());
       
   344         assertTrue("expected suite from storage to be equal with original",
       
   345                    equals(suite, suiteOrig));
       
   346         iStorageHandler.close();
       
   347 
       
   348         iStorageHandler = new StorageHandler();
       
   349         tryStartSession();
       
   350 
       
   351         tryRemoveSuite(suiteOrig);
       
   352         tryReadNoSuite(suiteOrig.getUid());
       
   353 
       
   354         tryCommitSession();
       
   355 
       
   356         // After commit we have to get a new StorageHandler instance.
       
   357         iStorageHandler = new StorageHandler();
       
   358         tryReadNoSuite(suiteOrig.getUid());
       
   359         iStorageHandler.close();
       
   360     }
       
   361 
       
   362     public void testWriteCommitReadByNameAndVendorRemoveCommit()
       
   363     {
       
   364         tryStartSession();
       
   365         SuiteInfo suite = createSuiteInfo();
       
   366         SuiteInfo suiteOrig = suite;
       
   367         tryWriteSuite(suite);
       
   368         suite = tryReadSuite(suiteOrig.getName(), suiteOrig.getVendor());
       
   369         assertTrue("expected suite from storage to be equal with original",
       
   370                    equals(suite, suiteOrig));
       
   371 
       
   372         tryCommitSession();
       
   373 
       
   374         // After commit we have to get a new StorageHandler instance.
       
   375         iStorageHandler = new StorageHandler();
       
   376         suite = tryReadSuite(suiteOrig.getName(), suiteOrig.getVendor());
       
   377         assertTrue("expected suite from storage to be equal with original",
       
   378                    equals(suite, suiteOrig));
       
   379         iStorageHandler.close();
       
   380 
       
   381         iStorageHandler = new StorageHandler();
       
   382         tryStartSession();
       
   383 
       
   384         tryRemoveSuite(suiteOrig);
       
   385         tryReadNoSuite(suiteOrig.getUid());
       
   386 
       
   387         tryCommitSession();
       
   388 
       
   389         // After commit we have to get a new StorageHandler instance.
       
   390         iStorageHandler = new StorageHandler();
       
   391         tryReadNoSuite(suiteOrig.getUid());
       
   392         iStorageHandler.close();
       
   393     }
       
   394 
       
   395     public void testWriteRollbackReadByUid()
       
   396     {
       
   397         tryStartSession();
       
   398         SuiteInfo suite = createSuiteInfo();
       
   399         SuiteInfo suiteOrig = suite;
       
   400         tryWriteSuite(suite);
       
   401         suite = tryReadSuite(suiteOrig.getUid());
       
   402         assertTrue("expected suite from storage to be equal with original",
       
   403                    equals(suite, suiteOrig));
       
   404 
       
   405         tryRollbackSession();
       
   406 
       
   407         // After rollback we have to get a new StorageHandler instance.
       
   408         iStorageHandler = new StorageHandler();
       
   409         tryReadNoSuite(suiteOrig.getUid());
       
   410         iStorageHandler.close();
       
   411     }
       
   412 
       
   413     public void testWriteRollbackReadByNameAndVendor()
       
   414     {
       
   415         tryStartSession();
       
   416         SuiteInfo suite = createSuiteInfo();
       
   417         SuiteInfo suiteOrig = suite;
       
   418         tryWriteSuite(suite);
       
   419         suite = tryReadSuite(suiteOrig.getName(), suiteOrig.getVendor());
       
   420         assertTrue("expected suite from storage to be equal with original",
       
   421                    equals(suite, suiteOrig));
       
   422 
       
   423         tryRollbackSession();
       
   424 
       
   425         // After rollback we have to get a new StorageHandler instance.
       
   426         iStorageHandler = new StorageHandler();
       
   427         tryReadNoSuite(suiteOrig.getUid());
       
   428         iStorageHandler.close();
       
   429     }
       
   430 
       
   431     public void testAllocateUid()
       
   432     {
       
   433         Uid uid = iStorageHandler.allocateUid("");
       
   434         assertTrue("expected uid to be not null", uid != null);
       
   435     }
       
   436 
       
   437     public void testGetSuiteUids()
       
   438     {
       
   439         tryStartSession();
       
   440         SuiteInfo[] suites = new SuiteInfo[MAX_SUITE_COUNT];
       
   441         for (int i = 0; i < suites.length; i++)
       
   442         {
       
   443             suites[i] = createSuiteInfo(i);
       
   444             tryWriteSuite(suites[i]);
       
   445         }
       
   446 
       
   447         try
       
   448         {
       
   449             Uid[] uids = iStorageHandler.getSuiteUids();
       
   450             assertTrue("expected uids to be not null", uids != null);
       
   451             assertTrue("expected uids.length to be suites.length",
       
   452                        uids.length == suites.length);
       
   453         }
       
   454         catch (Exception ex)
       
   455         {
       
   456             ex.printStackTrace();
       
   457             assertTrue("Exception at getSuiteUids: " + ex.toString(), false);
       
   458         }
       
   459 
       
   460         tryRollbackSession();
       
   461 
       
   462         try
       
   463         {
       
   464             StorageHandler storageHandler = new StorageHandler();
       
   465             Uid[] uids = storageHandler.getSuiteUids();
       
   466             storageHandler.close();
       
   467             assertTrue("expected uids to be null", uids == null);
       
   468         }
       
   469         catch (Exception ex)
       
   470         {
       
   471             ex.printStackTrace();
       
   472             assertTrue("Exception at getSuiteUids after rollback: " +
       
   473                        ex.toString(), false);
       
   474         }
       
   475     }
       
   476 
       
   477     public void testGetSuiteUidFromAppUid()
       
   478     {
       
   479         tryStartSession();
       
   480         SuiteInfo suite = createSuiteInfo();
       
   481         tryWriteSuite(suite);
       
   482 
       
   483         Uid[] appUids = suite.getApplicationUids();
       
   484         assertTrue("expected appUids not to be null", appUids != null);
       
   485         for (int i = 0; i < appUids.length; i++)
       
   486         {
       
   487             Uid uid = iStorageHandler.getSuiteUid(appUids[i]);
       
   488             assertTrue("expected uids to be equal", suite.getUid().equals(uid));
       
   489         }
       
   490 
       
   491         tryRollbackSession();
       
   492     }
       
   493 
       
   494     public void testGetSuiteUidFromSuiteUid()
       
   495     {
       
   496         tryStartSession();
       
   497         SuiteInfo suite = createSuiteInfo();
       
   498         tryWriteSuite(suite);
       
   499 
       
   500         Uid uid = iStorageHandler.getSuiteUid(suite.getUid());
       
   501         assertTrue("expected uids to be equal", suite.getUid().equals(uid));
       
   502 
       
   503         tryRollbackSession();
       
   504     }
       
   505 
       
   506     public void testGetAppUids()
       
   507     {
       
   508         tryStartSession();
       
   509         SuiteInfo suite = createSuiteInfo();
       
   510         tryWriteSuite(suite);
       
   511 
       
   512         Uid[] appUidsOrig = suite.getApplicationUids();
       
   513         Uid[] appUids = iStorageHandler.getAppUids(suite.getUid());
       
   514         assertTrue("expected appUids not to be null", appUids != null);
       
   515         assertTrue("appUids length does not match",
       
   516                    appUidsOrig.length == appUids.length);
       
   517         boolean uidFound = false;
       
   518         for (int i = 0; i < appUids.length; i++)
       
   519         {
       
   520             for (int j = 0; j < appUidsOrig.length; j++)
       
   521             {
       
   522                 if (appUids[i].equals(appUidsOrig[j]))
       
   523                 {
       
   524                     // Found matching uid.
       
   525                     uidFound = true;
       
   526                     break;
       
   527                 }
       
   528             }
       
   529         }
       
   530         assertTrue("incorrect uid returned: " + appUids +
       
   531                    ", expected: " + appUidsOrig, uidFound);
       
   532 
       
   533         tryRollbackSession();
       
   534     }
       
   535 
       
   536     public void testInstallPreinstall()
       
   537     {
       
   538         tryStartSession();
       
   539         SuiteInfo suiteOrig = createSuiteInfo();
       
   540 
       
   541         suiteOrig.iPreinstallState = SuiteInfo.STATE_INSTALLED;
       
   542         tryWriteSuite(suiteOrig);
       
   543         SuiteInfo suite = tryReadSuite(suiteOrig.getUid());
       
   544         assertTrue("suite is null", suite != null);
       
   545         assertTrue("preinstall state != INSTALLED: " + suite.iPreinstallState,
       
   546                    suite.iPreinstallState == SuiteInfo.STATE_INSTALLED);
       
   547 
       
   548         tryRemoveSuite(suiteOrig);
       
   549         int preinstallState = readPreinstallState(suiteOrig);
       
   550         assertTrue("preinstall state != -1: " + preinstallState,
       
   551                    preinstallState == -1);
       
   552 
       
   553         suiteOrig.iPreinstallState = SuiteInfo.STATE_PREINSTALLED;
       
   554         tryWriteSuite(suiteOrig);
       
   555         suite = tryReadSuite(suiteOrig.getUid());
       
   556         assertTrue("suite is null", suite != null);
       
   557         assertTrue("preinstall state 2 != PREINSTALLED: " + suite.iPreinstallState,
       
   558                    suite.iPreinstallState == SuiteInfo.STATE_PREINSTALLED);
       
   559 
       
   560         tryRemoveSuite(suiteOrig);
       
   561         tryReadNoSuite(suiteOrig.getUid());
       
   562 
       
   563         preinstallState = readPreinstallState(suiteOrig);
       
   564         assertTrue("preinstall state 3 != NO_PREINSTALL: " + preinstallState,
       
   565                    preinstallState == SuiteInfo.STATE_NO_PREINSTALL);
       
   566 
       
   567         tryRollbackSession();
       
   568     }
       
   569 
       
   570     public void testPreinstallInstall()
       
   571     {
       
   572         tryStartSession();
       
   573         SuiteInfo suiteOrig = createSuiteInfo();
       
   574 
       
   575         suiteOrig.iPreinstallState = SuiteInfo.STATE_PREINSTALLED;
       
   576         tryWriteSuite(suiteOrig);
       
   577         SuiteInfo suite = tryReadSuite(suiteOrig.getUid());
       
   578         assertTrue("suite is null", suite != null);
       
   579         assertTrue("preinstall state != PREINSTALLED: " + suite.iPreinstallState,
       
   580                    suite.iPreinstallState == SuiteInfo.STATE_PREINSTALLED);
       
   581 
       
   582         tryRemoveSuite(suiteOrig);
       
   583         int preinstallState = readPreinstallState(suiteOrig);
       
   584         assertTrue("preinstall state 2 != NO_PREINSTALL: " + preinstallState,
       
   585                    preinstallState == SuiteInfo.STATE_NO_PREINSTALL);
       
   586 
       
   587         suiteOrig.iPreinstallState = SuiteInfo.STATE_INSTALLED;
       
   588         tryWriteSuite(suiteOrig);
       
   589         suite = tryReadSuite(suiteOrig.getUid());
       
   590         assertTrue("suite is null", suite != null);
       
   591         assertTrue("preinstall state 3 != NO_PREINSTALL: " + suite.iPreinstallState,
       
   592                    suite.iPreinstallState == SuiteInfo.STATE_NO_PREINSTALL);
       
   593 
       
   594         tryRemoveSuite(suiteOrig);
       
   595         preinstallState = readPreinstallState(suiteOrig);
       
   596         assertTrue("preinstall state 4 != NO_PREINSTALL: " + preinstallState,
       
   597                    preinstallState == SuiteInfo.STATE_NO_PREINSTALL);
       
   598 
       
   599         suiteOrig.iPreinstallState = SuiteInfo.STATE_PREINSTALLED;
       
   600         tryWriteSuite(suiteOrig);
       
   601         suite = tryReadSuite(suiteOrig.getUid());
       
   602         assertTrue("suite is null", suite != null);
       
   603         assertTrue("preinstall state 5 != PREINSTALLED: " + suite.iPreinstallState,
       
   604                    suite.iPreinstallState == SuiteInfo.STATE_PREINSTALLED);
       
   605 
       
   606         tryRollbackSession();
       
   607     }
       
   608 
       
   609     private SuiteInfo createSuiteInfo()
       
   610     {
       
   611         return createSuiteInfo(0);
       
   612     }
       
   613 
       
   614     private SuiteInfo createSuiteInfo(int aIndex)
       
   615     {
       
   616         Uid uid = Uid.createUid("[eeff000" + aIndex + "]");
       
   617         SuiteInfo suiteInfo = new SuiteInfo(uid);
       
   618         suiteInfo.setName("TestSuite");
       
   619         suiteInfo.setVendor("Nokia");
       
   620         suiteInfo.setVersion(Version.getVersion("1.0"));
       
   621         suiteInfo.setRootDir("/private/midlets/" + uid.getStringValue());
       
   622         suiteInfo.setJadPath(suiteInfo.getRootDir() + "/TestMidlet.jad");
       
   623         suiteInfo.setJarPath(suiteInfo.getRootDir() + "/TestMidlet.jar");
       
   624         suiteInfo.setMediaId(1234);
       
   625         suiteInfo.setInitialSize(50000);
       
   626         suiteInfo.setOnScreenKeypad(SuiteInfo.OSK_NO);
       
   627         suiteInfo.setAccessPoint("0");
       
   628         Hashtable attrs = new Hashtable();
       
   629         attrs.put("Created-By",
       
   630                   new Attribute("Created-By", "1.5.0_06 (Sun Microsystems Inc.)"));
       
   631         attrs.put("Manifest-Version",
       
   632                   new Attribute("Manifest-Version", "1.0"));
       
   633         attrs.put("MicroEdition-Configuration",
       
   634                   new Attribute("MicroEdition-Configuration", "CLDC-1.0"));
       
   635         attrs.put("MIDlet-Name",
       
   636                   new Attribute("MIDlet-Name", "TestSuite" + aIndex));
       
   637         attrs.put("MIDlet-Vendor",
       
   638                   new Attribute("MIDlet-Vendor", "Nokia"));
       
   639         attrs.put("MIDlet-1",
       
   640                   new Attribute("MIDlet-1", "MIDlet1, , test.MIDlet1"));
       
   641         attrs.put("MIDlet-2",
       
   642                   new Attribute("MIDlet-2", "MIDlet2, , test.MIDlet2"));
       
   643         attrs.put("MIDlet-Version",
       
   644                   new Attribute("MIDlet-Version", "1.0"));
       
   645         attrs.put("MicroEdition-Profile",
       
   646                   new Attribute("MicroEdition-Profile", "MIDP-1.0"));
       
   647         suiteInfo.setAttributes(attrs);
       
   648         Vector apps = new Vector();
       
   649         apps.addElement(new ApplicationInfo(Uid.createUid("[eeff001" + aIndex + "]"),
       
   650                                             "MIDlet-1", "", "test.MIDlet1", 0));
       
   651         apps.addElement(new ApplicationInfo(Uid.createUid("[eeff002" + aIndex + "]"),
       
   652                                             "MIDlet-2", "", "test.MIDlet2", 0));
       
   653         suiteInfo.setApplications(apps);
       
   654         return suiteInfo;
       
   655     }
       
   656 
       
   657     private boolean equals(SuiteInfo aSuiteInfo1, SuiteInfo aSuiteInfo2)
       
   658     {
       
   659         boolean result = true;
       
   660         if ((aSuiteInfo1 == null && aSuiteInfo2 != null) ||
       
   661                 (aSuiteInfo1 != null && aSuiteInfo2 == null))
       
   662         {
       
   663             logEquals("other suite is null");
       
   664             result = false;
       
   665         }
       
   666         if (result && !aSuiteInfo1.getUid().equals(aSuiteInfo2.getUid()))
       
   667         {
       
   668             logEquals("uids don't match");
       
   669             result = false;
       
   670         }
       
   671         if (result && !aSuiteInfo1.getName().equals(aSuiteInfo2.getName()))
       
   672         {
       
   673             logEquals("names don't match");
       
   674             result = false;
       
   675         }
       
   676         if (result && !aSuiteInfo1.getVendor().equals(aSuiteInfo2.getVendor()))
       
   677         {
       
   678             logEquals("vendors don't match");
       
   679             result = false;
       
   680         }
       
   681         if (result && !aSuiteInfo1.getVersion().equals(aSuiteInfo2.getVersion()))
       
   682         {
       
   683             logEquals("versions don't match");
       
   684             result = false;
       
   685         }
       
   686         if (result && !aSuiteInfo1.getJadPath().equals(aSuiteInfo2.getJadPath()))
       
   687         {
       
   688             logEquals("jadPaths don't match");
       
   689             result = false;
       
   690         }
       
   691         if (result && !aSuiteInfo1.getJarPath().equals(aSuiteInfo2.getJarPath()))
       
   692         {
       
   693             logEquals("jarPaths don't match");
       
   694             result = false;
       
   695         }
       
   696         if (result && !aSuiteInfo1.getRootDir().equals(aSuiteInfo2.getRootDir()))
       
   697         {
       
   698             logEquals("rootDirs don't match");
       
   699             result = false;
       
   700         }
       
   701         if (result && !aSuiteInfo1.getAccessPoint().equals(aSuiteInfo2.getAccessPoint()))
       
   702         {
       
   703             logEquals("accessPoints don't match");
       
   704             result = false;
       
   705         }
       
   706         if (result && aSuiteInfo1.getMediaId() != aSuiteInfo2.getMediaId())
       
   707         {
       
   708             logEquals("mediaIds don't match");
       
   709             result = false;
       
   710         }
       
   711         if (result && aSuiteInfo1.getInitialSize() !=
       
   712                 aSuiteInfo2.getInitialSize())
       
   713         {
       
   714             logEquals("initSize don't match");
       
   715             result = false;
       
   716         }
       
   717         if (result && aSuiteInfo1.getOnScreenKeypad() !=
       
   718                 aSuiteInfo2.getOnScreenKeypad())
       
   719         {
       
   720             logEquals("onScreenKeypads don't match");
       
   721             result = false;
       
   722         }
       
   723         if (result)
       
   724         {
       
   725             Vector apps1 = aSuiteInfo1.getApplications();
       
   726             Vector apps2 = aSuiteInfo2.getApplications();
       
   727             if ((apps1 == null && apps2 != null) ||
       
   728                     (apps1 != null && apps2 == null))
       
   729             {
       
   730                 logEquals("other app is null");
       
   731                 result = false;
       
   732             }
       
   733             if (result && apps1.size() != apps2.size())
       
   734             {
       
   735                 logEquals("app sizes don't match");
       
   736                 result = false;
       
   737             }
       
   738             if (result)
       
   739             {
       
   740                 for (int i = 0; i < apps1.size(); i++)
       
   741                 {
       
   742                     if (!apps1.elementAt(i).equals(apps2.elementAt(i)))
       
   743                     {
       
   744                         logEquals("apps[" + i + "] don't match");
       
   745                         logEquals("apps1: " + apps1.elementAt(i));
       
   746                         logEquals("apps2: " + apps2.elementAt(i));
       
   747                         result = false;
       
   748                         break;
       
   749                     }
       
   750                 }
       
   751             }
       
   752         }
       
   753         if (result && !equalTables(aSuiteInfo1.getAttributes(),
       
   754                                    aSuiteInfo2.getAttributes()))
       
   755         {
       
   756             logEquals("attributes don't match");
       
   757             result = false;
       
   758         }
       
   759         return result;
       
   760     }
       
   761 
       
   762     private boolean equalTables(Hashtable t1, Hashtable t2)
       
   763     {
       
   764         if (t1 == null && t2 == null)
       
   765         {
       
   766             return true;
       
   767         }
       
   768         boolean result = true;
       
   769         if ((t1 == null && t2 != null) ||
       
   770                 (t1 != null && t2 == null))
       
   771         {
       
   772             result = false;
       
   773         }
       
   774         if (result && t1.size() != t2.size())
       
   775         {
       
   776             result = false;
       
   777         }
       
   778         if (result)
       
   779         {
       
   780             // Check that all values in t1 can be found from t2.
       
   781             Enumeration e = t1.keys();
       
   782             while (result && e.hasMoreElements())
       
   783             {
       
   784                 Object key = e.nextElement();
       
   785                 Object v1 = t1.get(key);
       
   786                 Object v2 = t2.get(key);
       
   787                 if (!v1.equals(v2))
       
   788                 {
       
   789                     result = false;
       
   790                 }
       
   791             }
       
   792         }
       
   793         if (result)
       
   794         {
       
   795             // Check that all values in t2 can be found from t1.
       
   796             Enumeration e = t2.keys();
       
   797             while (result && e.hasMoreElements())
       
   798             {
       
   799                 Object key = e.nextElement();
       
   800                 Object v1 = t1.get(key);
       
   801                 Object v2 = t2.get(key);
       
   802                 if (!v2.equals(v1))
       
   803                 {
       
   804                     result = false;
       
   805                 }
       
   806             }
       
   807         }
       
   808         return result;
       
   809     }
       
   810 
       
   811     private void logEquals(String aMsg)
       
   812     {
       
   813         System.out.println("StorageHandlerTest.equals: " + aMsg);
       
   814     }
       
   815 
       
   816     private void tryStartSession()
       
   817     {
       
   818         try
       
   819         {
       
   820             iStorageHandler.startSession();
       
   821         }
       
   822         catch (Exception ex)
       
   823         {
       
   824             Log.logError("tryStartSession exception", ex);
       
   825             assertTrue("Exception at startSession: " + ex.toString(), false);
       
   826         }
       
   827     }
       
   828 
       
   829     private void tryCommitSession()
       
   830     {
       
   831         try
       
   832         {
       
   833             iStorageHandler.commitSession();
       
   834         }
       
   835         catch (Exception ex)
       
   836         {
       
   837             Log.logError("tryCommitSession exception", ex);
       
   838             assertTrue("Exception at commitSession: " + ex.toString(), false);
       
   839         }
       
   840     }
       
   841 
       
   842     private void tryCommitSessionFails()
       
   843     {
       
   844         try
       
   845         {
       
   846             iStorageHandler.commitSession();
       
   847             assertTrue("No Exception at commitSession", false);
       
   848         }
       
   849         catch (Exception ex)
       
   850         {
       
   851             assertTrue("Exception at commitSession: " + ex.toString(), true);
       
   852         }
       
   853     }
       
   854 
       
   855     private void tryRollbackSession()
       
   856     {
       
   857         try
       
   858         {
       
   859             iStorageHandler.rollbackSession();
       
   860         }
       
   861         catch (Exception ex)
       
   862         {
       
   863             Log.logError("tryRollbackSession exception", ex);
       
   864             assertTrue("Exception at rollbackSession: " + ex.toString(), false);
       
   865         }
       
   866     }
       
   867 
       
   868     private void tryRollbackSessionFails()
       
   869     {
       
   870         try
       
   871         {
       
   872             iStorageHandler.rollbackSession();
       
   873             assertTrue("No Exception at rollbackSession", false);
       
   874         }
       
   875         catch (Exception ex)
       
   876         {
       
   877             assertTrue("Exception at rollbackSession: " + ex.toString(), true);
       
   878         }
       
   879     }
       
   880 
       
   881     private void tryWriteSuite(SuiteInfo aSuite)
       
   882     {
       
   883         try
       
   884         {
       
   885             iStorageHandler.writeSuiteInfo(aSuite);
       
   886         }
       
   887         catch (Exception ex)
       
   888         {
       
   889             Log.logError("tryWriteSuite exception", ex);
       
   890             assertTrue("Exception at writeSuiteInfo: " + ex.toString(), false);
       
   891         }
       
   892     }
       
   893 
       
   894     private SuiteInfo tryReadSuite(Uid aUid)
       
   895     {
       
   896         SuiteInfo suite = null;
       
   897         try
       
   898         {
       
   899             suite = new SuiteInfo(aUid);
       
   900             assertTrue("expected suite reading to succeed",
       
   901                        iStorageHandler.readSuiteInfo(suite));
       
   902         }
       
   903         catch (Exception ex)
       
   904         {
       
   905             Log.logError("tryReadSuite exception", ex);
       
   906             assertTrue("Exception at readSuiteInfo: " + ex.toString(), false);
       
   907         }
       
   908         return suite;
       
   909     }
       
   910 
       
   911     private void tryReadNoSuite(Uid aUid)
       
   912     {
       
   913         SuiteInfo suite = null;
       
   914         try
       
   915         {
       
   916             suite = new SuiteInfo(aUid);
       
   917             assertFalse("expected suite reading to fail",
       
   918                         iStorageHandler.readSuiteInfo(suite));
       
   919         }
       
   920         catch (Exception ex)
       
   921         {
       
   922             Log.logError("tryReadNoSuite exception", ex);
       
   923             assertTrue("Exception at readSuiteInfo: " + ex.toString(), false);
       
   924         }
       
   925     }
       
   926 
       
   927     private SuiteInfo tryReadSuite(String aName, String aVendor)
       
   928     {
       
   929         SuiteInfo suite = null;
       
   930         try
       
   931         {
       
   932             suite = new SuiteInfo(aName, aVendor);
       
   933             assertTrue("expected suite reading to succeed",
       
   934                        iStorageHandler.readSuiteInfo(suite));
       
   935         }
       
   936         catch (Exception ex)
       
   937         {
       
   938             Log.logError("tryReadSuite exception", ex);
       
   939             assertTrue("Exception at readSuiteInfo: " + ex.toString(), false);
       
   940         }
       
   941         return suite;
       
   942     }
       
   943 
       
   944     private void tryReadNoSuite(String aName, String aVendor)
       
   945     {
       
   946         SuiteInfo suite = null;
       
   947         try
       
   948         {
       
   949             suite = new SuiteInfo(aName, aVendor);
       
   950             assertFalse("expected suite reading to fail",
       
   951                         iStorageHandler.readSuiteInfo(suite));
       
   952         }
       
   953         catch (Exception ex)
       
   954         {
       
   955             Log.logError("tryReadNoSuite exception", ex);
       
   956             assertTrue("Exception at readSuiteInfo: " + ex.toString(), false);
       
   957         }
       
   958     }
       
   959 
       
   960     private void tryRemoveSuite(SuiteInfo aSuiteInfo)
       
   961     {
       
   962         try
       
   963         {
       
   964             iStorageHandler.removeSuiteInfo(aSuiteInfo);
       
   965         }
       
   966         catch (Exception ex)
       
   967         {
       
   968             Log.logError("tryRemoveSuite exception", ex);
       
   969             assertTrue("Exception at removeSuiteInfo: " + ex.toString(), false);
       
   970         }
       
   971     }
       
   972 
       
   973     /**
       
   974      * Reads preinstall state from INSTALL_STATE column
       
   975      * value from TBL_PREINSTALL table.
       
   976      * If INSTALL_STATE value is not found, returns -1.
       
   977      */
       
   978     private int readPreinstallState(SuiteInfo aSuiteInfo)
       
   979     {
       
   980         int result = -1;
       
   981         // Match preinstall state using name and vendor.
       
   982         StorageEntry query = new StorageEntry();
       
   983         query.addAttribute
       
   984         (new StorageAttribute
       
   985          (StorageNames.NAME, aSuiteInfo.getName()));
       
   986         query.addAttribute
       
   987         (new StorageAttribute
       
   988          (StorageNames.VENDOR, aSuiteInfo.getVendor()));
       
   989         query.addAttribute(new StorageAttribute(StorageNames.INSTALL_STATE, ""));
       
   990         StorageEntry[] entries =
       
   991             iStorageHandler.getSession().search
       
   992             (StorageNames.PREINSTALL_TABLE, query);
       
   993         if (entries != null && entries.length > 0)
       
   994         {
       
   995             // Fill suite info.
       
   996             StorageEntry entry = entries[0];
       
   997             result = Integer.parseInt
       
   998                      (StorageHandler.getAttributeValue
       
   999                       (entry, StorageNames.INSTALL_STATE));
       
  1000         }
       
  1001         return result;
       
  1002     }
       
  1003 
       
  1004     /**
       
  1005      * Removes preinstall state for given suite from preinstall table.
       
  1006      */
       
  1007     private void removePreinstallState(StorageSession aSession, SuiteInfo aSuiteInfo)
       
  1008     {
       
  1009         if (aSuiteInfo == null)
       
  1010         {
       
  1011             return;
       
  1012         }
       
  1013         // Match preinstall state using name and vendor.
       
  1014         StorageEntry matchEntry = new StorageEntry();
       
  1015         matchEntry.addAttribute
       
  1016         (new StorageAttribute
       
  1017          (StorageNames.NAME, aSuiteInfo.getName()));
       
  1018         matchEntry.addAttribute
       
  1019         (new StorageAttribute
       
  1020          (StorageNames.VENDOR, aSuiteInfo.getVendor()));
       
  1021         aSession.remove(StorageNames.PREINSTALL_TABLE, matchEntry);
       
  1022     }
       
  1023 
       
  1024     /**
       
  1025      * Removes suite data from MIDP_PACKAGE_TABLE table.
       
  1026      */
       
  1027     private void removeMidpPackageData(StorageSession aSession, SuiteInfo aSuiteInfo)
       
  1028     {
       
  1029         Uid uid = aSuiteInfo.getUid();
       
  1030         StorageEntry matchEntry = new StorageEntry();
       
  1031         matchEntry.addAttribute
       
  1032         (new StorageAttribute(StorageNames.ID, uid.getStringValue()));
       
  1033         StorageEntry[] entries =
       
  1034             aSession.search(StorageNames.MIDP_PACKAGE_TABLE, matchEntry);
       
  1035         if (entries != null && entries.length > 0)
       
  1036         {
       
  1037             // Entry exists, remove it.
       
  1038             aSession.remove(StorageNames.MIDP_PACKAGE_TABLE, uid);
       
  1039         }
       
  1040     }
       
  1041 
       
  1042     private void cleanSuiteFromStorage(SuiteInfo aSuiteInfo)
       
  1043     {
       
  1044         StorageHandler storageHandler = new StorageHandler();
       
  1045         try
       
  1046         {
       
  1047             storageHandler.startSession();
       
  1048         }
       
  1049         catch (Exception ex)
       
  1050         {
       
  1051             ex.printStackTrace();
       
  1052             assertTrue("Exception at cleanStorage startSession: " + ex.toString(), false);
       
  1053             return;
       
  1054         }
       
  1055 
       
  1056         try
       
  1057         {
       
  1058             storageHandler.removeSuiteInfo(aSuiteInfo);
       
  1059         }
       
  1060         catch (Exception ex)
       
  1061         {
       
  1062             ex.printStackTrace();
       
  1063             assertTrue("Exception at cleanStorage removeSuiteInfo: " + ex.toString(), false);
       
  1064             return;
       
  1065         }
       
  1066 
       
  1067         try
       
  1068         {
       
  1069             removeMidpPackageData(storageHandler.getSession(), aSuiteInfo);
       
  1070         }
       
  1071         catch (Exception ex)
       
  1072         {
       
  1073             ex.printStackTrace();
       
  1074             assertTrue("Exception at cleanStorage removeMidpPackageData: " + ex.toString(), false);
       
  1075             return;
       
  1076         }
       
  1077 
       
  1078         try
       
  1079         {
       
  1080             removePreinstallState(storageHandler.getSession(), aSuiteInfo);
       
  1081         }
       
  1082         catch (Exception ex)
       
  1083         {
       
  1084             ex.printStackTrace();
       
  1085             assertTrue("Exception at cleanStorage removePreinstallState: " + ex.toString(), false);
       
  1086             return;
       
  1087         }
       
  1088 
       
  1089         try
       
  1090         {
       
  1091             storageHandler.commitSession();
       
  1092         }
       
  1093         catch (Exception ex)
       
  1094         {
       
  1095             ex.printStackTrace();
       
  1096             assertTrue("Exception at cleanStorage commitSession: " + ex.toString(), false);
       
  1097             return;
       
  1098         }
       
  1099     }
       
  1100 }