mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
ALF-9156 Refactor the repeating events code, and start on unit testing for it
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@29187 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
234
source/java/org/alfresco/repo/calendar/CalendarHelpersTest.java
Normal file
234
source/java/org/alfresco/repo/calendar/CalendarHelpersTest.java
Normal file
@@ -0,0 +1,234 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2005-2011 Alfresco Software Limited.
|
||||||
|
*
|
||||||
|
* This file is part of Alfresco
|
||||||
|
*
|
||||||
|
* Alfresco is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Alfresco is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package org.alfresco.repo.calendar;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.alfresco.model.ContentModel;
|
||||||
|
import org.alfresco.query.PagingRequest;
|
||||||
|
import org.alfresco.query.PagingResults;
|
||||||
|
import org.alfresco.repo.policy.BehaviourFilter;
|
||||||
|
import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
||||||
|
import org.alfresco.repo.site.SiteModel;
|
||||||
|
import org.alfresco.repo.transaction.RetryingTransactionHelper;
|
||||||
|
import org.alfresco.service.cmr.calendar.CalendarEntry;
|
||||||
|
import org.alfresco.service.cmr.calendar.CalendarEntryDTO;
|
||||||
|
import org.alfresco.service.cmr.calendar.CalendarRecurrenceHelper;
|
||||||
|
import org.alfresco.service.cmr.calendar.CalendarService;
|
||||||
|
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
||||||
|
import org.alfresco.service.cmr.repository.NodeRef;
|
||||||
|
import org.alfresco.service.cmr.repository.NodeService;
|
||||||
|
import org.alfresco.service.cmr.security.MutableAuthenticationService;
|
||||||
|
import org.alfresco.service.cmr.security.PermissionService;
|
||||||
|
import org.alfresco.service.cmr.security.PersonService;
|
||||||
|
import org.alfresco.service.cmr.site.SiteInfo;
|
||||||
|
import org.alfresco.service.cmr.site.SiteService;
|
||||||
|
import org.alfresco.service.cmr.site.SiteVisibility;
|
||||||
|
import org.alfresco.service.cmr.tagging.TaggingService;
|
||||||
|
import org.alfresco.util.ApplicationContextHelper;
|
||||||
|
import org.alfresco.util.PropertyMap;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test cases for the helpers relating to the {@link CalendarService},
|
||||||
|
* but which don't need a full repo
|
||||||
|
*
|
||||||
|
* @author Nick Burch
|
||||||
|
* @since 4.0
|
||||||
|
*/
|
||||||
|
public class CalendarHelpersTest
|
||||||
|
{
|
||||||
|
private static SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
|
||||||
|
@Test public void allDayDetection()
|
||||||
|
{
|
||||||
|
Calendar c20110719_0000 = Calendar.getInstance();
|
||||||
|
Calendar c20110719_1000 = Calendar.getInstance();
|
||||||
|
Calendar c20110720_0000 = Calendar.getInstance();
|
||||||
|
Calendar c20110721_0000 = Calendar.getInstance();
|
||||||
|
c20110719_0000.set(2011, 07, 19, 0, 0, 0);
|
||||||
|
c20110719_1000.set(2011, 07, 19, 1, 0, 0);
|
||||||
|
c20110720_0000.set(2011, 07, 20, 0, 0, 0);
|
||||||
|
c20110721_0000.set(2011, 07, 21, 0, 0, 0);
|
||||||
|
|
||||||
|
CalendarEntryDTO entry = new CalendarEntryDTO();
|
||||||
|
|
||||||
|
// Start and end at the same midnight
|
||||||
|
entry.setStart(c20110719_0000.getTime());
|
||||||
|
entry.setEnd( c20110719_0000.getTime());
|
||||||
|
assertTrue(CalendarEntryDTO.isAllDay(entry));
|
||||||
|
|
||||||
|
// Start and end at the next midnight
|
||||||
|
entry.setStart(c20110719_0000.getTime());
|
||||||
|
entry.setEnd( c20110720_0000.getTime());
|
||||||
|
assertTrue(CalendarEntryDTO.isAllDay(entry));
|
||||||
|
|
||||||
|
// Start and end at the midnight after
|
||||||
|
entry.setStart(c20110719_0000.getTime());
|
||||||
|
entry.setEnd( c20110721_0000.getTime());
|
||||||
|
assertTrue(CalendarEntryDTO.isAllDay(entry));
|
||||||
|
|
||||||
|
// One is midnight, one not
|
||||||
|
entry.setStart(c20110719_0000.getTime());
|
||||||
|
entry.setEnd( c20110719_1000.getTime());
|
||||||
|
assertFalse(CalendarEntryDTO.isAllDay(entry));
|
||||||
|
|
||||||
|
entry.setStart(c20110719_1000.getTime());
|
||||||
|
entry.setEnd( c20110720_0000.getTime());
|
||||||
|
assertFalse(CalendarEntryDTO.isAllDay(entry));
|
||||||
|
|
||||||
|
// Neither midnight
|
||||||
|
entry.setStart(c20110719_1000.getTime());
|
||||||
|
entry.setEnd( c20110719_1000.getTime());
|
||||||
|
assertFalse(CalendarEntryDTO.isAllDay(entry));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void dailyRecurrenceDates()
|
||||||
|
{
|
||||||
|
List<Date> dates = new ArrayList<Date>();
|
||||||
|
|
||||||
|
Calendar currentDate = Calendar.getInstance();
|
||||||
|
|
||||||
|
|
||||||
|
// Dates in the past, get nothing
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildDailyRecurrences(
|
||||||
|
currentDate, dates, null,
|
||||||
|
date(2011,7,10), date(2011,7,15),
|
||||||
|
true, 1
|
||||||
|
);
|
||||||
|
assertEquals(0, dates.size());
|
||||||
|
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildDailyRecurrences(
|
||||||
|
currentDate, dates, null,
|
||||||
|
date(2011,7,10), date(2011,7,15),
|
||||||
|
false, 1
|
||||||
|
);
|
||||||
|
assertEquals(0, dates.size());
|
||||||
|
|
||||||
|
|
||||||
|
// From today
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildDailyRecurrences(
|
||||||
|
currentDate, dates, null,
|
||||||
|
date(2011,7,19), date(2011,7,25),
|
||||||
|
true, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-07-19", dateFmt.format(dates.get(0)));
|
||||||
|
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildDailyRecurrences(
|
||||||
|
currentDate, dates, null,
|
||||||
|
date(2011,7,19), date(2011,7,25),
|
||||||
|
false, 1
|
||||||
|
);
|
||||||
|
assertEquals(6, dates.size());
|
||||||
|
assertEquals("2011-07-19", dateFmt.format(dates.get(0)));
|
||||||
|
assertEquals("2011-07-24", dateFmt.format(dates.get(5)));
|
||||||
|
|
||||||
|
|
||||||
|
// Dates in the future, goes from then
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildDailyRecurrences(
|
||||||
|
currentDate, dates, null,
|
||||||
|
date(2011,7,20), date(2011,7,30),
|
||||||
|
true, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-07-20", dateFmt.format(dates.get(0)));
|
||||||
|
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildDailyRecurrences(
|
||||||
|
currentDate, dates, null,
|
||||||
|
date(2011,7,20), date(2011,7,30),
|
||||||
|
false, 1
|
||||||
|
);
|
||||||
|
assertEquals(10, dates.size());
|
||||||
|
assertEquals("2011-07-20", dateFmt.format(dates.get(0)));
|
||||||
|
assertEquals("2011-07-29", dateFmt.format(dates.get(9)));
|
||||||
|
|
||||||
|
|
||||||
|
// With no end date but only first, check it behaves
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildDailyRecurrences(
|
||||||
|
currentDate, dates, null,
|
||||||
|
date(2011,7,19), null,
|
||||||
|
true, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-07-19", dateFmt.format(dates.get(0)));
|
||||||
|
|
||||||
|
dates.clear();
|
||||||
|
currentDate.set(2011,7-1,19,10,30);
|
||||||
|
RecurrenceHelper.buildDailyRecurrences(
|
||||||
|
currentDate, dates, null,
|
||||||
|
date(2011,7,20), null,
|
||||||
|
true, 1
|
||||||
|
);
|
||||||
|
assertEquals(1, dates.size());
|
||||||
|
assertEquals("2011-07-20", dateFmt.format(dates.get(0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Date date(int year, int month, int day)
|
||||||
|
{
|
||||||
|
return date(year, month, day, 0, 0);
|
||||||
|
}
|
||||||
|
private static Date date(int year, int month, int day, int hour, int minute)
|
||||||
|
{
|
||||||
|
Calendar c = Calendar.getInstance();
|
||||||
|
c.set(year, month-1, day, hour, minute, 0);
|
||||||
|
c.set(Calendar.MILLISECOND, 0);
|
||||||
|
return c.getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class RecurrenceHelper extends CalendarRecurrenceHelper
|
||||||
|
{
|
||||||
|
protected static void buildDailyRecurrences(Calendar currentDate, List<Date> dates,
|
||||||
|
Map<String,String> params, Date onOrAfter, Date until, boolean firstOnly, int interval)
|
||||||
|
{
|
||||||
|
CalendarRecurrenceHelper.buildDailyRecurrences(
|
||||||
|
currentDate, dates, params, onOrAfter, until, firstOnly, interval);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -188,7 +188,75 @@ public class CalendarRecurrenceHelper
|
|||||||
|
|
||||||
Calendar currentDate = Calendar.getInstance();
|
Calendar currentDate = Calendar.getInstance();
|
||||||
currentDate.setTime(entry.getStart());
|
currentDate.setTime(entry.getStart());
|
||||||
if ("WEEKLY".equals(freq))
|
|
||||||
|
if ("DAILY".equals(freq))
|
||||||
|
{
|
||||||
|
buildDailyRecurrences(currentDate, dates, params, onOrAfter, until, firstOnly, interval);
|
||||||
|
}
|
||||||
|
else if ("WEEKLY".equals(freq))
|
||||||
|
{
|
||||||
|
buildWeeklyRecurrences(currentDate, dates, params, onOrAfter, until, firstOnly, interval);
|
||||||
|
}
|
||||||
|
else if ("MONTHLY".equals(freq))
|
||||||
|
{
|
||||||
|
buildMonthlyRecurrences(currentDate, dates, params, onOrAfter, until, firstOnly, interval);
|
||||||
|
}
|
||||||
|
else if ("YEARLY".equals(freq))
|
||||||
|
{
|
||||||
|
buildYearlyRecurrences(currentDate, dates, params, onOrAfter, until, firstOnly, interval);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.warn("Unsupported recurrence frequency " + freq);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return what we've got
|
||||||
|
return dates;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.warn("No frequency found, possible invalid rule? " + recurrence);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void buildDailyRecurrences(Calendar currentDate, List<Date> dates,
|
||||||
|
Map<String,String> params, Date onOrAfter, Date until, boolean firstOnly, int interval)
|
||||||
|
{
|
||||||
|
// Nice and easy
|
||||||
|
while(currentDate.getTime().before(onOrAfter))
|
||||||
|
{
|
||||||
|
currentDate.add(Calendar.DATE, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(firstOnly)
|
||||||
|
{
|
||||||
|
// Save the first date, if valid
|
||||||
|
if(until != null)
|
||||||
|
{
|
||||||
|
if(currentDate.getTime().before(until))
|
||||||
|
{
|
||||||
|
dates.add(currentDate.getTime());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dates.add(currentDate.getTime());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Run until the end
|
||||||
|
while(currentDate.getTime().before(until))
|
||||||
|
{
|
||||||
|
dates.add(currentDate.getTime());
|
||||||
|
currentDate.add(Calendar.DATE, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void buildWeeklyRecurrences(Calendar currentDate, List<Date> dates,
|
||||||
|
Map<String,String> params, Date onOrAfter, Date until, boolean firstOnly, int interval)
|
||||||
{
|
{
|
||||||
// Get a sorted list of the days it applies to
|
// Get a sorted list of the days it applies to
|
||||||
List<Integer> daysOfWeek = new ArrayList<Integer>();
|
List<Integer> daysOfWeek = new ArrayList<Integer>();
|
||||||
@@ -250,41 +318,8 @@ public class CalendarRecurrenceHelper
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else if ("DAILY".equals(freq))
|
protected static void buildMonthlyRecurrences(Calendar currentDate, List<Date> dates,
|
||||||
{
|
Map<String,String> params, Date onOrAfter, Date until, boolean firstOnly, int interval)
|
||||||
// Nice and easy
|
|
||||||
while(currentDate.before(onOrAfter))
|
|
||||||
{
|
|
||||||
currentDate.add(Calendar.DATE, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(firstOnly)
|
|
||||||
{
|
|
||||||
// Save the first date, if valid
|
|
||||||
if(until != null)
|
|
||||||
{
|
|
||||||
if(currentDate.before(until))
|
|
||||||
{
|
|
||||||
dates.add(currentDate.getTime());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
dates.add(currentDate.getTime());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Run until the end
|
|
||||||
while(currentDate.before(until))
|
|
||||||
{
|
|
||||||
dates.add(currentDate.getTime());
|
|
||||||
currentDate.add(Calendar.DATE, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else if ("MONTHLY".equals(freq))
|
|
||||||
{
|
{
|
||||||
if (params.get("BYMONTHDAY") != null)
|
if (params.get("BYMONTHDAY") != null)
|
||||||
{
|
{
|
||||||
@@ -365,7 +400,8 @@ public class CalendarRecurrenceHelper
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else if ("YEARLY".equals(freq))
|
protected static void buildYearlyRecurrences(Calendar currentDate, List<Date> dates,
|
||||||
|
Map<String,String> params, Date onOrAfter, Date until, boolean firstOnly, int interval)
|
||||||
{
|
{
|
||||||
int month = Integer.parseInt(params.get("BYMONTH"));
|
int month = Integer.parseInt(params.get("BYMONTH"));
|
||||||
|
|
||||||
@@ -419,20 +455,7 @@ public class CalendarRecurrenceHelper
|
|||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
logger.warn("Unsupported recurrence frequency " + freq);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return what we've got
|
|
||||||
return dates;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
logger.warn("No frequency found, possible invalid rule? " + recurrence);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void addMonthToDayOfMonth(Calendar c, int dayOfMonth)
|
private static void addMonthToDayOfMonth(Calendar c, int dayOfMonth)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user