1
2
3
4
5
6
7 package eu.etaxonomy.cdm.test.integration;
8
9 import java.sql.ResultSet;
10 import java.sql.SQLException;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15
16 import org.apache.log4j.Logger;
17 import org.springframework.transaction.TransactionStatus;
18
19 import eu.etaxonomy.cdm.api.application.CdmApplicationController;
20 import eu.etaxonomy.cdm.common.AccountStore;
21 import eu.etaxonomy.cdm.database.CdmDataSource;
22 import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
23 import eu.etaxonomy.cdm.database.DbSchemaValidation;
24 import eu.etaxonomy.cdm.database.ICdmDataSource;
25 import eu.etaxonomy.cdm.io.common.Source;
26 import eu.etaxonomy.cdm.model.agent.AgentBase;
27 import eu.etaxonomy.cdm.model.common.CdmBase;
28 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
29 import eu.etaxonomy.cdm.model.common.LanguageString;
30 import eu.etaxonomy.cdm.model.common.Representation;
31 import eu.etaxonomy.cdm.model.common.TermVocabulary;
32 import eu.etaxonomy.cdm.model.common.init.TermNotFoundException;
33 import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
34 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
35 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
36 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
37
38
39
40
41
42 public class TestCdmDbComparator {
43
44 private static final String sourceDbOne = "cdm_test_jaxb";
45 private static final String sourceDbTwo = "cdm_test_jaxb2";
46
47 private static final ICdmDataSource sourceOne = TestCdmDbComparator.CDM_DB(sourceDbOne);
48 private static final ICdmDataSource sourceTwo = TestCdmDbComparator.CDM_DB(sourceDbTwo);
49
50 private static final String server = "192.168.2.10";
51 private static final String username = "edit";
52
53 public static ICdmDataSource CDM_DB(String dbname) {
54
55 String password = AccountStore.readOrStorePassword(dbname, server, username, null);
56 ICdmDataSource datasource = CdmDataSource.NewMySqlInstance(server, dbname, username, password);
57 return datasource;
58 }
59
60 private final int MAX_ROWS = 60000;
61 private final int MAX_TABLES = 150;
62
63 private static final Logger logger = Logger.getLogger(TestCdmDbComparator.class);
64
65 private static final String[] table_list = {
66
67 "Agent",
68
69
70
71
72
73
74
75
76
77 "Annotation",
78
79
80
81
82
83
84
85
86
87
88 "DefinedTermBase",
89
90
91
92
93
94
95
96 "DescriptionBase",
97
98
99
100
101
102
103
104 "DescriptionElementBase",
105
106
107
108
109
110
111
112
113
114
115 "FeatureNode",
116 "FeatureTree",
117
118
119
120
121
122 "HomotypicalGroup",
123
124
125
126
127
128
129 "LanguageString",
130
131 "Marker",
132 "Media",
133 "MediaRepresentation",
134 "MediaRepresentationPart",
135
136
137
138
139
140 "NameRelationship",
141
142
143 "NomenclaturalStatus",
144
145
146 "OriginalSource",
147
148
149
150 "ReferenceBase",
151
152
153
154
155
156
157
158 "Representation",
159
160
161
162
163
164
165
166
167
168
169 "SpecimenOrObservationBase",
170
171
172
173
174
175
176
177
178
179
180
181
182 "SynonymRelationship",
183
184
185 "TaxonBase",
186
187
188
189
190
191 "TaxonNameBase",
192
193
194
195
196
197
198
199
200 "TaxonRelationship",
201
202
203 "TermVocabulary",
204
205 "TypeDesignationBase",
206
207
208
209 };
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245 private Map<String, List<String>> doLoadDataFromDb(String dbname, Source source) {
246
247 Map<String, List<String>> dbTables = new HashMap<String, List<String>>();
248
249 logger.info("Loading data from DB " + dbname);
250
251 CdmApplicationController appCtr = null;
252
253 try {
254 String password = AccountStore.readOrStorePassword(dbname, server, username, null);
255
256 DbSchemaValidation dbSchemaValidation = DbSchemaValidation.VALIDATE;
257 ICdmDataSource datasource = CdmDataSource.NewMySqlInstance(server, dbname, username, password);
258 appCtr = CdmApplicationController.NewInstance(datasource, dbSchemaValidation, true);
259
260 } catch (DataSourceNotFoundException e) {
261 logger.error("datasource error");
262 } catch (TermNotFoundException e) {
263 logger.error("defined terms not found");
264 }
265
266 TransactionStatus txStatus = appCtr.startTransaction(true);
267
268
269
270 try {
271
272 dbTables = retrieveAllTables(appCtr);
273
274 } catch (Exception e) {
275 logger.error("error setting data");
276 e.printStackTrace();
277 }
278 appCtr.commitTransaction(txStatus);
279 appCtr.close();
280
281 return dbTables;
282
283 }
284
285 private Map<String, List<CdmBase>> doLoadDataFromDb_(String dbname) {
286
287 Map<String, List<CdmBase>> dbTables = new HashMap<String, List<CdmBase>>();
288
289 logger.info("Loading data from DB " + dbname);
290
291 CdmApplicationController appCtr = null;
292
293 try {
294 String password = AccountStore.readOrStorePassword(dbname, server, username, null);
295
296 DbSchemaValidation dbSchemaValidation = DbSchemaValidation.VALIDATE;
297 ICdmDataSource datasource = CdmDataSource.NewMySqlInstance(server, dbname, username, password);
298 appCtr = CdmApplicationController.NewInstance(datasource, dbSchemaValidation, true);
299
300 } catch (DataSourceNotFoundException e) {
301 logger.error("datasource error");
302 } catch (TermNotFoundException e) {
303 logger.error("defined terms not found");
304 }
305
306 TransactionStatus txStatus = appCtr.startTransaction(true);
307
308
309
310 try {
311
312 dbTables = retrieveAllTables_(appCtr);
313
314 } catch (Exception e) {
315 logger.error("error setting data");
316 e.printStackTrace();
317 }
318 appCtr.commitTransaction(txStatus);
319 appCtr.close();
320
321 return dbTables;
322
323 }
324
325 private Map<String, List<String>> doLoadDataFromDb__(String dbname, Source source) {
326
327 Map<String, List<String>> dbTables = new HashMap<String, List<String>>();
328
329 logger.info("Loading data from DB " + dbname);
330
331 try {
332
333 dbTables = retrieveAllTables__(source);
334
335 } catch (Exception e) {
336 logger.error("error setting data");
337 e.printStackTrace();
338 }
339 return dbTables;
340
341 }
342
343 private Map<String, List<CdmBase>> retrieveAllTables_(CdmApplicationController appCtr) {
344
345 Map<String, List<CdmBase>> tables_ = new HashMap<String, List<CdmBase>>(table_list.length);
346
347 List<String> tableRows = new ArrayList<String>(MAX_ROWS);
348
349
350
351 try {
352
353 for (int i = 0; i < table_list.length; i++) {
354
355 logger.debug("Retrieving table '" + table_list[i] + "'");
356 System.out.println("Retrieving table '" + table_list[i] + "'");
357
358 List<CdmBase> rows = new ArrayList<CdmBase>(MAX_ROWS);
359
360 rows = appCtr.getMainService().rows(table_list[i], MAX_ROWS, 0);
361
362 tables_.put(table_list[i], rows);
363
364 }
365
366 } catch (Exception e) {
367 logger.error("error retrieving data");
368 e.printStackTrace();
369 }
370
371 return tables_;
372 }
373
374 private Map<String, List<String>> retrieveAllTables___(Source source) {
375
376 Map<String, List<String>> tables = new HashMap<String, List<String>>(table_list.length);
377 List<String> tableRows = new ArrayList<String>(MAX_ROWS);
378
379 try {
380
381 for (int i = 0; i < table_list.length; i++) {
382
383 List<String> rows = new ArrayList<String>(MAX_ROWS);
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398 tables.put(table_list[i], rows);
399
400 }
401
402 } catch (Exception e) {
403 logger.error("error retrieving data");
404 e.printStackTrace();
405 }
406 return tables;
407 }
408
409 private Map<String, List<String>> retrieveAllTables__(Source source) {
410
411
412
413
414
415
416 Map<String, List<String>> tables = new HashMap<String, List<String>>(table_list.length);
417 List<String> tableRows = new ArrayList<String>(MAX_ROWS);
418
419 try {
420
421 for (int i = 0; i < table_list.length; i++) {
422 String strQuery =
423 " SELECT * FROM " + table_list[i];
424 logger.debug("SQL Statement: " + strQuery);
425
426
427 ResultSet rs = source.getResultSet(strQuery) ;
428 List<String> rows = new ArrayList<String>(MAX_ROWS);
429
430 while (rs.next()) {
431 rows.add(rs.toString());
432 }
433 tables.put(table_list[i], rows);
434 }
435 } catch (SQLException e) {
436 logger.error("SQLException:" + e);
437 }
438 return tables;
439 }
440
441 private Map<String, List<String>> retrieveAllTables(CdmApplicationController appCtr) {
442
443 Map<String, List<String>> tables = new HashMap<String, List<String>>(table_list.length);
444
445 List<String> agentTableContent = new ArrayList<String>(MAX_ROWS);
446 List<? extends AgentBase> agents = appCtr.getAgentService().list(null,MAX_ROWS, 0,null,null);
447 for (AgentBase agent: agents ) {
448
449 agentTableContent.add(agent.toString());
450 }
451 tables.put("agents", agentTableContent);
452
453
454
455 List<String> definedTermBaseTableContent = new ArrayList<String>(MAX_ROWS);
456 List<DefinedTermBase> definedTermBases = appCtr.getTermService().list(null,MAX_ROWS, 0,null,null);
457 for (DefinedTermBase definedTermBase: definedTermBases ) {
458 definedTermBaseTableContent.add(definedTermBase.toString());
459 }
460 tables.put("definedTermBases", definedTermBaseTableContent);
461
462
463
464
465 List<LanguageString> languageStrings = appCtr.getTermService().getAllLanguageStrings(MAX_ROWS, 0);
466
467
468 List<NomenclaturalStatus> nomenclaturalStatus = appCtr.getNameService().getAllNomenclaturalStatus(MAX_ROWS, 0);
469
470 List<ReferenceBase> referenceBases = appCtr.getReferenceService().list(null,MAX_ROWS, 0,null,null);
471 List<Representation> representations = appCtr.getTermService().getAllRepresentations(MAX_ROWS, 0);
472 List<SpecimenOrObservationBase> specimenOrObservationBases = appCtr.getOccurrenceService().list(null,MAX_ROWS, 0,null,null);
473
474
475
476
477 List<TermVocabulary> termVocabularies = appCtr.getVocabularyService().list(null,MAX_ROWS, 0,null,null);
478 List<TypeDesignationBase> typeDesignationBases = appCtr.getNameService().getAllTypeDesignations(MAX_ROWS, 0);
479
480 return tables;
481 }
482
483 private void compareTables(String tableName, List<CdmBase> tablesDbOne, List<CdmBase> tablesDbTwo) {
484
485 int tableOneSize = tablesDbOne.size();
486 int tableTwoSize = tablesDbTwo.size();
487 int tableMinSize = 0;
488 int tableMaxSize = 0;
489
490 if (tableOneSize != tableTwoSize) {
491 logger.warn("Table '" + tableName + "', Rows differ: " + tablesDbOne.size() + ", " + tablesDbTwo.size());
492 tableMinSize = Math.min(tableOneSize, tableTwoSize);
493 tableMaxSize = Math.max(tableOneSize, tableTwoSize);
494 } else {
495 logger.info("Table '" + tableName + "': " + tablesDbOne.size());
496 }
497
498 int different = 0;
499
500 try {
501 for (int i = 0; i < tableMinSize; i++) {
502
503 CdmBase obj1 = tablesDbOne.get(i);
504 CdmBase obj2 = tablesDbTwo.get(i);
505
506
507
508 logger.debug("Row # " + i + ":");
509 if (obj1.equals(obj2) != true) {
510 different++;
511 logger.debug("Table 1 = " + obj1);
512 logger.debug("Table 2 = " + obj2);
513 } else {
514 logger.debug("Entry = " + obj1);
515 }
516 }
517 if (different > 0) {
518 logger.info("# Rows identical: " + (tableMaxSize - different));
519 logger.warn("# Rows different: " + different);
520 }
521 } catch (org.hibernate.LazyInitializationException e){
522 logger.error("LazyInitializationException");
523 }
524 }
525
526 private void doCompareDatabases(Map<String, List<CdmBase>> tablesDbOne, Map<String, List<CdmBase>> tablesDbTwo) {
527
528
529 logger.debug("# Tables in DB 1: " + tablesDbOne.size());
530 logger.debug("# Tables in DB 2: " + tablesDbTwo.size());
531
532 for (String tableName: tablesDbOne.keySet()) {
533
534 logger.info("Comparing table '" + tableName + "'");
535
536
537
538 List<CdmBase> dbOneTableRows = new ArrayList<CdmBase>();
539 List<CdmBase> dbTwoTableRows = new ArrayList<CdmBase>();
540
541 dbOneTableRows = tablesDbOne.get(tableName);
542 dbTwoTableRows = tablesDbTwo.get(tableName);
543
544
545
546
547 int different = 0;
548 int tableSize = dbOneTableRows.size();
549
550 for (int i = 0; i < tableSize; i++) {
551
552
553
554 CdmBase str1 = dbOneTableRows.get(i);
555 CdmBase str2 = dbTwoTableRows.get(i);
556
557 if (str1.equals(str2) != true) {
558
559 different++;
560 logger.debug("Rows differ:");
561 logger.debug("Table 1 Row = " + str1);
562 logger.debug("Table 2 Row = " + str2);
563
564 }
565 i++;
566 }
567 if (different > 0) {
568 logger.info("Compared table '" + tableName + "':");
569 logger.info("# Rows total: " + tableSize);
570 logger.info("# Rows identical: " + (tableSize - different));
571 logger.warn("# Rows different: " + different);
572 }
573 }
574 logger.info("End database comparison");
575 }
576
577
578
579
580
581
582
583
584
585 private void test(){
586
587 CdmApplicationController appCtrOne = null;
588 CdmApplicationController appCtrTwo = null;
589 logger.info("Comparing '" + sourceDbOne + "' and '" + sourceDbTwo + "'");
590
591 try {
592 appCtrOne = CdmApplicationController.NewInstance(sourceOne, DbSchemaValidation.VALIDATE, true);
593 appCtrTwo = CdmApplicationController.NewInstance(sourceTwo, DbSchemaValidation.VALIDATE, true);
594
595 } catch (Exception e) {
596 logger.error("Error creating application controller");
597 e.printStackTrace();
598 System.exit(1);
599 }
600
601 try {
602
603 TransactionStatus txStatOne = appCtrOne.startTransaction(true);
604 TransactionStatus txStatTwo = appCtrTwo.startTransaction(true);
605 for (int i = 0; i < table_list.length; i++) {
606
607 List<CdmBase> rowsDbOne = new ArrayList<CdmBase>(MAX_ROWS);
608 List<CdmBase> rowsDbTwo = new ArrayList<CdmBase>(MAX_ROWS);
609 rowsDbOne = appCtrOne.getMainService().rows(table_list[i], MAX_ROWS, 0);
610 rowsDbTwo = appCtrTwo.getMainService().rows(table_list[i], MAX_ROWS, 0);
611 compareTables(table_list[i], rowsDbOne, rowsDbTwo);
612 }
613 appCtrTwo.commitTransaction(txStatTwo);
614 appCtrOne.commitTransaction(txStatOne);
615
616 appCtrOne.close();
617 appCtrTwo.close();
618 logger.info("End database comparison");
619
620 } catch (Exception e) {
621 logger.error("Error retrieving or comparing data");
622 e.printStackTrace();
623 }
624 }
625
626
627
628
629 public static void main(String[] args) {
630 TestCdmDbComparator diff = new TestCdmDbComparator();
631 diff.test();
632 }
633 }