00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <spl/Debug.h>
00018 #include <spl/Log.h>
00019 #include <spl/xml/XmlElement.h>
00020 #include <spl/xml/XmlDocument.h>
00021 #include <spl/xml/xpath/XPath.h>
00022
00023 #ifdef DEBUG
00024
00025 static const char *_xpathTestXml =
00026 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
00027 "\n"
00028 "<bookstore>\n"
00029 "\n"
00030 "<book category=\"COOKING\">\n"
00031 " <title lang=\"en\">Everyday Italian</title>\n"
00032 " <author>Giada De Laurentiis</author>\n"
00033 " <year>2005</year>\n"
00034 " <price>30.00</price>\n"
00035 "</book>\n"
00036 "\n"
00037 "<book category=\"CHILDREN\">\n"
00038 " <title lang=\"en\">Harry Potter</title>\n"
00039 " <author>J K. Rowling</author>\n"
00040 " <year>2005</year>\n"
00041 " <price>29.99</price>\n"
00042 "</book>\n"
00043 "\n"
00044 "<book category=\"WEB\">\n"
00045 " <title lang=\"en\">XQuery Kick Start</title>\n"
00046 " <author>James McGovern</author>\n"
00047 " <author>Per Bothner</author>\n"
00048 " <author>Kurt Cagle</author>\n"
00049 " <author>James Linn</author>\n"
00050 " <author>Vaidyanathan Nagarajan</author>\n"
00051 " <year>2003</year>\n"
00052 " <price>49.99</price>\n"
00053 "</book>\n"
00054 "\n"
00055 "<book category=\"WEB\">\n"
00056 " <title lang=\"en\">Learning XML</title>\n"
00057 " <author>Erik T. Ray</author>\n"
00058 " <year>2003</year>\n"
00059 " <price>39.95</price>\n"
00060 "</book>\n"
00061 "\n"
00062 "</bookstore>";
00063
00064 static void _TestXPathRoot()
00065 {
00066 XmlDocumentPtr doc = XmlDocument::ParseXml(_xpathTestXml);
00067
00068 XPath xpath("/");
00069
00070 DEBUG_CLEAR_MEM_CHECK_POINTS();
00071 xpath.CheckMem();
00072 doc.CheckMem();
00073 DEBUG_DUMP_MEM_LEAKS();
00074 UNIT_ASSERT_MEM_NOTED("Xpath / 1");
00075
00076 XmlNodePtr root = xpath.SelectSingleNode(doc);
00077 UNIT_ASSERT("Shouldn't be null", root.IsNotNull());
00078 UNIT_ASSERT("/ == docuemnt", root->IsDocument());
00079
00080 DEBUG_CLEAR_MEM_CHECK_POINTS();
00081 xpath.CheckMem();
00082 doc.CheckMem();
00083 root.CheckMem();
00084 DEBUG_DUMP_MEM_LEAKS();
00085 UNIT_ASSERT_MEM_NOTED("Xpath / 2");
00086
00087 Log::SWriteOkFail( "XPath /" );
00088 }
00089
00090 static void _TestXPathRootChild()
00091 {
00092 XmlDocumentPtr doc = XmlDocument::ParseXml(_xpathTestXml);
00093
00094 XPath xpath("/bookstore");
00095
00096 DEBUG_CLEAR_MEM_CHECK_POINTS();
00097 xpath.CheckMem();
00098 doc.CheckMem();
00099 DEBUG_DUMP_MEM_LEAKS();
00100 UNIT_ASSERT_MEM_NOTED("Xpath /bookstore 1");
00101
00102 XmlNodePtr root = xpath.SelectSingleNode(doc);
00103 UNIT_ASSERT("Shouldn't be null", root.IsNotNull());
00104 UNIT_ASSERT("/bookstore == bookstore", root->Name() == "bookstore");
00105
00106 xpath = XPath("bookstore");
00107 root = xpath.SelectSingleNode(doc);
00108 UNIT_ASSERT("Shouldn't be null", root.IsNotNull());
00109 UNIT_ASSERT("/bookstore == bookstore", root->Name() == "bookstore");
00110
00111 DEBUG_CLEAR_MEM_CHECK_POINTS();
00112 xpath.CheckMem();
00113 doc.CheckMem();
00114 root.CheckMem();
00115 DEBUG_DUMP_MEM_LEAKS();
00116 UNIT_ASSERT_MEM_NOTED("Xpath /bookstore 2");
00117
00118 Log::SWriteOkFail( "XPath /bookstore" );
00119 }
00120
00121 static void _TestXPathRootPath()
00122 {
00123 XmlDocumentPtr doc = XmlDocument::ParseXml(_xpathTestXml);
00124
00125 XPath xpath("/bookstore/book");
00126
00127 DEBUG_CLEAR_MEM_CHECK_POINTS();
00128 xpath.CheckMem();
00129 doc.CheckMem();
00130 DEBUG_DUMP_MEM_LEAKS();
00131 UNIT_ASSERT_MEM_NOTED("Xpath /bookstore/book 1");
00132
00133 XmlNodeListPtr root = xpath.SelectNodes(doc);
00134 UNIT_ASSERT("Shouldn't be null", root.IsNotNull());
00135 UNIT_ASSERT("Should be 4 nodes", root->Count() == 4);
00136 int x;
00137 for (x = 0; x < root->Count(); x++)
00138 {
00139 UNIT_ASSERT("tag name", root->Item(x)->Name() == "book");
00140 }
00141
00142 xpath = XPath("/bookstore/*");
00143 root = xpath.SelectNodes(doc);
00144 UNIT_ASSERT("Shouldn't be null", root.IsNotNull());
00145 UNIT_ASSERT("Should be 4 nodes", root->Count() == 4);
00146 for (x = 0; x < root->Count(); x++)
00147 {
00148 UNIT_ASSERT("tag name", root->Item(x)->Name() == "book");
00149 }
00150
00151 DEBUG_CLEAR_MEM_CHECK_POINTS();
00152 xpath.CheckMem();
00153 doc.CheckMem();
00154 root.CheckMem();
00155 DEBUG_DUMP_MEM_LEAKS();
00156 UNIT_ASSERT_MEM_NOTED("Xpath /bookstore/book 2");
00157
00158 Log::SWriteOkFail( "XPath /bookstore/book" );
00159 }
00160
00161 static void _TestXPathRootPathSearch()
00162 {
00163 XmlDocumentPtr doc = XmlDocument::ParseXml(_xpathTestXml);
00164
00165 XPath xpath("/bookstore//price");
00166
00167 DEBUG_CLEAR_MEM_CHECK_POINTS();
00168 xpath.CheckMem();
00169 doc.CheckMem();
00170 DEBUG_DUMP_MEM_LEAKS();
00171 UNIT_ASSERT_MEM_NOTED("Xpath /bookstore//price 1");
00172
00173 XmlNodeListPtr root = xpath.SelectNodes(doc);
00174 UNIT_ASSERT("Shouldn't be null", root.IsNotNull());
00175 UNIT_ASSERT("Should be 4 nodes", root->Count() == 4);
00176 for (int x = 0; x < root->Count(); x++)
00177 {
00178 UNIT_ASSERT("tag name", root->Item(x)->Name() == "price");
00179 }
00180
00181 DEBUG_CLEAR_MEM_CHECK_POINTS();
00182 xpath.CheckMem();
00183 doc.CheckMem();
00184 root.CheckMem();
00185 DEBUG_DUMP_MEM_LEAKS();
00186 UNIT_ASSERT_MEM_NOTED("Xpath /bookstore//price 2");
00187
00188 Log::SWriteOkFail( "XPath /bookstore//price" );
00189 }
00190
00191 static void _TestXPathAttribPath()
00192 {
00193 XmlDocumentPtr doc = XmlDocument::ParseXml(_xpathTestXml);
00194
00195 XPath xpath("/bookstore/book/title@lang");
00196
00197 DEBUG_CLEAR_MEM_CHECK_POINTS();
00198 xpath.CheckMem();
00199 doc.CheckMem();
00200 DEBUG_DUMP_MEM_LEAKS();
00201 UNIT_ASSERT_MEM_NOTED("Xpath /bookstore/book 1");
00202
00203 XmlNodeListPtr root = xpath.SelectNodes(doc);
00204 UNIT_ASSERT("Shouldn't be null", root.IsNotNull());
00205 UNIT_ASSERT("Should be 4 nodes", root->Count() == 4);
00206 int x;
00207 for (x = 0; x < root->Count(); x++)
00208 {
00209 UNIT_ASSERT("tag name", root->Item(x)->Name() == "lang");
00210 }
00211
00212 xpath = XPath("/bookstore/@");
00213 root = xpath.SelectNodes(doc);
00214 UNIT_ASSERT("Shouldn't be null", root.IsNotNull());
00215 UNIT_ASSERT("Should be 4 nodes", root->Count() == 4);
00216 for (x = 0; x < root->Count(); x++)
00217 {
00218 UNIT_ASSERT("tag name", root->Item(x)->Name() == "category");
00219 }
00220
00221 DEBUG_CLEAR_MEM_CHECK_POINTS();
00222 xpath.CheckMem();
00223 doc.CheckMem();
00224 root.CheckMem();
00225 DEBUG_DUMP_MEM_LEAKS();
00226 UNIT_ASSERT_MEM_NOTED("Xpath /bookstore/book 2");
00227
00228 Log::SWriteOkFail( "XPath /bookstore/book/title@lang" );
00229 }
00230
00231 static void _TestXPathPredicate()
00232 {
00233 XmlDocumentPtr doc = XmlDocument::ParseXml(_xpathTestXml);
00234 XmlNodeListPtr nodes = doc->SelectNodes("/bookstore/book[@category = 'WEB']");
00235
00236 UNIT_ASSERT("Shouldn't be null", nodes.IsNotNull());
00237 UNIT_ASSERT("Should be 2 nodes", nodes->Count() == 2);
00238
00239 for (int x = 0; x < nodes->Count(); x++)
00240 {
00241 UNIT_ASSERT("tag name", nodes->Item(x)->Name() == "book");
00242 XmlElementPtr xn = nodes->Item(x)->ToElement();
00243 XmlAttributePtr an = xn->Attribute("category");
00244 UNIT_ASSERT("tag name", an->Name() == "category");
00245 UNIT_ASSERT("tag name", *an->Value() == "WEB");
00246 }
00247
00248 Log::SWriteOkFail( "XPath /bookstore/book[@category = 'WEB']" );
00249 }
00250
00251 void _TestXPath()
00252 {
00253 _TestXPathRoot();
00254 DEBUG_CLEAR_MEM_CHECK_POINTS();
00255 DEBUG_DUMP_MEM_LEAKS();
00256
00257 _TestXPathRootChild();
00258 DEBUG_CLEAR_MEM_CHECK_POINTS();
00259 DEBUG_DUMP_MEM_LEAKS();
00260
00261 _TestXPathRootPath();
00262 DEBUG_CLEAR_MEM_CHECK_POINTS();
00263 DEBUG_DUMP_MEM_LEAKS();
00264
00265 _TestXPathRootPathSearch();
00266 DEBUG_CLEAR_MEM_CHECK_POINTS();
00267 DEBUG_DUMP_MEM_LEAKS();
00268
00269 _TestXPathAttribPath();
00270 DEBUG_CLEAR_MEM_CHECK_POINTS();
00271 DEBUG_DUMP_MEM_LEAKS();
00272
00273 _TestXPathPredicate();
00274 DEBUG_CLEAR_MEM_CHECK_POINTS();
00275 DEBUG_DUMP_MEM_LEAKS();
00276 }
00277
00278 #endif