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) 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 69 import java.time.temporal.ValueRange; 70 71 import org.testng.annotations.DataProvider; 72 import org.testng.annotations.Test; 73 import test.java.time.AbstractTest; 74 75 /** 76 * Test. 77 */ 78 @Test 79 public class TestDateTimeValueRange extends AbstractTest { 80 81 //----------------------------------------------------------------------- 82 // Basics 83 //----------------------------------------------------------------------- 84 @Test 85 public void test_immutable() { 86 assertImmutable(ValueRange.class); 87 } 88 89 //----------------------------------------------------------------------- 90 // Serialization 91 //----------------------------------------------------------------------- 92 public void test_serialization() throws Exception { 93 Object obj = ValueRange.of(1, 2, 3, 4); 94 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 95 ObjectOutputStream oos = new ObjectOutputStream(baos); 96 oos.writeObject(obj); 97 oos.close(); 98 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); 99 assertEquals(ois.readObject(), obj); 100 } 101 102 //----------------------------------------------------------------------- 103 // of(long,long) 104 //----------------------------------------------------------------------- 105 public void test_of_longlong() { 106 ValueRange test = ValueRange.of(1, 12); 107 assertEquals(test.getMinimum(), 1); 108 assertEquals(test.getLargestMinimum(), 1); 109 assertEquals(test.getSmallestMaximum(), 12); 110 assertEquals(test.getMaximum(), 12); 111 assertEquals(test.isFixed(), true); 112 assertEquals(test.isIntValue(), true); 113 } 114 115 public void test_of_longlong_big() { 116 ValueRange test = ValueRange.of(1, 123456789012345L); 117 assertEquals(test.getMinimum(), 1); 118 assertEquals(test.getLargestMinimum(), 1); 119 assertEquals(test.getSmallestMaximum(), 123456789012345L); 120 assertEquals(test.getMaximum(), 123456789012345L); 121 assertEquals(test.isFixed(), true); 122 assertEquals(test.isIntValue(), false); 123 } 124 125 @Test(expectedExceptions = IllegalArgumentException.class) 126 public void test_of_longlong_minGtMax() { 127 ValueRange.of(12, 1); 128 } 129 130 //----------------------------------------------------------------------- 131 // of(long,long,long) 132 //----------------------------------------------------------------------- 133 public void test_of_longlonglong() { 134 ValueRange test = ValueRange.of(1, 28, 31); 135 assertEquals(test.getMinimum(), 1); 136 assertEquals(test.getLargestMinimum(), 1); 137 assertEquals(test.getSmallestMaximum(), 28); 138 assertEquals(test.getMaximum(), 31); 139 assertEquals(test.isFixed(), false); 140 assertEquals(test.isIntValue(), true); 141 } 142 143 @Test(expectedExceptions = IllegalArgumentException.class) 144 public void test_of_longlonglong_minGtMax() { 145 ValueRange.of(12, 1, 2); 146 } 147 148 @Test(expectedExceptions = IllegalArgumentException.class) 149 public void test_of_longlonglong_smallestmaxminGtMax() { 150 ValueRange.of(1, 31, 28); 151 } 152 153 //----------------------------------------------------------------------- 154 // of(long,long,long,long) 155 //----------------------------------------------------------------------- 156 @DataProvider(name="valid") 157 Object[][] data_valid() { 158 return new Object[][] { 159 {1, 1, 1, 1}, 160 {1, 1, 1, 2}, 161 {1, 1, 2, 2}, 162 {1, 2, 3, 4}, 163 {1, 1, 28, 31}, 164 {1, 3, 31, 31}, 165 {-5, -4, -3, -2}, 166 {-5, -4, 3, 4}, 167 {1, 20, 10, 31}, 168 }; 169 } 170 171 @Test(dataProvider="valid") 172 public void test_of_longlonglonglong(long sMin, long lMin, long sMax, long lMax) { 173 ValueRange test = ValueRange.of(sMin, lMin, sMax, lMax); 174 assertEquals(test.getMinimum(), sMin); 175 assertEquals(test.getLargestMinimum(), lMin); 176 assertEquals(test.getSmallestMaximum(), sMax); 177 assertEquals(test.getMaximum(), lMax); 178 assertEquals(test.isFixed(), sMin == lMin && sMax == lMax); 179 assertEquals(test.isIntValue(), true); 180 } 181 182 @DataProvider(name="invalid") 183 Object[][] data_invalid() { 184 return new Object[][] { 185 {1, 2, 31, 28}, 186 {1, 31, 2, 28}, 187 {31, 2, 1, 28}, 188 {31, 2, 3, 28}, 189 190 {2, 1, 28, 31}, 191 {2, 1, 31, 28}, 192 {12, 13, 1, 2}, 193 }; 194 } 195 196 @Test(dataProvider="invalid", expectedExceptions=IllegalArgumentException.class) 197 public void test_of_longlonglonglong_invalid(long sMin, long lMin, long sMax, long lMax) { 198 ValueRange.of(sMin, lMin, sMax, lMax); 199 } 200 201 //----------------------------------------------------------------------- 202 // isValidValue(long) 203 //----------------------------------------------------------------------- 204 public void test_isValidValue_long() { 205 ValueRange test = ValueRange.of(1, 28, 31); 206 assertEquals(test.isValidValue(0), false); 207 assertEquals(test.isValidValue(1), true); 208 assertEquals(test.isValidValue(2), true); 209 assertEquals(test.isValidValue(30), true); 210 assertEquals(test.isValidValue(31), true); 211 assertEquals(test.isValidValue(32), false); 212 } 213 214 //----------------------------------------------------------------------- 215 // isValidIntValue(long) 216 //----------------------------------------------------------------------- 217 public void test_isValidValue_long_int() { 218 ValueRange test = ValueRange.of(1, 28, 31); 219 assertEquals(test.isValidValue(0), false); 220 assertEquals(test.isValidValue(1), true); 221 assertEquals(test.isValidValue(31), true); 222 assertEquals(test.isValidValue(32), false); 223 } 224 225 public void test_isValidValue_long_long() { 226 ValueRange test = ValueRange.of(1, 28, Integer.MAX_VALUE + 1L); 227 assertEquals(test.isValidIntValue(0), false); 228 assertEquals(test.isValidIntValue(1), false); 229 assertEquals(test.isValidIntValue(31), false); 230 assertEquals(test.isValidIntValue(32), false); 231 } 232 233 //----------------------------------------------------------------------- 234 // equals() / hashCode() 235 //----------------------------------------------------------------------- 236 public void test_equals1() { 237 ValueRange a = ValueRange.of(1, 2, 3, 4); 238 ValueRange b = ValueRange.of(1, 2, 3, 4); 239 assertEquals(a.equals(a), true); 240 assertEquals(a.equals(b), true); 241 assertEquals(b.equals(a), true); 242 assertEquals(b.equals(b), true); 243 assertEquals(a.hashCode() == b.hashCode(), true); 244 } 245 246 public void test_equals2() { 247 ValueRange a = ValueRange.of(1, 2, 3, 4); 248 assertEquals(a.equals(ValueRange.of(0, 2, 3, 4)), false); 249 assertEquals(a.equals(ValueRange.of(1, 3, 3, 4)), false); 250 assertEquals(a.equals(ValueRange.of(1, 2, 4, 4)), false); 251 assertEquals(a.equals(ValueRange.of(1, 2, 3, 5)), false); 252 } 253 254 public void test_equals_otherType() { 255 ValueRange a = ValueRange.of(1, 12); 256 assertEquals(a.equals("Rubbish"), false); 257 } 258 259 public void test_equals_null() { 260 ValueRange a = ValueRange.of(1, 12); 261 assertEquals(a.equals(null), false); 262 } 263 264 //----------------------------------------------------------------------- 265 // toString() 266 //----------------------------------------------------------------------- 267 public void test_toString() { 268 assertEquals(ValueRange.of(1, 1, 4, 4).toString(), "1 - 4"); 269 assertEquals(ValueRange.of(1, 1, 3, 4).toString(), "1 - 3/4"); 270 assertEquals(ValueRange.of(1, 2, 3, 4).toString(), "1/2 - 3/4"); 271 assertEquals(ValueRange.of(1, 2, 4, 4).toString(), "1/2 - 4"); 272 } 273 274 }