1 /******************************************************************** 2 * COPYRIGHT: 3 * Copyright (c) 2002-2014, International Business Machines Corporation and 4 * others. All Rights Reserved. 5 ********************************************************************/ 6 7 /* Created by weiv 05/09/2002 */ 8 9 #include <stdarg.h> 10 11 #include "unicode/tstdtmod.h" 12 #include "cmemory.h" 13 #include <stdio.h> 14 15 TestLog::~TestLog() {} 16 17 IcuTestErrorCode::~IcuTestErrorCode() { 18 // Safe because our handleFailure() does not throw exceptions. 19 if(isFailure()) { handleFailure(); } 20 } 21 22 UBool IcuTestErrorCode::logIfFailureAndReset(const char *fmt, ...) { 23 if(isFailure()) { 24 char buffer[4000]; 25 va_list ap; 26 va_start(ap, fmt); 27 vsprintf(buffer, fmt, ap); 28 va_end(ap); 29 UnicodeString msg(testName, -1, US_INV); 30 msg.append(UNICODE_STRING_SIMPLE(" failure: ")).append(UnicodeString(errorName(), -1, US_INV)); 31 msg.append(UNICODE_STRING_SIMPLE(" - ")).append(UnicodeString(buffer, -1, US_INV)); 32 testClass.errln(msg); 33 reset(); 34 return TRUE; 35 } else { 36 reset(); 37 return FALSE; 38 } 39 } 40 41 UBool IcuTestErrorCode::logDataIfFailureAndReset(const char *fmt, ...) { 42 if(isFailure()) { 43 char buffer[4000]; 44 va_list ap; 45 va_start(ap, fmt); 46 vsprintf(buffer, fmt, ap); 47 va_end(ap); 48 UnicodeString msg(testName, -1, US_INV); 49 msg.append(UNICODE_STRING_SIMPLE(" failure: ")).append(UnicodeString(errorName(), -1, US_INV)); 50 msg.append(UNICODE_STRING_SIMPLE(" - ")).append(UnicodeString(buffer, -1, US_INV)); 51 testClass.dataerrln(msg); 52 reset(); 53 return TRUE; 54 } else { 55 reset(); 56 return FALSE; 57 } 58 } 59 60 void IcuTestErrorCode::handleFailure() const { 61 // testClass.errln("%s failure - %s", testName, errorName()); 62 UnicodeString msg(testName, -1, US_INV); 63 msg.append(UNICODE_STRING_SIMPLE(" failure: ")).append(UnicodeString(errorName(), -1, US_INV)); 64 65 if (get() == U_MISSING_RESOURCE_ERROR || get() == U_FILE_ACCESS_ERROR) { 66 testClass.dataerrln(msg); 67 } else { 68 testClass.errln(msg); 69 } 70 } 71 72 TestDataModule *TestDataModule::getTestDataModule(const char* name, TestLog& log, UErrorCode &status) 73 { 74 if(U_FAILURE(status)) { 75 return NULL; 76 } 77 TestDataModule *result = NULL; 78 79 // TODO: probe for resource bundle and then for XML. 80 // According to that, construct an appropriate driver object 81 82 result = new RBTestDataModule(name, log, status); 83 if(U_SUCCESS(status)) { 84 return result; 85 } else { 86 delete result; 87 return NULL; 88 } 89 } 90 91 TestDataModule::TestDataModule(const char* name, TestLog& log, UErrorCode& /*status*/) 92 : testName(name), 93 fInfo(NULL), 94 fLog(log) 95 { 96 } 97 98 TestDataModule::~TestDataModule() { 99 if(fInfo != NULL) { 100 delete fInfo; 101 } 102 } 103 104 const char * TestDataModule::getName() const 105 { 106 return testName; 107 } 108 109 110 111 RBTestDataModule::~RBTestDataModule() 112 { 113 ures_close(fTestData); 114 ures_close(fModuleBundle); 115 ures_close(fInfoRB); 116 uprv_free(tdpath); 117 } 118 119 RBTestDataModule::RBTestDataModule(const char* name, TestLog& log, UErrorCode& status) 120 : TestDataModule(name, log, status), 121 fModuleBundle(NULL), 122 fTestData(NULL), 123 fInfoRB(NULL), 124 tdpath(NULL) 125 { 126 fNumberOfTests = 0; 127 fDataTestValid = TRUE; 128 fModuleBundle = getTestBundle(name, status); 129 if(fDataTestValid) { 130 fTestData = ures_getByKey(fModuleBundle, "TestData", NULL, &status); 131 fNumberOfTests = ures_getSize(fTestData); 132 fInfoRB = ures_getByKey(fModuleBundle, "Info", NULL, &status); 133 if(status != U_ZERO_ERROR) { 134 log.errln(UNICODE_STRING_SIMPLE("Unable to initalize test data - missing mandatory description resources!")); 135 fDataTestValid = FALSE; 136 } else { 137 fInfo = new RBDataMap(fInfoRB, status); 138 } 139 } 140 } 141 142 UBool RBTestDataModule::getInfo(const DataMap *& info, UErrorCode &/*status*/) const 143 { 144 info = fInfo; 145 if(fInfo) { 146 return TRUE; 147 } else { 148 return FALSE; 149 } 150 } 151 152 TestData* RBTestDataModule::createTestData(int32_t index, UErrorCode &status) const 153 { 154 TestData *result = NULL; 155 UErrorCode intStatus = U_ZERO_ERROR; 156 157 if(fDataTestValid == TRUE) { 158 // Both of these resources get adopted by a TestData object. 159 UResourceBundle *DataFillIn = ures_getByIndex(fTestData, index, NULL, &status); 160 UResourceBundle *headers = ures_getByKey(fInfoRB, "Headers", NULL, &intStatus); 161 162 if(U_SUCCESS(status)) { 163 result = new RBTestData(DataFillIn, headers, status); 164 165 if(U_SUCCESS(status)) { 166 return result; 167 } else { 168 delete result; 169 } 170 } else { 171 ures_close(DataFillIn); 172 ures_close(headers); 173 } 174 } else { 175 status = U_MISSING_RESOURCE_ERROR; 176 } 177 return NULL; 178 } 179 180 TestData* RBTestDataModule::createTestData(const char* name, UErrorCode &status) const 181 { 182 TestData *result = NULL; 183 UErrorCode intStatus = U_ZERO_ERROR; 184 185 if(fDataTestValid == TRUE) { 186 // Both of these resources get adopted by a TestData object. 187 UResourceBundle *DataFillIn = ures_getByKey(fTestData, name, NULL, &status); 188 UResourceBundle *headers = ures_getByKey(fInfoRB, "Headers", NULL, &intStatus); 189 190 if(U_SUCCESS(status)) { 191 result = new RBTestData(DataFillIn, headers, status); 192 if(U_SUCCESS(status)) { 193 return result; 194 } else { 195 delete result; 196 } 197 } else { 198 ures_close(DataFillIn); 199 ures_close(headers); 200 } 201 } else { 202 status = U_MISSING_RESOURCE_ERROR; 203 } 204 return NULL; 205 } 206 207 208 209 //Get test data from ResourceBundles 210 UResourceBundle* 211 RBTestDataModule::getTestBundle(const char* bundleName, UErrorCode &status) 212 { 213 if(U_SUCCESS(status)) { 214 UResourceBundle *testBundle = NULL; 215 const char* icu_data = fLog.getTestDataPath(status); 216 if (testBundle == NULL) { 217 testBundle = ures_openDirect(icu_data, bundleName, &status); 218 if (status != U_ZERO_ERROR) { 219 fLog.dataerrln(UNICODE_STRING_SIMPLE("Could not load test data from resourcebundle: ") + UnicodeString(bundleName, -1, US_INV)); 220 fDataTestValid = FALSE; 221 } 222 } 223 return testBundle; 224 } else { 225 return NULL; 226 } 227 } 228 229