• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  *  * Neither the name of JSR-310 nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.threeten.bp;
33 
34 import static org.testng.Assert.assertEquals;
35 import static org.threeten.bp.Month.DECEMBER;
36 import static org.threeten.bp.Month.JANUARY;
37 import static org.threeten.bp.Month.JUNE;
38 import static org.threeten.bp.temporal.ChronoField.MONTH_OF_YEAR;
39 
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.List;
43 import java.util.Locale;
44 
45 import org.testng.annotations.DataProvider;
46 import org.testng.annotations.Test;
47 import org.threeten.bp.chrono.IsoChronology;
48 import org.threeten.bp.format.TextStyle;
49 import org.threeten.bp.temporal.ChronoField;
50 import org.threeten.bp.temporal.ChronoUnit;
51 import org.threeten.bp.temporal.JulianFields;
52 import org.threeten.bp.temporal.TemporalAccessor;
53 import org.threeten.bp.temporal.TemporalField;
54 import org.threeten.bp.temporal.TemporalQueries;
55 
56 /**
57  * Test Month.
58  */
59 @Test
60 public class TestMonth extends AbstractDateTimeTest {
61 
62     private static final int MAX_LENGTH = 12;
63 
64     //-----------------------------------------------------------------------
65     @Override
samples()66     protected List<TemporalAccessor> samples() {
67         TemporalAccessor[] array = {JANUARY, JUNE, DECEMBER, };
68         return Arrays.asList(array);
69     }
70 
71     @Override
validFields()72     protected List<TemporalField> validFields() {
73         TemporalField[] array = {
74             MONTH_OF_YEAR,
75         };
76         return Arrays.asList(array);
77     }
78 
79     @Override
invalidFields()80     protected List<TemporalField> invalidFields() {
81         List<TemporalField> list = new ArrayList<TemporalField>(Arrays.<TemporalField>asList(ChronoField.values()));
82         list.removeAll(validFields());
83         list.add(JulianFields.JULIAN_DAY);
84         list.add(JulianFields.MODIFIED_JULIAN_DAY);
85         list.add(JulianFields.RATA_DIE);
86         return list;
87     }
88 
89     //-----------------------------------------------------------------------
90     @Test
test_factory_int_singleton()91     public void test_factory_int_singleton() {
92         for (int i = 1; i <= MAX_LENGTH; i++) {
93             Month test = Month.of(i);
94             assertEquals(test.getValue(), i);
95         }
96     }
97 
98     @Test(expectedExceptions=DateTimeException.class)
test_factory_int_tooLow()99     public void test_factory_int_tooLow() {
100         Month.of(0);
101     }
102 
103     @Test(expectedExceptions=DateTimeException.class)
test_factory_int_tooHigh()104     public void test_factory_int_tooHigh() {
105         Month.of(13);
106     }
107 
108     //-----------------------------------------------------------------------
109     @Test
test_factory_CalendricalObject()110     public void test_factory_CalendricalObject() {
111         assertEquals(Month.from(LocalDate.of(2011, 6, 6)), JUNE);
112     }
113 
114     @Test(expectedExceptions=DateTimeException.class)
test_factory_CalendricalObject_invalid_noDerive()115     public void test_factory_CalendricalObject_invalid_noDerive() {
116         Month.from(LocalTime.of(12, 30));
117     }
118 
119     @Test(expectedExceptions=NullPointerException.class)
test_factory_CalendricalObject_null()120     public void test_factory_CalendricalObject_null() {
121         Month.from((TemporalAccessor) null);
122     }
123 
124     //-----------------------------------------------------------------------
125     // get(TemporalField)
126     //-----------------------------------------------------------------------
127     @Test
test_get_TemporalField()128     public void test_get_TemporalField() {
129         assertEquals(Month.JULY.get(ChronoField.MONTH_OF_YEAR), 7);
130     }
131 
132     @Test
test_getLong_TemporalField()133     public void test_getLong_TemporalField() {
134         assertEquals(Month.JULY.getLong(ChronoField.MONTH_OF_YEAR), 7);
135     }
136 
137     //-----------------------------------------------------------------------
138     // query(TemporalQuery)
139     //-----------------------------------------------------------------------
140     @Test
test_query()141     public void test_query() {
142         assertEquals(Month.JUNE.query(TemporalQueries.chronology()), IsoChronology.INSTANCE);
143         assertEquals(Month.JUNE.query(TemporalQueries.localDate()), null);
144         assertEquals(Month.JUNE.query(TemporalQueries.localTime()), null);
145         assertEquals(Month.JUNE.query(TemporalQueries.offset()), null);
146         assertEquals(Month.JUNE.query(TemporalQueries.precision()), ChronoUnit.MONTHS);
147         assertEquals(Month.JUNE.query(TemporalQueries.zone()), null);
148         assertEquals(Month.JUNE.query(TemporalQueries.zoneId()), null);
149     }
150 
151     @Test(expectedExceptions=NullPointerException.class)
test_query_null()152     public void test_query_null() {
153         Month.JUNE.query(null);
154     }
155 
156     //-----------------------------------------------------------------------
157     // getDisplayName()
158     //-----------------------------------------------------------------------
159     @Test
test_getDisplayName()160     public void test_getDisplayName() {
161         assertEquals(Month.JANUARY.getDisplayName(TextStyle.SHORT, Locale.US), "Jan");
162     }
163 
164     @Test(expectedExceptions = NullPointerException.class)
test_getDisplayName_nullStyle()165     public void test_getDisplayName_nullStyle() {
166         Month.JANUARY.getDisplayName(null, Locale.US);
167     }
168 
169     @Test(expectedExceptions = NullPointerException.class)
test_getDisplayName_nullLocale()170     public void test_getDisplayName_nullLocale() {
171         Month.JANUARY.getDisplayName(TextStyle.FULL, null);
172     }
173 
174     //-----------------------------------------------------------------------
175     // plus(long), plus(long,unit)
176     //-----------------------------------------------------------------------
177     @DataProvider(name="plus")
data_plus()178     Object[][] data_plus() {
179         return new Object[][] {
180             {1, -13, 12},
181             {1, -12, 1},
182             {1, -11, 2},
183             {1, -10, 3},
184             {1, -9, 4},
185             {1, -8, 5},
186             {1, -7, 6},
187             {1, -6, 7},
188             {1, -5, 8},
189             {1, -4, 9},
190             {1, -3, 10},
191             {1, -2, 11},
192             {1, -1, 12},
193             {1, 0, 1},
194             {1, 1, 2},
195             {1, 2, 3},
196             {1, 3, 4},
197             {1, 4, 5},
198             {1, 5, 6},
199             {1, 6, 7},
200             {1, 7, 8},
201             {1, 8, 9},
202             {1, 9, 10},
203             {1, 10, 11},
204             {1, 11, 12},
205             {1, 12, 1},
206             {1, 13, 2},
207 
208             {1, 1, 2},
209             {2, 1, 3},
210             {3, 1, 4},
211             {4, 1, 5},
212             {5, 1, 6},
213             {6, 1, 7},
214             {7, 1, 8},
215             {8, 1, 9},
216             {9, 1, 10},
217             {10, 1, 11},
218             {11, 1, 12},
219             {12, 1, 1},
220 
221             {1, -1, 12},
222             {2, -1, 1},
223             {3, -1, 2},
224             {4, -1, 3},
225             {5, -1, 4},
226             {6, -1, 5},
227             {7, -1, 6},
228             {8, -1, 7},
229             {9, -1, 8},
230             {10, -1, 9},
231             {11, -1, 10},
232             {12, -1, 11},
233         };
234     }
235 
236     @Test(dataProvider="plus")
test_plus_long(int base, long amount, int expected)237     public void test_plus_long(int base, long amount, int expected) {
238         assertEquals(Month.of(base).plus(amount), Month.of(expected));
239     }
240 
241     //-----------------------------------------------------------------------
242     // minus(long), minus(long,unit)
243     //-----------------------------------------------------------------------
244     @DataProvider(name="minus")
data_minus()245     Object[][] data_minus() {
246         return new Object[][] {
247             {1, -13, 2},
248             {1, -12, 1},
249             {1, -11, 12},
250             {1, -10, 11},
251             {1, -9, 10},
252             {1, -8, 9},
253             {1, -7, 8},
254             {1, -6, 7},
255             {1, -5, 6},
256             {1, -4, 5},
257             {1, -3, 4},
258             {1, -2, 3},
259             {1, -1, 2},
260             {1, 0, 1},
261             {1, 1, 12},
262             {1, 2, 11},
263             {1, 3, 10},
264             {1, 4, 9},
265             {1, 5, 8},
266             {1, 6, 7},
267             {1, 7, 6},
268             {1, 8, 5},
269             {1, 9, 4},
270             {1, 10, 3},
271             {1, 11, 2},
272             {1, 12, 1},
273             {1, 13, 12},
274         };
275     }
276 
277     @Test(dataProvider="minus")
test_minus_long(int base, long amount, int expected)278     public void test_minus_long(int base, long amount, int expected) {
279         assertEquals(Month.of(base).minus(amount), Month.of(expected));
280     }
281 
282     //-----------------------------------------------------------------------
283     // length(boolean)
284     //-----------------------------------------------------------------------
285     @Test
test_length_boolean_notLeapYear()286     public void test_length_boolean_notLeapYear() {
287         assertEquals(Month.JANUARY.length(false), 31);
288         assertEquals(Month.FEBRUARY.length(false), 28);
289         assertEquals(Month.MARCH.length(false), 31);
290         assertEquals(Month.APRIL.length(false), 30);
291         assertEquals(Month.MAY.length(false), 31);
292         assertEquals(Month.JUNE.length(false), 30);
293         assertEquals(Month.JULY.length(false), 31);
294         assertEquals(Month.AUGUST.length(false), 31);
295         assertEquals(Month.SEPTEMBER.length(false), 30);
296         assertEquals(Month.OCTOBER.length(false), 31);
297         assertEquals(Month.NOVEMBER.length(false), 30);
298         assertEquals(Month.DECEMBER.length(false), 31);
299     }
300 
301     @Test
test_length_boolean_leapYear()302     public void test_length_boolean_leapYear() {
303         assertEquals(Month.JANUARY.length(true), 31);
304         assertEquals(Month.FEBRUARY.length(true), 29);
305         assertEquals(Month.MARCH.length(true), 31);
306         assertEquals(Month.APRIL.length(true), 30);
307         assertEquals(Month.MAY.length(true), 31);
308         assertEquals(Month.JUNE.length(true), 30);
309         assertEquals(Month.JULY.length(true), 31);
310         assertEquals(Month.AUGUST.length(true), 31);
311         assertEquals(Month.SEPTEMBER.length(true), 30);
312         assertEquals(Month.OCTOBER.length(true), 31);
313         assertEquals(Month.NOVEMBER.length(true), 30);
314         assertEquals(Month.DECEMBER.length(true), 31);
315     }
316 
317     //-----------------------------------------------------------------------
318     // minLength()
319     //-----------------------------------------------------------------------
320     @Test
test_minLength()321     public void test_minLength() {
322         assertEquals(Month.JANUARY.minLength(), 31);
323         assertEquals(Month.FEBRUARY.minLength(), 28);
324         assertEquals(Month.MARCH.minLength(), 31);
325         assertEquals(Month.APRIL.minLength(), 30);
326         assertEquals(Month.MAY.minLength(), 31);
327         assertEquals(Month.JUNE.minLength(), 30);
328         assertEquals(Month.JULY.minLength(), 31);
329         assertEquals(Month.AUGUST.minLength(), 31);
330         assertEquals(Month.SEPTEMBER.minLength(), 30);
331         assertEquals(Month.OCTOBER.minLength(), 31);
332         assertEquals(Month.NOVEMBER.minLength(), 30);
333         assertEquals(Month.DECEMBER.minLength(), 31);
334     }
335 
336     //-----------------------------------------------------------------------
337     // maxLength()
338     //-----------------------------------------------------------------------
339     @Test
test_maxLength()340     public void test_maxLength() {
341         assertEquals(Month.JANUARY.maxLength(), 31);
342         assertEquals(Month.FEBRUARY.maxLength(), 29);
343         assertEquals(Month.MARCH.maxLength(), 31);
344         assertEquals(Month.APRIL.maxLength(), 30);
345         assertEquals(Month.MAY.maxLength(), 31);
346         assertEquals(Month.JUNE.maxLength(), 30);
347         assertEquals(Month.JULY.maxLength(), 31);
348         assertEquals(Month.AUGUST.maxLength(), 31);
349         assertEquals(Month.SEPTEMBER.maxLength(), 30);
350         assertEquals(Month.OCTOBER.maxLength(), 31);
351         assertEquals(Month.NOVEMBER.maxLength(), 30);
352         assertEquals(Month.DECEMBER.maxLength(), 31);
353     }
354 
355     //-----------------------------------------------------------------------
356     // firstDayOfYear(boolean)
357     //-----------------------------------------------------------------------
358     @Test
test_firstDayOfYear_notLeapYear()359     public void test_firstDayOfYear_notLeapYear() {
360         assertEquals(Month.JANUARY.firstDayOfYear(false), 1);
361         assertEquals(Month.FEBRUARY.firstDayOfYear(false), 1 + 31);
362         assertEquals(Month.MARCH.firstDayOfYear(false), 1 + 31 + 28);
363         assertEquals(Month.APRIL.firstDayOfYear(false), 1 + 31 + 28 + 31);
364         assertEquals(Month.MAY.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30);
365         assertEquals(Month.JUNE.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31);
366         assertEquals(Month.JULY.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30);
367         assertEquals(Month.AUGUST.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31);
368         assertEquals(Month.SEPTEMBER.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31);
369         assertEquals(Month.OCTOBER.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30);
370         assertEquals(Month.NOVEMBER.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31);
371         assertEquals(Month.DECEMBER.firstDayOfYear(false), 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30);
372     }
373 
374     @Test
test_firstDayOfYear_leapYear()375     public void test_firstDayOfYear_leapYear() {
376         assertEquals(Month.JANUARY.firstDayOfYear(true), 1);
377         assertEquals(Month.FEBRUARY.firstDayOfYear(true), 1 + 31);
378         assertEquals(Month.MARCH.firstDayOfYear(true), 1 + 31 + 29);
379         assertEquals(Month.APRIL.firstDayOfYear(true), 1 + 31 + 29 + 31);
380         assertEquals(Month.MAY.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30);
381         assertEquals(Month.JUNE.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31);
382         assertEquals(Month.JULY.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30);
383         assertEquals(Month.AUGUST.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31);
384         assertEquals(Month.SEPTEMBER.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31);
385         assertEquals(Month.OCTOBER.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30);
386         assertEquals(Month.NOVEMBER.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31);
387         assertEquals(Month.DECEMBER.firstDayOfYear(true), 1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30);
388     }
389 
390     //-----------------------------------------------------------------------
391     // firstMonthOfQuarter()
392     //-----------------------------------------------------------------------
393     @Test
test_firstMonthOfQuarter()394     public void test_firstMonthOfQuarter() {
395         assertEquals(Month.JANUARY.firstMonthOfQuarter(), Month.JANUARY);
396         assertEquals(Month.FEBRUARY.firstMonthOfQuarter(), Month.JANUARY);
397         assertEquals(Month.MARCH.firstMonthOfQuarter(), Month.JANUARY);
398         assertEquals(Month.APRIL.firstMonthOfQuarter(), Month.APRIL);
399         assertEquals(Month.MAY.firstMonthOfQuarter(), Month.APRIL);
400         assertEquals(Month.JUNE.firstMonthOfQuarter(), Month.APRIL);
401         assertEquals(Month.JULY.firstMonthOfQuarter(), Month.JULY);
402         assertEquals(Month.AUGUST.firstMonthOfQuarter(), Month.JULY);
403         assertEquals(Month.SEPTEMBER.firstMonthOfQuarter(), Month.JULY);
404         assertEquals(Month.OCTOBER.firstMonthOfQuarter(), Month.OCTOBER);
405         assertEquals(Month.NOVEMBER.firstMonthOfQuarter(), Month.OCTOBER);
406         assertEquals(Month.DECEMBER.firstMonthOfQuarter(), Month.OCTOBER);
407     }
408 
409     //-----------------------------------------------------------------------
410     // toString()
411     //-----------------------------------------------------------------------
412     @Test
test_toString()413     public void test_toString() {
414         assertEquals(Month.JANUARY.toString(), "JANUARY");
415         assertEquals(Month.FEBRUARY.toString(), "FEBRUARY");
416         assertEquals(Month.MARCH.toString(), "MARCH");
417         assertEquals(Month.APRIL.toString(), "APRIL");
418         assertEquals(Month.MAY.toString(), "MAY");
419         assertEquals(Month.JUNE.toString(), "JUNE");
420         assertEquals(Month.JULY.toString(), "JULY");
421         assertEquals(Month.AUGUST.toString(), "AUGUST");
422         assertEquals(Month.SEPTEMBER.toString(), "SEPTEMBER");
423         assertEquals(Month.OCTOBER.toString(), "OCTOBER");
424         assertEquals(Month.NOVEMBER.toString(), "NOVEMBER");
425         assertEquals(Month.DECEMBER.toString(), "DECEMBER");
426     }
427 
428     //-----------------------------------------------------------------------
429     // generated methods
430     //-----------------------------------------------------------------------
431     @Test
test_enum()432     public void test_enum() {
433         assertEquals(Month.valueOf("JANUARY"), Month.JANUARY);
434         assertEquals(Month.values()[0], Month.JANUARY);
435     }
436 
437 }
438