Skip to content

Commit

Permalink
Add basic code for FileLoaderRegistry. Refs #7263
Browse files Browse the repository at this point in the history
Currently based on string names.
  • Loading branch information
martyngigg committed Jun 27, 2013
1 parent b69909f commit 886d0b0
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set ( SRC_FILES
src/Expression.cpp
src/FermiChopperModel.cpp
src/FileFinder.cpp
src/FileLoaderRegistry.cpp
src/FileProperty.cpp
src/FrameworkManager.cpp
src/FuncMinimizerFactory.cpp
Expand Down Expand Up @@ -152,6 +153,7 @@ set ( INC_FILES
inc/MantidAPI/Expression.h
inc/MantidAPI/FermiChopperModel.h
inc/MantidAPI/FileFinder.h
inc/MantidAPI/FileLoaderRegistry.h
inc/MantidAPI/FileProperty.h
inc/MantidAPI/FrameworkManager.h
inc/MantidAPI/FuncMinimizerFactory.h
Expand Down Expand Up @@ -282,6 +284,7 @@ set ( TEST_FILES
ExpressionTest.h
FermiChopperModelTest.h
FileFinderTest.h
FileLoaderRegistryTest.h
FilePropertyTest.h
FrameworkManagerTest.h
FuncMinimizerFactoryTest.h
Expand Down
58 changes: 58 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/FileLoaderRegistry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifndef MANTID_API_FILELOADERREGISTRY_H_
#define MANTID_API_FILELOADERREGISTRY_H_

#include "MantidAPI/DllConfig.h"

#include <set>
#include <string>

namespace Mantid
{
namespace API
{

/**
Copyright &copy; 2013 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class MANTID_API_DLL FileLoaderRegistry
{
public:
/// Default constructor
FileLoaderRegistry();

/// @returns the number of entries in the registry
inline size_t size() const { return m_names.size(); }
/// Adds an entry
void subscribe(const std::string & name);
/// Pick the best loader for the given filename
std::string findLoader(const std::string & filename) const;

private:
/// The registered names
std::set<std::string> m_names;
};

} // namespace API
} // namespace Mantid

#endif /* MANTID_API_FILELOADERREGISTRY_H_ */
53 changes: 53 additions & 0 deletions Code/Mantid/Framework/API/src/FileLoaderRegistry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "MantidAPI/FileLoaderRegistry.h"
#include "MantidKernel/Exception.h"

#include <Poco/File.h>

namespace Mantid
{
namespace API
{
//----------------------------------------------------------------------------------------------
// Public members
//----------------------------------------------------------------------------------------------
/**
* Creates an empty registry
*/
FileLoaderRegistry::FileLoaderRegistry()
{
}

/**
* @param name The string name of the entry
* @throws std::invalid_argument if an entry with this name already exists
*/
void FileLoaderRegistry::subscribe(const std::string & name)
{
auto insertionResult = m_names.insert(name);
if(!insertionResult.second)
{
throw std::invalid_argument("FileLoaderRegistry::subscribe - Cannot subscribe '"
+ name + "'. An entry with that name already exists");
}
}

/**
* Attempts to pick the best suited loader for the given file name from those in the registry
* @param filename A string that should point to an existing file
* @throws std::invalid_argument if the filename does not point to an existing file or an empty string is given
* @throws Exception::NotFoundError if a loader could not be found
*/
std::string FileLoaderRegistry::findLoader(const std::string & filename) const
{
if(filename.empty() || !Poco::File(filename).exists())
{
throw std::invalid_argument("FileLoaderRegistry::chooserLoader - Cannot open file '" + filename + "'");
}
}

//----------------------------------------------------------------------------------------------
// Private members
//----------------------------------------------------------------------------------------------

} // namespace API
} // namespace Mantid
53 changes: 53 additions & 0 deletions Code/Mantid/Framework/API/test/FileLoaderRegistryTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#ifndef MANTID_API_FILELOADERREGISTRYTEST_H_
#define MANTID_API_FILELOADERREGISTRYTEST_H_

#include <cxxtest/TestSuite.h>
#include "MantidAPI/FileLoaderRegistry.h"

using Mantid::API::FileLoaderRegistry;

class FileLoaderRegistryTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static FileLoaderRegistryTest *createSuite() { return new FileLoaderRegistryTest(); }
static void destroySuite( FileLoaderRegistryTest *suite ) { delete suite; }

void test_Construction_Gives_Empty_Registry()
{
FileLoaderRegistry registry;

TS_ASSERT_EQUALS(0, registry.size());
}

void test_Subscribing_Entry_That_Does_Not_Exist_Increases_Size_By_One()
{
FileLoaderRegistry registry;

TS_ASSERT_THROWS_NOTHING(registry.subscribe("LoadEventNexus"));
TS_ASSERT_EQUALS(1, registry.size());
}

// ======================== Failure cases ===================================
void test_Adding_Entry_That_Already_Exists_Throws_Error_And_Keeps_The_Size_The_Same()
{
FileLoaderRegistry registry;
registry.subscribe("LoadEventNexus");

TS_ASSERT_THROWS(registry.subscribe("LoadEventNexus"), std::invalid_argument);
TS_ASSERT_EQUALS(1, registry.size());
}

void test_Finding_A_Loader_Throws_Invalid_Argument_If_Filename_Does_Not_Point_To_Valid_File()
{
FileLoaderRegistry registry;

TS_ASSERT_THROWS(registry.findLoader(""), std::invalid_argument);
TS_ASSERT_THROWS(registry.findLoader("__notafile.txt__"), std::invalid_argument);
}

};


#endif /* MANTID_API_FILELOADERREGISTRYTEST_H_ */

0 comments on commit 886d0b0

Please sign in to comment.