1 /* 2 * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * * Neither the name of JSR-310 nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package org.threeten.bp; 33 34 import static org.testng.Assert.assertEquals; 35 import static org.testng.Assert.assertSame; 36 import static org.threeten.bp.DayOfWeek.MONDAY; 37 import static org.threeten.bp.DayOfWeek.SUNDAY; 38 import static org.threeten.bp.DayOfWeek.WEDNESDAY; 39 import static org.threeten.bp.temporal.ChronoField.DAY_OF_WEEK; 40 41 import java.util.ArrayList; 42 import java.util.Arrays; 43 import java.util.List; 44 import java.util.Locale; 45 46 import org.testng.annotations.BeforeMethod; 47 import org.testng.annotations.DataProvider; 48 import org.testng.annotations.Test; 49 import org.threeten.bp.format.TextStyle; 50 import org.threeten.bp.temporal.ChronoField; 51 import org.threeten.bp.temporal.ChronoUnit; 52 import org.threeten.bp.temporal.JulianFields; 53 import org.threeten.bp.temporal.Temporal; 54 import org.threeten.bp.temporal.TemporalAccessor; 55 import org.threeten.bp.temporal.TemporalField; 56 import org.threeten.bp.temporal.TemporalQueries; 57 58 /** 59 * Test DayOfWeek. 60 */ 61 @Test 62 public class TestDayOfWeek extends AbstractDateTimeTest { 63 64 @BeforeMethod setUp()65 public void setUp() { 66 } 67 68 //----------------------------------------------------------------------- 69 @Override samples()70 protected List<TemporalAccessor> samples() { 71 TemporalAccessor[] array = {MONDAY, WEDNESDAY, SUNDAY, }; 72 return Arrays.asList(array); 73 } 74 75 @Override validFields()76 protected List<TemporalField> validFields() { 77 TemporalField[] array = { 78 DAY_OF_WEEK, 79 }; 80 return Arrays.asList(array); 81 } 82 83 @Override invalidFields()84 protected List<TemporalField> invalidFields() { 85 List<TemporalField> list = new ArrayList<TemporalField>(Arrays.<TemporalField>asList(ChronoField.values())); 86 list.removeAll(validFields()); 87 list.add(JulianFields.JULIAN_DAY); 88 list.add(JulianFields.MODIFIED_JULIAN_DAY); 89 list.add(JulianFields.RATA_DIE); 90 return list; 91 } 92 93 //----------------------------------------------------------------------- 94 @Test test_factory_int_singleton()95 public void test_factory_int_singleton() { 96 for (int i = 1; i <= 7; i++) { 97 DayOfWeek test = DayOfWeek.of(i); 98 assertEquals(test.getValue(), i); 99 assertSame(DayOfWeek.of(i), test); 100 } 101 } 102 103 @Test(expectedExceptions=DateTimeException.class) test_factory_int_valueTooLow()104 public void test_factory_int_valueTooLow() { 105 DayOfWeek.of(0); 106 } 107 108 @Test(expectedExceptions=DateTimeException.class) test_factory_int_valueTooHigh()109 public void test_factory_int_valueTooHigh() { 110 DayOfWeek.of(8); 111 } 112 113 //----------------------------------------------------------------------- 114 @Test test_factory_CalendricalObject()115 public void test_factory_CalendricalObject() { 116 assertEquals(DayOfWeek.from(LocalDate.of(2011, 6, 6)), DayOfWeek.MONDAY); 117 } 118 119 @Test(expectedExceptions=DateTimeException.class) test_factory_CalendricalObject_invalid_noDerive()120 public void test_factory_CalendricalObject_invalid_noDerive() { 121 DayOfWeek.from(LocalTime.of(12, 30)); 122 } 123 124 @Test(expectedExceptions=NullPointerException.class) test_factory_CalendricalObject_null()125 public void test_factory_CalendricalObject_null() { 126 DayOfWeek.from((TemporalAccessor) null); 127 } 128 129 //----------------------------------------------------------------------- 130 // get(TemporalField) 131 //----------------------------------------------------------------------- 132 @Test test_get_TemporalField()133 public void test_get_TemporalField() { 134 assertEquals(DayOfWeek.WEDNESDAY.getLong(ChronoField.DAY_OF_WEEK), 3); 135 } 136 137 @Test test_getLong_TemporalField()138 public void test_getLong_TemporalField() { 139 assertEquals(DayOfWeek.WEDNESDAY.getLong(ChronoField.DAY_OF_WEEK), 3); 140 } 141 142 //----------------------------------------------------------------------- 143 // query(TemporalQuery) 144 //----------------------------------------------------------------------- 145 @Test test_query()146 public void test_query() { 147 assertEquals(DayOfWeek.FRIDAY.query(TemporalQueries.chronology()), null); 148 assertEquals(DayOfWeek.FRIDAY.query(TemporalQueries.localDate()), null); 149 assertEquals(DayOfWeek.FRIDAY.query(TemporalQueries.localTime()), null); 150 assertEquals(DayOfWeek.FRIDAY.query(TemporalQueries.offset()), null); 151 assertEquals(DayOfWeek.FRIDAY.query(TemporalQueries.precision()), ChronoUnit.DAYS); 152 assertEquals(DayOfWeek.FRIDAY.query(TemporalQueries.zone()), null); 153 assertEquals(DayOfWeek.FRIDAY.query(TemporalQueries.zoneId()), null); 154 } 155 156 @Test(expectedExceptions=NullPointerException.class) test_query_null()157 public void test_query_null() { 158 DayOfWeek.FRIDAY.query(null); 159 } 160 161 //----------------------------------------------------------------------- 162 // getDisplayName() 163 //----------------------------------------------------------------------- 164 @Test test_getDisplayName()165 public void test_getDisplayName() { 166 assertEquals(DayOfWeek.MONDAY.getDisplayName(TextStyle.SHORT, Locale.US), "Mon"); 167 } 168 169 @Test(expectedExceptions = NullPointerException.class) test_getDisplayName_nullStyle()170 public void test_getDisplayName_nullStyle() { 171 DayOfWeek.MONDAY.getDisplayName(null, Locale.US); 172 } 173 174 @Test(expectedExceptions = NullPointerException.class) test_getDisplayName_nullLocale()175 public void test_getDisplayName_nullLocale() { 176 DayOfWeek.MONDAY.getDisplayName(TextStyle.FULL, null); 177 } 178 179 //----------------------------------------------------------------------- 180 // plus(long), plus(long,unit) 181 //----------------------------------------------------------------------- 182 @DataProvider(name="plus") data_plus()183 Object[][] data_plus() { 184 return new Object[][] { 185 {1, -8, 7}, 186 {1, -7, 1}, 187 {1, -6, 2}, 188 {1, -5, 3}, 189 {1, -4, 4}, 190 {1, -3, 5}, 191 {1, -2, 6}, 192 {1, -1, 7}, 193 {1, 0, 1}, 194 {1, 1, 2}, 195 {1, 2, 3}, 196 {1, 3, 4}, 197 {1, 4, 5}, 198 {1, 5, 6}, 199 {1, 6, 7}, 200 {1, 7, 1}, 201 {1, 8, 2}, 202 203 {1, 1, 2}, 204 {2, 1, 3}, 205 {3, 1, 4}, 206 {4, 1, 5}, 207 {5, 1, 6}, 208 {6, 1, 7}, 209 {7, 1, 1}, 210 211 {1, -1, 7}, 212 {2, -1, 1}, 213 {3, -1, 2}, 214 {4, -1, 3}, 215 {5, -1, 4}, 216 {6, -1, 5}, 217 {7, -1, 6}, 218 }; 219 } 220 221 @Test(dataProvider="plus") test_plus_long(int base, long amount, int expected)222 public void test_plus_long(int base, long amount, int expected) { 223 assertEquals(DayOfWeek.of(base).plus(amount), DayOfWeek.of(expected)); 224 } 225 226 //----------------------------------------------------------------------- 227 // minus(long), minus(long,unit) 228 //----------------------------------------------------------------------- 229 @DataProvider(name="minus") data_minus()230 Object[][] data_minus() { 231 return new Object[][] { 232 {1, -8, 2}, 233 {1, -7, 1}, 234 {1, -6, 7}, 235 {1, -5, 6}, 236 {1, -4, 5}, 237 {1, -3, 4}, 238 {1, -2, 3}, 239 {1, -1, 2}, 240 {1, 0, 1}, 241 {1, 1, 7}, 242 {1, 2, 6}, 243 {1, 3, 5}, 244 {1, 4, 4}, 245 {1, 5, 3}, 246 {1, 6, 2}, 247 {1, 7, 1}, 248 {1, 8, 7}, 249 }; 250 } 251 252 @Test(dataProvider="minus") test_minus_long(int base, long amount, int expected)253 public void test_minus_long(int base, long amount, int expected) { 254 assertEquals(DayOfWeek.of(base).minus(amount), DayOfWeek.of(expected)); 255 } 256 257 //----------------------------------------------------------------------- 258 // adjustInto() 259 //----------------------------------------------------------------------- 260 @Test test_adjustInto()261 public void test_adjustInto() { 262 assertEquals(DayOfWeek.MONDAY.adjustInto(LocalDate.of(2012, 9, 2)), LocalDate.of(2012, 8, 27)); 263 assertEquals(DayOfWeek.MONDAY.adjustInto(LocalDate.of(2012, 9, 3)), LocalDate.of(2012, 9, 3)); 264 assertEquals(DayOfWeek.MONDAY.adjustInto(LocalDate.of(2012, 9, 4)), LocalDate.of(2012, 9, 3)); 265 assertEquals(DayOfWeek.MONDAY.adjustInto(LocalDate.of(2012, 9, 10)), LocalDate.of(2012, 9, 10)); 266 assertEquals(DayOfWeek.MONDAY.adjustInto(LocalDate.of(2012, 9, 11)), LocalDate.of(2012, 9, 10)); 267 } 268 269 @Test(expectedExceptions=NullPointerException.class) test_adjustInto_null()270 public void test_adjustInto_null() { 271 DayOfWeek.MONDAY.adjustInto((Temporal) null); 272 } 273 274 //----------------------------------------------------------------------- 275 // toString() 276 //----------------------------------------------------------------------- 277 @Test test_toString()278 public void test_toString() { 279 assertEquals(DayOfWeek.MONDAY.toString(), "MONDAY"); 280 assertEquals(DayOfWeek.TUESDAY.toString(), "TUESDAY"); 281 assertEquals(DayOfWeek.WEDNESDAY.toString(), "WEDNESDAY"); 282 assertEquals(DayOfWeek.THURSDAY.toString(), "THURSDAY"); 283 assertEquals(DayOfWeek.FRIDAY.toString(), "FRIDAY"); 284 assertEquals(DayOfWeek.SATURDAY.toString(), "SATURDAY"); 285 assertEquals(DayOfWeek.SUNDAY.toString(), "SUNDAY"); 286 } 287 288 //----------------------------------------------------------------------- 289 // generated methods 290 //----------------------------------------------------------------------- 291 @Test test_enum()292 public void test_enum() { 293 assertEquals(DayOfWeek.valueOf("MONDAY"), DayOfWeek.MONDAY); 294 assertEquals(DayOfWeek.values()[0], DayOfWeek.MONDAY); 295 } 296 297 } 298