00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00013
00014 #include <fstream>
00015 #include <memory>
00016
00017 #include "DatabaseInterface/Dbi.h"
00018 #include "DatabaseInterface/DbiCascader.h"
00019 #include "DatabaseInterface/DbiTableProxyRegistry.h"
00020 #include "DatabaseInterface/DbiValRecSet.h"
00021
00022 #include "DatabaseMaintenance/DbmModule.h"
00023 #include "DatabaseMaintenance/DbmValidate.h"
00024
00025 #include "JobControl/JobCommand.h"
00026 #include "LeakChecker/Lea.h"
00027 #include "MessageService/MsgService.h"
00028
00029 #include "TSQLStatement.h"
00030
00031 ClassImp(DbmValidate)
00032
00033
00034
00035
00036
00037 CVSID("$Id: DbmValidate.cxx,v 1.10 2007/04/26 14:20:43 west Exp $");
00038
00039
00040
00041
00042
00043
00044
00045 DbmValidate::DbmValidate(DbmModule* module) :
00046 fModule(module),
00047 fStatement0(0),
00048 fStatement1(0)
00049 {
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 LEA_CTOR
00073
00074 MSG("Dbi", Msg::kVerbose) << "Creating DbmValidate" << endl;
00075
00076 fStatement0 = DbiTableProxyRegistry::Instance()
00077 .GetCascader()
00078 .CreateStatement(0);
00079 fStatement1 = DbiTableProxyRegistry::Instance()
00080 .GetCascader()
00081 .CreateStatement(1);
00082
00083 if ( ! fStatement0 || ! fStatement1 ) {
00084 MSG("Dbi", Msg::kInfo)
00085 << "Cannot get DbiStatement for cascade entries 0,1 "
00086 << "unable to validate Dbm." << endl;
00087 }
00088 else {
00089 if ( ! PrepareDb() ) {
00090 MSG("Dbi", Msg::kInfo) << "Cannot run validation!" << endl;
00091 }
00092 else {
00093 if ( RunAllTests() ) {
00094 MSG("Dbi", Msg::kInfo) << "All tests passed" << endl;
00095 }
00096 else {
00097 MSG("Dbi", Msg::kInfo) << "Validation failed!" << endl;
00098 }
00099 }
00100 }
00101
00102 }
00103
00104
00105
00106
00107 DbmValidate::~DbmValidate() {
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 LEA_DTOR
00131
00132 MSG("Dbi", Msg::kVerbose) << "Destroying DbmValidate" << endl;
00133
00134 delete fStatement0;
00135 fStatement0 = 0;
00136 delete fStatement1;
00137 fStatement0 = 0;
00138
00139 }
00140
00141
00142
00143
00144 Bool_t DbmValidate::PrepareDb() {
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 Bool_t ok = kTRUE;
00167
00168 if ( ! fStatement0 || ! fStatement1 ) {
00169 MSG("Dbm",Msg::kInfo)
00170 << "Cannot prepare for tests (no database access)" << endl;
00171 return kFALSE;
00172 }
00173
00174 string prepareDBFileName
00175 = fFileLocater.Find("DatabaseMaintenance/test/PrepareDB.mysql");
00176 ifstream prepareDBFile(prepareDBFileName.c_str());
00177
00178 if ( ! prepareDBFile.is_open() ) {
00179 MSG("Dbm",Msg::kInfo)
00180 << "Cannot prepare for tests (no validation data file)" << endl;
00181 return kFALSE;
00182 }
00183
00184
00185
00186 while ( ! prepareDBFile.eof() ) {
00187 string sql;
00188 string line;
00189 while ( ! prepareDBFile.eof() ) {
00190 getline(prepareDBFile,line);
00191
00192 int last = line.size()-1;
00193 while ( last >= 0 && line[last] == ' ') line.erase(last--);
00194
00195 if ( last < 0 || line[0] == '#' ) continue;
00196
00197 sql += line;
00198 if ( line[last] == ';' ) {
00199 MSG("Dbm",Msg::kDebug) << "SQL " << sql << endl;
00200 fStatement0->ExecuteUpdate(sql.c_str());
00201 if ( fStatement0->PrintExceptions() ) ok = kFALSE;
00202 fStatement1->ExecuteUpdate(sql.c_str());
00203 if ( fStatement1->PrintExceptions() ) ok = kFALSE;
00204 sql = "";
00205 break;
00206 }
00207 }
00208 if ( sql != ""
00209 ) MSG("Dbm",Msg::kInfo)
00210 << "File ends with incomplete SQL "<< sql << endl;
00211 }
00212
00213 return ok;
00214
00215 }
00216
00217
00218
00219 Bool_t DbmValidate::RunAllTests() {
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 Bool_t ok = kTRUE;
00241
00242 if ( ! TestGlobaliseSeqNo() ) ok = kFALSE;
00243 if ( ! TestExportImport() ) ok = kFALSE;
00244
00245 return ok;
00246
00247 }
00248
00249
00250 Bool_t DbmValidate::TestExportImport() {
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 MSG("Dbm",Msg::kInfo) << "Testing Export/Import ..." << endl;
00272
00273 Bool_t ok = kTRUE;
00274
00275
00276 if ( ! ok ) {
00277 MSG("Dbm",Msg::kError) << " Cannot prepare test" << endl;
00278 return kFALSE;
00279 }
00280
00281
00282
00283 JobCommand jcExport("/Export --Table DBMVALIDATEDATA1 --File DbmValidateData1.dat");
00284 fModule->HandleCommand(&jcExport);
00285
00286
00287 fModule->ClearCache();
00288 fModule->fValRecSet = new DbiValRecSet("DBMVALIDATEDATA1",1);
00289
00290
00291 JobCommand jcImport("/Import --DatabaseNumber 1 --File DbmValidateData1.dat");
00292 fModule->HandleCommand(&jcImport);
00293
00294 return ok;
00295
00296 }
00297
00298 Bool_t DbmValidate::TestGlobaliseSeqNo() {
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319 MSG("Dbm",Msg::kInfo) << "Testing SeqNo globalisation ..." << endl;
00320
00321 string sqlLocalMain =
00322 "select SeqNo from DBMVALIDATEDATA1 where SeqNo < 100000000;";
00323 string sqlLocalVal =
00324 "select SeqNo from DBMVALIDATEDATA1VLD where SeqNo < 100000000;";
00325
00326 TSQLStatement* stmt = 0;
00327
00328
00329 Int_t numBefore = 0;
00330 stmt = fStatement0->ExecuteQuery(sqlLocalMain.c_str());
00331 fStatement0->PrintExceptions();
00332 if ( stmt ) while ( stmt->NextResultRow() ) ++numBefore;
00333 delete stmt;
00334 stmt = fStatement0->ExecuteQuery(sqlLocalVal.c_str());
00335 fStatement0->PrintExceptions();
00336 if ( stmt ) while ( stmt->NextResultRow() ) ++numBefore;
00337 delete stmt;
00338
00339
00340 JobCommand jc("/GlobaliseSeqNo DBMVALIDATEDATA1");
00341 fModule->HandleCommand(&jc);
00342
00343
00344 Int_t numAfter = 0;
00345 stmt = fStatement0->ExecuteQuery(sqlLocalMain.c_str());
00346 fStatement0->PrintExceptions();
00347 if ( stmt ) while ( stmt->NextResultRow() ) ++numAfter;
00348 delete stmt;
00349 stmt = fStatement0->ExecuteQuery(sqlLocalVal.c_str());
00350 fStatement0->PrintExceptions();
00351 if ( stmt ) while ( stmt->NextResultRow() ) ++numAfter;
00352 delete stmt;
00353
00354 Bool_t ok = numBefore == 9 && numAfter == 0;
00355 MSG("Dbm",Msg::kInfo)
00356 << " Before there should be 9 local Seqno, found "
00357 << numBefore << endl
00358 << " After there should be 0 local Seqno, found "
00359 << numAfter << endl
00360 << "Test has " << (ok ? "passed" : "failed!!") << endl;
00361
00362 return ok;
00363
00364 }
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395