michael@0: /******************************************************************** michael@0: * COPYRIGHT: michael@0: * Copyright (c) 2002-2010, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: ********************************************************************/ michael@0: michael@0: /* Created by weiv 05/09/2002 */ michael@0: michael@0: #include michael@0: michael@0: #include "unicode/tstdtmod.h" michael@0: #include "cmemory.h" michael@0: #include michael@0: michael@0: TestLog::~TestLog() {} michael@0: michael@0: IcuTestErrorCode::~IcuTestErrorCode() { michael@0: // Safe because our handleFailure() does not throw exceptions. michael@0: if(isFailure()) { handleFailure(); } michael@0: } michael@0: michael@0: UBool IcuTestErrorCode::logIfFailureAndReset(const char *fmt, ...) { michael@0: if(isFailure()) { michael@0: char buffer[4000]; michael@0: va_list ap; michael@0: va_start(ap, fmt); michael@0: vsprintf(buffer, fmt, ap); michael@0: va_end(ap); michael@0: UnicodeString msg(testName, -1, US_INV); michael@0: msg.append(UNICODE_STRING_SIMPLE(" failure: ")).append(UnicodeString(errorName(), -1, US_INV)); michael@0: msg.append(UNICODE_STRING_SIMPLE(" - ")).append(UnicodeString(buffer, -1, US_INV)); michael@0: testClass.errln(msg); michael@0: reset(); michael@0: return TRUE; michael@0: } else { michael@0: reset(); michael@0: return FALSE; michael@0: } michael@0: } michael@0: michael@0: UBool IcuTestErrorCode::logDataIfFailureAndReset(const char *fmt, ...) { michael@0: if(isFailure()) { michael@0: char buffer[4000]; michael@0: va_list ap; michael@0: va_start(ap, fmt); michael@0: vsprintf(buffer, fmt, ap); michael@0: va_end(ap); michael@0: UnicodeString msg(testName, -1, US_INV); michael@0: msg.append(UNICODE_STRING_SIMPLE(" failure: ")).append(UnicodeString(errorName(), -1, US_INV)); michael@0: msg.append(UNICODE_STRING_SIMPLE(" - ")).append(UnicodeString(buffer, -1, US_INV)); michael@0: testClass.dataerrln(msg); michael@0: reset(); michael@0: return TRUE; michael@0: } else { michael@0: reset(); michael@0: return FALSE; michael@0: } michael@0: } michael@0: michael@0: void IcuTestErrorCode::handleFailure() const { michael@0: // testClass.errln("%s failure - %s", testName, errorName()); michael@0: UnicodeString msg(testName, -1, US_INV); michael@0: msg.append(UNICODE_STRING_SIMPLE(" failure: ")).append(UnicodeString(errorName(), -1, US_INV)); michael@0: michael@0: if (get() == U_MISSING_RESOURCE_ERROR) { michael@0: testClass.dataerrln(msg); michael@0: } else { michael@0: testClass.errln(msg); michael@0: } michael@0: } michael@0: michael@0: TestDataModule *TestDataModule::getTestDataModule(const char* name, TestLog& log, UErrorCode &status) michael@0: { michael@0: if(U_FAILURE(status)) { michael@0: return NULL; michael@0: } michael@0: TestDataModule *result = NULL; michael@0: michael@0: // TODO: probe for resource bundle and then for XML. michael@0: // According to that, construct an appropriate driver object michael@0: michael@0: result = new RBTestDataModule(name, log, status); michael@0: if(U_SUCCESS(status)) { michael@0: return result; michael@0: } else { michael@0: delete result; michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: TestDataModule::TestDataModule(const char* name, TestLog& log, UErrorCode& /*status*/) michael@0: : testName(name), michael@0: fInfo(NULL), michael@0: fLog(log) michael@0: { michael@0: } michael@0: michael@0: TestDataModule::~TestDataModule() { michael@0: if(fInfo != NULL) { michael@0: delete fInfo; michael@0: } michael@0: } michael@0: michael@0: const char * TestDataModule::getName() const michael@0: { michael@0: return testName; michael@0: } michael@0: michael@0: michael@0: michael@0: RBTestDataModule::~RBTestDataModule() michael@0: { michael@0: ures_close(fTestData); michael@0: ures_close(fModuleBundle); michael@0: ures_close(fInfoRB); michael@0: uprv_free(tdpath); michael@0: } michael@0: michael@0: RBTestDataModule::RBTestDataModule(const char* name, TestLog& log, UErrorCode& status) michael@0: : TestDataModule(name, log, status), michael@0: fModuleBundle(NULL), michael@0: fTestData(NULL), michael@0: fInfoRB(NULL), michael@0: tdpath(NULL) michael@0: { michael@0: fNumberOfTests = 0; michael@0: fDataTestValid = TRUE; michael@0: fModuleBundle = getTestBundle(name, status); michael@0: if(fDataTestValid) { michael@0: fTestData = ures_getByKey(fModuleBundle, "TestData", NULL, &status); michael@0: fNumberOfTests = ures_getSize(fTestData); michael@0: fInfoRB = ures_getByKey(fModuleBundle, "Info", NULL, &status); michael@0: if(status != U_ZERO_ERROR) { michael@0: log.errln(UNICODE_STRING_SIMPLE("Unable to initalize test data - missing mandatory description resources!")); michael@0: fDataTestValid = FALSE; michael@0: } else { michael@0: fInfo = new RBDataMap(fInfoRB, status); michael@0: } michael@0: } michael@0: } michael@0: michael@0: UBool RBTestDataModule::getInfo(const DataMap *& info, UErrorCode &/*status*/) const michael@0: { michael@0: info = fInfo; michael@0: if(fInfo) { michael@0: return TRUE; michael@0: } else { michael@0: return FALSE; michael@0: } michael@0: } michael@0: michael@0: TestData* RBTestDataModule::createTestData(int32_t index, UErrorCode &status) const michael@0: { michael@0: TestData *result = NULL; michael@0: UErrorCode intStatus = U_ZERO_ERROR; michael@0: michael@0: if(fDataTestValid == TRUE) { michael@0: // Both of these resources get adopted by a TestData object. michael@0: UResourceBundle *DataFillIn = ures_getByIndex(fTestData, index, NULL, &status); michael@0: UResourceBundle *headers = ures_getByKey(fInfoRB, "Headers", NULL, &intStatus); michael@0: michael@0: if(U_SUCCESS(status)) { michael@0: result = new RBTestData(DataFillIn, headers, status); michael@0: michael@0: if(U_SUCCESS(status)) { michael@0: return result; michael@0: } else { michael@0: delete result; michael@0: } michael@0: } else { michael@0: ures_close(DataFillIn); michael@0: ures_close(headers); michael@0: } michael@0: } else { michael@0: status = U_MISSING_RESOURCE_ERROR; michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: TestData* RBTestDataModule::createTestData(const char* name, UErrorCode &status) const michael@0: { michael@0: TestData *result = NULL; michael@0: UErrorCode intStatus = U_ZERO_ERROR; michael@0: michael@0: if(fDataTestValid == TRUE) { michael@0: // Both of these resources get adopted by a TestData object. michael@0: UResourceBundle *DataFillIn = ures_getByKey(fTestData, name, NULL, &status); michael@0: UResourceBundle *headers = ures_getByKey(fInfoRB, "Headers", NULL, &intStatus); michael@0: michael@0: if(U_SUCCESS(status)) { michael@0: result = new RBTestData(DataFillIn, headers, status); michael@0: if(U_SUCCESS(status)) { michael@0: return result; michael@0: } else { michael@0: delete result; michael@0: } michael@0: } else { michael@0: ures_close(DataFillIn); michael@0: ures_close(headers); michael@0: } michael@0: } else { michael@0: status = U_MISSING_RESOURCE_ERROR; michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: michael@0: michael@0: //Get test data from ResourceBundles michael@0: UResourceBundle* michael@0: RBTestDataModule::getTestBundle(const char* bundleName, UErrorCode &status) michael@0: { michael@0: if(U_SUCCESS(status)) { michael@0: UResourceBundle *testBundle = NULL; michael@0: const char* icu_data = fLog.getTestDataPath(status); michael@0: if (testBundle == NULL) { michael@0: testBundle = ures_openDirect(icu_data, bundleName, &status); michael@0: if (status != U_ZERO_ERROR) { michael@0: fLog.dataerrln(UNICODE_STRING_SIMPLE("Could not load test data from resourcebundle: ") + UnicodeString(bundleName, -1, US_INV)); michael@0: fDataTestValid = FALSE; michael@0: } michael@0: } michael@0: return testBundle; michael@0: } else { michael@0: return NULL; michael@0: } michael@0: } michael@0: