parent
a9ad45ad4d
commit
37fd5e8695
5 changed files with 466 additions and 142 deletions
@ -0,0 +1,92 @@ |
||||
/*! @file
|
||||
|
||||
@id $Id$ |
||||
*/ |
||||
// 1 2 3 4 5 6 7 8
|
||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
|
||||
// g++ -I../../src ../../src/xml.cxx serialization.cxx
|
||||
|
||||
#include <xml-cxx/xml.hxx> |
||||
#include <iostream> |
||||
#include <sstream> |
||||
|
||||
class B: public xml::Serialize { |
||||
public: |
||||
int a; |
||||
std::string txt; |
||||
protected: |
||||
void initXmlMembers() { |
||||
className("b"); |
||||
persist(a, "a"); |
||||
persist(txt, "txt"); |
||||
} |
||||
}; |
||||
|
||||
class B: public xml::Serialize { |
||||
public: |
||||
int a; |
||||
std::string txt; |
||||
protected: |
||||
void initXmlMembers() { |
||||
className("b"); |
||||
persist(a, "a"); |
||||
persist(txt, "txt"); |
||||
} |
||||
}; |
||||
|
||||
//! Class with external xml::Serialize
|
||||
class C { |
||||
public: |
||||
int a; |
||||
std::string txt; |
||||
}; |
||||
|
||||
int main(int, char**) { |
||||
{ // Serialization as a member
|
||||
std::stringstream ss("<a>\n" |
||||
"\t<a>1234</a>\n" |
||||
"\t<txt>Dies ist ein Serialisierungs-Test</txt>\n" |
||||
"</a>"); |
||||
A a; |
||||
a._ser.loadXml(ss); |
||||
if (a.a==1234) a.a=4321; |
||||
a._ser.saveXml(std::cout)<<std::endl; |
||||
} { // Inherited Serialization
|
||||
std::stringstream ss("<b>\n" |
||||
"\t<a>1234</a>\n" |
||||
"\t<txt>Dies ist ein Serialisierungs-Test</txt>\n" |
||||
"</b>"); |
||||
B b; |
||||
b.loadXml(ss); |
||||
if (b.a==1234) b.a=4321; |
||||
b.saveXml(std::cout)<<std::endl; |
||||
} { // External xml::Serialize: "ser" must live in no longer than "c"!
|
||||
std::stringstream ss("<c>\n" |
||||
"\t<a>1234</a>\n" |
||||
"\t<txt>Dies ist ein Serialisierungs-Test</txt>\n" |
||||
"</c>"); |
||||
C c; |
||||
xml::Serialize ser(xml::Serialize("c") |
||||
.persist(c.a, "a") |
||||
.persist(c.txt, "txt")); |
||||
ser.loadXml(ss); |
||||
if (c.a==1234) c.a=4321; |
||||
ser.saveXml(std::cout)<<std::endl; |
||||
} { // Use xml::Serialize to store anything, e.g. local variables
|
||||
// Important: "ser" must live in no longer than the variables!
|
||||
std::stringstream ss("<d>\n" |
||||
"\t<a>1234</a>\n" |
||||
"\t<txt>Dies ist ein Serialisierungs-Test</txt>\n" |
||||
"</d>"); |
||||
int a; |
||||
std::string txt; |
||||
xml::Serialize ser(xml::Serialize("d") |
||||
.persist(a, "a") |
||||
.persist(txt, "txt")); |
||||
ser.loadXml(ss); |
||||
if (a==1234) a=4321; |
||||
ser.saveXml(std::cout)<<std::endl; |
||||
} |
||||
return 0; |
||||
} |
@ -0,0 +1,109 @@ |
||||
/*! @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::Serialization { |
||||
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 = 4123674622; |
||||
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; |
||||
} |
Loading…
Reference in new issue