Skip to content

Commit

Permalink
refs #4328. Better file checking prior to loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Jan 20, 2012
1 parent 5a28883 commit b5a1dd3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ Add a nexus files on disk
*/
void CreateMDWorkspace::addNexusFileClicked()
{
QStringList fileNames = findFiles("Raw Files (*.nxs)");
QStringList fileNames = findFiles("Nexus files (*.nxs)");

QStringList::iterator it = fileNames.begin();
QStringList::const_iterator end = fileNames.end();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "MantidQtCustomInterfaces/EventNexusFileMemento.h"
#include "MantidAPI/LoadAlgorithmFactory.h"
#include "MantidKernel/Matrix.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/IEventWorkspace.h"
Expand All @@ -21,15 +22,24 @@ namespace MantidQt
{
boost::regex pattern("(NXS)$", boost::regex_constants::icase);

//Fail if wrong extension
if(!boost::regex_search(fileName, pattern))
{
std::string msg = "EventNexusFileMemento:: Unknown File extension on: " + fileName;
throw std::invalid_argument(msg);
}


//Check file exists at given location
if(!checkStillThere())
{
throw std::runtime_error("EventNexusFileMemento:: File doesn't exist");
throw std::invalid_argument("EventNexusFileMemento:: File doesn't exist");
}

//Detailed check of file structure.
IDataFileChecker_sptr alg = LoadAlgorithmFactory::Instance().create("LoadEventNexus");
if(!alg->fileCheck(m_fileName))
{
throw std::invalid_argument("Expecting Event Nexus files. This file type is not recognised");
}

std::vector<std::string> strs;
Expand Down Expand Up @@ -86,7 +96,7 @@ namespace MantidQt
{
checkStillThere();

IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("LoadEventNexus");
IDataFileChecker_sptr alg = LoadAlgorithmFactory::Instance().create("LoadEventNexus");
alg->initialize();
alg->setRethrows(true);
alg->setProperty("Filename", m_fileName);
Expand Down
7 changes: 5 additions & 2 deletions Code/Mantid/MantidQt/CustomInterfaces/src/RawFileMemento.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "MantidKernel/Matrix.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/LoadAlgorithmFactory.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include <iostream>
#include <fstream>
Expand All @@ -21,15 +22,17 @@ namespace MantidQt
{
boost::regex pattern("(NXS)$", boost::regex_constants::icase);

//Fail if the file extension is wrong.
if(!boost::regex_search(fileName, pattern))
{
std::string msg = "NexusFileMemento:: Unknown File extension on: " + fileName;
throw std::invalid_argument(msg);
}

//Fail if there is no file at the given location
if(!checkStillThere())
{
throw std::runtime_error("NexusFileMemento:: File doesn't exist");
throw std::invalid_argument("NexusFileMemento:: File doesn't exist");
}

std::vector<std::string> strs;
Expand Down Expand Up @@ -93,7 +96,7 @@ namespace MantidQt
alg->setPropertyValue("OutputWorkspace", m_adsID);
if(protocol == MinimalData)
{
alg->setProperty("SpectrumMin", 1);
alg->setProperty("SpectrumMin", 0);
alg->setProperty("SpectrumMax", 1);
}
alg->execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class EventNexusFileMementoTest : public CxxTest::TestSuite
return Mantid::API::FileFinder::Instance().getFullPath("CNCS_7860_event.nxs");
}

static std::string getUnSuitableFileNamePath()
{
return Mantid::API::FileFinder::Instance().getFullPath("MDEW_4D.nxs");
}

public:

void testConstructorThrowsWithWrongExtension()
Expand All @@ -36,7 +41,12 @@ class EventNexusFileMementoTest : public CxxTest::TestSuite

void testConstructThrowsWhenFileDoesntExist()
{
TSM_ASSERT_THROWS("Unknown file, should throw.", new EventNexusFileMemento("MadeUp.nxs"), std::runtime_error);
TSM_ASSERT_THROWS("Unknown file, should throw.", new EventNexusFileMemento("MadeUp.nxs"), std::invalid_argument);
}

void testConstructThrowsOnInvalidFile()
{
TSM_ASSERT_THROWS("Unknown file structure, should throw.", new EventNexusFileMemento(getUnSuitableFileNamePath()), std::invalid_argument);
}

void testFetchItSucceedsWhenFileExists()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class RawFileMementoTest : public CxxTest::TestSuite

void testConstructThrowsWhenFileDoesntExist()
{
TSM_ASSERT_THROWS("Unknown file, should throw.", new RawFileMemento("MadeUp.nxs"), std::runtime_error);
TSM_ASSERT_THROWS("Unknown file, should throw.", new RawFileMemento("MadeUp.nxs"), std::invalid_argument);
}

void testFetchItSucceedsWhenFileExists()
Expand Down

0 comments on commit b5a1dd3

Please sign in to comment.