Skip to content

Commit

Permalink
refs #6105. Use actual modified date instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Nov 16, 2012
1 parent 459ab10 commit 86fcf7c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
}

//----------------------------------------------------------------------------------------------
Expand Down
33 changes: 32 additions & 1 deletion Code/Mantid/Framework/Geometry/test/IDFObjectTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,7 +66,7 @@ class IDFObjectTest : public CxxTest::TestSuite
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;
Expand All @@ -81,6 +83,35 @@ class IDFObjectTest : public CxxTest::TestSuite
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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -156,9 +156,6 @@ class InstrumentDefinitionParserTest : public CxxTest::TestSuite
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);
Expand Down Expand Up @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions Code/Mantid/Framework/TestHelpers/src/ScopedFileHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 86fcf7c

Please sign in to comment.