aboutsummaryrefslogtreecommitdiff
path: root/src/test/cpp/rolling/manualrollingtest.cpp
blob: 4e479ac0b5ca68ca0374f267210a6b8d1556e90b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "../util/compare.h"
#include "../insertwide.h"
#include "../logunit.h"
#include <apr_time.h>
#include <log4cxx/logmanager.h>
#include <log4cxx/xml/domconfigurator.h>
#include <log4cxx/patternlayout.h>
#include <log4cxx/rolling/fixedwindowrollingpolicy.h>
#include <log4cxx/rolling/sizebasedtriggeringpolicy.h>
#include <log4cxx/filter/levelrangefilter.h>
#include <log4cxx/helpers/pool.h>
#include <log4cxx/logger.h>
#include <log4cxx/propertyconfigurator.h>
#include <log4cxx/rolling/rollingfileappender.h>
#include <log4cxx/helpers/stringhelper.h>
#include <log4cxx/consoleappender.h>
#include <log4cxx/helpers/exception.h>
#include <log4cxx/helpers/fileoutputstream.h>


using namespace log4cxx;
using namespace log4cxx::xml;
using namespace log4cxx::filter;
using namespace log4cxx::helpers;
using namespace log4cxx::rolling;

/**
 *   Tests of explicit manual rolling of RollingFileAppenders.
 *
 * 
 *
 */
