45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
51 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
52 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
53 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60 package tck.java.time;
61
62 import static java.time.temporal.ChronoField.ERA;
63 import static java.time.temporal.ChronoField.YEAR;
64 import static java.time.temporal.ChronoField.YEAR_OF_ERA;
65 import static org.testng.Assert.assertEquals;
66 import static org.testng.Assert.fail;
67
68 import java.io.ByteArrayOutputStream;
69 import java.io.DataOutputStream;
70 import java.time.Clock;
71 import java.time.DateTimeException;
72 import java.time.Instant;
73 import java.time.LocalDate;
74 import java.time.LocalTime;
75 import java.time.Month;
76 import java.time.MonthDay;
77 import java.time.OffsetDateTime;
78 import java.time.Year;
79 import java.time.YearMonth;
80 import java.time.ZoneId;
81 import java.time.ZoneOffset;
82 import java.time.chrono.IsoChronology;
83 import java.time.format.DateTimeFormatter;
84 import java.time.format.DateTimeParseException;
85 import java.time.temporal.ChronoField;
86 import java.time.temporal.ChronoUnit;
87 import java.time.temporal.JulianFields;
88 import java.time.temporal.Queries;
89 import java.time.temporal.Temporal;
90 import java.time.temporal.TemporalAccessor;
91 import java.time.temporal.TemporalField;
92 import java.time.temporal.TemporalQuery;
93 import java.util.ArrayList;
94 import java.util.Arrays;
95 import java.util.List;
96
97 import org.testng.annotations.BeforeMethod;
98 import org.testng.annotations.DataProvider;
99 import org.testng.annotations.Test;
100
101 /**
102 * Test Year.
103 */
104 @Test
105 public class TCKYear extends AbstractDateTimeTest {
106
107 private static final Year TEST_2008 = Year.of(2008);
108
109 @BeforeMethod
110 public void setUp() {
111 }
112
142 public void test_serialization() throws Exception {
143 assertSerializable(Year.of(2));
144 assertSerializable(Year.of(0));
145 assertSerializable(Year.of(-2));
146 }
147
148 @Test
149 public void test_serialization_format() throws Exception {
150 ByteArrayOutputStream baos = new ByteArrayOutputStream();
151 try (DataOutputStream dos = new DataOutputStream(baos) ) {
152 dos.writeByte(11); // java.time.temporal.Ser.YEAR_TYPE
153 dos.writeInt(2012);
154 }
155 byte[] bytes = baos.toByteArray();
156 assertSerializedBySer(Year.of(2012), bytes);
157 }
158
159 //-----------------------------------------------------------------------
160 // now()
161 //-----------------------------------------------------------------------
162 @Test(groups={"tck"})
163 public void now() {
164 Year expected = Year.now(Clock.systemDefaultZone());
165 Year test = Year.now();
166 for (int i = 0; i < 100; i++) {
167 if (expected.equals(test)) {
168 return;
169 }
170 expected = Year.now(Clock.systemDefaultZone());
171 test = Year.now();
172 }
173 assertEquals(test, expected);
174 }
175
176 //-----------------------------------------------------------------------
177 // now(ZoneId)
178 //-----------------------------------------------------------------------
179 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
180 public void now_ZoneId_nullZoneId() {
181 Year.now((ZoneId) null);
182 }
183
184 @Test(groups={"tck"})
185 public void now_ZoneId() {
186 ZoneId zone = ZoneId.of("UTC+01:02:03");
187 Year expected = Year.now(Clock.system(zone));
188 Year test = Year.now(zone);
189 for (int i = 0; i < 100; i++) {
190 if (expected.equals(test)) {
191 return;
192 }
193 expected = Year.now(Clock.system(zone));
194 test = Year.now(zone);
195 }
196 assertEquals(test, expected);
197 }
198
199 //-----------------------------------------------------------------------
200 // now(Clock)
201 //-----------------------------------------------------------------------
202 @Test(groups={"tck"})
203 public void now_Clock() {
204 Instant instant = OffsetDateTime.of(LocalDate.of(2010, 12, 31), LocalTime.of(0, 0), ZoneOffset.UTC).toInstant();
205 Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
206 Year test = Year.now(clock);
207 assertEquals(test.getValue(), 2010);
208 }
209
210 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
211 public void now_Clock_nullClock() {
212 Year.now((Clock) null);
213 }
214
215 //-----------------------------------------------------------------------
216 @Test(groups={"tck"})
217 public void test_factory_int_singleton() {
218 for (int i = -4; i <= 2104; i++) {
219 Year test = Year.of(i);
220 assertEquals(test.getValue(), i);
221 assertEquals(Year.of(i), test);
222 }
223 }
224
225 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
226 public void test_factory_int_tooLow() {
227 Year.of(Year.MIN_VALUE - 1);
228 }
229
230 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
231 public void test_factory_int_tooHigh() {
232 Year.of(Year.MAX_VALUE + 1);
233 }
234
235 //-----------------------------------------------------------------------
236 @Test(groups={"tck"})
237 public void test_factory_CalendricalObject() {
238 assertEquals(Year.from(LocalDate.of(2007, 7, 15)), Year.of(2007));
239 }
240
241 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
242 public void test_factory_CalendricalObject_invalid_noDerive() {
243 Year.from(LocalTime.of(12, 30));
244 }
245
246 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
247 public void test_factory_CalendricalObject_null() {
248 Year.from((TemporalAccessor) null);
249 }
250
251 //-----------------------------------------------------------------------
252 // parse()
253 //-----------------------------------------------------------------------
254 @DataProvider(name="goodParseData")
255 Object[][] provider_goodParseData() {
256 return new Object[][] {
257 {"0000", Year.of(0)},
258 {"9999", Year.of(9999)},
259 {"2000", Year.of(2000)},
260
261 {"+12345678", Year.of(12345678)},
262 {"+123456", Year.of(123456)},
263 {"-1234", Year.of(-1234)},
264 {"-12345678", Year.of(-12345678)},
265
266 {"+" + Year.MAX_VALUE, Year.of(Year.MAX_VALUE)},
267 {"" + Year.MIN_VALUE, Year.of(Year.MIN_VALUE)},
268 };
269 }
270
271 @Test(dataProvider="goodParseData", groups={"tck"})
272 public void factory_parse_success(String text, Year expected) {
273 Year year = Year.parse(text);
274 assertEquals(year, expected);
275 }
276
277 @DataProvider(name="badParseData")
278 Object[][] provider_badParseData() {
279 return new Object[][] {
280 {"", 0},
281 {"-00", 1},
282 {"--01-0", 1},
283 {"A01", 0},
284 {"200", 0},
285 {"2009/12", 4},
286
287 {"-0000-10", 0},
288 {"-12345678901-10", 11},
289 {"+1-10", 1},
290 {"+12-10", 1},
291 {"+123-10", 1},
292 {"+1234-10", 0},
293 {"12345-10", 0},
294 {"+12345678901-10", 11},
295 };
296 }
297
298 @Test(dataProvider="badParseData", expectedExceptions=DateTimeParseException.class, groups={"tck"})
299 public void factory_parse_fail(String text, int pos) {
300 try {
301 Year.parse(text);
302 fail(String.format("Parse should have failed for %s at position %d", text, pos));
303 } catch (DateTimeParseException ex) {
304 assertEquals(ex.getParsedString(), text);
305 assertEquals(ex.getErrorIndex(), pos);
306 throw ex;
307 }
308 }
309
310 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
311 public void factory_parse_nullText() {
312 Year.parse(null);
313 }
314
315 //-----------------------------------------------------------------------
316 // parse(DateTimeFormatter)
317 //-----------------------------------------------------------------------
318 @Test(groups={"tck"})
319 public void factory_parse_formatter() {
320 DateTimeFormatter f = DateTimeFormatter.ofPattern("y");
321 Year test = Year.parse("2010", f);
322 assertEquals(test, Year.of(2010));
323 }
324
325 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
326 public void factory_parse_formatter_nullText() {
327 DateTimeFormatter f = DateTimeFormatter.ofPattern("y");
328 Year.parse((String) null, f);
329 }
330
331 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
332 public void factory_parse_formatter_nullFormatter() {
333 Year.parse("ANY", null);
334 }
335
336 //-----------------------------------------------------------------------
337 // get(TemporalField)
338 //-----------------------------------------------------------------------
339 @Test
340 public void test_get_TemporalField() {
341 assertEquals(TEST_2008.get(ChronoField.YEAR), 2008);
342 assertEquals(TEST_2008.get(ChronoField.YEAR_OF_ERA), 2008);
343 assertEquals(TEST_2008.get(ChronoField.ERA), 1);
344 }
345
346 @Test
347 public void test_getLong_TemporalField() {
348 assertEquals(TEST_2008.getLong(ChronoField.YEAR), 2008);
349 assertEquals(TEST_2008.getLong(ChronoField.YEAR_OF_ERA), 2008);
350 assertEquals(TEST_2008.getLong(ChronoField.ERA), 1);
351 }
352
353 //-----------------------------------------------------------------------
354 // query(TemporalQuery)
355 //-----------------------------------------------------------------------
356 @DataProvider(name="query")
357 Object[][] data_query() {
358 return new Object[][] {
359 {TEST_2008, Queries.chronology(), IsoChronology.INSTANCE},
360 {TEST_2008, Queries.zoneId(), null},
361 {TEST_2008, Queries.precision(), ChronoUnit.YEARS},
362 {TEST_2008, Queries.zone(), null},
363 {TEST_2008, Queries.offset(), null},
364 {TEST_2008, Queries.localDate(), null},
365 {TEST_2008, Queries.localTime(), null},
366 };
367 }
368
369 @Test(dataProvider="query")
370 public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
371 assertEquals(temporal.query(query), expected);
372 }
373
374 @Test(dataProvider="query")
375 public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
376 assertEquals(query.queryFrom(temporal), expected);
377 }
378
379 @Test(expectedExceptions=NullPointerException.class)
380 public void test_query_null() {
381 TEST_2008.query(null);
382 }
383
384 //-----------------------------------------------------------------------
385 // isLeap()
386 //-----------------------------------------------------------------------
387 @Test(groups={"tck"})
388 public void test_isLeap() {
389 assertEquals(Year.of(1999).isLeap(), false);
390 assertEquals(Year.of(2000).isLeap(), true);
391 assertEquals(Year.of(2001).isLeap(), false);
392
393 assertEquals(Year.of(2007).isLeap(), false);
394 assertEquals(Year.of(2008).isLeap(), true);
395 assertEquals(Year.of(2009).isLeap(), false);
396 assertEquals(Year.of(2010).isLeap(), false);
397 assertEquals(Year.of(2011).isLeap(), false);
398 assertEquals(Year.of(2012).isLeap(), true);
399
400 assertEquals(Year.of(2095).isLeap(), false);
401 assertEquals(Year.of(2096).isLeap(), true);
402 assertEquals(Year.of(2097).isLeap(), false);
403 assertEquals(Year.of(2098).isLeap(), false);
404 assertEquals(Year.of(2099).isLeap(), false);
405 assertEquals(Year.of(2100).isLeap(), false);
406 assertEquals(Year.of(2101).isLeap(), false);
407 assertEquals(Year.of(2102).isLeap(), false);
408 assertEquals(Year.of(2103).isLeap(), false);
409 assertEquals(Year.of(2104).isLeap(), true);
410 assertEquals(Year.of(2105).isLeap(), false);
411
412 assertEquals(Year.of(-500).isLeap(), false);
413 assertEquals(Year.of(-400).isLeap(), true);
414 assertEquals(Year.of(-300).isLeap(), false);
415 assertEquals(Year.of(-200).isLeap(), false);
416 assertEquals(Year.of(-100).isLeap(), false);
417 assertEquals(Year.of(0).isLeap(), true);
418 assertEquals(Year.of(100).isLeap(), false);
419 assertEquals(Year.of(200).isLeap(), false);
420 assertEquals(Year.of(300).isLeap(), false);
421 assertEquals(Year.of(400).isLeap(), true);
422 assertEquals(Year.of(500).isLeap(), false);
423 }
424
425 //-----------------------------------------------------------------------
426 // plusYears()
427 //-----------------------------------------------------------------------
428 @Test(groups={"tck"})
429 public void test_plusYears() {
430 assertEquals(Year.of(2007).plusYears(-1), Year.of(2006));
431 assertEquals(Year.of(2007).plusYears(0), Year.of(2007));
432 assertEquals(Year.of(2007).plusYears(1), Year.of(2008));
433 assertEquals(Year.of(2007).plusYears(2), Year.of(2009));
434
435 assertEquals(Year.of(Year.MAX_VALUE - 1).plusYears(1), Year.of(Year.MAX_VALUE));
436 assertEquals(Year.of(Year.MAX_VALUE).plusYears(0), Year.of(Year.MAX_VALUE));
437
438 assertEquals(Year.of(Year.MIN_VALUE + 1).plusYears(-1), Year.of(Year.MIN_VALUE));
439 assertEquals(Year.of(Year.MIN_VALUE).plusYears(0), Year.of(Year.MIN_VALUE));
440 }
441
442 @Test(groups={"tck"})
443 public void test_plusYear_zero_equals() {
444 Year base = Year.of(2007);
445 assertEquals(base.plusYears(0), base);
446 }
447
448 @Test(groups={"tck"})
449 public void test_plusYears_big() {
450 long years = 20L + Year.MAX_VALUE;
451 assertEquals(Year.of(-40).plusYears(years), Year.of((int) (-40L + years)));
452 }
453
454 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
455 public void test_plusYears_max() {
456 Year.of(Year.MAX_VALUE).plusYears(1);
457 }
458
459 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
460 public void test_plusYears_maxLots() {
461 Year.of(Year.MAX_VALUE).plusYears(1000);
462 }
463
464 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
465 public void test_plusYears_min() {
466 Year.of(Year.MIN_VALUE).plusYears(-1);
467 }
468
469 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
470 public void test_plusYears_minLots() {
471 Year.of(Year.MIN_VALUE).plusYears(-1000);
472 }
473
474 //-----------------------------------------------------------------------
475 // minusYears()
476 //-----------------------------------------------------------------------
477 @Test(groups={"tck"})
478 public void test_minusYears() {
479 assertEquals(Year.of(2007).minusYears(-1), Year.of(2008));
480 assertEquals(Year.of(2007).minusYears(0), Year.of(2007));
481 assertEquals(Year.of(2007).minusYears(1), Year.of(2006));
482 assertEquals(Year.of(2007).minusYears(2), Year.of(2005));
483
484 assertEquals(Year.of(Year.MAX_VALUE - 1).minusYears(-1), Year.of(Year.MAX_VALUE));
485 assertEquals(Year.of(Year.MAX_VALUE).minusYears(0), Year.of(Year.MAX_VALUE));
486
487 assertEquals(Year.of(Year.MIN_VALUE + 1).minusYears(1), Year.of(Year.MIN_VALUE));
488 assertEquals(Year.of(Year.MIN_VALUE).minusYears(0), Year.of(Year.MIN_VALUE));
489 }
490
491 @Test(groups={"tck"})
492 public void test_minusYear_zero_equals() {
493 Year base = Year.of(2007);
494 assertEquals(base.minusYears(0), base);
495 }
496
497 @Test(groups={"tck"})
498 public void test_minusYears_big() {
499 long years = 20L + Year.MAX_VALUE;
500 assertEquals(Year.of(40).minusYears(years), Year.of((int) (40L - years)));
501 }
502
503 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
504 public void test_minusYears_max() {
505 Year.of(Year.MAX_VALUE).minusYears(-1);
506 }
507
508 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
509 public void test_minusYears_maxLots() {
510 Year.of(Year.MAX_VALUE).minusYears(-1000);
511 }
512
513 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
514 public void test_minusYears_min() {
515 Year.of(Year.MIN_VALUE).minusYears(1);
516 }
517
518 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
519 public void test_minusYears_minLots() {
520 Year.of(Year.MIN_VALUE).minusYears(1000);
521 }
522
523 //-----------------------------------------------------------------------
524 // adjustInto()
525 //-----------------------------------------------------------------------
526 @Test(groups={"tck"})
527 public void test_adjustDate() {
528 LocalDate base = LocalDate.of(2007, 2, 12);
529 for (int i = -4; i <= 2104; i++) {
530 Temporal result = Year.of(i).adjustInto(base);
531 assertEquals(result, LocalDate.of(i, 2, 12));
532 }
533 }
534
535 @Test(groups={"tck"})
536 public void test_adjustDate_resolve() {
537 Year test = Year.of(2011);
538 assertEquals(test.adjustInto(LocalDate.of(2012, 2, 29)), LocalDate.of(2011, 2, 28));
539 }
540
541 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
542 public void test_adjustDate_nullLocalDate() {
543 Year test = Year.of(1);
544 test.adjustInto((LocalDate) null);
545 }
546
547 //-----------------------------------------------------------------------
548 // length()
549 //-----------------------------------------------------------------------
550 @Test(groups={"tck"})
551 public void test_length() {
552 assertEquals(Year.of(1999).length(), 365);
553 assertEquals(Year.of(2000).length(), 366);
554 assertEquals(Year.of(2001).length(), 365);
555
556 assertEquals(Year.of(2007).length(), 365);
557 assertEquals(Year.of(2008).length(), 366);
558 assertEquals(Year.of(2009).length(), 365);
559 assertEquals(Year.of(2010).length(), 365);
560 assertEquals(Year.of(2011).length(), 365);
561 assertEquals(Year.of(2012).length(), 366);
562
563 assertEquals(Year.of(2095).length(), 365);
564 assertEquals(Year.of(2096).length(), 366);
565 assertEquals(Year.of(2097).length(), 365);
566 assertEquals(Year.of(2098).length(), 365);
567 assertEquals(Year.of(2099).length(), 365);
568 assertEquals(Year.of(2100).length(), 365);
569 assertEquals(Year.of(2101).length(), 365);
570 assertEquals(Year.of(2102).length(), 365);
589 // isValidMonthDay(MonthDay)
590 //-----------------------------------------------------------------------
591 @DataProvider(name="isValidMonthDay")
592 Object[][] data_isValidMonthDay() {
593 return new Object[][] {
594 {Year.of(2007), MonthDay.of(6, 30), true},
595 {Year.of(2008), MonthDay.of(2, 28), true},
596 {Year.of(2008), MonthDay.of(2, 29), true},
597 {Year.of(2009), MonthDay.of(2, 28), true},
598 {Year.of(2009), MonthDay.of(2, 29), false},
599 {Year.of(2009), null, false},
600 };
601 }
602
603 @Test(dataProvider="isValidMonthDay")
604 public void test_isValidMonthDay(Year year, MonthDay monthDay, boolean expected) {
605 assertEquals(year.isValidMonthDay(monthDay), expected);
606 }
607
608 //-----------------------------------------------------------------------
609 // atMonth(Month)
610 //-----------------------------------------------------------------------
611 @Test
612 public void test_atMonth() {
613 Year test = Year.of(2008);
614 assertEquals(test.atMonth(Month.JUNE), YearMonth.of(2008, 6));
615 }
616
617 @Test(expectedExceptions=NullPointerException.class)
618 public void test_atMonth_nullMonth() {
619 Year test = Year.of(2008);
620 test.atMonth((Month) null);
621 }
622
623 //-----------------------------------------------------------------------
624 // atMonth(int)
625 //-----------------------------------------------------------------------
626 @Test
627 public void test_atMonth_int() {
628 Year test = Year.of(2008);
629 assertEquals(test.atMonth(6), YearMonth.of(2008, 6));
630 }
631
632 @Test(expectedExceptions=DateTimeException.class)
633 public void test_atMonth_int_invalidMonth() {
634 Year test = Year.of(2008);
635 test.atMonth(13);
636 }
637
638 //-----------------------------------------------------------------------
639 // atMonthDay(Month)
640 //-----------------------------------------------------------------------
641 @DataProvider(name="atMonthDay")
642 Object[][] data_atMonthDay() {
643 return new Object[][] {
644 {Year.of(2008), MonthDay.of(6, 30), LocalDate.of(2008, 6, 30)},
645 {Year.of(2008), MonthDay.of(2, 29), LocalDate.of(2008, 2, 29)},
646 {Year.of(2009), MonthDay.of(2, 29), LocalDate.of(2009, 2, 28)},
647 };
648 }
649
650 @Test(dataProvider="atMonthDay")
651 public void test_atMonthDay(Year year, MonthDay monthDay, LocalDate expected) {
652 assertEquals(year.atMonthDay(monthDay), expected);
653 }
654
655 @Test(expectedExceptions=NullPointerException.class)
656 public void test_atMonthDay_nullMonthDay() {
657 Year test = Year.of(2008);
658 test.atMonthDay((MonthDay) null);
659 }
660
661 //-----------------------------------------------------------------------
662 // atDay(int)
663 //-----------------------------------------------------------------------
664 @Test(groups={"tck"})
665 public void test_atDay_notLeapYear() {
666 Year test = Year.of(2007);
667 LocalDate expected = LocalDate.of(2007, 1, 1);
668 for (int i = 1; i <= 365; i++) {
669 assertEquals(test.atDay(i), expected);
670 expected = expected.plusDays(1);
671 }
672 }
673
674 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
675 public void test_atDay_notLeapYear_day366() {
676 Year test = Year.of(2007);
677 test.atDay(366);
678 }
679
680 @Test(groups={"tck"})
681 public void test_atDay_leapYear() {
682 Year test = Year.of(2008);
683 LocalDate expected = LocalDate.of(2008, 1, 1);
684 for (int i = 1; i <= 366; i++) {
685 assertEquals(test.atDay(i), expected);
686 expected = expected.plusDays(1);
687 }
688 }
689
690 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
691 public void test_atDay_day0() {
692 Year test = Year.of(2007);
693 test.atDay(0);
694 }
695
696 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
697 public void test_atDay_day367() {
698 Year test = Year.of(2007);
699 test.atDay(367);
700 }
701
702 //-----------------------------------------------------------------------
703 // compareTo()
704 //-----------------------------------------------------------------------
705 @Test(groups={"tck"})
706 public void test_compareTo() {
707 for (int i = -4; i <= 2104; i++) {
708 Year a = Year.of(i);
709 for (int j = -4; j <= 2104; j++) {
710 Year b = Year.of(j);
711 if (i < j) {
712 assertEquals(a.compareTo(b) < 0, true);
713 assertEquals(b.compareTo(a) > 0, true);
714 assertEquals(a.isAfter(b), false);
715 assertEquals(a.isBefore(b), true);
716 assertEquals(b.isAfter(a), true);
717 assertEquals(b.isBefore(a), false);
718 } else if (i > j) {
719 assertEquals(a.compareTo(b) > 0, true);
720 assertEquals(b.compareTo(a) < 0, true);
721 assertEquals(a.isAfter(b), true);
722 assertEquals(a.isBefore(b), false);
723 assertEquals(b.isAfter(a), false);
724 assertEquals(b.isBefore(a), true);
725 } else {
726 assertEquals(a.compareTo(b), 0);
727 assertEquals(b.compareTo(a), 0);
728 assertEquals(a.isAfter(b), false);
729 assertEquals(a.isBefore(b), false);
730 assertEquals(b.isAfter(a), false);
731 assertEquals(b.isBefore(a), false);
732 }
733 }
734 }
735 }
736
737 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
738 public void test_compareTo_nullYear() {
739 Year doy = null;
740 Year test = Year.of(1);
741 test.compareTo(doy);
742 }
743
744 //-----------------------------------------------------------------------
745 // equals() / hashCode()
746 //-----------------------------------------------------------------------
747 @Test(groups={"tck"})
748 public void test_equals() {
749 for (int i = -4; i <= 2104; i++) {
750 Year a = Year.of(i);
751 for (int j = -4; j <= 2104; j++) {
752 Year b = Year.of(j);
753 assertEquals(a.equals(b), i == j);
754 assertEquals(a.hashCode() == b.hashCode(), i == j);
755 }
756 }
757 }
758
759 @Test(groups={"tck"})
760 public void test_equals_same() {
761 Year test = Year.of(2011);
762 assertEquals(test.equals(test), true);
763 }
764
765 @Test(groups={"tck"})
766 public void test_equals_nullYear() {
767 Year doy = null;
768 Year test = Year.of(1);
769 assertEquals(test.equals(doy), false);
770 }
771
772 @Test(groups={"tck"})
773 public void test_equals_incorrectType() {
774 Year test = Year.of(1);
775 assertEquals(test.equals("Incorrect type"), false);
776 }
777
778 //-----------------------------------------------------------------------
779 // toString()
780 //-----------------------------------------------------------------------
781 @Test(groups={"tck"})
782 public void test_toString() {
783 for (int i = -4; i <= 2104; i++) {
784 Year a = Year.of(i);
785 assertEquals(a.toString(), "" + i);
786 }
787 }
788
789 //-----------------------------------------------------------------------
790 // toString(DateTimeFormatter)
791 //-----------------------------------------------------------------------
792 @Test(groups={"tck"})
793 public void test_toString_formatter() {
794 DateTimeFormatter f = DateTimeFormatter.ofPattern("y");
795 String t = Year.of(2010).toString(f);
796 assertEquals(t, "2010");
797 }
798
799 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
800 public void test_toString_formatter_null() {
801 Year.of(2010).toString(null);
802 }
803
804 }
|
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
51 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
52 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
53 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60 package tck.java.time;
61
62 import static java.time.temporal.ChronoField.ERA;
63 import static java.time.temporal.ChronoField.YEAR;
64 import static java.time.temporal.ChronoField.YEAR_OF_ERA;
65 import static java.time.temporal.ChronoUnit.CENTURIES;
66 import static java.time.temporal.ChronoUnit.DAYS;
67 import static java.time.temporal.ChronoUnit.DECADES;
68 import static java.time.temporal.ChronoUnit.HOURS;
69 import static java.time.temporal.ChronoUnit.MILLENNIA;
70 import static java.time.temporal.ChronoUnit.MONTHS;
71 import static java.time.temporal.ChronoUnit.WEEKS;
72 import static java.time.temporal.ChronoUnit.YEARS;
73 import static org.testng.Assert.assertEquals;
74 import static org.testng.Assert.fail;
75
76 import java.io.ByteArrayOutputStream;
77 import java.io.DataOutputStream;
78 import java.time.Clock;
79 import java.time.DateTimeException;
80 import java.time.Duration;
81 import java.time.Instant;
82 import java.time.LocalDate;
83 import java.time.LocalTime;
84 import java.time.Month;
85 import java.time.MonthDay;
86 import java.time.OffsetDateTime;
87 import java.time.Period;
88 import java.time.Year;
89 import java.time.YearMonth;
90 import java.time.ZoneId;
91 import java.time.ZoneOffset;
92 import java.time.chrono.IsoChronology;
93 import java.time.format.DateTimeFormatter;
94 import java.time.format.DateTimeParseException;
95 import java.time.temporal.ChronoField;
96 import java.time.temporal.ChronoUnit;
97 import java.time.temporal.JulianFields;
98 import java.time.temporal.Temporal;
99 import java.time.temporal.TemporalAccessor;
100 import java.time.temporal.TemporalAmount;
101 import java.time.temporal.TemporalField;
102 import java.time.temporal.TemporalQuery;
103 import java.time.temporal.TemporalUnit;
104 import java.time.temporal.UnsupportedTemporalTypeException;
105 import java.util.ArrayList;
106 import java.util.Arrays;
107 import java.util.List;
108
109 import org.testng.annotations.BeforeMethod;
110 import org.testng.annotations.DataProvider;
111 import org.testng.annotations.Test;
112
113 /**
114 * Test Year.
115 */
116 @Test
117 public class TCKYear extends AbstractDateTimeTest {
118
119 private static final Year TEST_2008 = Year.of(2008);
120
121 @BeforeMethod
122 public void setUp() {
123 }
124
154 public void test_serialization() throws Exception {
155 assertSerializable(Year.of(2));
156 assertSerializable(Year.of(0));
157 assertSerializable(Year.of(-2));
158 }
159
160 @Test
161 public void test_serialization_format() throws Exception {
162 ByteArrayOutputStream baos = new ByteArrayOutputStream();
163 try (DataOutputStream dos = new DataOutputStream(baos) ) {
164 dos.writeByte(11); // java.time.temporal.Ser.YEAR_TYPE
165 dos.writeInt(2012);
166 }
167 byte[] bytes = baos.toByteArray();
168 assertSerializedBySer(Year.of(2012), bytes);
169 }
170
171 //-----------------------------------------------------------------------
172 // now()
173 //-----------------------------------------------------------------------
174 @Test
175 public void now() {
176 Year expected = Year.now(Clock.systemDefaultZone());
177 Year test = Year.now();
178 for (int i = 0; i < 100; i++) {
179 if (expected.equals(test)) {
180 return;
181 }
182 expected = Year.now(Clock.systemDefaultZone());
183 test = Year.now();
184 }
185 assertEquals(test, expected);
186 }
187
188 //-----------------------------------------------------------------------
189 // now(ZoneId)
190 //-----------------------------------------------------------------------
191 @Test(expectedExceptions=NullPointerException.class)
192 public void now_ZoneId_nullZoneId() {
193 Year.now((ZoneId) null);
194 }
195
196 @Test
197 public void now_ZoneId() {
198 ZoneId zone = ZoneId.of("UTC+01:02:03");
199 Year expected = Year.now(Clock.system(zone));
200 Year test = Year.now(zone);
201 for (int i = 0; i < 100; i++) {
202 if (expected.equals(test)) {
203 return;
204 }
205 expected = Year.now(Clock.system(zone));
206 test = Year.now(zone);
207 }
208 assertEquals(test, expected);
209 }
210
211 //-----------------------------------------------------------------------
212 // now(Clock)
213 //-----------------------------------------------------------------------
214 @Test
215 public void now_Clock() {
216 Instant instant = OffsetDateTime.of(LocalDate.of(2010, 12, 31), LocalTime.of(0, 0), ZoneOffset.UTC).toInstant();
217 Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
218 Year test = Year.now(clock);
219 assertEquals(test.getValue(), 2010);
220 }
221
222 @Test(expectedExceptions=NullPointerException.class)
223 public void now_Clock_nullClock() {
224 Year.now((Clock) null);
225 }
226
227 //-----------------------------------------------------------------------
228 @Test
229 public void test_factory_int_singleton() {
230 for (int i = -4; i <= 2104; i++) {
231 Year test = Year.of(i);
232 assertEquals(test.getValue(), i);
233 assertEquals(Year.of(i), test);
234 }
235 }
236
237 @Test(expectedExceptions=DateTimeException.class)
238 public void test_factory_int_tooLow() {
239 Year.of(Year.MIN_VALUE - 1);
240 }
241
242 @Test(expectedExceptions=DateTimeException.class)
243 public void test_factory_int_tooHigh() {
244 Year.of(Year.MAX_VALUE + 1);
245 }
246
247 //-----------------------------------------------------------------------
248 @Test
249 public void test_factory_CalendricalObject() {
250 assertEquals(Year.from(LocalDate.of(2007, 7, 15)), Year.of(2007));
251 }
252
253 @Test(expectedExceptions=DateTimeException.class)
254 public void test_factory_CalendricalObject_invalid_noDerive() {
255 Year.from(LocalTime.of(12, 30));
256 }
257
258 @Test(expectedExceptions=NullPointerException.class)
259 public void test_factory_CalendricalObject_null() {
260 Year.from((TemporalAccessor) null);
261 }
262
263 //-----------------------------------------------------------------------
264 // parse()
265 //-----------------------------------------------------------------------
266 @DataProvider(name="goodParseData")
267 Object[][] provider_goodParseData() {
268 return new Object[][] {
269 {"0000", Year.of(0)},
270 {"9999", Year.of(9999)},
271 {"2000", Year.of(2000)},
272
273 {"+12345678", Year.of(12345678)},
274 {"+123456", Year.of(123456)},
275 {"-1234", Year.of(-1234)},
276 {"-12345678", Year.of(-12345678)},
277
278 {"+" + Year.MAX_VALUE, Year.of(Year.MAX_VALUE)},
279 {"" + Year.MIN_VALUE, Year.of(Year.MIN_VALUE)},
280 };
281 }
282
283 @Test(dataProvider="goodParseData")
284 public void factory_parse_success(String text, Year expected) {
285 Year year = Year.parse(text);
286 assertEquals(year, expected);
287 }
288
289 @DataProvider(name="badParseData")
290 Object[][] provider_badParseData() {
291 return new Object[][] {
292 {"", 0},
293 {"-00", 1},
294 {"--01-0", 1},
295 {"A01", 0},
296 {"200", 0},
297 {"2009/12", 4},
298
299 {"-0000-10", 0},
300 {"-12345678901-10", 11},
301 {"+1-10", 1},
302 {"+12-10", 1},
303 {"+123-10", 1},
304 {"+1234-10", 0},
305 {"12345-10", 0},
306 {"+12345678901-10", 11},
307 };
308 }
309
310 @Test(dataProvider="badParseData", expectedExceptions=DateTimeParseException.class)
311 public void factory_parse_fail(String text, int pos) {
312 try {
313 Year.parse(text);
314 fail(String.format("Parse should have failed for %s at position %d", text, pos));
315 } catch (DateTimeParseException ex) {
316 assertEquals(ex.getParsedString(), text);
317 assertEquals(ex.getErrorIndex(), pos);
318 throw ex;
319 }
320 }
321
322 @Test(expectedExceptions=NullPointerException.class)
323 public void factory_parse_nullText() {
324 Year.parse(null);
325 }
326
327 //-----------------------------------------------------------------------
328 // parse(DateTimeFormatter)
329 //-----------------------------------------------------------------------
330 @Test
331 public void factory_parse_formatter() {
332 DateTimeFormatter f = DateTimeFormatter.ofPattern("y");
333 Year test = Year.parse("2010", f);
334 assertEquals(test, Year.of(2010));
335 }
336
337 @Test(expectedExceptions=NullPointerException.class)
338 public void factory_parse_formatter_nullText() {
339 DateTimeFormatter f = DateTimeFormatter.ofPattern("y");
340 Year.parse((String) null, f);
341 }
342
343 @Test(expectedExceptions=NullPointerException.class)
344 public void factory_parse_formatter_nullFormatter() {
345 Year.parse("ANY", null);
346 }
347
348 //-----------------------------------------------------------------------
349 // get(TemporalField)
350 //-----------------------------------------------------------------------
351 @Test
352 public void test_get_TemporalField() {
353 assertEquals(TEST_2008.get(ChronoField.YEAR), 2008);
354 assertEquals(TEST_2008.get(ChronoField.YEAR_OF_ERA), 2008);
355 assertEquals(TEST_2008.get(ChronoField.ERA), 1);
356 }
357
358 @Test
359 public void test_getLong_TemporalField() {
360 assertEquals(TEST_2008.getLong(ChronoField.YEAR), 2008);
361 assertEquals(TEST_2008.getLong(ChronoField.YEAR_OF_ERA), 2008);
362 assertEquals(TEST_2008.getLong(ChronoField.ERA), 1);
363 }
364
365 //-----------------------------------------------------------------------
366 // query(TemporalQuery)
367 //-----------------------------------------------------------------------
368 @DataProvider(name="query")
369 Object[][] data_query() {
370 return new Object[][] {
371 {TEST_2008, TemporalQuery.chronology(), IsoChronology.INSTANCE},
372 {TEST_2008, TemporalQuery.zoneId(), null},
373 {TEST_2008, TemporalQuery.precision(), ChronoUnit.YEARS},
374 {TEST_2008, TemporalQuery.zone(), null},
375 {TEST_2008, TemporalQuery.offset(), null},
376 {TEST_2008, TemporalQuery.localDate(), null},
377 {TEST_2008, TemporalQuery.localTime(), null},
378 };
379 }
380
381 @Test(dataProvider="query")
382 public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
383 assertEquals(temporal.query(query), expected);
384 }
385
386 @Test(dataProvider="query")
387 public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
388 assertEquals(query.queryFrom(temporal), expected);
389 }
390
391 @Test(expectedExceptions=NullPointerException.class)
392 public void test_query_null() {
393 TEST_2008.query(null);
394 }
395
396 //-----------------------------------------------------------------------
397 // isLeap()
398 //-----------------------------------------------------------------------
399 @Test
400 public void test_isLeap() {
401 assertEquals(Year.of(1999).isLeap(), false);
402 assertEquals(Year.of(2000).isLeap(), true);
403 assertEquals(Year.of(2001).isLeap(), false);
404
405 assertEquals(Year.of(2007).isLeap(), false);
406 assertEquals(Year.of(2008).isLeap(), true);
407 assertEquals(Year.of(2009).isLeap(), false);
408 assertEquals(Year.of(2010).isLeap(), false);
409 assertEquals(Year.of(2011).isLeap(), false);
410 assertEquals(Year.of(2012).isLeap(), true);
411
412 assertEquals(Year.of(2095).isLeap(), false);
413 assertEquals(Year.of(2096).isLeap(), true);
414 assertEquals(Year.of(2097).isLeap(), false);
415 assertEquals(Year.of(2098).isLeap(), false);
416 assertEquals(Year.of(2099).isLeap(), false);
417 assertEquals(Year.of(2100).isLeap(), false);
418 assertEquals(Year.of(2101).isLeap(), false);
419 assertEquals(Year.of(2102).isLeap(), false);
420 assertEquals(Year.of(2103).isLeap(), false);
421 assertEquals(Year.of(2104).isLeap(), true);
422 assertEquals(Year.of(2105).isLeap(), false);
423
424 assertEquals(Year.of(-500).isLeap(), false);
425 assertEquals(Year.of(-400).isLeap(), true);
426 assertEquals(Year.of(-300).isLeap(), false);
427 assertEquals(Year.of(-200).isLeap(), false);
428 assertEquals(Year.of(-100).isLeap(), false);
429 assertEquals(Year.of(0).isLeap(), true);
430 assertEquals(Year.of(100).isLeap(), false);
431 assertEquals(Year.of(200).isLeap(), false);
432 assertEquals(Year.of(300).isLeap(), false);
433 assertEquals(Year.of(400).isLeap(), true);
434 assertEquals(Year.of(500).isLeap(), false);
435 }
436
437 //-----------------------------------------------------------------------
438 // plus(Period)
439 //-----------------------------------------------------------------------
440 @DataProvider(name="plusValid")
441 Object[][] data_plusValid() {
442 return new Object[][] {
443 {2012, Period.ofYears(0), 2012},
444 {2012, Period.ofYears(1), 2013},
445 {2012, Period.ofYears(2), 2014},
446 {2012, Period.ofYears(-2), 2010},
447 };
448 }
449
450 @Test(dataProvider="plusValid")
451 public void test_plusValid(int year, TemporalAmount amount, int expected) {
452 assertEquals(Year.of(year).plus(amount), Year.of(expected));
453 }
454
455 @DataProvider(name="plusInvalidUnit")
456 Object[][] data_plusInvalidUnit() {
457 return new Object[][] {
458 {Period.of(0, 1, 0)},
459 {Period.of(0, 0, 1)},
460 {Period.of(0, 1, 1)},
461 {Period.of(1, 1, 1)},
462 {Duration.ofDays(1)},
463 {Duration.ofHours(1)},
464 {Duration.ofMinutes(1)},
465 {Duration.ofSeconds(1)},
466 };
467 }
468
469 @Test(dataProvider="plusInvalidUnit", expectedExceptions=UnsupportedTemporalTypeException.class)
470 public void test_plusInvalidUnit(TemporalAmount amount) {
471 TEST_2008.plus(amount);
472 }
473
474 @Test(expectedExceptions=NullPointerException.class)
475 public void test_plus_null() {
476 TEST_2008.plus(null);
477 }
478
479 //-----------------------------------------------------------------------
480 // plusYears()
481 //-----------------------------------------------------------------------
482 @Test
483 public void test_plusYears() {
484 assertEquals(Year.of(2007).plusYears(-1), Year.of(2006));
485 assertEquals(Year.of(2007).plusYears(0), Year.of(2007));
486 assertEquals(Year.of(2007).plusYears(1), Year.of(2008));
487 assertEquals(Year.of(2007).plusYears(2), Year.of(2009));
488
489 assertEquals(Year.of(Year.MAX_VALUE - 1).plusYears(1), Year.of(Year.MAX_VALUE));
490 assertEquals(Year.of(Year.MAX_VALUE).plusYears(0), Year.of(Year.MAX_VALUE));
491
492 assertEquals(Year.of(Year.MIN_VALUE + 1).plusYears(-1), Year.of(Year.MIN_VALUE));
493 assertEquals(Year.of(Year.MIN_VALUE).plusYears(0), Year.of(Year.MIN_VALUE));
494 }
495
496 @Test
497 public void test_plusYear_zero_equals() {
498 Year base = Year.of(2007);
499 assertEquals(base.plusYears(0), base);
500 }
501
502 @Test
503 public void test_plusYears_big() {
504 long years = 20L + Year.MAX_VALUE;
505 assertEquals(Year.of(-40).plusYears(years), Year.of((int) (-40L + years)));
506 }
507
508 @Test(expectedExceptions=DateTimeException.class)
509 public void test_plusYears_max() {
510 Year.of(Year.MAX_VALUE).plusYears(1);
511 }
512
513 @Test(expectedExceptions=DateTimeException.class)
514 public void test_plusYears_maxLots() {
515 Year.of(Year.MAX_VALUE).plusYears(1000);
516 }
517
518 @Test(expectedExceptions=DateTimeException.class)
519 public void test_plusYears_min() {
520 Year.of(Year.MIN_VALUE).plusYears(-1);
521 }
522
523 @Test(expectedExceptions=DateTimeException.class)
524 public void test_plusYears_minLots() {
525 Year.of(Year.MIN_VALUE).plusYears(-1000);
526 }
527
528 //-----------------------------------------------------------------------
529 // minus(Period)
530 //-----------------------------------------------------------------------
531 @DataProvider(name="minusValid")
532 Object[][] data_minusValid() {
533 return new Object[][] {
534 {2012, Period.ofYears(0), 2012},
535 {2012, Period.ofYears(1), 2011},
536 {2012, Period.ofYears(2), 2010},
537 {2012, Period.ofYears(-2), 2014},
538 };
539 }
540
541 @Test(dataProvider="minusValid")
542 public void test_minusValid(int year, TemporalAmount amount, int expected) {
543 assertEquals(Year.of(year).minus(amount), Year.of(expected));
544 }
545
546 @DataProvider(name="minusInvalidUnit")
547 Object[][] data_minusInvalidUnit() {
548 return new Object[][] {
549 {Period.of(0, 1, 0)},
550 {Period.of(0, 0, 1)},
551 {Period.of(0, 1, 1)},
552 {Period.of(1, 1, 1)},
553 {Duration.ofDays(1)},
554 {Duration.ofHours(1)},
555 {Duration.ofMinutes(1)},
556 {Duration.ofSeconds(1)},
557 };
558 }
559
560 @Test(dataProvider="minusInvalidUnit", expectedExceptions=UnsupportedTemporalTypeException.class)
561 public void test_minusInvalidUnit(TemporalAmount amount) {
562 TEST_2008.minus(amount);
563 }
564
565 @Test(expectedExceptions=NullPointerException.class)
566 public void test_minus_null() {
567 TEST_2008.minus(null);
568 }
569
570 //-----------------------------------------------------------------------
571 // minusYears()
572 //-----------------------------------------------------------------------
573 @Test
574 public void test_minusYears() {
575 assertEquals(Year.of(2007).minusYears(-1), Year.of(2008));
576 assertEquals(Year.of(2007).minusYears(0), Year.of(2007));
577 assertEquals(Year.of(2007).minusYears(1), Year.of(2006));
578 assertEquals(Year.of(2007).minusYears(2), Year.of(2005));
579
580 assertEquals(Year.of(Year.MAX_VALUE - 1).minusYears(-1), Year.of(Year.MAX_VALUE));
581 assertEquals(Year.of(Year.MAX_VALUE).minusYears(0), Year.of(Year.MAX_VALUE));
582
583 assertEquals(Year.of(Year.MIN_VALUE + 1).minusYears(1), Year.of(Year.MIN_VALUE));
584 assertEquals(Year.of(Year.MIN_VALUE).minusYears(0), Year.of(Year.MIN_VALUE));
585 }
586
587 @Test
588 public void test_minusYear_zero_equals() {
589 Year base = Year.of(2007);
590 assertEquals(base.minusYears(0), base);
591 }
592
593 @Test
594 public void test_minusYears_big() {
595 long years = 20L + Year.MAX_VALUE;
596 assertEquals(Year.of(40).minusYears(years), Year.of((int) (40L - years)));
597 }
598
599 @Test(expectedExceptions=DateTimeException.class)
600 public void test_minusYears_max() {
601 Year.of(Year.MAX_VALUE).minusYears(-1);
602 }
603
604 @Test(expectedExceptions=DateTimeException.class)
605 public void test_minusYears_maxLots() {
606 Year.of(Year.MAX_VALUE).minusYears(-1000);
607 }
608
609 @Test(expectedExceptions=DateTimeException.class)
610 public void test_minusYears_min() {
611 Year.of(Year.MIN_VALUE).minusYears(1);
612 }
613
614 @Test(expectedExceptions=DateTimeException.class)
615 public void test_minusYears_minLots() {
616 Year.of(Year.MIN_VALUE).minusYears(1000);
617 }
618
619 //-----------------------------------------------------------------------
620 // adjustInto()
621 //-----------------------------------------------------------------------
622 @Test
623 public void test_adjustDate() {
624 LocalDate base = LocalDate.of(2007, 2, 12);
625 for (int i = -4; i <= 2104; i++) {
626 Temporal result = Year.of(i).adjustInto(base);
627 assertEquals(result, LocalDate.of(i, 2, 12));
628 }
629 }
630
631 @Test
632 public void test_adjustDate_resolve() {
633 Year test = Year.of(2011);
634 assertEquals(test.adjustInto(LocalDate.of(2012, 2, 29)), LocalDate.of(2011, 2, 28));
635 }
636
637 @Test(expectedExceptions=NullPointerException.class)
638 public void test_adjustDate_nullLocalDate() {
639 Year test = Year.of(1);
640 test.adjustInto((LocalDate) null);
641 }
642
643 //-----------------------------------------------------------------------
644 // length()
645 //-----------------------------------------------------------------------
646 @Test
647 public void test_length() {
648 assertEquals(Year.of(1999).length(), 365);
649 assertEquals(Year.of(2000).length(), 366);
650 assertEquals(Year.of(2001).length(), 365);
651
652 assertEquals(Year.of(2007).length(), 365);
653 assertEquals(Year.of(2008).length(), 366);
654 assertEquals(Year.of(2009).length(), 365);
655 assertEquals(Year.of(2010).length(), 365);
656 assertEquals(Year.of(2011).length(), 365);
657 assertEquals(Year.of(2012).length(), 366);
658
659 assertEquals(Year.of(2095).length(), 365);
660 assertEquals(Year.of(2096).length(), 366);
661 assertEquals(Year.of(2097).length(), 365);
662 assertEquals(Year.of(2098).length(), 365);
663 assertEquals(Year.of(2099).length(), 365);
664 assertEquals(Year.of(2100).length(), 365);
665 assertEquals(Year.of(2101).length(), 365);
666 assertEquals(Year.of(2102).length(), 365);
685 // isValidMonthDay(MonthDay)
686 //-----------------------------------------------------------------------
687 @DataProvider(name="isValidMonthDay")
688 Object[][] data_isValidMonthDay() {
689 return new Object[][] {
690 {Year.of(2007), MonthDay.of(6, 30), true},
691 {Year.of(2008), MonthDay.of(2, 28), true},
692 {Year.of(2008), MonthDay.of(2, 29), true},
693 {Year.of(2009), MonthDay.of(2, 28), true},
694 {Year.of(2009), MonthDay.of(2, 29), false},
695 {Year.of(2009), null, false},
696 };
697 }
698
699 @Test(dataProvider="isValidMonthDay")
700 public void test_isValidMonthDay(Year year, MonthDay monthDay, boolean expected) {
701 assertEquals(year.isValidMonthDay(monthDay), expected);
702 }
703
704 //-----------------------------------------------------------------------
705 // periodUntil(Temporal, TemporalUnit)
706 //-----------------------------------------------------------------------
707 @DataProvider(name="periodUntilUnit")
708 Object[][] data_periodUntilUnit() {
709 return new Object[][] {
710 {Year.of(2000), Year.of(-1), YEARS, -2001},
711 {Year.of(2000), Year.of(0), YEARS, -2000},
712 {Year.of(2000), Year.of(1), YEARS, -1999},
713 {Year.of(2000), Year.of(1998), YEARS, -2},
714 {Year.of(2000), Year.of(1999), YEARS, -1},
715 {Year.of(2000), Year.of(2000), YEARS, 0},
716 {Year.of(2000), Year.of(2001), YEARS, 1},
717 {Year.of(2000), Year.of(2002), YEARS, 2},
718 {Year.of(2000), Year.of(2246), YEARS, 246},
719
720 {Year.of(2000), Year.of(-1), DECADES, -200},
721 {Year.of(2000), Year.of(0), DECADES, -200},
722 {Year.of(2000), Year.of(1), DECADES, -199},
723 {Year.of(2000), Year.of(1989), DECADES, -1},
724 {Year.of(2000), Year.of(1990), DECADES, -1},
725 {Year.of(2000), Year.of(1991), DECADES, 0},
726 {Year.of(2000), Year.of(2000), DECADES, 0},
727 {Year.of(2000), Year.of(2009), DECADES, 0},
728 {Year.of(2000), Year.of(2010), DECADES, 1},
729 {Year.of(2000), Year.of(2011), DECADES, 1},
730
731 {Year.of(2000), Year.of(-1), CENTURIES, -20},
732 {Year.of(2000), Year.of(0), CENTURIES, -20},
733 {Year.of(2000), Year.of(1), CENTURIES, -19},
734 {Year.of(2000), Year.of(1899), CENTURIES, -1},
735 {Year.of(2000), Year.of(1900), CENTURIES, -1},
736 {Year.of(2000), Year.of(1901), CENTURIES, 0},
737 {Year.of(2000), Year.of(2000), CENTURIES, 0},
738 {Year.of(2000), Year.of(2099), CENTURIES, 0},
739 {Year.of(2000), Year.of(2100), CENTURIES, 1},
740 {Year.of(2000), Year.of(2101), CENTURIES, 1},
741
742 {Year.of(2000), Year.of(-1), MILLENNIA, -2},
743 {Year.of(2000), Year.of(0), MILLENNIA, -2},
744 {Year.of(2000), Year.of(1), MILLENNIA, -1},
745 {Year.of(2000), Year.of(999), MILLENNIA, -1},
746 {Year.of(2000), Year.of(1000), MILLENNIA, -1},
747 {Year.of(2000), Year.of(1001), MILLENNIA, 0},
748 {Year.of(2000), Year.of(2000), MILLENNIA, 0},
749 {Year.of(2000), Year.of(2999), MILLENNIA, 0},
750 {Year.of(2000), Year.of(3000), MILLENNIA, 1},
751 {Year.of(2000), Year.of(3001), MILLENNIA, 1},
752 };
753 }
754
755 @Test(dataProvider="periodUntilUnit")
756 public void test_periodUntil_TemporalUnit(Year year1, Year year2, TemporalUnit unit, long expected) {
757 long amount = year1.periodUntil(year2, unit);
758 assertEquals(amount, expected);
759 }
760
761 @Test(dataProvider="periodUntilUnit")
762 public void test_periodUntil_TemporalUnit_negated(Year year1, Year year2, TemporalUnit unit, long expected) {
763 long amount = year2.periodUntil(year1, unit);
764 assertEquals(amount, -expected);
765 }
766
767 @Test(expectedExceptions = UnsupportedTemporalTypeException.class)
768 public void test_periodUntil_TemporalUnit_unsupportedUnit() {
769 TEST_2008.periodUntil(TEST_2008, MONTHS);
770 }
771
772 @Test(expectedExceptions = NullPointerException.class)
773 public void test_periodUntil_TemporalUnit_nullEnd() {
774 TEST_2008.periodUntil(null, DAYS);
775 }
776
777 @Test(expectedExceptions = NullPointerException.class)
778 public void test_periodUntil_TemporalUnit_nullUnit() {
779 TEST_2008.periodUntil(TEST_2008, null);
780 }
781
782 //-----------------------------------------------------------------------
783 // format(DateTimeFormatter)
784 //-----------------------------------------------------------------------
785 @Test
786 public void test_format_formatter() {
787 DateTimeFormatter f = DateTimeFormatter.ofPattern("y");
788 String t = Year.of(2010).format(f);
789 assertEquals(t, "2010");
790 }
791
792 @Test(expectedExceptions=NullPointerException.class)
793 public void test_format_formatter_null() {
794 Year.of(2010).format(null);
795 }
796
797 //-----------------------------------------------------------------------
798 // atMonth(Month)
799 //-----------------------------------------------------------------------
800 @Test
801 public void test_atMonth() {
802 Year test = Year.of(2008);
803 assertEquals(test.atMonth(Month.JUNE), YearMonth.of(2008, 6));
804 }
805
806 @Test(expectedExceptions=NullPointerException.class)
807 public void test_atMonth_nullMonth() {
808 Year test = Year.of(2008);
809 test.atMonth((Month) null);
810 }
811
812 //-----------------------------------------------------------------------
813 // atMonth(int)
814 //-----------------------------------------------------------------------
815 @Test
816 public void test_atMonth_int() {
817 Year test = Year.of(2008);
818 assertEquals(test.atMonth(6), YearMonth.of(2008, 6));
819 }
820
821 @Test(expectedExceptions=DateTimeException.class)
822 public void test_atMonth_int_invalidMonth() {
823 Year test = Year.of(2008);
824 test.atMonth(13);
825 }
826
827 //-----------------------------------------------------------------------
828 // atMonthDay(MonthDay)
829 //-----------------------------------------------------------------------
830 @DataProvider(name="atMonthDay")
831 Object[][] data_atMonthDay() {
832 return new Object[][] {
833 {Year.of(2008), MonthDay.of(6, 30), LocalDate.of(2008, 6, 30)},
834 {Year.of(2008), MonthDay.of(2, 29), LocalDate.of(2008, 2, 29)},
835 {Year.of(2009), MonthDay.of(2, 29), LocalDate.of(2009, 2, 28)},
836 };
837 }
838
839 @Test(dataProvider="atMonthDay")
840 public void test_atMonthDay(Year year, MonthDay monthDay, LocalDate expected) {
841 assertEquals(year.atMonthDay(monthDay), expected);
842 }
843
844 @Test(expectedExceptions=NullPointerException.class)
845 public void test_atMonthDay_nullMonthDay() {
846 Year test = Year.of(2008);
847 test.atMonthDay((MonthDay) null);
848 }
849
850 //-----------------------------------------------------------------------
851 // atDay(int)
852 //-----------------------------------------------------------------------
853 @Test
854 public void test_atDay_notLeapYear() {
855 Year test = Year.of(2007);
856 LocalDate expected = LocalDate.of(2007, 1, 1);
857 for (int i = 1; i <= 365; i++) {
858 assertEquals(test.atDay(i), expected);
859 expected = expected.plusDays(1);
860 }
861 }
862
863 @Test(expectedExceptions=DateTimeException.class)
864 public void test_atDay_notLeapYear_day366() {
865 Year test = Year.of(2007);
866 test.atDay(366);
867 }
868
869 @Test
870 public void test_atDay_leapYear() {
871 Year test = Year.of(2008);
872 LocalDate expected = LocalDate.of(2008, 1, 1);
873 for (int i = 1; i <= 366; i++) {
874 assertEquals(test.atDay(i), expected);
875 expected = expected.plusDays(1);
876 }
877 }
878
879 @Test(expectedExceptions=DateTimeException.class)
880 public void test_atDay_day0() {
881 Year test = Year.of(2007);
882 test.atDay(0);
883 }
884
885 @Test(expectedExceptions=DateTimeException.class)
886 public void test_atDay_day367() {
887 Year test = Year.of(2007);
888 test.atDay(367);
889 }
890
891 //-----------------------------------------------------------------------
892 // compareTo()
893 //-----------------------------------------------------------------------
894 @Test
895 public void test_compareTo() {
896 for (int i = -4; i <= 2104; i++) {
897 Year a = Year.of(i);
898 for (int j = -4; j <= 2104; j++) {
899 Year b = Year.of(j);
900 if (i < j) {
901 assertEquals(a.compareTo(b) < 0, true);
902 assertEquals(b.compareTo(a) > 0, true);
903 assertEquals(a.isAfter(b), false);
904 assertEquals(a.isBefore(b), true);
905 assertEquals(b.isAfter(a), true);
906 assertEquals(b.isBefore(a), false);
907 } else if (i > j) {
908 assertEquals(a.compareTo(b) > 0, true);
909 assertEquals(b.compareTo(a) < 0, true);
910 assertEquals(a.isAfter(b), true);
911 assertEquals(a.isBefore(b), false);
912 assertEquals(b.isAfter(a), false);
913 assertEquals(b.isBefore(a), true);
914 } else {
915 assertEquals(a.compareTo(b), 0);
916 assertEquals(b.compareTo(a), 0);
917 assertEquals(a.isAfter(b), false);
918 assertEquals(a.isBefore(b), false);
919 assertEquals(b.isAfter(a), false);
920 assertEquals(b.isBefore(a), false);
921 }
922 }
923 }
924 }
925
926 @Test(expectedExceptions=NullPointerException.class)
927 public void test_compareTo_nullYear() {
928 Year doy = null;
929 Year test = Year.of(1);
930 test.compareTo(doy);
931 }
932
933 //-----------------------------------------------------------------------
934 // equals() / hashCode()
935 //-----------------------------------------------------------------------
936 @Test
937 public void test_equals() {
938 for (int i = -4; i <= 2104; i++) {
939 Year a = Year.of(i);
940 for (int j = -4; j <= 2104; j++) {
941 Year b = Year.of(j);
942 assertEquals(a.equals(b), i == j);
943 assertEquals(a.hashCode() == b.hashCode(), i == j);
944 }
945 }
946 }
947
948 @Test
949 public void test_equals_same() {
950 Year test = Year.of(2011);
951 assertEquals(test.equals(test), true);
952 }
953
954 @Test
955 public void test_equals_nullYear() {
956 Year doy = null;
957 Year test = Year.of(1);
958 assertEquals(test.equals(doy), false);
959 }
960
961 @Test
962 public void test_equals_incorrectType() {
963 Year test = Year.of(1);
964 assertEquals(test.equals("Incorrect type"), false);
965 }
966
967 //-----------------------------------------------------------------------
968 // toString()
969 //-----------------------------------------------------------------------
970 @Test
971 public void test_toString() {
972 for (int i = -4; i <= 2104; i++) {
973 Year a = Year.of(i);
974 assertEquals(a.toString(), "" + i);
975 }
976 }
977
978 }
|