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>
tags/v3.2.0.201312181205-r
Matthias Sohn 10 years ago
parent
commit
7f7dd171df

+ 8
- 3
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/GarbageCollectCommandTest.java View File



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;




@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);
} }
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))
.call();
Properties res = git
.gc()
.setExpire(
GitDateParser.parse("now", null, SystemReader
.getInstance().getLocale())).call();
assertTrue(res.size() == 7); assertTrue(res.size() == 7);
} }
} }

+ 4
- 2
org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitDateParserBadlyFormattedTest.java View File

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) {
@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) {

+ 26
- 13
org.eclipse.jgit.test/tst/org/eclipse/jgit/util/GitDateParserTest.java View File

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);
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);
.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);
.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);
} }


.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);
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);
} }


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);
} }


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);
} }


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);
} }


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);
} }


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);
} }


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);
} }


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);
} }
} }

+ 3
- 1
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java View File

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
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)

+ 77
- 15
org.eclipse.jgit/src/org/eclipse/jgit/util/GitDateParser.java View File

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;
// 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>>() {
protected Map<ParseableSimpleDateFormat, SimpleDateFormat> initialValue() {
return new HashMap<ParseableSimpleDateFormat, SimpleDateFormat>();
private static ThreadLocal<Map<Locale, Map<ParseableSimpleDateFormat, SimpleDateFormat>>> formatCache =
new ThreadLocal<Map<Locale, Map<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
// appropriate instance in the (ThreadLocal) cache the create one and put in
// into the cache
private static SimpleDateFormat getDateFormat(ParseableSimpleDateFormat f) {
Map<ParseableSimpleDateFormat, SimpleDateFormat> map = formatCache
// Gets an instance of a SimpleDateFormat for the specified locale. If there
// is not already an appropriate instance in the (ThreadLocal) cache then
// create one and put it into the cache.
private static SimpleDateFormat getDateFormat(ParseableSimpleDateFormat f,
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;
} }
} }


/** /**
* Parses a string into a {@link Date}. Since this parser also supports
* relative formats (e.g. "yesterday") the caller can specify the reference
* date. These types of strings can be parsed:
* Parses a string into a {@link Date} using the default 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> * <ul>
* <li>"never"</li> * <li>"never"</li>
* <li>"now"</li> * <li>"now"</li>
*/ */
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;


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
} }
} }


// 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
- 0
org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java View File

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