/*
* Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of JSR-310 nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package javax.time;
import static java.time.DayOfWeek.MONDAY;
import static java.time.DayOfWeek.SUNDAY;
import static java.time.DayOfWeek.WEDNESDAY;
import static java.time.calendrical.ChronoField.DAY_OF_WEEK;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertSame;
import java.time.DateTimeException;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.calendrical.ChronoField;
import java.time.calendrical.DateTime;
import java.time.calendrical.DateTimeAccessor;
import java.time.calendrical.DateTimeAccessor.Query;
import java.time.calendrical.DateTimeField;
import java.time.calendrical.JulianDayField;
import java.time.format.TextStyle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
* Test DayOfWeek.
*/
@Test
public class TCKDayOfWeek extends AbstractDateTimeTest {
@BeforeMethod
public void setUp() {
}
// -----------------------------------------------------------------------
@Override
protected List<DateTimeAccessor> samples() {
DateTimeAccessor[] array = { MONDAY, WEDNESDAY, SUNDAY, };
return Arrays.asList(array);
}
@Override
protected List<DateTimeField> validFields() {
DateTimeField[] array = { DAY_OF_WEEK, };
return Arrays.asList(array);
}
@Override
protected List<DateTimeField> invalidFields() {
List<DateTimeField> list = new ArrayList<>(Arrays.<DateTimeField> asList(ChronoField.values()));
list.removeAll(validFields());
list.add(JulianDayField.JULIAN_DAY);
list.add(JulianDayField.MODIFIED_JULIAN_DAY);
list.add(JulianDayField.RATA_DIE);
return list;
}
// -----------------------------------------------------------------------
@Test(groups = { "tck" })
public void test_factory_int_singleton() {
for (int i = 1; i <= 7; i++) {
DayOfWeek test = DayOfWeek.of(i);
assertEquals(test.getValue(), i);
assertSame(DayOfWeek.of(i), test);
}
}
@Test(expectedExceptions = DateTimeException.class, groups = { "tck" })
public void test_factory_int_valueTooLow() {
DayOfWeek.of(0);
}
@Test(expectedExceptions = DateTimeException.class, groups = { "tck" })
public void test_factory_int_valueTooHigh() {
DayOfWeek.of(8);
}
// -----------------------------------------------------------------------
@Test(groups = { "tck" })
public void test_factory_CalendricalObject() {
assertEquals(DayOfWeek.from(LocalDate.of(2011, 6, 6)), DayOfWeek.MONDAY);
}
@Test(expectedExceptions = DateTimeException.class, groups = { "tck" })
public void test_factory_CalendricalObject_invalid_noDerive() {
DayOfWeek.from(LocalTime.of(12, 30));
}
@Test(expectedExceptions = NullPointerException.class, groups = { "tck" })
public void test_factory_CalendricalObject_null() {
DayOfWeek.from((DateTimeAccessor) null);
}
// -----------------------------------------------------------------------
// query(Query)
// -----------------------------------------------------------------------
@Test(groups = { "tck" })
public void test_query_chrono() {
assertEquals(DayOfWeek.FRIDAY.query(Query.CHRONO), null);
}
@Test(groups = { "tck" })
public void test_query_zone() {
assertEquals(DayOfWeek.FRIDAY.query(Query.ZONE_ID), null);
}
@Test(groups = { "tck" })
public void test_query_timePrecision() {
assertEquals(DayOfWeek.FRIDAY.query(Query.TIME_PRECISION), null);
}
@Test(expectedExceptions = NullPointerException.class, groups = { "tck" })
public void test_query_null() {
DayOfWeek.FRIDAY.query(null);
}
// -----------------------------------------------------------------------
// getText()
// -----------------------------------------------------------------------
@Test(groups = { "tck" })
public void test_getText() {
assertEquals(DayOfWeek.MONDAY.getText(TextStyle.SHORT, Locale.US), "Mon");
}
@Test(expectedExceptions = NullPointerException.class, groups = { "tck" })
public void test_getText_nullStyle() {
DayOfWeek.MONDAY.getText(null, Locale.US);
}
@Test(expectedExceptions = NullPointerException.class, groups = { "tck" })
public void test_getText_nullLocale() {
DayOfWeek.MONDAY.getText(TextStyle.FULL, null);
}
// -----------------------------------------------------------------------
// plus(long), plus(long,unit)
// -----------------------------------------------------------------------
@DataProvider(name = "plus")
Object[][] data_plus() {
return new Object[][] { { 1, -8, 7 }, { 1, -7, 1 }, { 1, -6, 2 }, { 1, -5, 3 }, { 1, -4, 4 }, { 1, -3, 5 },
{ 1, -2, 6 }, { 1, -1, 7 }, { 1, 0, 1 }, { 1, 1, 2 }, { 1, 2, 3 }, { 1, 3, 4 }, { 1, 4, 5 }, { 1, 5, 6 },
{ 1, 6, 7 }, { 1, 7, 1 }, { 1, 8, 2 },
{ 1, 1, 2 }, { 2, 1, 3 }, { 3, 1, 4 }, { 4, 1, 5 }, { 5, 1, 6 }, { 6, 1, 7 }, { 7, 1, 1 },
{ 1, -1, 7 }, { 2, -1, 1 }, { 3, -1, 2 }, { 4, -1, 3 }, { 5, -1, 4 }, { 6, -1, 5 }, { 7, -1, 6 }, };
}
@Test(dataProvider = "plus", groups = { "tck" })
public void test_plus_long(int base, long amount, int expected) {
assertEquals(DayOfWeek.of(base).plus(amount), DayOfWeek.of(expected));
}
// -----------------------------------------------------------------------
// minus(long), minus(long,unit)
// -----------------------------------------------------------------------
@DataProvider(name = "minus")
Object[][] data_minus() {
return new Object[][] { { 1, -8, 2 }, { 1, -7, 1 }, { 1, -6, 7 }, { 1, -5, 6 }, { 1, -4, 5 }, { 1, -3, 4 },
{ 1, -2, 3 }, { 1, -1, 2 }, { 1, 0, 1 }, { 1, 1, 7 }, { 1, 2, 6 }, { 1, 3, 5 }, { 1, 4, 4 }, { 1, 5, 3 },
{ 1, 6, 2 }, { 1, 7, 1 }, { 1, 8, 7 }, };
}
@Test(dataProvider = "minus", groups = { "tck" })
public void test_minus_long(int base, long amount, int expected) {
assertEquals(DayOfWeek.of(base).minus(amount), DayOfWeek.of(expected));
}
// -----------------------------------------------------------------------
// doAdjustment()
// -----------------------------------------------------------------------
@Test(groups = { "tck" })
public void test_doAdjustment() {
assertEquals(DayOfWeek.MONDAY.doWithAdjustment(LocalDate.of(2012, 9, 2)), LocalDate.of(2012, 8, 27));
assertEquals(DayOfWeek.MONDAY.doWithAdjustment(LocalDate.of(2012, 9, 3)), LocalDate.of(2012, 9, 3));
assertEquals(DayOfWeek.MONDAY.doWithAdjustment(LocalDate.of(2012, 9, 4)), LocalDate.of(2012, 9, 3));
assertEquals(DayOfWeek.MONDAY.doWithAdjustment(LocalDate.of(2012, 9, 10)), LocalDate.of(2012, 9, 10));
assertEquals(DayOfWeek.MONDAY.doWithAdjustment(LocalDate.of(2012, 9, 11)), LocalDate.of(2012, 9, 10));
}
@Test(expectedExceptions = NullPointerException.class, groups = { "tck" })
public void test_doAdjustment_null() {
DayOfWeek.MONDAY.doWithAdjustment((DateTime) null);
}
// -----------------------------------------------------------------------
// toString()
// -----------------------------------------------------------------------
@Test(groups = { "tck" })
public void test_toString() {
assertEquals(DayOfWeek.MONDAY.toString(), "MONDAY");
assertEquals(DayOfWeek.TUESDAY.toString(), "TUESDAY");
assertEquals(DayOfWeek.WEDNESDAY.toString(), "WEDNESDAY");
assertEquals(DayOfWeek.THURSDAY.toString(), "THURSDAY");
assertEquals(DayOfWeek.FRIDAY.toString(), "FRIDAY");
assertEquals(DayOfWeek.SATURDAY.toString(), "SATURDAY");
assertEquals(DayOfWeek.SUNDAY.toString(), "SUNDAY");
}
// -----------------------------------------------------------------------
// generated methods
// -----------------------------------------------------------------------
@Test(groups = { "tck" })
public void test_enum() {
assertEquals(DayOfWeek.valueOf("MONDAY"), DayOfWeek.MONDAY);
assertEquals(DayOfWeek.values()[0], DayOfWeek.MONDAY);
}
}