1 /* 2 * Copyright (c) 2012, 2020, 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) 2009-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 test.java.time.temporal; 61 62 import static org.testng.Assert.assertEquals; 63 64 import java.io.ByteArrayInputStream; 65 import java.io.ByteArrayOutputStream; 66 import java.io.ObjectInputStream; 67 import java.io.ObjectOutputStream; 68 import java.time.DateTimeException; 69 import java.time.temporal.ChronoField; 70 import java.time.temporal.ValueRange; 71 72 import org.testng.annotations.DataProvider; 73 import org.testng.annotations.Test; 74 import test.java.time.AbstractTest; 75 76 /** 77 * Test. 78 * @bug 8239520 79 */ 80 @Test 81 public class TestDateTimeValueRange extends AbstractTest { 82 83 //----------------------------------------------------------------------- 84 // Basics 85 //----------------------------------------------------------------------- 86 @Test test_immutable()87 public void test_immutable() { 88 assertImmutable(ValueRange.class); 89 } 90 91 //----------------------------------------------------------------------- 92 // of(long,long) 93 //----------------------------------------------------------------------- test_of_longlong()94 public void test_of_longlong() { 95 ValueRange test = ValueRange.of(1, 12); 96 assertEquals(test.getMinimum(), 1); 97 assertEquals(test.getLargestMinimum(), 1); 98 assertEquals(test.getSmallestMaximum(), 12); 99 assertEquals(test.getMaximum(), 12); 100 assertEquals(test.isFixed(), true); 101 assertEquals(test.isIntValue(), true); 102 } 103 test_of_longlong_big()104 public void test_of_longlong_big() { 105 ValueRange test = ValueRange.of(1, 123456789012345L); 106 assertEquals(test.getMinimum(), 1); 107 assertEquals(test.getLargestMinimum(), 1); 108 assertEquals(test.getSmallestMaximum(), 123456789012345L); 109 assertEquals(test.getMaximum(), 123456789012345L); 110 assertEquals(test.isFixed(), true); 111 assertEquals(test.isIntValue(), false); 112 } 113 114 @Test(expectedExceptions = IllegalArgumentException.class) test_of_longlong_minGtMax()115 public void test_of_longlong_minGtMax() { 116 ValueRange.of(12, 1); 117 } 118 119 //----------------------------------------------------------------------- 120 // of(long,long,long) 121 //----------------------------------------------------------------------- test_of_longlonglong()122 public void test_of_longlonglong() { 123 ValueRange test = ValueRange.of(1, 28, 31); 124 assertEquals(test.getMinimum(), 1); 125 assertEquals(test.getLargestMinimum(), 1); 126 assertEquals(test.getSmallestMaximum(), 28); 127 assertEquals(test.getMaximum(), 31); 128 assertEquals(test.isFixed(), false); 129 assertEquals(test.isIntValue(), true); 130 } 131 132 @Test(expectedExceptions = IllegalArgumentException.class) test_of_longlonglong_minGtMax()133 public void test_of_longlonglong_minGtMax() { 134 ValueRange.of(12, 1, 2); 135 } 136 137 @Test(expectedExceptions = IllegalArgumentException.class) test_of_longlonglong_smallestmaxminGtMax()138 public void test_of_longlonglong_smallestmaxminGtMax() { 139 ValueRange.of(1, 31, 28); 140 } 141 142 @Test(expectedExceptions = IllegalArgumentException.class) test_of_longlonglong_minGtSmallestMax()143 public void test_of_longlonglong_minGtSmallestMax() { 144 ValueRange.of(5, 2, 10); 145 } 146 147 //----------------------------------------------------------------------- 148 // of(long,long,long,long) 149 //----------------------------------------------------------------------- 150 @DataProvider(name="valid") data_valid()151 Object[][] data_valid() { 152 return new Object[][] { 153 {1, 1, 1, 1}, 154 {1, 1, 1, 2}, 155 {1, 1, 2, 2}, 156 {1, 2, 3, 4}, 157 {1, 1, 28, 31}, 158 {1, 3, 31, 31}, 159 {-5, -4, -3, -2}, 160 {-5, -4, 3, 4}, 161 {1, 20, 10, 31}, 162 }; 163 } 164 165 @Test(dataProvider="valid") test_of_longlonglonglong(long sMin, long lMin, long sMax, long lMax)166 public void test_of_longlonglonglong(long sMin, long lMin, long sMax, long lMax) { 167 ValueRange test = ValueRange.of(sMin, lMin, sMax, lMax); 168 assertEquals(test.getMinimum(), sMin); 169 assertEquals(test.getLargestMinimum(), lMin); 170 assertEquals(test.getSmallestMaximum(), sMax); 171 assertEquals(test.getMaximum(), lMax); 172 assertEquals(test.isFixed(), sMin == lMin && sMax == lMax); 173 assertEquals(test.isIntValue(), true); 174 } 175 176 @DataProvider(name="invalid") data_invalid()177 Object[][] data_invalid() { 178 return new Object[][] { 179 {1, 2, 31, 28}, 180 {1, 31, 2, 28}, 181 {31, 2, 1, 28}, 182 {31, 2, 3, 28}, 183 184 {2, 1, 28, 31}, 185 {2, 1, 31, 28}, 186 {12, 13, 1, 2}, 187 188 {10, 11, 0, 12}, // smallest minimum is greater than the smallest maximum 189 {0, 1, 11, 10}, // smallest maximum is greater than the largest maximum 190 {0, 11, 1, 10}, // largest minimum is greater than the largest maximum 191 {1, 0, 10, 11}, // smallest minimum is greater than the largest minimum 192 }; 193 } 194 195 @Test(dataProvider="invalid", expectedExceptions=IllegalArgumentException.class) test_of_longlonglonglong_invalid(long sMin, long lMin, long sMax, long lMax)196 public void test_of_longlonglonglong_invalid(long sMin, long lMin, long sMax, long lMax) { 197 ValueRange.of(sMin, lMin, sMax, lMax); 198 } 199 200 //----------------------------------------------------------------------- 201 // isValidValue(long) 202 //----------------------------------------------------------------------- test_isValidValue_long()203 public void test_isValidValue_long() { 204 ValueRange test = ValueRange.of(1, 28, 31); 205 assertEquals(test.isValidValue(0), false); 206 assertEquals(test.isValidValue(1), true); 207 assertEquals(test.isValidValue(2), true); 208 assertEquals(test.isValidValue(30), true); 209 assertEquals(test.isValidValue(31), true); 210 assertEquals(test.isValidValue(32), false); 211 } 212 213 //----------------------------------------------------------------------- 214 // isValidIntValue(long) 215 //----------------------------------------------------------------------- test_isValidValue_long_int()216 public void test_isValidValue_long_int() { 217 ValueRange test = ValueRange.of(1, 28, 31); 218 assertEquals(test.isValidValue(0), false); 219 assertEquals(test.isValidValue(1), true); 220 assertEquals(test.isValidValue(31), true); 221 assertEquals(test.isValidValue(32), false); 222 } 223 test_isValidValue_long_long()224 public void test_isValidValue_long_long() { 225 ValueRange test = ValueRange.of(1, 28, Integer.MAX_VALUE + 1L); 226 assertEquals(test.isValidIntValue(0), false); 227 assertEquals(test.isValidIntValue(1), false); 228 assertEquals(test.isValidIntValue(31), false); 229 assertEquals(test.isValidIntValue(32), false); 230 } 231 232 //----------------------------------------------------------------------- 233 // checkValidValue 234 //----------------------------------------------------------------------- 235 @Test(dataProvider="valid") test_of_checkValidValue(long sMin, long lMin, long sMax, long lMax)236 public void test_of_checkValidValue(long sMin, long lMin, long sMax, long lMax) { 237 ValueRange test = ValueRange.of(sMin, lMin, sMax, lMax); 238 assertEquals(test.checkValidIntValue(sMin, null), sMin); 239 assertEquals(test.checkValidIntValue(lMin, null), lMin); 240 assertEquals(test.checkValidIntValue(sMax, null), sMax); 241 assertEquals(test.checkValidIntValue(lMax, null), lMax); 242 } 243 244 @Test(dataProvider="valid", expectedExceptions = DateTimeException.class) test_of_checkValidValueMinException(long sMin, long lMin, long sMax, long lMax)245 public void test_of_checkValidValueMinException(long sMin, long lMin, long sMax, long lMax) { 246 ValueRange test = ValueRange.of(sMin, lMin, sMax, lMax); 247 test.checkValidIntValue(sMin-1, null); 248 } 249 250 @Test(dataProvider="valid", expectedExceptions = DateTimeException.class) test_of_checkValidValueMaxException(long sMin, long lMin, long sMax, long lMax)251 public void test_of_checkValidValueMaxException(long sMin, long lMin, long sMax, long lMax) { 252 ValueRange test = ValueRange.of(sMin, lMin, sMax, lMax); 253 test.checkValidIntValue(lMax+1, null); 254 } 255 256 @Test(expectedExceptions = DateTimeException.class) test_checkValidValueUnsupported_long_long()257 public void test_checkValidValueUnsupported_long_long() { 258 ValueRange test = ValueRange.of(1, 28, Integer.MAX_VALUE + 1L); 259 test.checkValidIntValue(0, (ChronoField)null); 260 } 261 262 @Test(expectedExceptions = DateTimeException.class) test_checkValidValueInvalid_long_long()263 public void test_checkValidValueInvalid_long_long() { 264 ValueRange test = ValueRange.of(1, 28, Integer.MAX_VALUE + 1L); 265 test.checkValidIntValue(Integer.MAX_VALUE + 2L, (ChronoField)null); 266 } 267 268 //----------------------------------------------------------------------- 269 // equals() / hashCode() 270 //----------------------------------------------------------------------- test_equals1()271 public void test_equals1() { 272 ValueRange a = ValueRange.of(1, 2, 3, 4); 273 ValueRange b = ValueRange.of(1, 2, 3, 4); 274 assertEquals(a.equals(a), true); 275 assertEquals(a.equals(b), true); 276 assertEquals(b.equals(a), true); 277 assertEquals(b.equals(b), true); 278 assertEquals(a.hashCode() == b.hashCode(), true); 279 } 280 test_equals2()281 public void test_equals2() { 282 ValueRange a = ValueRange.of(1, 2, 3, 4); 283 assertEquals(a.equals(ValueRange.of(0, 2, 3, 4)), false); 284 assertEquals(a.equals(ValueRange.of(1, 3, 3, 4)), false); 285 assertEquals(a.equals(ValueRange.of(1, 2, 4, 4)), false); 286 assertEquals(a.equals(ValueRange.of(1, 2, 3, 5)), false); 287 } 288 test_equals_otherType()289 public void test_equals_otherType() { 290 ValueRange a = ValueRange.of(1, 12); 291 assertEquals(a.equals("Rubbish"), false); 292 } 293 test_equals_null()294 public void test_equals_null() { 295 ValueRange a = ValueRange.of(1, 12); 296 assertEquals(a.equals(null), false); 297 } 298 299 //----------------------------------------------------------------------- 300 // toString() 301 //----------------------------------------------------------------------- test_toString()302 public void test_toString() { 303 assertEquals(ValueRange.of(1, 1, 4, 4).toString(), "1 - 4"); 304 assertEquals(ValueRange.of(1, 1, 3, 4).toString(), "1 - 3/4"); 305 assertEquals(ValueRange.of(1, 2, 3, 4).toString(), "1/2 - 3/4"); 306 assertEquals(ValueRange.of(1, 2, 4, 4).toString(), "1/2 - 4"); 307 } 308 309 } 310