1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include "../util/compare.h"
19 #include "../insertwide.h"
20 #include "../logunit.h"
21 #include <apr_time.h>
22 #include <log4cxx/logmanager.h>
23 #include <log4cxx/xml/domconfigurator.h>
24 #include <log4cxx/patternlayout.h>
25 #include <log4cxx/rolling/rollingfileappender.h>
26 #include <log4cxx/rolling/fixedwindowrollingpolicy.h>
27 #include <log4cxx/rolling/filterbasedtriggeringpolicy.h>
28 #include <log4cxx/filter/levelrangefilter.h>
29 #include <log4cxx/helpers/pool.h>
30 #include <log4cxx/logger.h>
31 #include <log4cxx/propertyconfigurator.h>
32 #include <log4cxx/dailyrollingfileappender.h>
33 #include <log4cxx/helpers/stringhelper.h>
34
35
36 using namespace log4cxx;
37 using namespace log4cxx::rolling;
38 using namespace log4cxx::xml;
39 using namespace log4cxx::filter;
40 using namespace log4cxx::helpers;
41
42
43 /**
44 * Tests the emulation of org.apache.log4j.DailyRollingFileAppender
45 *
46 *
47 *
48 */
LOGUNIT_CLASS(ObsoleteDailyRollingFileAppenderTest)49 LOGUNIT_CLASS(ObsoleteDailyRollingFileAppenderTest)
50 {
51 LOGUNIT_TEST_SUITE(ObsoleteDailyRollingFileAppenderTest);
52 LOGUNIT_TEST(test1);
53 LOGUNIT_TEST(test2);
54 LOGUNIT_TEST_SUITE_END();
55
56
57 public:
58
59 void tearDown()
60 {
61 LogManager::shutdown();
62 }
63
64 /**
65 * Test basic rolling functionality.
66 */
67 void test1()
68 {
69 PropertyConfigurator::configure(File("input/rolling/obsoleteDRFA1.properties"));
70
71 int preCount = getFileCount("output", LOG4CXX_STR("obsoleteDRFA-test1.log."));
72 LoggerPtr logger(Logger::getLogger("org.apache.log4j.ObsoleteDailyRollingFileAppenderTest"));
73
74 char msg[11];
75 strcpy(msg, "Hello---??");
76
77 for (int i = 0; i < 25; i++)
78 {
79 apr_sleep(100000);
80 msg[8] = (char) ('0' + (i / 10));
81 msg[9] = (char) ('0' + (i % 10));
82 LOG4CXX_DEBUG(logger, msg);
83 }
84
85 int postCount = getFileCount("output", LOG4CXX_STR("obsoleteDRFA-test1.log."));
86 LOGUNIT_ASSERT_EQUAL(true, postCount > preCount);
87 }
88
89 /**
90 * Test basic rolling functionality.
91 * @deprecated Class under test is deprecated.
92 */
93 void test2()
94 {
95 PatternLayoutPtr layout(new PatternLayout(LOG4CXX_STR("%m%n")));
96 log4cxx::DailyRollingFileAppenderPtr rfa(new log4cxx::DailyRollingFileAppender());
97 rfa->setName(LOG4CXX_STR("ROLLING"));
98 rfa->setLayout(layout);
99 rfa->setAppend(false);
100 rfa->setFile(LOG4CXX_STR("output/obsoleteDRFA-test2.log"));
101 rfa->setDatePattern(LOG4CXX_STR("'.'yyyy-MM-dd-HH_mm_ss"));
102 Pool p;
103 rfa->activateOptions(p);
104 LoggerPtr root(Logger::getRootLogger());
105 root->addAppender(rfa);
106 LoggerPtr logger(Logger::getLogger("org.apache.log4j.ObsoleteDailyRollingAppenderTest"));
107
108 int preCount = getFileCount("output", LOG4CXX_STR("obsoleteDRFA-test2.log."));
109
110 char msg[11];
111 strcpy(msg, "Hello---??");
112
113 for (int i = 0; i < 25; i++)
114 {
115 apr_sleep(100000);
116 msg[8] = (char) ('0' + i / 10);
117 msg[9] = (char) ('0' + i % 10);
118 LOG4CXX_DEBUG(logger, msg);
119 }
120
121 int postCount = getFileCount("output", LOG4CXX_STR("obsoleteDRFA-test2.log."));
122 LOGUNIT_ASSERT_EQUAL(true, postCount > preCount);
123 }
124
125 private:
126 static int getFileCount(const char* dir, const LogString & initial)
127 {
128 Pool p;
129 std::vector<LogString> files(File(dir).list(p));
130 int count = 0;
131
132 for (size_t i = 0; i < files.size(); i++)
133 {
134 if (StringHelper::startsWith(files[i], initial))
135 {
136 count++;
137 }
138 }
139
140 return count;
141 }
142 };
143
144 LOGUNIT_TEST_SUITE_REGISTRATION(ObsoleteDailyRollingFileAppenderTest);
145
146