aboutsummaryrefslogtreecommitdiff
path: root/src/test/cpp/encodingtest.cpp
blob: ce0ab545e0e7d1054affd247293e2d2d3fcc3a3f (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
/*
 * 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/logger.h>
#include "logunit.h"
//
//  If there is no support for wchar_t logging then
//     there is not a consistent way to get the test characters logged
//
#if LOG4CXX_WCHAR_T_API


#include <log4cxx/patternlayout.h>
#include <log4cxx/fileappender.h>
#include <log4cxx/level.h>

#include "util/binarycompare.h"
#include <log4cxx/file.h>
#include <log4cxx/helpers/pool.h>

using namespace log4cxx;
using namespace log4cxx::util;
using namespace log4cxx::helpers;

/**
 * Tests support for encoding specification.
 * 
 * 
 */
LOGUNIT_CLASS(EncodingTest) {
  LOGUNIT_TEST_SUITE(EncodingTest);
          LOGUNIT_TEST(testASCII);
          LOGUNIT_TEST(testLatin1);
          LOGUNIT_TEST(testUtf8);
          LOGUNIT_TEST(testUtf16);
          LOGUNIT_TEST(testUtf16LE);
          LOGUNIT_TEST(testUtf16BE);
  LOGUNIT_TEST_SUITE_END();
public:
    /**
     * Resets configuration after each test.
     */
    void tearDown() {
      Logger::getRootLogger()->getLoggerRepository()->resetConfiguration();
    }


    /**
     * Test us-ascii encoding.
     */
  void testASCII() {
      LoggerPtr root(Logger::getRootLogger());
      configure(root, LOG4CXX_STR("output/ascii.log"), LOG4CXX_STR("US-ASCII"));
      common(root);
      BinaryCompare::compare("output/ascii.log", "witness/encoding/ascii.log");
  }

    /**
     * Test iso-8859-1 encoding.
     */
    void testLatin1() {
        LoggerPtr root(Logger::getRootLogger());
        configure(root, LOG4CXX_STR("output/latin1.log"), LOG4CXX_STR("iso-8859-1"));
        common(root);
        BinaryCompare::compare("output/latin1.log", "witness/encoding/latin1.log");
    }

    /**
     * Test utf-8 encoding.
     */
    void testUtf8() {
        LoggerPtr root(Logger::getRootLogger());
        configure(root, LOG4CXX_STR("output/UTF-8.log"), LOG4CXX_STR("UTF-8"));
        common(root);
        BinaryCompare::compare("output/UTF-8.log", "witness/encoding/UTF-8.log");
    }

    /**
     * Test utf-16 encoding.
     */
    void testUtf16() {
        LoggerPtr root(Logger::getRootLogger());
        configure(root, LOG4CXX_STR("output/UTF-16.log"), LOG4CXX_STR("UTF-16"));
        common(root);
        BinaryCompare::compare("output/UTF-16.log", "witness/encoding/UTF-16.log");
    }

    /**
     * Test utf-16be encoding.
     */
    void testUtf16BE() {
        LoggerPtr root(Logger::getRootLogger());
        configure(root, LOG4CXX_STR("output/UTF-16BE.log"), LOG4CXX_STR("UTF-16BE"));
        common(root);
        BinaryCompare::compare("output/UTF-16BE.log", "witness/encoding/UTF-16BE.log");
    }

    /**
     * Test utf16-le encoding.
     */
    void testUtf16LE() {
        LoggerPtr root(Logger::getRootLogger());
        configure(root, LOG4CXX_STR("output/UTF-16LE.log"), LOG4CXX_STR("UTF-16LE"));
        common(root);
        BinaryCompare::compare("output/UTF-16LE.log", "witness/encoding/UTF-16LE.log");
    }

    /**
     * Configure logging.
     * @param logger logger
     * @param filename logging file name
     * @param encoding encoding
     */
    private:
    void configure(LoggerPtr& logger,
        const LogString& filename, const LogString& encoding) {
        PatternLayoutPtr layout(new PatternLayout());
        layout->setConversionPattern(LOG4CXX_STR("%p - %m\n"));
        Pool p;
        layout->activateOptions(p);
        FileAppenderPtr appender(new FileAppender());
        appender->setFile(filename);
        appender->setEncoding(encoding);
        appender->setAppend(false);
        appender->setLayout(layout);
        appender->activateOptions(p);
        logger->addAppender(appender);
        logger->setLevel(Level::getInfo());
    }

    /**
     * Common logging requests.
     * @param logger logger
     */
    void common(LoggerPtr& logger) {
        logger->info("Hello, World");
        // pi can be encoded in iso-8859-1
        const wchar_t pi[] = { 0x00B9, 0 };
        logger->info(pi);
        //   arbitrary, hopefully meaningless, characters from
        //     Latin, Arabic, Armenian, Bengali, CJK and Cyrillic
        const wchar_t greeting[] = { L'A', 0x0605, 0x0530, 0x986, 0x4E03, 0x400, 0 };
        logger->info(greeting);

    }
};

LOGUNIT_TEST_SUITE_REGISTRATION(EncodingTest);

#endif