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.

uoXMLWriterTest.cpp 5.2KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <fstream>
  23. #include <cstdio>
  24. #include <xml/IWriter.h>
  25. #include <xml/IXMLHandler.h>
  26. #include <fs/Files.h>
  27. #include <gtest/gtest.h>
  28. #include <boost/filesystem.hpp>
  29. class CWriterCallBack : public XML::IWriterCallBack
  30. {
  31. public:
  32. CWriterCallBack(const char* filename) { m_file = FS::Files::open(filename, "wb"); }
  33. ~CWriterCallBack() override { if (m_file) { std::fclose(m_file); } } // in case release is not called
  34. void write(const char* outputData) override { if (m_file) { std::fputs(outputData, m_file); } }
  35. // necessary thjor the test to close the stream and re-open it for inspection
  36. void release()
  37. {
  38. std::fclose(m_file);
  39. m_file = nullptr;
  40. }
  41. private:
  42. std::FILE* m_file{ nullptr };
  43. };
  44. TEST(XML_Writer_Test_Case, validateWriter)
  45. {
  46. std::string expectedFile = DATA_DIR "/ref_data.xml";
  47. std::string outputFile = TEMP_DIR "/uoXMLWriterTest.xml";
  48. FS::Files::createPath(TEMP_DIR);
  49. ASSERT_TRUE(FS::Files::directoryExists(TEMP_DIR));
  50. FS::Files::removeAll(outputFile.c_str());
  51. ASSERT_FALSE(FS::Files::fileExists(outputFile.c_str()));
  52. // The test serializes a known xml sequence and compares the output
  53. // to a reference.
  54. // serializing
  55. CWriterCallBack callback(outputFile.c_str());
  56. XML::IWriter* writer = createWriter(callback);
  57. writer->openChild("Document"); ///< Document Node
  58. writer->setAttribute("name", "test_reference");
  59. writer->openChild("NodeWithChildren"); ///< NodeWithChildren Node
  60. writer->openChild("ChildNodeWithData");
  61. writer->setChildData("child node data with more than 10 alphanumeric characters");
  62. writer->closeChild();
  63. writer->openChild("ChildNodeEmpty");
  64. writer->closeChild();
  65. writer->openChild("ChildNodeWithChildren");
  66. writer->openChild("ChildNodeEmpty");
  67. writer->closeChild();
  68. writer->closeChild();
  69. writer->closeChild(); ///< NodeWithChildren END
  70. writer->openChild("NodeWithData");
  71. writer->setAttribute("status", "hasData");
  72. writer->setChildData("node data with special characters <>,;:!?./&\"'(-_)=~#{[|`\\^@]}/*-+");
  73. writer->closeChild();
  74. writer->openChild("NodeEmptyWithNumber666");
  75. writer->setAttribute("status", "noData");
  76. writer->setAttribute("ref", "test");
  77. writer->closeChild();
  78. writer->closeChild(); ///< Document Node END
  79. writer->release();
  80. callback.release();
  81. // comparison part
  82. std::ifstream generatedStream;
  83. std::ifstream expectedStream;
  84. FS::Files::openIFStream(generatedStream, outputFile.c_str());
  85. FS::Files::openIFStream(expectedStream, expectedFile.c_str());
  86. ASSERT_TRUE(generatedStream.is_open());
  87. ASSERT_TRUE(expectedStream.is_open());
  88. std::string generatedString;
  89. std::string expectedString;
  90. while (std::getline(expectedStream, expectedString))
  91. {
  92. std::getline(generatedStream, generatedString);
  93. ASSERT_EQ(expectedString, generatedString);
  94. }
  95. // last check to verify the expected file has no additional line
  96. std::getline(generatedStream, generatedString);
  97. }
  98. TEST(XML_Writer_Test_Case, validateHandlerWriteToJapanesePath)
  99. {
  100. std::string expectedFile = DATA_DIR "/日本語/ref_data_jp.xml";
  101. std::string outputFile = TEMP_DIR "/オッペンヴィベ/日本語.xml";
  102. FS::Files::createPath(TEMP_DIR);
  103. ASSERT_TRUE(FS::Files::directoryExists(TEMP_DIR));
  104. FS::Files::removeAll(outputFile.c_str());
  105. ASSERT_FALSE(FS::Files::fileExists(outputFile.c_str()));
  106. XML::IXMLHandler* xmlHandler = XML::createXMLHandler();
  107. std::string testData = "<Document name=\"日本語\"><Node>日本語 1</Node><Node>日本語 2</Node><Node>日本語 3</Node></Document>";
  108. XML::IXMLNode* rootNode = xmlHandler->parseString(testData.c_str(), testData.size());
  109. xmlHandler->writeXMLInFile(*rootNode, outputFile.c_str());
  110. // comparison part
  111. std::ifstream generatedStream;
  112. std::ifstream expectedStream;
  113. FS::Files::openIFStream(generatedStream, outputFile.c_str());
  114. FS::Files::openIFStream(expectedStream, expectedFile.c_str());
  115. ASSERT_TRUE(generatedStream.is_open());
  116. ASSERT_TRUE(expectedStream.is_open());
  117. std::string generatedString;
  118. std::string expectedString;
  119. while (std::getline(expectedStream, expectedString))
  120. {
  121. std::getline(generatedStream, generatedString);
  122. ASSERT_EQ(expectedString, generatedString);
  123. }
  124. // last check to verify the expected file has no additional line
  125. std::getline(generatedStream, generatedString);
  126. }
  127. int uoXMLWriterTest(int argc, char* argv[])
  128. {
  129. testing::InitGoogleTest(&argc, argv);
  130. ::testing::GTEST_FLAG(filter) = "XML_Writer_Test_Case.*";
  131. return RUN_ALL_TESTS();
  132. }