ChimeraTK-ControlSystemAdapter-OPCUAAdapter  04.00.01
test_xml_handler.cpp
Go to the documentation of this file.
1 #include "xml_file_handler.h"
2 
3 #include <boost/algorithm/string.hpp>
4 #include <boost/test/included/unit_test.hpp>
5 
6 using namespace boost::unit_test_framework;
7 using namespace std;
8 using namespace ChimeraTK;
9 
11  public:
12  static void readDocFile();
13  static void getContent();
14 };
15 
17  std::cout << "Enter XMLFileHandlerTest - readDocFile" << std::endl;
18  BOOST_CHECK_NO_THROW(xml_file_handler("uamapping_test_1.xml"));
19 
20  // Emtpy path
21  BOOST_CHECK_THROW(xml_file_handler(""), std::logic_error);
22  // Set a not wellformed document
23  BOOST_CHECK_THROW(xml_file_handler("uamapping_test_notwellformed.xml"), std::logic_error);
24 
25  // Set a document
26  xml_file_handler xmlHandlerTwo("uamapping_test_2.xml");
27  BOOST_CHECK(xmlHandlerTwo.isDocSetted() == true);
28  BOOST_CHECK(xmlHandlerTwo.getNodeSet("//process_variable") != NULL);
29 }
30 
32  std::cout << "Enter XMLFileHandlerTest - getContent" << std::endl;
33  xml_file_handler xmlHandler("uamapping_test_1.xml");
34 
35  xmlXPathObjectPtr result = xmlHandler.getNodeSet(
36  //"//application[@name='TestCaseForXMLFileHandlerTest::getContent']//map");
37  "//process_variable[@sourceName='int8Array__s15']");
38  BOOST_CHECK(result != NULL);
39  xmlNodeSetPtr nodeset = NULL;
40 
41  if(result) {
42  nodeset = result->nodesetval;
43 
44  // Check if Tag <map> contain a Attribute "sourceVariableName" with value
45  string sourceVariableName = xmlHandler.getAttributeValueFromNode(nodeset->nodeTab[0], "sourceName");
46  BOOST_CHECK(sourceVariableName != "");
47 
48  // We want to get a value from a not existing attribute
49  BOOST_CHECK(xmlHandler.getAttributeValueFromNode(nodeset->nodeTab[0], "notExistingAttribute") == "");
50 
51  // Get a nodeList
52  vector<xmlNodePtr> nodeList = xmlHandler.getNodesByName(nodeset->nodeTab[0]->children, "destination");
53  BOOST_CHECK(nodeList.size() == 1);
54 
55  // Get a content from a tag element like: <tag>content</tag>
56  string unrollPath = xmlHandler.getContentFromNode(nodeList[0]);
57  BOOST_CHECK(unrollPath == "TestCaseForXMLFileHandlerTest::getContent/NorthSideLINAC/partB");
58 
59  // We want to get a conten from a not existing node
60  string emptyContent = xmlHandler.getContentFromNode(NULL);
61  BOOST_CHECK(emptyContent == "");
62 
63  vector<string> path =
64  xmlHandler.parseVariablePath(sourceVariableName, xmlHandler.getAttributeValueFromNode(nodeList[0], "name"));
65  BOOST_CHECK(path.size() == 1);
66  }
67  else {
68  BOOST_CHECK(false);
69  }
70 
71  // We want to get an nodeset from a node which not exist
72  BOOST_CHECK(xmlHandler.getNodeSet("//test") == NULL);
73 }
74 
78 class XMLFileHandlerTestSuite : public test_suite {
79  public:
80  XMLFileHandlerTestSuite() : test_suite("XMLFileHandler Test Suite") {
81  add(BOOST_TEST_CASE(&XMLFileHandlerTest::readDocFile));
82  add(BOOST_TEST_CASE(&XMLFileHandlerTest::getContent));
83  }
84 };
85 
86 test_suite* init_unit_test_suite(int /*argc*/, char** /*argv[]*/) {
87  framework::master_test_suite().add(new XMLFileHandlerTestSuite);
88  return 0;
89 }
ChimeraTK::xml_file_handler::getNodeSet
xmlXPathObjectPtr getNodeSet(const std::string &xPathString)
This methode return a pointer of a xPath element depending of the given xPathString.
Definition: xml_file_handler.cpp:56
init_unit_test_suite
test_suite * init_unit_test_suite(int, char **)
Definition: test_xml_handler.cpp:86
XMLFileHandlerTestSuite::XMLFileHandlerTestSuite
XMLFileHandlerTestSuite()
Definition: test_xml_handler.cpp:80
ChimeraTK::xml_file_handler
This class support any file interaction with a xml file.
Definition: xml_file_handler.h:44
xml_file_handler.h
ChimeraTK::xml_file_handler::parseVariablePath
static std::vector< std::string > parseVariablePath(const std::string &variablePath, const std::string &seperator="/")
This methode splitt a given string bey the given seperators.
Definition: xml_file_handler.cpp:110
XMLFileHandlerTestSuite
The boost test suite.
Definition: test_xml_handler.cpp:78
ChimeraTK::xml_file_handler::getNodesByName
static std::vector< xmlNodePtr > getNodesByName(xmlNodePtr startNode, const std::string &nodeName)
This methode return a list of all nodes with the given name nodeName starting by the given startNode.
Definition: xml_file_handler.cpp:44
XMLFileHandlerTest::getContent
static void getContent()
Definition: test_xml_handler.cpp:31
ChimeraTK::xml_file_handler::getContentFromNode
static std::string getContentFromNode(xmlNode *node)
This methode returns the value between a xml tag.
Definition: xml_file_handler.cpp:131
XMLFileHandlerTest::readDocFile
static void readDocFile()
Definition: test_xml_handler.cpp:16
XMLFileHandlerTest
Definition: test_xml_handler.cpp:10
ChimeraTK
Definition: csa_additionalvariable.h:28
ChimeraTK::xml_file_handler::getAttributeValueFromNode
static std::string getAttributeValueFromNode(xmlNode *node, const std::string &attributeName)
This methode returns a value of the given attribute from the given node you want to know.
Definition: xml_file_handler.cpp:122
ChimeraTK::xml_file_handler::isDocSetted
bool isDocSetted()
This Methode check if a document is currently setted.
Definition: xml_file_handler.cpp:87