test/java/time/test/java/time/TestOffsetDateTime.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 test.java.time.temporal;
  61 
  62 import static org.testng.Assert.assertSame;
  63 
  64 import java.time.LocalDate;
  65 import java.time.LocalDateTime;
  66 import java.time.LocalTime;
  67 import java.time.ZoneOffset;
  68 import java.time.temporal.OffsetDateTime;
  69 
  70 import org.testng.annotations.BeforeMethod;
  71 import org.testng.annotations.DataProvider;
  72 import org.testng.annotations.Test;
  73 import test.java.time.AbstractTest;
  74 import test.java.time.MockSimplePeriod;
  75 
  76 /**
  77  * Test OffsetDateTime.
  78  */
  79 @Test
  80 public class TestOffsetDateTime extends AbstractTest {
  81 
  82     private static final ZoneOffset OFFSET_PONE = ZoneOffset.ofHours(1);
  83     private static final ZoneOffset OFFSET_PTWO = ZoneOffset.ofHours(2);
  84     private OffsetDateTime TEST_2008_6_30_11_30_59_000000500;
  85 
  86     @BeforeMethod(groups={"tck","implementation"})
  87     public void setUp() {
  88         TEST_2008_6_30_11_30_59_000000500 = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59, 500), OFFSET_PONE);
  89     }
  90 
  91     @Test
  92     public void test_immutable() {
  93         assertImmutable(OffsetDateTime.class);
  94     }


  97     // basics
  98     //-----------------------------------------------------------------------
  99     @DataProvider(name="sampleTimes")
 100     Object[][] provider_sampleTimes() {
 101         return new Object[][] {
 102             {2008, 6, 30, 11, 30, 20, 500, OFFSET_PONE},
 103             {2008, 6, 30, 11, 0, 0, 0, OFFSET_PONE},
 104             {2008, 6, 30, 23, 59, 59, 999999999, OFFSET_PONE},
 105             {-1, 1, 1, 0, 0, 0, 0, OFFSET_PONE},
 106         };
 107     }
 108 
 109     @Test(dataProvider="sampleTimes", groups={"implementation"})
 110     public void test_get_same(int y, int o, int d, int h, int m, int s, int n, ZoneOffset offset) {
 111         LocalDate localDate = LocalDate.of(y, o, d);
 112         LocalTime localTime = LocalTime.of(h, m, s, n);
 113         LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
 114         OffsetDateTime a = OffsetDateTime.of(localDateTime, offset);
 115 
 116         assertSame(a.getOffset(), offset);
 117         assertSame(a.getDate(), localDate);
 118         assertSame(a.getTime(), localTime);
 119         assertSame(a.getDateTime(), localDateTime);
 120     }
 121 
 122     //-----------------------------------------------------------------------
 123     // withOffsetSameLocal()
 124     //-----------------------------------------------------------------------
 125     @Test(groups={"implementation"})
 126     public void test_withOffsetSameLocal() {
 127         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 128         OffsetDateTime test = base.withOffsetSameLocal(OFFSET_PTWO);
 129         assertSame(test.getDateTime(), base.getDateTime());
 130         assertSame(test.getOffset(), OFFSET_PTWO);
 131     }
 132 
 133     @Test(groups={"implementation"})
 134     public void test_withOffsetSameLocal_noChange() {
 135         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 136         OffsetDateTime test = base.withOffsetSameLocal(OFFSET_PONE);
 137         assertSame(test, base);
 138     }
 139 
 140     @Test(groups={"implementation"})
 141     public void test_withOffsetSameInstant_noChange() {
 142         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 143         OffsetDateTime test = base.withOffsetSameInstant(OFFSET_PONE);
 144         assertSame(test, base);
 145     }
 146 
 147     @Test(groups={"implementation"})
 148     public void test_withYear_noChange() {
 149         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);




  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 test.java.time;
  61 
  62 import static org.testng.Assert.assertSame;
  63 
  64 import java.time.LocalDate;
  65 import java.time.LocalDateTime;
  66 import java.time.LocalTime;
  67 import java.time.ZoneOffset;
  68 import java.time.OffsetDateTime;
  69 
  70 import org.testng.annotations.BeforeMethod;
  71 import org.testng.annotations.DataProvider;
  72 import org.testng.annotations.Test;


  73 
  74 /**
  75  * Test OffsetDateTime.
  76  */
  77 @Test
  78 public class TestOffsetDateTime extends AbstractTest {
  79 
  80     private static final ZoneOffset OFFSET_PONE = ZoneOffset.ofHours(1);
  81     private static final ZoneOffset OFFSET_PTWO = ZoneOffset.ofHours(2);
  82     private OffsetDateTime TEST_2008_6_30_11_30_59_000000500;
  83 
  84     @BeforeMethod(groups={"tck","implementation"})
  85     public void setUp() {
  86         TEST_2008_6_30_11_30_59_000000500 = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59, 500), OFFSET_PONE);
  87     }
  88 
  89     @Test
  90     public void test_immutable() {
  91         assertImmutable(OffsetDateTime.class);
  92     }


  95     // basics
  96     //-----------------------------------------------------------------------
  97     @DataProvider(name="sampleTimes")
  98     Object[][] provider_sampleTimes() {
  99         return new Object[][] {
 100             {2008, 6, 30, 11, 30, 20, 500, OFFSET_PONE},
 101             {2008, 6, 30, 11, 0, 0, 0, OFFSET_PONE},
 102             {2008, 6, 30, 23, 59, 59, 999999999, OFFSET_PONE},
 103             {-1, 1, 1, 0, 0, 0, 0, OFFSET_PONE},
 104         };
 105     }
 106 
 107     @Test(dataProvider="sampleTimes", groups={"implementation"})
 108     public void test_get_same(int y, int o, int d, int h, int m, int s, int n, ZoneOffset offset) {
 109         LocalDate localDate = LocalDate.of(y, o, d);
 110         LocalTime localTime = LocalTime.of(h, m, s, n);
 111         LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
 112         OffsetDateTime a = OffsetDateTime.of(localDateTime, offset);
 113 
 114         assertSame(a.getOffset(), offset);
 115         assertSame(a.toLocalDate(), localDate);
 116         assertSame(a.toLocalTime(), localTime);
 117         assertSame(a.toLocalDateTime(), localDateTime);
 118     }
 119 
 120     //-----------------------------------------------------------------------
 121     // withOffsetSameLocal()
 122     //-----------------------------------------------------------------------
 123     @Test(groups={"implementation"})
 124     public void test_withOffsetSameLocal() {
 125         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 126         OffsetDateTime test = base.withOffsetSameLocal(OFFSET_PTWO);
 127         assertSame(test.toLocalDateTime(), base.toLocalDateTime());
 128         assertSame(test.getOffset(), OFFSET_PTWO);
 129     }
 130 
 131     @Test(groups={"implementation"})
 132     public void test_withOffsetSameLocal_noChange() {
 133         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 134         OffsetDateTime test = base.withOffsetSameLocal(OFFSET_PONE);
 135         assertSame(test, base);
 136     }
 137 
 138     @Test(groups={"implementation"})
 139     public void test_withOffsetSameInstant_noChange() {
 140         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
 141         OffsetDateTime test = base.withOffsetSameInstant(OFFSET_PONE);
 142         assertSame(test, base);
 143     }
 144 
 145     @Test(groups={"implementation"})
 146     public void test_withYear_noChange() {
 147         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);