Browse Source

Cache SimpleDateFormat in GitDateParser per locale

Otherwise switching to another locale yields wrong results when parsing
date strings in GitDateParser. Since the MockSystemReader explicitly
uses english locale the tests need to specify the locale to be used when
parsing date strings.

Bug: 420772
Change-Id: I313ef6b1e9ef3bfb43d929ce34712ebd21f2cd9c
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-3.3
Matthias Sohn 11 years ago committed by Gerrit Code Review @ Eclipse.org
parent
commit
7f7dd171df
  1. 11
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/GarbageCollectCommandTest.java
  2. 6
      org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitDateParserBadlyFormattedTest.java
  3. 39
      org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitDateParserTest.java
  4. 4
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java
  5. 92
      org.eclipse.jgit/src/org/eclipse/jgit/util/GitDateParser.java
  6. 15
      org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java

11
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/GarbageCollectCommandTest.java

@ -49,6 +49,7 @@ import java.util.Properties;
import org.eclipse.jgit.junit.RepositoryTestCase; import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.util.GitDateParser; import org.eclipse.jgit.util.GitDateParser;
import org.eclipse.jgit.util.SystemReader;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -68,7 +69,8 @@ public class GarbageCollectCommandTest extends RepositoryTestCase {
@Test @Test
public void testGConeCommit() throws Exception { public void testGConeCommit() throws Exception {
Date expire = GitDateParser.parse("now", null); Date expire = GitDateParser.parse("now", null, SystemReader
.getInstance().getLocale());
Properties res = git.gc().setExpire(expire).call(); Properties res = git.gc().setExpire(expire).call();
assertTrue(res.size() == 7); assertTrue(res.size() == 7);
} }
@ -83,8 +85,11 @@ public class GarbageCollectCommandTest extends RepositoryTestCase {
writeTrashFile("b.txt", "a couple of words for gc to pack more 2"); writeTrashFile("b.txt", "a couple of words for gc to pack more 2");
writeTrashFile("c.txt", "a couple of words for gc to pack more 3"); writeTrashFile("c.txt", "a couple of words for gc to pack more 3");
git.commit().setAll(true).setMessage("commit3").call(); git.commit().setAll(true).setMessage("commit3").call();
Properties res = git.gc().setExpire(GitDateParser.parse("now", null)) Properties res = git
.call(); .gc()
.setExpire(
GitDateParser.parse("now", null, SystemReader
.getInstance().getLocale())).call();
assertTrue(res.size() == 7); assertTrue(res.size() == 7);
} }
} }

6
org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitDateParserBadlyFormattedTest.java

