• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef AW_CXX_DISTRIBUTED_UTILS_CSV_TRANSFORM_XML_H_
17 #define AW_CXX_DISTRIBUTED_UTILS_CSV_TRANSFORM_XML_H_
18 
19 #include <iostream>
20 #include <vector>
21 #include <string>
22 #include <cstdio>
23 #include <cstring>
24 #include <fstream>
25 #include <stdlib.h>
26 
27 #define CSV_FILE_PATH "/data/test/"
28 #define XML_FILE_PATH "/data/test/"
29 
30 class CsvTransformXml {
31     std::vector<std::string> csvFiles_;
32     std::vector<std::string> vecLines_;
33     std::string csvFileName_ = "";
34     std::string xmlFileName_ = "";
35     std::string fileName_ = "";
36 
37 public:
38 
CsvTransformXml(std::string targetFile)39     explicit CsvTransformXml(std::string targetFile)
40     {
41         SetFileName(targetFile);
42         SetCvsFileName();
43         SetXmlFileName();
44         FileOperate();
45     }
46 
FileOperate()47     void FileOperate()
48     {
49         std::fstream csvIn = std::fstream(csvFileName_.c_str(), std::ios::in);
50         std::fstream xmlOut = std::fstream(xmlFileName_.c_str(), std::ios::out);
51         if (!xmlOut) {
52             csvIn.close();
53             xmlOut.close();
54             std::cout << "file created fail!" << std::endl;
55             return;
56         }
57         if (!csvIn) {
58             csvIn.close();
59             xmlOut.close();
60             std::cout << "file not exit!" << std::endl;
61             return;
62         }
63         std::string strLine;
64         getline(csvIn, strLine);
65         while (csvIn >> strLine) {
66             SplitString(strLine, vecLines_, ",");
67         }
68         int testCaseSum = vecLines_.size() / 3; // one item case_result from csv includes 3 strings
69         int failCaseSum = 0;
70         for (std::string s : vecLines_) {
71             if (!s.compare("FAILED")) {
72                 failCaseSum++;
73             }
74         }
75         xmlOut << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
76         xmlOut << "<testsuites tests=\""<< testCaseSum <<"\" failures=\""<< failCaseSum
77                << "\" disabled=\"0\" errors=\"\" timestamp=\"\" time=\"\" name=\"AllTests\">"<<std::endl;
78         xmlOut << "  <testsuite name=\""<< fileName_ << "\" tests=\"" << testCaseSum
79                << "\" failures=\""<< failCaseSum << "\" disabled=\"0\" errors=\"0\" time=\"192.553\">" << std::endl;
80         unsigned long i = 0;
81         while (i < vecLines_.size()) {
82             if (!(vecLines_.at(i + 2).compare("FAILED"))) {  // the result of every case intervals 2 string
83                 xmlOut << "    <testcase name=\""<< vecLines_.at(i)
84                        <<"\" status=\"run\" time=\"\" classname=\"" << fileName_ << "\" level=\"3\">" << std::endl;
85                 xmlOut << "      <failure message=\"NULL\"></failure>"<< std::endl;
86                 xmlOut << "    </testcase>" << std::endl;
87             } else {
88                 xmlOut <<"    <testcase name=\""<< vecLines_.at(i) <<"\" status=\"run\" time=\"0\" classname=\""
89                        << fileName_ << "\" level=\"0\" />" << std::endl;
90             }
91             i = i + 3;  // one item case_result from csv includes 3 strings
92         }
93         xmlOut << "  </testsuite>" << std::endl;
94         xmlOut << "</testsuites>" << std::endl;
95         csvIn.close();
96         xmlOut.close();
97     }
98 
SetFileName(std::string targetFile)99     void SetFileName(std::string targetFile)
100     {
101         fileName_ = targetFile;
102     }
103 
SetCvsFileName()104     void SetCvsFileName()
105     {
106         getAllFiles(CSV_FILE_PATH);
107         csvFileName_ = csvFiles_.at(0);
108         std::cout << csvFileName_ << std::endl;
109     }
110 
SetXmlFileName()111     void SetXmlFileName()
112     {
113         std::string string1 = XML_FILE_PATH;
114         string1 += fileName_ + ".xml";
115         xmlFileName_ = string1;
116     }
117 
SplitString(const std::string & s,std::vector<std::string> & v,const std::string & c)118     void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c)
119     {
120         std::string::size_type pos1 = 0;
121         std::string::size_type pos2 = 0;
122         pos2 = s.find(c);
123         pos1 = 0;
124         while (std::string::npos != pos2) {
125             v.push_back(s.substr(pos1, pos2 - pos1));
126             pos1 = pos2 + c.size();
127             pos2 = s.find(c, pos1);
128         }
129         if (pos1 != s.length()) {
130             v.push_back(s.substr(pos1));
131         }
132     }
133 
getAllFiles(std::string path)134     void getAllFiles(std::string path)
135     {
136         std::string p;
137         csvFiles_.push_back(p.assign(path).append("hulutest_result.csv"));
138     }
139 };
140 
141 #endif // AW_CXX_DISTRIBUTED_UTILS_CSV_TRANSFORM_XML_H_
142 
143