File: datetimedateformattestcase.cpp

package info (click to toggle)
log4cxx 0.10.0-12
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 17,108 kB
  • ctags: 6,500
  • sloc: cpp: 33,940; sh: 8,860; xml: 4,686; makefile: 507; java: 48
file content (241 lines) | stat: -rw-r--r-- 7,581 bytes parent folder | download | duplicates (3)
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
/*
 * 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.
 */

#define __STDC_CONSTANT_MACROS
#include <log4cxx/logstring.h>
#include <log4cxx/helpers/datetimedateformat.h>
#include "../logunit.h"
#include <log4cxx/helpers/pool.h>
#include "../insertwide.h"
#include <apr.h>
#include <apr_time.h>
#include <sstream>

using namespace log4cxx;
using namespace log4cxx::helpers;
using namespace std;

#define LOCALE_US "C"
#if defined(_WIN32)
#define LOCALE_FR "French_france"
#else
#define LOCALE_FR "fr_FR"
#endif

#define LOG4CXX_TEST 1
#include <log4cxx/private/log4cxx_private.h>


#if LOG4CXX_HAS_STD_LOCALE
#include <locale>
#include "localechanger.h"
#define MAKE_LOCALE(ptr, id)     \
std::locale loco(id);            \
std::locale* ptr = &loco;
#else
#define MAKE_LOCALE(ptr, id)     \
std::locale* ptr = NULL;
#endif


