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 test.java.time.chrono;
58 
59 import static org.testng.Assert.assertEquals;
60 import static org.testng.Assert.assertTrue;
61 
62 import java.time.LocalDate;
63 import java.time.LocalDateTime;
64 import java.time.chrono.ChronoLocalDate;
65 import java.time.chrono.ChronoLocalDateTime;
66 import java.time.chrono.Chronology;
67 import java.time.chrono.ThaiBuddhistChronology;
68 import java.time.chrono.ThaiBuddhistDate;
69 import java.time.temporal.ChronoUnit;
70 import java.util.ArrayList;
71 import java.util.Collections;
72 import java.util.List;
73 
74 import org.testng.annotations.Test;
75 
76 /**
77  * Test chrono local date.
78  */
79 @Test
80 public class TestChronoLocalDate {
81     // this class primarily tests whether the generics work OK
82 
83     //-----------------------------------------------------------------------
test_date_comparator_checkGenerics_ISO()84     public void test_date_comparator_checkGenerics_ISO() {
85         List<ChronoLocalDate> dates = new ArrayList<>();
86         ChronoLocalDate date = LocalDate.of(2013, 1, 1);
87 
88         // Insert dates in order, no duplicates
89         dates.add(date.minus(10, ChronoUnit.YEARS));
90         dates.add(date.minus(1, ChronoUnit.YEARS));
91         dates.add(date.minus(1, ChronoUnit.MONTHS));
92         dates.add(date.minus(1, ChronoUnit.WEEKS));
93         dates.add(date.minus(1, ChronoUnit.DAYS));
94         dates.add(date);
95         dates.add(date.plus(1, ChronoUnit.DAYS));
96         dates.add(date.plus(1, ChronoUnit.WEEKS));
97         dates.add(date.plus(1, ChronoUnit.MONTHS));
98         dates.add(date.plus(1, ChronoUnit.YEARS));
99         dates.add(date.plus(10, ChronoUnit.YEARS));
100 
101         List<ChronoLocalDate> copy = new ArrayList<>(dates);
102         Collections.shuffle(copy);
103         Collections.sort(copy, ChronoLocalDate.timeLineOrder());
104         assertEquals(copy, dates);
105         assertTrue(ChronoLocalDate.timeLineOrder().compare(copy.get(0), copy.get(1)) < 0);
106     }
107 
108     public void test_date_comparator_checkGenerics_LocalDate() {
109         List<LocalDate> dates = new ArrayList<>();
110         LocalDate date = LocalDate.of(2013, 1, 1);
111 
112         // Insert dates in order, no duplicates
113         dates.add(date.minus(10, ChronoUnit.YEARS));
114         dates.add(date.minus(1, ChronoUnit.YEARS));
115         dates.add(date.minus(1, ChronoUnit.MONTHS));
116         dates.add(date.minus(1, ChronoUnit.WEEKS));
117         dates.add(date.minus(1, ChronoUnit.DAYS));
118         dates.add(date);
119         dates.add(date.plus(1, ChronoUnit.DAYS));
120         dates.add(date.plus(1, ChronoUnit.WEEKS));
121         dates.add(date.plus(1, ChronoUnit.MONTHS));
122         dates.add(date.plus(1, ChronoUnit.YEARS));
123         dates.add(date.plus(10, ChronoUnit.YEARS));
124 
125         List<LocalDate> copy = new ArrayList<>(dates);
126         Collections.shuffle(copy);
127         Collections.sort(copy, ChronoLocalDate.timeLineOrder());
128         assertEquals(copy, dates);
129         assertTrue(ChronoLocalDate.timeLineOrder().compare(copy.get(0), copy.get(1)) < 0);
130     }
131 
132     //-----------------------------------------------------------------------
133     public void test_date_checkGenerics_genericsMethod() {
134         Chronology chrono = ThaiBuddhistChronology.INSTANCE;
135         ChronoLocalDate date = chrono.dateNow();
136         date = processOK(date);
137         date = processClassOK(ThaiBuddhistDate.class);
138         date = dateSupplier();
139 
140         date = processClassWeird(ThaiBuddhistDate.class);
141     }
142 
143     public void test_date_checkGenerics_genericsMethod_concreteType() {
144         ThaiBuddhistChronology chrono = ThaiBuddhistChronology.INSTANCE;
145         ThaiBuddhistDate date = chrono.dateNow();
146         date = ThaiBuddhistDate.now();
147         date = processOK(date);
148         date = processClassOK(ThaiBuddhistDate.class);
149         date = dateSupplier();
150 
151         // date = processClassWeird(ThaiBuddhistDate.class);  // does not compile (correct)
152     }
153 
154     public <D extends ChronoLocalDate> void test_date_checkGenerics_genericsMethod_withType() {
155         Chronology chrono = ThaiBuddhistChronology.INSTANCE;
156         @SuppressWarnings("unchecked")
157         D date = (D) chrono.dateNow();
158         date = processOK(date);
159         // date = processClassOK(ThaiBuddhistDate.class);  // does not compile (correct)
160         date = dateSupplier();
161 
162         // date = processWeird(date);  // does not compile (correct)
163         // date = processClassWeird(ThaiBuddhistDate.class);  // does not compile (correct)
164     }
165 
166     @SuppressWarnings("unchecked")
167     private <D extends ChronoLocalDate> D dateSupplier() {
168         return (D) ThaiBuddhistChronology.INSTANCE.dateNow();
169     }
170 
171     // decent generics signatures that need to work
172     @SuppressWarnings("unchecked")
173     private <D extends ChronoLocalDate> D processOK(D date) {
174         return (D) date.plus(1, ChronoUnit.DAYS);
175     }
176     private <D extends ChronoLocalDate> D processClassOK(Class<D> cls) {
177         return null;
178     }
179 
180     // weird generics signatures that shouldn't really work
181     private <D extends ChronoLocalDate> ChronoLocalDate processClassWeird(Class<D> cls) {
182         return null;
183     }
184 
185     public void test_date_checkGenerics_chronoLocalDateTime1() {
186         LocalDateTime now = LocalDateTime.now();
187         Chronology chrono = ThaiBuddhistChronology.INSTANCE;
188         ChronoLocalDateTime<?> ldt = chrono.localDateTime(now);
189         ldt = processCLDT(ldt);
190     }
191 
192     public void test_date_checkGenerics_chronoLocalDateTime2() {
193         LocalDateTime now = LocalDateTime.now();
194         Chronology chrono = ThaiBuddhistChronology.INSTANCE;
195         ChronoLocalDateTime<? extends ChronoLocalDate> ldt = chrono.localDateTime(now);
196         ldt = processCLDT(ldt);
197     }
198 
199     private <D extends ChronoLocalDate> ChronoLocalDateTime<D> processCLDT(ChronoLocalDateTime<D> dt) {
200         return dt;
201     }
202 }
203