javaextensions/midprms_db/tsrc/javasrc/com/nokia/mj/test/rms/TestRecordEnumeration.java
changeset 21 2a9601315dfc
child 67 63b81d807542
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 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.test.rms;
       
    20 
       
    21 import j2meunit.framework.Test;
       
    22 import j2meunit.framework.TestCase;
       
    23 import j2meunit.framework.TestMethod;
       
    24 import j2meunit.framework.TestSuite;
       
    25 
       
    26 import javax.microedition.rms.*;
       
    27 
       
    28 /* Test cases for verifying that functionality is according to MIDP2.1 spec.
       
    29  * These tests will verify that argument validation is done correctly,
       
    30  * correct exceptions are thrown and behavior follows the spec
       
    31  */
       
    32 public class TestRecordEnumeration extends TestCase
       
    33 {
       
    34 
       
    35     public TestRecordEnumeration() {}
       
    36 
       
    37     public TestRecordEnumeration(String sTestName, TestMethod rTestMethod)
       
    38     {
       
    39         super(sTestName, rTestMethod);
       
    40     }
       
    41 
       
    42     public Test suite()
       
    43     {
       
    44         TestSuite aSuite = new TestSuite();
       
    45 
       
    46         aSuite.addTest(new TestRecordEnumeration("testDestroy", new TestMethod()
       
    47         {
       
    48             public void run(TestCase tc)
       
    49             {
       
    50                 ((TestRecordEnumeration) tc).testDestroy();
       
    51             }
       
    52         }));
       
    53 
       
    54         aSuite.addTest(new TestRecordEnumeration("testHasNextElement", new TestMethod()
       
    55         {
       
    56             public void run(TestCase tc)
       
    57             {
       
    58                 ((TestRecordEnumeration) tc).testHasNextElement();
       
    59             }
       
    60         }));
       
    61 
       
    62         aSuite.addTest(new TestRecordEnumeration("testHasPreviousElement", new TestMethod()
       
    63         {
       
    64             public void run(TestCase tc)
       
    65             {
       
    66                 ((TestRecordEnumeration) tc).testHasPreviousElement();
       
    67             }
       
    68         }));
       
    69 
       
    70         aSuite.addTest(new TestRecordEnumeration("testIsKeptUpdated", new TestMethod()
       
    71         {
       
    72             public void run(TestCase tc)
       
    73             {
       
    74                 ((TestRecordEnumeration) tc).testIsKeptUpdated();
       
    75             }
       
    76         }));
       
    77 
       
    78         aSuite.addTest(new TestRecordEnumeration("testKeepUpdated", new TestMethod()
       
    79         {
       
    80             public void run(TestCase tc)
       
    81             {
       
    82                 ((TestRecordEnumeration) tc).testKeepUpdated();
       
    83             }
       
    84         }));
       
    85 
       
    86         aSuite.addTest(new TestRecordEnumeration("testNextRecord", new TestMethod()
       
    87         {
       
    88             public void run(TestCase tc)
       
    89             {
       
    90                 ((TestRecordEnumeration) tc).testNextRecord();
       
    91             }
       
    92         }));
       
    93 
       
    94         aSuite.addTest(new TestRecordEnumeration("testNextRecordId", new TestMethod()
       
    95         {
       
    96             public void run(TestCase tc)
       
    97             {
       
    98                 ((TestRecordEnumeration) tc).testNextRecordId();
       
    99             }
       
   100         }));
       
   101 
       
   102         aSuite.addTest(new TestRecordEnumeration("testNumRecords", new TestMethod()
       
   103         {
       
   104             public void run(TestCase tc)
       
   105             {
       
   106                 ((TestRecordEnumeration) tc).testNumRecords();
       
   107             }
       
   108         }));
       
   109 
       
   110         aSuite.addTest(new TestRecordEnumeration("testPreviousRecord", new TestMethod()
       
   111         {
       
   112             public void run(TestCase tc)
       
   113             {
       
   114                 ((TestRecordEnumeration) tc).testPreviousRecord();
       
   115             }
       
   116         }));
       
   117 
       
   118         aSuite.addTest(new TestRecordEnumeration("testPreviousRecordId", new TestMethod()
       
   119         {
       
   120             public void run(TestCase tc)
       
   121             {
       
   122                 ((TestRecordEnumeration) tc).testPreviousRecordId();
       
   123             }
       
   124         }));
       
   125 
       
   126         aSuite.addTest(new TestRecordEnumeration("testRebuild", new TestMethod()
       
   127         {
       
   128             public void run(TestCase tc)
       
   129             {
       
   130                 ((TestRecordEnumeration) tc).testRebuild();
       
   131             }
       
   132         }));
       
   133 
       
   134         aSuite.addTest(new TestRecordEnumeration("testReset", new TestMethod()
       
   135         {
       
   136             public void run(TestCase tc)
       
   137             {
       
   138                 ((TestRecordEnumeration) tc).testReset();
       
   139             }
       
   140         }));
       
   141 
       
   142         return aSuite;
       
   143     }
       
   144 
       
   145     public void assertEquals(byte[] a, byte[] b)
       
   146     {
       
   147         assertEquals(a.length, b.length);
       
   148         for (int i = 0; i < a.length; i++)
       
   149         {
       
   150             assertEquals(a[i], b[i]);
       
   151         }
       
   152     }
       
   153 
       
   154     String[] iData = { "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf",
       
   155                        "Hotel", "India", "Juliet", "Kilo", "Lima", "Mike", "November",
       
   156                        "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform",
       
   157                        "Victor", "Whiskey", "Xray", "Yankee", "Zulu"
       
   158                      };
       
   159 
       
   160     private void populateRecordStore(RecordStore aStore) throws RecordStoreException
       
   161     {
       
   162 
       
   163 
       
   164         for (int i=0; i<iData.length; i++)
       
   165         {
       
   166             aStore.addRecord(iData[i].getBytes(), 0, iData[i].getBytes().length);
       
   167         }
       
   168     }
       
   169 
       
   170     class DefaultFilter implements RecordFilter
       
   171     {
       
   172         public boolean matches(byte[] candidate)
       
   173         {
       
   174             if (candidate[0] > 25)
       
   175                 return true;
       
   176             else
       
   177                 return false;
       
   178         }
       
   179     }
       
   180 
       
   181     class DefaultComparator implements RecordComparator
       
   182     {
       
   183         public int compare(byte[] rec1, byte[] rec2)
       
   184         {
       
   185             if (rec1[0] < rec2[0])
       
   186                 return RecordComparator.PRECEDES;
       
   187             else if (rec1[0] > rec2[0])
       
   188                 return RecordComparator.FOLLOWS;
       
   189             else
       
   190                 return RecordComparator.EQUIVALENT;
       
   191         }
       
   192     }
       
   193 
       
   194     public void testDestroy()
       
   195     {
       
   196         System.out.println("TestRecordEnumeration.testDestroy()");
       
   197         RecordStore store = null;
       
   198         String rec_store_name = "destroy";
       
   199 
       
   200         try
       
   201         {
       
   202             // 0: Init
       
   203             System.out.println("0: Init");
       
   204             try
       
   205             {
       
   206                 RecordStore.deleteRecordStore(rec_store_name);
       
   207             }
       
   208             catch (Exception e) {}
       
   209             store = RecordStore.openRecordStore(rec_store_name, true);
       
   210             populateRecordStore(store);
       
   211 
       
   212             // 1: destroy
       
   213             System.out.println("1: destroy");
       
   214             RecordEnumeration enumeration = store.enumerateRecords(null, null, true);
       
   215             enumeration.destroy();
       
   216 
       
   217             // 2: calling methods after destroy
       
   218             System.out.println("2: calling methods after destroy");
       
   219             try
       
   220             {
       
   221                 enumeration.destroy();
       
   222                 fail("IllegalStateException not thrown - destroy");
       
   223             }
       
   224             catch (IllegalStateException re) {}
       
   225             try
       
   226             {
       
   227                 enumeration.hasNextElement();
       
   228                 fail("IllegalStateException not thrown - hasNextElement");
       
   229             }
       
   230             catch (IllegalStateException re) {}
       
   231             try
       
   232             {
       
   233                 enumeration.hasPreviousElement();
       
   234                 fail("IllegalStateException not thrown - hasPreviousElement");
       
   235             }
       
   236             catch (IllegalStateException re) {}
       
   237             try
       
   238             {
       
   239                 enumeration.isKeptUpdated();
       
   240                 fail("IllegalStateException not thrown - isKeptUpdated");
       
   241             }
       
   242             catch (IllegalStateException re) {}
       
   243             try
       
   244             {
       
   245                 enumeration.keepUpdated(true);
       
   246                 fail("IllegalStateException not thrown - keepUpdated");
       
   247             }
       
   248             catch (IllegalStateException re) {}
       
   249             try
       
   250             {
       
   251                 enumeration.nextRecord();
       
   252                 fail("IllegalStateException not thrown - nextRecord");
       
   253             }
       
   254             catch (IllegalStateException re) {}
       
   255             try
       
   256             {
       
   257                 enumeration.nextRecordId();
       
   258                 fail("IllegalStateException not thrown - nextRecordId");
       
   259             }
       
   260             catch (IllegalStateException re) {}
       
   261             try
       
   262             {
       
   263                 enumeration.numRecords();
       
   264                 fail("IllegalStateException not thrown - numRecords");
       
   265             }
       
   266             catch (IllegalStateException re) {}
       
   267             try
       
   268             {
       
   269                 enumeration.previousRecord();
       
   270                 fail("IllegalStateException not thrown - previousRecord");
       
   271             }
       
   272             catch (IllegalStateException re) {}
       
   273             try
       
   274             {
       
   275                 enumeration.rebuild();
       
   276                 fail("IllegalStateException not thrown - rebuild");
       
   277             }
       
   278             catch (IllegalStateException re) {}
       
   279             try
       
   280             {
       
   281                 enumeration.reset();
       
   282                 fail("IllegalStateException not thrown - reset");
       
   283             }
       
   284             catch (IllegalStateException re) {}
       
   285 
       
   286         }
       
   287         catch (Exception e)
       
   288         {
       
   289             e.printStackTrace();
       
   290             fail("Unexpected exception " + e);
       
   291         }
       
   292         finally
       
   293         {
       
   294             try
       
   295             {
       
   296                 store.closeRecordStore();
       
   297             }
       
   298             catch (Exception e) {}
       
   299             try
       
   300             {
       
   301                 RecordStore.deleteRecordStore(rec_store_name);
       
   302             }
       
   303             catch (Exception e) {}
       
   304         }
       
   305     }
       
   306 
       
   307     public void testHasNextElement()
       
   308     {
       
   309         System.out.println("TestRecordEnumeration.testHasNextElement()");
       
   310         RecordStore store = null;
       
   311         String rec_store_name = "hasNextElement";
       
   312 
       
   313         try
       
   314         {
       
   315             // 0: Init
       
   316             System.out.println("0: Init");
       
   317             try
       
   318             {
       
   319                 RecordStore.deleteRecordStore(rec_store_name);
       
   320             }
       
   321             catch (Exception e) {}
       
   322             store = RecordStore.openRecordStore(rec_store_name, true);
       
   323 
       
   324             // 1: empty store
       
   325             System.out.println("1: empty store");
       
   326             RecordEnumeration enumeration = store.enumerateRecords(null, null, true);
       
   327             assertEquals(enumeration.numRecords(), store.getNumRecords());
       
   328             assertTrue(enumeration.hasNextElement() == false);
       
   329             enumeration.destroy();
       
   330 
       
   331             // 2: traverse all records in the record store
       
   332             System.out.println("2: traverse all records in the record store");
       
   333             populateRecordStore(store);
       
   334             enumeration = store.enumerateRecords(null, null, true);
       
   335             assertEquals(enumeration.numRecords(), store.getNumRecords());
       
   336             int count = 0;
       
   337             while (enumeration.hasNextElement())
       
   338             {
       
   339                 enumeration.nextRecordId();
       
   340                 count++;
       
   341             }
       
   342             assertEquals(enumeration.numRecords(), count);
       
   343             enumeration.destroy();
       
   344 
       
   345         }
       
   346         catch (Exception e)
       
   347         {
       
   348             e.printStackTrace();
       
   349             fail("Unexpected exception " + e);
       
   350         }
       
   351         finally
       
   352         {
       
   353             try
       
   354             {
       
   355                 store.closeRecordStore();
       
   356             }
       
   357             catch (Exception e) {}
       
   358             try
       
   359             {
       
   360                 RecordStore.deleteRecordStore(rec_store_name);
       
   361             }
       
   362             catch (Exception e) {}
       
   363         }
       
   364     }
       
   365 
       
   366     public void testHasPreviousElement()
       
   367     {
       
   368         System.out.println("TestRecordEnumeration.testHasPreviousElement()");
       
   369         RecordStore store = null;
       
   370         String rec_store_name = "hasPreviousElement";
       
   371 
       
   372         try
       
   373         {
       
   374             // 0: Init
       
   375             System.out.println("0: Init");
       
   376             try
       
   377             {
       
   378                 RecordStore.deleteRecordStore(rec_store_name);
       
   379             }
       
   380             catch (Exception e) {}
       
   381             store = RecordStore.openRecordStore(rec_store_name, true);
       
   382 
       
   383             // 1: empty store
       
   384             System.out.println("1: empty store");
       
   385             RecordEnumeration enumeration = store.enumerateRecords(null, null, true);
       
   386             assertEquals(enumeration.numRecords(), store.getNumRecords());
       
   387             assertTrue(enumeration.hasPreviousElement() == false);
       
   388             enumeration.destroy();
       
   389 
       
   390             // 2: traverse all records in the record store
       
   391             System.out.println("2: traverse all records in the record store");
       
   392             populateRecordStore(store);
       
   393             enumeration = store.enumerateRecords(null, null, true);
       
   394             assertEquals(enumeration.numRecords(), store.getNumRecords());
       
   395             try
       
   396             {
       
   397                 while (true)
       
   398                 {
       
   399                     enumeration.nextRecordId();
       
   400                 }
       
   401             }
       
   402             catch (InvalidRecordIDException e) {}
       
   403             int count = 0;
       
   404             while (enumeration.hasPreviousElement())
       
   405             {
       
   406                 enumeration.previousRecordId();
       
   407                 count++;
       
   408             }
       
   409             assertEquals(enumeration.numRecords(), count);
       
   410             enumeration.destroy();
       
   411 
       
   412         }
       
   413         catch (Exception e)
       
   414         {
       
   415             e.printStackTrace();
       
   416             fail("Unexpected exception " + e);
       
   417         }
       
   418         finally
       
   419         {
       
   420             try
       
   421             {
       
   422                 store.closeRecordStore();
       
   423             }
       
   424             catch (Exception e) {}
       
   425             try
       
   426             {
       
   427                 RecordStore.deleteRecordStore(rec_store_name);
       
   428             }
       
   429             catch (Exception e) {}
       
   430         }
       
   431     }
       
   432 
       
   433     public void testIsKeptUpdated()
       
   434     {
       
   435         System.out.println("TestRecordEnumeration.testIsKeptUpdated()");
       
   436         RecordStore store = null;
       
   437         String rec_store_name = "isKeptUpdated";
       
   438 
       
   439         try
       
   440         {
       
   441             // 0: Init
       
   442             System.out.println("0: Init");
       
   443             try
       
   444             {
       
   445                 RecordStore.deleteRecordStore(rec_store_name);
       
   446             }
       
   447             catch (Exception e) {}
       
   448             store = RecordStore.openRecordStore(rec_store_name, true);
       
   449             populateRecordStore(store);
       
   450 
       
   451             // 1: isKeptUpdated after open
       
   452             System.out.println("1: isKeptUpdated after open");
       
   453             RecordEnumeration enumeration = store.enumerateRecords(null, null, true);
       
   454             assertTrue(enumeration.isKeptUpdated() == true);
       
   455             enumeration = store.enumerateRecords(null, null, false);
       
   456             assertTrue(enumeration.isKeptUpdated() == false);
       
   457             enumeration.destroy();
       
   458 
       
   459             // 2: isKeptUpdated after keepUpdated
       
   460             System.out.println("2: isKeptUpdated after keepUpdated");
       
   461             enumeration = store.enumerateRecords(null, null, true);
       
   462             enumeration.keepUpdated(true);
       
   463             assertTrue(enumeration.isKeptUpdated() == true);
       
   464             enumeration.keepUpdated(false);
       
   465             assertTrue(enumeration.isKeptUpdated() == false);
       
   466             enumeration.destroy();
       
   467 
       
   468         }
       
   469         catch (Exception e)
       
   470         {
       
   471             e.printStackTrace();
       
   472             fail("Unexpected exception " + e);
       
   473         }
       
   474         finally
       
   475         {
       
   476             try
       
   477             {
       
   478                 store.closeRecordStore();
       
   479             }
       
   480             catch (Exception e) {}
       
   481             try
       
   482             {
       
   483                 RecordStore.deleteRecordStore(rec_store_name);
       
   484             }
       
   485             catch (Exception e) {}
       
   486         }
       
   487     }
       
   488 
       
   489     public void testKeepUpdated()
       
   490     {
       
   491         System.out.println("TestRecordEnumeration.testKeepUpdated()");
       
   492         RecordStore store = null;
       
   493         String rec_store_name = "keepUpdated";
       
   494 
       
   495         try
       
   496         {
       
   497             // 0: Init
       
   498             System.out.println("0: Init");
       
   499             try
       
   500             {
       
   501                 RecordStore.deleteRecordStore(rec_store_name);
       
   502             }
       
   503             catch (Exception e) {}
       
   504             store = RecordStore.openRecordStore(rec_store_name, true);
       
   505             populateRecordStore(store);
       
   506 
       
   507             // 1: keepUpdated false (open true)
       
   508             System.out.println("1: keepUpdated false  (open true)");
       
   509             RecordEnumeration enumeration = store.enumerateRecords(null, null, true);
       
   510             assertTrue(enumeration.isKeptUpdated() == true);
       
   511             store.addRecord(null, 0, 0);
       
   512             assertEquals(enumeration.numRecords(), store.getNumRecords());
       
   513             enumeration.keepUpdated(false);
       
   514             store.addRecord(null, 0, 0);
       
   515             assertTrue(enumeration.isKeptUpdated() == false);
       
   516             assertEquals(enumeration.numRecords(), store.getNumRecords()-1);
       
   517             enumeration.destroy();
       
   518 
       
   519             // 2: keepUpdated false (open false)
       
   520             System.out.println("2: keepUpdated false  (open false)");
       
   521             enumeration = store.enumerateRecords(null, null, false);
       
   522             assertTrue(enumeration.isKeptUpdated() == false);
       
   523             store.addRecord(null, 0, 0);
       
   524             assertEquals(enumeration.numRecords(), store.getNumRecords()-1);
       
   525             enumeration.keepUpdated(false);
       
   526             assertTrue(enumeration.isKeptUpdated() == false);
       
   527             store.addRecord(null, 0, 0);
       
   528             assertEquals(enumeration.numRecords(), store.getNumRecords()-2);
       
   529             enumeration.destroy();
       
   530 
       
   531             // 3: keepUpdated true (open true)
       
   532             System.out.println("3: keepUpdated true  (open true)");
       
   533             enumeration = store.enumerateRecords(null, null, true);
       
   534             assertTrue(enumeration.isKeptUpdated() == true);
       
   535             store.addRecord(null, 0, 0);
       
   536             assertEquals(enumeration.numRecords(), store.getNumRecords());
       
   537             enumeration.keepUpdated(true);
       
   538             assertTrue(enumeration.isKeptUpdated() == true);
       
   539             store.addRecord(null, 0, 0);
       
   540             assertEquals(enumeration.numRecords(), store.getNumRecords());
       
   541             enumeration.destroy();
       
   542 
       
   543             // 4: keepUpdated true (open false)
       
   544             System.out.println("4: keepUpdated true  (open false)");
       
   545             enumeration = store.enumerateRecords(null, null, false);
       
   546             assertTrue(enumeration.isKeptUpdated() == false);
       
   547             store.addRecord(null, 0, 0);
       
   548             assertEquals(enumeration.numRecords(), store.getNumRecords()-1);
       
   549             enumeration.keepUpdated(true);
       
   550             assertTrue(enumeration.isKeptUpdated() == true);
       
   551             store.addRecord(null, 0, 0);
       
   552             assertEquals(enumeration.numRecords(), store.getNumRecords());
       
   553             enumeration.destroy();
       
   554 
       
   555         }
       
   556         catch (Exception e)
       
   557         {
       
   558             e.printStackTrace();
       
   559             fail("Unexpected exception " + e);
       
   560         }
       
   561         finally
       
   562         {
       
   563             try
       
   564             {
       
   565                 store.closeRecordStore();
       
   566             }
       
   567             catch (Exception e) {}
       
   568             try
       
   569             {
       
   570                 RecordStore.deleteRecordStore(rec_store_name);
       
   571             }
       
   572             catch (Exception e) {}
       
   573         }
       
   574     }
       
   575 
       
   576     public void testNextRecord()
       
   577     {
       
   578         System.out.println("TestRecordEnumeration.testNextRecord()");
       
   579         RecordStore store = null;
       
   580         String rec_store_name = "nextRecord";
       
   581 
       
   582         try
       
   583         {
       
   584             // 0: Init
       
   585             System.out.println("0: Init");
       
   586             try
       
   587             {
       
   588                 RecordStore.deleteRecordStore(rec_store_name);
       
   589             }
       
   590             catch (Exception e) {}
       
   591             store = RecordStore.openRecordStore(rec_store_name, true);
       
   592             populateRecordStore(store);
       
   593 
       
   594             // 1: nextRecord
       
   595             System.out.println("1: nextRecord");
       
   596             RecordEnumeration enumeration = store.enumerateRecords(null, null, true);
       
   597             int index = 0;
       
   598             try
       
   599             {
       
   600                 while (true)
       
   601                 {
       
   602                     byte[] data = enumeration.nextRecord();
       
   603                     // go back to get the record
       
   604                     if (enumeration.hasPreviousElement())
       
   605                     {
       
   606                         enumeration.previousRecordId();
       
   607                     }
       
   608                     else
       
   609                     {
       
   610                         enumeration.reset();
       
   611                     }
       
   612                     int id  = enumeration.nextRecordId();
       
   613                     byte[] orig = store.getRecord(id);
       
   614                     assertEquals(orig, data);
       
   615                     index++;
       
   616                 }
       
   617             }
       
   618             catch (InvalidRecordIDException re) {}
       
   619             assertEquals(enumeration.numRecords(), store.getNumRecords());
       
   620             assertEquals(index, store.getNumRecords());
       
   621 
       
   622             // 2: InvalidRecordIDException
       
   623             System.out.println("2: InvalidRecordIDException");
       
   624             try
       
   625             {
       
   626                 enumeration.nextRecord();
       
   627                 fail("InvalidRecordIDException not thrown");
       
   628             }
       
   629             catch (InvalidRecordIDException re)
       
   630             {
       
   631                 try
       
   632                 {
       
   633                     enumeration.nextRecord();
       
   634                     fail("InvalidRecordIDException not thrown - (2nd)");
       
   635                 }
       
   636                 catch (InvalidRecordIDException ie) {}
       
   637             }
       
   638             enumeration.reset();
       
   639             enumeration.nextRecord();
       
   640 
       
   641             // 3: RecordStoreNotOpenException
       
   642             System.out.println("3: RecordStoreNotOpenException");
       
   643             store.closeRecordStore();
       
   644             try
       
   645             {
       
   646                 enumeration.nextRecord();
       
   647                 fail("RecordStoreNotOpenException not thrown");
       
   648             }
       
   649             catch (RecordStoreNotOpenException re) {}
       
   650 
       
   651             enumeration.destroy();
       
   652             store = RecordStore.openRecordStore(rec_store_name, true);
       
   653         }
       
   654         catch (Exception e)
       
   655         {
       
   656             e.printStackTrace();
       
   657             fail("Unexpected exception " + e);
       
   658         }
       
   659         finally
       
   660         {
       
   661             try
       
   662             {
       
   663                 store.closeRecordStore();
       
   664             }
       
   665             catch (Exception e) {}
       
   666             try
       
   667             {
       
   668                 RecordStore.deleteRecordStore(rec_store_name);
       
   669             }
       
   670             catch (Exception e) {}
       
   671         }
       
   672     }
       
   673 
       
   674     public void testNextRecordId()
       
   675     {
       
   676         System.out.println("TestRecordEnumeration.testNextRecordId()");
       
   677         RecordStore store = null;
       
   678         String rec_store_name = "nextRecordId";
       
   679 
       
   680         try
       
   681         {
       
   682             // 0: Init
       
   683             System.out.println("0: Init");
       
   684             try
       
   685             {
       
   686                 RecordStore.deleteRecordStore(rec_store_name);
       
   687             }
       
   688             catch (Exception e) {}
       
   689             store = RecordStore.openRecordStore(rec_store_name, true);
       
   690             populateRecordStore(store);
       
   691 
       
   692             // 1: nextRecordId
       
   693             System.out.println("1: nextRecordId");
       
   694             RecordEnumeration enumeration = store.enumerateRecords(null, null, true);
       
   695             int index = 0;
       
   696             try
       
   697             {
       
   698                 while (true)
       
   699                 {
       
   700                     int id  = enumeration.nextRecordId();
       
   701                     // go back to get the record
       
   702                     if (enumeration.hasPreviousElement())
       
   703                     {
       
   704                         enumeration.previousRecordId();
       
   705                     }
       
   706                     else
       
   707                     {
       
   708                         enumeration.reset();
       
   709                     }
       
   710                     byte[] orig = store.getRecord(id);
       
   711                     byte[] data = enumeration.nextRecord();
       
   712                     assertEquals(orig, data);
       
   713                     index++;
       
   714                 }
       
   715             }
       
   716             catch (InvalidRecordIDException re) {}
       
   717             assertEquals(enumeration.numRecords(), store.getNumRecords());
       
   718             assertEquals(index, store.getNumRecords());
       
   719 
       
   720             // 2: InvalidRecordIDException
       
   721             System.out.println("2: InvalidRecordIDException");
       
   722             try
       
   723             {
       
   724                 enumeration.nextRecordId();
       
   725                 fail("InvalidRecordIDException not thrown");
       
   726             }
       
   727             catch (InvalidRecordIDException re)
       
   728             {
       
   729                 try
       
   730                 {
       
   731                     enumeration.nextRecordId();
       
   732                     fail("InvalidRecordIDException not thrown - (2nd)");
       
   733                 }
       
   734                 catch (InvalidRecordIDException ie) {}
       
   735             }
       
   736             enumeration.reset();
       
   737             enumeration.nextRecordId();
       
   738             enumeration.destroy();
       
   739         }
       
   740         catch (Exception e)
       
   741         {
       
   742             e.printStackTrace();
       
   743             fail("Unexpected exception " + e);
       
   744         }
       
   745         finally
       
   746         {
       
   747             try
       
   748             {
       
   749                 store.closeRecordStore();
       
   750             }
       
   751             catch (Exception e) {}
       
   752             try
       
   753             {
       
   754                 RecordStore.deleteRecordStore(rec_store_name);
       
   755             }
       
   756             catch (Exception e) {}
       
   757         }
       
   758     }
       
   759 
       
   760     public void testNumRecords()
       
   761     {
       
   762         System.out.println("TestRecordEnumeration.testNumRecords()");
       
   763         RecordStore store = null;
       
   764         String rec_store_name = "numRecords";
       
   765 
       
   766         try
       
   767         {
       
   768             // 0: Init
       
   769             System.out.println("0: Init");
       
   770             try
       
   771             {
       
   772                 RecordStore.deleteRecordStore(rec_store_name);
       
   773             }
       
   774             catch (Exception e) {}
       
   775             store = RecordStore.openRecordStore(rec_store_name, true);
       
   776             populateRecordStore(store);
       
   777 
       
   778             // 1: testNumRecords
       
   779             System.out.println("1: testNumRecords");
       
   780             RecordEnumeration enumeration = store.enumerateRecords(null, null, true);
       
   781             assertEquals(enumeration.numRecords(), store.getNumRecords());
       
   782             try
       
   783             {
       
   784                 while (true)
       
   785                 {
       
   786                     int id  = enumeration.nextRecordId();
       
   787                     store.deleteRecord(id);
       
   788                     assertEquals(enumeration.numRecords(), store.getNumRecords());
       
   789                 }
       
   790             }
       
   791             catch (InvalidRecordIDException re) {}
       
   792             enumeration.destroy();
       
   793         }
       
   794         catch (Exception e)
       
   795         {
       
   796             e.printStackTrace();
       
   797             fail("Unexpected exception " + e);
       
   798         }
       
   799         finally
       
   800         {
       
   801             try
       
   802             {
       
   803                 store.closeRecordStore();
       
   804             }
       
   805             catch (Exception e) {}
       
   806             try
       
   807             {
       
   808                 RecordStore.deleteRecordStore(rec_store_name);
       
   809             }
       
   810             catch (Exception e) {}
       
   811         }
       
   812     }
       
   813 
       
   814     public void testPreviousRecord()
       
   815     {
       
   816         System.out.println("TestRecordEnumeration.testPreviousRecord()");
       
   817         RecordStore store = null;
       
   818         String rec_store_name = "previousRecord";
       
   819 
       
   820         try
       
   821         {
       
   822             // 0: Init
       
   823             System.out.println("0: Init");
       
   824             try
       
   825             {
       
   826                 RecordStore.deleteRecordStore(rec_store_name);
       
   827             }
       
   828             catch (Exception e) {}
       
   829             store = RecordStore.openRecordStore(rec_store_name, true);
       
   830             populateRecordStore(store);
       
   831 
       
   832             // 1: previousRecord
       
   833             System.out.println("1: previousRecord");
       
   834             RecordEnumeration enumeration = store.enumerateRecords(null, null, true);
       
   835             int index = 0;
       
   836             try
       
   837             {
       
   838                 while (true)
       
   839                 {
       
   840                     enumeration.nextRecord();
       
   841                     // go back to get the record
       
   842                     if (enumeration.hasPreviousElement())
       
   843                     {
       
   844                         byte[] data = enumeration.previousRecord();
       
   845                         enumeration.nextRecord();
       
   846                         int id  = enumeration.previousRecordId();
       
   847                         byte[] orig = store.getRecord(id);
       
   848                         assertEquals(orig, data);
       
   849                         enumeration.nextRecord();
       
   850                     }
       
   851                     index++;
       
   852                 }
       
   853             }
       
   854             catch (InvalidRecordIDException re) {}
       
   855             assertEquals(enumeration.numRecords(), store.getNumRecords());
       
   856             assertEquals(index, store.getNumRecords());
       
   857 
       
   858             // 2: InvalidRecordIDException
       
   859             System.out.println("2: InvalidRecordIDException");
       
   860             while (enumeration.hasPreviousElement())
       
   861             {
       
   862                 enumeration.previousRecord();
       
   863             }
       
   864             try
       
   865             {
       
   866                 enumeration.previousRecord();
       
   867                 fail("InvalidRecordIDException not thrown");
       
   868             }
       
   869             catch (InvalidRecordIDException re) {}
       
   870 
       
   871             // 3: InvalidRecordIDException
       
   872             System.out.println("3: InvalidRecordIDException - subsequent calls will throw exception");
       
   873             enumeration.destroy();
       
   874             enumeration = store.enumerateRecords(null, null, true);
       
   875             try
       
   876             {
       
   877                 enumeration.nextRecord();
       
   878                 enumeration.previousRecord();
       
   879                 fail("InvalidRecordIDException not thrown");
       
   880             }
       
   881             catch (InvalidRecordIDException re)
       
   882             {
       
   883                 try
       
   884                 {
       
   885                     enumeration.previousRecord();
       
   886                     fail("InvalidRecordIDException not thrown - (2nd)");
       
   887                 }
       
   888                 catch (InvalidRecordIDException ie) {}
       
   889             }
       
   890 
       
   891             // 4: RecordStoreNotOpenException
       
   892             System.out.println("4: RecordStoreNotOpenException");
       
   893             enumeration.nextRecord();
       
   894             store.closeRecordStore();
       
   895             try
       
   896             {
       
   897                 enumeration.previousRecord();
       
   898                 fail("RecordStoreNotOpenException not thrown");
       
   899             }
       
   900             catch (RecordStoreNotOpenException re) {}
       
   901 
       
   902             enumeration.destroy();
       
   903             store = RecordStore.openRecordStore(rec_store_name, true);
       
   904         }
       
   905         catch (Exception e)
       
   906         {
       
   907             e.printStackTrace();
       
   908             fail("Unexpected exception " + e);
       
   909         }
       
   910         finally
       
   911         {
       
   912             try
       
   913             {
       
   914                 store.closeRecordStore();
       
   915             }
       
   916             catch (Exception e) {}
       
   917             try
       
   918             {
       
   919                 RecordStore.deleteRecordStore(rec_store_name);
       
   920             }
       
   921             catch (Exception e) {}
       
   922         }
       
   923     }
       
   924 
       
   925     public void testPreviousRecordId()
       
   926     {
       
   927         System.out.println("TestRecordEnumeration.testPreviousRecordId()");
       
   928         RecordStore store = null;
       
   929         String rec_store_name = "previousRecordId";
       
   930 
       
   931         try
       
   932         {
       
   933             // 0: Init
       
   934             System.out.println("0: Init");
       
   935             try
       
   936             {
       
   937                 RecordStore.deleteRecordStore(rec_store_name);
       
   938             }
       
   939             catch (Exception e) {}
       
   940             store = RecordStore.openRecordStore(rec_store_name, true);
       
   941             populateRecordStore(store);
       
   942 
       
   943             // 1: previousRecordId
       
   944             System.out.println("1: previousRecordId");
       
   945             RecordEnumeration enumeration = store.enumerateRecords(null, null, true);
       
   946             int index = 0;
       
   947             try
       
   948             {
       
   949                 while (true)
       
   950                 {
       
   951                     enumeration.nextRecord();
       
   952                     // go back to get the record
       
   953                     if (enumeration.hasPreviousElement())
       
   954                     {
       
   955                         byte[] data = enumeration.previousRecord();
       
   956                         enumeration.nextRecord();
       
   957                         int id  = enumeration.previousRecordId();
       
   958                         byte[] orig = store.getRecord(id);
       
   959                         assertEquals(orig, data);
       
   960                         enumeration.nextRecord();
       
   961                     }
       
   962                     index++;
       
   963                 }
       
   964             }
       
   965             catch (InvalidRecordIDException re) {}
       
   966             assertEquals(enumeration.numRecords(), store.getNumRecords());
       
   967             assertEquals(index, store.getNumRecords());
       
   968 
       
   969             // 2: InvalidRecordIDException
       
   970             System.out.println("2: InvalidRecordIDException");
       
   971             while (enumeration.hasPreviousElement())
       
   972             {
       
   973                 enumeration.previousRecordId();
       
   974             }
       
   975             try
       
   976             {
       
   977                 enumeration.previousRecordId();
       
   978                 fail("InvalidRecordIDException not thrown");
       
   979             }
       
   980             catch (InvalidRecordIDException re) {}
       
   981             enumeration.destroy();
       
   982 
       
   983             // 3: InvalidRecordIDException
       
   984             System.out.println("3: InvalidRecordIDException - subsequent calls will throw exception");
       
   985             enumeration = store.enumerateRecords(null, null, true);
       
   986             try
       
   987             {
       
   988                 enumeration.nextRecordId();
       
   989                 enumeration.previousRecordId();
       
   990                 fail("InvalidRecordIDException not thrown");
       
   991             }
       
   992             catch (InvalidRecordIDException re)
       
   993             {
       
   994                 try
       
   995                 {
       
   996                     enumeration.previousRecordId();
       
   997                     fail("InvalidRecordIDException not thrown - (2nd)");
       
   998                 }
       
   999                 catch (InvalidRecordIDException ie) {}
       
  1000             }
       
  1001             enumeration.destroy();
       
  1002 
       
  1003         }
       
  1004         catch (Exception e)
       
  1005         {
       
  1006             e.printStackTrace();
       
  1007             fail("Unexpected exception " + e);
       
  1008         }
       
  1009         finally
       
  1010         {
       
  1011             try
       
  1012             {
       
  1013                 store.closeRecordStore();
       
  1014             }
       
  1015             catch (Exception e) {}
       
  1016             try
       
  1017             {
       
  1018                 RecordStore.deleteRecordStore(rec_store_name);
       
  1019             }
       
  1020             catch (Exception e) {}
       
  1021         }
       
  1022     }
       
  1023 
       
  1024     public void testRebuild()
       
  1025     {
       
  1026         System.out.println("TestRecordEnumeration.testRebuild()");
       
  1027         RecordStore store = null;
       
  1028         String rec_store_name = "rebuild";
       
  1029 
       
  1030         try
       
  1031         {
       
  1032             // 0: Init
       
  1033             System.out.println("0: Init");
       
  1034             try
       
  1035             {
       
  1036                 RecordStore.deleteRecordStore(rec_store_name);
       
  1037             }
       
  1038             catch (Exception e) {}
       
  1039             store = RecordStore.openRecordStore(rec_store_name, true);
       
  1040 
       
  1041             // 1: rebuild (empty store)
       
  1042             System.out.println("1: rebuild (empty store)");
       
  1043             RecordEnumeration enumeration = store.enumerateRecords(null, null, false);
       
  1044             assertEquals(enumeration.numRecords(), store.getNumRecords());
       
  1045             enumeration.rebuild();
       
  1046             assertEquals(enumeration.numRecords(), store.getNumRecords());
       
  1047 
       
  1048             // 2: rebuild
       
  1049             System.out.println("2: rebuild");
       
  1050             populateRecordStore(store);
       
  1051             assertEquals(enumeration.numRecords(), 0);
       
  1052             enumeration.rebuild();
       
  1053             assertEquals(enumeration.numRecords(), store.getNumRecords());
       
  1054             RecordEnumeration enumeration2 = store.enumerateRecords(null, null, false);
       
  1055             int i = 0;
       
  1056             try
       
  1057             {
       
  1058                 while (true)
       
  1059                 {
       
  1060                     assertEquals(enumeration.nextRecord(), enumeration2.nextRecord());
       
  1061                     i++;
       
  1062                 }
       
  1063             }
       
  1064             catch (InvalidRecordIDException re) {}
       
  1065             assertEquals(store.getNumRecords(), i);
       
  1066 
       
  1067             enumeration.destroy();
       
  1068             enumeration2.destroy();
       
  1069         }
       
  1070         catch (Exception e)
       
  1071         {
       
  1072             e.printStackTrace();
       
  1073             fail("Unexpected exception " + e);
       
  1074         }
       
  1075         finally
       
  1076         {
       
  1077             try
       
  1078             {
       
  1079                 store.closeRecordStore();
       
  1080             }
       
  1081             catch (Exception e) {}
       
  1082             try
       
  1083             {
       
  1084                 RecordStore.deleteRecordStore(rec_store_name);
       
  1085             }
       
  1086             catch (Exception e) {}
       
  1087         }
       
  1088     }
       
  1089 
       
  1090     public void testReset()
       
  1091     {
       
  1092         System.out.println("TestRecordEnumeration.testReset()");
       
  1093         RecordStore store = null;
       
  1094         String rec_store_name = "reset";
       
  1095 
       
  1096         try
       
  1097         {
       
  1098             // 0: Init
       
  1099             System.out.println("0: Init");
       
  1100             try
       
  1101             {
       
  1102                 RecordStore.deleteRecordStore(rec_store_name);
       
  1103             }
       
  1104             catch (Exception e) {}
       
  1105             store = RecordStore.openRecordStore(rec_store_name, true);
       
  1106 
       
  1107             // 1: reset (empty store)
       
  1108             System.out.println("1: reset (empty store)");
       
  1109             RecordEnumeration enumeration = store.enumerateRecords(null, null, true);
       
  1110             RecordEnumeration enumeration2 = store.enumerateRecords(null, null, true);
       
  1111             assertTrue(enumeration.hasPreviousElement() == enumeration2.hasPreviousElement());
       
  1112             assertTrue(enumeration.hasNextElement() == enumeration2.hasNextElement());
       
  1113             enumeration.reset();
       
  1114             assertTrue(enumeration.hasPreviousElement() == enumeration2.hasPreviousElement());
       
  1115             assertTrue(enumeration.hasNextElement() == enumeration2.hasNextElement());
       
  1116 
       
  1117             // 2: reset
       
  1118             System.out.println("2: reset");
       
  1119             populateRecordStore(store);
       
  1120             assertTrue(enumeration.hasPreviousElement() == enumeration2.hasPreviousElement());
       
  1121             assertTrue(enumeration.hasNextElement() == enumeration2.hasNextElement());
       
  1122             enumeration.nextRecord();
       
  1123             enumeration.reset();
       
  1124             assertTrue(enumeration.hasPreviousElement() == enumeration2.hasPreviousElement());
       
  1125             assertTrue(enumeration.hasNextElement() == enumeration2.hasNextElement());
       
  1126 
       
  1127             enumeration.destroy();
       
  1128             enumeration2.destroy();
       
  1129         }
       
  1130         catch (Exception e)
       
  1131         {
       
  1132             e.printStackTrace();
       
  1133             fail("Unexpected exception " + e);
       
  1134         }
       
  1135         finally
       
  1136         {
       
  1137             try
       
  1138             {
       
  1139                 store.closeRecordStore();
       
  1140             }
       
  1141             catch (Exception e) {}
       
  1142             try
       
  1143             {
       
  1144                 RecordStore.deleteRecordStore(rec_store_name);
       
  1145             }
       
  1146             catch (Exception e) {}
       
  1147         }
       
  1148     }
       
  1149 
       
  1150 }
       
  1151