AddSchedulerMVCActionCommand extends BaseMVCActionCommand { protected String getCronText( ActionRequest actionRequest, Calendar startCalendar, boolean timeZoneSensitive, int recurrenceType) { Calendar calendar = null; if (timeZoneSensitive) { calendar = CalendarFactoryUtil.getCalendar(); calendar.setTime(startCalendar.getTime()); } else { calendar = (Calendar)startCalendar.clone(); } Recurrence recurrence = new Recurrence( calendar, new Duration(1, 0, 0, 0), recurrenceType); recurrence.setWeekStart(Calendar.SUNDAY); if (recurrenceType == Recurrence.DAILY) { int dailyType = ParamUtil.getInteger(actionRequest, "dailyType"); if (dailyType == 0) { int dailyInterval = ParamUtil.getInteger( actionRequest, "dailyInterval", 1); recurrence.setInterval(dailyInterval); } else { recurrence.setByDay( new DayAndPosition[] { new DayAndPosition(Calendar.MONDAY, 0), new DayAndPosition(Calendar.TUESDAY, 0), new DayAndPosition(Calendar.WEDNESDAY, 0), new DayAndPosition(Calendar.THURSDAY, 0), new DayAndPosition(Calendar.FRIDAY, 0) }); } } else if (recurrenceType == Recurrence.WEEKLY) { int weeklyInterval = ParamUtil.getInteger( actionRequest, "weeklyInterval", 1); recurrence.setInterval(weeklyInterval); List<DayAndPosition> dayAndPositions = new ArrayList<>(); addWeeklyDayPos(actionRequest, dayAndPositions, Calendar.SUNDAY); addWeeklyDayPos(actionRequest, dayAndPositions, Calendar.MONDAY); addWeeklyDayPos(actionRequest, dayAndPositions, Calendar.TUESDAY); addWeeklyDayPos(actionRequest, dayAndPositions, Calendar.WEDNESDAY); addWeeklyDayPos(actionRequest, dayAndPositions, Calendar.THURSDAY); addWeeklyDayPos(actionRequest, dayAndPositions, Calendar.FRIDAY); addWeeklyDayPos(actionRequest, dayAndPositions, Calendar.SATURDAY); if (dayAndPositions.isEmpty()) { dayAndPositions.add(new DayAndPosition(Calendar.MONDAY, 0)); } recurrence.setByDay(dayAndPositions.toArray(new DayAndPosition[0])); } else if (recurrenceType == Recurrence.MONTHLY) { int monthlyType = ParamUtil.getInteger( actionRequest, "monthlyType"); if (monthlyType == 0) { int monthlyDay = ParamUtil.getInteger( actionRequest, "monthlyDay0", 1); recurrence.setByMonthDay(new int[] {monthlyDay}); int monthlyInterval = ParamUtil.getInteger( actionRequest, "monthlyInterval0", 1); recurrence.setInterval(monthlyInterval); } else { int monthlyDay = ParamUtil.getInteger( actionRequest, "monthlyDay1"); int monthlyPos = ParamUtil.getInteger( actionRequest, "monthlyPos"); recurrence.setByDay( new DayAndPosition[] { new DayAndPosition(monthlyDay, monthlyPos) }); int monthlyInterval = ParamUtil.getInteger( actionRequest, "monthlyInterval1", 1); recurrence.setInterval(monthlyInterval); } } else if (recurrenceType == Recurrence.YEARLY) { int yearlyType = ParamUtil.getInteger(actionRequest, "yearlyType"); if (yearlyType == 0) { int yearlyMonth = ParamUtil.getInteger( actionRequest, "yearlyMonth0"); recurrence.setByMonth(new int[] {yearlyMonth}); int yearlyDay = ParamUtil.getInteger( actionRequest, "yearlyDay0", 1); recurrence.setByMonthDay(new int[] {yearlyDay}); int yearlyInterval = ParamUtil.getInteger( actionRequest, "yearlyInterval0", 1); recurrence.setInterval(yearlyInterval); } else { int yearlyDay = ParamUtil.getInteger( actionRequest, "yearlyDay1"); int yearlyPos = ParamUtil.getInteger( actionRequest, "yearlyPos"); recurrence.setByDay( new DayAndPosition[] { new DayAndPosition(yearlyDay, yearlyPos) }); int yearlyMonth = ParamUtil.getInteger( actionRequest, "yearlyMonth1"); recurrence.setByMonth(new int[] {yearlyMonth}); int yearlyInterval = ParamUtil.getInteger( actionRequest, "yearlyInterval1", 1); recurrence.setInterval(yearlyInterval); } } return RecurrenceSerializer.toCronText(recurrence); }    }
@Test public void testDailyEveryTwoDaysRecurrence() { ActionRequest actionRequest = Mockito.mock(ActionRequest.class); whenActionRequestGetParameter(actionRequest, "dailyInterval", "2"); String actualCronText = _addSchedulerMVCActionCommand.getCronText( actionRequest, calendarOf(2017, 1, 1, 7, 0), true, Recurrence.DAILY); Assert.assertEquals("0 0 7 1/2 * ? *", actualCronText); }
@Test public void testDailyEveryWeekdayRecurrence() { ActionRequest actionRequest = Mockito.mock(ActionRequest.class); whenActionRequestGetParameter(actionRequest, "dailyType", "2"); String actualCronText = _addSchedulerMVCActionCommand.getCronText( actionRequest, calendarOf(2017, 1, 1, 7, 0), true, Recurrence.DAILY); Assert.assertEquals("0 0 7 ? * MON,TUE,WED,THU,FRI *", actualCronText); }
@Test public void testMonthlyDay12OfEveryTwoMonthsRecurrence() { ActionRequest actionRequest = Mockito.mock(ActionRequest.class); whenActionRequestGetParameter(actionRequest, "monthlyType", "0"); whenActionRequestGetParameter(actionRequest, "monthlyDay0", "12"); whenActionRequestGetParameter(actionRequest, "monthlyInterval0", "2"); String actualCronText = _addSchedulerMVCActionCommand.getCronText( actionRequest, calendarOf(2017, 1, 1, 13, 0), true, Recurrence.MONTHLY); Assert.assertEquals("0 0 13 12 1/2 ? *", actualCronText); }
@Test public void testMonthlyThirdTuesdayOfEveryThreeMonthsRecurrence() { ActionRequest actionRequest = Mockito.mock(ActionRequest.class); whenActionRequestGetParameter(actionRequest, "monthlyType", "1"); whenActionRequestGetParameter(actionRequest, "monthlyPos", "3"); whenActionRequestGetParameter(actionRequest, "monthlyDay1", "3"); whenActionRequestGetParameter(actionRequest, "monthlyInterval1", "3"); String actualCronText = _addSchedulerMVCActionCommand.getCronText( actionRequest, calendarOf(2017, 1, 1, 14, 0), true, Recurrence.MONTHLY); Assert.assertEquals("0 0 14 ? 1/3 TUE#3 *", actualCronText); }
@Test public void testWeeklyEverySundayRecurrence() { ActionRequest actionRequest = Mockito.mock(ActionRequest.class); whenActionRequestGetParameter(actionRequest, "weeklyDayPos1", "true"); String actualCronText = _addSchedulerMVCActionCommand.getCronText( actionRequest, calendarOf(2017, 1, 1, 12, 0), true, Recurrence.WEEKLY); Assert.assertEquals("0 0 12 ? * SUN/1 *", actualCronText); }
@Test public void testWeeklyMondayAndWednesdayRecurrence() { ActionRequest actionRequest = Mockito.mock(ActionRequest.class); whenActionRequestGetParameter(actionRequest, "weeklyDayPos2", "true"); whenActionRequestGetParameter(actionRequest, "weeklyDayPos4", "true"); whenActionRequestGetParameter(actionRequest, "weeklyInterval", "1"); String actualCronText = _addSchedulerMVCActionCommand.getCronText( actionRequest, calendarOf(2017, 1, 1, 12, 0), true, Recurrence.WEEKLY); Assert.assertEquals("0 0 12 ? * MON,WED/1 *", actualCronText); }
@Test public void testYearlyEveryTwoYearsThirdSundayOfMarchRecurrence() { ActionRequest actionRequest = Mockito.mock(ActionRequest.class); whenActionRequestGetParameter(actionRequest, "yearlyType", "1"); whenActionRequestGetParameter(actionRequest, "yearlyDay1", "1"); whenActionRequestGetParameter(actionRequest, "yearlyMonth1", "2"); whenActionRequestGetParameter(actionRequest, "yearlyPos", "3"); whenActionRequestGetParameter(actionRequest, "yearlyInterval1", "2"); String actualCronText = _addSchedulerMVCActionCommand.getCronText( actionRequest, calendarOf(2017, 1, 1, 12, 0), true, Recurrence.YEARLY); Assert.assertEquals("0 0 12 ? 3 SUN#3 2017/2", actualCronText); }
@Test public void testYearlyYearlyEveryTwoYearsJanuary12Recurrence() { ActionRequest actionRequest = Mockito.mock(ActionRequest.class); whenActionRequestGetParameter(actionRequest, "yearlyType", "0"); whenActionRequestGetParameter(actionRequest, "yearlyDay0", "12"); whenActionRequestGetParameter(actionRequest, "yearlyMonth0", "0"); whenActionRequestGetParameter(actionRequest, "yearlyInterval0", "2"); String actualCronText = _addSchedulerMVCActionCommand.getCronText( actionRequest, calendarOf(2017, 1, 1, 12, 0), true, Recurrence.YEARLY); Assert.assertEquals("0 0 12 12 1 ? 2017/2", actualCronText); }