Cleanup test of ScheduleConfig

Asserting not equals to missing doesn't sufficiently test the
value was parsed properly for a non-standard key. Instead check
for real values.

Replace the hacky MessagseFormat string parsing of Config with
API setters. The Config class is already well tested in JGit.
There is no need to obfuscate this code to "verify" again the
Config object can read from a string.

Change-Id: I60cbf6dae9621fe7a7d7a10c5db87a39d3b57aac
This commit is contained in:
Shawn Pearce
2014-10-01 10:41:09 -07:00
parent 8aeece53f0
commit a19dbb5f9a
2 changed files with 30 additions and 31 deletions

View File

@@ -14,6 +14,8 @@
package com.google.gerrit.server.config;
import com.google.common.annotations.VisibleForTesting;
import org.eclipse.jgit.lib.Config;
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
@@ -52,12 +54,13 @@ public class ScheduleConfig {
this(rc, section, subsection, keyInterval, keyStartTime, DateTime.now());
}
/* For testing we need to be able to pass now */
@VisibleForTesting
ScheduleConfig(Config rc, String section, String subsection, DateTime now) {
this(rc, section, subsection, KEY_INTERVAL, KEY_STARTTIME, now);
}
private ScheduleConfig(Config rc, String section, String subsection,
@VisibleForTesting
ScheduleConfig(Config rc, String section, String subsection,
String keyInterval, String keyStartTime, DateTime now) {
this.interval = interval(rc, section, subsection, keyInterval);
if (interval > 0) {
@@ -68,10 +71,18 @@ public class ScheduleConfig {
}
}
/**
* Milliseconds between constructor invocation and first event time.
* <p>
* If there is any lag between the constructor invocation and queuing the
* object into an executor the event will run later, as there is no method
* to adjust for the scheduling delay.
*/
public long getInitialDelay() {
return initialDelay;
}
/** Number of milliseconds between events. */
public long getInterval() {
return interval;
}

View File

@@ -19,14 +19,12 @@ 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;
import org.joda.time.DateTime;
import org.junit.Test;
import java.text.MessageFormat;
import java.util.concurrent.TimeUnit;
public class ScheduleConfigTest {
@@ -60,42 +58,32 @@ public class ScheduleConfigTest {
@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"));
Config rc = new Config();
rc.setString("a", "b", "i", "1h");
rc.setString("a", "b", "s", "01:00");
ScheduleConfig scheduleConfig;
ScheduleConfig s = new ScheduleConfig(rc, "a", "b", "i", "s", NOW);
assertEquals(ms(1, HOURS), s.getInterval());
assertEquals(ms(1, HOURS), s.getInitialDelay());
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);
s = new ScheduleConfig(rc, "a", "b", "myInterval", "myStart", NOW);
assertEquals(s.getInterval(), ScheduleConfig.MISSING_CONFIG);
assertEquals(s.getInitialDelay(), ScheduleConfig.MISSING_CONFIG);
}
private static long initialDelay(String startTime, String interval)
throws ConfigInvalidException {
return config(startTime, interval).getInitialDelay();
return new ScheduleConfig(
config(startTime, interval),
"section", "subsection", NOW).getInitialDelay();
}
private static ScheduleConfig config(String startTime, String interval)
private static Config config(String startTime, String interval)
throws ConfigInvalidException {
Config rc =
readConfig(MessageFormat.format(
"[section \"subsection\"]\nstartTime = {0}\ninterval = {1}\n",
startTime, interval));
return new ScheduleConfig(rc, "section", "subsection", NOW);
}
private static Config readConfig(String dat)
throws ConfigInvalidException {
Config config = new Config();
config.fromText(dat);
return config;
Config rc = new Config();
rc.setString("section", "subsection", "startTime", startTime);
rc.setString("section", "subsection", "interval", interval);
return rc;
}
private static long ms(int cnt, TimeUnit unit) {