• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * This file is available under and governed by the GNU General Public
26  * License version 2 only, as published by the Free Software Foundation.
27  * However, the following notice accompanied the original version of this
28  * file:
29  *
30  * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos
31  *
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions are met:
36  *
37  *  * Redistributions of source code must retain the above copyright notice,
38  *    this list of conditions and the following disclaimer.
39  *
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.OffsetDateTime;
68 import java.time.ZoneOffset;
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
setUp()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
test_immutable()90     public void test_immutable() {
91         assertImmutable(OffsetDateTime.class);
92     }
93 
94     //-----------------------------------------------------------------------
95     // basics
96     //-----------------------------------------------------------------------
97     @DataProvider(name="sampleTimes")
provider_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")
test_get_same(int y, int o, int d, int h, int m, int s, int n, ZoneOffset offset)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
test_withOffsetSameLocal()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
test_withOffsetSameLocal_noChange()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
test_withOffsetSameInstant_noChange()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
test_withYear_noChange()146     public void test_withYear_noChange() {
147         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
148         OffsetDateTime test = base.withYear(2008);
149         assertSame(test, base);
150     }
151 
152     @Test
test_withMonth_noChange()153     public void test_withMonth_noChange() {
154         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
155         OffsetDateTime test = base.withMonth(6);
156         assertSame(test, base);
157     }
158 
159     @Test
test_withDayOfMonth_noChange()160     public void test_withDayOfMonth_noChange() {
161         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
162         OffsetDateTime test = base.withDayOfMonth(30);
163         assertSame(test, base);
164     }
165 
166     @Test
test_withDayOfYear_noChange()167     public void test_withDayOfYear_noChange() {
168         OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.withDayOfYear(31 + 29 + 31 + 30 + 31 + 30);
169         assertSame(t, TEST_2008_6_30_11_30_59_000000500);
170     }
171 
172     @Test
test_withHour_noChange()173     public void test_withHour_noChange() {
174         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
175         OffsetDateTime test = base.withHour(11);
176         assertSame(test, base);
177     }
178 
179     @Test
test_withMinute_noChange()180     public void test_withMinute_noChange() {
181         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
182         OffsetDateTime test = base.withMinute(30);
183         assertSame(test, base);
184     }
185 
186     @Test
test_withSecond_noChange()187     public void test_withSecond_noChange() {
188         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
189         OffsetDateTime test = base.withSecond(59);
190         assertSame(test, base);
191     }
192 
193     @Test
test_withNanoOfSecond_noChange()194     public void test_withNanoOfSecond_noChange() {
195         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59, 1), OFFSET_PONE);
196         OffsetDateTime test = base.withNano(1);
197         assertSame(test, base);
198     }
199 
200     @Test
test_plus_Period_zero()201     public void test_plus_Period_zero() {
202         OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.plus(MockSimplePeriod.ZERO_DAYS);
203         assertSame(t, TEST_2008_6_30_11_30_59_000000500);
204     }
205 
206     @Test
test_plusYears_zero()207     public void test_plusYears_zero() {
208         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
209         OffsetDateTime test = base.plusYears(0);
210         assertSame(test, base);
211     }
212 
213     @Test
test_plusMonths_zero()214     public void test_plusMonths_zero() {
215         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
216         OffsetDateTime test = base.plusMonths(0);
217         assertSame(test, base);
218     }
219 
220     @Test
test_plusWeeks_zero()221     public void test_plusWeeks_zero() {
222         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
223         OffsetDateTime test = base.plusWeeks(0);
224         assertSame(test, base);
225     }
226 
227     @Test
test_plusDays_zero()228     public void test_plusDays_zero() {
229         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
230         OffsetDateTime test = base.plusDays(0);
231         assertSame(test, base);
232     }
233 
234     @Test
test_plusHours_zero()235     public void test_plusHours_zero() {
236         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
237         OffsetDateTime test = base.plusHours(0);
238         assertSame(test, base);
239     }
240 
241     @Test
test_plusMinutes_zero()242     public void test_plusMinutes_zero() {
243         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
244         OffsetDateTime test = base.plusMinutes(0);
245         assertSame(test, base);
246     }
247 
248     @Test
test_plusSeconds_zero()249     public void test_plusSeconds_zero() {
250         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
251         OffsetDateTime test = base.plusSeconds(0);
252         assertSame(test, base);
253     }
254 
255     @Test
test_plusNanos_zero()256     public void test_plusNanos_zero() {
257         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
258         OffsetDateTime test = base.plusNanos(0);
259     }
260 
261     @Test
test_minus_Period_zero()262     public void test_minus_Period_zero() {
263         OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.minus(MockSimplePeriod.ZERO_DAYS);
264         assertSame(t, TEST_2008_6_30_11_30_59_000000500);
265     }
266 
267     @Test
test_minusYears_zero()268     public void test_minusYears_zero() {
269         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2007, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
270         OffsetDateTime test = base.minusYears(0);
271         assertSame(test, base);
272     }
273 
274     @Test
test_minusMonths_zero()275     public void test_minusMonths_zero() {
276         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
277         OffsetDateTime test = base.minusMonths(0);
278         assertSame(test, base);
279     }
280 
281     @Test
test_minusWeeks_zero()282     public void test_minusWeeks_zero() {
283         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
284         OffsetDateTime test = base.minusWeeks(0);
285         assertSame(test, base);
286     }
287 
288     @Test
test_minusDays_zero()289     public void test_minusDays_zero() {
290         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
291         OffsetDateTime test = base.minusDays(0);
292         assertSame(test, base);
293     }
294 
295     @Test
test_minusHours_zero()296     public void test_minusHours_zero() {
297         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
298         OffsetDateTime test = base.minusHours(0);
299         assertSame(test, base);
300     }
301 
302     @Test
test_minusMinutes_zero()303     public void test_minusMinutes_zero() {
304         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
305         OffsetDateTime test = base.minusMinutes(0);
306         assertSame(test, base);
307     }
308 
309     @Test
test_minusSeconds_zero()310     public void test_minusSeconds_zero() {
311         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
312         OffsetDateTime test = base.minusSeconds(0);
313         assertSame(test, base);
314     }
315 
316     @Test
test_minusNanos_zero()317     public void test_minusNanos_zero() {
318         OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE);
319         OffsetDateTime test = base.minusNanos(0);
320         assertSame(test, base);
321     }
322 
323 }
324