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) 2008-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 tck.java.time; 61 62 import static java.time.DayOfWeek.MONDAY; 63 import static java.time.DayOfWeek.SUNDAY; 64 import static java.time.DayOfWeek.WEDNESDAY; 65 import static java.time.temporal.ChronoField.DAY_OF_WEEK; 66 import static org.testng.Assert.assertEquals; 67 import static org.testng.Assert.assertSame; 68 69 import java.util.ArrayList; 70 import java.util.Arrays; 71 import java.util.List; 72 import java.util.Locale; 73 74 import java.time.DateTimeException; 75 import java.time.DayOfWeek; 76 import java.time.LocalDate; 77 import java.time.LocalTime; 78 import java.time.Month; 79 import java.time.format.TextStyle; 80 import java.time.temporal.ChronoField; 81 import java.time.temporal.ChronoUnit; 82 import java.time.temporal.ISOChrono; 83 import java.time.temporal.JulianFields; 84 import java.time.temporal.Queries; 85 import java.time.temporal.Temporal; 86 import java.time.temporal.TemporalAccessor; 87 import java.time.temporal.TemporalField; 88 89 import org.testng.annotations.BeforeMethod; 90 import org.testng.annotations.DataProvider; 91 import org.testng.annotations.Test; 92 93 /** 94 * Test DayOfWeek. 95 */ 96 @Test 97 public class TCKDayOfWeek extends AbstractDateTimeTest { 98 99 @BeforeMethod 100 public void setUp() { 101 } 102 103 //----------------------------------------------------------------------- 104 @Override 105 protected List<TemporalAccessor> samples() { 106 TemporalAccessor[] array = {MONDAY, WEDNESDAY, SUNDAY, }; 107 return Arrays.asList(array); 108 } 109 110 @Override 111 protected List<TemporalField> validFields() { 112 TemporalField[] array = { 113 DAY_OF_WEEK, 114 }; 115 return Arrays.asList(array); 116 } 117 118 @Override 119 protected List<TemporalField> invalidFields() { 120 List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values())); 121 list.removeAll(validFields()); 122 list.add(JulianFields.JULIAN_DAY); 123 list.add(JulianFields.MODIFIED_JULIAN_DAY); 124 list.add(JulianFields.RATA_DIE); 125 return list; 126 } 127 128 //----------------------------------------------------------------------- 129 @Test(groups={"tck"}) 130 public void test_factory_int_singleton() { 131 for (int i = 1; i <= 7; i++) { 132 DayOfWeek test = DayOfWeek.of(i); 133 assertEquals(test.getValue(), i); 134 assertSame(DayOfWeek.of(i), test); 135 } 136 } 137 138 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 139 public void test_factory_int_valueTooLow() { 140 DayOfWeek.of(0); 141 } 142 143 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 144 public void test_factory_int_valueTooHigh() { 145 DayOfWeek.of(8); 146 } 147 148 //----------------------------------------------------------------------- 149 @Test(groups={"tck"}) 150 public void test_factory_CalendricalObject() { 151 assertEquals(DayOfWeek.from(LocalDate.of(2011, 6, 6)), DayOfWeek.MONDAY); 152 } 153 154 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 155 public void test_factory_CalendricalObject_invalid_noDerive() { 156 DayOfWeek.from(LocalTime.of(12, 30)); 157 } 158 159 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 160 public void test_factory_CalendricalObject_null() { 161 DayOfWeek.from((TemporalAccessor) null); 162 } 163 164 //----------------------------------------------------------------------- 165 // get(TemporalField) 166 //----------------------------------------------------------------------- 167 @Test 168 public void test_get_TemporalField() { 169 assertEquals(DayOfWeek.WEDNESDAY.getLong(ChronoField.DAY_OF_WEEK), 3); 170 } 171 172 @Test 173 public void test_getLong_TemporalField() { 174 assertEquals(DayOfWeek.WEDNESDAY.getLong(ChronoField.DAY_OF_WEEK), 3); 175 } 176 177 //----------------------------------------------------------------------- 178 // query(TemporalQuery) 179 //----------------------------------------------------------------------- 180 @Test 181 public void test_query_chrono() { 182 assertEquals(DayOfWeek.FRIDAY.query(Queries.chrono()), null); 183 assertEquals(Queries.chrono().queryFrom(DayOfWeek.FRIDAY), null); 184 } 185 186 @Test 187 public void test_query_zoneId() { 188 assertEquals(DayOfWeek.FRIDAY.query(Queries.zoneId()), null); 189 assertEquals(Queries.zoneId().queryFrom(DayOfWeek.FRIDAY), null); 190 } 191 192 @Test 193 public void test_query_precision() { 194 assertEquals(DayOfWeek.FRIDAY.query(Queries.precision()), ChronoUnit.DAYS); 195 assertEquals(Queries.precision().queryFrom(DayOfWeek.FRIDAY), ChronoUnit.DAYS); 196 } 197 198 @Test 199 public void test_query_offset() { 200 assertEquals(DayOfWeek.FRIDAY.query(Queries.offset()), null); 201 assertEquals(Queries.offset().queryFrom(DayOfWeek.FRIDAY), null); 202 } 203 204 @Test 205 public void test_query_zone() { 206 assertEquals(DayOfWeek.FRIDAY.query(Queries.zone()), null); 207 assertEquals(Queries.zone().queryFrom(DayOfWeek.FRIDAY), null); 208 } 209 210 @Test(expectedExceptions=NullPointerException.class) 211 public void test_query_null() { 212 DayOfWeek.FRIDAY.query(null); 213 } 214 215 //----------------------------------------------------------------------- 216 // getText() 217 //----------------------------------------------------------------------- 218 @Test(groups={"tck"}) 219 public void test_getText() { 220 assertEquals(DayOfWeek.MONDAY.getText(TextStyle.SHORT, Locale.US), "Mon"); 221 } 222 223 @Test(expectedExceptions = NullPointerException.class, groups={"tck"}) 224 public void test_getText_nullStyle() { 225 DayOfWeek.MONDAY.getText(null, Locale.US); 226 } 227 228 @Test(expectedExceptions = NullPointerException.class, groups={"tck"}) 229 public void test_getText_nullLocale() { 230 DayOfWeek.MONDAY.getText(TextStyle.FULL, null); 231 } 232 233 //----------------------------------------------------------------------- 234 // plus(long), plus(long,unit) 235 //----------------------------------------------------------------------- 236 @DataProvider(name="plus") 237 Object[][] data_plus() { 238 return new Object[][] { 239 {1, -8, 7}, 240 {1, -7, 1}, 241 {1, -6, 2}, 242 {1, -5, 3}, 243 {1, -4, 4}, 244 {1, -3, 5}, 245 {1, -2, 6}, 246 {1, -1, 7}, 247 {1, 0, 1}, 248 {1, 1, 2}, 249 {1, 2, 3}, 250 {1, 3, 4}, 251 {1, 4, 5}, 252 {1, 5, 6}, 253 {1, 6, 7}, 254 {1, 7, 1}, 255 {1, 8, 2}, 256 257 {1, 1, 2}, 258 {2, 1, 3}, 259 {3, 1, 4}, 260 {4, 1, 5}, 261 {5, 1, 6}, 262 {6, 1, 7}, 263 {7, 1, 1}, 264 265 {1, -1, 7}, 266 {2, -1, 1}, 267 {3, -1, 2}, 268 {4, -1, 3}, 269 {5, -1, 4}, 270 {6, -1, 5}, 271 {7, -1, 6}, 272 }; 273 } 274 275 @Test(dataProvider="plus", groups={"tck"}) 276 public void test_plus_long(int base, long amount, int expected) { 277 assertEquals(DayOfWeek.of(base).plus(amount), DayOfWeek.of(expected)); 278 } 279 280 //----------------------------------------------------------------------- 281 // minus(long), minus(long,unit) 282 //----------------------------------------------------------------------- 283 @DataProvider(name="minus") 284 Object[][] data_minus() { 285 return new Object[][] { 286 {1, -8, 2}, 287 {1, -7, 1}, 288 {1, -6, 7}, 289 {1, -5, 6}, 290 {1, -4, 5}, 291 {1, -3, 4}, 292 {1, -2, 3}, 293 {1, -1, 2}, 294 {1, 0, 1}, 295 {1, 1, 7}, 296 {1, 2, 6}, 297 {1, 3, 5}, 298 {1, 4, 4}, 299 {1, 5, 3}, 300 {1, 6, 2}, 301 {1, 7, 1}, 302 {1, 8, 7}, 303 }; 304 } 305 306 @Test(dataProvider="minus", groups={"tck"}) 307 public void test_minus_long(int base, long amount, int expected) { 308 assertEquals(DayOfWeek.of(base).minus(amount), DayOfWeek.of(expected)); 309 } 310 311 //----------------------------------------------------------------------- 312 // adjustInto() 313 //----------------------------------------------------------------------- 314 @Test(groups={"tck"}) 315 public void test_adjustInto() { 316 assertEquals(DayOfWeek.MONDAY.adjustInto(LocalDate.of(2012, 9, 2)), LocalDate.of(2012, 8, 27)); 317 assertEquals(DayOfWeek.MONDAY.adjustInto(LocalDate.of(2012, 9, 3)), LocalDate.of(2012, 9, 3)); 318 assertEquals(DayOfWeek.MONDAY.adjustInto(LocalDate.of(2012, 9, 4)), LocalDate.of(2012, 9, 3)); 319 assertEquals(DayOfWeek.MONDAY.adjustInto(LocalDate.of(2012, 9, 10)), LocalDate.of(2012, 9, 10)); 320 assertEquals(DayOfWeek.MONDAY.adjustInto(LocalDate.of(2012, 9, 11)), LocalDate.of(2012, 9, 10)); 321 } 322 323 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 324 public void test_adjustInto_null() { 325 DayOfWeek.MONDAY.adjustInto((Temporal) null); 326 } 327 328 //----------------------------------------------------------------------- 329 // toString() 330 //----------------------------------------------------------------------- 331 @Test(groups={"tck"}) 332 public void test_toString() { 333 assertEquals(DayOfWeek.MONDAY.toString(), "MONDAY"); 334 assertEquals(DayOfWeek.TUESDAY.toString(), "TUESDAY"); 335 assertEquals(DayOfWeek.WEDNESDAY.toString(), "WEDNESDAY"); 336 assertEquals(DayOfWeek.THURSDAY.toString(), "THURSDAY"); 337 assertEquals(DayOfWeek.FRIDAY.toString(), "FRIDAY"); 338 assertEquals(DayOfWeek.SATURDAY.toString(), "SATURDAY"); 339 assertEquals(DayOfWeek.SUNDAY.toString(), "SUNDAY"); 340 } 341 342 //----------------------------------------------------------------------- 343 // generated methods 344 //----------------------------------------------------------------------- 345 @Test(groups={"tck"}) 346 public void test_enum() { 347 assertEquals(DayOfWeek.valueOf("MONDAY"), DayOfWeek.MONDAY); 348 assertEquals(DayOfWeek.values()[0], DayOfWeek.MONDAY); 349 } 350 351 }