diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/config/ScheduleConfig.java b/gerrit-server/src/main/java/com/google/gerrit/server/config/ScheduleConfig.java index 9ffcdc64b8..66f0171472 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/config/ScheduleConfig.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/config/ScheduleConfig.java @@ -47,11 +47,22 @@ public class ScheduleConfig { this(rc, section, subsection, DateTime.now()); } + public ScheduleConfig(Config rc, String section, String subsection, + String keyInterval, String keyStartTime) { + this(rc, section, subsection, keyInterval, keyStartTime, DateTime.now()); + } + /* For testing we need to be able to pass now */ ScheduleConfig(Config rc, String section, String subsection, DateTime now) { - this.interval = interval(rc, section, subsection); + this(rc, section, subsection, KEY_INTERVAL, KEY_STARTTIME, now); + } + + private ScheduleConfig(Config rc, String section, String subsection, + String keyInterval, String keyStartTime, DateTime now) { + this.interval = interval(rc, section, subsection, keyInterval); if (interval > 0) { - this.initialDelay = initialDelay(rc, section, subsection, now, interval); + this.initialDelay = initialDelay(rc, section, subsection, keyStartTime, now, + interval); } else { this.initialDelay = interval; } @@ -65,20 +76,21 @@ public class ScheduleConfig { return interval; } - private static long interval(Config rc, String section, String subsection) { + private static long interval(Config rc, String section, String subsection, + String keyInterval) { long interval = MISSING_CONFIG; try { interval = - ConfigUtil.getTimeUnit(rc, section, subsection, KEY_INTERVAL, -1, + ConfigUtil.getTimeUnit(rc, section, subsection, keyInterval, -1, TimeUnit.MILLISECONDS); if (interval == MISSING_CONFIG) { log.info(MessageFormat.format( "{0} schedule parameter \"{0}.{1}\" is not configured", section, - KEY_INTERVAL)); + keyInterval)); } } catch (IllegalArgumentException e) { log.error(MessageFormat.format( - "Invalid {0} schedule parameter \"{0}.{1}\"", section, KEY_INTERVAL), + "Invalid {0} schedule parameter \"{0}.{1}\"", section, keyInterval), e); interval = INVALID_CONFIG; } @@ -86,9 +98,9 @@ public class ScheduleConfig { } private static long initialDelay(Config rc, String section, - String subsection, DateTime now, long interval) { + String subsection, String keyStartTime, DateTime now, long interval) { long delay = MISSING_CONFIG; - String start = rc.getString(section, subsection, KEY_STARTTIME); + String start = rc.getString(section, subsection, keyStartTime); try { if (start != null) { DateTimeFormatter formatter; @@ -116,12 +128,12 @@ public class ScheduleConfig { } else { log.info(MessageFormat.format( "{0} schedule parameter \"{0}.{1}\" is not configured", section, - KEY_STARTTIME)); + keyStartTime)); } } catch (IllegalArgumentException e2) { log.error( MessageFormat.format("Invalid {0} schedule parameter \"{0}.{1}\"", - section, KEY_STARTTIME), e2); + section, keyStartTime), e2); delay = INVALID_CONFIG; } return delay; diff --git a/gerrit-server/src/test/java/com/google/gerrit/server/config/ScheduleConfigTest.java b/gerrit-server/src/test/java/com/google/gerrit/server/config/ScheduleConfigTest.java index 8ee454bad2..e6e15eb76b 100644 --- a/gerrit-server/src/test/java/com/google/gerrit/server/config/ScheduleConfigTest.java +++ b/gerrit-server/src/test/java/com/google/gerrit/server/config/ScheduleConfigTest.java @@ -19,6 +19,7 @@ import static java.util.concurrent.TimeUnit.HOURS; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.MINUTES; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.lib.Config; @@ -54,6 +55,26 @@ public class ScheduleConfigTest { assertEquals(ms(1, HOURS), initialDelay("Mon 11:00", "1d")); assertEquals(ms(23, HOURS), initialDelay("Mon 09:00", "1d")); assertEquals(ms(1, DAYS), initialDelay("Mon 10:00", "1d")); + assertEquals(ms(1, DAYS), initialDelay("Mon 10:00", "1d")); + } + + @Test + public void testCustomKeys() throws ConfigInvalidException { + Config rc = readConfig(MessageFormat.format( + "[section \"subsection\"]\n{0} = {1}\n{2} = {3}\n", + "myStartTime", "01:00", "myInterval", "1h")); + + ScheduleConfig scheduleConfig; + + scheduleConfig = new ScheduleConfig(rc, "section", + "subsection", "myInterval", "myStartTime"); + assertNotEquals(scheduleConfig.getInterval(), ScheduleConfig.MISSING_CONFIG); + assertNotEquals(scheduleConfig.getInitialDelay(), ScheduleConfig.MISSING_CONFIG); + + scheduleConfig = new ScheduleConfig(rc, "section", + "subsection", "nonExistent", "myStartTime"); + assertEquals(scheduleConfig.getInterval(), ScheduleConfig.MISSING_CONFIG); + assertEquals(scheduleConfig.getInitialDelay(), ScheduleConfig.MISSING_CONFIG); } private static long initialDelay(String startTime, String interval)