Add JSONConfigurationTest

This commit is contained in:
Franky Braem 2012-07-04 20:47:59 +00:00
parent 9bdc08c9f0
commit c22c78c5ff
3 changed files with 191 additions and 0 deletions

View File

@ -42,6 +42,7 @@
#include "XMLConfigurationTest.h"
#include "FilesystemConfigurationTest.h"
#include "LoggingConfiguratorTest.h"
#include "JSONConfigurationTest.h"
CppUnit::Test* ConfigurationTestSuite::suite()
@ -58,6 +59,7 @@ CppUnit::Test* ConfigurationTestSuite::suite()
pSuite->addTest(XMLConfigurationTest::suite());
pSuite->addTest(FilesystemConfigurationTest::suite());
pSuite->addTest(LoggingConfiguratorTest::suite());
pSuite->addTest(JSONConfigurationTest::suite());
return pSuite;
}

View File

@ -0,0 +1,128 @@
//
// JSONConfigurationTest.cpp
//
// $Id$
//
// Copyright (c) 2004-2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "JSONConfigurationTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Util/JSONConfiguration.h"
#include "Poco/JSON/JSONException.h"
using Poco::Util::JSONConfiguration;
using Poco::Util::AbstractConfiguration;
using Poco::AutoPtr;
using Poco::NotImplementedException;
using Poco::NotFoundException;
using Poco::JSON::JSONException;
JSONConfigurationTest::JSONConfigurationTest(const std::string& name) : AbstractConfigurationTest(name)
{
}
JSONConfigurationTest::~JSONConfigurationTest()
{
}
void JSONConfigurationTest::testLoad()
{
JSONConfiguration config;
std::string json = "{ \"config\" : "
" { \"prop1\" : \"value1\", "
" \"prop2\" : 10, "
" \"prop3\" : [ \"element1\", \"element2\" ], "
" \"prop4\" : { \"prop5\" : false, "
" \"prop6\" : null } "
" }"
"}";
std::istringstream iss(json);
try
{
config.load(iss);
}
catch(JSONException jsone)
{
std::cout << jsone.message() << std::endl;
assert(false);
}
std::string property1 = config.getString("config.prop1");
assert(property1.compare("value1") == 0);
int property2 = config.getInt("config.prop2");
assert(property2 == 10);
int nonExistingProperty = config.getInt("config.prop7", 5);
assert(nonExistingProperty == 5);
std::string arrProperty = config.getString("config.prop3[1]");
assert(arrProperty.compare("element2") == 0);
bool property35 = config.getBool("config.prop4.prop5");
assert(! property35);
try
{
config.getString("propertyUnknown");
assert(true);
}
catch(NotFoundException nfe)
{
}
}
AbstractConfiguration* JSONConfigurationTest::allocConfiguration() const
{
return new JSONConfiguration;
}
void JSONConfigurationTest::setUp()
{
}
void JSONConfigurationTest::tearDown()
{
}
CppUnit::Test* JSONConfigurationTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("JSONConfigurationTest");
AbstractConfigurationTest_addTests(pSuite, JSONConfigurationTest);
CppUnit_addTest(pSuite, JSONConfigurationTest, testLoad);
return pSuite;
}

View File

@ -0,0 +1,61 @@
//
// JSONConfigurationTest.h
//
// $Id$
//
// Definition of the JSONConfigurationTest class.
//
// Copyright (c) 2004-2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef JSONConfigurationTest_INCLUDED
#define JSONConfigurationTest_INCLUDED
#include "AbstractConfigurationTest.h"
#include "Poco/Util/Util.h"
class JSONConfigurationTest: public AbstractConfigurationTest
{
public:
JSONConfigurationTest(const std::string& name);
virtual ~JSONConfigurationTest();
void testLoad();
void setUp();
void tearDown();
static CppUnit::Test* suite();
private:
virtual Poco::Util::AbstractConfiguration* allocConfiguration() const;
};
#endif // JSONConfigurationTest_INCLUDED