1 | // |
---|---|
2 | // SimpleFileChannelTest.cpp |
3 | // |
4 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "Poco/SimpleFileChannel.h" |
12 | #include "Poco/Message.h" |
13 | #include "Poco/Path.h" |
14 | #include "Poco/File.h" |
15 | #include "Poco/DirectoryIterator.h" |
16 | #include "Poco/Timestamp.h" |
17 | #include "Poco/DateTimeFormatter.h" |
18 | #include "Poco/AutoPtr.h" |
19 | #include "SimpleFileChannelTest.h" |
20 | #include "Poco/CppUnit/TestCaller.h" |
21 | #include "Poco/CppUnit/TestSuite.h" |
22 | |
23 | |
24 | using Poco::SimpleFileChannel; |
25 | using Poco::Message; |
26 | using Poco::Path; |
27 | using Poco::File; |
28 | using Poco::DirectoryIterator; |
29 | using Poco::Timestamp; |
30 | using Poco::DateTimeFormatter; |
31 | using Poco::AutoPtr; |
32 | |
33 | |
34 | SimpleFileChannelTest::SimpleFileChannelTest(const std::string& rName): CppUnit::TestCase(rName) |
35 | { |
36 | } |
37 | |
38 | |
39 | SimpleFileChannelTest::~SimpleFileChannelTest() |
40 | { |
41 | } |
42 | |
43 | |
44 | void SimpleFileChannelTest::testRotate() |
45 | { |
46 | std::string name = filename(); |
47 | try |
48 | { |
49 | AutoPtr<SimpleFileChannel> pChannel = new SimpleFileChannel(name); |
50 | pChannel->setProperty(SimpleFileChannel::PROP_ROTATION, "2 K"); |
51 | pChannel->open(); |
52 | Message msg("source", "This is a log file entry", Message::PRIO_INFORMATION); |
53 | for (int i = 0; i < 200; ++i) |
54 | { |
55 | pChannel->log(msg); |
56 | } |
57 | File f(name); |
58 | assertTrue (f.exists()); |
59 | f = name + ".0"; |
60 | assertTrue (f.exists()); |
61 | assertTrue (f.getSize() >= 2048); |
62 | } |
63 | catch (...) |
64 | { |
65 | remove(name); |
66 | throw; |
67 | } |
68 | remove(name); |
69 | } |
70 | |
71 | |
72 | void SimpleFileChannelTest::setUp() |
73 | { |
74 | } |
75 | |
76 | |
77 | void SimpleFileChannelTest::tearDown() |
78 | { |
79 | } |
80 | |
81 | |
82 | void SimpleFileChannelTest::remove(const std::string& baseName) |
83 | { |
84 | DirectoryIterator it(Path::current()); |
85 | DirectoryIterator end; |
86 | std::vector<std::string> files; |
87 | while (it != end) |
88 | { |
89 | if (it.name().find(baseName) == 0) |
90 | { |
91 | files.push_back(it.name()); |
92 | } |
93 | ++it; |
94 | } |
95 | for (std::vector<std::string>::iterator it = files.begin(); it != files.end(); ++it) |
96 | { |
97 | try |
98 | { |
99 | File f(*it); |
100 | f.remove(); |
101 | } |
102 | catch (...) |
103 | { |
104 | } |
105 | } |
106 | } |
107 | |
108 | |
109 | std::string SimpleFileChannelTest::filename() const |
110 | { |
111 | std::string name = "log_"; |
112 | name.append(DateTimeFormatter::format(Timestamp(), "%Y%m%d%H%M%S")); |
113 | name.append(".log"); |
114 | return name; |
115 | } |
116 | |
117 | |
118 | CppUnit::Test* SimpleFileChannelTest::suite() |
119 | { |
120 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SimpleFileChannelTest"); |
121 | |
122 | CppUnit_addTest(pSuite, SimpleFileChannelTest, testRotate); |
123 | |
124 | return pSuite; |
125 | } |
126 |