// ----------------------------------------------------------------------- // factory locale // ----------------------------------------------------------------------- public void test_factory_ofLocale() { assertEquals(WeekRules.of(Locale.US).getFirstDayOfWeek(), SUNDAY); assertEquals(1, WeekRules.of(Locale.US).getMinimalDaysInFirstWeek(), 1); assertEquals(WeekRules.of(Locale.FRANCE).getFirstDayOfWeek(), MONDAY); assertEquals(WeekRules.of(Locale.FRANCE).getMinimalDaysInFirstWeek(), 4); }
// ----------------------------------------------------------------------- // equals() // ----------------------------------------------------------------------- @Test(dataProvider = "compare") public void test_equals(WeekRules a, WeekRules b) { assertEquals(a.equals(a), true); assertEquals(b.equals(b), true); assertEquals(a.equals(b), false); assertEquals(b.equals(a), false); }
// ----------------------------------------------------------------------- // serialization // ----------------------------------------------------------------------- public void test_serialization() throws Exception { WeekRules orginal = WeekRules.of(MONDAY, 3); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(baos); out.writeObject(orginal); out.close(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream in = new ObjectInputStream(bais); WeekRules ser = (WeekRules) in.readObject(); assertEquals(WeekRules.of(MONDAY, 3), ser); }
public void test_weekOfMonth_buildDate_sun7() { // July 2011 starts on a Friday // Fri/Sat 1st/2nd are week 0 WeekRules wr = WeekRules.of(SUNDAY, 7); for (int i = 1; i <= 31; i++) { int w = MathUtils.floorDiv(i - 3, 7) + 1; int d = MathUtils.floorMod(i - 3, 7) + 1; CalendricalEngine engine = CalendricalEngine.merge( YearMonth.of(2011, JULY), wr.weekOfMonth().field(w), wr.dayOfWeek().field(d)); assertEquals(engine.derive(LocalDate.rule()), LocalDate.of(2011, JULY, i)); } }
public void test_weekOfMonth_getValue_sun7() { // July 2011 starts on a Friday // Fri/Sat 1st/2nd are week 0 DateTimeRule rule = WeekRules.of(SUNDAY, 7).weekOfMonth(); for (int i = 1; i <= 31; i++) { int w = MathUtils.floorDiv(i - 3, 7) + 1; // 3rd is start of week 1 assertEquals(rule.getValue(LocalDate.of(2011, 7, i)), rule.field(w)); } }
public void test_convertDayOfWeek_int_FridayBased() { assertEquals(WeekRules.of(FRIDAY, 4).convertDayOfWeek(4), MONDAY); assertEquals(WeekRules.of(FRIDAY, 4).convertDayOfWeek(5), TUESDAY); assertEquals(WeekRules.of(FRIDAY, 4).convertDayOfWeek(6), WEDNESDAY); assertEquals(WeekRules.of(FRIDAY, 4).convertDayOfWeek(7), THURSDAY); assertEquals(WeekRules.of(FRIDAY, 4).convertDayOfWeek(1), FRIDAY); assertEquals(WeekRules.of(FRIDAY, 4).convertDayOfWeek(2), SATURDAY); assertEquals(WeekRules.of(FRIDAY, 4).convertDayOfWeek(3), SUNDAY); }
public void test_convertDayOfWeekDOW_FridayBased() { assertEquals(WeekRules.of(FRIDAY, 4).convertDayOfWeek(MONDAY), 4); assertEquals(WeekRules.of(FRIDAY, 4).convertDayOfWeek(TUESDAY), 5); assertEquals(WeekRules.of(FRIDAY, 4).convertDayOfWeek(WEDNESDAY), 6); assertEquals(WeekRules.of(FRIDAY, 4).convertDayOfWeek(THURSDAY), 7); assertEquals(WeekRules.of(FRIDAY, 4).convertDayOfWeek(FRIDAY), 1); assertEquals(WeekRules.of(FRIDAY, 4).convertDayOfWeek(SATURDAY), 2); assertEquals(WeekRules.of(FRIDAY, 4).convertDayOfWeek(SUNDAY), 3); }
@Test(dataProvider = "allRules") public void test_weekOfMonth_getValue_crossCheckCalendar(DayOfWeek dow, int minimalDays) { DateTimeRule rule = WeekRules.of(dow, minimalDays).weekOfMonth(); GregorianCalendar gcal = new GregorianCalendar(); gcal.setFirstDayOfWeek(dow == SUNDAY ? 1 : dow.getValue() + 1); gcal.setMinimalDaysInFirstWeek(minimalDays); gcal.set(2011, Calendar.JANUARY, 1); LocalDate date = LocalDate.of(2011, 1, 1); for (int i = 1; i <= 365; i++) { assertEquals(rule.getValue(date).getValue(), gcal.get(Calendar.WEEK_OF_MONTH)); gcal.add(Calendar.DAY_OF_MONTH, 1); date = date.plusDays(1); } }
public void test_dayOfWeek_getValue_sun1() { // July 2011 starts on a Friday DateTimeRule rule = WeekRules.of(SUNDAY, 1).dayOfWeek(); for (int i = 1; i <= 31; i++) { int stdDOW = (i + 4 - 1) % 7 + 1; DayOfWeek stdObj = DayOfWeek.of(stdDOW); int relDOW = (i + 5 - 1) % 7 + 1; DateTimeField relField = rule.field(relDOW); assertEquals(rule.getValue(LocalDate.of(2011, 7, i)), relField); assertEquals(rule.getValue(stdObj), relField); // assertEquals(DayOfWeek.from(relField), stdObj); // TODO: engine change needed } // assertEquals(rule.field(-7), rule.derive(DAY_OF_WEEK.field(-8))); // 2prev Mon // // assertEquals(rule.field(-6), rule.derive(DAY_OF_WEEK.field(0))); // prev Sun // assertEquals(rule.field(-5), rule.derive(DAY_OF_WEEK.field(-6))); // prev Mon // assertEquals(rule.field(-4), rule.derive(DAY_OF_WEEK.field(-5))); // prev Tue // assertEquals(rule.field(-3), rule.derive(DAY_OF_WEEK.field(-4))); // prev Wed // assertEquals(rule.field(-2), rule.derive(DAY_OF_WEEK.field(-3))); // prev Thu // assertEquals(rule.field(-1), rule.derive(DAY_OF_WEEK.field(-2))); // prev Fri // assertEquals(rule.field(0), rule.derive(DAY_OF_WEEK.field(-1))); // prev Sat // TODO: these tests used to work (normalizer) // assertEquals(rule.field(1), rule.derive(DAY_OF_WEEK.field(7))); // Sun // assertEquals(rule.field(2), rule.derive(DAY_OF_WEEK.field(1))); // Mon // assertEquals(rule.field(3), rule.derive(DAY_OF_WEEK.field(2))); // Tue // assertEquals(rule.field(4), rule.derive(DAY_OF_WEEK.field(3))); // Wed // assertEquals(rule.field(5), rule.derive(DAY_OF_WEEK.field(4))); // Thu // assertEquals(rule.field(6), rule.derive(DAY_OF_WEEK.field(5))); // Fri // assertEquals(rule.field(7), rule.derive(DAY_OF_WEEK.field(6))); // Sat // assertEquals(null, rule.derive(DAY_OF_WEEK.field(0))); // assertEquals(null, rule.derive(DAY_OF_WEEK.field(8))); // assertEquals(rule.field(8), rule.derive(DAY_OF_WEEK.field(14))); // next Sun // assertEquals(rule.field(9), rule.derive(DAY_OF_WEEK.field(8))); // next Mon // assertEquals(rule.field(10), rule.derive(DAY_OF_WEEK.field(9))); // next Tue // assertEquals(rule.field(11), rule.derive(DAY_OF_WEEK.field(10))); // next Wed // assertEquals(rule.field(12), rule.derive(DAY_OF_WEEK.field(11))); // next Thu // assertEquals(rule.field(13), rule.derive(DAY_OF_WEEK.field(12))); // next Fri // assertEquals(rule.field(14), rule.derive(DAY_OF_WEEK.field(13))); // next Sat // // assertEquals(rule.field(15), rule.derive(DAY_OF_WEEK.field(21))); // 2next Sun }
// ----------------------------------------------------------------------- // weekOfMonth() // ----------------------------------------------------------------------- public void test_weekOfMonth_sun1() { DateTimeRule rule = WeekRules.of(SUNDAY, 1).weekOfMonth(); assertEquals(rule.toString(), "WeekOfMonth-WeekRules[SUNDAY,1]"); assertEquals(rule.getBaseRule(), rule); assertEquals(rule.convertToPeriod(-1), -1); assertEquals(rule.convertToPeriod(0), 0); assertEquals(rule.convertToPeriod(1), 1); assertEquals(rule.convertToPeriod(2), 2); assertEquals(rule.convertToPeriod(5), 5); assertEquals(rule.convertToPeriod(6), 6); assertEquals(rule.convertFromPeriod(-1), -1); assertEquals(rule.convertFromPeriod(0), 0); assertEquals(rule.convertFromPeriod(1), 1); assertEquals(rule.convertFromPeriod(2), 2); assertEquals(rule.convertFromPeriod(5), 5); assertEquals(rule.convertFromPeriod(6), 6); }
// ----------------------------------------------------------------------- // dayOfWeek() // ----------------------------------------------------------------------- public void test_dayOfWeek_sun1() { DateTimeRule rule = WeekRules.of(SUNDAY, 1).dayOfWeek(); assertEquals(rule.toString(), "DayOfWeek-WeekRules[SUNDAY,1]"); assertEquals(rule.getBaseRule(), rule); assertEquals(rule.convertToPeriod(1), 0); // Sun assertEquals(rule.convertToPeriod(2), 1); // Mon assertEquals(rule.convertToPeriod(3), 2); // Tue assertEquals(rule.convertToPeriod(4), 3); // Wed assertEquals(rule.convertToPeriod(5), 4); // Thu assertEquals(rule.convertToPeriod(6), 5); // Fri assertEquals(rule.convertToPeriod(7), 6); // Sat assertEquals(rule.convertToPeriod(8), 7); assertEquals(rule.convertToPeriod(0), -1); assertEquals(rule.convertFromPeriod(0), 1); // Sun assertEquals(rule.convertFromPeriod(6), 7); // Sat assertEquals(rule.convertFromPeriod(-1), 0); assertEquals(rule.convertFromPeriod(7), 8); }
@Test(expectedExceptions = IllegalCalendarFieldValueException.class) public void test_convertDayOfWeek_int_8() { WeekRules.of(FRIDAY, 4).convertDayOfWeek(8); }
@Test(expectedExceptions = NullPointerException.class) public void test_factory_ofLocale_null() { WeekRules.of((Locale) null); }
// ----------------------------------------------------------------------- // toString() // ----------------------------------------------------------------------- public void test_toString() { assertEquals(WeekRules.of(MONDAY, 4).toString(), "WeekRules[MONDAY,4]"); assertEquals(WeekRules.of(SUNDAY, 1).toString(), "WeekRules[SUNDAY,1]"); }
// ----------------------------------------------------------------------- // hashCode() // ----------------------------------------------------------------------- @Test(dataProvider = "compare") public void test_hashCode(WeekRules a, WeekRules b) { assertEquals(a.hashCode() == a.hashCode(), true); assertEquals(b.hashCode() == b.hashCode(), true); assertEquals(a.hashCode() == b.hashCode(), false); }
@Test(expectedExceptions = NullPointerException.class) public void test_convertDayOfWeekDOW_null() { WeekRules.of(FRIDAY, 4).convertDayOfWeek(null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void test_factory_of_tooLarge() { WeekRules.of(MONDAY, 8); }
// ----------------------------------------------------------------------- // compareTo() // ----------------------------------------------------------------------- @DataProvider(name = "compare") public Object[][] data_compare() { return new Object[][] { {WeekRules.of(MONDAY, 1), WeekRules.of(MONDAY, 2)}, {WeekRules.of(MONDAY, 1), WeekRules.of(MONDAY, 3)}, {WeekRules.of(MONDAY, 1), WeekRules.of(MONDAY, 7)}, {WeekRules.of(MONDAY, 1), WeekRules.of(TUESDAY, 1)}, {WeekRules.of(MONDAY, 7), WeekRules.of(TUESDAY, 1)}, {WeekRules.of(TUESDAY, 6), WeekRules.of(TUESDAY, 7)}, {WeekRules.of(MONDAY, 1), WeekRules.of(SUNDAY, 7)}, }; }
@Test(dataProvider = "compare") public void test_compareTo(WeekRules a, WeekRules b) { assertEquals(a.compareTo(a), 0); assertEquals(a.compareTo(b) < 0, true); assertEquals(b.compareTo(a) > 0, true); }
// ----------------------------------------------------------------------- // factory of // ----------------------------------------------------------------------- @Test(dataProvider = "allRules") public void test_factory_of(DayOfWeek dow, int minimalDays) { WeekRules rules = WeekRules.of(dow, minimalDays); assertEquals(rules.getFirstDayOfWeek(), dow); assertEquals(rules.getMinimalDaysInFirstWeek(), minimalDays); }
@Test(expectedExceptions = NullPointerException.class) public void test_factory_of_null() { WeekRules.of(null, 1); }