Fix build error with GCC6
[profile/ivi/log4cxx.git] / src / test / cpp / xml / domtestcase.cpp
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
19 #include <log4cxx/logger.h>
20 #include <log4cxx/xml/domconfigurator.h>
21 #include "../logunit.h"
22 #include "../util/compare.h"
23 #include "xlevel.h"
24 #include "../util/controlfilter.h"
25 #include "../util/iso8601filter.h"
26 #include "../util/threadfilter.h"
27 #include "../util/transformer.h"
28 #include <iostream>
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"
34
35 using namespace log4cxx;
36 using namespace log4cxx::helpers;
37 using namespace log4cxx::xml;
38
39
40 #define TEST1_1A_PAT \
41         "(DEBUG|INFO |WARN |ERROR|FATAL) \\w*\\.\\w* - Message [0-9]"
42
43 #define TEST1_1B_PAT "(DEBUG|INFO |WARN |ERROR|FATAL) root - Message [0-9]"
44
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]"
47
48 LOGUNIT_CLASS(DOMTestCase)
49 {
50         LOGUNIT_TEST_SUITE(DOMTestCase);
51                 LOGUNIT_TEST(test1);
52 #if defined(_WIN32)
53                 LOGUNIT_TEST(test2);
54 #endif
55                 LOGUNIT_TEST(test3);
56                 LOGUNIT_TEST(test4);                
57         LOGUNIT_TEST_SUITE_END();
58
59         LoggerPtr root;
60         LoggerPtr logger;
61
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;
70
71 public:
72         void setUp()
73         {
74                 root = Logger::getRootLogger();
75                 logger = Logger::getLogger(LOG4CXX_TEST_STR("org.apache.log4j.xml.DOMTestCase"));
76         }
77
78         void tearDown()
79         {
80                 root->getLoggerRepository()->resetConfiguration();
81         }
82
83
84         void test1() {
85                 DOMConfigurator::configure(LOG4CXX_TEST_STR("input/xml/DOMTestCase1.xml"));
86                 common();
87
88                 ControlFilter cf1;
89                 cf1 << TEST1_1A_PAT << TEST1_1B_PAT;
90
91                 ControlFilter cf2;
92                 cf2 << TEST1_2_PAT;
93
94                 ThreadFilter threadFilter;
95                 ISO8601Filter iso8601Filter;
96
97                 std::vector<Filter *> filters1;
98                 filters1.push_back(&cf1);
99
100                 std::vector<Filter *> filters2;
101                 filters2.push_back(&cf2);
102                 filters2.push_back(&threadFilter);
103                 filters2.push_back(&iso8601Filter);
104
105                 try
106                 {
107                         Transformer::transform(TEMP_A1, FILTERED_A1, filters1);
108                         Transformer::transform(TEMP_A2, FILTERED_A2, filters2);
109                 }
110                 catch(UnexpectedFormatException& e)
111                 {
112                     std::cout << "UnexpectedFormatException :" << e.what() << std::endl;
113                         throw;
114                 }
115
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
119                 //
120                 //                LOGUNIT_ASSERT(Compare::compare(FILTERED_A1, witness1));
121                 LOGUNIT_ASSERT(Compare::compare(FILTERED_A2, witness2));
122         }
123
124         //
125         //   Same test but backslashes instead of forward
126         //
127         void test2() {
128                 DOMConfigurator::configure(LOG4CXX_TEST_STR("input\\xml\\DOMTestCase2.xml"));
129                 common();
130
131                 ThreadFilter threadFilter;
132                 ISO8601Filter iso8601Filter;
133
134                 std::vector<Filter *> filters1;
135
136                 std::vector<Filter *> filters2;
137                 filters2.push_back(&threadFilter);
138                 filters2.push_back(&iso8601Filter);
139
140                 try
141                 {
142                         Transformer::transform(TEMP_A1_2, FILTERED_A1_2, filters1);
143                         Transformer::transform(TEMP_A2_2, FILTERED_A2_2, filters2);
144                 }
145                 catch(UnexpectedFormatException& e)
146                 {
147                     std::cout << "UnexpectedFormatException :" << e.what() << std::endl;
148                         throw;
149                 }
150
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
154                 //
155                 //                LOGUNIT_ASSERT(Compare::compare(FILTERED_A1, witness1));
156                 LOGUNIT_ASSERT(Compare::compare(FILTERED_A2, witness2));
157         }
158
159
160         void common()
161         {
162                 int i = 0;
163
164                 LOG4CXX_DEBUG(logger, "Message " << i);
165                 LOG4CXX_DEBUG(root, "Message " << i);
166
167                 i++;
168                 LOG4CXX_INFO(logger, "Message " << i);
169                 LOG4CXX_INFO(root, "Message " << i);
170
171                 i++;
172                 LOG4CXX_WARN(logger, "Message " << i);
173                 LOG4CXX_WARN(root, "Message " << i);
174
175                 i++;
176                 LOG4CXX_ERROR(logger, "Message " << i);
177                 LOG4CXX_ERROR(root, "Message " << i);
178
179                 i++;
180                 LOG4CXX_FATAL(logger, "Message " << i);
181                 LOG4CXX_FATAL(root, "Message " << i);
182
183         }
184       
185         /**
186          *   Creates a output file that ends with a superscript 3.
187          *   Output file is checked by build.xml after completion.
188          */  
189         void test3() {
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 };
194 #else
195                 const logchar fname[] = { 0x6F, 0x75, 0x74, 0x70, 0x75, 0x74, 0x2F, 0x64, 0x6F, 0x6D, static_cast<logchar>(0xB3), 0 };
196 #endif
197                 File file;
198                 file.setPath(fname);
199                 Pool p;
200                 bool exists = file.exists(p);
201                 LOGUNIT_ASSERT(exists);
202         }
203
204         /**
205          *   Creates a output file that ends with a ideographic 4.
206          *   Output file is checked by build.xml after completion.
207          */  
208         void test4() {
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 };
213 #else
214                 const logchar fname[] = { 0x6F, 0x75, 0x74, 0x70, 0x75, 0x74, 0x2F, 0x64, 0x6F, 0x6D, static_cast<logchar>(0x3195), 0 };
215 #endif
216                 File file;
217                 file.setPath(fname);
218                 Pool p;
219                 bool exists = file.exists(p);
220                 LOGUNIT_ASSERT(exists);
221         }
222         
223 };
224
225 LOGUNIT_TEST_SUITE_REGISTRATION(DOMTestCase);
226
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"));
231
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"));
236