efcaa31f497ca852fe51c831e3cb26c138fb8ae4
[platform/core/test/security-tests.git] / src / framework / src / test_results_collector_summary.cpp
1 /*
2  * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        test_results_collector_summary.cpp
18  * @author      Marcin Niesluchowski (m.niesluchow@samsung.com)
19  * @author      Michal Witanowski (m.witanowski@samsung.com)
20  * @version     1.0
21  * @brief       Source file containing SummaryCollector class definition.
22  */
23
24 #include <dpl/availability.h>
25 #include <dpl/log/log.h>
26 #include <dpl/test/test_results_collector_commons.h>
27
28 #include "dpl/test/test_results_collector_summary.h"
29
30 namespace DPL {
31 namespace Test {
32
33 namespace {
34
35 const std::string DEFAULT_SUMMARY_FILE_NAME = "summary.txt";
36
37 }
38
39 SummaryCollector::SummaryCollector()
40     : m_filename(DEFAULT_SUMMARY_FILE_NAME)
41 {
42 }
43
44 TestResultsCollectorBase* SummaryCollector::Constructor()
45 {
46     return new SummaryCollector();
47 }
48
49 std::string SummaryCollector::CollectorSpecificHelp() const
50 {
51     return CollectorFileHelp(DEFAULT_SUMMARY_FILE_NAME);
52 }
53
54 bool SummaryCollector::ParseCollectorSpecificArg(const std::string& arg)
55 {
56     return ParseCollectorFileArg(arg, m_filename);
57 }
58
59 void SummaryCollector::Start()
60 {
61     writeStats(true);
62 }
63
64 void SummaryCollector::CollectResult(const std::string& id, const TestResult &result)
65 {
66     DPL_UNUSED_PARAM(id);
67
68     m_stats.AddTest(result.GetFailStatus());
69     writeStats(true);
70 }
71
72 void SummaryCollector::Finish()
73 {
74     writeStats(false);
75 }
76
77 void SummaryCollector::writeStats(bool segfault)
78 {
79     m_output.open(m_filename.c_str(), std::ofstream::out | std::ofstream::trunc);
80     if (!m_output) {
81         LogPedantic("Could not open file " << m_filename << " for writing");
82         return;
83     }
84     m_output << m_stats.GetTotal() << ' '
85              << m_stats.GetPassed() << ' '
86              << m_stats.GetFailed() << ' '
87              << m_stats.GetIgnored() << (segfault ? " segfault" : "");
88     if (!m_output)
89         LogPedantic("Writing to " << m_filename << " file failed");
90     m_output.close();
91     if (!m_output)
92         LogPedantic("Failed to close " << m_filename << " file");
93 }
94
95 } // namespace Test
96 } // namespace DPL