Newer
Older
#ifndef MANTID_CONFIGSERVICETEST_H_
#define MANTID_CONFIGSERVICETEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/Logger.h"
Gigg, Martyn Anthony
committed
#include "Poco/Path.h"
Gigg, Martyn Anthony
committed
#include "Poco/File.h"
Gigg, Martyn Anthony
committed
#include <fstream>
Russell Taylor
committed
using namespace Mantid::Kernel;
class ConfigServiceTest : public CxxTest::TestSuite
Russell Taylor
committed
ConfigService::Instance().updateConfig("MantidTest.properties");
}
void testLogging()
{
//attempt some logging
Logger& log1 = Logger::get("logTest");
TS_ASSERT_THROWS_NOTHING(log1.debug("a debug string"));
TS_ASSERT_THROWS_NOTHING(log1.information("an information string"));
TS_ASSERT_THROWS_NOTHING(log1.warning("a warning string"));
TS_ASSERT_THROWS_NOTHING(log1.error("an error string"));
TS_ASSERT_THROWS_NOTHING(log1.fatal("a fatal string"));
TS_ASSERT_THROWS_NOTHING(
log1.fatal()<<"A fatal message from the stream operators " << 4.5 << std::endl;
log1.error()<<"A error message from the stream operators " << -0.2 << std::endl;
log1.warning()<<"A warning message from the stream operators " << 999.99 << std::endl;
log1.notice()<<"A notice message from the stream operators " << 0.0 << std::endl;
log1.information()<<"A information message from the stream operators " << -999.99 << std::endl;
log1.debug()<<"A debug message from the stream operators " << 5684568 << std::endl;
//checking the level - this should be set to debug in the config file
//therefore this should only return false for debug
TS_ASSERT(log1.is(Logger::PRIO_DEBUG) == false); //debug
TS_ASSERT(log1.is(Logger::PRIO_INFORMATION)); //information
TS_ASSERT(log1.is(Logger::PRIO_WARNING)); //warning
TS_ASSERT(log1.is(Logger::PRIO_ERROR)); //error
TS_ASSERT(log1.is(Logger::PRIO_FATAL)); //fatal
}
void testEnabled()
{
//attempt some logging
Logger& log1 = Logger::get("logTestEnabled");
TS_ASSERT(log1.getEnabled());
TS_ASSERT_THROWS_NOTHING(log1.fatal("a fatal string with enabled=true"));
TS_ASSERT_THROWS_NOTHING(log1.fatal()<<"A fatal message from the stream operators with enabled=true " << 4.5 << std::endl;);
TS_ASSERT_THROWS_NOTHING(log1.setEnabled(false));
TS_ASSERT(!log1.getEnabled());
TS_ASSERT_THROWS_NOTHING(log1.fatal("YOU SHOULD NEVER SEE THIS"));
TS_ASSERT_THROWS_NOTHING(log1.fatal()<<"YOU SHOULD NEVER SEE THIS VIA A STREAM" << std::endl;);
TS_ASSERT_THROWS_NOTHING(log1.setEnabled(true));
TS_ASSERT(log1.getEnabled());
TS_ASSERT_THROWS_NOTHING(log1.fatal("you are allowed to see this"));
TS_ASSERT_THROWS_NOTHING(log1.fatal()<<"you are allowed to see this via a stream" << std::endl;);
}
void testChangeName()
{
//attempt some logging
Logger& log1 = Logger::get("logTestName1");
TS_ASSERT_THROWS_NOTHING(log1.error("This should be from logTestName1"));
TS_ASSERT_THROWS_NOTHING(log1.error()<<"This should be from logTestName1 via a stream" << std::endl;);
TS_ASSERT_THROWS_NOTHING(log1.setName("logTestName2"));
TS_ASSERT_THROWS_NOTHING(log1.error("This should be from logTestName2"));
TS_ASSERT_THROWS_NOTHING(log1.error()<<"This should be from logTestName2 via a stream" << std::endl;);
TS_ASSERT_THROWS_NOTHING(log1.setName("logTestName1"));
TS_ASSERT_THROWS_NOTHING(log1.error("This should be from logTestName1"));
TS_ASSERT_THROWS_NOTHING(log1.error()<<"This should be from logTestName1 via a stream" << std::endl;);
}
void TestSystemValues()
{
//we cannot test the return values here as they will differ based on the environment.
//therfore we will just check they return a non empty string.
std::string osName = ConfigService::Instance().getOSName();
TS_ASSERT_LESS_THAN(0, osName.length()); //check that the string is not empty
std::string osArch = ConfigService::Instance().getOSArchitecture();
TS_ASSERT_LESS_THAN(0, osArch.length()); //check that the string is not empty
std::string osCompName = ConfigService::Instance().getComputerName();
TS_ASSERT_LESS_THAN(0, osCompName.length()); //check that the string is not empty
TS_ASSERT_LESS_THAN(0, ConfigService::Instance().getOSVersion().length()); //check that the string is not empty
TS_ASSERT_LESS_THAN(0, ConfigService::Instance().getCurrentDir().length()); //check that the string is not empty
// TS_ASSERT_LESS_THAN(0, ConfigService::Instance().getHomeDir().length()); //check that the string is not empty
TS_ASSERT_LESS_THAN(0, ConfigService::Instance().getTempDir().length()); //check that the string is not empty
}
void TestCustomProperty()
{
//Mantid.legs is defined in the properties script as 6
std::string legCountString = ConfigService::Instance().getString("mantid.legs");
TS_ASSERT_EQUALS(legCountString, "6");
}
void TestCustomPropertyAsValue()
{
//Mantid.legs is defined in the properties script as 6
int retVal = ConfigService::Instance().getValue("mantid.legs",value);
retVal = ConfigService::Instance().getValue("mantid.legs",dblValue);
TS_ASSERT_EQUALS(value, 6);
TS_ASSERT_EQUALS(dblValue, 6.0);
}
void TestMissingProperty()
{
//Mantid.noses is not defined in the properties script
std::string noseCountString = ConfigService::Instance().getString("mantid.noses");
//this should return an empty string
TS_ASSERT_EQUALS(noseCountString, "");
Gigg, Martyn Anthony
committed
void TestRelativeToAbsolute()
{
std::string instrumentPath = ConfigService::Instance().getString("instrumentDefinition.directory");
TS_ASSERT( Poco::Path(instrumentPath).isAbsolute() );
}
void TestAppendProperties()
{
//This should clear out all old properties
Russell Taylor
committed
ConfigService::Instance().updateConfig("MantidTest.properties");
//this should return an empty string
TS_ASSERT_EQUALS(ConfigService::Instance().getString("mantid.noses"), "");
//this should pass
TS_ASSERT_EQUALS(ConfigService::Instance().getString("mantid.legs"), "6");
TS_ASSERT_EQUALS(ConfigService::Instance().getString("mantid.thorax"), "1");
//This should append a new properties file properties
Russell Taylor
committed
ConfigService::Instance().updateConfig("MantidTest.user.properties",true);
//this should now be valid
TS_ASSERT_EQUALS(ConfigService::Instance().getString("mantid.noses"), "5");
//this should have been overridden
TS_ASSERT_EQUALS(ConfigService::Instance().getString("mantid.legs"), "76");
//this should have been left alone
TS_ASSERT_EQUALS(ConfigService::Instance().getString("mantid.thorax"), "1");
//This should clear out all old properties
Russell Taylor
committed
ConfigService::Instance().updateConfig("MantidTest.properties");
//this should return an empty string
TS_ASSERT_EQUALS(ConfigService::Instance().getString("mantid.noses"), "");
//this should pass
TS_ASSERT_EQUALS(ConfigService::Instance().getString("mantid.legs"), "6");
TS_ASSERT_EQUALS(ConfigService::Instance().getString("mantid.thorax"), "1");
}
Russell Taylor
committed
Gigg, Martyn Anthony
committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
void testInstrumentPrefixes()
{
//Test properties file contains a facility and some prefixes
const std::string test_facility("NewThing");
ConfigServiceImpl& settings = ConfigService::Instance();
std::vector<std::string> prefs;
TS_ASSERT_THROWS_NOTHING( prefs = settings.getInstrumentPrefixes(test_facility) );
TS_ASSERT_EQUALS(prefs.size(), 2);
if( prefs.size() == 2 )
{
TS_ASSERT_EQUALS(prefs[0], "ABC");
TS_ASSERT_EQUALS(prefs[1], "DEF");
}
TS_ASSERT_THROWS(prefs = settings.getInstrumentPrefixes(test_facility + "IsRubbish"),std::runtime_error );
}
void testSaveConfigCleanFile()
{
const std::string filename("user.settings");
ConfigServiceImpl& settings = ConfigService::Instance();
TS_ASSERT_THROWS_NOTHING(settings.saveConfig(filename));
Poco::File prop_file(filename);
// No changes yet, so no file
TS_ASSERT_EQUALS(prop_file.exists(), false);
runSaveTest(filename);
}
void testSaveConfigExistingSettings()
{
const std::string filename("user.settings");
ConfigServiceImpl& settings = ConfigService::Instance();
std::ofstream writer(filename.c_str(),std::ios_base::trunc);
writer << "mantid.legs = 6";
writer.close();
runSaveTest(filename);
}
private:
void runSaveTest(const std::string& filename)
{
ConfigServiceImpl& settings = ConfigService::Instance();
// Make a change and save again
std::string key("mantid.legs");
std::string value("10");
TS_ASSERT_THROWS_NOTHING(settings.setString(key, value));
TS_ASSERT_THROWS_NOTHING(settings.saveConfig(filename));
// Should exist
Poco::File prop_file(filename);
TS_ASSERT_EQUALS(prop_file.exists(), true);
// Test the entry
std::ifstream reader(filename.c_str(), std::ios::in);
if( reader.bad() )
{
Gigg, Martyn Anthony
committed
TS_FAIL("Unable to open config file for saving");
Gigg, Martyn Anthony
committed
}
std::string line("");
while(std::getline(reader, line))
{
if( line.empty() ) continue;
else break;
}
Gigg, Martyn Anthony
committed
reader.close();
Gigg, Martyn Anthony
committed
std::string key_value = key + "=" + value;
TS_ASSERT_EQUALS(line, key_value);
// Clean up
prop_file.remove();
}