< prev index next >

test/java/time/tck/java/time/TCKLocalDate.java

Print this page




 102 import java.time.ZoneOffset;
 103 import java.time.ZonedDateTime;
 104 import java.time.chrono.IsoChronology;
 105 import java.time.chrono.IsoEra;
 106 import java.time.format.DateTimeFormatter;
 107 import java.time.format.DateTimeParseException;
 108 import java.time.temporal.ChronoField;
 109 import java.time.temporal.ChronoUnit;
 110 import java.time.temporal.JulianFields;
 111 import java.time.temporal.Temporal;
 112 import java.time.temporal.TemporalAccessor;
 113 import java.time.temporal.TemporalAdjuster;
 114 import java.time.temporal.TemporalField;
 115 import java.time.temporal.TemporalQueries;
 116 import java.time.temporal.TemporalQuery;
 117 import java.time.temporal.TemporalUnit;
 118 import java.time.temporal.UnsupportedTemporalTypeException;
 119 import java.util.ArrayList;
 120 import java.util.Arrays;
 121 import java.util.List;

 122 
 123 import org.testng.annotations.BeforeMethod;
 124 import org.testng.annotations.DataProvider;
 125 import org.testng.annotations.Test;
 126 import test.java.time.MockSimplePeriod;
 127 import test.java.time.temporal.MockFieldNoValue;
 128 
 129 /**
 130  * Test LocalDate.
 131  */
 132 @Test
 133 public class TCKLocalDate extends AbstractDateTimeTest {
 134 
 135     private static final ZoneOffset OFFSET_PONE = ZoneOffset.ofHours(1);
 136     private static final ZoneOffset OFFSET_PTWO = ZoneOffset.ofHours(2);
 137     private static final ZoneOffset OFFSET_MTWO = ZoneOffset.ofHours(-2);
 138     private static final ZoneId ZONE_PARIS = ZoneId.of("Europe/Paris");
 139     private static final ZoneId ZONE_GAZA = ZoneId.of("Asia/Gaza");
 140 
 141     private LocalDate TEST_2007_07_15;


2367 
2368     @Test(dataProvider="sampleToString")
2369     public void test_toString(int y, int m, int d, String expected) {
2370         LocalDate t = LocalDate.of(y, m, d);
2371         String str = t.toString();
2372         assertEquals(str, expected);
2373     }
2374 
2375     private LocalDate date(int year, int month, int day) {
2376         return LocalDate.of(year, month, day);
2377     }
2378 
2379     //-----------------------------------------------------------------
2380     // getEra()
2381     // ----------------------------------------------------------------
2382     @Test
2383     public void test_getEra() {
2384         IsoEra isoEra = LocalDate.MAX.getEra();
2385         assertSame(isoEra,IsoEra.CE);
2386         assertSame(LocalDate.MIN.getEra(),IsoEra.BCE);































































































































































































2387     }
2388 }


 102 import java.time.ZoneOffset;
 103 import java.time.ZonedDateTime;
 104 import java.time.chrono.IsoChronology;
 105 import java.time.chrono.IsoEra;
 106 import java.time.format.DateTimeFormatter;
 107 import java.time.format.DateTimeParseException;
 108 import java.time.temporal.ChronoField;
 109 import java.time.temporal.ChronoUnit;
 110 import java.time.temporal.JulianFields;
 111 import java.time.temporal.Temporal;
 112 import java.time.temporal.TemporalAccessor;
 113 import java.time.temporal.TemporalAdjuster;
 114 import java.time.temporal.TemporalField;
 115 import java.time.temporal.TemporalQueries;
 116 import java.time.temporal.TemporalQuery;
 117 import java.time.temporal.TemporalUnit;
 118 import java.time.temporal.UnsupportedTemporalTypeException;
 119 import java.util.ArrayList;
 120 import java.util.Arrays;
 121 import java.util.List;
 122 import java.util.stream.Collectors;
 123 
 124 import org.testng.annotations.BeforeMethod;
 125 import org.testng.annotations.DataProvider;
 126 import org.testng.annotations.Test;
 127 import test.java.time.MockSimplePeriod;
 128 import test.java.time.temporal.MockFieldNoValue;
 129 
 130 /**
 131  * Test LocalDate.
 132  */
 133 @Test
 134 public class TCKLocalDate extends AbstractDateTimeTest {
 135 
 136     private static final ZoneOffset OFFSET_PONE = ZoneOffset.ofHours(1);
 137     private static final ZoneOffset OFFSET_PTWO = ZoneOffset.ofHours(2);
 138     private static final ZoneOffset OFFSET_MTWO = ZoneOffset.ofHours(-2);
 139     private static final ZoneId ZONE_PARIS = ZoneId.of("Europe/Paris");
 140     private static final ZoneId ZONE_GAZA = ZoneId.of("Asia/Gaza");
 141 
 142     private LocalDate TEST_2007_07_15;


2368 
2369     @Test(dataProvider="sampleToString")
2370     public void test_toString(int y, int m, int d, String expected) {
2371         LocalDate t = LocalDate.of(y, m, d);
2372         String str = t.toString();
2373         assertEquals(str, expected);
2374     }
2375 
2376     private LocalDate date(int year, int month, int day) {
2377         return LocalDate.of(year, month, day);
2378     }
2379 
2380     //-----------------------------------------------------------------
2381     // getEra()
2382     // ----------------------------------------------------------------
2383     @Test
2384     public void test_getEra() {
2385         IsoEra isoEra = LocalDate.MAX.getEra();
2386         assertSame(isoEra,IsoEra.CE);
2387         assertSame(LocalDate.MIN.getEra(),IsoEra.BCE);
2388     }
2389 
2390     //-----------------------------------------------------------------
2391     // datesUntil()
2392     // ----------------------------------------------------------------
2393     @Test
2394     public void test_datesUntil() {
2395         assertEquals(
2396                 date(2015, 9, 29).datesUntil(date(2015, 10, 3)).collect(
2397                         Collectors.toList()), Arrays.asList(date(2015, 9, 29),
2398                         date(2015, 9, 30), date(2015, 10, 1), date(2015, 10, 2)));
2399         assertEquals(date(2015, 9, 29).datesUntil(date(2015, 10, 3), Period.ofDays(2))
2400                 .collect(Collectors.toList()), Arrays.asList(date(2015, 9, 29),
2401                 date(2015, 10, 1)));
2402         assertEquals(date(2015, 1, 31).datesUntil(date(2015, 6, 1), Period.ofMonths(1))
2403                 .collect(Collectors.toList()), Arrays.asList(date(2015, 1, 31),
2404                 date(2015, 2, 28), date(2015, 3, 31), date(2015, 4, 30),
2405                 date(2015, 5, 31)));
2406     }
2407     
2408     @Test(expectedExceptions=NullPointerException.class)
2409     public void test_datesUntil_nullEnd() {
2410         LocalDate date = date(2015, 1, 31);
2411         date.datesUntil(null);
2412     }
2413     
2414     @Test(expectedExceptions=NullPointerException.class)
2415     public void test_datesUntil_nullEndStep() {
2416         LocalDate date = date(2015, 1, 31);
2417         date.datesUntil(null, Period.ofDays(1));
2418     }
2419     
2420     @Test(expectedExceptions=NullPointerException.class)
2421     public void test_datesUntil_nullStep() {
2422         LocalDate date = date(2015, 1, 31);
2423         date.datesUntil(date, null);
2424     }
2425     
2426     @Test(expectedExceptions = IllegalArgumentException.class)
2427     public void test_datesUntil_endBeforeStart() {
2428         date(2015, 1, 31).datesUntil(date(2015, 1, 30));
2429     }
2430     
2431     @Test(expectedExceptions = IllegalArgumentException.class)
2432     public void test_datesUntil_endBeforeStartPositiveStep() {
2433         date(2015, 1, 31).datesUntil(date(2015, 1, 30), Period.of(1, 0, 0));
2434     }
2435     
2436     @Test(expectedExceptions = IllegalArgumentException.class)
2437     public void test_datesUntil_endAfterStartNegativeStep() {
2438         date(2015, 1, 30).datesUntil(date(2015, 1, 31), Period.of(0, -1, -1));
2439     }
2440     
2441     @Test(expectedExceptions=IllegalArgumentException.class)
2442     public void test_datesUntil_zeroStep() {
2443         LocalDate date = date(2015, 1, 31);
2444         date.datesUntil(date, Period.ZERO);
2445     }
2446     
2447     @Test(expectedExceptions=IllegalArgumentException.class)
2448     public void test_datesUntil_oppositeSign() {
2449         LocalDate date = date(2015, 1, 31);
2450         date.datesUntil(date, Period.of(1, 0, -1));
2451     }
2452     
2453     @Test(expectedExceptions=IllegalArgumentException.class)
2454     public void test_datesUntil_oppositeSign2() {
2455         LocalDate date = date(2015, 1, 31);
2456         date.datesUntil(date, Period.of(0, -1, 1));
2457     }
2458     
2459     @DataProvider(name="datesUntil")
2460     public Object[][] provider_datesUntil() {
2461         return new Object[][] {
2462                 {MIN_DATE, MIN_DATE},
2463                 {MIN_DATE, MAX_DATE},
2464                 {MAX_DATE, MAX_DATE},
2465                 {date(2015,10,1), date(2015,10,2)},
2466                 {date(2015,10,1), date(2015,11,1)},
2467                 {date(2015,10,31), date(2015,11,1)},
2468                 {date(2015,10,1), MAX_DATE},
2469                 {MIN_DATE, date(2015,10,1)}
2470         };
2471     }
2472     
2473     @Test(dataProvider = "datesUntil")
2474     public void test_datesUntil_count(LocalDate start, LocalDate end) {
2475         assertEquals(start.datesUntil(end).count(), start.until(end, ChronoUnit.DAYS));
2476         assertEquals(start.datesUntil(end, Period.ofDays(1)).count(),
2477                 start.until(end, ChronoUnit.DAYS));
2478     }
2479 
2480     @DataProvider(name="datesUntilSteps")
2481     public Object[][] provider_datesUntil_steps() {
2482         List<Object[]> data = new ArrayList<>(Arrays.asList(new Object[][] {
2483             {MIN_DATE, MAX_DATE, Period.ofYears(Year.MAX_VALUE)},
2484             {MIN_DATE, MAX_DATE, Period.ofDays(2)},
2485             {MIN_DATE, MAX_DATE, Period.of(1,2,3)},
2486             {MIN_DATE, MAX_DATE, Period.of(1,2,1000000)},
2487             {MIN_DATE, MAX_DATE, Period.of(1,1000000,3)},
2488             {MIN_DATE, MAX_DATE, Period.of(1000000,2,3)},
2489             {MIN_DATE, MIN_DATE.plusMonths(1), Period.ofMonths(1)},
2490             {MIN_DATE, date(Year.MIN_VALUE, 2, 2), Period.ofMonths(1)},
2491             {MIN_DATE, date(Year.MIN_VALUE, 8, 9), Period.of(0, 1, 1)},
2492             {MIN_DATE, MAX_DATE.minusYears(1), Period.ofYears(Year.MAX_VALUE)},
2493             {MAX_DATE.minusMonths(1), MAX_DATE, Period.ofMonths(1)},
2494             {date(Year.MAX_VALUE, 2, 20), MAX_DATE, Period.of(0, 1, 1)},
2495             {date(2015,1,1), date(2016,1,1), Period.ofYears(1)},
2496             {date(2015,1,1), date(2016,1,1), Period.ofDays(365)},
2497             {date(2015,1,1), date(2016,1,1), Period.ofDays(366)},
2498             {date(2015,1,1), date(2016,1,1), Period.ofDays(4)},
2499             {date(2015,1,1), date(2016,1,1), Period.of(0,1,2)},
2500             {date(2015,1,1), date(2016,1,1), Period.ofMonths(1)},
2501             {date(2015,1,1), date(2016,1,1), Period.ofMonths(12)},
2502             {date(2015,1,1), date(2016,1,2), Period.ofMonths(12)},
2503             {date(2015,1,1), date(2016,1,1), Period.of(0, 11, 30)},
2504             {date(2015,1,1), date(2015,12,31), Period.of(0, 11, 30)},
2505             {date(2015,1,31), date(2015,12,31), Period.ofMonths(2)},
2506             {date(2015,1,31), date(2015,12,1), Period.ofMonths(2)},
2507             {date(2015,1,31), date(2015,11,30), Period.ofMonths(2)},
2508             {date(2015,1,31), date(2030,11,30), Period.of(1,30,365)},
2509             {date(2015,1,31), date(2043,1,31), Period.of(4,0,0)},
2510             {date(2015,1,31), date(2043,2,1), Period.of(4,0,0)},
2511             {date(2015,1,31), date(2043,1,31), Period.of(3,11,30)},
2512             {date(2015,1,31), date(2043,2,1), Period.of(3,11,30)},
2513             {date(2015,1,31), date(2043,1,31), Period.of(0,0,1460)},
2514             {date(2015,1,31), date(2043,1,31), Period.of(0,0,1461)},
2515             {date(2015,1,31), date(2043,2,1), Period.of(0,0,1461)},
2516             {date(2015,1,31), MAX_DATE, Period.of(10,100,1000)},
2517             {date(2015,1,31), MAX_DATE, Period.of(1000000,10000,100000)},
2518             {date(2015,1,31), MAX_DATE, Period.ofDays(10000000)},
2519             {date(2015,1,31), MAX_DATE, Period.ofDays(Integer.MAX_VALUE)},
2520             {date(2015,1,31), MAX_DATE, Period.ofMonths(Integer.MAX_VALUE)},
2521             {date(2015,1,31), MAX_DATE, Period.ofYears(Integer.MAX_VALUE)}
2522         }));
2523         LocalDate start = date(2014, 1, 15);
2524         LocalDate end = date(2015, 3, 4);
2525         for (int months : new int[] { 0, 1, 2, 3, 5, 7, 12, 13 }) {
2526             for (int days : new int[] { 0, 1, 2, 3, 5, 10, 17, 27, 28, 29, 30, 31, 32, 57, 58, 59,
2527                     60, 61, 62, 70, 80, 90 }) {
2528                 if (months > 0 || days > 0)
2529                     data.add(new Object[] { start, end, Period.of(0, months, days) });
2530             }
2531         }
2532         for (int days = 27; days < 100; days++) {
2533             data.add(new Object[] { start, start.plusDays(days), Period.ofMonths(1) });
2534         }
2535         return data.toArray(new Object[data.size()][]);
2536     }
2537 
2538     @Test(dataProvider="datesUntilSteps")
2539     public void test_datesUntil_step(LocalDate start, LocalDate end, Period step) {
2540         assertEquals(start.datesUntil(start, step).count(), 0);
2541         long count = start.datesUntil(end, step).count();
2542         assertTrue(count > 0);
2543         // the last value must be before the end date
2544         assertTrue(start.plusMonths(step.toTotalMonths()*(count-1)).plusDays(step.getDays()*(count-1)).isBefore(end));
2545         try {
2546             // the next after the last value must be either invalid or not before the end date
2547             assertFalse(start.plusMonths(step.toTotalMonths()*count).plusDays(step.getDays()*count).isBefore(end));
2548         } catch (ArithmeticException | DateTimeException e) {
2549             // ignore: possible overflow for the next value is ok
2550         }
2551         if(count < 1000) {
2552             assertTrue(start.datesUntil(end, step).allMatch(date -> !date.isBefore(start) && date.isBefore(end)));
2553             List<LocalDate> list = new ArrayList<>();
2554             for(long i=0; i<count; i++) {
2555                 list.add(start.plusMonths(step.toTotalMonths()*i).plusDays(step.getDays()*i));
2556             }
2557             assertEquals(start.datesUntil(end, step).collect(Collectors.toList()), list);
2558         }
2559         
2560         // swap end and start and negate the Period
2561         count = end.datesUntil(start, step.negated()).count();
2562         assertTrue(count > 0);
2563         // the last value must be after the start date
2564         assertTrue(end.minusMonths(step.toTotalMonths()*(count-1)).minusDays(step.getDays()*(count-1)).isAfter(start));
2565         try {
2566             // the next after the last value must be either invalid or not after the start date
2567             assertFalse(end.minusMonths(step.toTotalMonths()*count).minusDays(step.getDays()*count).isAfter(start));
2568         } catch (ArithmeticException | DateTimeException e) {
2569             // ignore: possible overflow for the next value is ok
2570         }
2571         if(count < 1000) {
2572             assertTrue(end.datesUntil(start, step.negated()).allMatch(date -> date.isAfter(start) && !date.isAfter(end)));
2573             List<LocalDate> list = new ArrayList<>();
2574             for(long i=0; i<count; i++) {
2575                 list.add(end.minusMonths(step.toTotalMonths()*i).minusDays(step.getDays()*i));
2576             }
2577             assertEquals(end.datesUntil(start, step.negated()).collect(Collectors.toList()), list);
2578         }
2579     }
2580 }
< prev index next >