/*! @file @id $Id$ */ // 1 2 3 4 5 6 7 8 // 45678901234567890123456789012345678901234567890123456789012345678901234567890 #include #include #include #include #include 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("\n" "\t15\n" "\t1\n" "\t123.456\n" "\tHello World\n" "\tThis is" " another Text\n" "\t4123674622\n" ""), 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: "<