提交 b47d450d 编写于 作者: K Kohsuke Kawaguchi

Fixed RunIdMigratorTest failures in non-EST time zone

RunIdMigrator.LEGACY_ID_FORMATTER was static, which gets instantiated before the time zone is overriden during the test. Changing this to an instance field so that it gets created later on.
上级 4c474cea
...@@ -60,7 +60,6 @@ import org.apache.commons.lang.time.FastDateFormat; ...@@ -60,7 +60,6 @@ import org.apache.commons.lang.time.FastDateFormat;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse; import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.StaplerProxy; import org.kohsuke.stapler.StaplerProxy;
import org.kohsuke.stapler.framework.io.WriterOutputStream; import org.kohsuke.stapler.framework.io.WriterOutputStream;
...@@ -76,8 +75,9 @@ import static java.util.logging.Level.*; ...@@ -76,8 +75,9 @@ import static java.util.logging.Level.*;
@Restricted(NoExternalUse.class) @Restricted(NoExternalUse.class)
public final class RunIdMigrator { public final class RunIdMigrator {
private final DateFormat legacyIdFormatter = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
static final Logger LOGGER = Logger.getLogger(RunIdMigrator.class.getName()); static final Logger LOGGER = Logger.getLogger(RunIdMigrator.class.getName());
private static final DateFormat LEGACY_ID_FORMATTER = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
private static final String MAP_FILE = "legacyIds"; private static final String MAP_FILE = "legacyIds";
/** avoids wasting a map for new jobs */ /** avoids wasting a map for new jobs */
private static final Map<String,Integer> EMPTY = new TreeMap<String,Integer>(); private static final Map<String,Integer> EMPTY = new TreeMap<String,Integer>();
...@@ -235,8 +235,8 @@ public final class RunIdMigrator { ...@@ -235,8 +235,8 @@ public final class RunIdMigrator {
} }
long timestamp; long timestamp;
try { try {
synchronized (LEGACY_ID_FORMATTER) { synchronized (legacyIdFormatter) {
timestamp = LEGACY_ID_FORMATTER.parse(name).getTime(); timestamp = legacyIdFormatter.parse(name).getTime();
} }
} catch (ParseException x) { } catch (ParseException x) {
LOGGER.log(WARNING, "found unexpected dir {0}", name); LOGGER.log(WARNING, "found unexpected dir {0}", name);
...@@ -304,9 +304,9 @@ public final class RunIdMigrator { ...@@ -304,9 +304,9 @@ public final class RunIdMigrator {
if (!jobs.isDirectory()) { if (!jobs.isDirectory()) {
throw new FileNotFoundException("no such $JENKINS_HOME " + root); throw new FileNotFoundException("no such $JENKINS_HOME " + root);
} }
unmigrateJobsDir(jobs); new RunIdMigrator().unmigrateJobsDir(jobs);
} }
private static void unmigrateJobsDir(File jobs) throws Exception { private void unmigrateJobsDir(File jobs) throws Exception {
for (File job : jobs.listFiles()) { for (File job : jobs.listFiles()) {
File[] kids = job.listFiles(); File[] kids = job.listFiles();
if (kids == null) { if (kids == null) {
...@@ -328,7 +328,7 @@ public final class RunIdMigrator { ...@@ -328,7 +328,7 @@ public final class RunIdMigrator {
private static final Pattern ID_ELT = Pattern.compile("(?m)^ <id>([0-9_-]+)</id>(\r?\n)"); private static final Pattern ID_ELT = Pattern.compile("(?m)^ <id>([0-9_-]+)</id>(\r?\n)");
private static final Pattern TIMESTAMP_ELT = Pattern.compile("(?m)^ <timestamp>(\\d+)</timestamp>(\r?\n)"); private static final Pattern TIMESTAMP_ELT = Pattern.compile("(?m)^ <timestamp>(\\d+)</timestamp>(\r?\n)");
/** Inverse of {@link #doMigrate}. */ /** Inverse of {@link #doMigrate}. */
private static void unmigrateBuildsDir(File builds) throws Exception { private void unmigrateBuildsDir(File builds) throws Exception {
File mapFile = new File(builds, MAP_FILE); File mapFile = new File(builds, MAP_FILE);
if (!mapFile.isFile()) { if (!mapFile.isFile()) {
System.err.println(builds + " does not look to have been migrated yet; skipping"); System.err.println(builds + " does not look to have been migrated yet; skipping");
...@@ -362,7 +362,7 @@ public final class RunIdMigrator { ...@@ -362,7 +362,7 @@ public final class RunIdMigrator {
xml = m.replaceFirst(""); xml = m.replaceFirst("");
} else { } else {
// Post-migration build. We give it a new ID based on its timestamp. // Post-migration build. We give it a new ID based on its timestamp.
id = LEGACY_ID_FORMATTER.format(new Date(timestamp)); id = legacyIdFormatter.format(new Date(timestamp));
} }
FileUtils.write(buildXml, xml, Charsets.UTF_8); FileUtils.write(buildXml, xml, Charsets.UTF_8);
if (!build.renameTo(new File(builds, id))) { if (!build.renameTo(new File(builds, id))) {
......
...@@ -28,6 +28,7 @@ import hudson.Util; ...@@ -28,6 +28,7 @@ import hudson.Util;
import hudson.util.StreamTaskListener; import hudson.util.StreamTaskListener;
import java.io.File; import java.io.File;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Date;
import java.util.Map; import java.util.Map;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.TreeMap; import java.util.TreeMap;
...@@ -65,7 +66,7 @@ public class RunIdMigratorTest { ...@@ -65,7 +66,7 @@ public class RunIdMigratorTest {
migrator = new RunIdMigrator(); migrator = new RunIdMigrator();
dir = tmp.getRoot(); dir = tmp.getRoot();
} }
@Test public void newJob() throws Exception { @Test public void newJob() throws Exception {
migrator.created(dir); migrator.created(dir);
assertEquals("{legacyIds=''}", summarize()); assertEquals("{legacyIds=''}", summarize());
...@@ -149,4 +150,4 @@ public class RunIdMigratorTest { ...@@ -149,4 +150,4 @@ public class RunIdMigratorTest {
return m.toString(); return m.toString();
} }
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册