Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/10626_better_preview_plots_in_indi…
…rect_ui Refs #10626
- Loading branch information
Showing
16 changed files
with
1,164 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/DownloadFile.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#ifndef MANTID_DATAHANDLING_DOWNLOADFILE_H_ | ||
#define MANTID_DATAHANDLING_DOWNLOADFILE_H_ | ||
|
||
#include "MantidKernel/System.h" | ||
#include "MantidAPI/Algorithm.h" | ||
|
||
namespace Mantid | ||
{ | ||
|
||
namespace Kernel | ||
{ | ||
//forward Declaration | ||
class InternetHelper; | ||
} | ||
|
||
namespace DataHandling | ||
{ | ||
|
||
/** DownloadFile : Downloads a file from a url to the file system | ||
Copyright © 2014 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 DLLExport DownloadFile : public API::Algorithm | ||
{ | ||
public: | ||
DownloadFile(); | ||
virtual ~DownloadFile(); | ||
|
||
virtual const std::string name() const; | ||
virtual int version() const; | ||
virtual const std::string category() const; | ||
virtual const std::string summary() const; | ||
|
||
protected: | ||
Kernel::InternetHelper* m_internetHelper; | ||
|
||
private: | ||
void init(); | ||
void exec(); | ||
|
||
|
||
|
||
}; | ||
|
||
|
||
} // namespace DataHandling | ||
} // namespace Mantid | ||
|
||
#endif /* MANTID_DATAHANDLING_DOWNLOADFILE_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include "MantidDataHandling/DownloadFile.h" | ||
#include "MantidKernel/InternetHelper.h" | ||
#include "MantidKernel/ListValidator.h" | ||
#include "MantidKernel/MandatoryValidator.h" | ||
#include "MantidAPI/FileProperty.h" | ||
|
||
#include "Poco/URI.h" | ||
|
||
#include "boost/make_shared.hpp" | ||
#include "boost/algorithm/string/predicate.hpp" | ||
|
||
#include <string> | ||
#include <stdexcept> | ||
|
||
namespace Mantid | ||
{ | ||
namespace DataHandling | ||
{ | ||
|
||
using Mantid::Kernel::Direction; | ||
using Mantid::Kernel::MandatoryValidator; | ||
using Mantid::Kernel::StringListValidator; | ||
using Mantid::API::WorkspaceProperty; | ||
using Mantid::API::FileProperty; | ||
|
||
// Register the algorithm into the AlgorithmFactory | ||
DECLARE_ALGORITHM(DownloadFile) | ||
|
||
|
||
|
||
//---------------------------------------------------------------------------------------------- | ||
/** Constructor | ||
*/ | ||
DownloadFile::DownloadFile():m_internetHelper(new Kernel::InternetHelper()) | ||
{ | ||
} | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/** Destructor | ||
*/ | ||
DownloadFile::~DownloadFile() | ||
{ | ||
delete m_internetHelper; | ||
} | ||
|
||
|
||
//---------------------------------------------------------------------------------------------- | ||
|
||
/// Algorithms name for identification. @see Algorithm::name | ||
const std::string DownloadFile::name() const { return "DownloadFile"; } | ||
|
||
/// Algorithm's version for identification. @see Algorithm::version | ||
int DownloadFile::version() const { return 1;} | ||
|
||
/// Algorithm's category for identification. @see Algorithm::category | ||
const std::string DownloadFile::category() const { return "DataHandling";} | ||
|
||
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary | ||
const std::string DownloadFile::summary() const { return "Downloads a file from a url to the file system";} | ||
|
||
|
||
//---------------------------------------------------------------------------------------------- | ||
/** Initialize the algorithm's properties. | ||
*/ | ||
void DownloadFile::init() | ||
{ | ||
declareProperty("Address", "", boost::make_shared<MandatoryValidator<std::string> >(), | ||
"The address of the network resource to download. This should start http:// or https:// .", Direction::InOut); | ||
declareProperty(new FileProperty("Filename", "", FileProperty::Save), | ||
"The filename to save the download to."); | ||
} | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/** Execute the algorithm. | ||
*/ | ||
void DownloadFile::exec() | ||
{ | ||
std::string address = getProperty("Address"); | ||
if ((!boost::starts_with(address,"http://")) && (!boost::starts_with(address,"https://"))) | ||
{ | ||
address = "http://" + address; | ||
g_log.information("Address must start http:// or https://, http has been assumed to continue: " + address); | ||
} | ||
std::string filename = getProperty("Filename"); | ||
|
||
Poco::URI url(address); | ||
m_internetHelper->downloadFile(url.toString(),filename); | ||
setProperty("Address",address); | ||
} | ||
|
||
|
||
} // namespace DataHandling | ||
} // namespace Mantid |
146 changes: 146 additions & 0 deletions
146
Code/Mantid/Framework/DataHandling/test/DownloadFileTest.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
#ifndef MANTID_DATAHANDLING_DOWNLOADFILETEST_H_ | ||
#define MANTID_DATAHANDLING_DOWNLOADFILETEST_H_ | ||
|
||
#include <cxxtest/TestSuite.h> | ||
|
||
#include "MantidDataHandling/DownloadFile.h" | ||
#include "MantidKernel/InternetHelper.h" | ||
|
||
#include <Poco/TemporaryFile.h> | ||
|
||
|
||
#include <fstream> | ||
#include <sstream> | ||
|
||
using Mantid::Kernel::InternetHelper; | ||
using Mantid::DataHandling::DownloadFile; | ||
using namespace Mantid::Kernel; | ||
using namespace Mantid::API; | ||
|
||
|
||
namespace | ||
{ | ||
/** | ||
* Mock out the internet calls of this helper | ||
*/ | ||
class MockedInternetHelper : public InternetHelper | ||
{ | ||
protected: | ||
virtual int sendHTTPSRequest(const std::string& url, | ||
std::ostream& responseStream, | ||
const StringToStringMap& headers = StringToStringMap()) | ||
{ | ||
UNUSED_ARG(url); | ||
UNUSED_ARG(headers); | ||
responseStream << "HTTPS request succeeded"; | ||
return 200; | ||
} | ||
virtual int sendHTTPRequest(const std::string& url, | ||
std::ostream& responseStream, | ||
const StringToStringMap& headers = StringToStringMap()) | ||
{ | ||
UNUSED_ARG(url); | ||
UNUSED_ARG(headers); | ||
responseStream << "HTTP request succeeded"; | ||
return 200; | ||
} | ||
}; | ||
|
||
/** | ||
* Mock out the internet calls of this algorithm | ||
*/ | ||
class MockedDownloadFile : public DownloadFile | ||
{ | ||
public: | ||
MockedDownloadFile() | ||
{ | ||
delete m_internetHelper; | ||
m_internetHelper = new MockedInternetHelper(); | ||
} | ||
}; | ||
} | ||
|
||
|
||
class DownloadFileTest : 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 DownloadFileTest *createSuite() { return new DownloadFileTest(); } | ||
static void destroySuite( DownloadFileTest *suite ) { delete suite; } | ||
|
||
|
||
void test_Init() | ||
{ | ||
DownloadFile alg; | ||
TS_ASSERT_THROWS_NOTHING( alg.initialize() ) | ||
TS_ASSERT( alg.isInitialized() ) | ||
} | ||
|
||
void test_Bad_Address() | ||
{ | ||
MockedInternetHelper internetHelper; | ||
std::string url = "www.google.com"; | ||
Poco::TemporaryFile tmpFile; | ||
exec_alg(url,tmpFile.path(), "http://" + url); | ||
} | ||
|
||
void exec_alg(std::string address, std::string filename, std::string newAddress = "") | ||
{ | ||
MockedDownloadFile alg; | ||
TS_ASSERT_THROWS_NOTHING( alg.initialize() ) | ||
TS_ASSERT( alg.isInitialized() ) | ||
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Address", address) ); | ||
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Filename", filename) ); | ||
TS_ASSERT_THROWS_NOTHING( alg.execute(); ); | ||
TS_ASSERT( alg.isExecuted() ); | ||
if (newAddress.size() > 0) | ||
{ | ||
TS_ASSERT_EQUALS(newAddress, alg.getPropertyValue("Address") ); | ||
} | ||
} | ||
|
||
void test_DownloadFile_HTTP() | ||
{ | ||
MockedInternetHelper internetHelper; | ||
std::string url = "http://www.google.com"; | ||
Poco::TemporaryFile tmpFile; | ||
exec_alg(url,tmpFile.path()); | ||
TSM_ASSERT("File has not been created.",tmpFile.exists()); | ||
TSM_ASSERT("File is not a file.",tmpFile.isFile()); | ||
std::fstream fs; | ||
TS_ASSERT_THROWS_NOTHING (fs.open (tmpFile.path().c_str(), std::fstream::in )); | ||
|
||
TSM_ASSERT("Cannot open file.",fs.is_open()); | ||
|
||
std::stringstream ss; | ||
ss << fs.rdbuf();//read the file | ||
fs.close(); | ||
|
||
TS_ASSERT_EQUALS ("HTTP request succeeded", ss.str()); | ||
} | ||
|
||
void test_DownloadFile_HTTPS() | ||
{ | ||
MockedInternetHelper internetHelper; | ||
std::string httpsUrl = "https://api.github.com/repos/mantidproject/mantid/contents"; | ||
Poco::TemporaryFile tmpFile; | ||
exec_alg(httpsUrl,tmpFile.path()); | ||
TSM_ASSERT("File has not been created.",tmpFile.exists()); | ||
TSM_ASSERT("File is not a file.",tmpFile.isFile()); | ||
std::fstream fs; | ||
TS_ASSERT_THROWS_NOTHING (fs.open (tmpFile.path().c_str(), std::fstream::in )); | ||
|
||
TSM_ASSERT("Cannot open file.",fs.is_open()); | ||
|
||
std::stringstream ss; | ||
ss << fs.rdbuf();//read the file | ||
fs.close(); | ||
|
||
TS_ASSERT_EQUALS ("HTTPS request succeeded", ss.str()); | ||
} | ||
|
||
}; | ||
|
||
|
||
#endif /* MANTID_DATAHANDLING_DOWNLOADFILETEST_H_ */ |
Oops, something went wrong.