/**
   Unit test {@link DateTimeDateFormat}.
   
   
*/
LOGUNIT_CLASS(DateTimeDateFormatTestCase)
{
  LOGUNIT_TEST_SUITE( DateTimeDateFormatTestCase );
  LOGUNIT_TEST( test1 );
  LOGUNIT_TEST( test2 );
  LOGUNIT_TEST( test3 );
  LOGUNIT_TEST( test4 );
  LOGUNIT_TEST( test5 );
  LOGUNIT_TEST( test6 );
#if LOG4CXX_HAS_STD_LOCALE
  LOGUNIT_TEST( test7 );
  LOGUNIT_TEST( test8 );
#endif
  LOGUNIT_TEST_SUITE_END();



private:

#define MICROSECONDS_PER_DAY APR_INT64_C(86400000000)


  /**
   Asserts that formatting the provided date results in the expected string.

  @param date Date date
   @param timeZone TimeZone timezone for conversion
   @param expected String expected string
  */
  void assertFormattedTime( apr_time_t date, const std::locale* locale,
       const TimeZonePtr& timeZone, const LogString& expected )
       {
         DateTimeDateFormat formatter(locale);
         formatter.setTimeZone(timeZone);
         LogString actual;
         Pool p;
         formatter.format(actual, date, p);
         LOGUNIT_ASSERT_EQUAL( expected, actual );
  }
public:

  /** Convert 02 Jan 2004 00:00:00 GMT for GMT. */
  void test1()
  {
    //
    //   02 Jan 2004 00:00 GMT
    //
    apr_time_t jan2 = MICROSECONDS_PER_DAY * 12419;
    MAKE_LOCALE(localeUS, LOCALE_US);
    assertFormattedTime( jan2, localeUS, TimeZone::getGMT(), LOG4CXX_STR("02 Jan 2004 00:00:00,000"));
  }

  /** Convert 03 Jan 2004 00:00:00 GMT for America/Chicago. */
  void test2()
  {
    //
    //   03 Jan 2004 00:00 GMT
    apr_time_t jan3 = MICROSECONDS_PER_DAY * 12420;
    MAKE_LOCALE(localeUS, LOCALE_US);
    assertFormattedTime( jan3, localeUS,
             TimeZone::getTimeZone(LOG4CXX_STR("GMT-6")),
             LOG4CXX_STR("02 Jan 2004 18:00:00,000"));
  }


  /** Convert 30 Jun 2004 00:00:00 GMT for GMT. */
  void test3()
  {
    apr_time_t jun30 = MICROSECONDS_PER_DAY * 12599;
    MAKE_LOCALE(localeUS, LOCALE_US);
    assertFormattedTime( jun30, localeUS, TimeZone::getGMT(),
           LOG4CXX_STR("30 Jun 2004 00:00:00,000"));
  }

  /** Convert 29 Jun 2004 00:00:00 GMT for Chicago, daylight savings in effect. */
  void test4()
  {
    apr_time_t jul1 = MICROSECONDS_PER_DAY * 12600;
    MAKE_LOCALE(localeUS, LOCALE_US);
    assertFormattedTime( jul1, localeUS,
           TimeZone::getTimeZone(LOG4CXX_STR("GMT-5")),
           LOG4CXX_STR("30 Jun 2004 19:00:00,000"));
  }

  /** Test multiple calls in close intervals. */
  void test5()
  {
    //   subsequent calls within one minute
    //     are optimized to reuse previous formatted value
    //     make a couple of nearly spaced calls
    apr_time_t ticks = MICROSECONDS_PER_DAY * 12601;
    MAKE_LOCALE(localeUS, LOCALE_US);
    assertFormattedTime( ticks, localeUS, TimeZone::getGMT(),
           LOG4CXX_STR("02 Jul 2004 00:00:00,000"));
    assertFormattedTime( ticks + 8000, localeUS, TimeZone::getGMT(),
           LOG4CXX_STR("02 Jul 2004 00:00:00,008"));
    assertFormattedTime( ticks + 17000, localeUS, TimeZone::getGMT(),
           LOG4CXX_STR("02 Jul 2004 00:00:00,017"));
    assertFormattedTime( ticks + 237000, localeUS, TimeZone::getGMT(),
           LOG4CXX_STR("02 Jul 2004 00:00:00,237"));
    assertFormattedTime( ticks + 1415000, localeUS, TimeZone::getGMT(),
           LOG4CXX_STR("02 Jul 2004 00:00:01,415"));
  }

  /** Check that caching does not disregard timezone. This test would fail for revision 1.4 of DateTimeDateFormat.java. */
  void test6()
  {
    apr_time_t jul3 = MICROSECONDS_PER_DAY * 12602;
    MAKE_LOCALE(localeUS, LOCALE_US);
    assertFormattedTime( jul3, localeUS, TimeZone::getGMT(),
        LOG4CXX_STR("03 Jul 2004 00:00:00,000"));
    assertFormattedTime( jul3, localeUS,
          TimeZone::getTimeZone(LOG4CXX_STR("GMT-5")),
          LOG4CXX_STR("02 Jul 2004 19:00:00,000"));
    assertFormattedTime( jul3, localeUS, TimeZone::getGMT(),
          LOG4CXX_STR("03 Jul 2004 00:00:00,000"));
  }

#if LOG4CXX_HAS_STD_LOCALE
  LogString formatDate(const std::locale& locale, const tm& date, const LogString& fmt) {
        //
        //  output the using STL
        //
        std::basic_ostringstream<logchar> buffer;
#if defined(_MSC_VER) && _MSC_VER < 1300
         _USEFAC(locale, std::time_put<logchar>)
             .put(buffer, buffer, &date, fmt.c_str(), fmt.c_str() + fmt.length());
#else
#if defined(_RWSTD_NO_TEMPLATE_ON_RETURN_TYPE)
         const std::time_put<logchar>& facet = std::use_facet(locale, (std::time_put<logchar>*) 0);
#else
         const std::time_put<logchar>& facet = std::use_facet<std::time_put<logchar> >(locale);
#endif
         facet.put(buffer, buffer, buffer.fill(), &date, fmt.c_str(), fmt.c_str() + fmt.length());
#endif
        return buffer.str();
  }

  /** Check that format is locale sensitive. */
  void test7()
  {
    apr_time_t avr11 = MICROSECONDS_PER_DAY * 12519;
    LocaleChanger localeChange(LOCALE_FR);
    if (localeChange.isEffective()) {
        LogString formatted;
        Pool p;
        SimpleDateFormat formatter(LOG4CXX_STR("MMM"));
        formatter.format(formatted, avr11, p);

        std::locale localeFR(LOCALE_FR);
        struct tm avr11tm = { 0, 0, 0, 11, 03, 104 };
        LogString expected(formatDate(localeFR, avr11tm, LOG4CXX_STR("%b")));

        LOGUNIT_ASSERT_EQUAL(expected, formatted);
    }
  }

  /** Check that format is locale sensitive. */
  void test8()
  {
    apr_time_t apr11 = MICROSECONDS_PER_DAY * 12519;
    LocaleChanger localeChange(LOCALE_US);
    if (localeChange.isEffective()) {
        LogString formatted;
        Pool p;
        SimpleDateFormat formatter(LOG4CXX_STR("MMM"));
        formatter.setTimeZone(TimeZone::getGMT());
        formatter.format(formatted, apr11, p);

        std::locale localeUS(LOCALE_US);
        struct tm apr11tm = { 0, 0, 0, 11, 03, 104 };
        LogString expected(formatDate(localeUS, apr11tm, LOG4CXX_STR("%b")));

        LOGUNIT_ASSERT_EQUAL(expected, formatted);
    }
  }
#endif

};

LOGUNIT_TEST_SUITE_REGISTRATION(DateTimeDateFormatTestCase);