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