1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #include "preextstl.h" 29*cdf0e10cSrcweir #include <cppunit/TestAssert.h> 30*cdf0e10cSrcweir #include <cppunit/TestFixture.h> 31*cdf0e10cSrcweir #include <cppunit/extensions/HelperMacros.h> 32*cdf0e10cSrcweir #include <cppunit/plugin/TestPlugIn.h> 33*cdf0e10cSrcweir #include "postextstl.h" 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir #include <com/sun/star/util/DateTime.hpp> 38*cdf0e10cSrcweir #include <com/sun/star/util/Date.hpp> 39*cdf0e10cSrcweir #include <com/sun/star/util/Duration.hpp> 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir #include "sax/tools/converter.hxx" 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir using namespace ::com::sun::star; 45*cdf0e10cSrcweir using sax::Converter; 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir namespace { 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir class ConverterTest 51*cdf0e10cSrcweir : public ::CppUnit::TestFixture 52*cdf0e10cSrcweir { 53*cdf0e10cSrcweir public: 54*cdf0e10cSrcweir virtual void setUp(); 55*cdf0e10cSrcweir virtual void tearDown(); 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir void testDuration(); 58*cdf0e10cSrcweir void testDateTime(); 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir CPPUNIT_TEST_SUITE(ConverterTest); 61*cdf0e10cSrcweir CPPUNIT_TEST(testDuration); 62*cdf0e10cSrcweir CPPUNIT_TEST(testDateTime); 63*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir private: 66*cdf0e10cSrcweir }; 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir void ConverterTest::setUp() 69*cdf0e10cSrcweir { 70*cdf0e10cSrcweir } 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir void ConverterTest::tearDown() 73*cdf0e10cSrcweir { 74*cdf0e10cSrcweir } 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir static bool eqDuration(util::Duration a, util::Duration b) { 77*cdf0e10cSrcweir return a.Years == b.Years && a.Months == b.Months && a.Days == b.Days 78*cdf0e10cSrcweir && a.Hours == b.Hours && a.Minutes == b.Minutes 79*cdf0e10cSrcweir && a.Seconds == b.Seconds 80*cdf0e10cSrcweir && a.MilliSeconds == b.MilliSeconds 81*cdf0e10cSrcweir && a.Negative == b.Negative; 82*cdf0e10cSrcweir } 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir static void doTest(util::Duration const & rid, char const*const pis, 85*cdf0e10cSrcweir char const*const i_pos = 0) 86*cdf0e10cSrcweir { 87*cdf0e10cSrcweir char const*const pos((i_pos) ? i_pos : pis); 88*cdf0e10cSrcweir util::Duration od; 89*cdf0e10cSrcweir ::rtl::OUString is(::rtl::OUString::createFromAscii(pis)); 90*cdf0e10cSrcweir bool bSuccess = Converter::convertDuration(od, is); 91*cdf0e10cSrcweir OSL_TRACE("%d %dY %dM %dD %dH %dM %dS %dm", 92*cdf0e10cSrcweir od.Negative, od.Years, od.Months, od.Days, 93*cdf0e10cSrcweir od.Hours, od.Minutes, od.Seconds, od.MilliSeconds); 94*cdf0e10cSrcweir CPPUNIT_ASSERT(bSuccess); 95*cdf0e10cSrcweir CPPUNIT_ASSERT(eqDuration(rid, od)); 96*cdf0e10cSrcweir ::rtl::OUStringBuffer buf; 97*cdf0e10cSrcweir Converter::convertDuration(buf, od); 98*cdf0e10cSrcweir OSL_TRACE( 99*cdf0e10cSrcweir ::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8)); 100*cdf0e10cSrcweir CPPUNIT_ASSERT(buf.makeStringAndClear().equalsAscii(pos)); 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir static void doTestDurationF(char const*const pis) 104*cdf0e10cSrcweir { 105*cdf0e10cSrcweir util::Duration od; 106*cdf0e10cSrcweir bool bSuccess = Converter::convertDuration(od, 107*cdf0e10cSrcweir ::rtl::OUString::createFromAscii(pis)); 108*cdf0e10cSrcweir OSL_TRACE("%d %dY %dM %dD %dH %dM %dS %dH", 109*cdf0e10cSrcweir od.Negative, od.Years, od.Months, od.Days, 110*cdf0e10cSrcweir od.Hours, od.Minutes, od.Seconds, od.MilliSeconds); 111*cdf0e10cSrcweir CPPUNIT_ASSERT(!bSuccess); 112*cdf0e10cSrcweir } 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir void ConverterTest::testDuration() 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir OSL_TRACE("\nSAX CONVERTER TEST BEGIN\n"); 117*cdf0e10cSrcweir doTest( util::Duration(false, 1, 0, 0, 0, 0, 0, 0), "P1Y" ); 118*cdf0e10cSrcweir doTest( util::Duration(false, 0, 42, 0, 0, 0, 0, 0), "P42M" ); 119*cdf0e10cSrcweir doTest( util::Duration(false, 0, 0, 111, 0, 0, 0, 0), "P111D" ); 120*cdf0e10cSrcweir doTest( util::Duration(false, 0, 0, 0, 52, 0, 0, 0), "PT52H" ); 121*cdf0e10cSrcweir doTest( util::Duration(false, 0, 0, 0, 0, 717, 0, 0), "PT717M" ); 122*cdf0e10cSrcweir doTest( util::Duration(false, 0, 0, 0, 0, 0, 121, 0), "PT121S" ); 123*cdf0e10cSrcweir doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 190), "PT0.19S" ); 124*cdf0e10cSrcweir doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 90), "PT0.09S" ); 125*cdf0e10cSrcweir doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 9), "PT0.009S" ); 126*cdf0e10cSrcweir doTest( util::Duration(false, 0, 0, 0, 0, 0, 9, 999), 127*cdf0e10cSrcweir "PT9.999999999999999999999999999999S", "PT9.999S" ); 128*cdf0e10cSrcweir doTest( util::Duration(true , 0, 0, 9999, 0, 0, 0, 0), "-P9999D" ); 129*cdf0e10cSrcweir doTest( util::Duration(true , 7, 6, 5, 4, 3, 2, 10), 130*cdf0e10cSrcweir "-P7Y6M5DT4H3M2.01S" ); 131*cdf0e10cSrcweir doTest( util::Duration(false, 0, 6, 0, 0, 3, 0, 0), "P6MT3M" ); 132*cdf0e10cSrcweir doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 0), "P0D" ); 133*cdf0e10cSrcweir doTestDurationF("1Y1M"); // invalid: no ^P 134*cdf0e10cSrcweir doTestDurationF("P-1Y1M"); // invalid: - after P 135*cdf0e10cSrcweir doTestDurationF("P1M1Y"); // invalid: Y after M 136*cdf0e10cSrcweir doTestDurationF("PT1Y"); // invalid: Y after T 137*cdf0e10cSrcweir doTestDurationF("P1Y1M1M"); // invalid: M twice, no T 138*cdf0e10cSrcweir doTestDurationF("P1YT1MT1M"); // invalid: T twice 139*cdf0e10cSrcweir doTestDurationF("P1YT"); // invalid: T but no H,M,S 140*cdf0e10cSrcweir doTestDurationF("P99999999999Y"); // cannot parse so many Ys 141*cdf0e10cSrcweir doTestDurationF("PT.1S"); // invalid: no 0 preceding . 142*cdf0e10cSrcweir doTestDurationF("PT5M.134S"); // invalid: no 0 preceding . 143*cdf0e10cSrcweir doTestDurationF("PT1.S"); // invalid: no digit following . 144*cdf0e10cSrcweir OSL_TRACE("\nSAX CONVERTER TEST END\n"); 145*cdf0e10cSrcweir } 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir static bool eqDateTime(util::DateTime a, util::DateTime b) { 149*cdf0e10cSrcweir return a.Year == b.Year && a.Month == b.Month && a.Day == b.Day 150*cdf0e10cSrcweir && a.Hours == b.Hours && a.Minutes == b.Minutes 151*cdf0e10cSrcweir && a.Seconds == b.Seconds 152*cdf0e10cSrcweir && a.HundredthSeconds == b.HundredthSeconds; 153*cdf0e10cSrcweir } 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir static void doTest(util::DateTime const & rdt, char const*const pis, 156*cdf0e10cSrcweir char const*const i_pos = 0) 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir char const*const pos((i_pos) ? i_pos : pis); 159*cdf0e10cSrcweir ::rtl::OUString is(::rtl::OUString::createFromAscii(pis)); 160*cdf0e10cSrcweir util::DateTime odt; 161*cdf0e10cSrcweir bool bSuccess( Converter::convertDateTime(odt, is) ); 162*cdf0e10cSrcweir OSL_TRACE("Y:%d M:%d D:%d H:%d M:%d S:%d H:%d", 163*cdf0e10cSrcweir odt.Year, odt.Month, odt.Day, 164*cdf0e10cSrcweir odt.Hours, odt.Minutes, odt.Seconds, odt.HundredthSeconds); 165*cdf0e10cSrcweir CPPUNIT_ASSERT(bSuccess); 166*cdf0e10cSrcweir CPPUNIT_ASSERT(eqDateTime(rdt, odt)); 167*cdf0e10cSrcweir ::rtl::OUStringBuffer buf; 168*cdf0e10cSrcweir Converter::convertDateTime(buf, odt, true); 169*cdf0e10cSrcweir OSL_TRACE( 170*cdf0e10cSrcweir ::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8)); 171*cdf0e10cSrcweir CPPUNIT_ASSERT(buf.makeStringAndClear().equalsAscii(pos)); 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir static void doTestDateTimeF(char const*const pis) 175*cdf0e10cSrcweir { 176*cdf0e10cSrcweir util::DateTime odt; 177*cdf0e10cSrcweir bool bSuccess = Converter::convertDateTime(odt, 178*cdf0e10cSrcweir ::rtl::OUString::createFromAscii(pis)); 179*cdf0e10cSrcweir OSL_TRACE("Y:%d M:%d D:%d H:%dH M:%d S:%d H:%d", 180*cdf0e10cSrcweir odt.Year, odt.Month, odt.Day, 181*cdf0e10cSrcweir odt.Hours, odt.Minutes, odt.Seconds, odt.HundredthSeconds); 182*cdf0e10cSrcweir CPPUNIT_ASSERT(!bSuccess); 183*cdf0e10cSrcweir } 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir void ConverterTest::testDateTime() 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir OSL_TRACE("\nSAX CONVERTER TEST BEGIN\n"); 188*cdf0e10cSrcweir doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1), "0001-01-01T00:00:00" ); 189*cdf0e10cSrcweir doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1), 190*cdf0e10cSrcweir "0001-01-01T00:00:00Z", "0001-01-01T00:00:00" ); 191*cdf0e10cSrcweir // doTest( util::DateTime(0, 0, 0, 0, 1, 1, -1), "-0001-01-01T00:00:00" ); 192*cdf0e10cSrcweir // doTest( util::DateTime(0, 0, 0, 0, 1, 1, -1), "-0001-01-01T00:00:00Z" ); 193*cdf0e10cSrcweir doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1), 194*cdf0e10cSrcweir "0001-01-01T00:00:00-00:00", "0001-01-01T00:00:00" ); 195*cdf0e10cSrcweir doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1), 196*cdf0e10cSrcweir "0001-01-01T00:00:00+00:00", "0001-01-01T00:00:00" ); 197*cdf0e10cSrcweir doTest( util::DateTime(0, 0, 0, 0, 2, 1, 1)/*(0, 0, 12, 0, 2, 1, 1)*/, 198*cdf0e10cSrcweir "0001-01-02T00:00:00-12:00", "0001-01-02T00:00:00" ); 199*cdf0e10cSrcweir // "0001-02-01T12:00:00" ); 200*cdf0e10cSrcweir doTest( util::DateTime(0, 0, 0, 0, 2, 1, 1)/*(0, 0, 12, 0, 1, 1, 1)*/, 201*cdf0e10cSrcweir "0001-01-02T00:00:00+12:00", "0001-01-02T00:00:00" ); 202*cdf0e10cSrcweir // "0001-01-01T12:00:00" ); 203*cdf0e10cSrcweir doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999), 204*cdf0e10cSrcweir "9999-12-31T23:59:59.99" ); 205*cdf0e10cSrcweir doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999), 206*cdf0e10cSrcweir "9999-12-31T23:59:59.99Z", "9999-12-31T23:59:59.99" ); 207*cdf0e10cSrcweir doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999), 208*cdf0e10cSrcweir "9999-12-31T23:59:59.9999999999999999999999999999999999999", 209*cdf0e10cSrcweir "9999-12-31T23:59:59.99" ); 210*cdf0e10cSrcweir doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999), 211*cdf0e10cSrcweir "9999-12-31T23:59:59.9999999999999999999999999999999999999Z", 212*cdf0e10cSrcweir "9999-12-31T23:59:59.99" ); 213*cdf0e10cSrcweir doTest( util::DateTime(0, 0, 0, 24, 1, 1, 333) 214*cdf0e10cSrcweir /*(0, 0, 0, 0, 2, 1, 333)*/, 215*cdf0e10cSrcweir "0333-01-01T24:00:00"/*, "0333-01-02T00:00:00"*/ ); 216*cdf0e10cSrcweir doTestDateTimeF( "+0001-01-01T00:00:00" ); // invalid: ^+ 217*cdf0e10cSrcweir doTestDateTimeF( "1-01-01T00:00:00" ); // invalid: < 4 Y 218*cdf0e10cSrcweir doTestDateTimeF( "0001-1-01T00:00:00" ); // invalid: < 2 M 219*cdf0e10cSrcweir doTestDateTimeF( "0001-01-1T00:00:00" ); // invalid: < 2 D 220*cdf0e10cSrcweir doTestDateTimeF( "0001-01-01T0:00:00" ); // invalid: < 2 H 221*cdf0e10cSrcweir doTestDateTimeF( "0001-01-01T00:0:00" ); // invalid: < 2 M 222*cdf0e10cSrcweir doTestDateTimeF( "0001-01-01T00:00:0" ); // invalid: < 2 S 223*cdf0e10cSrcweir doTestDateTimeF( "0001-01-01T00:00:00." ); // invalid: .$ 224*cdf0e10cSrcweir doTestDateTimeF( "0001-01-01T00:00:00+1:00" ); // invalid: < 2 TZ H 225*cdf0e10cSrcweir doTestDateTimeF( "0001-01-01T00:00:00+00:1" ); // invalid: < 2 TZ M 226*cdf0e10cSrcweir doTestDateTimeF( "0001-13-01T00:00:00" ); // invalid: M > 12 227*cdf0e10cSrcweir doTestDateTimeF( "0001-01-32T00:00:00" ); // invalid: D > 31 228*cdf0e10cSrcweir doTestDateTimeF( "0001-01-01T25:00:00" ); // invalid: H > 24 229*cdf0e10cSrcweir doTestDateTimeF( "0001-01-01T00:60:00" ); // invalid: H > 59 230*cdf0e10cSrcweir doTestDateTimeF( "0001-01-01T00:00:60" ); // invalid: S > 59 231*cdf0e10cSrcweir doTestDateTimeF( "0001-01-01T24:01:00" ); // invalid: H=24, but M != 0 232*cdf0e10cSrcweir doTestDateTimeF( "0001-01-01T24:00:01" ); // invalid: H=24, but S != 0 233*cdf0e10cSrcweir doTestDateTimeF( "0001-01-01T24:00:00.1" ); // invalid: H=24, but H != 0 234*cdf0e10cSrcweir doTestDateTimeF( "0001-01-02T00:00:00+15:00" ); // invalid: TZ > +14:00 235*cdf0e10cSrcweir doTestDateTimeF( "0001-01-02T00:00:00+14:01" ); // invalid: TZ > +14:00 236*cdf0e10cSrcweir doTestDateTimeF( "0001-01-02T00:00:00-15:00" ); // invalid: TZ < -14:00 237*cdf0e10cSrcweir doTestDateTimeF( "0001-01-02T00:00:00-14:01" ); // invalid: TZ < -14:00 238*cdf0e10cSrcweir OSL_TRACE("\nSAX CONVERTER TEST END\n"); 239*cdf0e10cSrcweir } 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_REGISTRATION(ConverterTest); 242*cdf0e10cSrcweir 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir CPPUNIT_PLUGIN_IMPLEMENT(); 246*cdf0e10cSrcweir 247