| 60 | } |
| 61 | |
| 62 | void TestTempMethods() |
| 63 | { |
| 64 | std::string tmpPath = mitk::IOUtil::GetTempPath(); |
| 65 | CPPUNIT_ASSERT(!tmpPath.empty()); |
| 66 | CPPUNIT_ASSERT(fs::path(tmpPath).has_filename() == true); |
| 67 | |
| 68 | std::ofstream tmpFile; |
| 69 | std::string tmpFilePath = mitk::IOUtil::CreateTemporaryFile(tmpFile); |
| 70 | CPPUNIT_ASSERT(tmpFile && tmpFile.is_open()); |
| 71 | CPPUNIT_ASSERT(tmpFilePath.size() > tmpPath.size()); |
| 72 | CPPUNIT_ASSERT(tmpFilePath.substr(0, tmpPath.size()) == tmpPath); |
| 73 | |
| 74 | tmpFile.close(); |
| 75 | CPPUNIT_ASSERT(std::remove(tmpFilePath.c_str()) == 0); |
| 76 | |
| 77 | std::string programPath = mitk::IOUtil::GetProgramPath(); |
| 78 | CPPUNIT_ASSERT(!programPath.empty()); |
| 79 | std::ofstream tmpFile2; |
| 80 | std::string tmpFilePath2 = mitk::IOUtil::CreateTemporaryFile(tmpFile2, "my-XXXXXX", programPath); |
| 81 | CPPUNIT_ASSERT(tmpFile2 && tmpFile2.is_open()); |
| 82 | CPPUNIT_ASSERT(tmpFilePath2.size() > programPath.size()); |
| 83 | CPPUNIT_ASSERT(tmpFilePath2.substr(0, programPath.size()) == programPath); |
| 84 | tmpFile2.close(); |
| 85 | CPPUNIT_ASSERT(std::remove(tmpFilePath2.c_str()) == 0); |
| 86 | |
| 87 | std::ofstream tmpFile3; |
| 88 | std::string tmpFilePath3 = |
| 89 | mitk::IOUtil::CreateTemporaryFile(tmpFile3, std::ios_base::binary, "my-XXXXXX.TXT", programPath); |
| 90 | CPPUNIT_ASSERT(tmpFile3 && tmpFile3.is_open()); |
| 91 | CPPUNIT_ASSERT(tmpFilePath3.size() > programPath.size()); |
| 92 | CPPUNIT_ASSERT(tmpFilePath3.substr(0, programPath.size()) == programPath); |
| 93 | CPPUNIT_ASSERT(tmpFilePath3.substr(tmpFilePath3.size() - 13, 3) == "my-"); |
| 94 | CPPUNIT_ASSERT(tmpFilePath3.substr(tmpFilePath3.size() - 4) == ".TXT"); |
| 95 | tmpFile3.close(); |
| 96 | CPPUNIT_ASSERT(std::remove(tmpFilePath3.c_str()) == 0); |
| 97 | |
| 98 | std::string tmpFilePath4 = mitk::IOUtil::CreateTemporaryFile(); |
| 99 | std::ofstream file; |
| 100 | file.open(tmpFilePath4.c_str()); |
| 101 | CPPUNIT_ASSERT_MESSAGE("Testing if file exists after CreateTemporaryFile()", file.is_open()); |
| 102 | |
| 103 | CPPUNIT_ASSERT_THROW(mitk::IOUtil::CreateTemporaryFile(tmpFile2, "XX"), mitk::Exception); |
| 104 | |
| 105 | std::string tmpDir = mitk::IOUtil::CreateTemporaryDirectory(); |
| 106 | CPPUNIT_ASSERT(tmpDir.size() > tmpPath.size()); |
| 107 | CPPUNIT_ASSERT(tmpDir.substr(0, tmpPath.size()) == tmpPath); |
| 108 | CPPUNIT_ASSERT(itksys::SystemTools::RemoveADirectory(mitk::Utf8Util::Local8BitToUtf8(tmpDir).c_str())); |
| 109 | |
| 110 | std::string tmpDir2 = mitk::IOUtil::CreateTemporaryDirectory("my-XXXXXX", programPath); |
| 111 | CPPUNIT_ASSERT(tmpDir2.size() > programPath.size()); |
| 112 | CPPUNIT_ASSERT(tmpDir2.substr(0, programPath.size()) == programPath); |
| 113 | CPPUNIT_ASSERT(itksys::SystemTools::RemoveADirectory(mitk::Utf8Util::Local8BitToUtf8(tmpDir2).c_str())); |
| 114 | } |
| 115 | |
| 116 | void TestTempMethodsForUniqueFilenames() |
| 117 | { |