javamanager/javaregistry/tsrc/src/utils/storagetestutils.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 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 #include "storagetestutils.h"
       
    19 #include "javastorageexception.h"
       
    20 #include "javacommonutils.h"
       
    21 #include "javaoslayer.h"
       
    22 
       
    23 #include "javastorage.h"
       
    24 #include "javastorageentry.h"
       
    25 #include "javastoragenames.h"
       
    26 #include "javauid.h"
       
    27 
       
    28 using namespace java::storage;
       
    29 using namespace java::util;
       
    30 using namespace std;
       
    31 
       
    32 OS_EXPORT JavaStorageTestUtils::JavaStorageTestUtils()
       
    33 {
       
    34 }
       
    35 
       
    36 OS_EXPORT JavaStorageTestUtils::~JavaStorageTestUtils()
       
    37 {
       
    38 }
       
    39 
       
    40 /**
       
    41  * Compare two JavaStorageEntry to each other.
       
    42  *
       
    43  * @return true if match.
       
    44  */
       
    45 OS_EXPORT bool JavaStorageTestUtils::compareEntry(
       
    46     const JavaStorageEntry& aRef,
       
    47     const JavaStorageEntry& aSource)
       
    48 {
       
    49     bool equals = true;
       
    50     if (aRef.entryName() != aSource.entryName())
       
    51     {
       
    52         equals = false;
       
    53         printf("[JTU]: Entry name does not match\n");
       
    54         char* sqlInUtf = JavaCommonUtils::wstringToUtf8(aRef.entryName());
       
    55         printf("[JTU]: Entry Ref Name: '%s'", sqlInUtf);
       
    56         delete[] sqlInUtf;
       
    57 
       
    58         sqlInUtf = JavaCommonUtils::wstringToUtf8(aSource.entryName());
       
    59         printf(" Source Name: '%s'\n", sqlInUtf);
       
    60         delete[] sqlInUtf;
       
    61     }
       
    62     else if (aRef.entryValue() != aSource.entryValue())
       
    63     {
       
    64         equals = false;
       
    65         printf("[JTU]: Entry value does not match\n");
       
    66         char* sqlInUtf = JavaCommonUtils::wstringToUtf8(aRef.entryValue());
       
    67         printf("[JTU]: Entry Ref val: '%s'", sqlInUtf);
       
    68         delete[] sqlInUtf;
       
    69 
       
    70         sqlInUtf = JavaCommonUtils::wstringToUtf8(aSource.entryValue());
       
    71         printf(" Source val: '%s'\n", sqlInUtf);
       
    72         delete[] sqlInUtf;
       
    73     }
       
    74 
       
    75     return equals;
       
    76 }
       
    77 
       
    78 /**
       
    79  * Compare two ApplicationEntry to each other.
       
    80  *
       
    81  * @return true if match.
       
    82  */
       
    83 OS_EXPORT bool JavaStorageTestUtils::compareEntries(
       
    84     const JavaStorageApplicationEntry_t& aRef,
       
    85     const JavaStorageApplicationEntry_t& aSource)
       
    86 {
       
    87     bool equals = false;
       
    88 
       
    89     if (aRef.size() != aSource.size())
       
    90     {
       
    91         printf("[JTU]: Size does not match. RefSize: %d", aRef.size());
       
    92         printf(" SourceSize: %d\n", aSource.size());
       
    93         printf("Printing ref entry\n");
       
    94         printEntry(aRef);
       
    95         printf("Printing source entry\n");
       
    96         printEntry(aSource);
       
    97 
       
    98         return equals;
       
    99     }
       
   100 
       
   101     JavaStorageApplicationEntry_t::const_iterator refIter;
       
   102 
       
   103     // Go through all reference values.
       
   104     for (refIter = aRef.begin(); refIter != aRef.end(); refIter++)
       
   105     {
       
   106         // printf("[JTU]: Comparing entry\n");
       
   107 
       
   108         JavaStorageEntry refEntry = (*refIter);
       
   109 
       
   110         // Check source contains same entry.
       
   111         JavaStorageApplicationEntry_t::const_iterator sourceIterator =
       
   112             aSource.find(refEntry);
       
   113 
       
   114         if (sourceIterator != aSource.end())
       
   115         {
       
   116             JavaStorageEntry sourceEntry = (*sourceIterator);
       
   117             // Make sure it definitely is same.
       
   118             bool result = compareEntry(refEntry, sourceEntry);
       
   119             if (!result)
       
   120             {
       
   121                 printf("[JTU]: Entry did not match\n");
       
   122                 return equals;
       
   123             }
       
   124         }
       
   125         else
       
   126         {
       
   127             printf("[JTU]: No similar source entry found\n");
       
   128             return equals;
       
   129         }
       
   130     }
       
   131     return true;
       
   132 }
       
   133 
       
   134 OS_EXPORT bool JavaStorageTestUtils::populate(
       
   135     JavaStorage& aStorage,
       
   136     const string& aTableName,
       
   137     const JavaStorageApplicationEntry_t& aRef)
       
   138 {
       
   139     bool success = false;
       
   140 
       
   141     try
       
   142     {
       
   143         aStorage.write(aTableName, aRef);
       
   144         success = true;
       
   145     }
       
   146     catch (JavaStorageException& jse)
       
   147     {
       
   148         printf("[JTU]: JSE while populating: %s\n", jse.toString().c_str());
       
   149     }
       
   150     catch (...)
       
   151     {
       
   152         printf("[JTU]: Unexcpedted exception thrown while populating\n");
       
   153     }
       
   154 
       
   155     return success;
       
   156 }
       
   157 
       
   158 
       
   159 OS_EXPORT bool JavaStorageTestUtils::update(
       
   160     JavaStorage& aStorage,
       
   161     const string& aTableName,
       
   162     const JavaStorageApplicationEntry_t& aUpdate,
       
   163     const JavaStorageApplicationEntry_t& aMatch)
       
   164 {
       
   165     bool success = false;
       
   166 
       
   167     try
       
   168     {
       
   169         aStorage.update(aTableName, aUpdate, aMatch);
       
   170         success = true;
       
   171     }
       
   172     catch (JavaStorageException& jse)
       
   173     {
       
   174         printf("[JTU]: JSE while updating: %s\n", jse.toString().c_str());
       
   175     }
       
   176     catch (...)
       
   177     {
       
   178         printf("[JTU]: Unexcpedted exception thrown while updating\n");
       
   179     }
       
   180 
       
   181     return success;
       
   182 }
       
   183 
       
   184 OS_EXPORT bool JavaStorageTestUtils::checkParams(
       
   185     JavaStorage& aStorage,
       
   186     const string& aTableName,
       
   187     const JavaStorageApplicationEntry_t& aRef,
       
   188     int aFoundApps)
       
   189 {
       
   190     bool equals = false;
       
   191 
       
   192     try
       
   193     {
       
   194         JavaStorageApplicationList_t foundApps;
       
   195 
       
   196         aStorage.search(aTableName, aRef, foundApps);
       
   197 
       
   198         if (foundApps.size() != aFoundApps)
       
   199         {
       
   200             printf("[JTU]: Invalid amount of applications received: ");
       
   201             printf("Expected: %d", aFoundApps);
       
   202             printf(" Found: %d\n", foundApps.size());
       
   203 
       
   204             /*            // ################################
       
   205 
       
   206                         JavaStorageApplicationList_t::const_iterator appIter;
       
   207                         appIter = foundApps.begin();
       
   208 
       
   209                         if (foundApps.size() > 0)
       
   210                         {
       
   211                             printEntry((*appIter));
       
   212                             appIter++;
       
   213                         }
       
   214                         if (foundApps.size() > 1)
       
   215                         {
       
   216                             printEntry((*appIter));
       
   217                         }
       
   218 
       
   219                         // ##################################
       
   220             */
       
   221             return false;
       
   222         }
       
   223         else
       
   224         {
       
   225             JavaStorageApplicationList_t::const_iterator appIter;
       
   226 
       
   227             // Go through all entries
       
   228             for (appIter = foundApps.begin(); appIter != foundApps.end(); appIter++)
       
   229             {
       
   230                 // printf("Comparing application\n");
       
   231                 JavaStorageApplicationEntry_t app = (*appIter);
       
   232 
       
   233                 equals = compareEntries(aRef, app);
       
   234                 app.clear();
       
   235                 if (!equals)
       
   236                 {
       
   237                     printf("Applications did not match\n");
       
   238                     return false;
       
   239                 }
       
   240             }
       
   241         }
       
   242         foundApps.clear();
       
   243 
       
   244         equals = true;
       
   245     }
       
   246     catch (JavaStorageException& jse)
       
   247     {
       
   248         printf("[JTU]: JSE while checking: %s\n", jse.toString().c_str());
       
   249     }
       
   250     catch (...)
       
   251     {
       
   252         printf("[JTU]: Unexcpedted exception thrown while checking\n");
       
   253     }
       
   254 
       
   255     return equals;
       
   256 }
       
   257 
       
   258 OS_EXPORT bool JavaStorageTestUtils::remove(
       
   259     JavaStorage& aStorage,
       
   260     const string& aTableName,
       
   261     const JavaStorageApplicationEntry_t& aRef)
       
   262 {
       
   263     bool success = false;
       
   264 
       
   265     try
       
   266     {
       
   267         aStorage.remove(aTableName, aRef);
       
   268 
       
   269         JavaStorageApplicationList_t foundApps;
       
   270 
       
   271         // Verify removal
       
   272         aStorage.search(aTableName, aRef, foundApps);
       
   273         // printf("[JTU]: Entries found: %d\n", foundApps.size());
       
   274         if (foundApps.size() == 0)
       
   275         {
       
   276             success = true;
       
   277         }
       
   278     }
       
   279     catch (JavaStorageException& jse)
       
   280     {
       
   281         printf("[JTU]: JSE while removing: %s\n", jse.toString().c_str());
       
   282     }
       
   283     catch (...)
       
   284     {
       
   285         printf("[JTU]: Unexcpedted exception thrown while removing\n");
       
   286     }
       
   287 
       
   288     return success;
       
   289 }
       
   290 
       
   291 OS_EXPORT void JavaStorageTestUtils::createAppDataEntry(
       
   292     JavaStorageApplicationEntry_t& aEntry)
       
   293 {
       
   294     wstring name = PACKAGE_NAME;
       
   295     wstring nameValue = L"MyDefaultMIDlet";
       
   296     JavaStorageEntry entry;
       
   297 
       
   298     entry.setEntry(name, nameValue);
       
   299     aEntry.insert(entry);
       
   300 
       
   301     name = VENDOR;
       
   302     nameValue = L"MyDefaultVendor";
       
   303     entry.setEntry(name, nameValue);
       
   304     aEntry.insert(entry);
       
   305 
       
   306     name = VERSION;
       
   307     nameValue = L"12.12.12";
       
   308     entry.setEntry(name, nameValue);
       
   309     aEntry.insert(entry);
       
   310 
       
   311     name = ROOT_PATH;
       
   312     nameValue = L"/home/default";
       
   313     entry.setEntry(name, nameValue);
       
   314     aEntry.insert(entry);
       
   315 
       
   316     name = MEDIA_ID;
       
   317     nameValue = L"/mnt/DefaultMMC";
       
   318     entry.setEntry(name, nameValue);
       
   319     aEntry.insert(entry);
       
   320 
       
   321     name = INITIAL_SIZE;
       
   322     nameValue = L"1234";
       
   323     entry.setEntry(name, nameValue, JavaStorageEntry::INT);
       
   324     aEntry.insert(entry);
       
   325 
       
   326     name = JAD_PATH;
       
   327     nameValue = L"/usr/local/defaultApp/JAD";
       
   328     entry.setEntry(name, nameValue);
       
   329     aEntry.insert(entry);
       
   330 
       
   331     name = JAR_PATH;
       
   332     nameValue = L"/usr/local/defaultApp/JAR";
       
   333     entry.setEntry(name, nameValue);
       
   334     aEntry.insert(entry);
       
   335 
       
   336     name = ACCESS_POINT;
       
   337     nameValue = L"Default Access Point";
       
   338     entry.setEntry(name, nameValue);
       
   339     aEntry.insert(entry);
       
   340 }
       
   341 
       
   342 OS_EXPORT void JavaStorageTestUtils::createDefaultEntry(
       
   343     JavaStorageApplicationEntry_t& aEntry)
       
   344 {
       
   345     JavaStorageEntry entry;
       
   346     entry.setEntry(PACKAGE_NAME, L"BasicGame");
       
   347     aEntry.insert(entry);
       
   348 
       
   349     entry.setEntry(VENDOR, L"GreatVendor");
       
   350     aEntry.insert(entry);
       
   351 
       
   352     entry.setEntry(VERSION, L"18.01.91");
       
   353     aEntry.insert(entry);
       
   354 }
       
   355 
       
   356 OS_EXPORT void JavaStorageTestUtils::printEntry(
       
   357     JavaStorageApplicationEntry_t aEntry)
       
   358 {
       
   359     JavaStorageApplicationEntry_t::const_iterator iter;
       
   360 
       
   361     printf("    [JTU]: Printing entry\n");
       
   362 
       
   363     // Go through all reference values.
       
   364     for (iter = aEntry.begin(); iter != aEntry.end(); iter++)
       
   365     {
       
   366         JavaStorageEntry entry = (*iter);
       
   367         char* name = JavaCommonUtils::wstringToUtf8(entry.entryName());
       
   368         char* value = JavaCommonUtils::wstringToUtf8(entry.entryValue());
       
   369         printf("    Entry: %s=", name);
       
   370         printf("'%s'\n", value);
       
   371 
       
   372         delete[] name;
       
   373         delete[] value;
       
   374     }
       
   375     printf("    [JTU]: Printing entry DONE\n");
       
   376 
       
   377 }
       
   378 OS_EXPORT void JavaStorageTestUtils::databasePath(std::string& aDatabasePath)
       
   379 {
       
   380     JavaOsLayer::getOsSpecificJavaRootPath(aDatabasePath);
       
   381 
       
   382     int endIdx = (aDatabasePath.size() - 1);
       
   383 
       
   384     if (aDatabasePath[endIdx] != '/' && aDatabasePath[endIdx] != '\\')
       
   385     {
       
   386         // Detect used slash char
       
   387         // This do not work if root path is without slashes. Then windows
       
   388         // path is expected.
       
   389         int idx = aDatabasePath.find("/");
       
   390 
       
   391         if (idx != -1)
       
   392         {
       
   393             aDatabasePath.append("/bin/");
       
   394         }
       
   395         else
       
   396         {
       
   397             aDatabasePath.append("\\bin\\");
       
   398         }
       
   399     }
       
   400 }
       
   401