From 2ddf42785b3a1627f0e37bd6a0d21dc98635bfa3 Mon Sep 17 00:00:00 2001 From: Hans Johnson Date: Thu, 4 Aug 2022 12:53:57 -0500 Subject: [PATCH] BUG: A temporary work-around for HDF5 testing The HDF5ImageIO objects are single use for writing. After the initial use, the WriteImageInformation function is short circuited, and can not be reset for writing a second image. This bug was exposed while fixing a problem with the not throwing an exception when attempting to write to a path that does not exist. This temporary work around in the test framework allows fixing the initial problem without requiring fixing the HDF5ImageIO re-use issues. NOTE: Resetting the HDF5ImageIO object at the end of writing fixes the re-use problem for non-streaming case, but that causes the streaming tests to fail. --- Modules/IO/HDF5/src/itkHDF5ImageIO.cxx | 3 +++ Modules/IO/ImageBase/include/itkIOTestHelper.h | 14 +++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Modules/IO/HDF5/src/itkHDF5ImageIO.cxx b/Modules/IO/HDF5/src/itkHDF5ImageIO.cxx index c8747c362cb..f7a59b47da3 100644 --- a/Modules/IO/HDF5/src/itkHDF5ImageIO.cxx +++ b/Modules/IO/HDF5/src/itkHDF5ImageIO.cxx @@ -1320,6 +1320,9 @@ HDF5ImageIO ::Write(const void * buffer) itkExceptionMacro(<< "Unspecified error occured during Write: " << this->GetFileName() << " with " << this->GetNameOfClass()); } + // TODO: including this line allows the IO object to be re-used multiple times for writing, but + // but causes the streaming tests for HDF5ImageIO to fail. + // this->ResetToInitialState(); } // diff --git a/Modules/IO/ImageBase/include/itkIOTestHelper.h b/Modules/IO/ImageBase/include/itkIOTestHelper.h index 57869e39cfa..03e4f599238 100644 --- a/Modules/IO/ImageBase/include/itkIOTestHelper.h +++ b/Modules/IO/ImageBase/include/itkIOTestHelper.h @@ -79,12 +79,13 @@ class IOTestHelper const std::string & filename, typename ImageIOType::Pointer imageio = nullptr) { - if (imageio.IsNull()) - { - imageio = ImageIOType::New(); - } + const bool create_local_io_object{ imageio.IsNull() }; using WriterType = itk::ImageFileWriter; { // Test valid filename writing + if (create_local_io_object) + { + imageio = ImageIOType::New(); + } auto writer = WriterType::New(); writer->SetImageIO(imageio); writer->SetFileName(filename); @@ -100,7 +101,10 @@ class IOTestHelper } } - { // Test if writing to an invalid location causes exception to be thrown: + { // Test if writing to an invalid location causes exception to be thrown: + imageio = imageio->Clone(); // A new io object is needed because the HDF5 io object is single use. A new IO + // object is needed to re-intialize the internal state. + const std::string bad_root_path{ "/a_blatantly_obvious/bad_file_path/that/should/never/exist/on/the/computer/" }; const std::string bad_filename{ bad_root_path + filename }; bool exception_correctly_caught = false;