@ -92,7 +92,8 @@ public class GitDateParserBadlyFormattedTest {
Calendar ref = new GregorianCalendar(SystemReader.getInstance() Calendar ref = new GregorianCalendar(SystemReader.getInstance()
.getTimeZone(), SystemReader.getInstance().getLocale()); .getTimeZone(), SystemReader.getInstance().getLocale());
try { try {
GitDateParser.parse(dateStr, ref); GitDateParser.parse(dateStr, ref, SystemReader.getInstance()
.getLocale());
fail("The expected ParseException while parsing '" + dateStr fail("The expected ParseException while parsing '" + dateStr
+ "' did not occur."); + "' did not occur.");
} catch (ParseException e) { } catch (ParseException e) {
@ -103,7 +104,8 @@ public class GitDateParserBadlyFormattedTest {
@Theory @Theory
public void badlyFormattedWithoutRef() { public void badlyFormattedWithoutRef() {
try { try {
GitDateParser.parse(dateStr, null); GitDateParser.parse(dateStr, null, SystemReader.getInstance()
.getLocale());
fail("The expected ParseException while parsing '" + dateStr fail("The expected ParseException while parsing '" + dateStr
+ "' did not occur."); + "' did not occur.");
} catch (ParseException e) { } catch (ParseException e) {

39
org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitDateParserTest.java

@ -72,7 +72,8 @@ public class GitDateParserTest {
GregorianCalendar cal = new GregorianCalendar(SystemReader GregorianCalendar cal = new GregorianCalendar(SystemReader
.getInstance().getTimeZone(), SystemReader.getInstance() .getInstance().getTimeZone(), SystemReader.getInstance()
.getLocale()); .getLocale());
Date parse = GitDateParser.parse("yesterday", cal); Date parse = GitDateParser.parse("yesterday", cal, SystemReader
.getInstance().getLocale());
cal.add(Calendar.DATE, -1); cal.add(Calendar.DATE, -1);
cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0); cal.set(Calendar.MINUTE, 0);
@ -87,7 +88,8 @@ public class GitDateParserTest {
GregorianCalendar cal = new GregorianCalendar(SystemReader GregorianCalendar cal = new GregorianCalendar(SystemReader
.getInstance().getTimeZone(), SystemReader.getInstance() .getInstance().getTimeZone(), SystemReader.getInstance()
.getLocale()); .getLocale());
Date parse = GitDateParser.parse("never", cal); Date parse = GitDateParser.parse("never", cal, SystemReader
.getInstance().getLocale());
Assert.assertEquals(GitDateParser.NEVER, parse); Assert.assertEquals(GitDateParser.NEVER, parse);
parse = GitDateParser.parse("never", null); parse = GitDateParser.parse("never", null);
Assert.assertEquals(GitDateParser.NEVER, parse); Assert.assertEquals(GitDateParser.NEVER, parse);
@ -104,7 +106,8 @@ public class GitDateParserTest {
.getLocale()); .getLocale());
cal.setTime(refDate); cal.setTime(refDate);
Date parse = GitDateParser.parse("now", cal); Date parse = GitDateParser.parse("now", cal, SystemReader.getInstance()
.getLocale());
Assert.assertEquals(refDate, parse); Assert.assertEquals(refDate, parse);
long t1 = SystemReader.getInstance().getCurrentTime(); long t1 = SystemReader.getInstance().getCurrentTime();
parse = GitDateParser.parse("now", null); parse = GitDateParser.parse("now", null);
@ -123,7 +126,8 @@ public class GitDateParserTest {
.getLocale()); .getLocale());
cal.setTime(refDate); cal.setTime(refDate);
Date parse = GitDateParser.parse("2 weeks ago", cal); Date parse = GitDateParser.parse("2 weeks ago", cal, SystemReader
.getInstance().getLocale());
Assert.assertEquals(df.parse("2007-02-07 15:35:00 +0100"), parse); Assert.assertEquals(df.parse("2007-02-07 15:35:00 +0100"), parse);
} }
@ -138,7 +142,8 @@ public class GitDateParserTest {
.getLocale()); .getLocale());
cal.setTime(refDate); cal.setTime(refDate);
Date parse = GitDateParser.parse("2 weeks ago", cal); Date parse = GitDateParser.parse("2 weeks ago", cal, SystemReader.getInstance()
.getLocale());
Assert.assertEquals(df.parse("2007-02-07 15:35:00 +0100"), parse); Assert.assertEquals(df.parse("2007-02-07 15:35:00 +0100"), parse);
parse = GitDateParser.parse("3 days 2 weeks ago", cal); parse = GitDateParser.parse("3 days 2 weeks ago", cal);
Assert.assertEquals(df.parse("2007-02-04 15:35:00 +0100"), parse); Assert.assertEquals(df.parse("2007-02-04 15:35:00 +0100"), parse);
@ -151,7 +156,8 @@ public class GitDateParserTest {
String dateStr = "2007-02-21 15:35:00 +0100"; String dateStr = "2007-02-21 15:35:00 +0100";
Date exp = SystemReader.getInstance() Date exp = SystemReader.getInstance()
.getSimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse(dateStr); .getSimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse(dateStr);
Date parse = GitDateParser.parse(dateStr, null); Date parse = GitDateParser.parse(dateStr, null, SystemReader
.getInstance().getLocale());
Assert.assertEquals(exp, parse); Assert.assertEquals(exp, parse);
} }
@ -161,7 +167,8 @@ public class GitDateParserTest {
Date exp = SystemReader.getInstance() Date exp = SystemReader.getInstance()
.getSimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z") .getSimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z")
.parse(dateStr); .parse(dateStr);
Date parse = GitDateParser.parse(dateStr, null); Date parse = GitDateParser.parse(dateStr, null, SystemReader
.getInstance().getLocale());
Assert.assertEquals(exp, parse); Assert.assertEquals(exp, parse);
} }
@ -170,7 +177,8 @@ public class GitDateParserTest {
String dateStr = "2007-02-21"; String dateStr = "2007-02-21";
Date exp = SystemReader.getInstance().getSimpleDateFormat("yyyy-MM-dd") Date exp = SystemReader.getInstance().getSimpleDateFormat("yyyy-MM-dd")
.parse(dateStr); .parse(dateStr);
Date parse = GitDateParser.parse(dateStr, null); Date parse = GitDateParser.parse(dateStr, null, SystemReader
.getInstance().getLocale());
Assert.assertEquals(exp, parse); Assert.assertEquals(exp, parse);
} }
@ -179,7 +187,8 @@ public class GitDateParserTest {
String dateStr = "2007.02.21"; String dateStr = "2007.02.21";
Date exp = SystemReader.getInstance().getSimpleDateFormat("yyyy.MM.dd") Date exp = SystemReader.getInstance().getSimpleDateFormat("yyyy.MM.dd")
.parse(dateStr); .parse(dateStr);
Date parse = GitDateParser.parse(dateStr, null); Date parse = GitDateParser.parse(dateStr, null, SystemReader
.getInstance().getLocale());
Assert.assertEquals(exp, parse); Assert.assertEquals(exp, parse);
} }
@ -188,7 +197,8 @@ public class GitDateParserTest {
String dateStr = "02/21/2007"; String dateStr = "02/21/2007";
Date exp = SystemReader.getInstance().getSimpleDateFormat("MM/dd/yyyy") Date exp = SystemReader.getInstance().getSimpleDateFormat("MM/dd/yyyy")
.parse(dateStr); .parse(dateStr);
Date parse = GitDateParser.parse(dateStr, null); Date parse = GitDateParser.parse(dateStr, null, SystemReader
.getInstance().getLocale());
Assert.assertEquals(exp, parse); Assert.assertEquals(exp, parse);
} }
@ -197,7 +207,8 @@ public class GitDateParserTest {
String dateStr = "21.02.2007"; String dateStr = "21.02.2007";
Date exp = SystemReader.getInstance().getSimpleDateFormat("dd.MM.yyyy") Date exp = SystemReader.getInstance().getSimpleDateFormat("dd.MM.yyyy")
.parse(dateStr); .parse(dateStr);
Date parse = GitDateParser.parse(dateStr, null); Date parse = GitDateParser.parse(dateStr, null, SystemReader
.getInstance().getLocale());
Assert.assertEquals(exp, parse); Assert.assertEquals(exp, parse);
} }
@ -207,7 +218,8 @@ public class GitDateParserTest {
Date exp = SystemReader.getInstance() Date exp = SystemReader.getInstance()
.getSimpleDateFormat("EEE MMM dd HH:mm:ss yyyy Z") .getSimpleDateFormat("EEE MMM dd HH:mm:ss yyyy Z")
.parse(dateStr); .parse(dateStr);
Date parse = GitDateParser.parse(dateStr, null); Date parse = GitDateParser.parse(dateStr, null, SystemReader
.getInstance().getLocale());
Assert.assertEquals(exp, parse); Assert.assertEquals(exp, parse);
} }
@ -216,7 +228,8 @@ public class GitDateParserTest {
String dateStr = "Wed Feb 21 15:35:00 2007"; String dateStr = "Wed Feb 21 15:35:00 2007";
Date exp = SystemReader.getInstance() Date exp = SystemReader.getInstance()
.getSimpleDateFormat("EEE MMM dd HH:mm:ss yyyy").parse(dateStr); .getSimpleDateFormat("EEE MMM dd HH:mm:ss yyyy").parse(dateStr);
Date parse = GitDateParser.parse(dateStr, null); Date parse = GitDateParser.parse(dateStr, null, SystemReader
.getInstance().getLocale());
Assert.assertEquals(exp, parse); Assert.assertEquals(exp, parse);
} }
} }

