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) 2007-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 tck.java.time.temporal;
61 
62 import static java.time.DayOfWeek.MONDAY;
63 import static java.time.DayOfWeek.TUESDAY;
64 import static java.time.Month.DECEMBER;
65 import static java.time.Month.JANUARY;
66 import static org.testng.Assert.assertEquals;
67 import static org.testng.Assert.assertFalse;
68 import static org.testng.Assert.assertNotNull;
69 import static org.testng.Assert.assertSame;
70 import static org.testng.Assert.assertTrue;
71 
72 import java.time.DayOfWeek;
73 import java.time.LocalDate;
74 import java.time.Month;
75 import java.time.temporal.TemporalAdjuster;
76 import java.time.temporal.TemporalAdjusters;
77 
78 import org.testng.annotations.DataProvider;
79 import org.testng.annotations.Test;
80 
81 /**
82  * Test TemporalAdjusters.
83  */
84 @Test
85 public class TCKTemporalAdjusters {
86 
87     //-----------------------------------------------------------------------
88     // ofDateAdjuster()
89     //-----------------------------------------------------------------------
90     @Test
factory_ofDateAdjuster()91     public void factory_ofDateAdjuster() {
92         TemporalAdjuster test = TemporalAdjusters.ofDateAdjuster(date -> date.plusDays(2));
93         assertEquals(LocalDate.of(2012, 6, 30).with(test), LocalDate.of(2012, 7, 2));
94     }
95 
96     @Test(expectedExceptions = NullPointerException.class)
factory_ofDateAdjuster_null()97     public void factory_ofDateAdjuster_null() {
98         TemporalAdjusters.ofDateAdjuster(null);
99     }
100 
101 
102     //-----------------------------------------------------------------------
103     // firstDayOfMonth()
104     //-----------------------------------------------------------------------
105     @Test
factory_firstDayOfMonth()106     public void factory_firstDayOfMonth() {
107         assertNotNull(TemporalAdjusters.firstDayOfMonth());
108     }
109 
110     @Test
test_firstDayOfMonth_nonLeap()111     public void test_firstDayOfMonth_nonLeap() {
112         for (Month month : Month.values()) {
113             for (int i = 1; i <= month.length(false); i++) {
114                 LocalDate date = date(2007, month, i);
115                 LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfMonth().adjustInto(date);
116                 assertEquals(test.getYear(), 2007);
117                 assertEquals(test.getMonth(), month);
118                 assertEquals(test.getDayOfMonth(), 1);
119             }
120         }
121     }
122 
123     @Test
test_firstDayOfMonth_leap()124     public void test_firstDayOfMonth_leap() {
125         for (Month month : Month.values()) {
126             for (int i = 1; i <= month.length(true); i++) {
127                 LocalDate date = date(2008, month, i);
128                 LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfMonth().adjustInto(date);
129                 assertEquals(test.getYear(), 2008);
130                 assertEquals(test.getMonth(), month);
131                 assertEquals(test.getDayOfMonth(), 1);
132             }
133         }
134     }
135 
136     //-----------------------------------------------------------------------
137     // lastDayOfMonth()
138     //-----------------------------------------------------------------------
139     @Test
factory_lastDayOfMonth()140     public void factory_lastDayOfMonth() {
141         assertNotNull(TemporalAdjusters.lastDayOfMonth());
142     }
143 
144     @Test
test_lastDayOfMonth_nonLeap()145     public void test_lastDayOfMonth_nonLeap() {
146         for (Month month : Month.values()) {
147             for (int i = 1; i <= month.length(false); i++) {
148                 LocalDate date = date(2007, month, i);
149                 LocalDate test = (LocalDate) TemporalAdjusters.lastDayOfMonth().adjustInto(date);
150                 assertEquals(test.getYear(), 2007);
151                 assertEquals(test.getMonth(), month);
152                 assertEquals(test.getDayOfMonth(), month.length(false));
153             }
154         }
155     }
156 
157     @Test
test_lastDayOfMonth_leap()158     public void test_lastDayOfMonth_leap() {
159         for (Month month : Month.values()) {
160             for (int i = 1; i <= month.length(true); i++) {
161                 LocalDate date = date(2008, month, i);
162                 LocalDate test = (LocalDate) TemporalAdjusters.lastDayOfMonth().adjustInto(date);
163                 assertEquals(test.getYear(), 2008);
164                 assertEquals(test.getMonth(), month);
165                 assertEquals(test.getDayOfMonth(), month.length(true));
166             }
167         }
168     }
169 
170     //-----------------------------------------------------------------------
171     // firstDayOfNextMonth()
172     //-----------------------------------------------------------------------
173     @Test
factory_firstDayOfNextMonth()174     public void factory_firstDayOfNextMonth() {
175         assertNotNull(TemporalAdjusters.firstDayOfNextMonth());
176     }
177 
178     @Test
test_firstDayOfNextMonth_nonLeap()179     public void test_firstDayOfNextMonth_nonLeap() {
180         for (Month month : Month.values()) {
181             for (int i = 1; i <= month.length(false); i++) {
182                 LocalDate date = date(2007, month, i);
183                 LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfNextMonth().adjustInto(date);
184                 assertEquals(test.getYear(), month == DECEMBER ? 2008 : 2007);
185                 assertEquals(test.getMonth(), month.plus(1));
186                 assertEquals(test.getDayOfMonth(), 1);
187             }
188         }
189     }
190 
191     @Test
test_firstDayOfNextMonth_leap()192     public void test_firstDayOfNextMonth_leap() {
193         for (Month month : Month.values()) {
194             for (int i = 1; i <= month.length(true); i++) {
195                 LocalDate date = date(2008, month, i);
196                 LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfNextMonth().adjustInto(date);
197                 assertEquals(test.getYear(), month == DECEMBER ? 2009 : 2008);
198                 assertEquals(test.getMonth(), month.plus(1));
199                 assertEquals(test.getDayOfMonth(), 1);
200             }
201         }
202     }
203 
204     //-----------------------------------------------------------------------
205     // firstDayOfYear()
206     //-----------------------------------------------------------------------
207     @Test
factory_firstDayOfYear()208     public void factory_firstDayOfYear() {
209         assertNotNull(TemporalAdjusters.firstDayOfYear());
210     }
211 
212     @Test
test_firstDayOfYear_nonLeap()213     public void test_firstDayOfYear_nonLeap() {
214         for (Month month : Month.values()) {
215             for (int i = 1; i <= month.length(false); i++) {
216                 LocalDate date = date(2007, month, i);
217                 LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfYear().adjustInto(date);
218                 assertEquals(test.getYear(), 2007);
219                 assertEquals(test.getMonth(), Month.JANUARY);
220                 assertEquals(test.getDayOfMonth(), 1);
221             }
222         }
223     }
224 
225     @Test
test_firstDayOfYear_leap()226     public void test_firstDayOfYear_leap() {
227         for (Month month : Month.values()) {
228             for (int i = 1; i <= month.length(true); i++) {
229                 LocalDate date = date(2008, month, i);
230                 LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfYear().adjustInto(date);
231                 assertEquals(test.getYear(), 2008);
232                 assertEquals(test.getMonth(), Month.JANUARY);
233                 assertEquals(test.getDayOfMonth(), 1);
234             }
235         }
236     }
237 
238     //-----------------------------------------------------------------------
239     // lastDayOfYear()
240     //-----------------------------------------------------------------------
241     @Test
factory_lastDayOfYear()242     public void factory_lastDayOfYear() {
243         assertNotNull(TemporalAdjusters.lastDayOfYear());
244     }
245 
246     @Test
test_lastDayOfYear_nonLeap()247     public void test_lastDayOfYear_nonLeap() {
248         for (Month month : Month.values()) {
249             for (int i = 1; i <= month.length(false); i++) {
250                 LocalDate date = date(2007, month, i);
251                 LocalDate test = (LocalDate) TemporalAdjusters.lastDayOfYear().adjustInto(date);
252                 assertEquals(test.getYear(), 2007);
253                 assertEquals(test.getMonth(), Month.DECEMBER);
254                 assertEquals(test.getDayOfMonth(), 31);
255             }
256         }
257     }
258 
259     @Test
test_lastDayOfYear_leap()260     public void test_lastDayOfYear_leap() {
261         for (Month month : Month.values()) {
262             for (int i = 1; i <= month.length(true); i++) {
263                 LocalDate date = date(2008, month, i);
264                 LocalDate test = (LocalDate) TemporalAdjusters.lastDayOfYear().adjustInto(date);
265                 assertEquals(test.getYear(), 2008);
266                 assertEquals(test.getMonth(), Month.DECEMBER);
267                 assertEquals(test.getDayOfMonth(), 31);
268             }
269         }
270     }
271 
272     //-----------------------------------------------------------------------
273     // firstDayOfNextYear()
274     //-----------------------------------------------------------------------
275     @Test
factory_firstDayOfNextYear()276     public void factory_firstDayOfNextYear() {
277         assertNotNull(TemporalAdjusters.firstDayOfNextYear());
278     }
279 
280     @Test
test_firstDayOfNextYear_nonLeap()281     public void test_firstDayOfNextYear_nonLeap() {
282         for (Month month : Month.values()) {
283             for (int i = 1; i <= month.length(false); i++) {
284                 LocalDate date = date(2007, month, i);
285                 LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfNextYear().adjustInto(date);
286                 assertEquals(test.getYear(), 2008);
287                 assertEquals(test.getMonth(), JANUARY);
288                 assertEquals(test.getDayOfMonth(), 1);
289             }
290         }
291     }
292 
293     @Test
test_firstDayOfNextYear_leap()294     public void test_firstDayOfNextYear_leap() {
295         for (Month month : Month.values()) {
296             for (int i = 1; i <= month.length(true); i++) {
297                 LocalDate date = date(2008, month, i);
298                 LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfNextYear().adjustInto(date);
299                 assertEquals(test.getYear(), 2009);
300                 assertEquals(test.getMonth(), JANUARY);
301                 assertEquals(test.getDayOfMonth(), 1);
302             }
303         }
304     }
305 
306     //-----------------------------------------------------------------------
307     // dayOfWeekInMonth()
308     //-----------------------------------------------------------------------
309     @Test
factory_dayOfWeekInMonth()310     public void factory_dayOfWeekInMonth() {
311         assertNotNull(TemporalAdjusters.dayOfWeekInMonth(1, MONDAY));
312     }
313 
314     @Test(expectedExceptions=NullPointerException.class)
factory_dayOfWeekInMonth_nullDayOfWeek()315     public void factory_dayOfWeekInMonth_nullDayOfWeek() {
316         TemporalAdjusters.dayOfWeekInMonth(1, null);
317     }
318 
319     @DataProvider(name = "dayOfWeekInMonth_positive")
data_dayOfWeekInMonth_positive()320     Object[][] data_dayOfWeekInMonth_positive() {
321         return new Object[][] {
322             {2011, 1, TUESDAY, date(2011, 1, 4)},
323             {2011, 2, TUESDAY, date(2011, 2, 1)},
324             {2011, 3, TUESDAY, date(2011, 3, 1)},
325             {2011, 4, TUESDAY, date(2011, 4, 5)},
326             {2011, 5, TUESDAY, date(2011, 5, 3)},
327             {2011, 6, TUESDAY, date(2011, 6, 7)},
328             {2011, 7, TUESDAY, date(2011, 7, 5)},
329             {2011, 8, TUESDAY, date(2011, 8, 2)},
330             {2011, 9, TUESDAY, date(2011, 9, 6)},
331             {2011, 10, TUESDAY, date(2011, 10, 4)},
332             {2011, 11, TUESDAY, date(2011, 11, 1)},
333             {2011, 12, TUESDAY, date(2011, 12, 6)},
334         };
335     }
336 
337     @Test(dataProvider = "dayOfWeekInMonth_positive")
test_dayOfWeekInMonth_positive(int year, int month, DayOfWeek dow, LocalDate expected)338     public void test_dayOfWeekInMonth_positive(int year, int month, DayOfWeek dow, LocalDate expected) {
339         for (int ordinal = 1; ordinal <= 5; ordinal++) {
340             for (int day = 1; day <= Month.of(month).length(false); day++) {
341                 LocalDate date = date(year, month, day);
342                 LocalDate test = (LocalDate) TemporalAdjusters.dayOfWeekInMonth(ordinal, dow).adjustInto(date);
343                 assertEquals(test, expected.plusWeeks(ordinal - 1));
344             }
345         }
346     }
347 
348     @DataProvider(name = "dayOfWeekInMonth_zero")
data_dayOfWeekInMonth_zero()349     Object[][] data_dayOfWeekInMonth_zero() {
350         return new Object[][] {
351             {2011, 1, TUESDAY, date(2010, 12, 28)},
352             {2011, 2, TUESDAY, date(2011, 1, 25)},
353             {2011, 3, TUESDAY, date(2011, 2, 22)},
354             {2011, 4, TUESDAY, date(2011, 3, 29)},
355             {2011, 5, TUESDAY, date(2011, 4, 26)},
356             {2011, 6, TUESDAY, date(2011, 5, 31)},
357             {2011, 7, TUESDAY, date(2011, 6, 28)},
358             {2011, 8, TUESDAY, date(2011, 7, 26)},
359             {2011, 9, TUESDAY, date(2011, 8, 30)},
360             {2011, 10, TUESDAY, date(2011, 9, 27)},
361             {2011, 11, TUESDAY, date(2011, 10, 25)},
362             {2011, 12, TUESDAY, date(2011, 11, 29)},
363         };
364     }
365 
366     @Test(dataProvider = "dayOfWeekInMonth_zero")
test_dayOfWeekInMonth_zero(int year, int month, DayOfWeek dow, LocalDate expected)367     public void test_dayOfWeekInMonth_zero(int year, int month, DayOfWeek dow, LocalDate expected) {
368         for (int day = 1; day <= Month.of(month).length(false); day++) {
369             LocalDate date = date(year, month, day);
370             LocalDate test = (LocalDate) TemporalAdjusters.dayOfWeekInMonth(0, dow).adjustInto(date);
371             assertEquals(test, expected);
372         }
373     }
374 
375     @DataProvider(name = "dayOfWeekInMonth_negative")
data_dayOfWeekInMonth_negative()376     Object[][] data_dayOfWeekInMonth_negative() {
377         return new Object[][] {
378             {2011, 1, TUESDAY, date(2011, 1, 25)},
379             {2011, 2, TUESDAY, date(2011, 2, 22)},
380             {2011, 3, TUESDAY, date(2011, 3, 29)},
381             {2011, 4, TUESDAY, date(2011, 4, 26)},
382             {2011, 5, TUESDAY, date(2011, 5, 31)},
383             {2011, 6, TUESDAY, date(2011, 6, 28)},
384             {2011, 7, TUESDAY, date(2011, 7, 26)},
385             {2011, 8, TUESDAY, date(2011, 8, 30)},
386             {2011, 9, TUESDAY, date(2011, 9, 27)},
387             {2011, 10, TUESDAY, date(2011, 10, 25)},
388             {2011, 11, TUESDAY, date(2011, 11, 29)},
389             {2011, 12, TUESDAY, date(2011, 12, 27)},
390         };
391     }
392 
393     @Test(dataProvider = "dayOfWeekInMonth_negative")
test_dayOfWeekInMonth_negative(int year, int month, DayOfWeek dow, LocalDate expected)394     public void test_dayOfWeekInMonth_negative(int year, int month, DayOfWeek dow, LocalDate expected) {
395         for (int ordinal = 0; ordinal < 5; ordinal++) {
396             for (int day = 1; day <= Month.of(month).length(false); day++) {
397                 LocalDate date = date(year, month, day);
398                 LocalDate test = (LocalDate) TemporalAdjusters.dayOfWeekInMonth(-1 - ordinal, dow).adjustInto(date);
399                 assertEquals(test, expected.minusWeeks(ordinal));
400             }
401         }
402     }
403 
404     //-----------------------------------------------------------------------
405     // firstInMonth()
406     //-----------------------------------------------------------------------
407     @Test
factory_firstInMonth()408     public void factory_firstInMonth() {
409         assertNotNull(TemporalAdjusters.firstInMonth(MONDAY));
410     }
411 
412     @Test(expectedExceptions=NullPointerException.class)
factory_firstInMonth_nullDayOfWeek()413     public void factory_firstInMonth_nullDayOfWeek() {
414         TemporalAdjusters.firstInMonth(null);
415     }
416 
417     @Test(dataProvider = "dayOfWeekInMonth_positive")
test_firstInMonth(int year, int month, DayOfWeek dow, LocalDate expected)418     public void test_firstInMonth(int year, int month, DayOfWeek dow, LocalDate expected) {
419         for (int day = 1; day <= Month.of(month).length(false); day++) {
420             LocalDate date = date(year, month, day);
421             LocalDate test = (LocalDate) TemporalAdjusters.firstInMonth(dow).adjustInto(date);
422             assertEquals(test, expected, "day-of-month=" + day);
423         }
424     }
425 
426     //-----------------------------------------------------------------------
427     // lastInMonth()
428     //-----------------------------------------------------------------------
429     @Test
factory_lastInMonth()430     public void factory_lastInMonth() {
431         assertNotNull(TemporalAdjusters.lastInMonth(MONDAY));
432     }
433 
434     @Test(expectedExceptions=NullPointerException.class)
factory_lastInMonth_nullDayOfWeek()435     public void factory_lastInMonth_nullDayOfWeek() {
436         TemporalAdjusters.lastInMonth(null);
437     }
438 
439     @Test(dataProvider = "dayOfWeekInMonth_negative")
test_lastInMonth(int year, int month, DayOfWeek dow, LocalDate expected)440     public void test_lastInMonth(int year, int month, DayOfWeek dow, LocalDate expected) {
441         for (int day = 1; day <= Month.of(month).length(false); day++) {
442             LocalDate date = date(year, month, day);
443             LocalDate test = (LocalDate) TemporalAdjusters.lastInMonth(dow).adjustInto(date);
444             assertEquals(test, expected, "day-of-month=" + day);
445         }
446     }
447 
448     //-----------------------------------------------------------------------
449     // next()
450     //-----------------------------------------------------------------------
451     @Test
factory_next()452     public void factory_next() {
453         assertNotNull(TemporalAdjusters.next(MONDAY));
454     }
455 
456     @Test(expectedExceptions = NullPointerException.class)
factory_next_nullDayOfWeek()457     public void factory_next_nullDayOfWeek() {
458         TemporalAdjusters.next(null);
459     }
460 
461     @Test
test_next()462     public void test_next() {
463         for (Month month : Month.values()) {
464             for (int i = 1; i <= month.length(false); i++) {
465                 LocalDate date = date(2007, month, i);
466 
467                 for (DayOfWeek dow : DayOfWeek.values()) {
468                     LocalDate test = (LocalDate) TemporalAdjusters.next(dow).adjustInto(date);
469 
470                     assertSame(test.getDayOfWeek(), dow, date + " " + test);
471 
472                     if (test.getYear() == 2007) {
473                         int dayDiff = test.getDayOfYear() - date.getDayOfYear();
474                         assertTrue(dayDiff > 0 && dayDiff < 8);
475                     } else {
476                         assertSame(month, Month.DECEMBER);
477                         assertTrue(date.getDayOfMonth() > 24);
478                         assertEquals(test.getYear(), 2008);
479                         assertSame(test.getMonth(), Month.JANUARY);
480                         assertTrue(test.getDayOfMonth() < 8);
481                     }
482                 }
483             }
484         }
485     }
486 
487     //-----------------------------------------------------------------------
488     // nextOrSame()
489     //-----------------------------------------------------------------------
490     @Test
491     public void factory_nextOrCurrent() {
492         assertNotNull(TemporalAdjusters.nextOrSame(MONDAY));
493     }
494 
495     @Test(expectedExceptions = NullPointerException.class)
496     public void factory_nextOrCurrent_nullDayOfWeek() {
497         TemporalAdjusters.nextOrSame(null);
498     }
499 
500     @Test
501     public void test_nextOrCurrent() {
502         for (Month month : Month.values()) {
503             for (int i = 1; i <= month.length(false); i++) {
504                 LocalDate date = date(2007, month, i);
505 
506                 for (DayOfWeek dow : DayOfWeek.values()) {
507                     LocalDate test = (LocalDate) TemporalAdjusters.nextOrSame(dow).adjustInto(date);
508 
509                     assertSame(test.getDayOfWeek(), dow);
510 
511                     if (test.getYear() == 2007) {
512                         int dayDiff = test.getDayOfYear() - date.getDayOfYear();
513                         assertTrue(dayDiff < 8);
514                         assertEquals(date.equals(test), date.getDayOfWeek() == dow);
515                     } else {
516                         assertFalse(date.getDayOfWeek() == dow);
517                         assertSame(month, Month.DECEMBER);
518                         assertTrue(date.getDayOfMonth() > 24);
519                         assertEquals(test.getYear(), 2008);
520                         assertSame(test.getMonth(), Month.JANUARY);
521                         assertTrue(test.getDayOfMonth() < 8);
522                     }
523                 }
524             }
525         }
526     }
527 
528     //-----------------------------------------------------------------------
529     // previous()
530     //-----------------------------------------------------------------------
531     @Test
532     public void factory_previous() {
533         assertNotNull(TemporalAdjusters.previous(MONDAY));
534     }
535 
536     @Test(expectedExceptions = NullPointerException.class)
537     public void factory_previous_nullDayOfWeek() {
538         TemporalAdjusters.previous(null);
539     }
540 
541     @Test
542     public void test_previous() {
543         for (Month month : Month.values()) {
544             for (int i = 1; i <= month.length(false); i++) {
545                 LocalDate date = date(2007, month, i);
546 
547                 for (DayOfWeek dow : DayOfWeek.values()) {
548                     LocalDate test = (LocalDate) TemporalAdjusters.previous(dow).adjustInto(date);
549 
550                     assertSame(test.getDayOfWeek(), dow, date + " " + test);
551 
552                     if (test.getYear() == 2007) {
553                         int dayDiff = test.getDayOfYear() - date.getDayOfYear();
554                         assertTrue(dayDiff < 0 && dayDiff > -8, dayDiff + " " + test);
555                     } else {
556                         assertSame(month, Month.JANUARY);
557                         assertTrue(date.getDayOfMonth() < 8);
558                         assertEquals(test.getYear(), 2006);
559                         assertSame(test.getMonth(), Month.DECEMBER);
560                         assertTrue(test.getDayOfMonth() > 24);
561                     }
562                 }
563             }
564         }
565     }
566 
567     //-----------------------------------------------------------------------
568     // previousOrSame()
569     //-----------------------------------------------------------------------
570     @Test
571     public void factory_previousOrCurrent() {
572         assertNotNull(TemporalAdjusters.previousOrSame(MONDAY));
573     }
574 
575     @Test(expectedExceptions = NullPointerException.class)
576     public void factory_previousOrCurrent_nullDayOfWeek() {
577         TemporalAdjusters.previousOrSame(null);
578     }
579 
580     @Test
581     public void test_previousOrCurrent() {
582         for (Month month : Month.values()) {
583             for (int i = 1; i <= month.length(false); i++) {
584                 LocalDate date = date(2007, month, i);
585 
586                 for (DayOfWeek dow : DayOfWeek.values()) {
587                     LocalDate test = (LocalDate) TemporalAdjusters.previousOrSame(dow).adjustInto(date);
588 
589                     assertSame(test.getDayOfWeek(), dow);
590 
591                     if (test.getYear() == 2007) {
592                         int dayDiff = test.getDayOfYear() - date.getDayOfYear();
593                         assertTrue(dayDiff <= 0 && dayDiff > -7);
594                         assertEquals(date.equals(test), date.getDayOfWeek() == dow);
595                     } else {
596                         assertFalse(date.getDayOfWeek() == dow);
597                         assertSame(month, Month.JANUARY);
598                         assertTrue(date.getDayOfMonth() < 7);
599                         assertEquals(test.getYear(), 2006);
600                         assertSame(test.getMonth(), Month.DECEMBER);
601                         assertTrue(test.getDayOfMonth() > 25);
602                     }
603                 }
604             }
605         }
606     }
607 
608     private LocalDate date(int year, Month month, int day) {
609         return LocalDate.of(year, month, day);
610     }
611 
612     private LocalDate date(int year, int month, int day) {
613         return LocalDate.of(year, month, day);
614     }
615 
616 }
617