ctkDICOMDatabaseTest3.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QCoreApplication>
  16. #include <QDir>
  17. // ctkDICOMCore includes
  18. #include "ctkDICOMDatabase.h"
  19. // STD includes
  20. #include <iostream>
  21. #include <cstdlib>
  22. int ctkDICOMDatabaseTest3( int argc, char * argv [] )
  23. {
  24. QCoreApplication app(argc, argv);
  25. if (argc <= 1)
  26. {
  27. std::cerr << "Warning, no sql file given. Test stops" << std::endl;
  28. std::cerr << "Usage: ctkDICOMDatabaseTest3 <olddumpfile.sql>" << std::endl;
  29. return EXIT_FAILURE;
  30. }
  31. QDir databaseDirectory = QDir::temp();
  32. databaseDirectory.remove("ctkDICOMDatabase.sql");
  33. QFileInfo databaseFile(databaseDirectory, QString("database.test"));
  34. QString databaseFileName(databaseFile.absoluteFilePath());
  35. std::cerr << "Populating database " << databaseFileName.toStdString() << "\n";
  36. // first, create a database and initialize it with the old schema
  37. try
  38. {
  39. ctkDICOMDatabase myCTK( databaseFileName );
  40. if (!myCTK.initializeDatabase(argv[1]))
  41. {
  42. std::cerr << "Error when initializing the data base with: " << argv[1]
  43. << " error: " << myCTK.lastError().toStdString();
  44. return EXIT_FAILURE;
  45. }
  46. if ( myCTK.schemaVersionLoaded() != QString("") )
  47. {
  48. std::cerr << "Schema tag should be empty in old schema\n";
  49. std::cerr << "Instead we got: (" << myCTK.schemaVersionLoaded().toStdString() << ")\n";
  50. return EXIT_FAILURE;
  51. }
  52. myCTK.closeDatabase();
  53. }
  54. catch (std::exception e)
  55. {
  56. std::cerr << "Error when opening the data base file: " << databaseFileName.toStdString()
  57. << " error: " << e.what();
  58. return EXIT_FAILURE;
  59. }
  60. // now try opening it and updating the schema
  61. try
  62. {
  63. ctkDICOMDatabase myCTK( databaseFileName );
  64. if ( myCTK.schemaVersionLoaded() == myCTK.schemaVersion() )
  65. {
  66. std::cerr << "Schema version should Not match\n";
  67. return EXIT_FAILURE;
  68. }
  69. if ( !myCTK.updateSchema() )
  70. {
  71. std::cerr << "Could not update schema\n";
  72. return EXIT_FAILURE;
  73. }
  74. if ( myCTK.schemaVersionLoaded() != myCTK.schemaVersion() )
  75. {
  76. std::cerr << "Schema version should match\n";
  77. return EXIT_FAILURE;
  78. }
  79. myCTK.closeDatabase();
  80. }
  81. catch (std::exception e)
  82. {
  83. std::cerr << "Error when re-opening the data base file: " << databaseFileName.toStdString()
  84. << " error: " << e.what();
  85. return EXIT_FAILURE;
  86. }
  87. return EXIT_SUCCESS;
  88. }