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

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

    00001
    00002 //
    00003 // Copyright (c) 2005           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/Io/Xml/XercesSetup.h>
    00015 #include <Mers/Utils/Io/Xml/ScopedTranscoder.h>
    00016 #include <Mers/Utils/Io/Xml/Dom/XMLSaveLoad.h>
    00017 #include <xercesc/dom/DOM.hpp>
    00018
    00020
    00021 using Mers::Utils::Lang::CharUtils;
    00022 using Mers::Utils::Lang::String;
    00023 using Mers::Utils::Io::Xml::XercesSetup;
    00024 using Mers::Utils::Io::Xml::CScopedTranscoder;
    00025 using Mers::Utils::Io::Xml::XMLScopedTranscoder;
    00026 using Mers::Utils::Io::Xml::Dom::XMLSaveLoad;
    00027 using XERCES_CPP_NAMESPACE::DOMImplementation;
    00028 using XERCES_CPP_NAMESPACE::DOMDocument;
    00029 using XERCES_CPP_NAMESPACE::DOMElement;
    00030 using XERCES_CPP_NAMESPACE::DOMImplementationRegistry;
    00031 using XERCES_CPP_NAMESPACE::DOMAttr;
    00032 using XERCES_CPP_NAMESPACE::DOMNode;
    00033 using XERCES_CPP_NAMESPACE::DOMNodeList;
    00034 using tut::ensure;
    00035 using tut::ensure_equals;
    00036
    00038
    00039 namespace {
    00040
    00044 class XMLSaveLoadData
    00045 {
    00046 public:
    00050         XMLSaveLoadData() :
    00051                 m_xerces( NULL )
    00052         {
    00053                 // Setup Xerces.
    00054                 m_xerces = new XercesSetup;
    00055
    00056                 // Create an XML document.
    00057                 XMLScopedTranscoder sXMLV1("XML");
    00058                 m_implementation =
    00059                         DOMImplementationRegistry::getDOMImplementation( sXMLV1 );
    00060                 XMLScopedTranscoder sDocRootName("save_test");
    00061                 m_document =
    00062                         m_implementation->createDocument(NULL, sDocRootName, NULL);
    00063         }
    00064
    00065         ~XMLSaveLoadData()
    00066         {
    00067                 m_document->release();
    00068                 delete m_xerces;
    00069         }
    00070
    00071         template< typename t_Data >
    00072         void testSaveLoad( const t_Data valCopy, const t_Data valDefault,
    00073                 bool useDefault )
    00074         {
    00075                 // Create our SaveLoad object for saving.
    00076                 XMLSaveLoad savingSaveLoad( m_document, XMLSaveLoad::kSave,
    00077                         XMLSaveLoad::kCurrentVersion );
    00078
    00079                 // Create a t_Data value to save.
    00080                 t_Data val( valCopy );
    00081
    00082                 // Save the value.
    00083                 savingSaveLoad.saveLoad( "property", val, valDefault,
    00084                         XMLSaveLoad::kCurrentVersion );
    00085
    00086                 // Make sure we didn't mess up the value.
    00087                 ensure_equals( "Saving did not alter the data", valCopy, val );
    00088
    00089                 // Create our SaveLoad object for loading.
    00090                 XMLSaveLoad loadingSaveLoad( m_document, XMLSaveLoad::kLoad );
    00091                 // Load a new value.
    00092                 t_Data valLoad;
    00093                 if( useDefault ) {
    00094                         loadingSaveLoad.saveLoad( "property", valLoad, valDefault,
    00095                                 XMLSaveLoad::kCurrentVersion + 1 );
    00096
    00097                         // Verify the data loaded
    00098                         ensure_equals( "Loading loads the value", valDefault, valLoad );
    00099                 } else {
    00100                         loadingSaveLoad.saveLoad( "property", valLoad, valDefault,
    00101                                 XMLSaveLoad::kCurrentVersion );
    00102
    00103                         // Verify the data loaded
    00104                         ensure_equals( "Loading loads the value", valCopy, valLoad );
    00105                 }
    00106         }
    00107
    00108 protected:
    00109         // Setup Xerces for the test.
    00110         XercesSetup * m_xerces;
    00111
    00112         // The DOM implementation of Xerces.
    00113         DOMImplementation * m_implementation;
    00114
    00115         // The document we're going to use for testing.
    00116         DOMDocument * m_document;
    00117 };
    00118
    00120
    00121 typedef tut::test_group<XMLSaveLoadData, 15> t_XMLSaveLoadFactory;
    00122 typedef t_XMLSaveLoadFactory::object t_XMLSaveLoadObject;
    00123
    00125
    00126 t_XMLSaveLoadFactory ssFactory( "XML SaveLoad Tests" );
    00127
    00129
    00130 } // End nameless namespace
    00131
    00133
    00134 namespace tut {
    00135
    00137
    00141 template<> template<> void t_XMLSaveLoadObject::test<1>()
    00142 {
    00143         testSaveLoad< String >( "This is a complex String! With some symbols.",
    00144                 "Some other default string.", false );
    00145 }
    00146
    00148
    00152 template<> template<> void t_XMLSaveLoadObject::test<2>()
    00153 {
    00154         testSaveLoad< String >( "This is a complex String! With some symbols.",
    00155                 "Some other default string.", true );
    00156 }
    00157
    00159
    00163 template<> template<> void t_XMLSaveLoadObject::test<3>()
    00164 {
    00165         // Create our SaveLoad object for saving.
    00166         XMLSaveLoad savingSaveLoad( m_document, XMLSaveLoad::kSave,
    00167                 XMLSaveLoad::kCurrentVersion );
    00168
    00169         // Create a string to save.
    00170         char * str =
    00171                 CharUtils::strdupnew( "This is a complex String! With some symbols." );
    00172         char * strCopy( CharUtils::strdupnew( str ) );
    00173         const char * strDefault = "Some other default string.";
    00174
    00175         // Save the value.
    00176         savingSaveLoad.saveLoad( "pchar_property", str, strDefault,
    00177                 XMLSaveLoad::kCurrentVersion );
    00178
    00179         // Make sure we didn't mess up the value.
    00180         ensure( "Saving did not alter the data", strcmp( strCopy, str ) == 0 );
    00181
    00182         // Create our SaveLoad object for loading.
    00183         XMLSaveLoad loadingSaveLoad( m_document, XMLSaveLoad::kLoad );
    00184         // Load a new value.
    00185         char * strLoad;
    00186         loadingSaveLoad.saveLoad( "pchar_property", strLoad, strDefault,
    00187                 XMLSaveLoad::kCurrentVersion );
    00188
    00189         // Verify the data loaded
    00190         ensure( "Loading loads the value", strcmp( str, strLoad ) == 0 );
    00191
    00192         // Clean up
    00193         delete str;
    00194         delete strCopy;
    00195         delete strLoad;
    00196 }
    00197
    00199
    00203 template<> template<> void t_XMLSaveLoadObject::test<4>()
    00204 {
    00205         // Create our SaveLoad object for saving.
    00206         XMLSaveLoad savingSaveLoad( m_document, XMLSaveLoad::kSave,
    00207                 XMLSaveLoad::kCurrentVersion );
    00208
    00209         // Create a string to save.
    00210         char * str =
    00211                 CharUtils::strdupnew( "This is a complex String! With some symbols." );
    00212         char * strCopy( CharUtils::strdupnew( str ) );
    00213         const char * strDefault = "Some other default string.";
    00214
    00215         // Save the value.
    00216         savingSaveLoad.saveLoad( "pchar_property", str, strDefault,
    00217                 XMLSaveLoad::kCurrentVersion );
    00218
    00219         // Make sure we didn't mess up the value.
    00220         ensure( "Saving did not alter the data", strcmp( strCopy, str ) == 0 );
    00221
    00222         // Create our SaveLoad object for loading.
    00223         XMLSaveLoad loadingSaveLoad( m_document, XMLSaveLoad::kLoad );
    00224         // Load a new value.
    00225         char * strLoad;
    00226         loadingSaveLoad.saveLoad( "pchar_property", strLoad, strDefault,
    00227                 XMLSaveLoad::kCurrentVersion + 1 );
    00228
    00229         // Verify the data loaded
    00230         ensure( "Loading loads the default", strcmp( strDefault, strLoad ) == 0 );
    00231
    00232         // Clean up
    00233         delete str;
    00234         delete strCopy;
    00235         delete strLoad;
    00236 }
    00237
    00239
    00243 template<> template<> void t_XMLSaveLoadObject::test<5>()
    00244 {
    00245         testSaveLoad< int >( 11357, -325714, false );
    00246 }
    00247
    00249
    00253 template<> template<> void t_XMLSaveLoadObject::test<6>()
    00254 {
    00255         testSaveLoad< int >( 11357, -325714, true );
    00256 }
    00257
    00259
    00263 template<> template<> void t_XMLSaveLoadObject::test<7>()
    00264 {
    00265         testSaveLoad< unsigned int >( 11354, 325724, false );
    00266 }
    00267
    00269
    00273 template<> template<> void t_XMLSaveLoadObject::test<8>()
    00274 {
    00275         testSaveLoad< unsigned int >( 11354, 325724, true );
    00276 }
    00277
    00279
    00283 template<> template<> void t_XMLSaveLoadObject::test<9>()
    00284 {
    00285         testSaveLoad< short >( 11354, -30724, false );
    00286 }
    00287
    00289
    00293 template<> template<> void t_XMLSaveLoadObject::test<10>()
    00294 {
    00295         testSaveLoad< short >( 11354, -30724, true );
    00296 }
    00297
    00299
    00303 template<> template<> void t_XMLSaveLoadObject::test<11>()
    00304 {
    00305         testSaveLoad< unsigned short >( 11354, 32724, false );
    00306 }
    00307
    00309
    00313 template<> template<> void t_XMLSaveLoadObject::test<12>()
    00314 {
    00315         testSaveLoad< unsigned short >( 11354, 32724, true );
    00316 }
    00317
    00319
    00323 template<> template<> void t_XMLSaveLoadObject::test<13>()
    00324 {
    00325         testSaveLoad< double >( 13.2, 192.31232245, false );
    00326 }
    00327
    00329
    00333 template<> template<> void t_XMLSaveLoadObject::test<14>()
    00334 {
    00335         testSaveLoad< double >( 13.2, 192.31232245, true );
    00336 }
    00337
    00339
    00343 template<> template<> void t_XMLSaveLoadObject::test<15>()
    00344 {
    00345         testSaveLoad< unsigned int >( 237629, 325724, false );
    00346 }
    00347
    00349
    00350 } // tut
    00351
    00353