Skip to content

Commit

Permalink
IDF parser is back to naming vtp files after filenames... Refs #5949
Browse files Browse the repository at this point in the history
not the instrument name.
  • Loading branch information
martyngigg committed Oct 12, 2012
1 parent 6301f8a commit 2f5a74e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ namespace Geometry
class DLLExport IDFObject
{
public:
static const std::string expectedExtension();

IDFObject(const std::string& fileName);
Poco::Path getParentDirectory() const;
Poco::Path getFileFullPath() const;
std::string getFileNameOnly() const;
std::string getExtension() const;
virtual Poco::Timestamp getLastModified() const;
virtual bool exists() const;
virtual ~IDFObject();
Expand All @@ -62,4 +65,4 @@ namespace Geometry
} // namespace Geometry
} // namespace Mantid

#endif /* MANTID_GEOMETRY_IDFOBJECT_H_ */
#endif /* MANTID_GEOMETRY_IDFOBJECT_H_ */
23 changes: 21 additions & 2 deletions Code/Mantid/Framework/Geometry/src/Instrument/IDFObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ namespace Mantid
{
namespace Geometry
{

//----------------------------------------------------------------------------------------------
/**
* Returns the expected extension of an IDF file
* @returns A string containing the expected extension of an IDF file, including the leading period (.)
*/
const std::string IDFObject::expectedExtension()
{
return ".xml";
}

//----------------------------------------------------------------------------------------------
/** Constructor
Expand Down Expand Up @@ -47,6 +55,17 @@ namespace Mantid
return Poco::Path(m_defFile.path()).getFileName();
}

/**
* Gets the extension of this IDF file, including the leading period
* @return A string containing the extension for this file
*/
std::string IDFObject::getExtension() const
{
std::string ext = Poco::Path(m_defFile.path()).getExtension();
if(ext.empty()) return ext;
else return "." + ext;
}

/**
Gets the last modified timestamp of the file.
@return last modified timestamp.
Expand All @@ -69,4 +88,4 @@ namespace Mantid


} // namespace Geometry
} // namespace Mantid
} // namespace Mantid
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <Poco/File.h>
#include <Poco/Path.h>
#include <boost/make_shared.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <sstream>
#include <cstdlib>

Expand Down Expand Up @@ -88,7 +89,19 @@ namespace Geometry
void InstrumentDefinitionParser::initialize(const std::string & filename, const std::string & instName, const std::string & xmlText)
{
IDFObject_const_sptr xmlFile = boost::make_shared<const IDFObject>(filename);
IDFObject_const_sptr vtpFile = boost::make_shared<const IDFObject>(xmlFile->getParentDirectory().toString() + instName + ".vtp");
// Use the filename to construct the cachefile name so that there is a 1:1 map between a definition file & cache
std::string idfExt = xmlFile->getExtension();
std::string vtpFilename = filename;
static const char * vtpExt = ".vtp";
if(idfExt.empty())
{
vtpFilename += vtpExt;
}
else
{
boost::replace_last(vtpFilename, idfExt, vtpExt);
}
IDFObject_const_sptr vtpFile = boost::make_shared<const IDFObject>(vtpFilename);

this->initialize(xmlFile, vtpFile, instName, xmlText);
}
Expand Down
14 changes: 13 additions & 1 deletion Code/Mantid/Framework/Geometry/test/IDFObjectTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class IDFObjectTest : public CxxTest::TestSuite
static IDFObjectTest *createSuite() { return new IDFObjectTest(); }
static void destroySuite( IDFObjectTest *suite ) { delete suite; }

void testExpectedExtensionIsXML()
{
TS_ASSERT_EQUALS(".xml", IDFObject::expectedExtension());
}

void testExists()
{
const std::string filename = ConfigService::Instance().getInstrumentDirectory() + "/IDFs_for_UNIT_TESTING/IDF_for_UNIT_TESTING.xml";
Expand Down Expand Up @@ -52,6 +57,13 @@ class IDFObjectTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(Poco::Path(filename).toString(), obj.getFileFullPath().toString());
}

void testGetExtension()
{
const std::string filename = ConfigService::Instance().getInstrumentDirectory() + "/IDFs_for_UNIT_TESTING/IDF_for_UNIT_TESTING.xml";
IDFObject obj(filename);
TS_ASSERT_EQUALS(".xml", obj.getExtension());
}

void testGetLastModified()
{
const std::string filenameonly = "IDF_for_UNIT_TESTING.xml";
Expand All @@ -72,4 +84,4 @@ class IDFObjectTest : public CxxTest::TestSuite
};


#endif /* MANTID_GEOMETRY_IDFOBJECTTEST_H_ */
#endif /* MANTID_GEOMETRY_IDFOBJECTTEST_H_ */
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,36 @@ class InstrumentDefinitionParserTest : public CxxTest::TestSuite

void test_parse_IDF_for_unit_testing() // IDF stands for Instrument Definition File
{
std::string filename = ConfigService::Instance().getInstrumentDirectory() + "/IDFs_for_UNIT_TESTING/IDF_for_UNIT_TESTING.xml";
std::string filenameNoExt = ConfigService::Instance().getInstrumentDirectory() + "/IDFs_for_UNIT_TESTING/IDF_for_UNIT_TESTING";
std::string filename = filenameNoExt + ".xml";
std::string xmlText = Strings::loadFile(filename);
boost::shared_ptr<const Instrument> i;

// Parse the XML
// Parse the XML (remove old vtp file if it exists)
std::string vtpFilename = filenameNoExt + ".vtp";
try
{
Poco::File vtpFile(vtpFilename);
vtpFile.remove();
}
catch(Poco::FileNotFoundException&) {}

InstrumentDefinitionParser parser;
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);
vtpFile.remove();
}
catch(Poco::FileNotFoundException&)
{
TS_FAIL("Cannot find expected .vtp file next to " + filename);
}

boost::shared_ptr<const IObjComponent> source = i->getSource();
TS_ASSERT_EQUALS( source->getName(), "undulator");
Expand Down

0 comments on commit 2f5a74e

Please sign in to comment.