Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged branch cpp11_v3 back to trunk

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