Home | History | Annotate | Download | only in ctestfw
      1 /********************************************************************
      2  * COPYRIGHT:
      3  * Copyright (c) 2002-2009, International Business Machines Corporation and
      4  * others. All Rights Reserved.
      5  ********************************************************************/
      6 
      7 /* Created by weiv 05/09/2002 */
      8 
      9 #include "unicode/tstdtmod.h"
     10 #include "cmemory.h"
     11 
     12 TestLog::~TestLog() {}
     13 
     14 TestDataModule *TestDataModule::getTestDataModule(const char* name, TestLog& log, UErrorCode &status)
     15 {
     16   if(U_FAILURE(status)) {
     17     return NULL;
     18   }
     19   TestDataModule *result = NULL;
     20 
     21   // TODO: probe for resource bundle and then for XML.
     22   // According to that, construct an appropriate driver object
     23 
     24   result = new RBTestDataModule(name, log, status);
     25   if(U_SUCCESS(status)) {
     26     return result;
     27   } else {
     28     delete result;
     29     return NULL;
     30   }
     31 }
     32 
     33 TestDataModule::TestDataModule(const char* name, TestLog& log, UErrorCode& /*status*/)
     34 : testName(name),
     35 fInfo(NULL),
     36 fLog(log)
     37 {
     38 }
     39 
     40 TestDataModule::~TestDataModule() {
     41   if(fInfo != NULL) {
     42     delete fInfo;
     43   }
     44 }
     45 
     46 const char * TestDataModule::getName() const
     47 {
     48   return testName;
     49 }
     50 
     51 
     52 
     53 RBTestDataModule::~RBTestDataModule()
     54 {
     55   ures_close(fTestData);
     56   ures_close(fModuleBundle);
     57   ures_close(fInfoRB);
     58   uprv_free(tdpath);
     59 }
     60 
     61 RBTestDataModule::RBTestDataModule(const char* name, TestLog& log, UErrorCode& status)
     62 : TestDataModule(name, log, status),
     63   fModuleBundle(NULL),
     64   fTestData(NULL),
     65   fInfoRB(NULL),
     66   tdpath(NULL)
     67 {
     68   fNumberOfTests = 0;
     69   fDataTestValid = TRUE;
     70   fModuleBundle = getTestBundle(name, status);
     71   if(fDataTestValid) {
     72     fTestData = ures_getByKey(fModuleBundle, "TestData", NULL, &status);
     73     fNumberOfTests = ures_getSize(fTestData);
     74     fInfoRB = ures_getByKey(fModuleBundle, "Info", NULL, &status);
     75     if(status != U_ZERO_ERROR) {
     76       log.errln(UNICODE_STRING_SIMPLE("Unable to initalize test data - missing mandatory description resources!"));
     77       fDataTestValid = FALSE;
     78     } else {
     79       fInfo = new RBDataMap(fInfoRB, status);
     80     }
     81   }
     82 }
     83 
     84 UBool RBTestDataModule::getInfo(const DataMap *& info, UErrorCode &/*status*/) const
     85 {
     86     info = fInfo;
     87     if(fInfo) {
     88         return TRUE;
     89     } else {
     90         return FALSE;
     91     }
     92 }
     93 
     94 TestData* RBTestDataModule::createTestData(int32_t index, UErrorCode &status) const
     95 {
     96   TestData *result = NULL;
     97   UErrorCode intStatus = U_ZERO_ERROR;
     98 
     99   if(fDataTestValid == TRUE) {
    100     // Both of these resources get adopted by a TestData object.
    101     UResourceBundle *DataFillIn = ures_getByIndex(fTestData, index, NULL, &status);
    102     UResourceBundle *headers = ures_getByKey(fInfoRB, "Headers", NULL, &intStatus);
    103 
    104     if(U_SUCCESS(status)) {
    105       result = new RBTestData(DataFillIn, headers, status);
    106 
    107       if(U_SUCCESS(status)) {
    108         return result;
    109       } else {
    110         delete result;
    111       }
    112     } else {
    113       ures_close(DataFillIn);
    114       ures_close(headers);
    115     }
    116   } else {
    117     status = U_MISSING_RESOURCE_ERROR;
    118   }
    119   return NULL;
    120 }
    121 
    122 TestData* RBTestDataModule::createTestData(const char* name, UErrorCode &status) const
    123 {
    124   TestData *result = NULL;
    125   UErrorCode intStatus = U_ZERO_ERROR;
    126 
    127   if(fDataTestValid == TRUE) {
    128     // Both of these resources get adopted by a TestData object.
    129     UResourceBundle *DataFillIn = ures_getByKey(fTestData, name, NULL, &status);
    130     UResourceBundle *headers = ures_getByKey(fInfoRB, "Headers", NULL, &intStatus);
    131 
    132     if(U_SUCCESS(status)) {
    133       result = new RBTestData(DataFillIn, headers, status);
    134       if(U_SUCCESS(status)) {
    135         return result;
    136       } else {
    137         delete result;
    138       }
    139     } else {
    140       ures_close(DataFillIn);
    141       ures_close(headers);
    142     }
    143   } else {
    144     status = U_MISSING_RESOURCE_ERROR;
    145   }
    146   return NULL;
    147 }
    148 
    149 
    150 
    151 //Get test data from ResourceBundles
    152 UResourceBundle*
    153 RBTestDataModule::getTestBundle(const char* bundleName, UErrorCode &status)
    154 {
    155   if(U_SUCCESS(status)) {
    156     UResourceBundle *testBundle = NULL;
    157     const char* icu_data = fLog.getTestDataPath(status);
    158     if (testBundle == NULL) {
    159         testBundle = ures_openDirect(icu_data, bundleName, &status);
    160         if (status != U_ZERO_ERROR) {
    161             fLog.dataerrln(UNICODE_STRING_SIMPLE("Could not load test data from resourcebundle: ") + UnicodeString(bundleName, -1, US_INV) + "\n");
    162             fDataTestValid = FALSE;
    163         }
    164     }
    165     return testBundle;
    166   } else {
    167     return NULL;
    168   }
    169 }
    170 
    171