예제 #1
0
  // -----------------------------------------------------------------------
  // 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);
  }
예제 #2
0
 // -----------------------------------------------------------------------
 // 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);
 }
예제 #3
0
 // -----------------------------------------------------------------------
 // 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);
 }
예제 #4
0
 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));
   }
 }
예제 #5
0
 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));
   }
 }
예제 #6
0
 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);
 }
예제 #7
0
 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);
 }
예제 #8
0
 @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);
   }
 }
예제 #9
0
  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
  }
예제 #10
0
  // -----------------------------------------------------------------------
  // 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);
  }
예제 #11
0
  // -----------------------------------------------------------------------
  // 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);
  }
예제 #12
0
 @Test(expectedExceptions = IllegalCalendarFieldValueException.class)
 public void test_convertDayOfWeek_int_8() {
   WeekRules.of(FRIDAY, 4).convertDayOfWeek(8);
 }
예제 #13
0
 @Test(expectedExceptions = NullPointerException.class)
 public void test_factory_ofLocale_null() {
   WeekRules.of((Locale) null);
 }
예제 #14
0
 // -----------------------------------------------------------------------
 // toString()
 // -----------------------------------------------------------------------
 public void test_toString() {
   assertEquals(WeekRules.of(MONDAY, 4).toString(), "WeekRules[MONDAY,4]");
   assertEquals(WeekRules.of(SUNDAY, 1).toString(), "WeekRules[SUNDAY,1]");
 }
예제 #15
0
 // -----------------------------------------------------------------------
 // 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);
 }
예제 #16
0
 @Test(expectedExceptions = NullPointerException.class)
 public void test_convertDayOfWeekDOW_null() {
   WeekRules.of(FRIDAY, 4).convertDayOfWeek(null);
 }
예제 #17
0
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void test_factory_of_tooLarge() {
   WeekRules.of(MONDAY, 8);
 }
예제 #18
0
 // -----------------------------------------------------------------------
 // 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)},
   };
 }
예제 #19
0
 @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);
 }
예제 #20
0
 // -----------------------------------------------------------------------
 // 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);
 }
예제 #21
0
 @Test(expectedExceptions = NullPointerException.class)
 public void test_factory_of_null() {
   WeekRules.of(null, 1);
 }