javacommons/javastorage/tsrc/java_api/javasrc/com/nokia/mj/test/storage/TestRobustness.java
branchRCL_3
changeset 19 04becd199f91
child 80 d6dafc5d983f
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     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 OTA table specific test cases. See test methods for test
       
    32  * case details.
       
    33  */
       
    34 public class TestRobustness extends TestCase implements InstallerMain, StorageNames
       
    35 {
       
    36     /**
       
    37      * Directory for JavaStorage tests.
       
    38      */
       
    39     private static final String iTestRoot = "./jstest";
       
    40 
       
    41     /**
       
    42      * Directory for JavaStorage journal and temp files.
       
    43      */
       
    44     private static final String iIsRoot = iTestRoot + "/js";
       
    45 
       
    46     private StorageSession iSession = null;
       
    47     private StorageSessionTestUtils iJtu = null;
       
    48 
       
    49     public void installerMain(String[] args)
       
    50     {
       
    51         TestSuite suite = new TestSuite(this.getClass().getName());
       
    52 
       
    53         suite.addTest(new TestRobustness("testAttributeValues", new TestMethod()
       
    54         {
       
    55             public void run(TestCase tc)
       
    56             {
       
    57                 ((TestRobustness)tc).testAttributeValues();
       
    58             }
       
    59         }));
       
    60 
       
    61         suite.addTest(new TestRobustness("testInvalidAttributeValues", new TestMethod()
       
    62         {
       
    63             public void run(TestCase tc)
       
    64             {
       
    65                 ((TestRobustness)tc).testInvalidAttributeValues();
       
    66             }
       
    67         }));
       
    68 
       
    69         com.nokia.mj.impl.utils.OmjTestRunner.run(suite);
       
    70     }
       
    71 
       
    72     public TestRobustness()
       
    73     {
       
    74     }
       
    75 
       
    76     public TestRobustness(String aTestName, TestMethod aTestMethod)
       
    77     {
       
    78         super(aTestName, aTestMethod);
       
    79     }
       
    80 
       
    81     protected void setUp()
       
    82     {
       
    83         iSession = StorageFactory.createSession();
       
    84         iJtu = new StorageSessionTestUtils();
       
    85     }
       
    86 
       
    87     protected void tearDown()
       
    88     {
       
    89         if (iSession != null)
       
    90         {
       
    91             try
       
    92             {
       
    93                 iSession.destroySession();
       
    94             }
       
    95             catch (StorageException se)
       
    96             {
       
    97                 // No can do
       
    98                 System.out.println("TearDown failed: " + se.toString());
       
    99             }
       
   100         }
       
   101     }
       
   102 
       
   103     /**
       
   104      * Test complicated attribute values. Write and read attributes with
       
   105      * complicated values to/from storage.
       
   106      *
       
   107      * 1. Value is 'null' string.
       
   108      * 2. Value contains ';' char.
       
   109      * 3. Value contains ''' char.
       
   110      * 4. Value contains '*' char.
       
   111      * 5. Value contains '!' char.
       
   112      * 6. Value contains ' ' char.
       
   113      * 7. Value contains '?' char.
       
   114      * 8. Value contains '#' char.
       
   115      * 9. Value contains '@' char.
       
   116      * 10. Value contains '€' char.
       
   117      * 11. Value contains '%' char.
       
   118      * 12. Value contains '/' char.
       
   119      * 13. Value contains '\' char.
       
   120      * 14. Value contains '\\' string.
       
   121      * 15. Value contains ':' char.
       
   122      * 16. Value contains '.' char.
       
   123      * 17. Value contains ',' char.
       
   124      * 18. Value contains '-' char.
       
   125      * 19. Value contains '_' char.
       
   126      * 20. Value contains '=' char.
       
   127      * 21. Value contains '>' char.
       
   128      * 22. Value contains '<' char.
       
   129      * 23. Value contains '|' char.
       
   130      * 24. Value contains '&' char.
       
   131      * 25. Value contains '&&' string.
       
   132      * 26. Value contains '||' string.
       
   133      * 27. Value contains '[' char.
       
   134      * 28. Value contains '[]' string.
       
   135      * 29. Value contains '{' char.
       
   136      * 30. Value contains '{}' string.
       
   137      * 31. Value contains 'AND' string.
       
   138      * 32. Value contains 'OR' string.
       
   139      * 33. Value contains '''' string.
       
   140      * 34. Value contains '$' char.
       
   141      * 35. Value contains '"' char.
       
   142      * 36. Value contains '\n' char.
       
   143      * 37. Value contains '\t' char.
       
   144      * 38. Value contains '\0' char.
       
   145      */
       
   146     public void testAttributeValues()
       
   147     {
       
   148         // 1. Value is 'null' string.
       
   149         executeAttributeTest("at1", "null");
       
   150 
       
   151         // 2. Value contains ';' char.
       
   152         executeAttributeTest("at2", "Great;Vendor");
       
   153 
       
   154         // 3. Value contains ''' char.
       
   155         executeAttributeTest("at3", "Band'Char'Inside`Again");
       
   156 
       
   157         // 4. Value contains '*' char.
       
   158         executeAttributeTest("at4", "Foo*Bar");
       
   159 
       
   160         // 5. Value contains '!' char.
       
   161         executeAttributeTest("at5", "Call Me!");
       
   162 
       
   163         // 6. Value contains ' ' char.
       
   164         executeAttributeTest("at6", "White space");
       
   165 
       
   166         // 7. Value contains '?' char.
       
   167         executeAttributeTest("at7", "Do you question me?");
       
   168 
       
   169         // 8. Value contains '#' char.
       
   170         executeAttributeTest("at8", "# code");
       
   171 
       
   172         // 9. Value contains '@' char.
       
   173         executeAttributeTest("at9", "temporary@address.com");
       
   174 
       
   175         // This is EURO character but SVN converts it to some different!
       
   176         // 10. Value contains Euro char. Two different euro codes
       
   177         executeAttributeTest("at10", "\u20ac");
       
   178 
       
   179         // 11. Value contains '%' char.
       
   180         executeAttributeTest("at11", "Done 100%");
       
   181 
       
   182         // 12. Value contains '/' char.
       
   183         executeAttributeTest("at12", "By/Gone");
       
   184 
       
   185         // 13. Value contains '\' char.
       
   186         executeAttributeTest("at13", "Gone\by");
       
   187 
       
   188         // 14. Value contains '\\' string.
       
   189         executeAttributeTest("at14", "Gone\\Beyond");
       
   190 
       
   191         // 15. Value contains ':' char.
       
   192         executeAttributeTest("at15", "Hmm:mmH");
       
   193 
       
   194         // 16. Value contains '.' char.
       
   195         executeAttributeTest("at16", "Dot.Point");
       
   196 
       
   197         // 17. Value contains ',' char.
       
   198         executeAttributeTest("at17", "comma,separated");
       
   199 
       
   200         // 18. Value contains '-' char.
       
   201         executeAttributeTest("at18", "Score-Me");
       
   202 
       
   203         // 19. Value contains '_' char.
       
   204         executeAttributeTest("at19", "Under_Score");
       
   205 
       
   206         // 20. Value contains '=' char.
       
   207         executeAttributeTest("at20", "Im=Equal");
       
   208 
       
   209         // 21. Value contains '>' char.
       
   210         executeAttributeTest("at21", "Greater>Value");
       
   211 
       
   212         // 22. Value contains '<' char.
       
   213         executeAttributeTest("at22", "Below<Boundary");
       
   214 
       
   215         // 23. Value contains '|' char.
       
   216         executeAttributeTest("at23", "Almos|Spacer");
       
   217 
       
   218         // 24. Value contains '&' char.
       
   219         executeAttributeTest("at24", "And&");
       
   220 
       
   221         // 25. Value contains '&&' string.
       
   222         executeAttributeTest("at25", "Double&&A");
       
   223 
       
   224         // 26. Value contains '||' string.
       
   225         executeAttributeTest("at26", "OrOr||");
       
   226 
       
   227         // 27. Value contains '[' char.
       
   228         executeAttributeTest("at27", "Keep[Separated");
       
   229 
       
   230         // 28. Value contains '[]' string.
       
   231         executeAttributeTest("at28", "[Surround Me]");
       
   232 
       
   233         // 29. Value contains '{' char.
       
   234         executeAttributeTest("at29", "Curly{One");
       
   235 
       
   236         // 30. Value contains '{}' string.
       
   237         executeAttributeTest("at30", "{Fully Curled}");
       
   238 
       
   239         // 31. Value contains 'AND' string.
       
   240         executeAttributeTest("at31", "Me AND Your");
       
   241 
       
   242         // 32. Value contains 'OR' string.
       
   243         executeAttributeTest("at32", "first OR second");
       
   244 
       
   245         // 33. Value contains '''' string.
       
   246         executeAttributeTest("at33", "'Oh'");
       
   247 
       
   248         // 34. Value contains '$' char.
       
   249         executeAttributeTest("at34", "$is Money");
       
   250 
       
   251         // 35. Value contains '"' char.
       
   252         executeAttributeTest("at35", "Time \"is\" Money");
       
   253 
       
   254         // 36. Value contains '\n' char.
       
   255         executeAttributeTest("at36", "Why \n not");
       
   256 
       
   257         // 37. Value contains '\t' char.
       
   258         executeAttributeTest("at37", "Decoration \t is beatiful");
       
   259     }
       
   260 
       
   261     /**
       
   262      * Test invalid attribute values.
       
   263      *
       
   264      * 1. Value contains '\0' char. StorageException is thrown.
       
   265      * 2. Storage name contains '\0' char. StorageException is thrown.
       
   266      */
       
   267     public void testInvalidAttributeValues()
       
   268     {
       
   269         // 1. Value contains '\0' char.
       
   270         try
       
   271         {
       
   272             iSession.open();
       
   273             iSession.startTransaction();
       
   274 
       
   275             String tableName =  APPLICATION_PACKAGE_TABLE;
       
   276             StorageEntry entry = new StorageEntry();
       
   277 
       
   278             entry.addAttribute(
       
   279                 new StorageAttribute(VENDOR, "Terminated\0Middle"));
       
   280 
       
   281             entry.addAttribute(
       
   282                 new StorageAttribute(ID, "MyBrokenValue"));
       
   283 
       
   284             iSession.write(tableName, entry);
       
   285 
       
   286             iSession.rollbackTransaction();
       
   287             assertTrue("No exp when containing invalid char", false);
       
   288         }
       
   289         catch (StorageException se)
       
   290         {
       
   291             // PASSED
       
   292         }
       
   293         catch (Throwable t)
       
   294         {
       
   295             iSession.rollbackTransaction();
       
   296             assertTrue("UnExp exp1: " + t.toString(), false);
       
   297         }
       
   298         iSession.close();
       
   299 
       
   300         // 2. Storage name contains '\0' char. StorageException is thrown.
       
   301         try
       
   302         {
       
   303             iSession.open("Got enough\0Nulls");
       
   304             assertTrue("No exp when DB name containing invalid char", false);
       
   305         }
       
   306         catch (StorageException se)
       
   307         {
       
   308             // PASSED
       
   309         }
       
   310         catch (Throwable t)
       
   311         {
       
   312             assertTrue("UnExp exp2: " + t.toString(), false);
       
   313         }
       
   314     }
       
   315     private void executeAttributeTest(String aUid, String aValue)
       
   316     {
       
   317         try
       
   318         {
       
   319             iSession.open();
       
   320             iSession.startTransaction();
       
   321 
       
   322             String tableName =  APPLICATION_PACKAGE_TABLE;
       
   323             StorageEntry entry = new StorageEntry();
       
   324 
       
   325             entry.addAttribute(
       
   326                 new StorageAttribute(VENDOR, aValue));
       
   327 
       
   328             assertTrue("Populate failed",
       
   329                        iJtu.populate(iSession, tableName, aUid, entry));
       
   330 
       
   331             assertTrue("Check failed",
       
   332                        iJtu.checkParams(iSession, tableName, entry, 1));
       
   333 
       
   334             // Utils add ID to entry
       
   335             entry.removeAttribute(ID);
       
   336 
       
   337             assertTrue("Remove failed",
       
   338                        iJtu.remove(iSession, tableName, aUid, entry));
       
   339 
       
   340             iSession.rollbackTransaction();
       
   341             iSession.close();
       
   342         }
       
   343         catch (Throwable t)
       
   344         {
       
   345             iSession.rollbackTransaction();
       
   346             iSession.close();
       
   347             assertTrue("UnExp exp1: " + t.toString(), false);
       
   348         }
       
   349     }
       
   350 }