C++ class for reading and writing XML structures. No need for a C++ code parser or special pre compiler. Specify a schema entirly in native C++. The schema is verified when XML is read and exceptions are thrown when the XML to be parse is invalid.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.5 KiB

/*! @file
@id $Id$
*/
// 1 2 3 4 5 6 7 8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
#include <xml-cxx/xml.hxx>
#include <cppunit/TestFixture.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
class A: public xml::Serialize {
public:
int _anInteger;
bool _aBool;
double _aDouble;
std::string _aString;
std::string _anotherString;
unsigned long _aLong;
protected:
void initXmlMembers() {
className("A");
persist(_anInteger, "anInteger");
persist(_aBool, "aBool");
persist(_aDouble, "aDouble");
persist(_aString, "aString");
persist(_anotherString, "anotherString");
persist(_aLong, "aLong");
}
};
class SerializationTest: public CppUnit::TestFixture {
public:
std::string _file;
A _a;
void setUp() {
_a._anInteger = 15;
_a._aBool = true;
_a._aDouble = 123.456;
_a._aString = "Hello World";
_a._anotherString = "This is another Text";
_a._aLong = 4123674622ul;
try {
std::stringstream ss;
_a.saveXml(ss);
_file = ss.str();
} catch (...) {}
}
void memberDeclaration() {
// This is more a compile time test than a runtime test.
A a;
a._anInteger = 15;
CPPUNIT_ASSERT_EQUAL(15, a._anInteger);
a._aString = "Hello World";
CPPUNIT_ASSERT_EQUAL(std::string("Hello World"), a._aString);
std::stringstream ss;
}
void store() {
{
std::stringstream ss;
CPPUNIT_ASSERT_NO_THROW(_a.saveXml(ss));
CPPUNIT_ASSERT_EQUAL(std::string("<A>\n"
"\t<anInteger>15</anInteger>\n"
"\t<aBool>1</aBool>\n"
"\t<aDouble>123.456</aDouble>\n"
"\t<aString>Hello World</aString>\n"
"\t<anotherString>This is"
" another Text</anotherString>\n"
"\t<aLong>4123674622</aLong>\n"
"</A>"),
ss.str());
} { // again - initialisation of A should be done only once
std::stringstream ss;
CPPUNIT_ASSERT_NO_THROW(_a.saveXml(ss));
CPPUNIT_ASSERT_EQUAL(_file, ss.str());
}
}
void restore() {
A a;
std::stringstream ss(_file);
CPPUNIT_ASSERT(_file.size()>0);
CPPUNIT_ASSERT_EQUAL(_file, ss.str());
CPPUNIT_ASSERT_NO_THROW(a.loadXml(ss));
CPPUNIT_ASSERT_EQUAL(15, a._anInteger);
CPPUNIT_ASSERT_EQUAL(true, a._aBool);
CPPUNIT_ASSERT_EQUAL(123.456, a._aDouble);
CPPUNIT_ASSERT_EQUAL(std::string("Hello World"), a._aString);
CPPUNIT_ASSERT_EQUAL(std::string("This is another Text"),
a._anotherString);
CPPUNIT_ASSERT_EQUAL(4123674622ul, a._aLong);
}
CPPUNIT_TEST_SUITE(SerializationTest);
CPPUNIT_TEST(memberDeclaration);
CPPUNIT_TEST(store);
CPPUNIT_TEST(restore);
CPPUNIT_TEST_SUITE_END();
};
CPPUNIT_TEST_SUITE_REGISTRATION(SerializationTest);
int main() try {
CppUnit::TextUi::TestRunner runner;
runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
return runner.run() ? 0 : 1;
} catch (std::exception& e) {
std::cerr<<"***Exception: "<<e.what()<<std::endl;
return 1;
}