1 16 package org.joda.time.format; 17 18 import java.util.Locale ; 19 import java.util.TimeZone ; 20 21 import junit.framework.TestCase; 22 import junit.framework.TestSuite; 23 24 import org.joda.time.DateTimeConstants; 25 import org.joda.time.DateTimeUtils; 26 import org.joda.time.DateTimeZone; 27 import org.joda.time.Period; 28 import org.joda.time.PeriodType; 29 30 35 public class TestPeriodFormatterBuilder extends TestCase { 36 37 private static final Period PERIOD = new Period(1, 2, 3, 4, 5, 6, 7, 8); 38 private static final Period EMPTY_PERIOD = new Period(0, 0, 0, 0, 0, 0, 0, 0); 39 private static final Period YEAR_DAY_PERIOD = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType.yearDayTime()); 40 private static final Period EMPTY_YEAR_DAY_PERIOD = new Period(0, 0, 0, 0, 0, 0, 0, 0, PeriodType.yearDayTime()); 41 private static final Period TIME_PERIOD = new Period(0, 0, 0, 0, 5, 6, 7, 8); 42 private static final Period DATE_PERIOD = new Period(1, 2, 3, 4, 0, 0, 0, 0); 43 44 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 46 48 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 49 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 50 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 51 366 + 365; 52 private long TEST_TIME_NOW = 54 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 55 56 private DateTimeZone originalDateTimeZone = null; 57 private TimeZone originalTimeZone = null; 58 private Locale originalLocale = null; 59 60 private PeriodFormatterBuilder builder; 61 62 public static void main(String [] args) { 63 junit.textui.TestRunner.run(suite()); 64 } 65 66 public static TestSuite suite() { 67 return new TestSuite(TestPeriodFormatterBuilder.class); 68 } 69 70 public TestPeriodFormatterBuilder(String name) { 71 super(name); 72 } 73 74 protected void setUp() throws Exception { 75 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 76 originalDateTimeZone = DateTimeZone.getDefault(); 77 originalTimeZone = TimeZone.getDefault(); 78 originalLocale = Locale.getDefault(); 79 DateTimeZone.setDefault(LONDON); 80 TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")); 81 Locale.setDefault(Locale.UK); 82 83 builder = new PeriodFormatterBuilder(); 84 } 85 86 protected void tearDown() throws Exception { 87 DateTimeUtils.setCurrentMillisSystem(); 88 DateTimeZone.setDefault(originalDateTimeZone); 89 TimeZone.setDefault(originalTimeZone); 90 Locale.setDefault(originalLocale); 91 originalDateTimeZone = null; 92 originalTimeZone = null; 93 originalLocale = null; 94 } 95 96 public void testToFormatterPrinterParser() { 98 builder.appendYears(); 99 assertNotNull(builder.toFormatter()); 100 assertNotNull(builder.toPrinter()); 101 assertNotNull(builder.toParser()); 102 } 103 104 public void testFormatYears() { 106 PeriodFormatter f = builder.appendYears().toFormatter(); 107 assertEquals("1", f.print(PERIOD)); 108 assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD, null)); 109 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 110 } 111 112 public void testFormatMonths() { 113 PeriodFormatter f = builder.appendMonths().toFormatter(); 114 assertEquals("2", f.print(PERIOD)); 115 assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD, null)); 116 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 117 } 118 119 public void testFormatWeeks() { 120 PeriodFormatter f = builder.appendWeeks().toFormatter(); 121 assertEquals("3", f.print(PERIOD)); 122 assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD, null)); 123 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 124 } 125 126 public void testFormatDays() { 127 PeriodFormatter f = builder.appendDays().toFormatter(); 128 assertEquals("4", f.print(PERIOD)); 129 assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD, null)); 130 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 131 } 132 133 public void testFormatHours() { 134 PeriodFormatter f = builder.appendHours().toFormatter(); 135 assertEquals("5", f.print(PERIOD)); 136 assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD, null)); 137 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 138 } 139 140 public void testFormatMinutes() { 141 PeriodFormatter f = builder.appendMinutes().toFormatter(); 142 assertEquals("6", f.print(PERIOD)); 143 assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD, null)); 144 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 145 } 146 147 public void testFormatSeconds() { 148 PeriodFormatter f = builder.appendSeconds().toFormatter(); 149 assertEquals("7", f.print(PERIOD)); 150 assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD, null)); 151 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 152 } 153 154 public void testFormatSecondsWithMillis() { 155 PeriodFormatter f = builder.appendSecondsWithMillis().toFormatter(); 156 Period p = new Period(0, 0, 0, 0, 0, 0, 7, 0); 157 assertEquals("7.000", f.print(p)); 158 assertEquals(5, f.getPrinter().calculatePrintedLength(p, null)); 159 assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null)); 160 161 p = new Period(0, 0, 0, 0, 0, 0, 7, 1); 162 assertEquals("7.001", f.print(p)); 163 assertEquals(5, f.getPrinter().calculatePrintedLength(p, null)); 164 assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null)); 165 166 p = new Period(0, 0, 0, 0, 0, 0, 7, 999); 167 assertEquals("7.999", f.print(p)); 168 assertEquals(5, f.getPrinter().calculatePrintedLength(p, null)); 169 assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null)); 170 171 p = new Period(0, 0, 0, 0, 0, 0, 7, 1000); 172 assertEquals("8.000", f.print(p)); 173 assertEquals(5, f.getPrinter().calculatePrintedLength(p, null)); 174 assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null)); 175 176 p = new Period(0, 0, 0, 0, 0, 0, 7, 1001); 177 assertEquals("8.001", f.print(p)); 178 assertEquals(5, f.getPrinter().calculatePrintedLength(p, null)); 179 assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null)); 180 181 p = new Period(0, 0, 0, 0, 0, 0, 7, -1); 182 assertEquals("6.999", f.print(p)); 183 assertEquals(5, f.getPrinter().calculatePrintedLength(p, null)); 184 assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null)); 185 186 p = new Period(0, 0, 0, 0, 0, 0, -7, 1); 187 assertEquals("-6.999", f.print(p)); 188 assertEquals(6, f.getPrinter().calculatePrintedLength(p, null)); 189 assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null)); 190 191 p = new Period(0, 0, 0, 0, 0, 0, -7, -1); 192 assertEquals("-7.001", f.print(p)); 193 assertEquals(6, f.getPrinter().calculatePrintedLength(p, null)); 194 assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null)); 195 } 196 197 public void testFormatSecondsWithOptionalMillis() { 198 PeriodFormatter f = builder.appendSecondsWithOptionalMillis().toFormatter(); 199 Period p = new Period(0, 0, 0, 0, 0, 0, 7, 0); 200 assertEquals("7", f.print(p)); 201 assertEquals(1, f.getPrinter().calculatePrintedLength(p, null)); 202 assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null)); 203 204 p = new Period(0, 0, 0, 0, 0, 0, 7, 1); 205 assertEquals("7.001", f.print(p)); 206 assertEquals(5, f.getPrinter().calculatePrintedLength(p, null)); 207 assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null)); 208 209 p = new Period(0, 0, 0, 0, 0, 0, 7, 999); 210 assertEquals("7.999", f.print(p)); 211 assertEquals(5, f.getPrinter().calculatePrintedLength(p, null)); 212 assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null)); 213 214 p = new Period(0, 0, 0, 0, 0, 0, 7, 1000); 215 assertEquals("8", f.print(p)); 216 assertEquals(1, f.getPrinter().calculatePrintedLength(p, null)); 217 assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null)); 218 219 p = new Period(0, 0, 0, 0, 0, 0, 7, 1001); 220 assertEquals("8.001", f.print(p)); 221 assertEquals(5, f.getPrinter().calculatePrintedLength(p, null)); 222 assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null)); 223 224 p = new Period(0, 0, 0, 0, 0, 0, 7, -1); 225 assertEquals("6.999", f.print(p)); 226 assertEquals(5, f.getPrinter().calculatePrintedLength(p, null)); 227 assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null)); 228 229 p = new Period(0, 0, 0, 0, 0, 0, -7, 1); 230 assertEquals("-6.999", f.print(p)); 231 assertEquals(6, f.getPrinter().calculatePrintedLength(p, null)); 232 assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null)); 233 234 p = new Period(0, 0, 0, 0, 0, 0, -7, -1); 235 assertEquals("-7.001", f.print(p)); 236 assertEquals(6, f.getPrinter().calculatePrintedLength(p, null)); 237 assertEquals(1, f.getPrinter().countFieldsToPrint(p, Integer.MAX_VALUE, null)); 238 } 239 240 public void testFormatMillis() { 241 PeriodFormatter f = builder.appendMillis().toFormatter(); 242 assertEquals("8", f.print(PERIOD)); 243 assertEquals(1, f.getPrinter().calculatePrintedLength(PERIOD, null)); 244 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 245 } 246 247 public void testFormatMillis3Digit() { 248 PeriodFormatter f = builder.appendMillis3Digit().toFormatter(); 249 assertEquals("008", f.print(PERIOD)); 250 assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD, null)); 251 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 252 } 253 254 public void testFormatPrefixSimple1() { 256 PeriodFormatter f = builder.appendPrefix("Years:").appendYears().toFormatter(); 257 assertEquals("Years:1", f.print(PERIOD)); 258 assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null)); 259 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 260 } 261 262 public void testFormatPrefixSimple2() { 263 PeriodFormatter f = builder.appendPrefix("Hours:").appendHours().toFormatter(); 264 assertEquals("Hours:5", f.print(PERIOD)); 265 assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null)); 266 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 267 } 268 269 public void testFormatPrefixSimple3() { 270 try { 271 builder.appendPrefix(null); 272 fail(); 273 } catch (IllegalArgumentException ex) {} 274 } 275 276 public void testFormatPrefixPlural1() { 277 PeriodFormatter f = builder.appendPrefix("Year:", "Years:").appendYears().toFormatter(); 278 assertEquals("Year:1", f.print(PERIOD)); 279 assertEquals(6, f.getPrinter().calculatePrintedLength(PERIOD, null)); 280 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 281 } 282 283 public void testFormatPrefixPlural2() { 284 PeriodFormatter f = builder.appendPrefix("Hour:", "Hours:").appendHours().toFormatter(); 285 assertEquals("Hours:5", f.print(PERIOD)); 286 assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null)); 287 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 288 } 289 290 public void testFormatPrefixPlural3() { 291 try { 292 builder.appendPrefix(null, ""); 293 fail(); 294 } catch (IllegalArgumentException ex) {} 295 try { 296 builder.appendPrefix("", null); 297 fail(); 298 } catch (IllegalArgumentException ex) {} 299 try { 300 builder.appendPrefix(null, null); 301 fail(); 302 } catch (IllegalArgumentException ex) {} 303 } 304 305 public void testFormatSuffixSimple1() { 307 PeriodFormatter f = builder.appendYears().appendSuffix(" years").toFormatter(); 308 assertEquals("1 years", f.print(PERIOD)); 309 assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null)); 310 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 311 } 312 313 public void testFormatSuffixSimple2() { 314 PeriodFormatter f = builder.appendHours().appendSuffix(" hours").toFormatter(); 315 assertEquals("5 hours", f.print(PERIOD)); 316 assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null)); 317 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 318 } 319 320 public void testFormatSuffixSimple3() { 321 try { 322 builder.appendSuffix(null); 323 fail(); 324 } catch (IllegalArgumentException ex) {} 325 } 326 327 public void testFormatSuffixSimple4() { 328 try { 329 builder.appendSuffix(" hours"); 330 fail(); 331 } catch (IllegalStateException ex) {} 332 } 333 334 public void testFormatSuffixPlural1() { 335 PeriodFormatter f = builder.appendYears().appendSuffix(" year", " years").toFormatter(); 336 assertEquals("1 year", f.print(PERIOD)); 337 assertEquals(6, f.getPrinter().calculatePrintedLength(PERIOD, null)); 338 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 339 } 340 341 public void testFormatSuffixPlural2() { 342 PeriodFormatter f = builder.appendHours().appendSuffix(" hour", " hours").toFormatter(); 343 assertEquals("5 hours", f.print(PERIOD)); 344 assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null)); 345 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 346 } 347 348 public void testFormatSuffixPlural3() { 349 try { 350 builder.appendSuffix(null, ""); 351 fail(); 352 } catch (IllegalArgumentException ex) {} 353 try { 354 builder.appendSuffix("", null); 355 fail(); 356 } catch (IllegalArgumentException ex) {} 357 try { 358 builder.appendSuffix(null, null); 359 fail(); 360 } catch (IllegalArgumentException ex) {} 361 } 362 363 public void testFormatSuffixPlural4() { 364 try { 365 builder.appendSuffix(" hour", " hours"); 366 fail(); 367 } catch (IllegalStateException ex) {} 368 } 369 370 public void testFormatPrefixSuffix() { 372 PeriodFormatter f = builder.appendPrefix("P").appendYears().appendSuffix("Y").toFormatter(); 373 assertEquals("P1Y", f.print(PERIOD)); 374 assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD, null)); 375 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 376 } 377 378 public void testFormatSeparatorSimple() { 380 PeriodFormatter f = builder.appendYears().appendSeparator("T").appendHours().toFormatter(); 381 assertEquals("1T5", f.print(PERIOD)); 382 assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD, null)); 383 assertEquals(2, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 384 385 assertEquals("5", f.print(TIME_PERIOD)); 386 assertEquals(1, f.getPrinter().calculatePrintedLength(TIME_PERIOD, null)); 387 assertEquals(1, f.getPrinter().countFieldsToPrint(TIME_PERIOD, Integer.MAX_VALUE, null)); 388 389 assertEquals("1", f.print(DATE_PERIOD)); 390 assertEquals(1, f.getPrinter().calculatePrintedLength(DATE_PERIOD, null)); 391 assertEquals(1, f.getPrinter().countFieldsToPrint(DATE_PERIOD, Integer.MAX_VALUE, null)); 392 } 393 394 public void testFormatSeparatorComplex() { 395 PeriodFormatter f = builder 396 .appendYears().appendSeparator(", ", " and ") 397 .appendHours().appendSeparator(", ", " and ") 398 .appendMinutes().appendSeparator(", ", " and ") 399 .toFormatter(); 400 assertEquals("1, 5 and 6", f.print(PERIOD)); 401 assertEquals(10, f.getPrinter().calculatePrintedLength(PERIOD, null)); 402 assertEquals(3, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 403 404 assertEquals("5 and 6", f.print(TIME_PERIOD)); 405 assertEquals(7, f.getPrinter().calculatePrintedLength(TIME_PERIOD, null)); 406 assertEquals(2, f.getPrinter().countFieldsToPrint(TIME_PERIOD, Integer.MAX_VALUE, null)); 407 408 assertEquals("1", f.print(DATE_PERIOD)); 409 assertEquals(1, f.getPrinter().calculatePrintedLength(DATE_PERIOD, null)); 410 assertEquals(1, f.getPrinter().countFieldsToPrint(DATE_PERIOD, Integer.MAX_VALUE, null)); 411 } 412 413 public void testFormatSeparatorIfFieldsAfter() { 414 PeriodFormatter f = builder.appendYears().appendSeparatorIfFieldsAfter("T").appendHours().toFormatter(); 415 assertEquals("1T5", f.print(PERIOD)); 416 assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD, null)); 417 assertEquals(2, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 418 419 assertEquals("T5", f.print(TIME_PERIOD)); 420 assertEquals(2, f.getPrinter().calculatePrintedLength(TIME_PERIOD, null)); 421 assertEquals(1, f.getPrinter().countFieldsToPrint(TIME_PERIOD, Integer.MAX_VALUE, null)); 422 423 assertEquals("1", f.print(DATE_PERIOD)); 424 assertEquals(1, f.getPrinter().calculatePrintedLength(DATE_PERIOD, null)); 425 assertEquals(1, f.getPrinter().countFieldsToPrint(DATE_PERIOD, Integer.MAX_VALUE, null)); 426 } 427 428 public void testFormatSeparatorIfFieldsBefore() { 429 PeriodFormatter f = builder.appendYears().appendSeparatorIfFieldsBefore("T").appendHours().toFormatter(); 430 assertEquals("1T5", f.print(PERIOD)); 431 assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD, null)); 432 assertEquals(2, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 433 434 assertEquals("5", f.print(TIME_PERIOD)); 435 assertEquals(1, f.getPrinter().calculatePrintedLength(TIME_PERIOD, null)); 436 assertEquals(1, f.getPrinter().countFieldsToPrint(TIME_PERIOD, Integer.MAX_VALUE, null)); 437 438 assertEquals("1T", f.print(DATE_PERIOD)); 439 assertEquals(2, f.getPrinter().calculatePrintedLength(DATE_PERIOD, null)); 440 assertEquals(1, f.getPrinter().countFieldsToPrint(DATE_PERIOD, Integer.MAX_VALUE, null)); 441 } 442 443 public void testFormatLiteral() { 445 PeriodFormatter f = builder.appendLiteral("HELLO").toFormatter(); 446 assertEquals("HELLO", f.print(PERIOD)); 447 assertEquals(5, f.getPrinter().calculatePrintedLength(PERIOD, null)); 448 assertEquals(0, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 449 } 450 451 public void testFormatAppendFormatter() { 452 PeriodFormatter base = builder.appendYears().appendLiteral("-").toFormatter(); 453 PeriodFormatter f = new PeriodFormatterBuilder().append(base).appendYears().toFormatter(); 454 assertEquals("1-1", f.print(PERIOD)); 455 assertEquals(3, f.getPrinter().calculatePrintedLength(PERIOD, null)); 456 assertEquals(2, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 457 } 458 459 public void testFormatMinDigits() { 460 PeriodFormatter f = new PeriodFormatterBuilder().minimumPrintedDigits(4).appendYears().toFormatter(); 461 assertEquals("0001", f.print(PERIOD)); 462 assertEquals(4, f.getPrinter().calculatePrintedLength(PERIOD, null)); 463 assertEquals(1, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 464 } 465 466 public void testFormatPrintZeroDefault() { 468 PeriodFormatter f = 469 new PeriodFormatterBuilder() 470 .appendYears().appendLiteral("-") 471 .appendMonths().appendLiteral("-") 472 .appendWeeks().appendLiteral("-") 473 .appendDays().toFormatter(); 474 assertEquals("1-2-3-4", f.print(PERIOD)); 475 assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null)); 476 assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 477 478 assertEquals("---0", f.print(EMPTY_YEAR_DAY_PERIOD)); 479 assertEquals(4, f.getPrinter().calculatePrintedLength(EMPTY_YEAR_DAY_PERIOD, null)); 480 assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null)); 481 482 assertEquals("1---4", f.print(YEAR_DAY_PERIOD)); 483 assertEquals(5, f.getPrinter().calculatePrintedLength(YEAR_DAY_PERIOD, null)); 484 assertEquals(2, f.getPrinter().countFieldsToPrint(YEAR_DAY_PERIOD, Integer.MAX_VALUE, null)); 485 486 assertEquals("---0", f.print(EMPTY_PERIOD)); 487 assertEquals(4, f.getPrinter().calculatePrintedLength(EMPTY_PERIOD, null)); 488 assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD, Integer.MAX_VALUE, null)); 489 490 f = new PeriodFormatterBuilder() 492 .appendYears().appendLiteral("-") 493 .appendYears().toFormatter(); 494 assertEquals("-0", f.print(EMPTY_PERIOD)); 495 assertEquals(2, f.getPrinter().calculatePrintedLength(EMPTY_PERIOD, null)); 496 assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD, Integer.MAX_VALUE, null)); 497 } 498 499 public void testFormatPrintZeroRarelyLast() { 500 PeriodFormatter f = 501 new PeriodFormatterBuilder() 502 .printZeroRarelyLast() 503 .appendYears().appendLiteral("-") 504 .appendMonths().appendLiteral("-") 505 .appendWeeks().appendLiteral("-") 506 .appendDays().toFormatter(); 507 assertEquals("1-2-3-4", f.print(PERIOD)); 508 assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null)); 509 assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 510 511 assertEquals("---0", f.print(EMPTY_YEAR_DAY_PERIOD)); 512 assertEquals(4, f.getPrinter().calculatePrintedLength(EMPTY_YEAR_DAY_PERIOD, null)); 513 assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null)); 514 515 assertEquals("1---4", f.print(YEAR_DAY_PERIOD)); 516 assertEquals(5, f.getPrinter().calculatePrintedLength(YEAR_DAY_PERIOD, null)); 517 assertEquals(2, f.getPrinter().countFieldsToPrint(YEAR_DAY_PERIOD, Integer.MAX_VALUE, null)); 518 519 assertEquals("---0", f.print(EMPTY_PERIOD)); 520 assertEquals(4, f.getPrinter().calculatePrintedLength(EMPTY_PERIOD, null)); 521 assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD, Integer.MAX_VALUE, null)); 522 } 523 524 public void testFormatPrintZeroRarelyFirst() { 525 PeriodFormatter f = 526 new PeriodFormatterBuilder() 527 .printZeroRarelyFirst() 528 .appendYears().appendLiteral("-") 529 .appendMonths().appendLiteral("-") 530 .appendWeeks().appendLiteral("-") 531 .appendDays().toFormatter(); 532 assertEquals("1-2-3-4", f.print(PERIOD)); 533 assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null)); 534 assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 535 536 assertEquals("0---", f.print(EMPTY_YEAR_DAY_PERIOD)); 537 assertEquals(4, f.getPrinter().calculatePrintedLength(EMPTY_YEAR_DAY_PERIOD, null)); 538 assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null)); 539 540 assertEquals("1---4", f.print(YEAR_DAY_PERIOD)); 541 assertEquals(5, f.getPrinter().calculatePrintedLength(YEAR_DAY_PERIOD, null)); 542 assertEquals(2, f.getPrinter().countFieldsToPrint(YEAR_DAY_PERIOD, Integer.MAX_VALUE, null)); 543 544 assertEquals("0---", f.print(EMPTY_PERIOD)); 545 assertEquals(4, f.getPrinter().calculatePrintedLength(EMPTY_PERIOD, null)); 546 assertEquals(1, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD, Integer.MAX_VALUE, null)); 547 } 548 549 public void testFormatPrintZeroIfSupported() { 550 PeriodFormatter f = 551 new PeriodFormatterBuilder() 552 .printZeroIfSupported() 553 .appendYears().appendLiteral("-") 554 .appendMonths().appendLiteral("-") 555 .appendWeeks().appendLiteral("-") 556 .appendDays().toFormatter(); 557 assertEquals("1-2-3-4", f.print(PERIOD)); 558 assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null)); 559 assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 560 561 assertEquals("0---0", f.print(EMPTY_YEAR_DAY_PERIOD)); 562 assertEquals(5, f.getPrinter().calculatePrintedLength(EMPTY_YEAR_DAY_PERIOD, null)); 563 assertEquals(2, f.getPrinter().countFieldsToPrint(EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null)); 564 565 assertEquals("1---4", f.print(YEAR_DAY_PERIOD)); 566 assertEquals(5, f.getPrinter().calculatePrintedLength(YEAR_DAY_PERIOD, null)); 567 assertEquals(2, f.getPrinter().countFieldsToPrint(YEAR_DAY_PERIOD, Integer.MAX_VALUE, null)); 568 569 assertEquals("0-0-0-0", f.print(EMPTY_PERIOD)); 570 assertEquals(7, f.getPrinter().calculatePrintedLength(EMPTY_PERIOD, null)); 571 assertEquals(4, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD, Integer.MAX_VALUE, null)); 572 } 573 574 public void testFormatPrintZeroAlways() { 575 PeriodFormatter f = 576 new PeriodFormatterBuilder() 577 .printZeroAlways() 578 .appendYears().appendLiteral("-") 579 .appendMonths().appendLiteral("-") 580 .appendWeeks().appendLiteral("-") 581 .appendDays().toFormatter(); 582 assertEquals("1-2-3-4", f.print(PERIOD)); 583 assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null)); 584 assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 585 586 assertEquals("0-0-0-0", f.print(EMPTY_YEAR_DAY_PERIOD)); 587 assertEquals(7, f.getPrinter().calculatePrintedLength(EMPTY_YEAR_DAY_PERIOD, null)); 588 assertEquals(4, f.getPrinter().countFieldsToPrint(EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null)); 589 590 assertEquals("1-0-0-4", f.print(YEAR_DAY_PERIOD)); 591 assertEquals(7, f.getPrinter().calculatePrintedLength(YEAR_DAY_PERIOD, null)); 592 assertEquals(4, f.getPrinter().countFieldsToPrint(YEAR_DAY_PERIOD, Integer.MAX_VALUE, null)); 593 594 assertEquals("0-0-0-0", f.print(EMPTY_PERIOD)); 595 assertEquals(7, f.getPrinter().calculatePrintedLength(EMPTY_PERIOD, null)); 596 assertEquals(4, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD, Integer.MAX_VALUE, null)); 597 } 598 599 public void testFormatPrintZeroNever() { 600 PeriodFormatter f = 601 new PeriodFormatterBuilder() 602 .printZeroNever() 603 .appendYears().appendLiteral("-") 604 .appendMonths().appendLiteral("-") 605 .appendWeeks().appendLiteral("-") 606 .appendDays().toFormatter(); 607 assertEquals("1-2-3-4", f.print(PERIOD)); 608 assertEquals(7, f.getPrinter().calculatePrintedLength(PERIOD, null)); 609 assertEquals(4, f.getPrinter().countFieldsToPrint(PERIOD, Integer.MAX_VALUE, null)); 610 611 assertEquals("---", f.print(EMPTY_YEAR_DAY_PERIOD)); 612 assertEquals(3, f.getPrinter().calculatePrintedLength(EMPTY_YEAR_DAY_PERIOD, null)); 613 assertEquals(0, f.getPrinter().countFieldsToPrint(EMPTY_YEAR_DAY_PERIOD, Integer.MAX_VALUE, null)); 614 615 assertEquals("1---4", f.print(YEAR_DAY_PERIOD)); 616 assertEquals(5, f.getPrinter().calculatePrintedLength(YEAR_DAY_PERIOD, null)); 617 assertEquals(2, f.getPrinter().countFieldsToPrint(YEAR_DAY_PERIOD, Integer.MAX_VALUE, null)); 618 619 assertEquals("---", f.print(EMPTY_PERIOD)); 620 assertEquals(3, f.getPrinter().calculatePrintedLength(EMPTY_PERIOD, null)); 621 assertEquals(0, f.getPrinter().countFieldsToPrint(EMPTY_PERIOD, Integer.MAX_VALUE, null)); 622 } 623 624 public void testFormatAppend_PrinterParser_null_null() { 626 try { 627 new PeriodFormatterBuilder().append(null, null); 628 fail(); 629 } catch (IllegalArgumentException ex) {} 630 } 631 632 public void testFormatAppend_PrinterParser_Printer_null() { 633 PeriodPrinter printer = new PeriodFormatterBuilder().appendYears().appendLiteral("-").toPrinter(); 634 PeriodFormatterBuilder bld = new PeriodFormatterBuilder().append(printer, null).appendMonths(); 635 assertNotNull(bld.toPrinter()); 636 assertNull(bld.toParser()); 637 638 PeriodFormatter f = bld.toFormatter(); 639 assertEquals("1-2", f.print(PERIOD)); 640 try { 641 f.parsePeriod("1-2"); 642 fail(); 643 } catch (UnsupportedOperationException ex) {} 644 } 645 646 public void testFormatAppend_PrinterParser_null_Parser() { 647 PeriodParser parser = new PeriodFormatterBuilder().appendWeeks().appendLiteral("-").toParser(); 648 PeriodFormatterBuilder bld = new PeriodFormatterBuilder().append(null, parser).appendMonths(); 649 assertNull(bld.toPrinter()); 650 assertNotNull(bld.toParser()); 651 652 PeriodFormatter f = bld.toFormatter(); 653 try { 654 f.print(PERIOD); 655 fail(); 656 } catch (UnsupportedOperationException ex) {} 657 assertEquals(new Period(0, 2, 1, 0, 0, 0, 0, 0), f.parsePeriod("1-2")); 658 } 659 660 public void testFormatAppend_PrinterParser_PrinterParser() { 661 PeriodPrinter printer = new PeriodFormatterBuilder().appendYears().appendLiteral("-").toPrinter(); 662 PeriodParser parser = new PeriodFormatterBuilder().appendWeeks().appendLiteral("-").toParser(); 663 PeriodFormatterBuilder bld = new PeriodFormatterBuilder().append(printer, parser).appendMonths(); 664 assertNotNull(bld.toPrinter()); 665 assertNotNull(bld.toParser()); 666 667 PeriodFormatter f = bld.toFormatter(); 668 assertEquals("1-2", f.print(PERIOD)); 669 assertEquals(new Period(0, 2, 1, 0, 0, 0, 0, 0), f.parsePeriod("1-2")); 670 } 671 672 public void testFormatAppend_PrinterParser_Printer_null_null_Parser() { 673 PeriodPrinter printer = new PeriodFormatterBuilder().appendYears().appendLiteral("-").toPrinter(); 674 PeriodParser parser = new PeriodFormatterBuilder().appendWeeks().appendLiteral("-").toParser(); 675 PeriodFormatterBuilder bld = new PeriodFormatterBuilder().append(printer, null).append(null, parser); 676 assertNull(bld.toPrinter()); 677 assertNull(bld.toParser()); 678 679 try { 680 bld.toFormatter(); 681 fail(); 682 } catch (IllegalStateException ex) {} 683 } 684 685 public void testFormatAppend_PrinterParserThenClear() { 686 PeriodPrinter printer = new PeriodFormatterBuilder().appendYears().appendLiteral("-").toPrinter(); 687 PeriodParser parser = new PeriodFormatterBuilder().appendWeeks().appendLiteral("-").toParser(); 688 PeriodFormatterBuilder bld = new PeriodFormatterBuilder().append(printer, null).append(null, parser); 689 assertNull(bld.toPrinter()); 690 assertNull(bld.toParser()); 691 bld.clear(); 692 bld.appendMonths(); 693 assertNotNull(bld.toPrinter()); 694 assertNotNull(bld.toParser()); 695 } 696 697 } 698 | Popular Tags |