/test/unit/testXMLUtil.cpp

https://github.com/deltaforge/nebu-common-cpp · C++ · 253 lines · 205 code · 45 blank · 3 comment · 0 complexity · 212f4af65a7427eeb20b4c08dc859bed MD5 · raw file

  1. #include "gtest/gtest.h"
  2. #include "gmock/gmock.h"
  3. #include "tinyxml2.h"
  4. #include "nebu/util/exceptions.h"
  5. #include "nebu/util/xmlUtil.h"
  6. // Using declarations - tinyxml2
  7. using tinyxml2::XMLAttribute;
  8. using tinyxml2::XMLDocument;
  9. using tinyxml2::XMLElement;
  10. // Using declarations - nebu-common
  11. using nebu::common::XMLUtil;
  12. using nebu::common::XMLParseException;
  13. // Using declarations - gtest/gmock
  14. using testing::Eq;
  15. using testing::IsNull;
  16. using testing::NotNull;
  17. using testing::StrEq;
  18. TEST(XMLUtilTest, testParseDocumentFromStringWithEmptyString) {
  19. try {
  20. XMLDocument doc;
  21. XMLUtil::parseDocumentFromString(doc, "");
  22. FAIL() << "Expected XMLParseException";
  23. } catch (XMLParseException &expected) {
  24. SUCCEED();
  25. }
  26. }
  27. TEST(XMLUtilTest, testParseDocumentFromStringWithInvalidXML) {
  28. try {
  29. XMLDocument doc;
  30. XMLUtil::parseDocumentFromString(doc, "<element>Missing end tag");
  31. FAIL() << "Expected XMLParseException";
  32. } catch (XMLParseException &expected) {
  33. SUCCEED();
  34. }
  35. }
  36. TEST(XMLUtilTest, testParseDocumentFromStringWithValidXML) {
  37. XMLDocument doc;
  38. XMLUtil::parseDocumentFromString(doc, "<rootElement />");
  39. EXPECT_THAT(doc.Error(), Eq(false));
  40. }
  41. TEST(XMLUtilTest, testFindChildElementOfDocForNonExistingNameNonMandatory) {
  42. XMLDocument doc;
  43. XMLUtil::parseDocumentFromString(doc, "<rootElement />");
  44. const XMLElement *childElement = XMLUtil::findChildElement(doc, "nonExistingChild");
  45. EXPECT_THAT(childElement, IsNull());
  46. }
  47. TEST(XMLUtilTest, testFindChildElementOfDocForNonExistingNameMandatory) {
  48. XMLDocument doc;
  49. XMLUtil::parseDocumentFromString(doc, "<rootElement />");
  50. try {
  51. XMLUtil::findChildElement(doc, "nonExistingChild", true);
  52. FAIL() << "Expected XMLParseException";
  53. } catch (XMLParseException &expected) {
  54. SUCCEED();
  55. }
  56. }
  57. TEST(XMLUtilTest, testFindChildElementOfDocForExistingName) {
  58. XMLDocument doc;
  59. XMLUtil::parseDocumentFromString(doc, "<rootElement />");
  60. const XMLElement *childElement = XMLUtil::findChildElement(doc, "rootElement", true);
  61. EXPECT_THAT(childElement, NotNull());
  62. EXPECT_THAT(childElement->Name(), StrEq("rootElement"));
  63. }
  64. TEST(XMLUtilTest, testFindChildElementOfNullElem) {
  65. try {
  66. XMLUtil::findChildElement(NULL, "nonExistentElement");
  67. FAIL() << "Expected XMLParseException";
  68. } catch (XMLParseException &expected) {
  69. SUCCEED();
  70. }
  71. }
  72. TEST(XMLUtilTest, testFindChildElementOfElemForNonExistingNameNonMandatory) {
  73. XMLDocument doc;
  74. XMLUtil::parseDocumentFromString(doc, "<rootElement><childElement /></rootElement>");
  75. const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
  76. const XMLElement *childElement = XMLUtil::findChildElement(rootElement, "nonExistentElement");
  77. EXPECT_THAT(childElement, IsNull());
  78. }
  79. TEST(XMLUtilTest, testFindChildElementOfElemForNonExistingNameMandatory) {
  80. XMLDocument doc;
  81. XMLUtil::parseDocumentFromString(doc, "<rootElement><childElement /></rootElement>");
  82. const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
  83. try {
  84. XMLUtil::findChildElement(rootElement, "nonExistentElement", true);
  85. FAIL() << "Expected XMLParseException";
  86. } catch (XMLParseException &expected) {
  87. SUCCEED();
  88. }
  89. }
  90. TEST(XMLUtilTest, testFindChildElementOfElemForExistingName) {
  91. XMLDocument doc;
  92. XMLUtil::parseDocumentFromString(doc, "<rootElement><childElement /></rootElement>");
  93. const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
  94. const XMLElement *childElement = XMLUtil::findChildElement(rootElement, "childElement", true);
  95. EXPECT_THAT(childElement, NotNull());
  96. EXPECT_THAT(childElement->Name(), StrEq("childElement"));
  97. }
  98. TEST(XMLUtilTest, testFindNextSiblingForNullElem) {
  99. try {
  100. XMLUtil::findNextSibling(NULL, "nonExistentElement");
  101. FAIL() << "Expected XMLParseException";
  102. } catch (XMLParseException &expected) {
  103. SUCCEED();
  104. }
  105. }
  106. TEST(XMLUtilTest, testFindNextSiblingForNonExistingNameNonMandatory) {
  107. XMLDocument doc;
  108. XMLUtil::parseDocumentFromString(doc, "<rootElement /><siblingElement />");
  109. const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
  110. const XMLElement *siblingElement = XMLUtil::findNextSibling(rootElement, "nonExistentElement");
  111. EXPECT_THAT(siblingElement, IsNull());
  112. }
  113. TEST(XMLUtilTest, testFindNextSiblingForNonExistingNameMandatory) {
  114. XMLDocument doc;
  115. XMLUtil::parseDocumentFromString(doc, "<rootElement /><siblingElement />");
  116. const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
  117. try {
  118. XMLUtil::findNextSibling(rootElement, "nonExistentElement", true);
  119. FAIL() << "Expected XMLParseException";
  120. } catch (XMLParseException &expected) {
  121. SUCCEED();
  122. }
  123. }
  124. TEST(XMLUtilTest, testFindNextSiblingForExistingName) {
  125. XMLDocument doc;
  126. XMLUtil::parseDocumentFromString(doc, "<rootElement /><siblingElement />");
  127. const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
  128. const XMLElement *siblingElement = XMLUtil::findNextSibling(rootElement, "siblingElement", true);
  129. EXPECT_THAT(siblingElement, NotNull());
  130. EXPECT_THAT(siblingElement->Name(), StrEq("siblingElement"));
  131. }
  132. TEST(XMLUtilTest, testFindNextSiblingChain) {
  133. XMLDocument doc;
  134. XMLUtil::parseDocumentFromString(doc, "<rootElement /><siblingElement /><siblingElement />");
  135. const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
  136. const XMLElement *siblingElementA = XMLUtil::findNextSibling(rootElement, "siblingElement", true);
  137. EXPECT_THAT(siblingElementA, NotNull());
  138. EXPECT_THAT(siblingElementA->Name(), StrEq("siblingElement"));
  139. const XMLElement *siblingElementB = XMLUtil::findNextSibling(siblingElementA, "siblingElement", true);
  140. EXPECT_THAT(siblingElementB, NotNull());
  141. EXPECT_THAT(siblingElementB->Name(), StrEq("siblingElement"));
  142. const XMLElement *siblingElementC = XMLUtil::findNextSibling(siblingElementB, "siblingElement");
  143. EXPECT_THAT(siblingElementC, IsNull());
  144. }
  145. TEST(XMLUtilTest, testFindAttributeOfNullElem) {
  146. try {
  147. XMLUtil::findAttribute(NULL, "nonExistentAttribute");
  148. FAIL() << "Expected XMLParseException";
  149. } catch (XMLParseException &expected) {
  150. SUCCEED();
  151. }
  152. }
  153. TEST(XMLUtilTest, testFindAttributeForNonExistingNameNonMandatory) {
  154. XMLDocument doc;
  155. XMLUtil::parseDocumentFromString(doc, "<rootElement attributeA='An attribute'/>");
  156. const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
  157. const XMLAttribute *attribute = XMLUtil::findAttribute(rootElement, "nonExistentAttribute");
  158. EXPECT_THAT(attribute, IsNull());
  159. }
  160. TEST(XMLUtilTest, testFindAttributeForNonExistingNameMandatory) {
  161. XMLDocument doc;
  162. XMLUtil::parseDocumentFromString(doc, "<rootElement attributeA='An attribute' />");
  163. const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
  164. try {
  165. XMLUtil::findAttribute(rootElement, "nonExistentAttribute", true);
  166. FAIL() << "Expected XMLParseException";
  167. } catch (XMLParseException &expected) {
  168. SUCCEED();
  169. }
  170. }
  171. TEST(XMLUtilTest, testFindAttributeForExistingName) {
  172. XMLDocument doc;
  173. XMLUtil::parseDocumentFromString(doc, "<rootElement attributeA='An attribute' />");
  174. const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
  175. const XMLAttribute *attribute = XMLUtil::findAttribute(rootElement, "attributeA", true);
  176. EXPECT_THAT(attribute, NotNull());
  177. EXPECT_THAT(attribute->Name(), StrEq("attributeA"));
  178. EXPECT_THAT(attribute->Value(), StrEq("An attribute"));
  179. }
  180. TEST(XMLUtilTest, testVerifyElementNameOfNullElem) {
  181. try {
  182. XMLUtil::verifyElementName(NULL, "incorrectName");
  183. FAIL() << "Expected XMLParseException";
  184. } catch (XMLParseException &expected) {
  185. SUCCEED();
  186. }
  187. }
  188. TEST(XMLUtilTest, testVerifyElementNameWithIncorrectName) {
  189. XMLDocument doc;
  190. XMLUtil::parseDocumentFromString(doc, "<rootElement />");
  191. const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
  192. try {
  193. XMLUtil::verifyElementName(rootElement, "incorrectName");
  194. FAIL() << "Expected XMLParseException";
  195. } catch (XMLParseException &expected) {
  196. SUCCEED();
  197. }
  198. }
  199. TEST(XMLUtilTest, testVerifyElementNameWithCorrectName) {
  200. XMLDocument doc;
  201. XMLUtil::parseDocumentFromString(doc, "<rootElement />");
  202. const XMLElement *rootElement = XMLUtil::findChildElement(doc, "rootElement", true);
  203. XMLUtil::verifyElementName(rootElement, "rootElement");
  204. }
  205. int main(int argc, char **argv) {
  206. testing::InitGoogleMock(&argc, argv);
  207. return RUN_ALL_TESTS();
  208. }