test/java/time/tck/java/time/TCKMonthDay.java

Print this page




  40  *  * Redistributions in binary form must reproduce the above copyright notice,
  41  *    this list of conditions and the following disclaimer in the documentation
  42  *    and/or other materials provided with the distribution.
  43  *
  44  *  * Neither the name of JSR-310 nor the names of its contributors
  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.temporal;
  61 
  62 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
  63 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  64 import static org.testng.Assert.assertEquals;
  65 import static org.testng.Assert.assertTrue;
  66 import static org.testng.Assert.fail;
  67 
  68 import java.io.ByteArrayOutputStream;
  69 import java.io.DataOutputStream;
  70 import java.io.IOException;
  71 import java.util.ArrayList;
  72 import java.util.Arrays;
  73 import java.util.HashSet;
  74 import java.util.List;
  75 import java.util.Set;
  76 
  77 import java.time.Clock;
  78 import java.time.DateTimeException;
  79 import java.time.Instant;
  80 import java.time.LocalDate;
  81 import java.time.LocalDateTime;
  82 import java.time.LocalTime;
  83 import java.time.Month;


  84 import java.time.ZoneId;
  85 import java.time.ZoneOffset;

  86 import java.time.format.DateTimeFormatter;
  87 import java.time.format.DateTimeFormatters;
  88 import java.time.format.DateTimeParseException;
  89 import java.time.temporal.ChronoField;
  90 import java.time.temporal.JulianFields;
  91 import java.time.temporal.MonthDay;
  92 import java.time.temporal.TemporalAccessor;
  93 import java.time.temporal.TemporalField;
  94 import java.time.temporal.YearMonth;





  95 
  96 import org.testng.annotations.BeforeMethod;
  97 import org.testng.annotations.DataProvider;
  98 import org.testng.annotations.Test;
  99 import tck.java.time.AbstractDateTimeTest;
 100 
 101 /**
 102  * Test MonthDay.
 103  */
 104 @Test
 105 public class TCKMonthDay extends AbstractDateTimeTest {
 106 
 107     private MonthDay TEST_07_15;
 108 
 109     @BeforeMethod(groups={"tck","implementation"})
 110     public void setUp() {
 111         TEST_07_15 = MonthDay.of(7, 15);
 112     }
 113 
 114     //-----------------------------------------------------------------------
 115     @Override
 116     protected List<TemporalAccessor> samples() {
 117         TemporalAccessor[] array = {TEST_07_15, };
 118         return Arrays.asList(array);
 119     }


 130     @Override
 131     protected List<TemporalField> invalidFields() {
 132         List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values()));
 133         list.removeAll(validFields());
 134         list.add(JulianFields.JULIAN_DAY);
 135         list.add(JulianFields.MODIFIED_JULIAN_DAY);
 136         list.add(JulianFields.RATA_DIE);
 137         return list;
 138     }
 139 
 140     //-----------------------------------------------------------------------
 141     @Test
 142     public void test_serialization() throws ClassNotFoundException, IOException {
 143         assertSerializable(TEST_07_15);
 144     }
 145 
 146     @Test
 147     public void test_serialization_format() throws Exception {
 148         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 149         try (DataOutputStream dos = new DataOutputStream(baos) ) {
 150             dos.writeByte(6);
 151             dos.writeByte(9);
 152             dos.writeByte(16);
 153         }
 154         byte[] bytes = baos.toByteArray();
 155         assertSerializedBySer(MonthDay.of(9, 16), bytes);
 156     }
 157 
 158     //-----------------------------------------------------------------------
 159     void check(MonthDay test, int m, int d) {
 160         assertEquals(test.getMonth().getValue(), m);
 161         assertEquals(test.getDayOfMonth(), d);
 162     }
 163 
 164     //-----------------------------------------------------------------------
 165     // now()
 166     //-----------------------------------------------------------------------
 167     @Test(groups={"tck"})
 168     public void now() {
 169         MonthDay expected = MonthDay.now(Clock.systemDefaultZone());
 170         MonthDay test = MonthDay.now();


 355     @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"})
 356     public void factory_parse_invalidValue_Day() {
 357         MonthDay.parse("--06-31");
 358     }
 359 
 360     @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"})
 361     public void factory_parse_illegalValue_Month() {
 362         MonthDay.parse("--13-25");
 363     }
 364 
 365     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
 366     public void factory_parse_nullText() {
 367         MonthDay.parse(null);
 368     }
 369 
 370     //-----------------------------------------------------------------------
 371     // parse(DateTimeFormatter)
 372     //-----------------------------------------------------------------------
 373     @Test(groups={"tck"})
 374     public void factory_parse_formatter() {
 375         DateTimeFormatter f = DateTimeFormatters.pattern("M d");
 376         MonthDay test = MonthDay.parse("12 3", f);
 377         assertEquals(test, MonthDay.of(12, 3));
 378     }
 379 
 380     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
 381     public void factory_parse_formatter_nullText() {
 382         DateTimeFormatter f = DateTimeFormatters.pattern("M d");
 383         MonthDay.parse((String) null, f);
 384     }
 385 
 386     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
 387     public void factory_parse_formatter_nullFormatter() {
 388         MonthDay.parse("ANY", null);
 389     }
 390 
 391     //-----------------------------------------------------------------------
 392     // get(TemporalField)
 393     //-----------------------------------------------------------------------
 394     @Test
 395     public void test_get_TemporalField() {
 396         assertEquals(TEST_07_15.get(ChronoField.DAY_OF_MONTH), 15);
 397         assertEquals(TEST_07_15.get(ChronoField.MONTH_OF_YEAR), 7);
 398     }
 399 
 400     @Test
 401     public void test_getLong_TemporalField() {
 402         assertEquals(TEST_07_15.getLong(ChronoField.DAY_OF_MONTH), 15);
 403         assertEquals(TEST_07_15.getLong(ChronoField.MONTH_OF_YEAR), 7);
 404     }
 405 
 406     //-----------------------------------------------------------------------































 407     // get*()
 408     //-----------------------------------------------------------------------
 409     @DataProvider(name="sampleDates")
 410     Object[][] provider_sampleDates() {
 411         return new Object[][] {
 412             {1, 1},
 413             {1, 31},
 414             {2, 1},
 415             {2, 28},
 416             {2, 29},
 417             {7, 4},
 418             {7, 5},
 419         };
 420     }
 421 
 422     @Test(dataProvider="sampleDates", groups={"tck"})
 423     public void test_get(int m, int d) {
 424         MonthDay a = MonthDay.of(m, d);
 425         assertEquals(a.getMonth(), Month.of(m));

 426         assertEquals(a.getDayOfMonth(), d);
 427     }
 428 
 429     //-----------------------------------------------------------------------
 430     // with(Month)
 431     //-----------------------------------------------------------------------
 432     @Test(groups={"tck"})
 433     public void test_with_Month() {
 434         assertEquals(MonthDay.of(6, 30).with(Month.JANUARY), MonthDay.of(1, 30));
 435     }
 436 
 437     @Test(groups={"tck"})
 438     public void test_with_Month_adjustToValid() {
 439         assertEquals(MonthDay.of(7, 31).with(Month.JUNE), MonthDay.of(6, 30));
 440     }
 441 
 442     @Test(groups={"tck"})
 443     public void test_with_Month_adjustToValidFeb() {
 444         assertEquals(MonthDay.of(7, 31).with(Month.FEBRUARY), MonthDay.of(2, 29));
 445     }


 726     Object[][] provider_sampleToString() {
 727         return new Object[][] {
 728             {7, 5, "--07-05"},
 729             {12, 31, "--12-31"},
 730             {1, 2, "--01-02"},
 731         };
 732     }
 733 
 734     @Test(dataProvider="sampleToString", groups={"tck"})
 735     public void test_toString(int m, int d, String expected) {
 736         MonthDay test = MonthDay.of(m, d);
 737         String str = test.toString();
 738         assertEquals(str, expected);
 739     }
 740 
 741     //-----------------------------------------------------------------------
 742     // toString(DateTimeFormatter)
 743     //-----------------------------------------------------------------------
 744     @Test(groups={"tck"})
 745     public void test_toString_formatter() {
 746         DateTimeFormatter f = DateTimeFormatters.pattern("M d");
 747         String t = MonthDay.of(12, 3).toString(f);
 748         assertEquals(t, "12 3");
 749     }
 750 
 751     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
 752     public void test_toString_formatter_null() {
 753         MonthDay.of(12, 3).toString(null);
 754     }
 755 
 756 }


  40  *  * Redistributions in binary form must reproduce the above copyright notice,
  41  *    this list of conditions and the following disclaimer in the documentation
  42  *    and/or other materials provided with the distribution.
  43  *
  44  *  * Neither the name of JSR-310 nor the names of its contributors
  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.DAY_OF_MONTH;
  63 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  64 import static org.testng.Assert.assertEquals;
  65 import static org.testng.Assert.assertTrue;
  66 import static org.testng.Assert.fail;
  67 
  68 import java.io.ByteArrayOutputStream;
  69 import java.io.DataOutputStream;
  70 import java.io.IOException;






  71 import java.time.Clock;
  72 import java.time.DateTimeException;
  73 import java.time.Instant;
  74 import java.time.LocalDate;
  75 import java.time.LocalDateTime;
  76 import java.time.LocalTime;
  77 import java.time.Month;
  78 import java.time.MonthDay;
  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.JulianFields;
  87 import java.time.temporal.Queries;
  88 import java.time.temporal.TemporalAccessor;
  89 import java.time.temporal.TemporalField;
  90 import java.time.temporal.TemporalQuery;
  91 import java.util.ArrayList;
  92 import java.util.Arrays;
  93 import java.util.HashSet;
  94 import java.util.List;
  95 import java.util.Set;
  96 
  97 import org.testng.annotations.BeforeMethod;
  98 import org.testng.annotations.DataProvider;
  99 import org.testng.annotations.Test;

 100 
 101 /**
 102  * Test MonthDay.
 103  */
 104 @Test
 105 public class TCKMonthDay extends AbstractDateTimeTest {
 106 
 107     private MonthDay TEST_07_15;
 108 
 109     @BeforeMethod(groups={"tck","implementation"})
 110     public void setUp() {
 111         TEST_07_15 = MonthDay.of(7, 15);
 112     }
 113 
 114     //-----------------------------------------------------------------------
 115     @Override
 116     protected List<TemporalAccessor> samples() {
 117         TemporalAccessor[] array = {TEST_07_15, };
 118         return Arrays.asList(array);
 119     }


 130     @Override
 131     protected List<TemporalField> invalidFields() {
 132         List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values()));
 133         list.removeAll(validFields());
 134         list.add(JulianFields.JULIAN_DAY);
 135         list.add(JulianFields.MODIFIED_JULIAN_DAY);
 136         list.add(JulianFields.RATA_DIE);
 137         return list;
 138     }
 139 
 140     //-----------------------------------------------------------------------
 141     @Test
 142     public void test_serialization() throws ClassNotFoundException, IOException {
 143         assertSerializable(TEST_07_15);
 144     }
 145 
 146     @Test
 147     public void test_serialization_format() throws Exception {
 148         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 149         try (DataOutputStream dos = new DataOutputStream(baos) ) {
 150             dos.writeByte(13);       // java.time.temporal.Ser.MONTH_DAY_TYPE
 151             dos.writeByte(9);
 152             dos.writeByte(16);
 153         }
 154         byte[] bytes = baos.toByteArray();
 155         assertSerializedBySer(MonthDay.of(9, 16), bytes);
 156     }
 157 
 158     //-----------------------------------------------------------------------
 159     void check(MonthDay test, int m, int d) {
 160         assertEquals(test.getMonth().getValue(), m);
 161         assertEquals(test.getDayOfMonth(), d);
 162     }
 163 
 164     //-----------------------------------------------------------------------
 165     // now()
 166     //-----------------------------------------------------------------------
 167     @Test(groups={"tck"})
 168     public void now() {
 169         MonthDay expected = MonthDay.now(Clock.systemDefaultZone());
 170         MonthDay test = MonthDay.now();


 355     @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"})
 356     public void factory_parse_invalidValue_Day() {
 357         MonthDay.parse("--06-31");
 358     }
 359 
 360     @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"})
 361     public void factory_parse_illegalValue_Month() {
 362         MonthDay.parse("--13-25");
 363     }
 364 
 365     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
 366     public void factory_parse_nullText() {
 367         MonthDay.parse(null);
 368     }
 369 
 370     //-----------------------------------------------------------------------
 371     // parse(DateTimeFormatter)
 372     //-----------------------------------------------------------------------
 373     @Test(groups={"tck"})
 374     public void factory_parse_formatter() {
 375         DateTimeFormatter f = DateTimeFormatter.ofPattern("M d");
 376         MonthDay test = MonthDay.parse("12 3", f);
 377         assertEquals(test, MonthDay.of(12, 3));
 378     }
 379 
 380     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
 381     public void factory_parse_formatter_nullText() {
 382         DateTimeFormatter f = DateTimeFormatter.ofPattern("M d");
 383         MonthDay.parse((String) null, f);
 384     }
 385 
 386     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
 387     public void factory_parse_formatter_nullFormatter() {
 388         MonthDay.parse("ANY", null);
 389     }
 390 
 391     //-----------------------------------------------------------------------
 392     // get(TemporalField)
 393     //-----------------------------------------------------------------------
 394     @Test
 395     public void test_get_TemporalField() {
 396         assertEquals(TEST_07_15.get(ChronoField.DAY_OF_MONTH), 15);
 397         assertEquals(TEST_07_15.get(ChronoField.MONTH_OF_YEAR), 7);
 398     }
 399 
 400     @Test
 401     public void test_getLong_TemporalField() {
 402         assertEquals(TEST_07_15.getLong(ChronoField.DAY_OF_MONTH), 15);
 403         assertEquals(TEST_07_15.getLong(ChronoField.MONTH_OF_YEAR), 7);
 404     }
 405 
 406     //-----------------------------------------------------------------------
 407     // query(TemporalQuery)
 408     //-----------------------------------------------------------------------
 409     @DataProvider(name="query")
 410     Object[][] data_query() {
 411         return new Object[][] {
 412                 {TEST_07_15, Queries.chronology(), IsoChronology.INSTANCE},
 413                 {TEST_07_15, Queries.zoneId(), null},
 414                 {TEST_07_15, Queries.precision(), null},
 415                 {TEST_07_15, Queries.zone(), null},
 416                 {TEST_07_15, Queries.offset(), null},
 417                 {TEST_07_15, Queries.localDate(), null},
 418                 {TEST_07_15, Queries.localTime(), null},
 419         };
 420     }
 421 
 422     @Test(dataProvider="query")
 423     public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
 424         assertEquals(temporal.query(query), expected);
 425     }
 426 
 427     @Test(dataProvider="query")
 428     public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
 429         assertEquals(query.queryFrom(temporal), expected);
 430     }
 431 
 432     @Test(expectedExceptions=NullPointerException.class)
 433     public void test_query_null() {
 434         TEST_07_15.query(null);
 435     }
 436 
 437     //-----------------------------------------------------------------------
 438     // get*()
 439     //-----------------------------------------------------------------------
 440     @DataProvider(name="sampleDates")
 441     Object[][] provider_sampleDates() {
 442         return new Object[][] {
 443             {1, 1},
 444             {1, 31},
 445             {2, 1},
 446             {2, 28},
 447             {2, 29},
 448             {7, 4},
 449             {7, 5},
 450         };
 451     }
 452 
 453     @Test(dataProvider="sampleDates")
 454     public void test_get(int m, int d) {
 455         MonthDay a = MonthDay.of(m, d);
 456         assertEquals(a.getMonth(), Month.of(m));
 457         assertEquals(a.getMonthValue(), m);
 458         assertEquals(a.getDayOfMonth(), d);
 459     }
 460 
 461     //-----------------------------------------------------------------------
 462     // with(Month)
 463     //-----------------------------------------------------------------------
 464     @Test(groups={"tck"})
 465     public void test_with_Month() {
 466         assertEquals(MonthDay.of(6, 30).with(Month.JANUARY), MonthDay.of(1, 30));
 467     }
 468 
 469     @Test(groups={"tck"})
 470     public void test_with_Month_adjustToValid() {
 471         assertEquals(MonthDay.of(7, 31).with(Month.JUNE), MonthDay.of(6, 30));
 472     }
 473 
 474     @Test(groups={"tck"})
 475     public void test_with_Month_adjustToValidFeb() {
 476         assertEquals(MonthDay.of(7, 31).with(Month.FEBRUARY), MonthDay.of(2, 29));
 477     }


 758     Object[][] provider_sampleToString() {
 759         return new Object[][] {
 760             {7, 5, "--07-05"},
 761             {12, 31, "--12-31"},
 762             {1, 2, "--01-02"},
 763         };
 764     }
 765 
 766     @Test(dataProvider="sampleToString", groups={"tck"})
 767     public void test_toString(int m, int d, String expected) {
 768         MonthDay test = MonthDay.of(m, d);
 769         String str = test.toString();
 770         assertEquals(str, expected);
 771     }
 772 
 773     //-----------------------------------------------------------------------
 774     // toString(DateTimeFormatter)
 775     //-----------------------------------------------------------------------
 776     @Test(groups={"tck"})
 777     public void test_toString_formatter() {
 778         DateTimeFormatter f = DateTimeFormatter.ofPattern("M d");
 779         String t = MonthDay.of(12, 3).toString(f);
 780         assertEquals(t, "12 3");
 781     }
 782 
 783     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
 784     public void test_toString_formatter_null() {
 785         MonthDay.of(12, 3).toString(null);
 786     }
 787 
 788 }