Skip to content

Commit

Permalink
SAK-50418 Calendar unit tests attempt to load CM when using ResourceL…
Browse files Browse the repository at this point in the history
  • Loading branch information
ern authored Aug 23, 2024
1 parent 1479053 commit 6fbfd5b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import lombok.Getter;
Expand Down Expand Up @@ -95,7 +94,7 @@ public class GenericCalendarImporter implements CalendarImporterService
public static final String REPEAT_DEFAULT_COLUMN_HEADER = "Repeat";
public static final String ACTUAL_TIMERANGE = "ActualStartTime";

private static final ResourceLoader rb = new ResourceLoader("calendar");
@Setter private static ResourceLoader rb = new ResourceLoader("calendar");

public static DateTimeFormatter timeFormatter() {
return DateTimeFormatter.ofPattern("h:mm[:ss] a");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void generatePDF(Document doc, String xslFileName, OutputStream streamOut) {
Source src = new DOMSource(doc);

Calendar c = Calendar.getInstance(timeService.getLocalTimeZone(), rb.getLocale());
CalendarUtil calUtil = new CalendarUtil(c, rb);
CalendarUtil calUtil = new CalendarUtil(c);
String[] dayNames = calUtil.getCalendarDaysOfWeekNames(true);
String[] monthNames = calUtil.getCalendarMonthNames(true);

Expand Down Expand Up @@ -324,7 +324,7 @@ void generateXMLDocument(int scheduleType, Document doc, TimeRange timeRange, In
// of the month if we're in the month view.
//
if (scheduleType == CalendarService.MONTH_VIEW) {
CalendarUtil monthCalendar = new CalendarUtil(rb);
CalendarUtil monthCalendar = new CalendarUtil();

// Use the middle of the month since the start/end ranges
// may be in an adjacent month.
Expand Down Expand Up @@ -376,7 +376,7 @@ void generateXMLDocument(int scheduleType, Document doc, TimeRange timeRange, In

// Calculate the day of the week.
Calendar c = Calendar.getInstance(timeService.getLocalTimeZone(), rb.getLocale());
CalendarUtil cal = new CalendarUtil(c, rb);
CalendarUtil cal = new CalendarUtil(c);

Time date = currentTimeRange.firstTime();
TimeBreakdown breakdown = date.breakdownLocal();
Expand Down Expand Up @@ -555,7 +555,7 @@ private TimeRange shrinkTimeRangeToCurrentMonth(TimeRange expandedTimeRange){
TimeBreakdown somewhereInTheMonthBreakdown = somewhereInTheMonthTime.breakdownLocal();


CalendarUtil calendar = new CalendarUtil(rb);
CalendarUtil calendar = new CalendarUtil();

calendar.setDay(somewhereInTheMonthBreakdown.getYear(), somewhereInTheMonthBreakdown.getMonth(),
somewhereInTheMonthBreakdown.getDay());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.MockitoRule;
import org.sakaiproject.calendar.impl.readers.CSVReader;
import org.sakaiproject.exception.ImportException;
Expand All @@ -30,12 +32,11 @@

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.io.StringReader;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@RunWith(MockitoJUnitRunner.class)
public class CSVReaderTest {

@Rule
Expand All @@ -51,6 +52,7 @@ public class CSVReaderTest {

@Before
public void setUp() {
GenericCalendarImporter.setRb(resourceLoader);
csvReader = new CSVReader();
csvReader.setTimeService(timeService);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.sakaiproject.time.api.UserTimeService;
import org.sakaiproject.time.impl.BasicTimeService;
import org.sakaiproject.time.impl.UserLocaleServiceImpl;
import org.sakaiproject.util.CalendarUtil;
import org.sakaiproject.util.ResourceLoader;
import org.w3c.dom.Document;

Expand Down Expand Up @@ -100,6 +101,7 @@ public void setUp() throws ParserConfigurationException {
when(resourceLoader.getLocale()).thenReturn(Locale.ENGLISH);
when(resourceLoader.getString(anyString())).then(invocation -> invocation.getArgument(0));

CalendarUtil.setRb(resourceLoader);
pdfExportService = new PDFExportService(timeService, resourceLoader);
docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,19 @@
import org.sakaiproject.time.api.UserTimeService;

import com.google.common.annotations.VisibleForTesting;
import lombok.Setter;

/**
* <p>CalendarUtil is a bunch of utility methods added to a java Calendar object.</p>
*/
public class CalendarUtil
{
private Clock clock = Clock.systemDefaultZone();

@Setter private static ResourceLoader rb = new ResourceLoader("calendar");

/** The calendar object this is based upon. */
Calendar m_calendar = null;
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
ResourceLoader rb;

Date dateSunday = null;
Date dateMonday = null;
Expand Down Expand Up @@ -88,7 +89,6 @@ public class CalendarUtil
*/
public CalendarUtil()
{
rb = new ResourceLoader("calendar");
Locale locale = rb.getLocale();
m_calendar = Calendar.getInstance(locale);
initDates();
Expand All @@ -100,7 +100,6 @@ public CalendarUtil()
*/
public CalendarUtil(Calendar calendar)
{
rb = new ResourceLoader("calendar");
m_calendar = calendar;
initDates();

Expand All @@ -110,30 +109,13 @@ public CalendarUtil(Calendar calendar)
* Constructor for testing.
* @param clock the clock to use for the current time.
*/
public CalendarUtil(Clock clock, ResourceLoader rb)
public CalendarUtil(Clock clock)
{
this.clock = clock;
this.rb = rb;
m_calendar = getCalendarInstance();
initDates();
}

/**
* Constructor for testing.
*/
public CalendarUtil(Calendar calendar, ResourceLoader rb)
{
this.rb = rb;
m_calendar = calendar;
initDates();
}

public CalendarUtil(ResourceLoader rb) {
Locale locale = rb.getLocale();
m_calendar = Calendar.getInstance(locale);
initDates();
}

/**
* This creates a calendar based on the clock. This is to allow testing of the class.
* @return A calendar.
Expand Down Expand Up @@ -603,7 +585,7 @@ private static ZoneId getZoneId() {
// Used for tests
@VisibleForTesting
static String getLocalAMString(Instant now, ZoneId zoneId) {
Locale locale = new ResourceLoader().getLocale();
Locale locale = rb.getLocale();
//we need an AM date
ZonedDateTime zdt = ZonedDateTime.ofInstant(now, zoneId);
zdt = zdt.toLocalDate().atStartOfDay(zoneId);
Expand All @@ -622,7 +604,7 @@ public static String getLocalPMString() {
// Used for tests
@VisibleForTesting
static String getLocalPMString(Instant now, ZoneId zoneId) {
Locale locale = new ResourceLoader().getLocale();
Locale locale = rb.getLocale();
//we need an PM date
ZonedDateTime zdt = ZonedDateTime.ofInstant(now, zoneId);
zdt = zdt.toLocalDate().atStartOfDay(zoneId).plusHours(14);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,23 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class CalendarUtilTest {

@Mock private ResourceLoader resourceLoader;

@Before
public void setUp() {
// Need this so that developers in locales that don't use AM/PM have passing tests
// If you want to test this setting is working set it to Locale.JAPAN which should
// result in failing tests.
Locale.setDefault(Locale.ENGLISH);
Mockito.when(resourceLoader.getLocale()).thenReturn(Locale.ENGLISH);
CalendarUtil.setRb(resourceLoader);
}

@Test
Expand Down Expand Up @@ -84,10 +90,8 @@ public void testLocalPMStringStartOfDay() {
@Test
public void testDayOfMonthAtEnd() {
// This tests a problem with how Sakai was calculating the month when it was the last day of the month.
ResourceLoader rb = Mockito.mock(ResourceLoader.class);
Mockito.when(rb.getLocale()).thenReturn(Locale.ENGLISH);
Instant instant = Instant.parse("2007-08-31T09:30:00Z");
CalendarUtil util = new CalendarUtil(Clock.fixed(instant, ZoneOffset.ofHours(0)),rb);
CalendarUtil util = new CalendarUtil(Clock.fixed(instant, ZoneOffset.ofHours(0)));
String[] calendarMonthNames = util.getCalendarMonthNames(false);
String[] expected = new String[]{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
Assert.assertArrayEquals(expected, calendarMonthNames);
Expand Down

0 comments on commit 6fbfd5b

Please sign in to comment.