diff --git a/repository/src/main/java/org/alfresco/repo/usage/RepoUsageComponentImpl.java b/repository/src/main/java/org/alfresco/repo/usage/RepoUsageComponentImpl.java index 8f5acfeec0..f0b22b84ac 100644 --- a/repository/src/main/java/org/alfresco/repo/usage/RepoUsageComponentImpl.java +++ b/repository/src/main/java/org/alfresco/repo/usage/RepoUsageComponentImpl.java @@ -456,12 +456,16 @@ public class RepoUsageComponentImpl implements RepoUsageComponent } } - // Check the license expiry + // Check the license expiration Long licenseExpiryDate = restrictions.getLicenseExpiryDate(); if (licenseExpiryDate != null) { - int remainingDays = DateUtil.calculateDays(System.currentTimeMillis(), licenseExpiryDate); - if (remainingDays <= 0) + //For informational purposes, get the remaining number of days, counting from the beginning of the day of each date (now and expiration date) + long remainingDays = DateUtil.calculateDays(System.currentTimeMillis(), licenseExpiryDate); + + //Get exact number of milliseconds between license expiration time and now to see if is expired + long remainingMills = DateUtil.calculateMs(System.currentTimeMillis(), licenseExpiryDate); + if (remainingMills <= 0) { errors.add(I18NUtil.getMessage("system.usage.err.limit_license_expired")); level = RepoUsageLevel.LOCKED_DOWN; diff --git a/repository/src/main/java/org/alfresco/util/DateUtil.java b/repository/src/main/java/org/alfresco/util/DateUtil.java index 963d5a1831..f56d681d83 100644 --- a/repository/src/main/java/org/alfresco/util/DateUtil.java +++ b/repository/src/main/java/org/alfresco/util/DateUtil.java @@ -71,4 +71,34 @@ public class DateUtil } return days; } + + /** + * Calculate the number of milliseconds between start and end dates based on the default timezone. + * If the end date is before the start date, the returned value is negative. + * + * @param startMs start date in milliseconds + * @param endMs end date in milliseconds + * @return number milliseconds between + */ + public static long calculateMs(long startMs, long endMs) + { + DateTime startDateTime = new DateTime(startMs); + DateTime endDateTime = new DateTime(endMs); + + long milliseconds; + if (endDateTime.isBefore(startDateTime)) + { + Interval interval = new Interval(endDateTime, startDateTime); + Period period = interval.toPeriod(PeriodType.millis()); + milliseconds = 0 - period.getMillis(); + } + else + { + Interval interval = new Interval(startDateTime, endDateTime); + Period period = interval.toPeriod(PeriodType.millis()); + milliseconds = period.getMillis(); + } + return milliseconds; + } + } diff --git a/repository/src/test/java/org/alfresco/repo/usage/RepoUsageComponentTest.java b/repository/src/test/java/org/alfresco/repo/usage/RepoUsageComponentTest.java index bd32f4de1c..646daa94c9 100644 --- a/repository/src/test/java/org/alfresco/repo/usage/RepoUsageComponentTest.java +++ b/repository/src/test/java/org/alfresco/repo/usage/RepoUsageComponentTest.java @@ -25,9 +25,9 @@ */ package org.alfresco.repo.usage; -import javax.transaction.UserTransaction; +import java.util.concurrent.TimeUnit; -import junit.framework.TestCase; +import javax.transaction.UserTransaction; import org.alfresco.repo.lock.JobLockService; import org.alfresco.repo.security.authentication.AuthenticationUtil; @@ -37,6 +37,7 @@ import org.alfresco.service.cmr.admin.RepoUsage; import org.alfresco.service.cmr.admin.RepoUsage.LicenseMode; import org.alfresco.service.cmr.admin.RepoUsage.UsageType; import org.alfresco.service.cmr.admin.RepoUsageStatus; +import org.alfresco.service.cmr.admin.RepoUsageStatus.RepoUsageLevel; import org.alfresco.service.transaction.TransactionService; import org.alfresco.test_category.OwnJVMTestsCategory; import org.alfresco.util.ApplicationContextHelper; @@ -48,6 +49,8 @@ import org.junit.experimental.categories.Category; import org.junit.runners.MethodSorters; import org.springframework.context.ApplicationContext; +import junit.framework.TestCase; + /** * Tests {@link RepoUsageComponent} * @@ -236,6 +239,64 @@ public class RepoUsageComponentTest extends TestCase RepoUsage usage = getUsage(); } + public void testLicenceHoursBeforeExpiration() throws Exception + { + // Update usage + updateUsage(UsageType.USAGE_ALL); + + // Set the restrictions for license to expire in 6 hours + RepoUsage restrictions = new RepoUsage( + System.currentTimeMillis(), + 5000L, + 100000L, + LicenseMode.TEAM, + System.currentTimeMillis() + TimeUnit.HOURS.toMillis(6), + false); + repoUsageComponent.setRestrictions(restrictions); + + // Update use + updateUsage(UsageType.USAGE_ALL); + + // Get the usage + RepoUsage usage = getUsage(); + + // Check + assertFalse("Usage is in read-only mode",usage.isReadOnly()); + assertTrue("System is in read-only mode",transactionService.getAllowWrite()); + + RepoUsageStatus status = repoUsageComponent.getUsageStatus(); + assertEquals("System is not at Warning All Level",status.getLevel(),RepoUsageLevel.WARN_ALL); + } + + public void testLicenceMinutesAfterExpiration() throws Exception + { + // Update usage + updateUsage(UsageType.USAGE_ALL); + + // Set the restrictions for license to expire in 6 hours + RepoUsage restrictions = new RepoUsage( + System.currentTimeMillis(), + 5000L, + 100000L, + LicenseMode.TEAM, + System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(1), + false); + repoUsageComponent.setRestrictions(restrictions); + + // Update use + updateUsage(UsageType.USAGE_ALL); + + // Get the usage + RepoUsage usage = getUsage(); + + // Check we are in read-only mode + assertTrue("Usage is not in read-only mode",usage.isReadOnly()); + assertFalse("System is not in read-only mode",transactionService.getAllowWrite()); + + RepoUsageStatus status = repoUsageComponent.getUsageStatus(); + assertEquals("System is not at Locked Level",status.getLevel(),RepoUsageLevel.LOCKED_DOWN); + } + /** * Check that concurrent updates are prevented *