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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /*
27  * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos
28  *
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions are met:
33  *
34  *  * Redistributions of source code must retain the above copyright notice,
35  *    this list of conditions and the following disclaimer.
36  *
37  *  * Redistributions in binary form must reproduce the above copyright notice,
38  *    this list of conditions and the following disclaimer in the documentation
39  *    and/or other materials provided with the distribution.
40  *
41  *  * Neither the name of JSR-310 nor the names of its contributors
42  *    may be used to endorse or promote products derived from this software
43  *    without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56  */
57 package tck.java.time.temporal;
58 
59 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH;
60 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR;
61 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH;
62 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
63 import static java.time.temporal.ChronoField.AMPM_OF_DAY;
64 import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_DAY;
65 import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_AMPM;
66 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
67 import static java.time.temporal.ChronoField.DAY_OF_WEEK;
68 import static java.time.temporal.ChronoField.DAY_OF_YEAR;
69 import static java.time.temporal.ChronoField.EPOCH_DAY;
70 import static java.time.temporal.ChronoField.HOUR_OF_DAY;
71 import static java.time.temporal.ChronoField.HOUR_OF_AMPM;
72 import static java.time.temporal.ChronoField.MICRO_OF_DAY;
73 import static java.time.temporal.ChronoField.MICRO_OF_SECOND;
74 import static java.time.temporal.ChronoField.MILLI_OF_DAY;
75 import static java.time.temporal.ChronoField.MILLI_OF_SECOND;
76 import static java.time.temporal.ChronoField.MINUTE_OF_DAY;
77 import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
78 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
79 import static java.time.temporal.ChronoField.NANO_OF_DAY;
80 import static java.time.temporal.ChronoField.NANO_OF_SECOND;
81 import static java.time.temporal.ChronoField.PROLEPTIC_MONTH;
82 import static java.time.temporal.ChronoField.SECOND_OF_DAY;
83 import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
84 import static java.time.temporal.ChronoField.YEAR;
85 import static java.time.temporal.ChronoField.YEAR_OF_ERA;
86 import static java.time.temporal.ChronoField.ERA;
87 import static java.time.temporal.ChronoUnit.DAYS;
88 import static java.time.temporal.ChronoUnit.FOREVER;
89 import static java.time.temporal.ChronoUnit.HOURS;
90 import static java.time.temporal.ChronoUnit.MICROS;
91 import static java.time.temporal.ChronoUnit.MILLIS;
92 import static java.time.temporal.ChronoUnit.MINUTES;
93 import static java.time.temporal.ChronoUnit.MONTHS;
94 import static java.time.temporal.ChronoUnit.NANOS;
95 import static java.time.temporal.ChronoUnit.SECONDS;
96 import static java.time.temporal.ChronoUnit.WEEKS;
97 import static java.time.temporal.ChronoUnit.YEARS;
98 import static org.testng.Assert.assertEquals;
99 import static org.testng.Assert.assertTrue;
100 import static org.testng.Assert.fail;
101 
102 import java.time.LocalDate;
103 import java.time.LocalDateTime;
104 import java.time.LocalTime;
105 import java.time.temporal.ChronoField;
106 import java.time.temporal.ChronoUnit;
107 import java.time.temporal.TemporalAccessor;
108 import java.time.temporal.ValueRange;
109 
110 import org.testng.annotations.DataProvider;
111 import org.testng.annotations.Test;
112 
113 /**
114  * Test.
115  */
116 @Test
117 public class TCKChronoField {
118 
119     //-----------------------------------------------------------------------
120     // getBaseUnit() and getRangeUnit()
121     //-----------------------------------------------------------------------
122     @DataProvider(name="fieldUnit")
data_fieldUnit()123     Object[][] data_fieldUnit() {
124         return new Object[][] {
125                 {YEAR, YEARS, FOREVER},
126                 {MONTH_OF_YEAR, MONTHS, YEARS},
127                 {DAY_OF_MONTH, DAYS, MONTHS},
128                 {DAY_OF_WEEK, DAYS, WEEKS},
129                 {DAY_OF_YEAR, DAYS, YEARS},
130                 {HOUR_OF_DAY, HOURS, DAYS},
131                 {MINUTE_OF_DAY, MINUTES, DAYS},
132                 {MINUTE_OF_HOUR, MINUTES, HOURS},
133                 {SECOND_OF_DAY, SECONDS, DAYS},
134                 {SECOND_OF_MINUTE, SECONDS, MINUTES},
135                 {MILLI_OF_DAY, MILLIS, DAYS},
136                 {MILLI_OF_SECOND, MILLIS, SECONDS},
137                 {MICRO_OF_SECOND, MICROS, SECONDS},
138                 {MICRO_OF_DAY, MICROS, DAYS},
139                 {NANO_OF_SECOND, NANOS, SECONDS},
140                 {NANO_OF_DAY, NANOS, DAYS},
141 
142         };
143     }
144 
145     @Test(dataProvider = "fieldUnit")
test_getBaseUnit(ChronoField field, ChronoUnit baseUnit, ChronoUnit rangeUnit)146     public void test_getBaseUnit(ChronoField field, ChronoUnit baseUnit, ChronoUnit rangeUnit) {
147         assertEquals(field.getBaseUnit(), baseUnit);
148         assertEquals(field.getRangeUnit(), rangeUnit);
149     }
150 
151     //-----------------------------------------------------------------------
152     // isDateBased() and isTimeBased()
153     //-----------------------------------------------------------------------
154     @DataProvider(name="fieldBased")
data_fieldBased()155     Object[][] data_fieldBased() {
156         return new Object[][] {
157                 {DAY_OF_WEEK, true, false},
158                 {ALIGNED_DAY_OF_WEEK_IN_MONTH, true, false},
159                 {ALIGNED_DAY_OF_WEEK_IN_YEAR, true, false},
160                 {DAY_OF_MONTH, true, false},
161                 {DAY_OF_YEAR, true, false},
162                 {EPOCH_DAY, true, false},
163                 {ALIGNED_WEEK_OF_MONTH, true, false},
164                 {ALIGNED_WEEK_OF_YEAR, true, false},
165                 {MONTH_OF_YEAR, true, false},
166                 {PROLEPTIC_MONTH, true, false},
167                 {YEAR_OF_ERA, true, false},
168                 {YEAR, true, false},
169                 {ERA, true, false},
170 
171                 {AMPM_OF_DAY, false, true},
172                 {CLOCK_HOUR_OF_DAY, false, true},
173                 {HOUR_OF_DAY, false, true},
174                 {CLOCK_HOUR_OF_AMPM, false, true},
175                 {HOUR_OF_AMPM, false, true},
176                 {MINUTE_OF_DAY, false, true},
177                 {MINUTE_OF_HOUR, false, true},
178                 {SECOND_OF_DAY, false, true},
179                 {SECOND_OF_MINUTE, false, true},
180                 {MILLI_OF_DAY, false, true},
181                 {MILLI_OF_SECOND, false, true},
182                 {MICRO_OF_DAY, false, true},
183                 {MICRO_OF_SECOND, false, true},
184                 {NANO_OF_DAY, false, true},
185                 {NANO_OF_SECOND, false, true},
186         };
187     }
188 
189     @Test(dataProvider = "fieldBased")
test_isDateBased(ChronoField field, boolean isDateBased, boolean isTimeBased)190     public void test_isDateBased(ChronoField field, boolean isDateBased, boolean isTimeBased) {
191         assertEquals(field.isDateBased(), isDateBased);
192         assertEquals(field.isTimeBased(), isTimeBased);
193     }
194 
195     //-----------------------------------------------------------------------
196     // isSupportedBy(TemporalAccessor temporal) and getFrom(TemporalAccessor temporal)
197     //-----------------------------------------------------------------------
198     @DataProvider(name="fieldAndAccessor")
data_fieldAndAccessor()199     Object[][] data_fieldAndAccessor() {
200         return new Object[][] {
201                 {YEAR, LocalDate.of(2000, 2, 29), true, 2000},
202                 {YEAR, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 2000},
203                 {MONTH_OF_YEAR, LocalDate.of(2000, 2, 29), true, 2},
204                 {MONTH_OF_YEAR, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 2},
205                 {DAY_OF_MONTH, LocalDate.of(2000, 2, 29), true, 29},
206                 {DAY_OF_MONTH, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 29},
207                 {DAY_OF_YEAR, LocalDate.of(2000, 2, 29), true, 60},
208                 {DAY_OF_YEAR, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 60},
209 
210                 {HOUR_OF_DAY, LocalTime.of(5, 4, 3, 200), true, 5},
211                 {HOUR_OF_DAY, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 5},
212 
213                 {MINUTE_OF_DAY, LocalTime.of(5, 4, 3, 200), true, 5*60 + 4},
214                 {MINUTE_OF_DAY, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 5*60 + 4},
215                 {MINUTE_OF_HOUR, LocalTime.of(5, 4, 3, 200), true, 4},
216                 {MINUTE_OF_HOUR, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 4},
217 
218                 {SECOND_OF_DAY, LocalTime.of(5, 4, 3, 200), true, 5*3600 + 4*60 + 3},
219                 {SECOND_OF_DAY, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 5*3600 + 4*60 + 3},
220                 {SECOND_OF_MINUTE, LocalTime.of(5, 4, 3, 200), true, 3},
221                 {SECOND_OF_MINUTE, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 3},
222 
223                 {NANO_OF_SECOND, LocalTime.of(5, 4, 3, 200), true, 200},
224                 {NANO_OF_SECOND, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 200},
225 
226                 {YEAR, LocalTime.of(5, 4, 3, 200), false, -1},
227                 {MONTH_OF_YEAR, LocalTime.of(5, 4, 3, 200), false, -1},
228                 {DAY_OF_MONTH, LocalTime.of(5, 4, 3, 200), false, -1},
229                 {DAY_OF_YEAR, LocalTime.of(5, 4, 3, 200), false, -1},
230                 {HOUR_OF_DAY, LocalDate.of(2000, 2, 29), false, -1},
231                 {MINUTE_OF_DAY, LocalDate.of(2000, 2, 29), false, -1},
232                 {MINUTE_OF_HOUR, LocalDate.of(2000, 2, 29), false, -1},
233                 {SECOND_OF_DAY, LocalDate.of(2000, 2, 29), false, -1},
234                 {SECOND_OF_MINUTE, LocalDate.of(2000, 2, 29), false, -1},
235                 {NANO_OF_SECOND, LocalDate.of(2000, 2, 29), false, -1},
236         };
237     }
238 
239     @Test(dataProvider = "fieldAndAccessor")
test_supportedAccessor(ChronoField field, TemporalAccessor accessor, boolean isSupported, long value)240     public void test_supportedAccessor(ChronoField field, TemporalAccessor accessor, boolean isSupported, long value) {
241         assertEquals(field.isSupportedBy(accessor), isSupported);
242         if (isSupported) {
243             assertEquals(field.getFrom(accessor), value);
244         }
245     }
246 
247     //-----------------------------------------------------------------------
248     // range() and rangeRefinedBy(TemporalAccessor temporal)
249     //-----------------------------------------------------------------------
250     @Test
test_range()251     public void test_range() {
252         assertEquals(MONTH_OF_YEAR.range(), ValueRange.of(1, 12));
253         assertEquals(MONTH_OF_YEAR.rangeRefinedBy(LocalDate.of(2000, 2, 29)), ValueRange.of(1, 12));
254 
255         assertEquals(DAY_OF_MONTH.range(), ValueRange.of(1, 28, 31));
256         assertEquals(DAY_OF_MONTH.rangeRefinedBy(LocalDate.of(2000, 2, 29)), ValueRange.of(1, 29));
257     }
258 
259     //-----------------------------------------------------------------------
260     // valueOf()
261     //-----------------------------------------------------------------------
262     @Test
test_valueOf()263     public void test_valueOf() {
264         for (ChronoField field : ChronoField.values()) {
265             assertEquals(ChronoField.valueOf(field.name()), field);
266         }
267     }
268 }
269