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