test/java/time/tck/java/time/TCKDuration.java

Print this page




  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.ChronoUnit.DAYS;
  63 import static java.time.temporal.ChronoUnit.HALF_DAYS;
  64 import static java.time.temporal.ChronoUnit.HOURS;
  65 import static java.time.temporal.ChronoUnit.MICROS;
  66 import static java.time.temporal.ChronoUnit.MILLIS;
  67 import static java.time.temporal.ChronoUnit.MINUTES;
  68 import static java.time.temporal.ChronoUnit.NANOS;
  69 import static java.time.temporal.ChronoUnit.SECONDS;
  70 import static java.time.temporal.ChronoUnit.WEEKS;
  71 import static org.testng.Assert.assertEquals;

  72 import static org.testng.Assert.fail;
  73 
  74 import java.io.ByteArrayInputStream;
  75 import java.io.ByteArrayOutputStream;
  76 import java.io.DataOutputStream;
  77 import java.io.ObjectInputStream;
  78 import java.io.ObjectOutputStream;
  79 
  80 import java.time.DateTimeException;
  81 import java.time.Duration;
  82 import java.time.Instant;



  83 import java.time.format.DateTimeParseException;

  84 import java.time.temporal.TemporalUnit;


  85 
  86 import org.testng.annotations.DataProvider;
  87 import org.testng.annotations.Test;
  88 
  89 /**
  90  * Test Duration.
  91  */
  92 @Test
  93 public class TCKDuration extends AbstractTCKTest {
  94 
  95     //-----------------------------------------------------------------------
  96     @Test
  97     public void test_serialization() throws Exception {
  98         assertSerializable(Duration.ofHours(5));
  99         assertSerializable(Duration.ofHours(0));
 100         assertSerializable(Duration.ofHours(-5));
 101     }
 102 
 103     @Test
 104     public void test_serialization_format() throws Exception {


 419     }
 420 
 421     @Test(dataProvider="OfTemporalUnitOutOfRange", expectedExceptions=ArithmeticException.class, groups={"tck"})
 422     public void factory_of_longTemporalUnit_outOfRange(long amount, TemporalUnit unit) {
 423         Duration.of(amount, unit);
 424     }
 425 
 426     @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
 427     public void factory_of_longTemporalUnit_estimatedUnit() {
 428         Duration.of(2, WEEKS);
 429     }
 430 
 431     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
 432     public void factory_of_longTemporalUnit_null() {
 433         Duration.of(1, (TemporalUnit) null);
 434     }
 435 
 436     //-----------------------------------------------------------------------
 437     // between()
 438     //-----------------------------------------------------------------------
 439     @DataProvider(name="DurationBetween")
 440     Object[][] provider_factory_between_Instant_Instant() {
 441         return new Object[][] {
 442             {0, 0, 0, 0, 0, 0},
 443             {3, 0, 7, 0, 4, 0},
 444             {3, 20, 7, 50, 4, 30},
 445             {3, 80, 7, 50, 3, 999999970},
 446             {7, 0, 3, 0, -4, 0},
 447         };
 448     }
 449 
 450     @Test(dataProvider="DurationBetween", groups={"tck"})
 451     public void factory_between_Instant_Instant(long secs1, int nanos1, long secs2, int nanos2, long expectedSeconds, int expectedNanoOfSecond) {
 452         Instant start = Instant.ofEpochSecond(secs1, nanos1);
 453         Instant end = Instant.ofEpochSecond(secs2, nanos2);
 454         Duration t = Duration.between(start, end);
 455         assertEquals(t.getSeconds(), expectedSeconds);
 456         assertEquals(t.getNano(), expectedNanoOfSecond);
 457     }
 458 
 459     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
 460     public void factory_between_Instant_Instant_startNull() {






















 461         Instant end = Instant.ofEpochSecond(1);
 462         Duration.between(null, end);
 463     }
 464 
 465     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
 466     public void factory_between_Instant_Instant_endNull() {
 467         Instant start = Instant.ofEpochSecond(1);
 468         Duration.between(start, null);
 469     }
 470 
 471     //-----------------------------------------------------------------------
 472     // parse(String)
 473     //-----------------------------------------------------------------------
 474     @DataProvider(name="Parse")
 475     Object[][] provider_factory_parse() {
 476         return new Object[][] {
 477             {"PT0S", 0, 0},
 478             {"pT0S", 0, 0},
 479             {"Pt0S", 0, 0},
 480             {"PT0s", 0, 0},
 481 
 482             {"PT1S", 1, 0},
 483             {"PT12S", 12, 0},
 484             {"PT123456789S", 123456789, 0},
 485             {"PT" + Long.MAX_VALUE + "S", Long.MAX_VALUE, 0},
 486 





 487             {"PT-1S", -1, 0},
 488             {"PT-12S", -12, 0},
 489             {"PT-123456789S", -123456789, 0},
 490             {"PT" + Long.MIN_VALUE + "S", Long.MIN_VALUE, 0},
 491 
 492             {"PT1.1S", 1, 100000000},
 493             {"PT1.12S", 1, 120000000},
 494             {"PT1.123S", 1, 123000000},
 495             {"PT1.1234S", 1, 123400000},
 496             {"PT1.12345S", 1, 123450000},
 497             {"PT1.123456S", 1, 123456000},
 498             {"PT1.1234567S", 1, 123456700},
 499             {"PT1.12345678S", 1, 123456780},
 500             {"PT1.123456789S", 1, 123456789},
 501 
 502             {"PT-1.1S", -2, 1000000000 - 100000000},
 503             {"PT-1.12S", -2, 1000000000 - 120000000},
 504             {"PT-1.123S", -2, 1000000000 - 123000000},
 505             {"PT-1.1234S", -2, 1000000000 - 123400000},
 506             {"PT-1.12345S", -2, 1000000000 - 123450000},
 507             {"PT-1.123456S", -2, 1000000000 - 123456000},
 508             {"PT-1.1234567S", -2, 1000000000 - 123456700},
 509             {"PT-1.12345678S", -2, 1000000000 - 123456780},
 510             {"PT-1.123456789S", -2, 1000000000 - 123456789},
 511 
 512             {"PT" + Long.MAX_VALUE + ".123456789S", Long.MAX_VALUE, 123456789},
 513             {"PT" + Long.MIN_VALUE + ".000000000S", Long.MIN_VALUE, 0},














































































 514         };
 515     }
 516 
 517     @Test(dataProvider="Parse", groups={"tck"})
 518     public void factory_parse(String text, long expectedSeconds, int expectedNanoOfSecond) {
 519         Duration t = Duration.parse(text);
 520         assertEquals(t.getSeconds(), expectedSeconds);
 521         assertEquals(t.getNano(), expectedNanoOfSecond);







 522     }
 523 
 524     @Test(dataProvider="Parse", groups={"tck"})













 525     public void factory_parse_comma(String text, long expectedSeconds, int expectedNanoOfSecond) {
 526         text = text.replace('.', ',');
 527         Duration t = Duration.parse(text);
 528         assertEquals(t.getSeconds(), expectedSeconds);
 529         assertEquals(t.getNano(), expectedNanoOfSecond);







 530     }
 531 
 532     @DataProvider(name="ParseFailures")
 533     Object[][] provider_factory_parseFailures() {
 534         return new Object[][] {
 535             {""},




 536             {"PTS"},
 537             {"AT0S"},
 538             {"PA0S"},
 539             {"PT0A"},
 540 















 541             {"PT+S"},
 542             {"PT-S"},
 543             {"PT.S"},
 544             {"PTAS"},
 545 
 546             {"PT+0S"},
 547             {"PT+00S"},
 548             {"PT+000S"},
 549             {"PT-0S"},
 550             {"PT-00S"},
 551             {"PT-000S"},
 552             {"PT+1S"},
 553             {"PT-.S"},
 554             {"PT+.S"},
 555 
 556             {"PT1ABC2S"},
 557             {"PT1.1ABC2S"},
 558 
 559             {"PT123456789123456789123456789S"},
 560             {"PT0.1234567891S"},
 561             {"PT1.S"},
 562             {"PT.1S"},
 563 
 564             {"PT2.-3"},
 565             {"PT-2.-3"},
 566             {"PT2.+3"},
 567             {"PT-2.+3"},
 568         };
 569     }
 570 
 571     @Test(dataProvider="ParseFailures", expectedExceptions=DateTimeParseException.class, groups={"tck"})
 572     public void factory_parseFailures(String text) {
 573         Duration.parse(text);
 574     }
 575 
 576     @Test(dataProvider="ParseFailures", expectedExceptions=DateTimeParseException.class, groups={"tck"})
 577     public void factory_parseFailures_comma(String text) {
 578         text = text.replace('.', ',');
 579         Duration.parse(text);
 580     }
 581 
 582     @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"})
 583     public void factory_parse_tooBig() {
 584         Duration.parse("PT" + Long.MAX_VALUE + "1S");
 585     }
 586 
 587     @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"})
 588     public void factory_parse_tooBig_decimal() {
 589         Duration.parse("PT" + Long.MAX_VALUE + "1.1S");
 590     }
 591 
 592     @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"})
 593     public void factory_parse_tooSmall() {
 594         Duration.parse("PT" + Long.MIN_VALUE + "1S");
 595     }
 596 
 597     @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"})
 598     public void factory_parse_tooSmall_decimal() {
 599         Duration.parse("PT" + Long.MIN_VALUE + ".1S");
 600     }
 601 
 602     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
 603     public void factory_parse_nullText() {
 604         Duration.parse((String) null);
 605     }
 606 
 607     @Test(groups={"tck"})

 608     public void test_deserialization() throws Exception {
 609         Duration orginal = Duration.ofSeconds(2);
 610         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 611         ObjectOutputStream out = new ObjectOutputStream(baos);
 612         out.writeObject(orginal);
 613         out.close();
 614         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
 615         ObjectInputStream in = new ObjectInputStream(bais);
 616         Duration ser = (Duration) in.readObject();
 617         assertEquals(Duration.ofSeconds(2), ser);
 618     }
 619 
 620     //-----------------------------------------------------------------------
 621     // isZero(), isPositive(), isPositiveOrZero(), isNegative(), isNegativeOrZero()
 622     //-----------------------------------------------------------------------
 623     @Test(groups={"tck"})
 624     public void test_isZero() {
 625         assertEquals(Duration.ofNanos(0).isZero(), true);
 626         assertEquals(Duration.ofSeconds(0).isZero(), true);
 627         assertEquals(Duration.ofNanos(1).isZero(), false);
 628         assertEquals(Duration.ofSeconds(1).isZero(), false);
 629         assertEquals(Duration.ofSeconds(1, 1).isZero(), false);
 630         assertEquals(Duration.ofNanos(-1).isZero(), false);
 631         assertEquals(Duration.ofSeconds(-1).isZero(), false);
 632         assertEquals(Duration.ofSeconds(-1, -1).isZero(), false);
 633     }
 634 
 635     @Test(groups={"tck"})
 636     public void test_isPositive() {
 637         assertEquals(Duration.ofNanos(0).isPositive(), false);
 638         assertEquals(Duration.ofSeconds(0).isPositive(), false);
 639         assertEquals(Duration.ofNanos(1).isPositive(), true);
 640         assertEquals(Duration.ofSeconds(1).isPositive(), true);
 641         assertEquals(Duration.ofSeconds(1, 1).isPositive(), true);
 642         assertEquals(Duration.ofNanos(-1).isPositive(), false);
 643         assertEquals(Duration.ofSeconds(-1).isPositive(), false);
 644         assertEquals(Duration.ofSeconds(-1, -1).isPositive(), false);
 645     }
 646 
 647     @Test(groups={"tck"})
 648     public void test_isNegative() {
 649         assertEquals(Duration.ofNanos(0).isNegative(), false);
 650         assertEquals(Duration.ofSeconds(0).isNegative(), false);
 651         assertEquals(Duration.ofNanos(1).isNegative(), false);
 652         assertEquals(Duration.ofSeconds(1).isNegative(), false);
 653         assertEquals(Duration.ofSeconds(1, 1).isNegative(), false);
 654         assertEquals(Duration.ofNanos(-1).isNegative(), true);
 655         assertEquals(Duration.ofSeconds(-1).isNegative(), true);
 656         assertEquals(Duration.ofSeconds(-1, -1).isNegative(), true);
 657     }
 658 
 659     //-----------------------------------------------------------------------
 660     // plus()
 661     //-----------------------------------------------------------------------
 662     @DataProvider(name="Plus")
 663     Object[][] provider_plus() {
 664         return new Object[][] {
 665             {Long.MIN_VALUE, 0, Long.MAX_VALUE, 0, -1, 0},
 666 
 667             {-4, 666666667, -4, 666666667, -7, 333333334},


1972             Duration.ofSeconds(-1L, 0),
1973             Duration.ofSeconds(-1L, 1),
1974             Duration.ofSeconds(-1L, 999999998),
1975             Duration.ofSeconds(-1L, 999999999),
1976             Duration.ofSeconds(0L, 0),
1977             Duration.ofSeconds(0L, 1),
1978             Duration.ofSeconds(0L, 2),
1979             Duration.ofSeconds(0L, 999999999),
1980             Duration.ofSeconds(1L, 0),
1981             Duration.ofSeconds(2L, 0)
1982         );
1983     }
1984 
1985     void doTest_comparisons_Duration(Duration... durations) {
1986         for (int i = 0; i < durations.length; i++) {
1987             Duration a = durations[i];
1988             for (int j = 0; j < durations.length; j++) {
1989                 Duration b = durations[j];
1990                 if (i < j) {
1991                     assertEquals(a.compareTo(b)< 0, true, a + " <=> " + b);
1992                     assertEquals(a.isLessThan(b), true, a + " <=> " + b);
1993                     assertEquals(a.isGreaterThan(b), false, a + " <=> " + b);
1994                     assertEquals(a.equals(b), false, a + " <=> " + b);
1995                 } else if (i > j) {
1996                     assertEquals(a.compareTo(b) > 0, true, a + " <=> " + b);
1997                     assertEquals(a.isLessThan(b), false, a + " <=> " + b);
1998                     assertEquals(a.isGreaterThan(b), true, a + " <=> " + b);
1999                     assertEquals(a.equals(b), false, a + " <=> " + b);
2000                 } else {
2001                     assertEquals(a.compareTo(b), 0, a + " <=> " + b);
2002                     assertEquals(a.isLessThan(b), false, a + " <=> " + b);
2003                     assertEquals(a.isGreaterThan(b), false, a + " <=> " + b);
2004                     assertEquals(a.equals(b), true, a + " <=> " + b);
2005                 }
2006             }
2007         }
2008     }
2009 
2010     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
2011     public void test_compareTo_ObjectNull() {
2012         Duration a = Duration.ofSeconds(0L, 0);
2013         a.compareTo(null);
2014     }
2015 
2016     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
2017     public void test_isLessThan_ObjectNull() {
2018         Duration a = Duration.ofSeconds(0L, 0);
2019         a.isLessThan(null);
2020     }
2021 
2022     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
2023     public void test_isGreaterThan_ObjectNull() {
2024         Duration a = Duration.ofSeconds(0L, 0);
2025         a.isGreaterThan(null);
2026     }
2027 
2028     @Test(expectedExceptions=ClassCastException.class, groups={"tck"})
2029     @SuppressWarnings({ "unchecked", "rawtypes" })
2030     public void compareToNonDuration() {
2031        Comparable c = Duration.ofSeconds(0L);
2032        c.compareTo(new Object());
2033     }
2034 
2035     //-----------------------------------------------------------------------
2036     // equals()
2037     //-----------------------------------------------------------------------
2038     @Test(groups={"tck"})
2039     public void test_equals() {
2040         Duration test5a = Duration.ofSeconds(5L, 20);
2041         Duration test5b = Duration.ofSeconds(5L, 20);
2042         Duration test5n = Duration.ofSeconds(5L, 30);
2043         Duration test6 = Duration.ofSeconds(6L, 20);
2044 
2045         assertEquals(test5a.equals(test5a), true);
2046         assertEquals(test5a.equals(test5b), true);
2047         assertEquals(test5a.equals(test5n), false);


2079     // hashCode()
2080     //-----------------------------------------------------------------------
2081     @Test(groups={"tck"})
2082     public void test_hashCode() {
2083         Duration test5a = Duration.ofSeconds(5L, 20);
2084         Duration test5b = Duration.ofSeconds(5L, 20);
2085         Duration test5n = Duration.ofSeconds(5L, 30);
2086         Duration test6 = Duration.ofSeconds(6L, 20);
2087 
2088         assertEquals(test5a.hashCode() == test5a.hashCode(), true);
2089         assertEquals(test5a.hashCode() == test5b.hashCode(), true);
2090         assertEquals(test5b.hashCode() == test5b.hashCode(), true);
2091 
2092         assertEquals(test5a.hashCode() == test5n.hashCode(), false);
2093         assertEquals(test5a.hashCode() == test6.hashCode(), false);
2094     }
2095 
2096     //-----------------------------------------------------------------------
2097     // toString()
2098     //-----------------------------------------------------------------------
2099     @DataProvider(name="ToString")
2100     Object[][] provider_toString() {
2101         return new Object[][] {
2102             {0, 0, "PT0S"},
2103             {0, 1, "PT0.000000001S"},
2104             {0, 10, "PT0.00000001S"},
2105             {0, 100, "PT0.0000001S"},
2106             {0, 1000, "PT0.000001S"},
2107             {0, 10000, "PT0.00001S"},
2108             {0, 100000, "PT0.0001S"},
2109             {0, 1000000, "PT0.001S"},
2110             {0, 10000000, "PT0.01S"},
2111             {0, 100000000, "PT0.1S"},
2112             {0, 120000000, "PT0.12S"},
2113             {0, 123000000, "PT0.123S"},
2114             {0, 123400000, "PT0.1234S"},
2115             {0, 123450000, "PT0.12345S"},
2116             {0, 123456000, "PT0.123456S"},
2117             {0, 123456700, "PT0.1234567S"},
2118             {0, 123456780, "PT0.12345678S"},
2119             {0, 123456789, "PT0.123456789S"},
2120             {1, 0, "PT1S"},











2121             {-1, 0, "PT-1S"},
2122             {-1, 1000, "PT-0.999999S"},
2123             {-1, 900000000, "PT-0.1S"},
2124             {Long.MAX_VALUE, 0, "PT9223372036854775807S"},
2125             {Long.MIN_VALUE, 0, "PT-9223372036854775808S"},


2126         };
2127     }
2128 
2129     @Test(dataProvider="ToString", groups={"tck"})
2130     public void test_toString(long seconds, int nanos, String expected) {
2131         Duration t = Duration.ofSeconds(seconds, nanos);
2132         assertEquals(t.toString(), expected);
2133     }
2134 









































2135 }


  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.ChronoUnit.DAYS;
  63 import static java.time.temporal.ChronoUnit.HALF_DAYS;
  64 import static java.time.temporal.ChronoUnit.HOURS;
  65 import static java.time.temporal.ChronoUnit.MICROS;
  66 import static java.time.temporal.ChronoUnit.MILLIS;
  67 import static java.time.temporal.ChronoUnit.MINUTES;
  68 import static java.time.temporal.ChronoUnit.NANOS;
  69 import static java.time.temporal.ChronoUnit.SECONDS;
  70 import static java.time.temporal.ChronoUnit.WEEKS;
  71 import static org.testng.Assert.assertEquals;
  72 import static org.testng.Assert.assertTrue;
  73 import static org.testng.Assert.fail;
  74 
  75 import java.io.ByteArrayInputStream;
  76 import java.io.ByteArrayOutputStream;
  77 import java.io.DataOutputStream;
  78 import java.io.ObjectInputStream;
  79 import java.io.ObjectOutputStream;

  80 import java.time.DateTimeException;
  81 import java.time.Duration;
  82 import java.time.Instant;
  83 import java.time.LocalTime;
  84 import java.time.ZoneOffset;
  85 import java.time.ZonedDateTime;
  86 import java.time.format.DateTimeParseException;
  87 import java.time.temporal.ChronoUnit;
  88 import java.time.temporal.TemporalUnit;
  89 import java.util.List;
  90 import java.util.Locale;
  91 
  92 import org.testng.annotations.DataProvider;
  93 import org.testng.annotations.Test;
  94 
  95 /**
  96  * Test Duration.
  97  */
  98 @Test
  99 public class TCKDuration extends AbstractTCKTest {
 100 
 101     //-----------------------------------------------------------------------
 102     @Test
 103     public void test_serialization() throws Exception {
 104         assertSerializable(Duration.ofHours(5));
 105         assertSerializable(Duration.ofHours(0));
 106         assertSerializable(Duration.ofHours(-5));
 107     }
 108 
 109     @Test
 110     public void test_serialization_format() throws Exception {


 425     }
 426 
 427     @Test(dataProvider="OfTemporalUnitOutOfRange", expectedExceptions=ArithmeticException.class, groups={"tck"})
 428     public void factory_of_longTemporalUnit_outOfRange(long amount, TemporalUnit unit) {
 429         Duration.of(amount, unit);
 430     }
 431 
 432     @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
 433     public void factory_of_longTemporalUnit_estimatedUnit() {
 434         Duration.of(2, WEEKS);
 435     }
 436 
 437     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
 438     public void factory_of_longTemporalUnit_null() {
 439         Duration.of(1, (TemporalUnit) null);
 440     }
 441 
 442     //-----------------------------------------------------------------------
 443     // between()
 444     //-----------------------------------------------------------------------
 445     @DataProvider(name="durationBetweenInstant")
 446     Object[][] data_durationBetweenInstant() {
 447         return new Object[][] {
 448             {0, 0, 0, 0, 0, 0},
 449             {3, 0, 7, 0, 4, 0},
 450             {3, 20, 7, 50, 4, 30},
 451             {3, 80, 7, 50, 3, 999999970},
 452             {7, 0, 3, 0, -4, 0},
 453         };
 454     }
 455 
 456     @Test(dataProvider="durationBetweenInstant")
 457     public void factory_between_TemporalTemporal_Instant(long secs1, int nanos1, long secs2, int nanos2, long expectedSeconds, int expectedNanoOfSecond) {
 458         Instant start = Instant.ofEpochSecond(secs1, nanos1);
 459         Instant end = Instant.ofEpochSecond(secs2, nanos2);
 460         Duration t = Duration.between(start, end);
 461         assertEquals(t.getSeconds(), expectedSeconds);
 462         assertEquals(t.getNano(), expectedNanoOfSecond);
 463     }
 464 
 465     @DataProvider(name="durationBetweenLocalTime")
 466     Object[][] data_durationBetweenLocalTime() {
 467         return new Object[][] {
 468                 {LocalTime.of(11, 0, 30), LocalTime.of(11, 0, 45), 15L, 0},
 469                 {LocalTime.of(11, 0, 30), LocalTime.of(11, 0, 25), -5L, 0},
 470         };
 471     }
 472 
 473     @Test(dataProvider="durationBetweenLocalTime")
 474     public void factory_between_TemporalTemporal_LT(LocalTime start, LocalTime end, long expectedSeconds, int expectedNanoOfSecond) {
 475         Duration t = Duration.between(start, end);
 476         assertEquals(t.getSeconds(), expectedSeconds);
 477         assertEquals(t.getNano(), expectedNanoOfSecond);
 478     }
 479 
 480     @Test(expectedExceptions=DateTimeException.class)
 481     public void factory_between_TemporalTemporal_mixedTypes() {
 482         Instant start = Instant.ofEpochSecond(1);
 483         ZonedDateTime end = Instant.ofEpochSecond(4).atZone(ZoneOffset.UTC);
 484         Duration.between(start, end);
 485     }
 486 
 487     @Test(expectedExceptions=NullPointerException.class)
 488     public void factory_between__TemporalTemporal_startNull() {
 489         Instant end = Instant.ofEpochSecond(1);
 490         Duration.between(null, end);
 491     }
 492 
 493     @Test(expectedExceptions=NullPointerException.class)
 494     public void factory_between__TemporalTemporal_endNull() {
 495         Instant start = Instant.ofEpochSecond(1);
 496         Duration.between(start, null);
 497     }
 498 
 499     //-----------------------------------------------------------------------
 500     // parse(String)
 501     //-----------------------------------------------------------------------
 502     @DataProvider(name="parseSuccess")
 503     Object[][] data_parseSuccess() {
 504         return new Object[][] {
 505                 {"PT0S", 0, 0},




 506                 {"PT1S", 1, 0},
 507                 {"PT12S", 12, 0},
 508                 {"PT123456789S", 123456789, 0},
 509                 {"PT" + Long.MAX_VALUE + "S", Long.MAX_VALUE, 0},
 510 
 511                 {"PT+1S", 1, 0},
 512                 {"PT+12S", 12, 0},
 513                 {"PT+123456789S", 123456789, 0},
 514                 {"PT+" + Long.MAX_VALUE + "S", Long.MAX_VALUE, 0},
 515 
 516                 {"PT-1S", -1, 0},
 517                 {"PT-12S", -12, 0},
 518                 {"PT-123456789S", -123456789, 0},
 519                 {"PT" + Long.MIN_VALUE + "S", Long.MIN_VALUE, 0},
 520 
 521                 {"PT1.1S", 1, 100000000},
 522                 {"PT1.12S", 1, 120000000},
 523                 {"PT1.123S", 1, 123000000},
 524                 {"PT1.1234S", 1, 123400000},
 525                 {"PT1.12345S", 1, 123450000},
 526                 {"PT1.123456S", 1, 123456000},
 527                 {"PT1.1234567S", 1, 123456700},
 528                 {"PT1.12345678S", 1, 123456780},
 529                 {"PT1.123456789S", 1, 123456789},
 530 
 531                 {"PT-1.1S", -2, 1000000000 - 100000000},
 532                 {"PT-1.12S", -2, 1000000000 - 120000000},
 533                 {"PT-1.123S", -2, 1000000000 - 123000000},
 534                 {"PT-1.1234S", -2, 1000000000 - 123400000},
 535                 {"PT-1.12345S", -2, 1000000000 - 123450000},
 536                 {"PT-1.123456S", -2, 1000000000 - 123456000},
 537                 {"PT-1.1234567S", -2, 1000000000 - 123456700},
 538                 {"PT-1.12345678S", -2, 1000000000 - 123456780},
 539                 {"PT-1.123456789S", -2, 1000000000 - 123456789},
 540 
 541                 {"PT" + Long.MAX_VALUE + ".123456789S", Long.MAX_VALUE, 123456789},
 542                 {"PT" + Long.MIN_VALUE + ".000000000S", Long.MIN_VALUE, 0},
 543 
 544                 {"PT01S", 1, 0},
 545                 {"PT001S", 1, 0},
 546                 {"PT000S", 0, 0},
 547                 {"PT+01S", 1, 0},
 548                 {"PT-01S", -1, 0},
 549 
 550                 {"PT1.S", 1, 0},
 551                 {"PT+1.S", 1, 0},
 552                 {"PT-1.S", -1, 0},
 553 
 554                 {"P0D", 0, 0},
 555                 {"P0DT0H", 0, 0},
 556                 {"P0DT0M", 0, 0},
 557                 {"P0DT0S", 0, 0},
 558                 {"P0DT0H0S", 0, 0},
 559                 {"P0DT0M0S", 0, 0},
 560                 {"P0DT0H0M0S", 0, 0},
 561 
 562                 {"P1D", 86400, 0},
 563                 {"P1DT0H", 86400, 0},
 564                 {"P1DT0M", 86400, 0},
 565                 {"P1DT0S", 86400, 0},
 566                 {"P1DT0H0S", 86400, 0},
 567                 {"P1DT0M0S", 86400, 0},
 568                 {"P1DT0H0M0S", 86400, 0},
 569 
 570                 {"P3D", 86400 * 3, 0},
 571                 {"P3DT2H", 86400 * 3 + 3600 * 2, 0},
 572                 {"P3DT2M", 86400 * 3 + 60 * 2, 0},
 573                 {"P3DT2S", 86400 * 3 + 2, 0},
 574                 {"P3DT2H1S", 86400 * 3 + 3600 * 2 + 1, 0},
 575                 {"P3DT2M1S", 86400 * 3 + 60 * 2 + 1, 0},
 576                 {"P3DT2H1M1S", 86400 * 3 + 3600 * 2 + 60 + 1, 0},
 577 
 578                 {"P-3D", -86400 * 3, 0},
 579                 {"P-3DT2H", -86400 * 3 + 3600 * 2, 0},
 580                 {"P-3DT2M", -86400 * 3 + 60 * 2, 0},
 581                 {"P-3DT2S", -86400 * 3 + 2, 0},
 582                 {"P-3DT2H1S", -86400 * 3 + 3600 * 2 + 1, 0},
 583                 {"P-3DT2M1S", -86400 * 3 + 60 * 2 + 1, 0},
 584                 {"P-3DT2H1M1S", -86400 * 3 + 3600 * 2 + 60 + 1, 0},
 585 
 586                 {"P-3DT-2H", -86400 * 3 - 3600 * 2, 0},
 587                 {"P-3DT-2M", -86400 * 3 - 60 * 2, 0},
 588                 {"P-3DT-2S", -86400 * 3 - 2, 0},
 589                 {"P-3DT-2H1S", -86400 * 3 - 3600 * 2 + 1, 0},
 590                 {"P-3DT-2M1S", -86400 * 3 - 60 * 2 + 1, 0},
 591                 {"P-3DT-2H1M1S", -86400 * 3 - 3600 * 2 + 60 + 1, 0},
 592 
 593                 {"PT0H", 0, 0},
 594                 {"PT0H0M", 0, 0},
 595                 {"PT0H0S", 0, 0},
 596                 {"PT0H0M0S", 0, 0},
 597 
 598                 {"PT1H", 3600, 0},
 599                 {"PT3H", 3600 * 3, 0},
 600                 {"PT-1H", -3600, 0},
 601                 {"PT-3H", -3600 * 3, 0},
 602 
 603                 {"PT2H5M", 3600 * 2 + 60 * 5, 0},
 604                 {"PT2H5S", 3600 * 2 + 5, 0},
 605                 {"PT2H5M8S", 3600 * 2 + 60 * 5 + 8, 0},
 606                 {"PT-2H5M", -3600 * 2 + 60 * 5, 0},
 607                 {"PT-2H5S", -3600 * 2 + 5, 0},
 608                 {"PT-2H5M8S", -3600 * 2 + 60 * 5 + 8, 0},
 609                 {"PT-2H-5M", -3600 * 2 - 60 * 5, 0},
 610                 {"PT-2H-5S", -3600 * 2 - 5, 0},
 611                 {"PT-2H-5M8S", -3600 * 2 - 60 * 5 + 8, 0},
 612                 {"PT-2H-5M-8S", -3600 * 2 - 60 * 5 - 8, 0},
 613 
 614                 {"PT0M", 0, 0},
 615                 {"PT1M", 60, 0},
 616                 {"PT3M", 60 * 3, 0},
 617                 {"PT-1M", -60, 0},
 618                 {"PT-3M", -60 * 3, 0},
 619                 {"P0DT3M", 60 * 3, 0},
 620                 {"P0DT-3M", -60 * 3, 0},
 621         };
 622     }
 623 
 624     @Test(dataProvider="parseSuccess")
 625     public void factory_parse(String text, long expectedSeconds, int expectedNanoOfSecond) {
 626         Duration test = Duration.parse(text);
 627         assertEquals(test.getSeconds(), expectedSeconds);
 628         assertEquals(test.getNano(), expectedNanoOfSecond);
 629     }
 630 
 631     @Test(dataProvider="parseSuccess")
 632     public void factory_parse_plus(String text, long expectedSeconds, int expectedNanoOfSecond) {
 633         Duration test = Duration.parse("+" + text);
 634         assertEquals(test.getSeconds(), expectedSeconds);
 635         assertEquals(test.getNano(), expectedNanoOfSecond);
 636     }
 637 
 638     @Test(dataProvider="parseSuccess")
 639     public void factory_parse_minus(String text, long expectedSeconds, int expectedNanoOfSecond) {
 640         Duration test;
 641         try {
 642             test = Duration.parse("-" + text);
 643         } catch (DateTimeParseException ex) {
 644             assertEquals(expectedSeconds == Long.MIN_VALUE, true);
 645             return;
 646         }
 647         // not inside try/catch or it breaks test
 648         assertEquals(test, Duration.ofSeconds(expectedSeconds, expectedNanoOfSecond).negated());
 649     }
 650 
 651     @Test(dataProvider="parseSuccess")
 652     public void factory_parse_comma(String text, long expectedSeconds, int expectedNanoOfSecond) {
 653         text = text.replace('.', ',');
 654         Duration test = Duration.parse(text);
 655         assertEquals(test.getSeconds(), expectedSeconds);
 656         assertEquals(test.getNano(), expectedNanoOfSecond);
 657     }
 658 
 659     @Test(dataProvider="parseSuccess")
 660     public void factory_parse_lowerCase(String text, long expectedSeconds, int expectedNanoOfSecond) {
 661         Duration test = Duration.parse(text.toLowerCase(Locale.ENGLISH));
 662         assertEquals(test.getSeconds(), expectedSeconds);
 663         assertEquals(test.getNano(), expectedNanoOfSecond);
 664     }
 665 
 666     @DataProvider(name="parseFailure")
 667     Object[][] data_parseFailure() {
 668         return new Object[][] {
 669                 {""},
 670                 {"ABCDEF"},
 671                 {" PT0S"},
 672                 {"PT0S "},
 673 
 674                 {"PTS"},
 675                 {"AT0S"},
 676                 {"PA0S"},
 677                 {"PT0A"},
 678 
 679                 {"P0Y"},
 680                 {"P1Y"},
 681                 {"P-2Y"},
 682                 {"P0M"},
 683                 {"P1M"},
 684                 {"P-2M"},
 685                 {"P3Y2D"},
 686                 {"P3M2D"},
 687                 {"P3W"},
 688                 {"P-3W"},
 689                 {"P2YT30S"},
 690                 {"P2MT30S"},
 691 
 692                 {"P1DT"},
 693 
 694                 {"PT+S"},
 695                 {"PT-S"},
 696                 {"PT.S"},
 697                 {"PTAS"},
 698 







 699                 {"PT-.S"},
 700                 {"PT+.S"},
 701 
 702                 {"PT1ABC2S"},
 703                 {"PT1.1ABC2S"},
 704 
 705                 {"PT123456789123456789123456789S"},
 706                 {"PT0.1234567891S"},


 707 
 708                 {"PT2.-3"},
 709                 {"PT-2.-3"},
 710                 {"PT2.+3"},
 711                 {"PT-2.+3"},
 712         };
 713     }
 714 
 715     @Test(dataProvider="parseFailure", expectedExceptions=DateTimeParseException.class)
 716     public void factory_parseFailures(String text) {
 717         Duration.parse(text);
 718     }
 719 
 720     @Test(dataProvider="parseFailure", expectedExceptions=DateTimeParseException.class)
 721     public void factory_parseFailures_comma(String text) {
 722         text = text.replace('.', ',');
 723         Duration.parse(text);
 724     }
 725 
 726     @Test(expectedExceptions=DateTimeParseException.class)
 727     public void factory_parse_tooBig() {
 728         Duration.parse("PT" + Long.MAX_VALUE + "1S");
 729     }
 730 
 731     @Test(expectedExceptions=DateTimeParseException.class)
 732     public void factory_parse_tooBig_decimal() {
 733         Duration.parse("PT" + Long.MAX_VALUE + "1.1S");
 734     }
 735 
 736     @Test(expectedExceptions=DateTimeParseException.class)
 737     public void factory_parse_tooSmall() {
 738         Duration.parse("PT" + Long.MIN_VALUE + "1S");
 739     }
 740 
 741     @Test(expectedExceptions=DateTimeParseException.class)
 742     public void factory_parse_tooSmall_decimal() {
 743         Duration.parse("PT" + Long.MIN_VALUE + ".1S");
 744     }
 745 
 746     @Test(expectedExceptions=NullPointerException.class)
 747     public void factory_parse_nullText() {
 748         Duration.parse(null);
 749     }
 750 
 751     //-----------------------------------------------------------------------
 752     @Test
 753     public void test_deserialization() throws Exception {
 754         Duration orginal = Duration.ofSeconds(2);
 755         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 756         ObjectOutputStream out = new ObjectOutputStream(baos);
 757         out.writeObject(orginal);
 758         out.close();
 759         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
 760         ObjectInputStream in = new ObjectInputStream(bais);
 761         Duration ser = (Duration) in.readObject();
 762         assertEquals(Duration.ofSeconds(2), ser);
 763     }
 764 
 765     //-----------------------------------------------------------------------
 766     // isZero(), isPositive(), isPositiveOrZero(), isNegative(), isNegativeOrZero()
 767     //-----------------------------------------------------------------------
 768     @Test
 769     public void test_isZero() {
 770         assertEquals(Duration.ofNanos(0).isZero(), true);
 771         assertEquals(Duration.ofSeconds(0).isZero(), true);
 772         assertEquals(Duration.ofNanos(1).isZero(), false);
 773         assertEquals(Duration.ofSeconds(1).isZero(), false);
 774         assertEquals(Duration.ofSeconds(1, 1).isZero(), false);
 775         assertEquals(Duration.ofNanos(-1).isZero(), false);
 776         assertEquals(Duration.ofSeconds(-1).isZero(), false);
 777         assertEquals(Duration.ofSeconds(-1, -1).isZero(), false);
 778     }
 779 
 780     @Test












 781     public void test_isNegative() {
 782         assertEquals(Duration.ofNanos(0).isNegative(), false);
 783         assertEquals(Duration.ofSeconds(0).isNegative(), false);
 784         assertEquals(Duration.ofNanos(1).isNegative(), false);
 785         assertEquals(Duration.ofSeconds(1).isNegative(), false);
 786         assertEquals(Duration.ofSeconds(1, 1).isNegative(), false);
 787         assertEquals(Duration.ofNanos(-1).isNegative(), true);
 788         assertEquals(Duration.ofSeconds(-1).isNegative(), true);
 789         assertEquals(Duration.ofSeconds(-1, -1).isNegative(), true);
 790     }
 791 
 792     //-----------------------------------------------------------------------
 793     // plus()
 794     //-----------------------------------------------------------------------
 795     @DataProvider(name="Plus")
 796     Object[][] provider_plus() {
 797         return new Object[][] {
 798             {Long.MIN_VALUE, 0, Long.MAX_VALUE, 0, -1, 0},
 799 
 800             {-4, 666666667, -4, 666666667, -7, 333333334},


2105             Duration.ofSeconds(-1L, 0),
2106             Duration.ofSeconds(-1L, 1),
2107             Duration.ofSeconds(-1L, 999999998),
2108             Duration.ofSeconds(-1L, 999999999),
2109             Duration.ofSeconds(0L, 0),
2110             Duration.ofSeconds(0L, 1),
2111             Duration.ofSeconds(0L, 2),
2112             Duration.ofSeconds(0L, 999999999),
2113             Duration.ofSeconds(1L, 0),
2114             Duration.ofSeconds(2L, 0)
2115         );
2116     }
2117 
2118     void doTest_comparisons_Duration(Duration... durations) {
2119         for (int i = 0; i < durations.length; i++) {
2120             Duration a = durations[i];
2121             for (int j = 0; j < durations.length; j++) {
2122                 Duration b = durations[j];
2123                 if (i < j) {
2124                     assertEquals(a.compareTo(b)< 0, true, a + " <=> " + b);


2125                     assertEquals(a.equals(b), false, a + " <=> " + b);
2126                 } else if (i > j) {
2127                     assertEquals(a.compareTo(b) > 0, true, a + " <=> " + b);


2128                     assertEquals(a.equals(b), false, a + " <=> " + b);
2129                 } else {
2130                     assertEquals(a.compareTo(b), 0, a + " <=> " + b);


2131                     assertEquals(a.equals(b), true, a + " <=> " + b);
2132                 }
2133             }
2134         }
2135     }
2136 
2137     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
2138     public void test_compareTo_ObjectNull() {
2139         Duration a = Duration.ofSeconds(0L, 0);
2140         a.compareTo(null);
2141     }
2142 












2143     @Test(expectedExceptions=ClassCastException.class, groups={"tck"})
2144     @SuppressWarnings({ "unchecked", "rawtypes" })
2145     public void compareToNonDuration() {
2146        Comparable c = Duration.ofSeconds(0L);
2147        c.compareTo(new Object());
2148     }
2149 
2150     //-----------------------------------------------------------------------
2151     // equals()
2152     //-----------------------------------------------------------------------
2153     @Test(groups={"tck"})
2154     public void test_equals() {
2155         Duration test5a = Duration.ofSeconds(5L, 20);
2156         Duration test5b = Duration.ofSeconds(5L, 20);
2157         Duration test5n = Duration.ofSeconds(5L, 30);
2158         Duration test6 = Duration.ofSeconds(6L, 20);
2159 
2160         assertEquals(test5a.equals(test5a), true);
2161         assertEquals(test5a.equals(test5b), true);
2162         assertEquals(test5a.equals(test5n), false);


2194     // hashCode()
2195     //-----------------------------------------------------------------------
2196     @Test(groups={"tck"})
2197     public void test_hashCode() {
2198         Duration test5a = Duration.ofSeconds(5L, 20);
2199         Duration test5b = Duration.ofSeconds(5L, 20);
2200         Duration test5n = Duration.ofSeconds(5L, 30);
2201         Duration test6 = Duration.ofSeconds(6L, 20);
2202 
2203         assertEquals(test5a.hashCode() == test5a.hashCode(), true);
2204         assertEquals(test5a.hashCode() == test5b.hashCode(), true);
2205         assertEquals(test5b.hashCode() == test5b.hashCode(), true);
2206 
2207         assertEquals(test5a.hashCode() == test5n.hashCode(), false);
2208         assertEquals(test5a.hashCode() == test6.hashCode(), false);
2209     }
2210 
2211     //-----------------------------------------------------------------------
2212     // toString()
2213     //-----------------------------------------------------------------------
2214     @DataProvider(name="toString")
2215     Object[][] provider_toString() {
2216         return new Object[][] {
2217             {0, 0, "PT0S"},
2218             {0, 1, "PT0.000000001S"},
2219             {0, 10, "PT0.00000001S"},
2220             {0, 100, "PT0.0000001S"},
2221             {0, 1000, "PT0.000001S"},
2222             {0, 10000, "PT0.00001S"},
2223             {0, 100000, "PT0.0001S"},
2224             {0, 1000000, "PT0.001S"},
2225             {0, 10000000, "PT0.01S"},
2226             {0, 100000000, "PT0.1S"},
2227             {0, 120000000, "PT0.12S"},
2228             {0, 123000000, "PT0.123S"},
2229             {0, 123400000, "PT0.1234S"},
2230             {0, 123450000, "PT0.12345S"},
2231             {0, 123456000, "PT0.123456S"},
2232             {0, 123456700, "PT0.1234567S"},
2233             {0, 123456780, "PT0.12345678S"},
2234             {0, 123456789, "PT0.123456789S"},
2235             {1, 0, "PT1S"},
2236             {59, 0, "PT59S"},
2237             {60, 0, "PT1M"},
2238             {61, 0, "PT1M1S"},
2239             {3599, 0, "PT59M59S"},
2240             {3600, 0, "PT1H"},
2241             {3601, 0, "PT1H1S"},
2242             {3661, 0, "PT1H1M1S"},
2243             {86399, 0, "PT23H59M59S"},
2244             {86400, 0, "PT24H"},
2245             {59, 0, "PT59S"},
2246             {59, 0, "PT59S"},
2247             {-1, 0, "PT-1S"},
2248             {-1, 1000, "PT-0.999999S"},
2249             {-1, 900000000, "PT-0.1S"},
2250             {Long.MAX_VALUE, 0, "PT" + (Long.MAX_VALUE / 3600) + "H" +
2251                     ((Long.MAX_VALUE % 3600) / 60) + "M" + (Long.MAX_VALUE % 60) + "S"},
2252             {Long.MIN_VALUE, 0, "PT" + (Long.MIN_VALUE / 3600) + "H" +
2253                     ((Long.MIN_VALUE % 3600) / 60) + "M" + (Long.MIN_VALUE % 60) + "S"},
2254         };
2255     }
2256 
2257     @Test(dataProvider="toString")
2258     public void test_toString(long seconds, int nanos, String expected) {
2259         Duration t = Duration.ofSeconds(seconds, nanos);
2260         assertEquals(t.toString(), expected);
2261     }
2262 
2263     //-----------------------------------------------------------------------
2264     @Test(groups="{tck}")
2265     public void test_duration_getUnits() {
2266         Duration duration = Duration.ofSeconds(5000, 1000);
2267         List<TemporalUnit> units = duration.getUnits();
2268         assertEquals(units.size(), 2, "Period.getUnits length");
2269         assertTrue(units.contains(ChronoUnit.SECONDS), "Period.getUnits contains ChronoUnit.SECONDS");
2270         assertTrue(units.contains(ChronoUnit.NANOS), "contains ChronoUnit.NANOS");
2271     }
2272 
2273     @Test()
2274     public void test_getUnit() {
2275         Duration test = Duration.ofSeconds(2000, 1000);
2276         long seconds = test.get(ChronoUnit.SECONDS);
2277         assertEquals(seconds, 2000, "duration.get(SECONDS)");
2278         long nanos = test.get(ChronoUnit.NANOS);
2279         assertEquals(nanos, 1000, "duration.get(NANOS)");
2280     }
2281 
2282     @DataProvider(name="BadTemporalUnit")
2283     Object[][] provider_factory_of_badTemporalUnit() {
2284         return new Object[][] {
2285             {0, MICROS},
2286             {0, MILLIS},
2287             {0, MINUTES},
2288             {0, HOURS},
2289             {0, HALF_DAYS},
2290             {0, DAYS},
2291             {0, ChronoUnit.MONTHS},
2292             {0, ChronoUnit.YEARS},
2293             {0, ChronoUnit.DECADES},
2294             {0, ChronoUnit.CENTURIES},
2295             {0, ChronoUnit.MILLENNIA},
2296         };
2297     }
2298 
2299     @Test(dataProvider="BadTemporalUnit", expectedExceptions=DateTimeException.class)
2300     public void test_bad_getUnit(long amount, TemporalUnit unit) {
2301         Duration t = Duration.of(amount, unit);
2302         long actual = t.get(unit);
2303     }
2304 }