1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Example Documentation</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.2.15 -->
<center>
<a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="namespaces.html">Namespace List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="examples.html">Examples</a> </center>
<hr><h1>UnitTests.cpp</h1>
<p>
CPPUnit Tests for this library. Binary should be installed as sp-gxmlcpp-test.
<p>
<div class="fragment"><pre><font class="comment">/*</font>
<font class="comment"> * (C) Copyright 2002, Schlund+Partner AG</font>
<font class="comment"> *</font>
<font class="comment"> * This library is free software; you can redistribute it and/or</font>
<font class="comment"> * modify it under the terms of the GNU Lesser General Public</font>
<font class="comment"> * License as published by the Free Software Foundation; either</font>
<font class="comment"> * version 2.1 of the License, or (at your option) any later version.</font>
<font class="comment"> *</font>
<font class="comment"> * This library is distributed in the hope that it will be useful,</font>
<font class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</font>
<font class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</font>
<font class="comment"> * Lesser General Public License for more details.</font>
<font class="comment"> *</font>
<font class="comment"> * You should have received a copy of the GNU Lesser General Public</font>
<font class="comment"> * License along with this library; if not, write to the Free Software</font>
<font class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</font>
<font class="comment">*/</font>
<font class="comment">// Local configuration</font>
<font class="preprocessor">#include "config.h"</font>
<font class="comment">// C++ Libraries</font>
<font class="preprocessor">#include <cppunit/TestCase.h></font>
<font class="preprocessor">#include <cppunit/TestSuite.h></font>
<font class="preprocessor">#include <cppunit/TestCaller.h></font>
<font class="preprocessor">#include <cppunit/TextTestRunner.h></font>
<font class="preprocessor">#include <<a class="code" href="XMLTree_8hpp.html">sp-gxmlcpp/XMLTree.hpp</a>></font>
<font class="preprocessor">#include <<a class="code" href="XMLDump_8hpp.html">sp-gxmlcpp/XMLDump.hpp</a>></font>
<font class="preprocessor">#include <<a class="code" href="XSLTrans_8hpp.html">sp-gxmlcpp/XSLTrans.hpp</a>></font>
<font class="preprocessor">#include <<a class="code" href="XMLNode_8hpp.html">sp-gxmlcpp/XMLNode.hpp</a>></font>
<font class="preprocessor">#include <<a class="code" href="XMLNodeSet_8hpp.html">sp-gxmlcpp/XMLNodeSet.hpp</a>></font>
<font class="preprocessor">#include <<a class="code" href="XMLException_8hpp.html">sp-gxmlcpp/XMLException.hpp</a>></font>
<font class="keyword">namespace </font>SP {
<font class="keyword">namespace </font>GXML {
<font class="comment">//</font>
<font class="comment">// XMLTree (&& XMLDump)</font>
<font class="comment">//</font>
<font class="keyword">class </font>TestXMLTreeOld: <font class="keyword">public</font> CppUnit::TestCase
{
<font class="keyword">public</font>:
TestXMLTreeOld()
{}
<font class="keywordtype">void</font> testRun()
{
<font class="comment">// ^^ Note: dump will always use " (not ') to quote, so we must do this here too for string compare</font>
<font class="keyword">const</font> std::string xmlString(
<font class="stringliteral">"<?xml version=\"1.0\"?>\n"</font>
<font class="stringliteral">"<req>\n"</font>
<font class="stringliteral">" <uid>5</uid>\n"</font>
<font class="stringliteral">" <home>/home/absurd</home>\n"</font>
<font class="stringliteral">" <key>AAAABBBBCCCCDDDD</key>\n"</font>
<font class="stringliteral">"\n"</font>
<font class="stringliteral">" <a>\n"</font>
<font class="stringliteral">" <b>\n"</font>
<font class="stringliteral">" <c>Wert1</c>\n"</font>
<font class="stringliteral">" </b>\n"</font>
<font class="stringliteral">" <b>\n"</font>
<font class="stringliteral">" <c> Wert 2</c>\n"</font>
<font class="stringliteral">" </b>\n"</font>
<font class="stringliteral">" </a>\n"</font>
<font class="stringliteral">"\n"</font>
<font class="stringliteral">" <X>5</X>\n"</font>
<font class="stringliteral">"\n"</font>
<font class="stringliteral">" <int>-42</int>\n"</font>
<font class="stringliteral">" <int>17</int>\n"</font>
<font class="stringliteral">" <bool>0</bool>\n"</font>
<font class="stringliteral">" <string> abc def </string>\n"</font>
<font class="stringliteral">"\n"</font>
<font class="stringliteral">" <X>13</X>\n"</font>
<font class="stringliteral">"\n"</font>
<font class="stringliteral">"<tags>\n"</font>
<font class="stringliteral">" <subtag>EINS</subtag>\n"</font>
<font class="stringliteral">"</tags>\n"</font>
<font class="stringliteral">"\n"</font>
<font class="stringliteral">" <tags>\n"</font>
<font class="stringliteral">" <subtag>ZWEI</subtag>\n"</font>
<font class="stringliteral">" </tags>\n"</font>
<font class="stringliteral">"\n"</font>
<font class="stringliteral">" <X>17</X>\n"</font>
<font class="stringliteral">"\n"</font>
<font class="stringliteral">"</req>\n"</font>);
<font class="comment">// ^^ Note: dump will always add a \n to the last line (in a full ("/") dump, that is), so our string should have this too for string compare</font>
<font class="comment">// Note2: It's a bit dumb to literally compare dumps here, as the default way of producing dumps might change... STS</font>
<font class="comment">// Partial Tree && Dump</font>
XMLTree xmlTree1(xmlString.c_str(), xmlString.size());
std::string dump1 = XMLDump(&xmlTree1, <font class="stringliteral">"/req/tags"</font>).get();
CPPUNIT_ASSERT(dump1 ==
<font class="stringliteral">"<tags>\n"</font>
<font class="stringliteral">" <subtag>EINS</subtag>\n"</font>
<font class="stringliteral">"</tags>"</font>);
<font class="comment">// Full Tree && Dump</font>
XMLTree xmlTree(xmlString.c_str(), xmlString.size());
std::string dump = XMLDump(&xmlTree, <font class="stringliteral">"/"</font>).get();
<font class="comment">// STS This does not work anymore with default encoding added in XMLDump</font>
<font class="comment">//CPPUNIT_ASSERT(dump == xmlString);</font>
<font class="comment">// XMLTree methods</font>
CPPUNIT_ASSERT(xmlTree.getValue(<font class="stringliteral">"/req/string"</font>) == <font class="stringliteral">" abc def "</font>);
CPPUNIT_ASSERT(xmlTree.getCount(<font class="stringliteral">"/req/int"</font>) == 2);
CPPUNIT_ASSERT(xmlTree.getValue(<font class="stringliteral">"/req/int"</font>) == <font class="stringliteral">"-42"</font>);
CPPUNIT_ASSERT(xmlTree.getValue(<font class="stringliteral">"/req/int[position()=2]"</font>) == <font class="stringliteral">"17"</font>);
CPPUNIT_ASSERT(xmlTree.getCount(<font class="stringliteral">"/req/tags/subtag"</font>) == 2);
CPPUNIT_ASSERT(xmlTree.getValue(<font class="stringliteral">"/req/tags/subtag"</font>) == <font class="stringliteral">"EINS"</font>);
CPPUNIT_ASSERT(xmlTree.getCount(<font class="stringliteral">"/req/nonexisting"</font>) == 0);
CPPUNIT_ASSERT(xmlTree.getCount(<font class="stringliteral">"/nonexisting/tag"</font>) == 0);
CPPUNIT_ASSERT(xmlTree.getValue(<font class="stringliteral">"/nonexisting/tag"</font>) == <font class="stringliteral">""</font>);
xmlTree.delTag(<font class="stringliteral">"/req/int"</font>);
CPPUNIT_ASSERT(xmlTree.getCount(<font class="stringliteral">"/req/int"</font>) == 1);
xmlTree.addTag(<font class="stringliteral">"/req"</font>, <font class="stringliteral">"newtag"</font>, <font class="stringliteral">"contents of new tag"</font>);
CPPUNIT_ASSERT(xmlTree.getValue(<font class="stringliteral">"/req/newtag"</font>) == <font class="stringliteral">"contents of new tag"</font>);
}
};
<font class="comment">// XSLTrans Test</font>
<font class="comment">//</font>
<font class="keyword">class </font>TestXSLTrans : <font class="keyword">public</font> CppUnit::TestCase
{
<font class="keyword">public</font>:
TestXSLTrans()
{}
<font class="keywordtype">void</font> testRun()
{
std::string xmlString(
<font class="stringliteral">"<?xml version=\"1.0\"?>\n"</font>
<font class="stringliteral">"<!DOCTYPE guestbook>\n"</font>
<font class="stringliteral">"\n"</font>
<font class="stringliteral">"<guestbook>\n"</font>
<font class="stringliteral">" <record>\n"</font>
<font class="stringliteral">" <name>kurt</name>\n"</font>
<font class="stringliteral">" <date>2.3.01</date>\n"</font>
<font class="stringliteral">" <text>\n"</font>
<font class="stringliteral">" find ich total klasse, koennte aber noch besser sein\n"</font>
<font class="stringliteral">" </text>\n"</font>
<font class="stringliteral">" </record>\n"</font>
<font class="stringliteral">" <record>\n"</font>
<font class="stringliteral">" <name>kurt</name>\n"</font>
<font class="stringliteral">" <date>2.3.01</date>\n"</font>
<font class="stringliteral">" <text>\n"</font>
<font class="stringliteral">" find ich total klasse, koennte aber noch besser sein\n"</font>
<font class="stringliteral">" </text>\n"</font>
<font class="stringliteral">" </record>\n"</font>
<font class="stringliteral">"</guestbook>\n"</font>);
std::string xslString(
<font class="stringliteral">"<?xml version=\"1.0\"?>\n"</font>
<font class="stringliteral">"<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n"</font>
<font class="stringliteral">"<!-- Basic stylesheet for GUESTBOOK -->\n"</font>
<font class="stringliteral">"<xsl:output indent=\"yes\" />\n"</font>
<font class="stringliteral">"<xsl:template match=\"guestbook\">\n"</font>
<font class="stringliteral">" <html>\n"</font>
<font class="stringliteral">" <head>\n"</font>
<font class="stringliteral">" <title>Gaestebuch</title>\n"</font>
<font class="stringliteral">" </head> \n"</font>
<font class="stringliteral">" <body bgcolor=\"FFFFFF\">\n"</font>
<font class="stringliteral">" <h1>Mein Gaestebuch</h1>\n"</font>
<font class="stringliteral">" <center>\n"</font>
<font class="stringliteral">" <table cellspacing=\"2\" cellpadding=\"3\" border=\"0\">\n"</font>
<font class="stringliteral">" \n"</font>
<font class="stringliteral">" <xsl:apply-templates/>\n"</font>
<font class="stringliteral">" \n"</font>
<font class="stringliteral">" </table>\n"</font>
<font class="stringliteral">" </center>\n"</font>
<font class="stringliteral">" </body>\n"</font>
<font class="stringliteral">" </html>\n"</font>
<font class="stringliteral">"</xsl:template>\n"</font>
<font class="stringliteral">"\n"</font>
<font class="stringliteral">"<xsl:template match=\"record\">\n"</font>
<font class="stringliteral">" \n"</font>
<font class="stringliteral">" <tr> \n"</font>
<font class="stringliteral">" <td bgcolor=\"336699\"><font face=\"Arial,helvitica\" size=\"-1\" color=\"FFFFFF\">Name</font></td>\n"</font>
<font class="stringliteral">" <td bgcolor=\"336699\"><font face=\"Arial,helvitica\" size=\"-1\" color=\"FFFFFF\"><xsl:value-of select=\"name\"/></font></td>\n"</font>
<font class="stringliteral">" </tr>\n"</font>
<font class="stringliteral">" <tr>\n"</font>
<font class="stringliteral">" <td bgcolor=\"336699\"><font face=\"Arial,helvitica\" size=\"-1\" color=\"FFFFFF\">Datum</font></td>\n"</font>
<font class="stringliteral">" <td bgcolor=\"336699\"><font face=\"Arial,helvitica\" size=\"-1\" color=\"FFFFFF\"><xsl:value-of select=\"date\"/></font></td>\n"</font>
<font class="stringliteral">" </tr>\n"</font>
<font class="stringliteral">" <tr>\n"</font>
<font class="stringliteral">" <td bgcolor=\"336699\"><font face=\"Arial,helvitica\" size=\"-1\" color=\"FFFFFF\">Text</font></td>\n"</font>
<font class="stringliteral">" <td bgcolor=\"336699\"><font face=\"Arial,helvitica\" size=\"-1\" color=\"FFFFFF\"><xsl:value-of select=\"text\"/></font></td>\n"</font>
<font class="stringliteral">" </tr>\n"</font>
<font class="stringliteral">" <tr >\n"</font>
<font class="stringliteral">" <td colspan=\"2\"><hr height=\"1\"/></td>\n"</font>
<font class="stringliteral">" </tr>\n"</font>
<font class="stringliteral">" \n"</font>
<font class="stringliteral">"</xsl:template>\n"</font>
<font class="stringliteral">"</xsl:stylesheet>\n"</font>);
XMLTree xmlTree(xmlString);
XSLTrans xslTrans(xslString);
std::auto_ptr<XMLTree> resultTree = xslTrans.transToXMLTree(xmlTree.getDocPtr());
CPPUNIT_ASSERT(resultTree.get()->getValue(<font class="stringliteral">"/html/head/title"</font>) == <font class="stringliteral">"Gaestebuch"</font>);
CPPUNIT_ASSERT(resultTree.get()->getValue(<font class="stringliteral">"/html/body/h1"</font>) == <font class="stringliteral">"Mein Gaestebuch"</font>);
CPPUNIT_ASSERT(resultTree.get()->getCount(<font class="stringliteral">"/html/body/center/table"</font>) == 1);
}
};
<font class="comment">// TestXMLTree</font>
<font class="comment">//</font>
<font class="keyword">class </font>TestXMLTree : <font class="keyword">public</font> CppUnit::TestCase
{
<font class="keyword">public</font>:
TestXMLTree()
:tree_(NULL)
{
<font class="comment">// test XMLTree's following functions:</font>
<font class="comment">// getString</font>
<font class="comment">// getBool (XSLT-functions: boolean, false, true, not)</font>
<font class="comment">// getFloat</font>
<font class="comment">// getNodeSet</font>
<font class="comment">// getXML</font>
<font class="comment">// getTree</font>
xmlTree_ = <font class="stringliteral">"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"</font>
<font class="stringliteral">"<test>\n"</font>
<font class="stringliteral">" <string>StringValue</string>\n"</font>
<font class="stringliteral">" <string>\n"</font>
<font class="stringliteral">" Also a string value\n"</font>
<font class="stringliteral">" </string>\n"</font>
<font class="stringliteral">" <boolean>1</boolean>\n"</font>
<font class="stringliteral">" <boolean>0</boolean>\n"</font>
<font class="stringliteral">" <boolean>-1</boolean>\n"</font>
<font class="stringliteral">" <float>3.5</float>\n"</font>
<font class="stringliteral">" <float>1.6</float>\n"</font>
<font class="stringliteral">" <float>-123.2</float>\n"</font>
<font class="stringliteral">"</test>"</font>;
tree_ = <font class="keyword">new</font> XMLTree( xmlTree_.c_str() );
}
~TestXMLTree()
{
<font class="keyword">delete</font> tree_;
}
<font class="keywordtype">void</font> testRun()
{
CPPUNIT_ASSERT( tree_ );
testGetString();
testGetBool();
testGetFloat();
testGetNodeSet();
testGetXML();
testGetTree();
}
<font class="keywordtype">void</font> testGetString()
{
<font class="comment">// test getString</font>
<font class="comment">// string( xpath ) returns content of first xpath-node</font>
<font class="comment">// string( invalid_xpath ) returns ""</font>
CPPUNIT_ASSERT( tree_->getString( <font class="stringliteral">"string( /test/string )"</font> ) == <font class="stringliteral">"StringValue"</font> );
CPPUNIT_ASSERT( tree_->getString( <font class="stringliteral">"string( /test/string[position() = 2] )"</font> ) == <font class="stringliteral">"\n\t\tAlso a string value\n\t"</font> );
CPPUNIT_ASSERT( tree_->getString( <font class="stringliteral">"string( /test/something )"</font> ) == <font class="stringliteral">""</font> );
<font class="keywordflow">try</font> {
<font class="comment">// xpath won't return a string</font>
tree_->getString( <font class="stringliteral">"/test/string"</font> );
CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
} <font class="keywordflow">catch</font> ( GXML::XMLException & e ) {
CPPUNIT_ASSERT( e.getException() == XMLException::XMLTREE_NO_STRING_FROM_PATH );
}
<font class="keywordflow">try</font> {
<font class="comment">// xpath won't return a string</font>
tree_->getString( <font class="stringliteral">"/test/something"</font> );
CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
} <font class="keywordflow">catch</font> ( XMLException & e ) {
CPPUNIT_ASSERT( e.getException() == XMLException::XMLTREE_NO_STRING_FROM_PATH );
}
}
<font class="keywordtype">void</font> testGetBool()
{
<font class="comment">// test getBool</font>
<font class="comment">// 0 = false else true, "" = false else true</font>
CPPUNIT_ASSERT( tree_->getBool( <font class="stringliteral">"boolean( number( /test/boolean ) )"</font> ) );
CPPUNIT_ASSERT( ! tree_->getBool( <font class="stringliteral">"boolean( number( /test/boolean[position() = 2] ) )"</font> ) );
CPPUNIT_ASSERT( tree_->getBool( <font class="stringliteral">"boolean( number( /test/boolean[position() = 3] ) )"</font> ) );
CPPUNIT_ASSERT( tree_->getBool( <font class="stringliteral">"true()"</font> ) );
CPPUNIT_ASSERT( ! tree_->getBool( <font class="stringliteral">"false()"</font> ) );
CPPUNIT_ASSERT( tree_->getBool( <font class="stringliteral">"not( /test/something )"</font> ) );
CPPUNIT_ASSERT( ! tree_->getBool( <font class="stringliteral">"not( /test/string )"</font> ) );
CPPUNIT_ASSERT( tree_->getBool( <font class="stringliteral">"boolean( /test/string )"</font> ) );
CPPUNIT_ASSERT( ! tree_->getBool( <font class="stringliteral">"boolean( /test/something )"</font> ) );
CPPUNIT_ASSERT( tree_->getBool( <font class="stringliteral">"boolean( 'anything' )"</font> ) );
CPPUNIT_ASSERT( ! tree_->getBool( <font class="stringliteral">"boolean( '' )"</font> ) );
<font class="keywordflow">try</font> {
<font class="comment">// xpath won't return a boolean</font>
tree_->getBool( <font class="stringliteral">"/test/string"</font> );
CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
} <font class="keywordflow">catch</font> ( XMLException & e ) {
CPPUNIT_ASSERT( e.getException() == XMLException::XMLTREE_NO_BOOL_FROM_PATH );
}
}
<font class="keywordtype">void</font> testGetFloat()
{
<font class="comment">// test getFloat</font>
CPPUNIT_ASSERT_DOUBLES_EQUAL( <font class="keywordtype">double</font>(-118.1), tree_->getFloat( <font class="stringliteral">"sum( /test/float )"</font> ), 1E-10 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( <font class="keywordtype">double</font>(-123.2), tree_->getFloat( <font class="stringliteral">"number( /test/float[position() = 3 ])"</font> ), 1E-10 );
CPPUNIT_ASSERT( tree_->getFloat( <font class="stringliteral">"count( /test/boolean )"</font> ) == 3 );
<font class="keywordflow">try</font> {
<font class="comment">// xpath won't return a number</font>
tree_->getFloat( <font class="stringliteral">"/test/string"</font> );
CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
} <font class="keywordflow">catch</font> ( XMLException & e ) {
CPPUNIT_ASSERT( e.getException() == XMLException::XMLTREE_NO_FLOAT_FROM_PATH );
}
}
<font class="keywordtype">void</font> testGetNodeSet()
{
std::auto_ptr<XMLNodeSet> set = tree_->getNodeSet( <font class="stringliteral">"/test/*"</font> );
CPPUNIT_ASSERT( set->size() == 8 );
<font class="keywordflow">try</font> {
<font class="comment">// xpath will return an empty set</font>
std::auto_ptr<XMLNodeSet> set = tree_->getNodeSet( <font class="stringliteral">"/something"</font> );
CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
} <font class="keywordflow">catch</font> ( XMLException & e ) {
CPPUNIT_ASSERT( e.getException() == XMLException::XMLNODESET_NO_MATCH );
}
<font class="keywordflow">try</font> {
<font class="comment">// xpath won't return a set</font>
std::auto_ptr<XMLNodeSet> set = tree_->getNodeSet( <font class="stringliteral">"true()"</font> );
CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
} <font class="keywordflow">catch</font> ( XMLException & e ) {
CPPUNIT_ASSERT( e.getException() == XMLException::XMLNODESET_NO_SET );
}
}
<font class="keywordtype">void</font> testGetXML()
{
CPPUNIT_ASSERT( xmlTree_+<font class="stringliteral">"\n"</font> == tree_->getXML( <font class="stringliteral">"/"</font> ) );
}
<font class="keywordtype">void</font> testGetTree()
{
CPPUNIT_ASSERT( tree_->getXML() == tree_->getTree()->getXML() );
}
<font class="keyword">private</font>:
std::string xmlTree_;
XMLTree *tree_;
};
<font class="comment">// TestXMLNode</font>
<font class="comment">//</font>
<font class="keyword">class </font>TestXMLNode : <font class="keyword">public</font> CppUnit::TestCase
{
<font class="keyword">public</font>:
TestXMLNode()
{
<font class="comment">// test XMLNode's following functions:</font>
<font class="comment">// getNext</font>
<font class="comment">// getPrev</font>
<font class="comment">// getParent</font>
<font class="comment">// getLast</font>
<font class="comment">// getChild</font>
<font class="comment">// getName</font>
<font class="comment">// getContent</font>
<font class="comment">// getAttribute( name )</font>
xmlTree_ = <font class="stringliteral">"<?xml version=\"1.0\"?>\n"</font>
<font class="stringliteral">"<test><node>node1</node><node name=\"node2\">node2<child>child of node 2</child></node><node>last node</node></test>"</font>;
tree_ = <font class="keyword">new</font> XMLTree( xmlTree_.c_str() );
}
<font class="keywordtype">void</font> testRun()
{
XMLNode node1 = XMLNode();
XMLNode node2 = XMLNode();
XMLNode node3 = XMLNode();
std::auto_ptr<XMLNodeSet> set = tree_->getNodeSet( <font class="stringliteral">"/test/node"</font> );
<font class="keywordflow">for</font> ( XMLNodeSet::Iterator iter = set->begin(); iter != set->end(); ++iter )
{
<font class="keywordflow">switch</font> ( iter.getPosition() )
{
<font class="keywordflow">case</font> 0: node1 = *iter;
<font class="keywordflow">break</font>;
<font class="keywordflow">case</font> 1: node2 = *iter;
<font class="keywordflow">break</font>;
<font class="keywordflow">case</font> 2: node3 = *iter;
<font class="keywordflow">break</font>;
<font class="keywordflow">default</font>: CPPUNIT_ASSERT( <font class="keyword">false</font> ); <font class="comment">//should never get here.</font>
}
}
CPPUNIT_ASSERT( node1.getName() == <font class="stringliteral">"node"</font> );
CPPUNIT_ASSERT( node1.getContent() == <font class="stringliteral">"node1"</font> );
CPPUNIT_ASSERT( node1.getNext().getContent() == node2.getContent() );
<font class="comment">// CPPUNIT_ASSERT( node1.getLast().getContent() == node3.getContent() );</font>
CPPUNIT_ASSERT( node2.getContent() == <font class="stringliteral">"node2"</font> );
CPPUNIT_ASSERT( node2.getNext().getContent() == node3.getContent() );
CPPUNIT_ASSERT( node2.getAttribute( <font class="stringliteral">"name"</font> ) == <font class="stringliteral">"node2"</font> );
<font class="comment">// first child of node2 is its content text-node, therefore take child->next</font>
CPPUNIT_ASSERT( node2.getChild().getNext().getContent() == <font class="stringliteral">"child of node 2"</font> );
CPPUNIT_ASSERT( node3.getPrev().getContent() == node2.getContent() );
CPPUNIT_ASSERT( node3.getParent().getName() == node2.getParent().getName() );
CPPUNIT_ASSERT( node3.getParent().getChild().getContent() == node1.getContent() );
CPPUNIT_ASSERT( node3.getParent().getLast().getContent() == node3.getContent() );
<font class="comment">// first child of node with content is a text-node</font>
CPPUNIT_ASSERT( node3.getChild().getName() == <font class="stringliteral">"text"</font> );
<font class="comment">// test for exceptions</font>
<font class="keywordflow">try</font> {
node3 = node3.getNext();
CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
} <font class="keywordflow">catch</font> ( XMLException & e ) {
CPPUNIT_ASSERT( e.getException() == XMLException::XMLNODE_LAST_NODE );
}
<font class="keywordflow">try</font> {
node1 = node1.getPrev();
CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
} <font class="keywordflow">catch</font> ( XMLException & e ) {
CPPUNIT_ASSERT( e.getException() == XMLException::XMLNODE_FIRST_NODE );
}
<font class="keywordflow">try</font> {
<font class="comment">// parent of parent is document node, which has no parent</font>
node1 = node1.getParent().getParent().getParent();
CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
} <font class="keywordflow">catch</font> ( XMLException & e ) {
CPPUNIT_ASSERT( e.getException() == XMLException::XMLNODE_NO_PARENT );
}
<font class="keywordflow">try</font> {
node1 = node1.getPrev();
CPPUNIT_ASSERT( ! <font class="stringliteral">"Exception expected!!!"</font> );
} <font class="keywordflow">catch</font> ( XMLException & e ) {
CPPUNIT_ASSERT( e.getException() == XMLException::XMLNODE_FIRST_NODE );
}
}
<font class="keyword">private</font>:
std::string xmlTree_;
XMLTree *tree_;
};
<font class="comment">// XMLNodeSet Test</font>
<font class="comment">//</font>
<font class="keyword">class </font>TestXMLNodeSet : <font class="keyword">public</font> CppUnit::TestCase
{
<font class="keyword">public</font>:
TestXMLNodeSet()
{};
<font class="keywordtype">void</font> testRun()
{}
};
<font class="comment">// Master Class for all tests</font>
<font class="comment">//</font>
<font class="keyword">class </font>UnitTests: <font class="keyword">public</font> CppUnit::TestSuite
{
<font class="keyword">public</font>:
UnitTests()
{
addTest(<font class="keyword">new</font> CppUnit::TestCaller<TestXMLTreeOld>(<font class="stringliteral">"Testing XMLTree (old)"</font>, &TestXMLTreeOld::testRun));
addTest(<font class="keyword">new</font> CppUnit::TestCaller<TestXMLTree> (<font class="stringliteral">"Testing XMLTree"</font>, &TestXMLTree::testRun));
addTest(<font class="keyword">new</font> CppUnit::TestCaller<TestXSLTrans> (<font class="stringliteral">"Testing XSLTrans"</font>, &TestXSLTrans::testRun));
addTest(<font class="keyword">new</font> CppUnit::TestCaller<TestXMLNode> (<font class="stringliteral">"Testing XMLNode"</font>, &TestXMLNode::testRun));
addTest(<font class="keyword">new</font> CppUnit::TestCaller<TestXMLNodeSet>(<font class="stringliteral">"Testing XMLNodeSet"</font>, &TestXMLNodeSet::testRun));
}
};
}}
<font class="comment">// create binary that tests the XML* stuff</font>
<font class="keywordtype">int</font>
main(<font class="keywordtype">int</font> argc, <font class="keywordtype">char</font> **argv)
{
CppUnit::TextTestRunner runner;
<font class="comment">// This must be an object, and must be created with new, as it is deleted by runner (this is braindead, but well).</font>
SP::GXML::UnitTests * unitTests = <font class="keyword">new</font> SP::GXML::UnitTests();
runner.addTest(unitTests);
<font class="keywordflow">if</font> (runner.run())
<font class="keywordflow">return</font>(0);
<font class="keywordflow">else</font>
<font class="keywordflow">return</font>(1);
}
</pre></div><hr><address align="right"><small>Generated on Thu Jun 3 19:20:12 2004 for sp-gxmlcpp by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0
width=110 height=53></a>1.2.15 </small></address>
</body>
</html>
|