1 7 8 package org.jibx.runtime; 9 10 import java.text.DateFormat ; 11 import java.text.ParseException ; 12 import java.text.SimpleDateFormat ; 13 import java.util.Calendar ; 14 import java.util.Date ; 15 import java.util.GregorianCalendar ; 16 import java.util.TimeZone ; 17 18 import junit.framework.TestCase; 19 20 23 24 public class UtilityTest extends TestCase { 25 26 private static long LMS_PER_DAY = (long)24*60*60*1000; 27 private DateFormat m_dateTimeFormat = 28 new SimpleDateFormat ("yyyy-MM-dd'T'hh:mm:ss"); 29 private DateFormat m_dateFormat = 30 new SimpleDateFormat ("yyyy-MM-dd"); 31 private DateFormat m_yearFormat = 32 new SimpleDateFormat ("yyyy"); 33 private Calendar m_calendar = 34 new GregorianCalendar (TimeZone.getTimeZone("UTC")); 35 36 40 public UtilityTest(String arg0) { 41 super(arg0); 42 m_dateTimeFormat.setCalendar(m_calendar); 43 m_dateFormat.setCalendar(m_calendar); 44 m_yearFormat.setCalendar(m_calendar); 45 } 46 47 public void testParseInt() throws JiBXException { 48 assertEquals(0, Utility.parseInt("0")); 49 assertEquals(2000000000, Utility.parseInt("2000000000")); 50 assertEquals(-2000000000, Utility.parseInt("-2000000000")); 51 assertEquals(2000000000, Utility.parseInt("+2000000000")); 52 try { 53 Utility.parseInt("-"); 54 fail(); 55 } catch (JiBXException ex) {} 56 try { 57 Utility.parseInt("+"); 58 fail(); 59 } catch (JiBXException ex) {} 60 try { 61 Utility.parseInt("20000000000"); 62 fail(); 63 } catch (JiBXException ex) {} 64 try { 65 Utility.parseInt("-20000000000"); 66 fail(); 67 } catch (JiBXException ex) {} 68 try { 69 Utility.parseInt("+20000000000"); 70 fail(); 71 } catch (JiBXException ex) {} 72 try { 73 Utility.parseInt("2000000X"); 74 fail(); 75 } catch (JiBXException ex) {} 76 } 77 78 public void testSerializeInt() throws JiBXException { 79 assertEquals("0", Utility.serializeInt(0)); 80 assertEquals("2000000", Utility.serializeInt(2000000)); 81 assertEquals("2000000000", Utility.serializeInt(2000000000)); 82 assertEquals("-2000000", Utility.serializeInt(-2000000)); 83 assertEquals("-2000000000", Utility.serializeInt(-2000000000)); 84 } 85 86 public void testParseLong() throws JiBXException { 87 assertEquals(0, Utility.parseLong("0")); 88 assertEquals(2000000000, Utility.parseLong("2000000000")); 89 assertEquals(-2000000000, Utility.parseLong("-2000000000")); 90 assertEquals(2000000000, Utility.parseLong("+2000000000")); 91 assertEquals(200000000000000L, Utility.parseLong("200000000000000")); 92 assertEquals(-200000000000000L, Utility.parseLong("-200000000000000")); 93 assertEquals(200000000000000L, Utility.parseLong("+200000000000000")); 94 try { 95 Utility.parseLong("-"); 96 fail(); 97 } catch (JiBXException ex) {} 98 try { 99 Utility.parseLong("+"); 100 fail(); 101 } catch (JiBXException ex) {} 102 try { 103 Utility.parseLong("2000000X"); 104 fail(); 105 } catch (JiBXException ex) {} 106 } 107 108 public void testSerializeLong() { 109 assertEquals("0", Utility.serializeLong(0)); 110 assertEquals("2000000", Utility.serializeLong(2000000)); 111 assertEquals("2000000000000000", 112 Utility.serializeLong(2000000000000000L)); 113 assertEquals("-2000000", Utility.serializeLong(-2000000)); 114 assertEquals("-2000000000000000", 115 Utility.serializeLong(-2000000000000000L)); 116 } 117 118 public void testParseShort() throws JiBXException { 119 assertEquals(0, Utility.parseShort("0")); 120 assertEquals(20000, Utility.parseShort("20000")); 121 assertEquals(-20000, Utility.parseShort("-20000")); 122 assertEquals(20000, Utility.parseShort("+20000")); 123 try { 124 Utility.parseShort("-"); 125 fail(); 126 } catch (JiBXException ex) {} 127 try { 128 Utility.parseShort("+"); 129 fail(); 130 } catch (JiBXException ex) {} 131 try { 132 Utility.parseShort("2000000"); 133 fail(); 134 } catch (JiBXException ex) {} 135 try { 136 Utility.parseShort("2000X"); 137 fail(); 138 } catch (JiBXException ex) {} 139 } 140 141 public void testSerializeShort() { 142 assertEquals("0", Utility.serializeShort((short)0)); 143 assertEquals("20000", Utility.serializeShort((short)20000)); 144 assertEquals("-20000", Utility.serializeShort((short)-20000)); 145 } 146 147 public void testParseByte() throws JiBXException { 148 assertEquals(0, Utility.parseByte("0")); 149 assertEquals(100, Utility.parseByte("100")); 150 assertEquals(-100, Utility.parseByte("-100")); 151 assertEquals(100, Utility.parseByte("+100")); 152 try { 153 Utility.parseByte("-"); 154 fail(); 155 } catch (JiBXException ex) {} 156 try { 157 Utility.parseByte("+"); 158 fail(); 159 } catch (JiBXException ex) {} 160 try { 161 Utility.parseByte("128"); 162 fail(); 163 } catch (JiBXException ex) {} 164 try { 165 Utility.parseByte("10X"); 166 fail(); 167 } catch (JiBXException ex) {} 168 } 169 170 public void testSerializeByte() { 171 assertEquals("0", Utility.serializeByte((byte)0)); 172 assertEquals("100", Utility.serializeByte((byte)100)); 173 assertEquals("-100", Utility.serializeByte((byte)-100)); 174 } 175 176 public void testParseBoolean() throws JiBXException { 177 assertTrue(Utility.parseBoolean("true")); 178 assertTrue(Utility.parseBoolean("1")); 179 assertFalse(Utility.parseBoolean("false")); 180 assertFalse(Utility.parseBoolean("0")); 181 try { 182 Utility.parseBoolean("x"); 183 fail(); 184 } catch (JiBXException ex) {} 185 try { 186 Utility.parseBoolean("2"); 187 fail(); 188 } catch (JiBXException ex) {} 189 try { 190 Utility.parseBoolean("+1"); 191 fail(); 192 } catch (JiBXException ex) {} 193 } 194 195 public void testSerializeBoolean() { 196 assertEquals("true", Utility.serializeBoolean(true)); 197 assertEquals("false", Utility.serializeBoolean(false)); 198 } 199 200 public void testParseChar() throws JiBXException { 201 assertEquals(0, Utility.parseChar("0")); 202 assertEquals(100, Utility.parseChar("100")); 203 assertEquals(1000, Utility.parseChar("+1000")); 204 assertEquals(65000, Utility.parseChar("65000")); 205 try { 206 Utility.parseChar("-"); 207 fail(); 208 } catch (JiBXException ex) {} 209 try { 210 Utility.parseChar("+"); 211 fail(); 212 } catch (JiBXException ex) {} 213 try { 214 Utility.parseChar("-10"); 215 fail(); 216 } catch (JiBXException ex) {} 217 try { 218 Utility.parseChar("69000"); 219 fail(); 220 } catch (JiBXException ex) {} 221 } 222 223 public void testSerializeChar() { 224 assertEquals("0", Utility.serializeChar((char)0)); 225 assertEquals("100", Utility.serializeChar((char)100)); 226 assertEquals("60000", Utility.serializeChar((char)60000)); 227 } 228 229 public void testParseFloat() throws JiBXException { 230 assertEquals(1.0f, Utility.parseFloat("1.0"), 0.000001f); 231 assertEquals(1000000000.0f, Utility.parseFloat("1000000000.0"), 100.0f); 232 assertEquals(0.0000000001f, 233 Utility.parseFloat("0.0000000001"), 1.0e-17f); 234 assertEquals(0, Float.compare(-0.0f, Utility.parseFloat("-0"))); 235 assertEquals(0, Float.compare(Float.NEGATIVE_INFINITY, 236 Utility.parseFloat("-INF"))); 237 assertEquals(0, Float.compare(Float.POSITIVE_INFINITY, 238 Utility.parseFloat("INF"))); 239 assertEquals(0, Float.compare(Float.NaN, Utility.parseFloat("NaN"))); 240 try { 241 Utility.parseFloat("NAN"); 242 fail(); 243 } catch (JiBXException ex) {} 244 try { 245 Utility.parseFloat("+INF"); 246 fail(); 247 } catch (JiBXException ex) {} 248 try { 249 Utility.parseFloat("1E+2.5"); 250 fail(); 251 } catch (JiBXException ex) {} 252 } 253 254 public void testSerializeFloat() { 255 assertEquals("1.0", Utility.serializeFloat(1.0f)); 256 assertEquals("1.0E9", Utility.serializeFloat(1000000000.0f)); 257 assertEquals("1.0E-10", Utility.serializeFloat(0.0000000001f)); 258 assertEquals("-INF", Utility.serializeFloat(Float.NEGATIVE_INFINITY)); 259 assertEquals("INF", Utility.serializeFloat(Float.POSITIVE_INFINITY)); 260 assertEquals("NaN", Utility.serializeFloat(Float.NaN)); 261 } 262 263 public void testParseDouble() throws JiBXException { 264 assertEquals(1.0d, Utility.parseDouble("1.0"), 1.0e-12d); 265 assertEquals(1000000000.0d, Utility.parseDouble("1000000000.0"), 0.01d); 266 assertEquals(0.0000000001d, 267 Utility.parseDouble("0.0000000001"), 1.0e-22d); 268 assertEquals(0, Double.compare(-0.0d, Utility.parseDouble("-0"))); 269 assertEquals(0, Double.compare(Double.NEGATIVE_INFINITY, 270 Utility.parseDouble("-INF"))); 271 assertEquals(0, Double.compare(Double.POSITIVE_INFINITY, 272 Utility.parseDouble("INF"))); 273 assertEquals(0, Double.compare(Double.NaN, Utility.parseDouble("NaN"))); 274 try { 275 Utility.parseDouble("NAN"); 276 fail(); 277 } catch (JiBXException ex) {} 278 try { 279 Utility.parseDouble("+INF"); 280 fail(); 281 } catch (JiBXException ex) {} 282 try { 283 Utility.parseDouble("1E+2.5"); 284 fail(); 285 } catch (JiBXException ex) {} 286 } 287 288 public void testSerializeDouble() { 289 assertEquals("1.0", Utility.serializeDouble(1.0d)); 290 assertEquals("1.0E9", Utility.serializeDouble(1000000000.0d)); 291 assertEquals("1.0E-10", Utility.serializeDouble(0.0000000001d)); 292 assertEquals("-INF", Utility.serializeDouble(Double.NEGATIVE_INFINITY)); 293 assertEquals("INF", Utility.serializeDouble(Double.POSITIVE_INFINITY)); 294 assertEquals("NaN", Utility.serializeDouble(Double.NaN)); 295 } 296 297 public void testParseYear() { 298 } 299 300 public void testParseYearMonth() { 301 } 302 303 public void testParseDate() throws JiBXException { 304 assertEquals(0, Utility.parseDate("1970-01-01")); 305 assertEquals(LMS_PER_DAY, Utility.parseDate("1970-01-02")); 306 assertEquals(LMS_PER_DAY, 307 Utility.parseDate("0001-03-01") - Utility.parseDate("0001-02-28")); 308 assertEquals(LMS_PER_DAY, 309 Utility.parseDate("0001-01-01") - Utility.parseDate("-0001-12-31")); 310 assertEquals(LMS_PER_DAY, 311 Utility.parseDate("-0001-03-01") - 312 Utility.parseDate("-0001-02-28")); 313 assertEquals(LMS_PER_DAY*2, 314 Utility.parseDate("-0004-03-01") - 315 Utility.parseDate("-0004-02-28")); 316 try { 317 Utility.parseDate("+1970-01-01"); 318 fail(); 319 } catch (JiBXException ex) {} 320 try { 321 Utility.parseDate("197X-01-01"); 322 fail(); 323 } catch (JiBXException ex) {} 324 try { 325 Utility.parseDate("1970-1-01"); 326 fail(); 327 } catch (JiBXException ex) {} 328 try { 329 Utility.parseDate("1970-01-32"); 330 fail(); 331 } catch (JiBXException ex) {} 332 try { 333 Utility.parseDate("1970-02-29"); 334 fail(); 335 } catch (JiBXException ex) {} 336 try { 337 Utility.parseDate("01-01-01"); 338 fail(); 339 } catch (JiBXException ex) {} 340 try { 341 Utility.parseDate("0001-02-29"); 342 fail(); 343 } catch (JiBXException ex) {} 344 try { 345 Utility.parseDate("0000-01-01"); 346 fail(); 347 } catch (JiBXException ex) {} 348 try { 349 Utility.parseDate("-0001-02-29"); 350 fail(); 351 } catch (JiBXException ex) {} 352 try { 353 Utility.parseDate("-0003-02-29"); 354 fail(); 355 } catch (JiBXException ex) {} 356 } 357 358 public void testParseDateTime() throws JiBXException { 359 assertEquals(0, Utility.parseDateTime("1970-01-01T00:00:00.000")); 360 assertEquals(0, Utility.parseDateTime("1970-01-01T00:00:00")); 361 assertEquals(1, Utility.parseDateTime("1970-01-01T00:00:00.001")); 362 assertEquals(1000, Utility.parseDateTime("1970-01-01T00:00:01")); 363 assertEquals(60*1000, Utility.parseDateTime("1970-01-01T00:01:00")); 364 assertEquals(60*60*1000, Utility.parseDateTime("1970-01-01T01:00:00Z")); 365 assertEquals(60*60*1000, 366 Utility.parseDateTime("1970-01-01T02:00:00+01:00")); 367 assertEquals(LMS_PER_DAY, 368 Utility.parseDateTime("1970-01-02T00:00:00Z")); 369 assertEquals(LMS_PER_DAY, 370 Utility.parseDateTime("0001-03-01T00:00:00Z") - 371 Utility.parseDateTime("0001-02-28T00:00:00Z")); 372 assertEquals(1000, Utility.parseDateTime("0001-01-01T00:00:00Z") - 373 Utility.parseDateTime("-0001-12-31T23:59:59Z")); 374 assertEquals(LMS_PER_DAY, 375 Utility.parseDateTime("0001-01-01T00:00:00Z") - 376 Utility.parseDateTime("-0001-12-31T00:00:00Z")); 377 assertEquals(LMS_PER_DAY, 378 Utility.parseDateTime("-0001-03-01T00:00:00Z") - 379 Utility.parseDateTime("-0001-02-28T00:00:00Z")); 380 assertEquals(LMS_PER_DAY*2, 381 Utility.parseDateTime("-0004-03-01T00:00:00Z") - 382 Utility.parseDateTime("-0004-02-28T00:00:00Z")); 383 try { 384 Utility.parseDateTime("+1970-01-01T00:00:00.000"); 385 fail(); 386 } catch (JiBXException ex) {} 387 try { 388 Utility.parseDateTime("197X-01-01T00:00:00.000"); 389 fail(); 390 } catch (JiBXException ex) {} 391 try { 392 Utility.parseDateTime("1970-1-01T00:00:00.000"); 393 fail(); 394 } catch (JiBXException ex) {} 395 try { 396 Utility.parseDateTime("1970-01-01T00:00"); 397 fail(); 398 } catch (JiBXException ex) {} 399 try { 400 Utility.parseDateTime("1970-01-32T00:00:00"); 401 fail(); 402 } catch (JiBXException ex) {} 403 try { 404 Utility.parseDateTime("1970-02-29T00:00:00"); 405 fail(); 406 } catch (JiBXException ex) {} 407 try { 408 Utility.parseDateTime("1970-01-01T60:00:00"); 409 fail(); 410 } catch (JiBXException ex) {} 411 try { 412 Utility.parseDateTime("1970-01-01T-5:00:00"); 413 fail(); 414 } catch (JiBXException ex) {} 415 try { 416 Utility.parseDateTime("01-01-01T00:00:00"); 417 fail(); 418 } catch (JiBXException ex) {} 419 try { 420 Utility.parseDateTime("0001-02-29T00:00:00"); 421 fail(); 422 } catch (JiBXException ex) {} 423 try { 424 Utility.parseDateTime("0000-01-01T00:00:00"); 425 fail(); 426 } catch (JiBXException ex) {} 427 try { 428 Utility.parseDateTime("-0001-02-29T00:00:00"); 429 fail(); 430 } catch (JiBXException ex) {} 431 try { 432 Utility.parseDateTime("-0003-02-29T00:00:00"); 433 fail(); 434 } catch (JiBXException ex) {} 435 } 436 437 public void testDeserializeDateTime() { 438 } 439 440 private void trySerializeYear(String dt) 441 throws JiBXException, ParseException { 442 Date date = m_yearFormat.parse(dt); 443 String result = Utility.serializeYear(date); 444 assertEquals(dt, result); 445 } 446 447 private void tryRoundtripYear(String dt) throws JiBXException { 448 long time = Utility.parseYear(dt); 449 String result = Utility.serializeYear(new Date (time)); 450 assertEquals(dt, result); 453 } 454 455 public void testSerializeYear() throws JiBXException, ParseException { 456 trySerializeYear("1970"); 457 trySerializeYear("1969"); 458 trySerializeYear("1968"); 459 trySerializeYear("1967"); 460 trySerializeYear("1966"); 461 trySerializeYear("1965"); 462 trySerializeYear("1800"); 463 trySerializeYear("1600"); 464 trySerializeYear("1999"); 465 trySerializeYear("2000"); 466 trySerializeYear("2999"); 467 trySerializeYear("3000"); 468 trySerializeYear("9999"); 469 tryRoundtripYear("1970"); 470 tryRoundtripYear("1969"); 471 tryRoundtripYear("1968"); 472 tryRoundtripYear("1967"); 473 tryRoundtripYear("1966"); 474 tryRoundtripYear("1965"); 475 tryRoundtripYear("1800"); 476 tryRoundtripYear("1600"); 477 tryRoundtripYear("1999"); 478 tryRoundtripYear("2000"); 479 tryRoundtripYear("2001"); 480 tryRoundtripYear("2002"); 481 tryRoundtripYear("2999"); 482 tryRoundtripYear("3000"); 483 tryRoundtripYear("9999"); 484 485 tryRoundtripYear("12999"); 488 tryRoundtripYear("1300"); 489 tryRoundtripYear("0300"); 490 tryRoundtripYear("0001"); 491 tryRoundtripYear("-0001"); 492 tryRoundtripYear("-0004"); 493 tryRoundtripYear("-0005"); 494 tryRoundtripYear("-0199"); 495 tryRoundtripYear("-0198"); 496 } 497 498 public void testSerializeYearMonth() throws JiBXException { 499 assertEquals("1970-01", Utility.serializeYearMonth(new Date (0))); 500 } 501 502 private void trySerializeDate(String dt) 503 throws JiBXException, ParseException { 504 Date date = m_dateFormat.parse(dt); 505 String result = Utility.serializeDate(date); 506 assertEquals(dt, result); 507 } 508 509 private void tryRoundtripDate(String dt) throws JiBXException { 510 long time = Utility.parseDate(dt); 511 String result = Utility.serializeDate(new Date (time)); 512 assertEquals(dt, result); 515 } 516 517 public void testSerializeDate() throws JiBXException, ParseException { 518 trySerializeDate("1970-01-01"); 519 trySerializeDate("1970-02-01"); 520 trySerializeDate("1970-01-02"); 521 trySerializeDate("1969-12-31"); 522 trySerializeDate("1969-01-01"); 523 trySerializeDate("1969-02-28"); 524 trySerializeDate("1969-03-01"); 525 trySerializeDate("1968-01-01"); 526 trySerializeDate("1968-02-29"); 527 trySerializeDate("1968-03-01"); 528 trySerializeDate("1967-01-01"); 529 trySerializeDate("1966-01-01"); 530 trySerializeDate("1965-01-01"); 531 trySerializeDate("1800-01-01"); 532 trySerializeDate("1600-11-11"); 533 trySerializeDate("1999-12-31"); 534 trySerializeDate("2000-01-01"); 535 trySerializeDate("2000-02-29"); 536 trySerializeDate("2000-12-31"); 537 trySerializeDate("2001-01-01"); 538 trySerializeDate("2001-12-31"); 539 trySerializeDate("2002-01-01"); 540 trySerializeDate("2999-12-31"); 541 trySerializeDate("3000-01-01"); 542 trySerializeDate("3000-12-31"); 543 trySerializeDate("9999-12-31"); 544 tryRoundtripDate("1970-01-01"); 545 tryRoundtripDate("1970-02-01"); 546 tryRoundtripDate("1970-01-02"); 547 tryRoundtripDate("1969-12-31"); 548 tryRoundtripDate("1969-01-01"); 549 tryRoundtripDate("1969-02-28"); 550 tryRoundtripDate("1969-03-01"); 551 tryRoundtripDate("1968-01-01"); 552 tryRoundtripDate("1968-02-29"); 553 tryRoundtripDate("1968-03-01"); 554 tryRoundtripDate("1967-01-01"); 555 tryRoundtripDate("1966-01-01"); 556 tryRoundtripDate("1965-01-01"); 557 tryRoundtripDate("1800-01-01"); 558 tryRoundtripDate("1600-11-11"); 559 tryRoundtripDate("1999-12-31"); 560 tryRoundtripDate("2000-01-01"); 561 tryRoundtripDate("2000-02-29"); 562 tryRoundtripDate("2000-12-31"); 563 tryRoundtripDate("2001-01-01"); 564 tryRoundtripDate("2001-12-31"); 565 tryRoundtripDate("2002-01-01"); 566 tryRoundtripDate("2999-12-31"); 567 tryRoundtripDate("3000-01-01"); 568 tryRoundtripDate("3000-12-31"); 569 tryRoundtripDate("9999-12-31"); 570 571 tryRoundtripDate("12999-12-31"); 574 tryRoundtripDate("1300-01-01"); 575 tryRoundtripDate("0300-12-31"); 576 tryRoundtripDate("0001-01-01"); 577 tryRoundtripDate("-0001-12-31"); 578 tryRoundtripDate("-0004-02-29"); 579 tryRoundtripDate("-0004-03-01"); 580 tryRoundtripDate("-0004-12-31"); 581 tryRoundtripDate("-0005-02-28"); 582 tryRoundtripDate("-0005-03-01"); 583 tryRoundtripDate("-0199-12-31"); 584 tryRoundtripDate("-0198-01-01"); 585 } 586 587 private void trySerializeDateTime(String dt) 588 throws JiBXException, ParseException { 589 Date date = m_dateTimeFormat.parse(dt); 590 String result = Utility.serializeDateTime(date); 591 assertEquals(dt, result); 592 } 593 594 private void tryRoundtripDateTime(String dt) throws JiBXException { 595 long time = Utility.parseDateTime(dt); 596 String result = Utility.serializeDateTime(new Date (time)); 597 assertEquals(dt, result); 600 } 601 602 public void testSerializeDateTime() throws JiBXException, ParseException { 603 assertEquals("1970-01-01T00:00:00", 604 Utility.serializeDateTime(new Date (0))); 605 trySerializeDateTime("1970-01-01T00:00:00"); 606 trySerializeDateTime("1970-01-01T01:00:00"); 607 trySerializeDateTime("1970-01-01T00:01:00"); 608 trySerializeDateTime("1970-01-01T00:00:01"); 609 trySerializeDateTime("1970-01-01T23:59:59"); 610 trySerializeDateTime("1970-02-01T00:00:00"); 611 trySerializeDateTime("1970-01-02T00:00:00"); 612 trySerializeDateTime("1969-12-31T23:59:59"); 613 trySerializeDateTime("1969-01-01T00:00:00"); 614 trySerializeDateTime("1969-02-28T00:00:00"); 615 trySerializeDateTime("1969-03-01T00:00:00"); 616 trySerializeDateTime("1968-01-01T00:00:00"); 617 trySerializeDateTime("1968-02-29T00:00:00"); 618 trySerializeDateTime("1968-03-01T00:00:00"); 619 trySerializeDateTime("1967-01-01T00:00:00"); 620 trySerializeDateTime("1966-01-01T00:00:00"); 621 trySerializeDateTime("1965-01-01T00:00:00"); 622 trySerializeDateTime("1800-01-01T00:00:00"); 623 trySerializeDateTime("1600-11-11T23:59:59"); 624 trySerializeDateTime("1999-12-31T23:59:59"); 625 trySerializeDateTime("2000-01-01T00:00:00"); 626 trySerializeDateTime("2000-02-29T00:00:00"); 627 trySerializeDateTime("2000-12-31T23:59:59"); 628 trySerializeDateTime("2001-01-01T00:00:00"); 629 trySerializeDateTime("2001-02-28T18:54:31"); 630 trySerializeDateTime("2001-12-31T23:59:59"); 631 trySerializeDateTime("2002-01-01T00:00:00"); 632 trySerializeDateTime("2999-12-31T23:59:59"); 633 trySerializeDateTime("3000-01-01T00:00:00"); 634 trySerializeDateTime("3000-12-31T23:59:59"); 635 trySerializeDateTime("9999-12-31T23:59:59"); 636 tryRoundtripDateTime("1970-01-01T00:00:00"); 637 tryRoundtripDateTime("1970-01-01T01:00:00"); 638 tryRoundtripDateTime("1970-01-01T00:01:00"); 639 tryRoundtripDateTime("1970-01-01T00:00:01"); 640 tryRoundtripDateTime("1970-01-01T23:59:59"); 641 tryRoundtripDateTime("1970-02-01T00:00:00"); 642 tryRoundtripDateTime("1970-01-02T00:00:00"); 643 tryRoundtripDateTime("1969-12-31T23:59:59"); 644 tryRoundtripDateTime("1969-01-01T00:00:00"); 645 tryRoundtripDateTime("1969-02-28T00:00:00"); 646 tryRoundtripDateTime("1969-03-01T00:00:00"); 647 tryRoundtripDateTime("1968-01-01T00:00:00"); 648 tryRoundtripDateTime("1968-02-29T00:00:00"); 649 tryRoundtripDateTime("1968-03-01T00:00:00"); 650 tryRoundtripDateTime("1967-01-01T00:00:00"); 651 tryRoundtripDateTime("1966-01-01T00:00:00"); 652 tryRoundtripDateTime("1965-01-01T00:00:00"); 653 tryRoundtripDateTime("1800-01-01T00:00:00"); 654 tryRoundtripDateTime("1600-11-11T23:59:59"); 655 tryRoundtripDateTime("1999-12-31T23:59:59"); 656 tryRoundtripDateTime("2000-01-01T00:00:00"); 657 tryRoundtripDateTime("2000-02-29T00:00:00"); 658 tryRoundtripDateTime("2000-12-31T23:59:59"); 659 tryRoundtripDateTime("2001-01-01T00:00:00"); 660 tryRoundtripDateTime("2001-02-28T18:54:31"); 661 tryRoundtripDateTime("2001-12-31T23:59:59"); 662 tryRoundtripDateTime("2002-01-01T00:00:00"); 663 tryRoundtripDateTime("2999-12-31T23:59:59"); 664 tryRoundtripDateTime("3000-01-01T00:00:00"); 665 tryRoundtripDateTime("3000-12-31T23:59:59"); 666 tryRoundtripDateTime("9999-12-31T23:59:59"); 667 668 tryRoundtripDateTime("12999-12-31T23:59:59"); 671 tryRoundtripDateTime("1300-01-01T00:00:00"); 672 tryRoundtripDateTime("0300-12-31T23:59:59"); 673 tryRoundtripDateTime("0001-01-01T00:00:00"); 674 tryRoundtripDateTime("-0001-12-31T23:59:59"); 675 tryRoundtripDateTime("-0004-02-29T23:59:59"); 676 tryRoundtripDateTime("-0004-03-01T23:59:59"); 677 tryRoundtripDateTime("-0199-12-31T23:59:59"); 678 tryRoundtripDateTime("-0198-01-01T00:00:00"); 679 } 680 681 private boolean equalsBytes(byte[] a, byte[] b) { 682 if (a == null) { 683 return b == null; 684 } else if (b == null) { 685 return false; 686 } else if (a.length != b.length) { 687 return false; 688 } else { 689 for (int i = 0; i < a.length; i++) { 690 if (a[i] != b[i]) { 691 return false; 692 } 693 } 694 return true; 695 } 696 } 697 698 public void testParseBase64() throws JiBXException { 699 assertTrue(equalsBytes(Utility.parseBase64(""), new byte[0])); 700 assertTrue(equalsBytes(Utility.parseBase64("Cg=="), "\n".getBytes())); 701 assertTrue(equalsBytes(Utility.parseBase64("d2hhdA=="), 702 "what".getBytes())); 703 assertTrue(equalsBytes(Utility.parseBase64 704 ("d2hhdCB3aWxsIHByaW50IG91dA=="), 705 "what will print out".getBytes())); 706 assertTrue(equalsBytes(Utility.parseBase64 707 ("d2hhdCAgd2lsbCAgIHByaW50ICAgICBvdXQgICAgICA="), 708 "what will print out ".getBytes())); 709 assertTrue(equalsBytes(Utility.parseBase64 710 ("d2hhdCAgd2lsbCAgIHByaW50ICAgICBvdXQ="), 711 "what will print out".getBytes())); 712 } 713 714 public void testSerializeBase64() { 715 assertEquals(Utility.serializeBase64(new byte[0]), ""); 716 assertEquals(Utility.serializeBase64("\n".getBytes()), "Cg=="); 717 assertEquals(Utility.serializeBase64("what".getBytes()), "d2hhdA=="); 718 assertEquals(Utility.serializeBase64("what will print out".getBytes()), 719 "d2hhdCB3aWxsIHByaW50IG91dA=="); 720 assertEquals(Utility.serializeBase64 721 ("what will print out ".getBytes()), 722 "d2hhdCAgd2lsbCAgIHByaW50ICAgICBvdXQgICAgICA="); 723 assertEquals(Utility.serializeBase64 724 ("what will print out".getBytes()), 725 "d2hhdCAgd2lsbCAgIHByaW50ICAgICBvdXQ="); 726 } 727 728 public void testIsEqual() { 729 assertTrue(Utility.isEqual(null, null)); 730 assertFalse(Utility.isEqual(null, "text")); 731 assertFalse(Utility.isEqual("text", null)); 732 assertTrue(Utility.isEqual("text", "text")); 733 } 734 735 public static void main(String [] args) { 736 String [] names = { UtilityTest.class.getName() }; 737 junit.textui.TestRunner.main(names); 738 } 739 } | Popular Tags |