4
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java

@ -97,6 +97,7 @@ import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.TreeFilter; import org.eclipse.jgit.treewalk.filter.TreeFilter;
import org.eclipse.jgit.util.FileUtils; import org.eclipse.jgit.util.FileUtils;
import org.eclipse.jgit.util.GitDateParser; import org.eclipse.jgit.util.GitDateParser;
import org.eclipse.jgit.util.SystemReader;
/** /**
* A garbage collector for git {@link FileRepository}. Instances of this class * A garbage collector for git {@link FileRepository}. Instances of this class
@ -307,7 +308,8 @@ public class GC {
ConfigConstants.CONFIG_KEY_PRUNEEXPIRE); ConfigConstants.CONFIG_KEY_PRUNEEXPIRE);
if (pruneExpireStr == null) if (pruneExpireStr == null)
pruneExpireStr = PRUNE_EXPIRE_DEFAULT; pruneExpireStr = PRUNE_EXPIRE_DEFAULT;
expire = GitDateParser.parse(pruneExpireStr, null); expire = GitDateParser.parse(pruneExpireStr, null, SystemReader
.getInstance().getLocale());
expireAgeMillis = -1; expireAgeMillis = -1;
} }
if (expire != null) if (expire != null)

92
org.eclipse.jgit/src/org/eclipse/jgit/util/GitDateParser.java

@ -49,6 +49,7 @@ import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.JGitText;
@ -71,23 +72,40 @@ public class GitDateParser {
// Since SimpleDateFormat instances are expensive to instantiate they should // Since SimpleDateFormat instances are expensive to instantiate they should
// be cached. Since they are also not threadsafe they are cached using // be cached. Since they are also not threadsafe they are cached using
// ThreadLocal. // ThreadLocal.
private static ThreadLocal<Map<ParseableSimpleDateFormat, SimpleDateFormat>> formatCache = new ThreadLocal<Map<ParseableSimpleDateFormat, SimpleDateFormat>>() { private static ThreadLocal<Map<Locale, Map<ParseableSimpleDateFormat, SimpleDateFormat>>> formatCache =
protected Map<ParseableSimpleDateFormat, SimpleDateFormat> initialValue() { new ThreadLocal<Map<Locale, Map<ParseableSimpleDateFormat, SimpleDateFormat>>>() {
return new HashMap<ParseableSimpleDateFormat, SimpleDateFormat>();
protected Map<Locale, Map<ParseableSimpleDateFormat, SimpleDateFormat>> initialValue() {
return new HashMap<Locale, Map<ParseableSimpleDateFormat, SimpleDateFormat>>();
} }
}; };
// Gets an instance of a SimpleDateFormat. If there is not already an // Gets an instance of a SimpleDateFormat for the specified locale. If there
// appropriate instance in the (ThreadLocal) cache the create one and put in // is not already an appropriate instance in the (ThreadLocal) cache then
// into the cache // create one and put it into the cache.
private static SimpleDateFormat getDateFormat(ParseableSimpleDateFormat f) { private static SimpleDateFormat getDateFormat(ParseableSimpleDateFormat f,
Map<ParseableSimpleDateFormat, SimpleDateFormat> map = formatCache Locale locale) {
Map<Locale, Map<ParseableSimpleDateFormat, SimpleDateFormat>> cache = formatCache
.get(); .get();
Map<ParseableSimpleDateFormat, SimpleDateFormat> map = cache
.get(locale);
if (map == null) {
map = new HashMap<ParseableSimpleDateFormat, SimpleDateFormat>();
cache.put(locale, map);
return getNewSimpleDateFormat(f, locale, map);
}
SimpleDateFormat dateFormat = map.get(f); SimpleDateFormat dateFormat = map.get(f);
if (dateFormat != null) if (dateFormat != null)
return dateFormat; return dateFormat;
SimpleDateFormat df = getNewSimpleDateFormat(f, locale, map);
return df;
}
private static SimpleDateFormat getNewSimpleDateFormat(
ParseableSimpleDateFormat f, Locale locale,
Map<ParseableSimpleDateFormat, SimpleDateFormat> map) {
SimpleDateFormat df = SystemReader.getInstance().getSimpleDateFormat( SimpleDateFormat df = SystemReader.getInstance().getSimpleDateFormat(
f.formatStr); f.formatStr, locale);
map.put(f, df); map.put(f, df);
return df; return df;
} }
@ -115,9 +133,9 @@ public class GitDateParser {
} }
/** /**
* Parses a string into a {@link Date}. Since this parser also supports * Parses a string into a {@link Date} using the default locale. Since this
* relative formats (e.g. "yesterday") the caller can specify the reference * parser also supports relative formats (e.g. "yesterday") the caller can
* date. These types of strings can be parsed: * specify the reference date. These types of strings can be parsed:
* <ul> * <ul>
* <li>"never"</li> * <li>"never"</li>
* <li>"now"</li> * <li>"now"</li>
@ -151,6 +169,49 @@ public class GitDateParser {
*/ */
public static Date parse(String dateStr, Calendar now) public static Date parse(String dateStr, Calendar now)
throws ParseException { throws ParseException {
return parse(dateStr, now, Locale.getDefault());
}
/**
* Parses a string into a {@link Date} using the given locale. Since this
* parser also supports relative formats (e.g. "yesterday") the caller can
* specify the reference date. These types of strings can be parsed:
* <ul>
* <li>"never"</li>
* <li>"now"</li>
* <li>"yesterday"</li>
* <li>"(x) years|months|weeks|days|hours|minutes|seconds ago"<br>
* Multiple specs can be combined like in "2 weeks 3 days ago". Instead of
* ' ' one can use '.' to seperate the words</li>
* <li>"yyyy-MM-dd HH:mm:ss Z" (ISO)</li>
* <li>"EEE, dd MMM yyyy HH:mm:ss Z" (RFC)</li>
* <li>"yyyy-MM-dd"</li>
* <li>"yyyy.MM.dd"</li>
* <li>"MM/dd/yyyy",</li>
* <li>"dd.MM.yyyy"</li>
* <li>"EEE MMM dd HH:mm:ss yyyy Z" (DEFAULT)</li>
* <li>"EEE MMM dd HH:mm:ss yyyy" (LOCAL)</li>
* </ul>
*
* @param dateStr
* the string to be parsed
* @param now
* the base date which is used for the calculation of relative
* formats. E.g. if baseDate is "25.8.2012" then parsing of the
* string "1 week ago" would result in a date corresponding to
* "18.8.2012". This is used when a JGit command calls this
* parser often but wants a consistent starting point for calls.<br>
* If set to <code>null</code> then the current time will be used
* instead.
* @param locale
* locale to be used to parse the date string
* @return the parsed {@link Date}
* @throws ParseException
* if the given dateStr was not recognized
* @since 3.2
*/
public static Date parse(String dateStr, Calendar now, Locale locale)
throws ParseException {
dateStr = dateStr.trim(); dateStr = dateStr.trim();
Date ret; Date ret;
@ -161,7 +222,7 @@ public class GitDateParser {
return ret; return ret;
for (ParseableSimpleDateFormat f : ParseableSimpleDateFormat.values()) { for (ParseableSimpleDateFormat f : ParseableSimpleDateFormat.values()) {
try { try {
return parse_simple(dateStr, f); return parse_simple(dateStr, f, locale);
} catch (ParseException e) { } catch (ParseException e) {
// simply proceed with the next parser // simply proceed with the next parser
} }
@ -177,9 +238,10 @@ public class GitDateParser {
} }
// tries to parse a string with the formats supported by SimpleDateFormat // tries to parse a string with the formats supported by SimpleDateFormat
private static Date parse_simple(String dateStr, ParseableSimpleDateFormat f) private static Date parse_simple(String dateStr,
ParseableSimpleDateFormat f, Locale locale)
throws ParseException { throws ParseException {
SimpleDateFormat dateFormat = getDateFormat(f); SimpleDateFormat dateFormat = getDateFormat(f, locale);
dateFormat.setLenient(false); dateFormat.setLenient(false);
return dateFormat.parse(dateStr); return dateFormat.parse(dateStr);
} }

15
org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java

@ -229,6 +229,21 @@ public abstract class SystemReader {
return new SimpleDateFormat(pattern); return new SimpleDateFormat(pattern);
} }
/**
* Returns a simple date format instance as specified by the given pattern.
*
* @param pattern
* the pattern as defined in
* {@link SimpleDateFormat#SimpleDateFormat(String)}
* @param locale
* locale to be used for the {@code SimpleDateFormat}
* @return the simple date format
* @since 3.2
*/
public SimpleDateFormat getSimpleDateFormat(String pattern, Locale locale) {
return new SimpleDateFormat(pattern, locale);
}
/** /**
* Returns a date/time format instance for the given styles. * Returns a date/time format instance for the given styles.
* *

Loading…
Cancel
Save