- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Utils/Io/Xml/Dom/unit-tests/XMLSaveLoadSerializableTest.cpp

    00001
    00002 //
    00003 // Copyright (c) 2006           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00010
    00011 #include <tut.h>
    00012 #include <Mers/Utils/Lang/String.h>
    00013 #include <Mers/Utils/Lang/CharUtils.h>
    00014 #include <Mers/Utils/Lang/Factory.h>
    00015 #include <Mers/Utils/Lang/FactorySupport.h>
    00016 #include <Mers/Utils/Io/SaveLoad/ISerializable.h>
    00017 #include <Mers/Utils/Io/Xml/XercesSetup.h>
    00018 #include <Mers/Utils/Io/Xml/ScopedTranscoder.h>
    00019 #include <Mers/Utils/Io/Xml/Dom/XMLSaveLoad.h>
    00020 #include <xercesc/dom/DOM.hpp>
    00021 #include <Mers/Utils/Logging/logger.h>
    00022 #include <iostream>
    00023
    00025
    00026 using Mers::Utils::Lang::CharUtils;
    00027 using Mers::Utils::Lang::String;
    00028 using Mers::Utils::Lang::FactorySupport;
    00029 using Mers::Utils::Io::SaveLoad::ISerializable;
    00030 using Mers::Utils::Io::SaveLoad::ISaveLoad;
    00031 using Mers::Utils::Io::Xml::XercesSetup;
    00032 using Mers::Utils::Io::Xml::CScopedTranscoder;
    00033 using Mers::Utils::Io::Xml::XMLScopedTranscoder;
    00034 using Mers::Utils::Io::Xml::Dom::XMLSaveLoad;
    00035 using XERCES_CPP_NAMESPACE::DOMImplementation;
    00036 using XERCES_CPP_NAMESPACE::DOMDocument;
    00037 using XERCES_CPP_NAMESPACE::DOMElement;
    00038 using XERCES_CPP_NAMESPACE::DOMImplementationRegistry;
    00039 using XERCES_CPP_NAMESPACE::DOMAttr;
    00040 using XERCES_CPP_NAMESPACE::DOMNode;
    00041 using XERCES_CPP_NAMESPACE::DOMNodeList;
    00042 using tut::ensure;
    00043 using tut::ensure_equals;
    00044 using log4cplus::Logger;
    00045 using std::ostream;
    00046
    00048
    00049 namespace {
    00050
    00054 class SerClassA : public ISerializable
    00055 {
    00056 private:
    00057         static Logger s_logger;
    00058
    00059 public:
    00060         SerClassA( const char * name ) : m_name( name ),
    00061                 m_left( NULL ), m_right( NULL )
    00062         {
    00063                 LOG4CPLUS_TRACE( s_logger, "SerClassA::SerClassA(" << name << ")" );
    00064         }
    00065
    00066         SerClassA( ISaveLoad * manager )
    00067         : m_name(), m_left( NULL ), m_right( NULL )
    00068         {
    00069                 LOG4CPLUS_TRACE( s_logger, "SerClassA::SerClassA( manager{" <<
    00070                         (manager->isLoading() ? "Loading" : "Saving") << "} )" );
    00071                 saveLoad( manager );
    00072         }
    00073
    00074         virtual ~SerClassA() {
    00075                 LOG4CPLUS_TRACE_STR( s_logger, "SerClassA::~SerClassA()" );
    00076         }
    00077
    00081         virtual void saveLoad( ISaveLoad * manager )
    00082         {
    00083                 manager->saveLoad( "name", m_name );
    00084                 manager->saveLoad( m_left );
    00085                 manager->saveLoad( m_right );
    00086         }
    00087
    00092         virtual const char * getTypeName()
    00093         {
    00094                 return "SerClassA";
    00095         }
    00096
    00100         static bool compare( const SerClassA * p1, const SerClassA * p2 )
    00101         {
    00102                 return
    00103                         // Shallow compare
    00104                         ( p1 == p2 ) ||
    00105                         // Or deep compare
    00106                         ( p1 != NULL && p2 != NULL &&
    00107                           *p1 == *p2 );
    00108         }
    00109
    00114         bool operator == ( const SerClassA & other ) const
    00115         {
    00116                 return m_name == other.m_name &&
    00117                         compare( m_left, other.m_left ) &&
    00118                         compare( m_right, other.m_right );
    00119         }
    00120
    00124         bool operator != ( const SerClassA & other ) const
    00125         {
    00126                 return !operator==( other );
    00127         }
    00128
    00132         friend ostream & operator << ( ostream & out, const SerClassA &obj )
    00133         {
    00134                 out << obj.m_name << "<";
    00135                 if( obj.m_left != NULL )
    00136                         out << *obj.m_left;
    00137                 out << "|";
    00138                 if( obj.m_right != NULL )
    00139                         out << *obj.m_right;
    00140                 out << ">";
    00141
    00142                 return out;
    00143         }
    00144
    00145
    00149         void set( SerClassA * left, SerClassA * right )
    00150         {
    00151                 LOG4CPLUS_TRACE( s_logger, "SerClassA::set(" <<
    00152                         (left == NULL ? "NULL" : (const char *)left->m_name) << ", " <<
    00153                         (right == NULL ? "NULL" : (const char *)right->m_name) << ")" );
    00154                 m_left = left;
    00155                 m_right = right;
    00156         }
    00157
    00158 public:
    00160         String m_name;
    00161
    00163         SerClassA * m_left;
    00164         SerClassA * m_right;
    00165 };
    00166
    00170 class XMLSaveLoadData
    00171 {
    00172 private:
    00173         static Logger s_logger;
    00174
    00175 public:
    00179         XMLSaveLoadData() :
    00180                 m_xerces( NULL ), m_serClassAFactorySupport( "SerClassA" )
    00181         {
    00182                 // Setup Xerces.
    00183                 m_xerces = new XercesSetup;
    00184
    00185                 // Create an XML document.
    00186                 XMLScopedTranscoder sXMLV1("XML");
    00187                 m_implementation =
    00188                         DOMImplementationRegistry::getDOMImplementation( sXMLV1 );
    00189                 XMLScopedTranscoder sDocRootName("save_test");
    00190                 m_document =
    00191                         m_implementation->createDocument(NULL, sDocRootName, NULL);
    00192         }
    00193
    00194         ~XMLSaveLoadData()
    00195         {
    00196                 m_document->release();
    00197                 delete m_xerces;
    00198         }
    00199
    00200         void testSaveLoadSerClassA( bool useDefault )
    00201         {
    00202                 SerClassA root("Root"), n1("N1"), n2("N2"), n3("N3");
    00203                 root.set( &n1, &n2 );
    00204                 n1.set( &n2, &n3 );
    00205                 n2.set( &n3, NULL );
    00206                 n3.set( NULL, NULL );
    00207
    00208                 // Create our SaveLoad object for saving.
    00209                 XMLSaveLoad savingSaveLoad( m_document, XMLSaveLoad::kSave,
    00210                         XMLSaveLoad::kCurrentVersion );
    00211
    00212                 // Create a SerClassA * value to save.
    00213                 SerClassA * val( &root );
    00214
    00215                 // Save the value.
    00216                 savingSaveLoad.saveLoad( val, XMLSaveLoad::kCurrentVersion );
    00217
    00218                 // Make sure we didn't mess up the pointer.
    00219                 // Ensure( actual, expected )
    00220                 SerClassA * pNull( NULL );
    00221                 ensure_equals( "Saving did not alter the pointer", val, &root );
    00222                 ensure_equals( "Saving did not alter the data", *val, root );
    00223                 ensure_equals( "Saving did not alter root.l", root.m_left, &n1 );
    00224                 ensure_equals( "Saving did not alter root.r", root.m_right, &n2 );
    00225                 ensure_equals( "Saving did not alter n1.l", n1.m_left, &n2 );
    00226                 ensure_equals( "Saving did not alter n1.r", n1.m_right, &n3 );
    00227                 ensure_equals( "Saving did not alter n2.l", n2.m_left, &n3 );
    00228                 ensure_equals( "Saving did not alter n2.r", n2.m_right, pNull );
    00229                 ensure_equals( "Saving did not alter n3.l", n3.m_left, pNull );
    00230                 ensure_equals( "Saving did not alter n3.r", n3.m_right, pNull );
    00231
    00232                 // Create our SaveLoad object for loading.
    00233                 XMLSaveLoad loadingSaveLoad( m_document, XMLSaveLoad::kLoad );
    00234                 // Load a new value.
    00235                 SerClassA * valLoad;
    00236                 if( useDefault ) {
    00237                         loadingSaveLoad.saveLoad( valLoad,
    00238                                 XMLSaveLoad::kCurrentVersion + 1 );
    00239
    00240                         // Verify the data loaded
    00241                         ensure_equals( "Loading loads the value", valLoad, pNull );
    00242                 } else {
    00243                         loadingSaveLoad.saveLoad( valLoad, XMLSaveLoad::kCurrentVersion );
    00244
    00245                         // Verify the data loaded
    00246                         ensure_equals( "Loading loads the same data", *valLoad, root );
    00247
    00248                         // Make sure we only have 4 objects now.
    00249                         ensure_equals( "Loading only one n2", valLoad->m_left->m_left,
    00250                                 valLoad->m_right );
    00251                         ensure_equals( "Loading only one n3", valLoad->m_left->m_right,
    00252                                 valLoad->m_right->m_left );
    00253                 }
    00254         }
    00255
    00256 protected:
    00257         // Setup Xerces for the test.
    00258         XercesSetup * m_xerces;
    00259
    00260         // The DOM implementation of Xerces.
    00261         DOMImplementation * m_implementation;
    00262
    00263         // The document we're going to use for testing.
    00264         DOMDocument * m_document;
    00265
    00266         // Automatic scoped support for factory creation of SerClassA
    00267         FactorySupport< ISaveLoad::t_SaveLoadFactory, SerClassA > m_serClassAFactorySupport;
    00268 };
    00270
    00271 Logger SerClassA::s_logger( Logger::getInstance("Mers.UnitTests.XMLSaveLoadSerializable.SerClassA") );
    00272 Logger XMLSaveLoadData::s_logger( Logger::getInstance("Mers.UnitTests.XMLSaveLoadSerializable.XMLSaveLoadData") );
    00273
    00275
    00276 typedef tut::test_group<XMLSaveLoadData, 14> t_XMLSaveLoadSerializableFactory;
    00277 typedef t_XMLSaveLoadSerializableFactory::object t_XMLSLSerializableObject;
    00278
    00280
    00281 t_XMLSaveLoadSerializableFactory sssFactory( "XML SaveLoad ISerializable Tests" );
    00282
    00284
    00285 } // End nameless namespace
    00286
    00288
    00289 namespace tut {
    00290
    00292
    00296 template<> template<> void t_XMLSLSerializableObject::test<1>()
    00297 {
    00298         testSaveLoadSerClassA( false );
    00299 }
    00300
    00302
    00306 template<> template<> void t_XMLSLSerializableObject::test<2>()
    00307 {
    00308         testSaveLoadSerClassA( true );
    00309 }
    00310
    00312
    00313 } // tut
    00314
    00316