xref: /aoo4110/main/sax/qa/cppunit/test_converter.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "preextstl.h"
25 #include <cppunit/TestAssert.h>
26 #include <cppunit/TestFixture.h>
27 #include <cppunit/extensions/HelperMacros.h>
28 #include <cppunit/plugin/TestPlugIn.h>
29 #include "postextstl.h"
30 
31 #include <rtl/ustrbuf.hxx>
32 
33 #include <com/sun/star/util/DateTime.hpp>
34 #include <com/sun/star/util/Date.hpp>
35 #include <com/sun/star/util/Duration.hpp>
36 
37 #include "sax/tools/converter.hxx"
38 
39 
40 using namespace ::com::sun::star;
41 using sax::Converter;
42 
43 
44 namespace {
45 
46 class ConverterTest
47     : public ::CppUnit::TestFixture
48 {
49 public:
50     virtual void setUp();
51     virtual void tearDown();
52 
53     void testDuration();
54     void testDateTime();
55 
56     CPPUNIT_TEST_SUITE(ConverterTest);
57     CPPUNIT_TEST(testDuration);
58     CPPUNIT_TEST(testDateTime);
59     CPPUNIT_TEST_SUITE_END();
60 
61 private:
62 };
63 
setUp()64 void ConverterTest::setUp()
65 {
66 }
67 
tearDown()68 void ConverterTest::tearDown()
69 {
70 }
71 
eqDuration(util::Duration a,util::Duration b)72 static bool eqDuration(util::Duration a, util::Duration b) {
73     return a.Years == b.Years && a.Months == b.Months && a.Days == b.Days
74         && a.Hours == b.Hours && a.Minutes == b.Minutes
75         && a.Seconds == b.Seconds
76         && a.MilliSeconds == b.MilliSeconds
77         && a.Negative == b.Negative;
78 }
79 
doTest(util::Duration const & rid,char const * const pis,char const * const i_pos=0)80 static void doTest(util::Duration const & rid, char const*const pis,
81         char const*const i_pos = 0)
82 {
83     char const*const pos((i_pos) ? i_pos : pis);
84     util::Duration od;
85     ::rtl::OUString is(::rtl::OUString::createFromAscii(pis));
86     bool bSuccess = Converter::convertDuration(od, is);
87     OSL_TRACE("%d %dY %dM %dD %dH %dM %dS %dm",
88         od.Negative, od.Years, od.Months, od.Days,
89         od.Hours, od.Minutes, od.Seconds, od.MilliSeconds);
90     CPPUNIT_ASSERT(bSuccess);
91     CPPUNIT_ASSERT(eqDuration(rid, od));
92     ::rtl::OUStringBuffer buf;
93     Converter::convertDuration(buf, od);
94     OSL_TRACE(
95         ::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8));
96     CPPUNIT_ASSERT(buf.makeStringAndClear().equalsAscii(pos));
97 }
98 
doTestDurationF(char const * const pis)99 static void doTestDurationF(char const*const pis)
100 {
101     util::Duration od;
102     bool bSuccess = Converter::convertDuration(od,
103             ::rtl::OUString::createFromAscii(pis));
104     OSL_TRACE("%d %dY %dM %dD %dH %dM %dS %dH",
105         od.Negative, od.Years, od.Months, od.Days,
106         od.Hours, od.Minutes, od.Seconds, od.MilliSeconds);
107     CPPUNIT_ASSERT(!bSuccess);
108 }
109 
testDuration()110 void ConverterTest::testDuration()
111 {
112     OSL_TRACE("\nSAX CONVERTER TEST BEGIN\n");
113     doTest( util::Duration(false, 1, 0, 0, 0, 0, 0, 0), "P1Y" );
114     doTest( util::Duration(false, 0, 42, 0, 0, 0, 0, 0), "P42M" );
115     doTest( util::Duration(false, 0, 0, 111, 0, 0, 0, 0), "P111D" );
116     doTest( util::Duration(false, 0, 0, 0, 52, 0, 0, 0), "PT52H" );
117     doTest( util::Duration(false, 0, 0, 0, 0, 717, 0, 0), "PT717M" );
118     doTest( util::Duration(false, 0, 0, 0, 0, 0, 121, 0), "PT121S" );
119     doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 190), "PT0.19S" );
120     doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 90), "PT0.09S" );
121     doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 9), "PT0.009S" );
122     doTest( util::Duration(false, 0, 0, 0, 0, 0, 9, 999),
123             "PT9.999999999999999999999999999999S", "PT9.999S" );
124     doTest( util::Duration(true , 0, 0, 9999, 0, 0, 0, 0), "-P9999D" );
125     doTest( util::Duration(true , 7, 6, 5, 4, 3, 2, 10),
126             "-P7Y6M5DT4H3M2.01S" );
127     doTest( util::Duration(false, 0, 6, 0, 0, 3, 0, 0), "P6MT3M" );
128     doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 0), "P0D" );
129     doTestDurationF("1Y1M");        // invalid: no ^P
130     doTestDurationF("P-1Y1M");      // invalid: - after P
131     doTestDurationF("P1M1Y");       // invalid: Y after M
132     doTestDurationF("PT1Y");        // invalid: Y after T
133     doTestDurationF("P1Y1M1M");     // invalid: M twice, no T
134     doTestDurationF("P1YT1MT1M");   // invalid: T twice
135     doTestDurationF("P1YT");        // invalid: T but no H,M,S
136     doTestDurationF("P99999999999Y");   // cannot parse so many Ys
137     doTestDurationF("PT.1S");       // invalid: no 0 preceding .
138     doTestDurationF("PT5M.134S");   // invalid: no 0 preceding .
139     doTestDurationF("PT1.S");       // invalid: no digit following .
140     OSL_TRACE("\nSAX CONVERTER TEST END\n");
141 }
142 
143 
eqDateTime(util::DateTime a,util::DateTime b)144 static bool eqDateTime(util::DateTime a, util::DateTime b) {
145     return a.Year == b.Year && a.Month == b.Month && a.Day == b.Day
146         && a.Hours == b.Hours && a.Minutes == b.Minutes
147         && a.Seconds == b.Seconds
148         && a.HundredthSeconds == b.HundredthSeconds;
149 }
150 
doTest(util::DateTime const & rdt,char const * const pis,char const * const i_pos=0)151 static void doTest(util::DateTime const & rdt, char const*const pis,
152         char const*const i_pos = 0)
153 {
154     char const*const pos((i_pos) ? i_pos : pis);
155     ::rtl::OUString is(::rtl::OUString::createFromAscii(pis));
156     util::DateTime odt;
157     bool bSuccess( Converter::convertDateTime(odt, is) );
158     OSL_TRACE("Y:%d M:%d D:%d  H:%d M:%d S:%d H:%d",
159         odt.Year, odt.Month, odt.Day,
160         odt.Hours, odt.Minutes, odt.Seconds, odt.HundredthSeconds);
161     CPPUNIT_ASSERT(bSuccess);
162     CPPUNIT_ASSERT(eqDateTime(rdt, odt));
163     ::rtl::OUStringBuffer buf;
164     Converter::convertDateTime(buf, odt, true);
165     OSL_TRACE(
166         ::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8));
167     CPPUNIT_ASSERT(buf.makeStringAndClear().equalsAscii(pos));
168 }
169 
doTestDateTimeF(char const * const pis)170 static void doTestDateTimeF(char const*const pis)
171 {
172     util::DateTime odt;
173     bool bSuccess = Converter::convertDateTime(odt,
174             ::rtl::OUString::createFromAscii(pis));
175     OSL_TRACE("Y:%d M:%d D:%d  H:%dH M:%d S:%d H:%d",
176         odt.Year, odt.Month, odt.Day,
177         odt.Hours, odt.Minutes, odt.Seconds, odt.HundredthSeconds);
178     CPPUNIT_ASSERT(!bSuccess);
179 }
180 
testDateTime()181 void ConverterTest::testDateTime()
182 {
183     OSL_TRACE("\nSAX CONVERTER TEST BEGIN\n");
184     doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1), "0001-01-01T00:00:00" );
185     doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1),
186             "0001-01-01T00:00:00Z", "0001-01-01T00:00:00" );
187 //    doTest( util::DateTime(0, 0, 0, 0, 1, 1, -1), "-0001-01-01T00:00:00" );
188 //    doTest( util::DateTime(0, 0, 0, 0, 1, 1, -1), "-0001-01-01T00:00:00Z" );
189     doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1),
190             "0001-01-01T00:00:00-00:00", "0001-01-01T00:00:00" );
191     doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1),
192             "0001-01-01T00:00:00+00:00", "0001-01-01T00:00:00" );
193     doTest( util::DateTime(0, 0, 0, 0, 2, 1, 1)/*(0, 0, 12, 0, 2, 1, 1)*/,
194             "0001-01-02T00:00:00-12:00", "0001-01-02T00:00:00" );
195 //            "0001-02-01T12:00:00" );
196     doTest( util::DateTime(0, 0, 0, 0, 2, 1, 1)/*(0, 0, 12, 0, 1, 1, 1)*/,
197             "0001-01-02T00:00:00+12:00", "0001-01-02T00:00:00" );
198 //            "0001-01-01T12:00:00" );
199     doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999),
200             "9999-12-31T23:59:59.99" );
201     doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999),
202             "9999-12-31T23:59:59.99Z", "9999-12-31T23:59:59.99" );
203     doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999),
204             "9999-12-31T23:59:59.9999999999999999999999999999999999999",
205             "9999-12-31T23:59:59.99" );
206     doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999),
207             "9999-12-31T23:59:59.9999999999999999999999999999999999999Z",
208             "9999-12-31T23:59:59.99" );
209     doTest( util::DateTime(0, 0, 0, 24, 1, 1, 333)
210                 /*(0, 0, 0, 0, 2, 1, 333)*/,
211             "0333-01-01T24:00:00"/*, "0333-01-02T00:00:00"*/ );
212     doTestDateTimeF( "+0001-01-01T00:00:00" ); // invalid: ^+
213     doTestDateTimeF( "1-01-01T00:00:00" ); // invalid: < 4 Y
214     doTestDateTimeF( "0001-1-01T00:00:00" ); // invalid: < 2 M
215     doTestDateTimeF( "0001-01-1T00:00:00" ); // invalid: < 2 D
216     doTestDateTimeF( "0001-01-01T0:00:00" ); // invalid: < 2 H
217     doTestDateTimeF( "0001-01-01T00:0:00" ); // invalid: < 2 M
218     doTestDateTimeF( "0001-01-01T00:00:0" ); // invalid: < 2 S
219     doTestDateTimeF( "0001-01-01T00:00:00." ); // invalid: .$
220     doTestDateTimeF( "0001-01-01T00:00:00+1:00" ); // invalid: < 2 TZ H
221     doTestDateTimeF( "0001-01-01T00:00:00+00:1" ); // invalid: < 2 TZ M
222     doTestDateTimeF( "0001-13-01T00:00:00" ); // invalid: M > 12
223     doTestDateTimeF( "0001-01-32T00:00:00" ); // invalid: D > 31
224     doTestDateTimeF( "0001-01-01T25:00:00" ); // invalid: H > 24
225     doTestDateTimeF( "0001-01-01T00:60:00" ); // invalid: H > 59
226     doTestDateTimeF( "0001-01-01T00:00:60" ); // invalid: S > 59
227     doTestDateTimeF( "0001-01-01T24:01:00" ); // invalid: H=24, but M != 0
228     doTestDateTimeF( "0001-01-01T24:00:01" ); // invalid: H=24, but S != 0
229     doTestDateTimeF( "0001-01-01T24:00:00.1" ); // invalid: H=24, but H != 0
230     doTestDateTimeF( "0001-01-02T00:00:00+15:00" ); // invalid: TZ > +14:00
231     doTestDateTimeF( "0001-01-02T00:00:00+14:01" ); // invalid: TZ > +14:00
232     doTestDateTimeF( "0001-01-02T00:00:00-15:00" ); // invalid: TZ < -14:00
233     doTestDateTimeF( "0001-01-02T00:00:00-14:01" ); // invalid: TZ < -14:00
234     OSL_TRACE("\nSAX CONVERTER TEST END\n");
235 }
236 
237 CPPUNIT_TEST_SUITE_REGISTRATION(ConverterTest);
238 
239 }
240 
241 CPPUNIT_PLUGIN_IMPLEMENT();
242 
243