From 6fbfd5b169eebcefc01ad04072828982f46c7c21 Mon Sep 17 00:00:00 2001 From: Earle Nietzel Date: Fri, 23 Aug 2024 14:26:58 -0400 Subject: [PATCH] SAK-50418 Calendar unit tests attempt to load CM when using ResourceLoader (#12819) --- .../impl/GenericCalendarImporter.java | 3 +- .../calendar/impl/PDFExportService.java | 8 ++--- .../calendar/impl/CSVReaderTest.java | 6 ++-- .../calendar/impl/PDFExportServiceTest.java | 2 ++ .../org/sakaiproject/util/CalendarUtil.java | 30 ++++--------------- .../sakaiproject/util/CalendarUtilTest.java | 12 +++++--- 6 files changed, 25 insertions(+), 36 deletions(-) diff --git a/calendar/calendar-impl/impl/src/java/org/sakaiproject/calendar/impl/GenericCalendarImporter.java b/calendar/calendar-impl/impl/src/java/org/sakaiproject/calendar/impl/GenericCalendarImporter.java index 326192e7b8dc..4333d9fc5d2d 100644 --- a/calendar/calendar-impl/impl/src/java/org/sakaiproject/calendar/impl/GenericCalendarImporter.java +++ b/calendar/calendar-impl/impl/src/java/org/sakaiproject/calendar/impl/GenericCalendarImporter.java @@ -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; @@ -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"); diff --git a/calendar/calendar-impl/impl/src/java/org/sakaiproject/calendar/impl/PDFExportService.java b/calendar/calendar-impl/impl/src/java/org/sakaiproject/calendar/impl/PDFExportService.java index 55c3948afb20..10a5a94a9f28 100644 --- a/calendar/calendar-impl/impl/src/java/org/sakaiproject/calendar/impl/PDFExportService.java +++ b/calendar/calendar-impl/impl/src/java/org/sakaiproject/calendar/impl/PDFExportService.java @@ -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); @@ -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. @@ -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(); @@ -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()); diff --git a/calendar/calendar-impl/impl/src/test/org/sakaiproject/calendar/impl/CSVReaderTest.java b/calendar/calendar-impl/impl/src/test/org/sakaiproject/calendar/impl/CSVReaderTest.java index 1cfb0d4e59ac..f1a14a6d64f2 100644 --- a/calendar/calendar-impl/impl/src/test/org/sakaiproject/calendar/impl/CSVReaderTest.java +++ b/calendar/calendar-impl/impl/src/test/org/sakaiproject/calendar/impl/CSVReaderTest.java @@ -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; @@ -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 @@ -51,6 +52,7 @@ public class CSVReaderTest { @Before public void setUp() { + GenericCalendarImporter.setRb(resourceLoader); csvReader = new CSVReader(); csvReader.setTimeService(timeService); } diff --git a/calendar/calendar-impl/impl/src/test/org/sakaiproject/calendar/impl/PDFExportServiceTest.java b/calendar/calendar-impl/impl/src/test/org/sakaiproject/calendar/impl/PDFExportServiceTest.java index 2a354728b0b9..2a6f77c9d2b9 100644 --- a/calendar/calendar-impl/impl/src/test/org/sakaiproject/calendar/impl/PDFExportServiceTest.java +++ b/calendar/calendar-impl/impl/src/test/org/sakaiproject/calendar/impl/PDFExportServiceTest.java @@ -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; @@ -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(); } diff --git a/calendar/calendar-util/util/src/java/org/sakaiproject/util/CalendarUtil.java b/calendar/calendar-util/util/src/java/org/sakaiproject/util/CalendarUtil.java index 9b656d93bb51..6ebba8364fac 100644 --- a/calendar/calendar-util/util/src/java/org/sakaiproject/util/CalendarUtil.java +++ b/calendar/calendar-util/util/src/java/org/sakaiproject/util/CalendarUtil.java @@ -45,6 +45,7 @@ import org.sakaiproject.time.api.UserTimeService; import com.google.common.annotations.VisibleForTesting; +import lombok.Setter; /** *

CalendarUtil is a bunch of utility methods added to a java Calendar object.

@@ -52,11 +53,11 @@ 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; @@ -88,7 +89,6 @@ public class CalendarUtil */ public CalendarUtil() { - rb = new ResourceLoader("calendar"); Locale locale = rb.getLocale(); m_calendar = Calendar.getInstance(locale); initDates(); @@ -100,7 +100,6 @@ public CalendarUtil() */ public CalendarUtil(Calendar calendar) { - rb = new ResourceLoader("calendar"); m_calendar = calendar; initDates(); @@ -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. @@ -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); @@ -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); diff --git a/calendar/calendar-util/util/src/test/org/sakaiproject/util/CalendarUtilTest.java b/calendar/calendar-util/util/src/test/org/sakaiproject/util/CalendarUtilTest.java index f6cd47dd2e99..b8ac69b7a30a 100644 --- a/calendar/calendar-util/util/src/test/org/sakaiproject/util/CalendarUtilTest.java +++ b/calendar/calendar-util/util/src/test/org/sakaiproject/util/CalendarUtilTest.java @@ -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 @@ -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);