From 20edcae7c4706e48001f5d545af26d2db8fc04c1 Mon Sep 17 00:00:00 2001 From: Tom Page Date: Tue, 9 Jan 2018 13:40:31 +0000 Subject: [PATCH] A couple of minor fixes. Fix method signature to refer to the precise exception thrown. Use Instant from Java 8 rather than DateUtils.truncate. --- .../RecordsManagementAuditServiceImpl.java | 10 ++++---- ...rdsManagementAuditServiceImplUnitTest.java | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/audit/RecordsManagementAuditServiceImpl.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/audit/RecordsManagementAuditServiceImpl.java index 138d3394a4..c444c9f8ed 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/audit/RecordsManagementAuditServiceImpl.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/audit/RecordsManagementAuditServiceImpl.java @@ -37,6 +37,7 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Serializable; import java.io.Writer; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; @@ -46,6 +47,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; +import javax.transaction.SystemException; import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.model.ContentModel; @@ -54,7 +56,6 @@ import org.alfresco.module.org_alfresco_module_rm.action.RecordsManagementAction import org.alfresco.module.org_alfresco_module_rm.audit.event.AuditEvent; import org.alfresco.module.org_alfresco_module_rm.capability.CapabilityService; import org.alfresco.module.org_alfresco_module_rm.fileplan.FilePlanService; -import org.alfresco.module.org_alfresco_module_rm.model.rma.type.RmSiteType; import org.alfresco.repo.audit.AuditComponent; import org.alfresco.repo.audit.model.AuditApplication; import org.alfresco.repo.content.MimetypeMap; @@ -727,7 +728,7 @@ public class RecordsManagementAuditServiceImpl extends AbstractLifecycleBean * * @param auditedNodes details of the nodes that were modified */ - private void auditInTxn(Set auditedNodes) throws Throwable + private void auditInTxn(Set auditedNodes) throws SystemException { // Go through all the audit information and audit it boolean auditedSomething = false; @@ -1137,9 +1138,10 @@ public class RecordsManagementAuditServiceImpl extends AbstractLifecycleBean * @param date The date for which the start should be calculated. * @return Returns the start of the given date. */ - private Date getStartOfDay(Date date) + protected Date getStartOfDay(Date date) { - return DateUtils.truncate(date == null ? new Date() : date, Calendar.DATE); + date = (date == null ? new Date() : date); + return Date.from(date.toInstant().truncatedTo(ChronoUnit.DAYS)); } /** diff --git a/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/audit/RecordsManagementAuditServiceImplUnitTest.java b/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/audit/RecordsManagementAuditServiceImplUnitTest.java index 2f1b39fa0c..6476b196d4 100644 --- a/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/audit/RecordsManagementAuditServiceImplUnitTest.java +++ b/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/audit/RecordsManagementAuditServiceImplUnitTest.java @@ -37,6 +37,7 @@ import static org.alfresco.module.org_alfresco_module_rm.fileplan.FilePlanServic import static org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel.TYPE_RM_SITE; import static org.alfresco.module.org_alfresco_module_rm.model.rma.type.RmSiteType.DEFAULT_SITE_NAME; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.times; @@ -46,7 +47,9 @@ import static org.mockito.MockitoAnnotations.initMocks; import java.io.IOException; import java.io.Writer; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; @@ -173,4 +176,25 @@ public class RecordsManagementAuditServiceImplUnitTest // Check that the event of viewing the audit log was itself audited. verify(mockAuditComponent).recordAuditValues(eq(RM_AUDIT_PATH_ROOT), any(Map.class)); } + + /** Check that passing null to getStartOfDay doesn't result in null being returned. */ + @Test + public void testGetStartOfDay_null() + { + Date startOfDay = recordsManagementAuditServiceImpl.getStartOfDay(null); + assertNotNull("Expected date to be created by method.", startOfDay); + } + + /** Check that any time component passed to getStartOfDay is not included in the response. */ + @Test + public void testGetStartOfDay_timeDiscarded() throws Exception + { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS"); + Date date = format.parse("2001-02-03 04:05:06.789"); + + // Call the method under test. + Date startOfDay = recordsManagementAuditServiceImpl.getStartOfDay(date); + + assertEquals("Unexpected date truncation.", format.parse("2001-02-03 00:00:00.000"), startOfDay); + } }