diff --git a/Code/Mantid/Framework/Geometry/src/Instrument/InstrumentDefinitionParser.cpp b/Code/Mantid/Framework/Geometry/src/Instrument/InstrumentDefinitionParser.cpp index fda76c12cb6bc844ee21cf0b97e781670aef51fa..10d20b57a763b8efd27288176a3e8bb78f35a629 100644 --- a/Code/Mantid/Framework/Geometry/src/Instrument/InstrumentDefinitionParser.cpp +++ b/Code/Mantid/Framework/Geometry/src/Instrument/InstrumentDefinitionParser.cpp @@ -30,6 +30,7 @@ #include <Poco/Exception.h> #include <Poco/File.h> #include <Poco/Path.h> +#include <Poco/DateTimeFormatter.h> #include <boost/make_shared.hpp> #include <boost/algorithm/string/replace.hpp> #include <sstream> @@ -164,15 +165,14 @@ namespace Geometry * */ std::string InstrumentDefinitionParser::getMangledName() { - if (!pDoc) - throw std::runtime_error("Call InstrumentDefinitionParser::initialize() before getMangledName."); - std::string lastModified = pRootElem->getAttribute("last-modified"); - if (lastModified.length() == 0) + if(!this->m_xmlFile->exists()) { - g_log.warning() << "The IDF that you are using doesn't contain a 'last-modified' field. "; - g_log.warning() << "You may not get the correct definition file loaded." << std::endl ; + throw std::runtime_error("Call InstrumentDefinitionParser::initialize() before getMangledName."); } - return m_xmlFile->getFileNameOnly() + lastModified; + + auto timeString = Poco::DateTimeFormatter::format(m_xmlFile->getLastModified(), "%Y: %dd %H:%M:%S.%i"); + + return m_xmlFile->getFileNameOnly() + timeString; } //---------------------------------------------------------------------------------------------- diff --git a/Code/Mantid/Framework/Geometry/test/IDFObjectTest.h b/Code/Mantid/Framework/Geometry/test/IDFObjectTest.h index 255359a01754faf7a0bcf5a80a35db56e94afd0e..b495f5c1f8523419d3488920cac1d404725df05f 100644 --- a/Code/Mantid/Framework/Geometry/test/IDFObjectTest.h +++ b/Code/Mantid/Framework/Geometry/test/IDFObjectTest.h @@ -4,7 +4,9 @@ #include <cxxtest/TestSuite.h> #include "MantidKernel/ConfigService.h" #include "MantidGeometry/Instrument/IDFObject.h" +#include "MantidTestHelpers/ScopedFileHelper.h" #include <Poco/Path.h> +#include <Poco/Thread.h> using Mantid::Geometry::IDFObject; using Mantid::Kernel::ConfigService; @@ -64,7 +66,7 @@ public: TS_ASSERT_EQUALS(".xml", obj.getExtension()); } - void testGetLastModified() + void testGetFileNameOnly() { const std::string filenameonly = "IDF_for_UNIT_TESTING.xml"; const std::string filename = ConfigService::Instance().getInstrumentDirectory() + "/IDFs_for_UNIT_TESTING/" + filenameonly; @@ -81,6 +83,35 @@ public: TS_ASSERT_EQUALS(file.getLastModified(), obj.getLastModified()); } + // Test that the last modified date fetched on each platform does actually make sense! + void testGetModifiedTimestampAfterChange() + { + const std::string fileName = "check_last_modified_date.xml"; + const std::string fileContents = "some_idf_contents_that_donesn't_matter"; + // Create a file. + ScopedFileHelper::ScopedFile file(fileContents, fileName); + IDFObject IDF(file.getFileName()); + // Record the modification timestamp + Poco::Timestamp timeOfCreation = IDF.getLastModified(); + // Delay + const int delay = 1; + Poco::Thread::sleep(delay); + // Modify the file. + std::ofstream modIDF; + modIDF.open(IDF.getFileFullPathStr(), std::ios::out | std::ios::app); + if (!modIDF.is_open()) + { + throw std::runtime_error("Cannot run test since file cannot be opened."); + } + modIDF << "\nchange" << std::endl; + modIDF.close(); + // Record the modification timestamp. + Poco::Timestamp timeOfModification = IDF.getLastModified(); + + // Compare the modification dates. + TSM_ASSERT("The file modification dates do not reflect the fact that the file has been modified." , timeOfModification >= (timeOfCreation + delay)); + } + void testGetFileFullPathStr() { const std::string filename = ConfigService::Instance().getInstrumentDirectory() + "/IDFs_for_UNIT_TESTING/IDF_for_UNIT_TESTING.xml"; diff --git a/Code/Mantid/Framework/Geometry/test/InstrumentDefinitionParserTest.h b/Code/Mantid/Framework/Geometry/test/InstrumentDefinitionParserTest.h index 82b3b35c8e96ea6647be6a3b9ce8059b6eb6807a..a36832fb8b2a5948b94940b5d5585b350d6233f1 100644 --- a/Code/Mantid/Framework/Geometry/test/InstrumentDefinitionParserTest.h +++ b/Code/Mantid/Framework/Geometry/test/InstrumentDefinitionParserTest.h @@ -16,7 +16,7 @@ #include "MantidTestHelpers/ScopedFileHelper.h" #include <gmock/gmock.h> -#include "boost/tuple/tuple.hpp" +#include <boost/tuple/tuple.hpp> using namespace Mantid; using namespace Mantid::Kernel; @@ -156,9 +156,6 @@ public: TS_ASSERT_THROWS_NOTHING( parser.initialize(filename, "For Unit Testing", xmlText); ); TS_ASSERT_THROWS_NOTHING( i = parser.parseXML(NULL); ); - // Check the mangled name - TS_ASSERT_EQUALS( parser.getMangledName(), "IDF_for_UNIT_TESTING.xmlHello!"); - // Remove it for clean test try { Poco::File vtpFile(vtpFilename); @@ -762,8 +759,6 @@ void testLoadingAndParsing() TS_ASSERT_THROWS_NOTHING( parser.initialize(filename, "For Unit Testing", xmlText); ); TS_ASSERT_THROWS_NOTHING( i = parser.parseXML(NULL); ); - // Check the mangled name - TS_ASSERT_EQUALS( parser.getMangledName(), "IDF_for_UNIT_TESTING.xmlHello!"); // Remove it for clean test try { diff --git a/Code/Mantid/Framework/TestHelpers/src/ScopedFileHelper.cpp b/Code/Mantid/Framework/TestHelpers/src/ScopedFileHelper.cpp index 75cdd81b294fbc8476a23cfcd8ec8e40dbab9a83..5c7f95f7275790f02bfd28a3afd961c34d7f76a3 100644 --- a/Code/Mantid/Framework/TestHelpers/src/ScopedFileHelper.cpp +++ b/Code/Mantid/Framework/TestHelpers/src/ScopedFileHelper.cpp @@ -71,8 +71,8 @@ namespace ScopedFileHelper } /** - Getter for the filename - @return File name only. + Getter for the filename with path. + @return File name and path. */ std::string ScopedFile::getFileName() const {