summaryrefslogtreecommitdiff
path: root/src/test/cpp/fileappendertest.cpp
blob: e16355a6b957d7beb09bc7aafb4601e0c8fc2389 (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
/*
 * 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 <log4cxx/helpers/pool.h>
#include <log4cxx/fileappender.h>
#include <log4cxx/patternlayout.h>
#include "logunit.h"

using namespace log4cxx;
using namespace log4cxx::helpers;


/**
 *
 * FileAppender tests.
 */
LOGUNIT_CLASS(FileAppenderTest) {
  LOGUNIT_TEST_SUITE(FileAppenderTest);
          LOGUNIT_TEST(testDirectoryCreation);
          LOGUNIT_TEST(testgetSetThreshold);
          LOGUNIT_TEST(testIsAsSevereAsThreshold);
  LOGUNIT_TEST_SUITE_END();
public:
  /**
   * Tests that any necessary directories are attempted to
   * be created if they don't exist.  See bug 9150.
   *
   */
  void testDirectoryCreation() {
      File newFile(LOG4CXX_STR("output/newdir/temp.log"));
      Pool p;
      newFile.deleteFile(p);

      File newDir(LOG4CXX_STR("output/newdir"));
      newDir.deleteFile(p);

      FileAppenderPtr wa(new FileAppender());
      wa->setFile(LOG4CXX_STR("output/newdir/temp.log"));
      wa->setLayout(new PatternLayout(LOG4CXX_STR("%m%n")));
      wa->activateOptions(p);

      LOGUNIT_ASSERT(File(LOG4CXX_STR("output/newdir/temp.log")).exists(p));
  }

  /**
   * Tests getThreshold and setThreshold.
   */
  void testgetSetThreshold() {
    FileAppenderPtr appender = new FileAppender();
    LevelPtr debug = Level::getDebug();
    //
    //  different from log4j where threshold is null.
    //
    LOGUNIT_ASSERT_EQUAL(Level::getAll(), appender->getThreshold());
    appender->setThreshold(debug);
    LOGUNIT_ASSERT_EQUAL(debug, appender->getThreshold());
  }

  /**
   * Tests isAsSevereAsThreshold.
   */
  void testIsAsSevereAsThreshold() {
    FileAppenderPtr appender = new FileAppender();
    LevelPtr debug = Level::getDebug();
    LOGUNIT_ASSERT(appender->isAsSevereAsThreshold(debug));
  }
};

LOGUNIT_TEST_SUITE_REGISTRATION(FileAppenderTest);