LOGUNIT_CLASS(ManualRollingTest)  {
   LOGUNIT_TEST_SUITE(ManualRollingTest);
           LOGUNIT_TEST(test1);
           LOGUNIT_TEST(test2);
//           TODO: Compression not yet implemented
//           LOGUNIT_TEST(test3);
           LOGUNIT_TEST(test4);
           LOGUNIT_TEST(test5);
   LOGUNIT_TEST_SUITE_END();

   LoggerPtr root;
   LoggerPtr logger;

 public:
  void setUp() {
    logger = Logger::getLogger("org.apache.log4j.rolling.ManualRollingTest");
    root = Logger::getRootLogger();
  }

  void tearDown() {
    LogManager::shutdown();
  }

  void common(RollingFileAppenderPtr& rfa,
       Pool& pool,
       LoggerPtr& logger1) {
    char msg[] = { 'H', 'e', 'l', 'l', 'o', '-', '-', '-', 'N', 0 };

    // Write exactly 10 bytes with each log
    for (int i = 0; i < 25; i++) {
      if (i < 10) {
        msg[8] = '0' + i;
      } else if (i < 100) {
        int digit = i % 10;
        if (digit == 0) {
          rfa->rollover(pool);
        }
        msg[7] = '0' + i / 10;
        msg[8] = '0' + digit;
      }
      LOG4CXX_DEBUG(logger1, msg);
    }
  }


  /**
   * Tests that the lack of an explicit active file will use the
   * low index as the active file.
   *
   */
  void test1() {
    PatternLayoutPtr layout = new PatternLayout(LOG4CXX_STR("%m\n"));
    RollingFileAppenderPtr rfa = new RollingFileAppender();
    rfa->setName(LOG4CXX_STR("ROLLING"));
    rfa->setAppend(false);
    rfa->setLayout(layout);

    FixedWindowRollingPolicyPtr swrp = new FixedWindowRollingPolicy();
    swrp->setMinIndex(0);

    swrp->setFileNamePattern(LOG4CXX_STR("output/manual-test1.%i"));
    Pool p;
    swrp->activateOptions(p);

    rfa->setRollingPolicy(swrp);
    rfa->activateOptions(p);
    root->addAppender(rfa);


    common(rfa, p, logger);

    LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test1.0").exists(p));
    LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test1.1").exists(p));
    LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test1.2").exists(p));
    LOGUNIT_ASSERT_EQUAL(true, Compare::compare(File("output/manual-test1.0"),
     File("witness/rolling/sbr-test2.log")));
    LOGUNIT_ASSERT_EQUAL(true, Compare::compare(File("output/manual-test1.1"),
     File("witness/rolling/sbr-test2.0")));
    LOGUNIT_ASSERT_EQUAL(true, Compare::compare(File("output/manual-test1.2"),
     File("witness/rolling/sbr-test2.1")));
  }

  /**
   * Test basic rolling functionality with explicit setting of FileAppender.file.
   */
  void test2() {
    PatternLayoutPtr layout = new PatternLayout(LOG4CXX_STR("%m\n"));
    RollingFileAppenderPtr rfa = new RollingFileAppender();
    rfa->setName(LOG4CXX_STR("ROLLING"));
    rfa->setAppend(false);
    rfa->setLayout(layout);
    rfa->setFile(LOG4CXX_STR("output/manual-test2.log"));

    Pool p;
    rfa->activateOptions(p);
    root->addAppender(rfa);

    common(rfa, p, logger);

    LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test2.log").exists(p));
    LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test2.log.1").exists(p));
    LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test2.log.2").exists(p));

    LOGUNIT_ASSERT_EQUAL(true, Compare::compare(File("output/manual-test2.log"),
     File("witness/rolling/sbr-test2.log")));
    LOGUNIT_ASSERT_EQUAL(true, Compare::compare(File("output/manual-test2.log.1"),
     File("witness/rolling/sbr-test2.0")));
    LOGUNIT_ASSERT_EQUAL(true, Compare::compare(File("output/manual-test2.log.2"),
     File("witness/rolling/sbr-test2.1")));
  }

  /**
   * Same as testBasic but also with GZ compression.
   */
  void test3() {
    PatternLayoutPtr layout = new PatternLayout(LOG4CXX_STR("%m\n"));
    RollingFileAppenderPtr rfa = new RollingFileAppender();
    rfa->setAppend(false);
    rfa->setLayout(layout);

    FixedWindowRollingPolicyPtr  fwrp = new FixedWindowRollingPolicy();

    fwrp->setMinIndex(0);
    rfa->setFile(LOG4CXX_STR("output/manual-test3.log"));
    fwrp->setFileNamePattern(LOG4CXX_STR("output/sbr-test3.%i.gz"));
    Pool p;
    fwrp->activateOptions(p);
    rfa->setRollingPolicy(fwrp);
    rfa->activateOptions(p);
    root->addAppender(rfa);

    common(rfa, p, logger);

    LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test3.log").exists(p));
    LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test3.0.gz").exists(p));
    LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test3.1.gz").exists(p));

    LOGUNIT_ASSERT_EQUAL(true, Compare::compare(File("output/manual-test3.log"),  File("witness/rolling/sbr-test3.log")));
    LOGUNIT_ASSERT_EQUAL(File("witness/rolling/sbr-test3.0.gz").length(p), File("output/manual-test3.0.gz").length(p));
    LOGUNIT_ASSERT_EQUAL(File("witness/rolling/sbr-test3.1.gz").length(p), File("output/manual-test3.1.gz").length(p));
  }

  /**
   * Test basic rolling functionality with bogus path in file name pattern.
   */
  void test4() {
    PatternLayoutPtr layout = new PatternLayout(LOG4CXX_STR("%m\n"));
    RollingFileAppenderPtr rfa = new RollingFileAppender();
    rfa->setName(LOG4CXX_STR("ROLLING"));
    rfa->setAppend(false);
    rfa->setLayout(layout);
    rfa->setFile(LOG4CXX_STR("output/manual-test4.log"));

    FixedWindowRollingPolicyPtr swrp = new FixedWindowRollingPolicy();

    swrp->setMinIndex(0);

    //
    //   test4 directory should not exists.  Should cause all rollover attempts to fail.
    //
    swrp->setFileNamePattern(LOG4CXX_STR("output/test4/manual-test4.%i"));
    Pool p;
    swrp->activateOptions(p);

    rfa->setRollingPolicy(swrp);
    rfa->activateOptions(p);
    root->addAppender(rfa);

    common(rfa, p, logger);

    LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test4.log").exists(p));

    LOGUNIT_ASSERT_EQUAL(true, Compare::compare(File("output/manual-test4.log"),
     File("witness/rolling/sbr-test4.log")));
  }

  /**
   * Checking handling of rename failures due to other access
   * to the indexed files.
   */
  void test5()  {
    PatternLayoutPtr layout = new PatternLayout(LOG4CXX_STR("%m\n"));
    RollingFileAppenderPtr rfa = new RollingFileAppender();
    rfa->setName(LOG4CXX_STR("ROLLING"));
    rfa->setAppend(false);
    rfa->setLayout(layout);
    rfa->setFile(LOG4CXX_STR("output/manual-test5.log"));

    FixedWindowRollingPolicyPtr swrp = new FixedWindowRollingPolicy();

    swrp->setMinIndex(0);

    swrp->setFileNamePattern(LOG4CXX_STR("output/manual-test5.%i"));
    Pool p;
    swrp->activateOptions(p);

    rfa->setRollingPolicy(swrp);
    rfa->activateOptions(p);
    root->addAppender(rfa);

    //
    //   put stray file about locked file
    FileOutputStream os1(LOG4CXX_STR("output/manual-test5.1"), false);
    os1.close(p);


    FileOutputStream os0(LOG4CXX_STR("output/manual-test5.0"), false);

    common(rfa, p, logger);

    os0.close(p);

    if (File("output/manual-test5.3").exists(p)) {
      //
      //    looks like platform where open files can be renamed
      //
      LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test5.log").exists(p));
      LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test5.0").exists(p));
      LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test5.1").exists(p));
      LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test5.2").exists(p));
      LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test5.3").exists(p));

      LOGUNIT_ASSERT_EQUAL(true, Compare::compare(File("output/manual-test5.log"),
       File("witness/rolling/sbr-test2.log")));
      LOGUNIT_ASSERT_EQUAL(true, Compare::compare(File("output/manual-test5.0"),
       File("witness/rolling/sbr-test2.0")));
      LOGUNIT_ASSERT_EQUAL(true, Compare::compare(File("output/manual-test5.1"),
       File("witness/rolling/sbr-test2.1")));

    } else {
      //
      //  rollover attempts should all fail
      //    so initial log file should have all log content
      //    open file should be unaffected
      //    stray file should have only been moved one slot.
      LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test5.log").exists(p));
      LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test5.0").exists(p));
      LOGUNIT_ASSERT_EQUAL(true, File("output/manual-test5.2").exists(p));

      LOGUNIT_ASSERT_EQUAL(true, Compare::compare(File("output/manual-test5.log"),
          File("witness/rolling/sbr-test4.log")));
    }
  }

};


LOGUNIT_TEST_SUITE_REGISTRATION(ManualRollingTest);