1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Test Executor
3 * ------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Batch result to JUnit report conversion tool.
22 *//*--------------------------------------------------------------------*/
24 #include "xeTestLogParser.hpp"
25 #include "xeTestResultParser.hpp"
26 #include "xeXMLWriter.hpp"
27 #include "deFilePath.hpp"
29 #include "deStringUtil.hpp"
47 std::string batchResultFile;
48 std::string outputFile;
51 static void printHelp (const char* binName)
53 printf("%s: [testlog] [output file]\n", binName);
56 static void parseCommandLine (CommandLine& cmdLine, int argc, const char* const* argv)
59 throw xe::Error("Expected input and output paths");
61 cmdLine.batchResultFile = argv[argc-2];
62 cmdLine.outputFile = argv[argc-1];
65 static void parseBatchResult (xe::TestLogParser& parser, const char* filename)
67 std::ifstream in (filename, std::ios_base::binary);
72 in.read((char*)&buf[0], sizeof(buf));
73 int numRead = (int)in.gcount();
76 parser.parse(&buf[0], numRead);
78 if (numRead < (int)sizeof(buf))
83 class ResultToJUnitHandler : public xe::TestLogHandler
86 ResultToJUnitHandler (xe::xml::Writer& writer)
91 void setSessionInfo (const xe::SessionInfo&)
95 xe::TestCaseResultPtr startTestCaseResult (const char* casePath)
97 return xe::TestCaseResultPtr(new xe::TestCaseResultData(casePath));
100 void testCaseResultUpdated (const xe::TestCaseResultPtr&)
104 void testCaseResultComplete (const xe::TestCaseResultPtr& resultData)
106 using xe::xml::Writer;
108 xe::TestCaseResult result;
110 xe::parseTestCaseResultFromData(&m_resultParser, &result, *resultData.get());
112 // Split group and case names.
113 size_t sepPos = result.casePath.find_last_of('.');
114 std::string caseName = result.casePath.substr(sepPos+1);
115 std::string groupName = result.casePath.substr(0, sepPos);
118 m_writer << Writer::BeginElement("testcase")
119 << Writer::Attribute("name", caseName)
120 << Writer::Attribute("classname", groupName);
122 if (result.statusCode != xe::TESTSTATUSCODE_PASS)
123 m_writer << Writer::BeginElement("failure")
124 << Writer::Attribute("type", xe::getTestStatusCodeName(result.statusCode))
125 << result.statusDetails
126 << Writer::EndElement;
128 m_writer << Writer::EndElement;
132 xe::xml::Writer& m_writer;
133 xe::TestResultParser m_resultParser;
136 static void batchResultToJUnitReport (const char* batchResultFilename, const char* dstFileName)
138 std::ofstream out (dstFileName, std::ios_base::binary);
139 xe::xml::Writer writer (out);
140 ResultToJUnitHandler handler (writer);
141 xe::TestLogParser parser (&handler);
143 XE_CHECK(out.good());
145 out << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
147 writer << xe::xml::Writer::BeginElement("testsuites")
148 << xe::xml::Writer::BeginElement("testsuite");
150 // Parse and write individual cases
151 parseBatchResult(parser, batchResultFilename);
153 writer << xe::xml::Writer::EndElement << xe::xml::Writer::EndElement;
156 int main (int argc, const char* const* argv)
161 parseCommandLine(cmdLine, argc, argv);
163 catch (const std::exception&)
171 batchResultToJUnitReport(cmdLine.batchResultFile.c_str(), cmdLine.outputFile.c_str());
173 catch (const std::exception& e)
175 printf("%s\n", e.what());