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