SOLR-4325: fix TestBuiltInEvaluators

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1438597 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James Dyer 2013-01-25 17:20:22 +00:00
parent 7d70f6190a
commit f8c00c1f85
1 changed files with 36 additions and 32 deletions

View File

@ -17,7 +17,6 @@
package org.apache.solr.handler.dataimport;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.net.URLEncoder;
@ -109,58 +108,63 @@ public class TestBuiltInEvaluators extends AbstractDataImportHandlerTestCase {
}
private Date getNow() {
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT"),
Locale.ROOT);
private Date twoDaysAgo(Locale l, TimeZone tz) {
Calendar calendar = Calendar.getInstance(tz, l);
calendar.add(Calendar.DAY_OF_YEAR, -2);
return calendar.getTime();
}
@Test
@Ignore("fails if somewhere on earth is a DST change")
public void testDateFormatEvaluator() {
Evaluator dateFormatEval = new DateFormatEvaluator();
ContextImpl context = new ContextImpl(null, resolver, null,
Context.FULL_DUMP, Collections.<String,Object> emptyMap(), null, null);
String currentLocale = Locale.getDefault().toString();
Locale rootLocale = Locale.ROOT;
Locale defaultLocale = Locale.getDefault();
TimeZone defaultTz = TimeZone.getDefault();
{
{
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH", Locale.ROOT);
String sdf = sdfDate.format(getNow());
String dfe = dateFormatEval.evaluate("'NOW-2DAYS','yyyy-MM-dd HH'", context);
assertEquals(sdf,dfe);
}
{
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH", Locale.getDefault());
String sdf = sdfDate.format(getNow());
String dfe = dateFormatEval.evaluate("'NOW-2DAYS','yyyy-MM-dd HH','"+ currentLocale + "'", context);
assertEquals(sdf,dfe);
for(String tz : TimeZone.getAvailableIDs()) {
sdfDate.setTimeZone(TimeZone.getTimeZone(tz));
sdf = sdfDate.format(getNow());
dfe = dateFormatEval.evaluate("'NOW-2DAYS','yyyy-MM-dd HH','" + currentLocale + "','" + tz + "'", context);
assertEquals(sdf,dfe);
}
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH", rootLocale);
String sdf = sdfDate.format(twoDaysAgo(rootLocale, defaultTz));
String dfe = dateFormatEval.evaluate("'NOW-2DAYS','yyyy-MM-dd HH'", context);
assertEquals(sdf,dfe);
}
{
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH", defaultLocale);
String sdf = sdfDate.format(twoDaysAgo(defaultLocale, TimeZone.getDefault()));
String dfe = dateFormatEval.evaluate(
"'NOW-2DAYS','yyyy-MM-dd HH','" + defaultLocale + "'", context);
assertEquals(sdf,dfe);
for(String tzStr : TimeZone.getAvailableIDs()) {
TimeZone tz = TimeZone.getTimeZone(tzStr);
sdfDate.setTimeZone(tz);
sdf = sdfDate.format(twoDaysAgo(defaultLocale, tz));
dfe = dateFormatEval.evaluate(
"'NOW-2DAYS','yyyy-MM-dd HH','" + defaultLocale + "','" + tzStr + "'", context);
assertEquals(sdf,dfe);
}
}
Date d = new Date();
Map<String,Object> map = new HashMap<String,Object>();
map.put("key", d);
resolver.addNamespace("A", map);
assertEquals(
new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ROOT).format(d),
new SimpleDateFormat("yyyy-MM-dd HH:mm", rootLocale).format(d),
dateFormatEval.evaluate("A.key, 'yyyy-MM-dd HH:mm'", context));
assertEquals(
new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault()).format(d),
dateFormatEval.evaluate("A.key, 'yyyy-MM-dd HH:mm','" + currentLocale
+ "'", context));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
for(String tz : TimeZone.getAvailableIDs()) {
sdf.setTimeZone(TimeZone.getTimeZone(tz));
new SimpleDateFormat("yyyy-MM-dd HH:mm", defaultLocale).format(d),
dateFormatEval.evaluate("A.key, 'yyyy-MM-dd HH:mm','" + defaultLocale + "'", context));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", defaultLocale);
for(String tzStr : TimeZone.getAvailableIDs()) {
TimeZone tz = TimeZone.getTimeZone(tzStr);
sdf.setTimeZone(tz);
assertEquals(
sdf.format(d),
dateFormatEval.evaluate("A.key, 'yyyy-MM-dd HH:mm','" + currentLocale + "', '" + tz + "'", context));
dateFormatEval.evaluate(
"A.key, 'yyyy-MM-dd HH:mm','" + defaultLocale + "', '" + tzStr + "'", context));
}