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 */
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 */
79 @Test
80 public class TestDateTimeValueRange extends AbstractTest {
81
82 //-----------------------------------------------------------------------
83 // Basics
84 //-----------------------------------------------------------------------
85 @Test
86 public void test_immutable() {
87 assertImmutable(ValueRange.class);
88 }
89
90 //-----------------------------------------------------------------------
91 // of(long,long)
92 //-----------------------------------------------------------------------
93 public void test_of_longlong() {
94 ValueRange test = ValueRange.of(1, 12);
95 assertEquals(test.getMinimum(), 1);
96 assertEquals(test.getLargestMinimum(), 1);
97 assertEquals(test.getSmallestMaximum(), 12);
121 public void test_of_longlonglong() {
122 ValueRange test = ValueRange.of(1, 28, 31);
123 assertEquals(test.getMinimum(), 1);
124 assertEquals(test.getLargestMinimum(), 1);
125 assertEquals(test.getSmallestMaximum(), 28);
126 assertEquals(test.getMaximum(), 31);
127 assertEquals(test.isFixed(), false);
128 assertEquals(test.isIntValue(), true);
129 }
130
131 @Test(expectedExceptions = IllegalArgumentException.class)
132 public void test_of_longlonglong_minGtMax() {
133 ValueRange.of(12, 1, 2);
134 }
135
136 @Test(expectedExceptions = IllegalArgumentException.class)
137 public void test_of_longlonglong_smallestmaxminGtMax() {
138 ValueRange.of(1, 31, 28);
139 }
140
141 //-----------------------------------------------------------------------
142 // of(long,long,long,long)
143 //-----------------------------------------------------------------------
144 @DataProvider(name="valid")
145 Object[][] data_valid() {
146 return new Object[][] {
147 {1, 1, 1, 1},
148 {1, 1, 1, 2},
149 {1, 1, 2, 2},
150 {1, 2, 3, 4},
151 {1, 1, 28, 31},
152 {1, 3, 31, 31},
153 {-5, -4, -3, -2},
154 {-5, -4, 3, 4},
155 {1, 20, 10, 31},
156 };
157 }
158
159 @Test(dataProvider="valid")
160 public void test_of_longlonglonglong(long sMin, long lMin, long sMax, long lMax) {
161 ValueRange test = ValueRange.of(sMin, lMin, sMax, lMax);
162 assertEquals(test.getMinimum(), sMin);
163 assertEquals(test.getLargestMinimum(), lMin);
164 assertEquals(test.getSmallestMaximum(), sMax);
165 assertEquals(test.getMaximum(), lMax);
166 assertEquals(test.isFixed(), sMin == lMin && sMax == lMax);
167 assertEquals(test.isIntValue(), true);
168 }
169
170 @DataProvider(name="invalid")
171 Object[][] data_invalid() {
172 return new Object[][] {
173 {1, 2, 31, 28},
174 {1, 31, 2, 28},
175 {31, 2, 1, 28},
176 {31, 2, 3, 28},
177
178 {2, 1, 28, 31},
179 {2, 1, 31, 28},
180 {12, 13, 1, 2},
181 };
182 }
183
184 @Test(dataProvider="invalid", expectedExceptions=IllegalArgumentException.class)
185 public void test_of_longlonglonglong_invalid(long sMin, long lMin, long sMax, long lMax) {
186 ValueRange.of(sMin, lMin, sMax, lMax);
187 }
188
189 //-----------------------------------------------------------------------
190 // isValidValue(long)
191 //-----------------------------------------------------------------------
192 public void test_isValidValue_long() {
193 ValueRange test = ValueRange.of(1, 28, 31);
194 assertEquals(test.isValidValue(0), false);
195 assertEquals(test.isValidValue(1), true);
196 assertEquals(test.isValidValue(2), true);
197 assertEquals(test.isValidValue(30), true);
198 assertEquals(test.isValidValue(31), true);
199 assertEquals(test.isValidValue(32), false);
200 }
|
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 */
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
87 public void test_immutable() {
88 assertImmutable(ValueRange.class);
89 }
90
91 //-----------------------------------------------------------------------
92 // of(long,long)
93 //-----------------------------------------------------------------------
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);
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)
133 public void test_of_longlonglong_minGtMax() {
134 ValueRange.of(12, 1, 2);
135 }
136
137 @Test(expectedExceptions = IllegalArgumentException.class)
138 public void test_of_longlonglong_smallestmaxminGtMax() {
139 ValueRange.of(1, 31, 28);
140 }
141
142 @Test(expectedExceptions = IllegalArgumentException.class)
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")
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")
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")
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 {5, 5, 2, 10},
188 };
189 }
190
191 @Test(dataProvider="invalid", expectedExceptions=IllegalArgumentException.class)
192 public void test_of_longlonglonglong_invalid(long sMin, long lMin, long sMax, long lMax) {
193 ValueRange.of(sMin, lMin, sMax, lMax);
194 }
195
196 //-----------------------------------------------------------------------
197 // isValidValue(long)
198 //-----------------------------------------------------------------------
199 public void test_isValidValue_long() {
200 ValueRange test = ValueRange.of(1, 28, 31);
201 assertEquals(test.isValidValue(0), false);
202 assertEquals(test.isValidValue(1), true);
203 assertEquals(test.isValidValue(2), true);
204 assertEquals(test.isValidValue(30), true);
205 assertEquals(test.isValidValue(31), true);
206 assertEquals(test.isValidValue(32), false);
207 }
|