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