You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uoXMLReaderTest.cpp 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*********************************************************************
  2. * Software License Agreement (AGPL-3 License)
  3. *
  4. * OpenViBE SDK Test Software
  5. * Based on OpenViBE V1.1.0, Copyright (C) Inria, 2006-2015
  6. * Copyright (C) Inria, 2015-2017,V1.0
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program.
  19. * If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <iostream>
  22. #include <iomanip>
  23. #include <vector>
  24. #include <map>
  25. #include <memory>
  26. #include "xml/IReader.h"
  27. #include "xml/IXMLHandler.h"
  28. #include <fs/Files.h>
  29. #include <gtest/gtest.h>
  30. class CReaderCallBack final : public XML::IReaderCallBack
  31. {
  32. public:
  33. struct SNode
  34. {
  35. std::string name;
  36. std::string data;
  37. std::map<std::string, std::string> attributes;
  38. std::vector<std::shared_ptr<SNode>> children;
  39. std::shared_ptr<SNode> parent{ nullptr };
  40. };
  41. std::shared_ptr<SNode> m_CurrentNode{ nullptr };
  42. protected:
  43. void openChild(const char* name, const char** attributeName, const char** attributeValue, const size_t nAttribute) override
  44. {
  45. auto node = std::make_shared<SNode>();
  46. if (m_CurrentNode)
  47. {
  48. node->parent = m_CurrentNode;
  49. m_CurrentNode->children.push_back(node);
  50. }
  51. m_CurrentNode = node;
  52. m_CurrentNode->name = name;
  53. for (size_t i = 0; i < nAttribute; ++i) { m_CurrentNode->attributes[attributeName[i]] = attributeValue[i]; }
  54. }
  55. void processChildData(const char* data) override { m_CurrentNode->data = data; }
  56. void closeChild() override { if (m_CurrentNode->parent) { m_CurrentNode = m_CurrentNode->parent; } }
  57. };
  58. TEST(XML_Reader_Test_Case, validateReader)
  59. {
  60. std::string dataFile = std::string(DATA_DIR) + "/ref_data.xml";
  61. CReaderCallBack callback;
  62. XML::IReader* xmlReader = createReader(callback);
  63. FILE* inputTestDataFile = fopen(dataFile.c_str(), "r");
  64. ASSERT_NE(nullptr, inputTestDataFile);
  65. char dataBuffer[1024];
  66. while (!feof(inputTestDataFile))
  67. {
  68. size_t length = std::fread(dataBuffer, 1, sizeof(dataBuffer), inputTestDataFile);
  69. xmlReader->processData(dataBuffer, length);
  70. }
  71. std::fclose(inputTestDataFile);
  72. xmlReader->release();
  73. // Analyze results
  74. auto rootNode = callback.m_CurrentNode;
  75. // Root node check
  76. ASSERT_EQ("Document", rootNode->name);
  77. ASSERT_EQ(1, rootNode->attributes.size());
  78. ASSERT_EQ("test_reference", rootNode->attributes["name"]);
  79. ASSERT_EQ(3, rootNode->children.size());
  80. auto complexChild = rootNode->children[0];
  81. auto dataChild = rootNode->children[1];
  82. auto emptyChild = rootNode->children[2];
  83. // Simple child with data check
  84. ASSERT_EQ("NodeWithData", dataChild->name);
  85. ASSERT_EQ("node data with special characters <>,;:!?./&\"'(-_)=~#{[|`\\^@]}/*-+", dataChild->data);
  86. ASSERT_EQ(1, dataChild->attributes.size());
  87. ASSERT_EQ("hasData", dataChild->attributes["status"]);
  88. // Simple child with no data and multiple attributes check
  89. ASSERT_EQ("NodeEmptyWithNumber666", emptyChild->name);
  90. ASSERT_EQ(2, emptyChild->attributes.size());
  91. ASSERT_EQ("noData", emptyChild->attributes["status"]);
  92. ASSERT_EQ("test", emptyChild->attributes["ref"]);
  93. // Complex child check
  94. ASSERT_EQ("NodeWithChildren", complexChild->name);
  95. ASSERT_TRUE(complexChild->attributes.empty());
  96. ASSERT_EQ(3, complexChild->children.size());
  97. dataChild = complexChild->children[0];
  98. emptyChild = complexChild->children[1];
  99. complexChild = complexChild->children[2];
  100. ASSERT_EQ("ChildNodeWithData", dataChild->name);
  101. ASSERT_EQ("child node data with more than 10 alphanumeric characters", dataChild->data);
  102. ASSERT_EQ(0, dataChild->attributes.size());
  103. ASSERT_EQ("ChildNodeEmpty", emptyChild->name);
  104. ASSERT_TRUE(emptyChild->attributes.empty());
  105. ASSERT_EQ("ChildNodeWithChildren", complexChild->name);
  106. ASSERT_TRUE(complexChild->attributes.empty());
  107. ASSERT_EQ(1, complexChild->children.size());
  108. }
  109. TEST(XML_Reader_Test_Case, validateHandlerReadJapanese)
  110. {
  111. const std::string dataFile = std::string(DATA_DIR) + "/日本語/ref_data_jp.xml";
  112. XML::IXMLHandler* xmlHandler = XML::createXMLHandler();
  113. XML::IXMLNode* rootNode = xmlHandler->parseFile(dataFile.c_str());
  114. ASSERT_NE(nullptr, rootNode);
  115. ASSERT_EQ(std::string("Document"), rootNode->getName());
  116. ASSERT_TRUE(rootNode->hasAttribute("name"));
  117. ASSERT_EQ(std::string("日本語"), rootNode->getAttribute("name"));
  118. ASSERT_EQ(3, rootNode->getChildCount());
  119. ASSERT_STREQ("日本語 1", rootNode->getChild(0)->getPCData());
  120. xmlHandler->release();
  121. }
  122. TEST(XML_Reader_Test_Case, validateHandlerReadFrench)
  123. {
  124. const std::string dataFile = std::string(DATA_DIR) + "/Français/ref_data_fr.xml";
  125. XML::IXMLHandler* xmlHandler = XML::createXMLHandler();
  126. XML::IXMLNode* rootNode = xmlHandler->parseFile(dataFile.c_str());
  127. ASSERT_NE(nullptr, rootNode);
  128. ASSERT_EQ(std::string("Document"), rootNode->getName());
  129. ASSERT_TRUE(rootNode->hasAttribute("name"));
  130. ASSERT_EQ(std::string("Français"), rootNode->getAttribute("name"));
  131. ASSERT_EQ(3, rootNode->getChildCount());
  132. ASSERT_STREQ("Français 1 (àèáéîôïç)", rootNode->getChild(0)->getPCData());
  133. xmlHandler->release();
  134. }
  135. TEST(XML_Reader_Test_Case, validateHandlerReadNBSP)
  136. {
  137. const std::string dataFile = std::string(DATA_DIR) + "/NB\xC2\xA0SP/ref_data_nbsp.xml";
  138. XML::IXMLHandler* xmlHandler = XML::createXMLHandler();
  139. XML::IXMLNode* rootNode = xmlHandler->parseFile(dataFile.c_str());
  140. ASSERT_NE(nullptr, rootNode);
  141. ASSERT_EQ(std::string("Document"), rootNode->getName());
  142. ASSERT_TRUE(rootNode->hasAttribute("name"));
  143. ASSERT_EQ(std::string("NB\xC2\xA0SP"), rootNode->getAttribute("name"));
  144. ASSERT_EQ(3, rootNode->getChildCount());
  145. ASSERT_STREQ("NB\xC2\xA0SP 1", rootNode->getChild(0)->getPCData());
  146. xmlHandler->release();
  147. }
  148. int uoXMLReaderTest(int argc, char* argv[])
  149. {
  150. testing::InitGoogleTest(&argc, argv);
  151. ::testing::GTEST_FLAG(filter) = "XML_Reader_Test_Case.*";
  152. return RUN_ALL_TESTS();
  153. }