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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <log4cxx/logger.h>
20 #include <log4cxx/xml/domconfigurator.h>
21 #include "../logunit.h"
22 #include "../util/compare.h"
24 #include "../util/controlfilter.h"
25 #include "../util/iso8601filter.h"
26 #include "../util/threadfilter.h"
27 #include "../util/transformer.h"
29 #include <log4cxx/file.h>
30 #include <log4cxx/fileappender.h>
31 #include <apr_pools.h>
32 #include <apr_file_io.h>
33 #include "../testchar.h"
35 using namespace log4cxx;
36 using namespace log4cxx::helpers;
37 using namespace log4cxx::xml;
40 #define TEST1_1A_PAT \
41 "(DEBUG|INFO |WARN |ERROR|FATAL) \\w*\\.\\w* - Message [0-9]"
43 #define TEST1_1B_PAT "(DEBUG|INFO |WARN |ERROR|FATAL) root - Message [0-9]"
45 #define TEST1_2_PAT "^[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\},[0-9]\\{3\\} " \
46 "\\[0x[0-9A-F]*]\\ (DEBUG|INFO|WARN|ERROR|FATAL) .* - Message [0-9]"
48 LOGUNIT_CLASS(DOMTestCase)
50 LOGUNIT_TEST_SUITE(DOMTestCase);
57 LOGUNIT_TEST_SUITE_END();
62 static const File TEMP_A1;
63 static const File TEMP_A2;
64 static const File FILTERED_A1;
65 static const File FILTERED_A2;
66 static const File TEMP_A1_2;
67 static const File TEMP_A2_2;
68 static const File FILTERED_A1_2;
69 static const File FILTERED_A2_2;
74 root = Logger::getRootLogger();
75 logger = Logger::getLogger(LOG4CXX_TEST_STR("org.apache.log4j.xml.DOMTestCase"));
80 root->getLoggerRepository()->resetConfiguration();
85 DOMConfigurator::configure(LOG4CXX_TEST_STR("input/xml/DOMTestCase1.xml"));
89 cf1 << TEST1_1A_PAT << TEST1_1B_PAT;
94 ThreadFilter threadFilter;
95 ISO8601Filter iso8601Filter;
97 std::vector<Filter *> filters1;
98 filters1.push_back(&cf1);
100 std::vector<Filter *> filters2;
101 filters2.push_back(&cf2);
102 filters2.push_back(&threadFilter);
103 filters2.push_back(&iso8601Filter);
107 Transformer::transform(TEMP_A1, FILTERED_A1, filters1);
108 Transformer::transform(TEMP_A2, FILTERED_A2, filters2);
110 catch(UnexpectedFormatException& e)
112 std::cout << "UnexpectedFormatException :" << e.what() << std::endl;
116 const File witness1(LOG4CXX_TEST_STR("witness/dom.A1.1"));
117 const File witness2(LOG4CXX_TEST_STR("witness/dom.A2.1"));
118 // TODO: A1 doesn't contain duplicate entries
120 // LOGUNIT_ASSERT(Compare::compare(FILTERED_A1, witness1));
121 LOGUNIT_ASSERT(Compare::compare(FILTERED_A2, witness2));
125 // Same test but backslashes instead of forward
128 DOMConfigurator::configure(LOG4CXX_TEST_STR("input\\xml\\DOMTestCase2.xml"));
131 ThreadFilter threadFilter;
132 ISO8601Filter iso8601Filter;
134 std::vector<Filter *> filters1;
136 std::vector<Filter *> filters2;
137 filters2.push_back(&threadFilter);
138 filters2.push_back(&iso8601Filter);
142 Transformer::transform(TEMP_A1_2, FILTERED_A1_2, filters1);
143 Transformer::transform(TEMP_A2_2, FILTERED_A2_2, filters2);
145 catch(UnexpectedFormatException& e)
147 std::cout << "UnexpectedFormatException :" << e.what() << std::endl;
151 const File witness1(LOG4CXX_TEST_STR("witness/dom.A1.2"));
152 const File witness2(LOG4CXX_TEST_STR("witness/dom.A2.2"));
153 // TODO: A1 doesn't contain duplicate entries
155 // LOGUNIT_ASSERT(Compare::compare(FILTERED_A1, witness1));
156 LOGUNIT_ASSERT(Compare::compare(FILTERED_A2, witness2));
164 LOG4CXX_DEBUG(logger, "Message " << i);
165 LOG4CXX_DEBUG(root, "Message " << i);
168 LOG4CXX_INFO(logger, "Message " << i);
169 LOG4CXX_INFO(root, "Message " << i);
172 LOG4CXX_WARN(logger, "Message " << i);
173 LOG4CXX_WARN(root, "Message " << i);
176 LOG4CXX_ERROR(logger, "Message " << i);
177 LOG4CXX_ERROR(root, "Message " << i);
180 LOG4CXX_FATAL(logger, "Message " << i);
181 LOG4CXX_FATAL(root, "Message " << i);
186 * Creates a output file that ends with a superscript 3.
187 * Output file is checked by build.xml after completion.
190 DOMConfigurator::configure(LOG4CXX_TEST_STR("input/xml/DOMTestCase3.xml"));
191 LOG4CXX_INFO(logger, "File name is expected to end with a superscript 3");
192 #if LOG4CXX_LOGCHAR_IS_UTF8
193 const logchar fname[] = { 0x6F, 0x75, 0x74, 0x70, 0x75, 0x74, 0x2F, 0x64, 0x6F, 0x6D, static_cast<logchar>(0xC2), static_cast<logchar>(0xB3), 0 };
195 const logchar fname[] = { 0x6F, 0x75, 0x74, 0x70, 0x75, 0x74, 0x2F, 0x64, 0x6F, 0x6D, static_cast<logchar>(0xB3), 0 };
200 bool exists = file.exists(p);
201 LOGUNIT_ASSERT(exists);
205 * Creates a output file that ends with a ideographic 4.
206 * Output file is checked by build.xml after completion.
209 DOMConfigurator::configure(LOG4CXX_TEST_STR("input/xml/DOMTestCase4.xml"));
210 LOG4CXX_INFO(logger, "File name is expected to end with an ideographic 4");
211 #if LOG4CXX_LOGCHAR_IS_UTF8
212 const logchar fname[] = { 0x6F, 0x75, 0x74, 0x70, 0x75, 0x74, 0x2F, 0x64, 0x6F, 0x6D, static_cast<logchar>(0xE3), static_cast<logchar>(0x86), static_cast<logchar>(0x95), 0 };
214 const logchar fname[] = { 0x6F, 0x75, 0x74, 0x70, 0x75, 0x74, 0x2F, 0x64, 0x6F, 0x6D, static_cast<logchar>(0x3195), 0 };
219 bool exists = file.exists(p);
220 LOGUNIT_ASSERT(exists);
225 LOGUNIT_TEST_SUITE_REGISTRATION(DOMTestCase);
227 const File DOMTestCase::TEMP_A1(LOG4CXX_TEST_STR("output/temp.A1"));
228 const File DOMTestCase::TEMP_A2(LOG4CXX_TEST_STR("output/temp.A2"));
229 const File DOMTestCase::FILTERED_A1(LOG4CXX_TEST_STR("output/filtered.A1"));
230 const File DOMTestCase::FILTERED_A2(LOG4CXX_TEST_STR("output/filtered.A2"));
232 const File DOMTestCase::TEMP_A1_2(LOG4CXX_TEST_STR("output/temp.A1.2"));
233 const File DOMTestCase::TEMP_A2_2(LOG4CXX_TEST_STR("output/temp.A2.2"));
234 const File DOMTestCase::FILTERED_A1_2(LOG4CXX_TEST_STR("output/filtered.A1.2"));
235 const File DOMTestCase::FILTERED_A2_2(LOG4CXX_TEST_STR("output/filtered.A2.2"));