Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/minigame4DHS14/test/util/output/LogWriterTest.cc @ 10087

Last change on this file since 10087 was 9549, checked in by landauf, 12 years ago

fixed test-build on msvc

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