Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/test/util/output/LogWriterTest.cc @ 10624

Last change on this file since 10624 was 10624, checked in by landauf, 9 years ago

merged branch core7 back to trunk

  • Property svn:eol-style set to native
File size: 6.8 KB
Line 
1#include <gtest/gtest.h>
2#include "util/Output.h"
3#include "util/output/LogWriter.h"
4#include "util/Convert.h"
5#include "util/output/OutputManager.h"
6#include "util/SharedPtr.h"
7
8namespace orxonox
9{
10    namespace
11    {
12        class MockLogWriter : public LogWriter
13        {
14            public:
15                virtual void printLine(const std::string& line, OutputLevel level)
16                    { this->LogWriter::printLine(line, level); }
17        };
18
19        // Fixture
20        class LogWriterTest : public ::testing::Test
21        {
22            public:
23                virtual void SetUp()
24                {
25                    // reset output manager
26                    OutputManager::Testing::getInstancePointer() = new OutputManager();
27                }
28
29                virtual void TearDown()
30                {
31                }
32        };
33    }
34
35    // test constructor opens file
36    TEST_F(LogWriterTest, ConstructorOpensFile)
37    {
38        LogWriter logWriter;
39        EXPECT_TRUE(logWriter.getFile().is_open());
40    }
41
42    // setLogDirectory changes file, opens correct file
43    bool fileExists(const std::string& path)
44    {
45        std::ifstream stream(path.c_str());
46        bool exists = stream.is_open() && stream.good();
47        stream.close();
48        return exists;
49    }
50
51    std::string getLineWhichContains(const std::string& path, const std::string& message)
52    {
53        std::ifstream file(path.c_str());
54        EXPECT_TRUE(file.is_open());
55        EXPECT_TRUE(file.good());
56        EXPECT_FALSE(file.eof());
57
58        while (file.is_open() && file.good() && !file.eof())
59        {
60            std::string line;
61            std::getline(file, line);
62
63            // see if we find the output and return
64            if (line.find(message) == (line.size() - message.size()))
65                return line;
66        }
67
68        return std::string();
69    }
70
71    bool fileContains(const std::string& path, const std::string& message)
72    {
73        return !getLineWhichContains(path, message).empty();
74    }
75
76    bool deleteFile(const std::string& path)
77    {
78        bool result = std::remove(path.c_str()) == 0;
79        EXPECT_FALSE(fileExists(path));
80        return result;
81    }
82
83    TEST_F(LogWriterTest, SetLogDirectoryOpensNewFile)
84    {
85        std::string path = "./orxonox.log";
86
87        // cleanup before test
88        deleteFile(path);
89
90        {
91            LogWriter logWriter;
92            EXPECT_FALSE(fileExists(path));
93            logWriter.setLogDirectory(".");
94            EXPECT_TRUE(fileExists(path));
95        }
96
97        // cleanup after test
98        EXPECT_TRUE(deleteFile(path));
99    }
100
101    // prints output to logfile
102    TEST_F(LogWriterTest, PrintsOutputToLogfile)
103    {
104        std::string path;
105
106        {
107            // write lines to log file
108            std::vector<std::string> lines;
109            lines.push_back("mytestoutput");
110
111            LogWriter logWriter;
112            logWriter.setLogDirectory(".");
113            logWriter.unfilteredOutput(level::debug_output, context::undefined(), lines);
114
115            path = logWriter.getPath();
116        }
117
118        EXPECT_TRUE(fileContains(path, "mytestoutput"));
119    }
120
121    // prints time to logfile
122    TEST_F(LogWriterTest, PrintsTimestampToLogfile)
123    {
124        std::string path;
125
126        {
127            // write lines to log file
128            std::vector<std::string> lines;
129            lines.push_back("myothertestoutput");
130
131            LogWriter logWriter;
132            logWriter.setLogDirectory(".");
133            logWriter.unfilteredOutput(level::debug_output, context::undefined(), lines);
134
135            path = logWriter.getPath();
136        }
137
138        std::string line = getLineWhichContains(path, "myothertestoutput");
139        EXPECT_FALSE(line.empty());
140        EXPECT_TRUE(isdigit(line[0]));
141        EXPECT_TRUE(isdigit(line[1]));
142        EXPECT_EQ(':', line[2]);
143        EXPECT_TRUE(isdigit(line[3]));
144        EXPECT_TRUE(isdigit(line[4]));
145        EXPECT_EQ(':', line[5]);
146        EXPECT_TRUE(isdigit(line[6]));
147        EXPECT_TRUE(isdigit(line[7]));
148        EXPECT_EQ(' ', line[8]);
149    }
150
151    void deleteAllLogFiles()
152    {
153        std::string path;
154        {
155            LogWriter writer;
156            path = writer.getPath();
157        }
158
159        deleteFile(path);
160        deleteFile(path + ".1");
161        deleteFile(path + ".2");
162        deleteFile(path + ".3");
163        deleteFile(path + ".4");
164        deleteFile(path + ".5");
165        deleteFile(path + ".6");
166        deleteFile(path + ".7");
167        deleteFile(path + ".8");
168        deleteFile(path + ".9");
169    }
170
171    TEST_F(LogWriterTest, ArchivesOldLogFile)
172    {
173        deleteAllLogFiles();
174
175        std::string path;
176
177        {
178            MockLogWriter writer;
179            path = writer.getPath();
180            writer.printLine("test1", level::message);
181        }
182
183        EXPECT_TRUE(fileExists(path));
184        EXPECT_TRUE(fileContains(path, "test1"));
185        EXPECT_FALSE(fileExists(path + ".1"));
186        EXPECT_FALSE(fileExists(path + ".2"));
187        EXPECT_FALSE(fileExists(path + ".3"));
188
189        {
190            MockLogWriter writer;
191            writer.printLine("test2", level::message);
192        }
193
194        EXPECT_TRUE(fileExists(path));
195        EXPECT_TRUE(fileContains(path, "test2"));
196        EXPECT_TRUE(fileExists(path + ".1"));
197        EXPECT_TRUE(fileContains(path + ".1", "test1"));
198        EXPECT_FALSE(fileExists(path + ".2"));
199        EXPECT_FALSE(fileExists(path + ".3"));
200
201        {
202            MockLogWriter writer;
203            writer.printLine("test3", level::message);
204        }
205
206        EXPECT_TRUE(fileExists(path));
207        EXPECT_TRUE(fileContains(path, "test3"));
208        EXPECT_TRUE(fileExists(path + ".1"));
209        EXPECT_TRUE(fileContains(path + ".1", "test2"));
210        EXPECT_TRUE(fileExists(path + ".2"));
211        EXPECT_TRUE(fileContains(path + ".2", "test1"));
212        EXPECT_FALSE(fileExists(path + ".3"));
213    }
214
215    TEST_F(LogWriterTest, ArchivesNineLogFiles)
216    {
217        deleteAllLogFiles();
218
219        std::string path;
220        for (int i = 0; i < 20; ++i)
221        {
222            MockLogWriter writer;
223            path = writer.getPath();
224            writer.printLine("test" + multi_cast<std::string>(i), level::message);
225        }
226
227        EXPECT_TRUE(fileContains(path, "test19"));
228        EXPECT_TRUE(fileContains(path + ".1", "test18"));
229        EXPECT_TRUE(fileContains(path + ".2", "test17"));
230        EXPECT_TRUE(fileContains(path + ".3", "test16"));
231        EXPECT_TRUE(fileContains(path + ".4", "test15"));
232        EXPECT_TRUE(fileContains(path + ".5", "test14"));
233        EXPECT_TRUE(fileContains(path + ".6", "test13"));
234        EXPECT_TRUE(fileContains(path + ".7", "test12"));
235        EXPECT_TRUE(fileContains(path + ".8", "test11"));
236        EXPECT_TRUE(fileContains(path + ".9", "test10"));
237        EXPECT_FALSE(fileExists(path + ".10"));
238        EXPECT_FALSE(fileExists(path + ".11"));
239    }
240}
Note: See TracBrowser for help on using the repository browser.