javacommons/javastorage/tsrc/java_api/javasrc/com/nokia/mj/test/storage/TestConnection.java
changeset 21 2a9601315dfc
child 80 d6dafc5d983f
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2008-2009 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.storage;
       
    20 
       
    21 import com.nokia.mj.test.storage.utils.StorageSessionTestUtils;
       
    22 import com.nokia.mj.impl.storage.*;
       
    23 
       
    24 import com.nokia.mj.impl.installer.utils.InstallerMain;
       
    25 import j2meunit.framework.Test;
       
    26 import j2meunit.framework.TestCase;
       
    27 import j2meunit.framework.TestMethod;
       
    28 import j2meunit.framework.TestSuite;
       
    29 
       
    30 /**
       
    31  * StorageSession connection test cases. See test methods for test case details.
       
    32  */
       
    33 public class TestConnection extends TestCase implements InstallerMain, StorageNames
       
    34 {
       
    35     /**
       
    36      * Directory for JavaStorage tests.
       
    37      */
       
    38     private static final String iTestRoot = "./jstest";
       
    39 
       
    40     /**
       
    41      * Directory for JavaStorage journal and temp files.
       
    42      */
       
    43     private static final String iIsRoot = iTestRoot + "/js";
       
    44 
       
    45     private StorageSession iSession = null;
       
    46     private StorageSessionTestUtils iJtu = null;
       
    47 
       
    48     public void installerMain(String[] args)
       
    49     {
       
    50         TestSuite suite = new TestSuite(this.getClass().getName());
       
    51 
       
    52         suite.addTest(new TestConnection("testOpen", new TestMethod()
       
    53         {
       
    54             public void run(TestCase tc)
       
    55             {
       
    56                 ((TestConnection)tc).testOpen();
       
    57             }
       
    58         }));
       
    59 
       
    60         suite.addTest(new TestConnection("testMultiOpen", new TestMethod()
       
    61         {
       
    62             public void run(TestCase tc)
       
    63             {
       
    64                 ((TestConnection)tc).testMultiOpen();
       
    65             }
       
    66         }));
       
    67 
       
    68         suite.addTest(new TestConnection("testClose", new TestMethod()
       
    69         {
       
    70             public void run(TestCase tc)
       
    71             {
       
    72                 ((TestConnection)tc).testClose();
       
    73             }
       
    74         }));
       
    75 
       
    76         com.nokia.mj.impl.utils.OmjTestRunner.run(suite);
       
    77     }
       
    78 
       
    79     public TestConnection()
       
    80     {
       
    81     }
       
    82 
       
    83     public TestConnection(String aTestName, TestMethod aTestMethod)
       
    84     {
       
    85         super(aTestName, aTestMethod);
       
    86     }
       
    87 
       
    88     protected void setUp()
       
    89     {
       
    90         iSession = StorageFactory.createSession();
       
    91         iJtu = new StorageSessionTestUtils();
       
    92     }
       
    93 
       
    94     protected void tearDown()
       
    95     {
       
    96         if (iSession != null)
       
    97         {
       
    98             try
       
    99             {
       
   100                 iSession.destroySession();
       
   101             }
       
   102             catch (StorageException se)
       
   103             {
       
   104                 // No can do
       
   105                 System.out.println("TearDown failed: " + se.toString());
       
   106             }
       
   107         }
       
   108     }
       
   109 
       
   110     /**
       
   111      * Test connection open
       
   112      * 1. Test open().
       
   113      * 2. Test open(JAVA_DATABASE_NAME)
       
   114      * 3. Test open to FOO_BAR.db.
       
   115      * 4. Test open with empty name. JavaStorageException is thrown.
       
   116      * 5. Test open with null name. JavaStorageException is thrown.
       
   117      * 6. Test two consecutive opens.
       
   118      * 7. Test re-open.
       
   119      */
       
   120     public void testOpen()
       
   121     {
       
   122         // 1. Test open
       
   123         try
       
   124         {
       
   125             String appUID = "TestOpen1";
       
   126             iSession.open();
       
   127             iSession.startTransaction();
       
   128 
       
   129             // Write and read to verify connection.
       
   130             assertTrue("Populate with default",
       
   131                        iJtu.populateWithDefault(iSession, appUID));
       
   132             assertTrue("Check defaults",
       
   133                        iJtu.checkDefaultParams(iSession, appUID));
       
   134 
       
   135             // Do not store changes.
       
   136             iSession.rollbackTransaction();
       
   137         }
       
   138         catch (StorageException se)
       
   139         {
       
   140             iSession.rollbackTransaction();
       
   141             assertTrue("Database open failed: " + se.getMessage(), false);
       
   142         }
       
   143 
       
   144         iSession.close();
       
   145 
       
   146         // 2. Test open (JAVA_DATABASE_NAME.)
       
   147         try
       
   148         {
       
   149             String appUID = "TestOpen1";
       
   150             iSession.open(JAVA_DATABASE_NAME);
       
   151             iSession.startTransaction();
       
   152 
       
   153             // Write and read to verify connection.
       
   154             assertTrue("Populate with default",
       
   155                        iJtu.populateWithDefault(iSession, appUID));
       
   156             assertTrue("Check defaults",
       
   157                        iJtu.checkDefaultParams(iSession, appUID));
       
   158 
       
   159             // Do not store changes.
       
   160             iSession.rollbackTransaction();
       
   161         }
       
   162         catch (StorageException se)
       
   163         {
       
   164             iSession.rollbackTransaction();
       
   165             assertTrue("Database open with name failed: " + se.getMessage(),
       
   166                        false);
       
   167         }
       
   168 
       
   169         iSession.close();
       
   170 
       
   171         // 3. Test open ("FOO_BAR.db".)
       
   172         try
       
   173         {
       
   174             // Depending on the platform this should change.
       
   175             // based on system variable.
       
   176             iSession.open("c:\\FOO_BAR.db");
       
   177         }
       
   178         catch (StorageException se)
       
   179         {
       
   180             assertTrue("Database open to foo bar failed: " + se.getMessage(),
       
   181                        false);
       
   182         }
       
   183 
       
   184         iSession.close();
       
   185 
       
   186         // 4. Test open with empty name
       
   187         try
       
   188         {
       
   189             iSession.open("");
       
   190             assertTrue("Opening with empty name did not thrown exception",
       
   191                        false);
       
   192 
       
   193         }
       
   194         catch (StorageException se)
       
   195         {
       
   196             // PASSED
       
   197         }
       
   198 
       
   199         // 5. Test open with null name
       
   200         try
       
   201         {
       
   202             String name = null;
       
   203             iSession.open(name);
       
   204             assertTrue("Opening with null name did not thrown exception",
       
   205                        false);
       
   206 
       
   207         }
       
   208         catch (StorageException se)
       
   209         {
       
   210             // PASSED
       
   211         }
       
   212 
       
   213         // 6. Test two consecutive opens.
       
   214         try
       
   215         {
       
   216             iSession.open();
       
   217             iSession.open();
       
   218             assertTrue("Consecutive open does not throw exp", false);
       
   219         }
       
   220         catch (StorageException se)
       
   221         {
       
   222             // PASSED
       
   223         }
       
   224         catch (Throwable t)
       
   225         {
       
   226             assertTrue("UnExp6: " + t.toString(), false);
       
   227         }
       
   228 
       
   229         iSession.close();
       
   230 
       
   231         // 7. Test re-open
       
   232         try
       
   233         {
       
   234             String appUID = "TestOpen2";
       
   235             iSession.open();
       
   236             iSession.startTransaction();
       
   237 
       
   238             assertTrue("Re-Open: Populate with default",
       
   239                        iJtu.populateWithDefault(iSession, appUID));
       
   240 
       
   241             // Write and read to verify connection.
       
   242             assertTrue("Re-Open: Check defaults",
       
   243                        iJtu.checkDefaultParams(iSession, appUID));
       
   244 
       
   245             assertTrue("Re-Open: Remove defaults",
       
   246                        iJtu.removeDefaultParams(iSession, appUID));
       
   247 
       
   248             // Store changes for re-open test cases.
       
   249             iSession.rollbackTransaction();
       
   250         }
       
   251         catch (StorageException se)
       
   252         {
       
   253             iSession.rollbackTransaction();
       
   254             assertTrue("Re-open: " + se.getMessage(), false);
       
   255         }
       
   256 
       
   257         // Currently this deletes instance.
       
   258         iSession.close();
       
   259     }
       
   260 
       
   261     /**
       
   262      * Test connection open.
       
   263      * 1. Test two connections to same target can be opened.
       
   264      * 2. Test two connections to separate targets can be opened.
       
   265      */
       
   266     public void testMultiOpen()
       
   267     {
       
   268         // 1. Two connections to same target.
       
   269         StorageSession session2 = null;
       
   270         try
       
   271         {
       
   272             String appUID = "TwoConnections";
       
   273             iSession.open();
       
   274 
       
   275             session2 = StorageFactory.createSession();
       
   276             session2.open();
       
   277 
       
   278             iSession.startTransaction();
       
   279             // session2.startTransaction(); // This will fail as not supported
       
   280 
       
   281             // Write and read to verify connection.
       
   282             assertTrue("Conn1: Populate with default",
       
   283                        iJtu.populateWithDefault(iSession, appUID));
       
   284             assertTrue("Conn1: Check defaults",
       
   285                        iJtu.checkDefaultParams(iSession, appUID));
       
   286 
       
   287             // Cannot have two concurrent transactions to same db without"
       
   288             // getting database busy.
       
   289             iSession.rollbackTransaction();
       
   290             session2.startTransaction();
       
   291 
       
   292             StorageEntry entry = new StorageEntry();
       
   293             entry.addAttribute(new StorageAttribute(PACKAGE_ID,
       
   294                                                     "GreatSuite"));
       
   295             entry.addAttribute(new StorageAttribute(NAME, "MyJavaApp"));
       
   296             entry.addAttribute(new StorageAttribute(MAIN_CLASS,
       
   297                                                     "com.my.java.app.GreatApp"));
       
   298             entry.addAttribute(new StorageAttribute(AUTORUN, "0",
       
   299                                                     StorageAttribute.INT_TYPE));
       
   300 
       
   301             assertTrue("Conn2: Populate",
       
   302                        iJtu.populate(session2, APPLICATION_TABLE,
       
   303                                      appUID, entry));
       
   304             assertTrue("Conn2: Check",
       
   305                        iJtu.checkParams(session2,
       
   306                                         APPLICATION_TABLE, entry, 1));
       
   307 
       
   308             session2.rollbackTransaction();
       
   309         }
       
   310         catch (StorageException se)
       
   311         {
       
   312             iSession.rollbackTransaction();
       
   313 
       
   314             if (session2 != null)
       
   315             {
       
   316                 session2.rollbackTransaction();
       
   317             }
       
   318 
       
   319             assertTrue("Test two connections: " + se.getMessage(), false);
       
   320         }
       
   321         iSession.close();
       
   322         session2.close();
       
   323 
       
   324         // 2. Two connections to different target.
       
   325         try
       
   326         {
       
   327             String appUID = "TwoConnections";
       
   328             iSession.open();
       
   329             session2.open("c:\\FOO_BAR.db");
       
   330 
       
   331             iSession.startTransaction();
       
   332             session2.startTransaction();
       
   333 
       
   334             // Write and read to verify connection.
       
   335             assertTrue("Conn1: Populate with default",
       
   336                        iJtu.populateWithDefault(iSession, appUID));
       
   337             assertTrue("Conn1: Check defaults",
       
   338                        iJtu.checkDefaultParams(iSession, appUID));
       
   339 
       
   340             StorageEntry entry = new StorageEntry();
       
   341             entry.addAttribute(new StorageAttribute(ID, ""));
       
   342             entry.addAttribute(new StorageAttribute(PACKAGE_ID,
       
   343                                                     "ROOT_PACKAGE"));
       
   344             entry.addAttribute(new StorageAttribute(NAME, "MyJavaApp"));
       
   345             entry.addAttribute(new StorageAttribute(MAIN_CLASS,
       
   346                                                     "com.my.java.app.GreatApp"));
       
   347             entry.addAttribute(new StorageAttribute(AUTORUN, "0",
       
   348                                                     StorageAttribute.INT_TYPE));
       
   349 
       
   350             // Create table to new database.
       
   351             session2.createTable(APPLICATION_TABLE, entry, null);
       
   352 
       
   353             entry.removeAttribute(ID);
       
   354 
       
   355             assertTrue("DB2Conn: Populate",
       
   356                        iJtu.populate(session2, APPLICATION_TABLE,
       
   357                                      appUID, entry));
       
   358 
       
   359             assertTrue("DB2Conn: Check",
       
   360                        iJtu.checkParams(session2,
       
   361                                         APPLICATION_TABLE, entry, 1));
       
   362 
       
   363             iSession.rollbackTransaction();
       
   364             session2.rollbackTransaction();
       
   365         }
       
   366         catch (StorageException se)
       
   367         {
       
   368             iSession.rollbackTransaction();
       
   369 
       
   370             if (session2 != null)
       
   371             {
       
   372                 session2.rollbackTransaction();
       
   373             }
       
   374 
       
   375             assertTrue("Test two diff connections: " + se.getMessage(), false);
       
   376         }
       
   377 
       
   378         iSession.close();
       
   379         session2.close();
       
   380         session2.destroySession();
       
   381     }
       
   382 
       
   383     /**
       
   384      * Test connection close.
       
   385      * 1. Test close not open connection.
       
   386      * 2. Close connection. JavaStorage cannot be read after that i.e.
       
   387      *    StorageException is thrown.
       
   388      * 3. Test two consecutive close.
       
   389      */
       
   390     public void testClose()
       
   391     {
       
   392         // 1. Close not open.
       
   393         try
       
   394         {
       
   395             iSession.close();
       
   396         }
       
   397         catch (StorageException se)
       
   398         {
       
   399             // PASSED
       
   400         }
       
   401         catch (Throwable t)
       
   402         {
       
   403             assertTrue("Close not open: " + t.toString(), false);
       
   404         }
       
   405 
       
   406         // 2. Read not open.
       
   407         try
       
   408         {
       
   409             StorageEntry[] entry = iSession.search(
       
   410                                        JAVA_DATABASE_NAME, iJtu.getDefaultEntry());
       
   411 
       
   412             assertTrue("Read closed connection did not thrown exp.", false);
       
   413         }
       
   414         catch (StorageException se)
       
   415         {
       
   416             // PASSED
       
   417         }
       
   418 
       
   419         iSession = StorageFactory.createSession();
       
   420 
       
   421         // 3. Two closes.
       
   422         try
       
   423         {
       
   424             iSession.close();
       
   425             iSession.close();
       
   426         }
       
   427         catch (StorageException se)
       
   428         {
       
   429             // PASSED
       
   430         }
       
   431         catch (Throwable t)
       
   432         {
       
   433             assertTrue("Two closes: " + t.toString(), false);
       
   434 
       
   435         }
       
   436     }
       
   437 }