jdk/test/java/time/tck/java/time/TCKYear.java
changeset 17474 8c100beabcc0
parent 16852 60207b2b4b42
child 19030 32f129cb6351
equal deleted inserted replaced
17473:35cd9b3a98ff 17474:8c100beabcc0
    69 import static java.time.temporal.ChronoUnit.MILLENNIA;
    69 import static java.time.temporal.ChronoUnit.MILLENNIA;
    70 import static java.time.temporal.ChronoUnit.MONTHS;
    70 import static java.time.temporal.ChronoUnit.MONTHS;
    71 import static java.time.temporal.ChronoUnit.WEEKS;
    71 import static java.time.temporal.ChronoUnit.WEEKS;
    72 import static java.time.temporal.ChronoUnit.YEARS;
    72 import static java.time.temporal.ChronoUnit.YEARS;
    73 import static org.testng.Assert.assertEquals;
    73 import static org.testng.Assert.assertEquals;
       
    74 import static org.testng.Assert.assertTrue;
    74 import static org.testng.Assert.fail;
    75 import static org.testng.Assert.fail;
    75 
    76 
    76 import java.io.ByteArrayOutputStream;
    77 import java.io.ByteArrayOutputStream;
    77 import java.io.DataOutputStream;
    78 import java.io.DataOutputStream;
    78 import java.time.Clock;
    79 import java.time.Clock;
    87 import java.time.Period;
    88 import java.time.Period;
    88 import java.time.Year;
    89 import java.time.Year;
    89 import java.time.YearMonth;
    90 import java.time.YearMonth;
    90 import java.time.ZoneId;
    91 import java.time.ZoneId;
    91 import java.time.ZoneOffset;
    92 import java.time.ZoneOffset;
       
    93 import java.time.chrono.IsoEra;
    92 import java.time.chrono.IsoChronology;
    94 import java.time.chrono.IsoChronology;
    93 import java.time.format.DateTimeFormatter;
    95 import java.time.format.DateTimeFormatter;
    94 import java.time.format.DateTimeParseException;
    96 import java.time.format.DateTimeParseException;
    95 import java.time.temporal.ChronoField;
    97 import java.time.temporal.ChronoField;
    96 import java.time.temporal.ChronoUnit;
    98 import java.time.temporal.ChronoUnit;
   524     public void test_plusYears_minLots() {
   526     public void test_plusYears_minLots() {
   525         Year.of(Year.MIN_VALUE).plusYears(-1000);
   527         Year.of(Year.MIN_VALUE).plusYears(-1000);
   526     }
   528     }
   527 
   529 
   528     //-----------------------------------------------------------------------
   530     //-----------------------------------------------------------------------
       
   531     // plus(long, TemporalUnit)
       
   532     //-----------------------------------------------------------------------
       
   533     @DataProvider(name="plus_long_TemporalUnit")
       
   534     Object[][] data_plus_long_TemporalUnit() {
       
   535         return new Object[][] {
       
   536             {Year.of(1), 1, ChronoUnit.YEARS, Year.of(2), null},
       
   537             {Year.of(1), -12, ChronoUnit.YEARS, Year.of(-11), null},
       
   538             {Year.of(1), 0, ChronoUnit.YEARS, Year.of(1), null},
       
   539             {Year.of(999999999), 0, ChronoUnit.YEARS, Year.of(999999999), null},
       
   540             {Year.of(-999999999), 0, ChronoUnit.YEARS, Year.of(-999999999), null},
       
   541             {Year.of(0), -999999999, ChronoUnit.YEARS, Year.of(-999999999), null},
       
   542             {Year.of(0), 999999999, ChronoUnit.YEARS, Year.of(999999999), null},
       
   543 
       
   544             {Year.of(-1), 1, ChronoUnit.ERAS, Year.of(2), null},
       
   545             {Year.of(5), 1, ChronoUnit.CENTURIES, Year.of(105), null},
       
   546             {Year.of(5), 1, ChronoUnit.DECADES, Year.of(15), null},
       
   547 
       
   548             {Year.of(999999999), 1, ChronoUnit.YEARS, null, DateTimeException.class},
       
   549             {Year.of(-999999999), -1, ChronoUnit.YEARS, null, DateTimeException.class},
       
   550 
       
   551             {Year.of(1), 0, ChronoUnit.DAYS, null, DateTimeException.class},
       
   552             {Year.of(1), 0, ChronoUnit.WEEKS, null, DateTimeException.class},
       
   553             {Year.of(1), 0, ChronoUnit.MONTHS, null, DateTimeException.class},
       
   554         };
       
   555     }
       
   556 
       
   557     @Test(groups={"tck"}, dataProvider="plus_long_TemporalUnit")
       
   558     public void test_plus_long_TemporalUnit(Year base, long amount, TemporalUnit unit, Year expectedYear, Class expectedEx) {
       
   559         if (expectedEx == null) {
       
   560             assertEquals(base.plus(amount, unit), expectedYear);
       
   561         } else {
       
   562             try {
       
   563                 Year result = base.plus(amount, unit);
       
   564                 fail();
       
   565             } catch (Exception ex) {
       
   566                 assertTrue(expectedEx.isInstance(ex));
       
   567             }
       
   568         }
       
   569     }
       
   570 
       
   571     //-----------------------------------------------------------------------
   529     // minus(Period)
   572     // minus(Period)
   530     //-----------------------------------------------------------------------
   573     //-----------------------------------------------------------------------
   531     @DataProvider(name="minusValid")
   574     @DataProvider(name="minusValid")
   532     Object[][] data_minusValid() {
   575     Object[][] data_minusValid() {
   533         return new Object[][] {
   576         return new Object[][] {
   615     public void test_minusYears_minLots() {
   658     public void test_minusYears_minLots() {
   616         Year.of(Year.MIN_VALUE).minusYears(1000);
   659         Year.of(Year.MIN_VALUE).minusYears(1000);
   617     }
   660     }
   618 
   661 
   619     //-----------------------------------------------------------------------
   662     //-----------------------------------------------------------------------
       
   663     // minus(long, TemporalUnit)
       
   664     //-----------------------------------------------------------------------
       
   665     @DataProvider(name="minus_long_TemporalUnit")
       
   666     Object[][] data_minus_long_TemporalUnit() {
       
   667         return new Object[][] {
       
   668             {Year.of(1), 1, ChronoUnit.YEARS, Year.of(0), null},
       
   669             {Year.of(1), -12, ChronoUnit.YEARS, Year.of(13), null},
       
   670             {Year.of(1), 0, ChronoUnit.YEARS, Year.of(1), null},
       
   671             {Year.of(999999999), 0, ChronoUnit.YEARS, Year.of(999999999), null},
       
   672             {Year.of(-999999999), 0, ChronoUnit.YEARS, Year.of(-999999999), null},
       
   673             {Year.of(0), -999999999, ChronoUnit.YEARS, Year.of(999999999), null},
       
   674             {Year.of(0), 999999999, ChronoUnit.YEARS, Year.of(-999999999), null},
       
   675 
       
   676             {Year.of(999999999), 1, ChronoUnit.ERAS, Year.of(-999999999 + 1), null},
       
   677             {Year.of(105), 1, ChronoUnit.CENTURIES, Year.of(5), null},
       
   678             {Year.of(15), 1, ChronoUnit.DECADES, Year.of(5), null},
       
   679 
       
   680             {Year.of(-999999999), 1, ChronoUnit.YEARS, null, DateTimeException.class},
       
   681             {Year.of(1), -999999999, ChronoUnit.YEARS, null, DateTimeException.class},
       
   682 
       
   683             {Year.of(1), 0, ChronoUnit.DAYS, null, DateTimeException.class},
       
   684             {Year.of(1), 0, ChronoUnit.WEEKS, null, DateTimeException.class},
       
   685             {Year.of(1), 0, ChronoUnit.MONTHS, null, DateTimeException.class},
       
   686         };
       
   687     }
       
   688 
       
   689     @Test(groups={"tck"}, dataProvider="minus_long_TemporalUnit")
       
   690     public void test_minus_long_TemporalUnit(Year base, long amount, TemporalUnit unit, Year expectedYear, Class expectedEx) {
       
   691         if (expectedEx == null) {
       
   692             assertEquals(base.minus(amount, unit), expectedYear);
       
   693         } else {
       
   694             try {
       
   695                 Year result = base.minus(amount, unit);
       
   696                 fail();
       
   697             } catch (Exception ex) {
       
   698                 assertTrue(expectedEx.isInstance(ex));
       
   699             }
       
   700         }
       
   701     }
       
   702 
       
   703     //-----------------------------------------------------------------------
   620     // adjustInto()
   704     // adjustInto()
   621     //-----------------------------------------------------------------------
   705     //-----------------------------------------------------------------------
   622     @Test
   706     @Test
   623     public void test_adjustDate() {
   707     public void test_adjustDate() {
   624         LocalDate base = LocalDate.of(2007, 2, 12);
   708         LocalDate base = LocalDate.of(2007, 2, 12);
   636 
   720 
   637     @Test(expectedExceptions=NullPointerException.class)
   721     @Test(expectedExceptions=NullPointerException.class)
   638     public void test_adjustDate_nullLocalDate() {
   722     public void test_adjustDate_nullLocalDate() {
   639         Year test = Year.of(1);
   723         Year test = Year.of(1);
   640         test.adjustInto((LocalDate) null);
   724         test.adjustInto((LocalDate) null);
       
   725     }
       
   726 
       
   727     //-----------------------------------------------------------------------
       
   728     // with(TemporalAdjuster)
       
   729     //-----------------------------------------------------------------------
       
   730     @Test
       
   731     public void test_with_TemporalAdjuster() {
       
   732         Year base = Year.of(-10);
       
   733         for (int i = -4; i <= 2104; i++) {
       
   734             Temporal result = base.with(Year.of(i));
       
   735             assertEquals(result, Year.of(i));
       
   736         }
       
   737     }
       
   738 
       
   739     @Test(expectedExceptions=DateTimeException.class)
       
   740     public void test_with_BadTemporalAdjuster() {
       
   741         Year test = Year.of(1);
       
   742         test.with(LocalTime.of(18, 1, 2));
       
   743     }
       
   744 
       
   745     //-----------------------------------------------------------------------
       
   746     // with(TemporalField, long)
       
   747     //-----------------------------------------------------------------------
       
   748     @Test(groups={"tck"})
       
   749     public void test_with() {
       
   750         Year base = Year.of(5);
       
   751         Year result = base.with(ChronoField.ERA, 0);
       
   752         assertEquals(result, base.with(IsoEra.of(0)));
       
   753 
       
   754         int prolepticYear = IsoChronology.INSTANCE.prolepticYear(IsoEra.of(0), 5);
       
   755         assertEquals(result.get(ChronoField.ERA), 0);
       
   756         assertEquals(result.get(ChronoField.YEAR), prolepticYear);
       
   757         assertEquals(result.get(ChronoField.YEAR_OF_ERA), 5);
       
   758 
       
   759         result = base.with(ChronoField.YEAR, 10);
       
   760         assertEquals(result.get(ChronoField.ERA), base.get(ChronoField.ERA));
       
   761         assertEquals(result.get(ChronoField.YEAR), 10);
       
   762         assertEquals(result.get(ChronoField.YEAR_OF_ERA), 10);
       
   763 
       
   764         result = base.with(ChronoField.YEAR_OF_ERA, 20);
       
   765         assertEquals(result.get(ChronoField.ERA), base.get(ChronoField.ERA));
       
   766         assertEquals(result.get(ChronoField.YEAR), 20);
       
   767         assertEquals(result.get(ChronoField.YEAR_OF_ERA), 20);
   641     }
   768     }
   642 
   769 
   643     //-----------------------------------------------------------------------
   770     //-----------------------------------------------------------------------
   644     // length()
   771     // length()
   645     //-----------------------------------------------------------------------
   772     //-----------------------------------------------------------------------