SOLR-4095: NumberFormatTransformer & DateFormatTransformer default to the Root Locale

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1411757 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James Dyer 2012-11-20 17:40:57 +00:00
parent b67c9e54ea
commit 0eab30da1d
5 changed files with 38 additions and 34 deletions

View File

@ -258,6 +258,10 @@ Other Changes
* SOLR-3602: Update ZooKeeper to 3.4.5 (Mark Miller)
* SOLR-4095 DIH NumberFormatTransformer & DateFormatTransformer default to the
ROOT Locale if none is specified. These previously used the machine's default.
(James Dyer)
================== 4.0.0 ==================
Versions of Major Components

View File

@ -49,7 +49,7 @@ public class DateFormatTransformer extends Transformer {
public Object transformRow(Map<String, Object> aRow, Context context) {
for (Map<String, String> map : context.getAllEntityFields()) {
Locale locale = Locale.getDefault();
Locale locale = Locale.ROOT;
String customLocale = map.get("locale");
if(customLocale != null){
locale = new Locale(customLocale);

View File

@ -68,7 +68,7 @@ public class NumberFormatTransformer extends Transformer {
throw new DataImportHandlerException(DataImportHandlerException.SEVERE, "Invalid Locale specified for field: " + fld);
}
} else {
locale = Locale.getDefault();
locale = Locale.ROOT;
}
Object val = row.get(srcCol);

View File

@ -34,16 +34,16 @@ public class TestDateFormatTransformer extends AbstractDataImportHandlerTestCase
@Test
@SuppressWarnings("unchecked")
public void testTransformRow_SingleRow() throws Exception {
List fields = new ArrayList();
List<Map<String, String>> fields = new ArrayList<Map<String, String>>();
fields.add(createMap(DataImporter.COLUMN, "lastModified"));
fields.add(createMap(DataImporter.COLUMN,
"dateAdded", RegexTransformer.SRC_COL_NAME, "lastModified",
DateFormatTransformer.DATE_TIME_FMT, "MM/dd/yyyy"));
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy", Locale.ROOT);
Date now = format.parse(format.format(new Date()));
Map row = createMap("lastModified", format.format(now));
Map<String,Object> row = createMap("lastModified", format.format(now));
VariableResolver resolver = new VariableResolver();
resolver.addNamespace("e", row);
@ -57,18 +57,18 @@ public class TestDateFormatTransformer extends AbstractDataImportHandlerTestCase
@Test
@SuppressWarnings("unchecked")
public void testTransformRow_MultipleRows() throws Exception {
List fields = new ArrayList();
List<Map<String, String>> fields = new ArrayList<Map<String, String>>();
fields.add(createMap(DataImporter.COLUMN, "lastModified"));
fields.add(createMap(DataImporter.COLUMN,
"dateAdded", RegexTransformer.SRC_COL_NAME, "lastModified",
DateFormatTransformer.DATE_TIME_FMT, "MM/dd/yyyy hh:mm:ss.SSS"));
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss.SSS");
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss.SSS", Locale.ROOT);
Date now1 = format.parse(format.format(new Date()));
Date now2 = format.parse(format.format(new Date()));
Map row = new HashMap();
List list = new ArrayList();
Map<String,Object> row = new HashMap<String,Object>();
List<String> list = new ArrayList<String>();
list.add(format.format(now1));
list.add(format.format(now2));
row.put("lastModified", list);
@ -79,7 +79,7 @@ public class TestDateFormatTransformer extends AbstractDataImportHandlerTestCase
Context context = getContext(null, resolver,
null, Context.FULL_DUMP, fields, null);
new DateFormatTransformer().transformRow(row, context);
List output = new ArrayList();
List<Object> output = new ArrayList<Object>();
output.add(now1);
output.add(now2);
assertEquals(output, row.get("dateAdded"));

View File

@ -33,20 +33,20 @@ import java.util.Map;
* @since solr 1.3
*/
public class TestNumberFormatTransformer extends AbstractDataImportHandlerTestCase {
private char GROUPING_SEP = new DecimalFormatSymbols().getGroupingSeparator();
private char DECIMAL_SEP = new DecimalFormatSymbols().getDecimalSeparator();
private char GROUPING_SEP = new DecimalFormatSymbols(Locale.ROOT).getGroupingSeparator();
private char DECIMAL_SEP = new DecimalFormatSymbols(Locale.ROOT).getDecimalSeparator();
@Test
@SuppressWarnings("unchecked")
@Test
public void testTransformRow_SingleNumber() {
char GERMAN_GROUPING_SEP = new DecimalFormatSymbols(Locale.GERMANY).getGroupingSeparator();
List l = new ArrayList();
List<Map<String, String>> l = new ArrayList<Map<String, String>>();
l.add(createMap("column", "num",
NumberFormatTransformer.FORMAT_STYLE, NumberFormatTransformer.NUMBER));
l.add(createMap("column", "localizedNum",
NumberFormatTransformer.FORMAT_STYLE, NumberFormatTransformer.NUMBER, NumberFormatTransformer.LOCALE, "de-DE"));
Context c = getContext(null, null, null, Context.FULL_DUMP, l, null);
Map m = createMap("num", "123" + GROUPING_SEP + "567", "localizedNum", "123" + GERMAN_GROUPING_SEP + "567");
Map<String,Object> m = createMap("num", "123" + GROUPING_SEP + "567", "localizedNum", "123" + GERMAN_GROUPING_SEP + "567");
new NumberFormatTransformer().transformRow(m, c);
assertEquals(new Long(123567), m.get("num"));
assertEquals(new Long(123567), m.get("localizedNum"));
@ -55,16 +55,16 @@ public class TestNumberFormatTransformer extends AbstractDataImportHandlerTestCa
@Test
@SuppressWarnings("unchecked")
public void testTransformRow_MultipleNumbers() throws Exception {
List fields = new ArrayList();
List<Map<String, String>> fields = new ArrayList<Map<String, String>>();
fields.add(createMap(DataImporter.COLUMN, "inputs"));
fields.add(createMap(DataImporter.COLUMN,
"outputs", RegexTransformer.SRC_COL_NAME, "inputs",
NumberFormatTransformer.FORMAT_STYLE, NumberFormatTransformer.NUMBER));
List inputs = new ArrayList();
List<String> inputs = new ArrayList<String>();
inputs.add("123" + GROUPING_SEP + "567");
inputs.add("245" + GROUPING_SEP + "678");
Map row = createMap("inputs", inputs);
Map<String, Object> row = createMap("inputs", inputs);
VariableResolver resolver = new VariableResolver();
resolver.addNamespace("e", row);
@ -72,10 +72,10 @@ public class TestNumberFormatTransformer extends AbstractDataImportHandlerTestCa
Context context = getContext(null, resolver, null, Context.FULL_DUMP, fields, null);
new NumberFormatTransformer().transformRow(row, context);
List output = new ArrayList();
List<Long> output = new ArrayList<Long>();
output.add(new Long(123567));
output.add(new Long(245678));
Map outputRow = createMap("inputs", inputs, "outputs", output);
Map<String, Object> outputRow = createMap("inputs", inputs, "outputs", output);
assertEquals(outputRow, row);
}
@ -83,77 +83,77 @@ public class TestNumberFormatTransformer extends AbstractDataImportHandlerTestCa
@Test(expected = DataImportHandlerException.class)
@SuppressWarnings("unchecked")
public void testTransformRow_InvalidInput1_Number() {
List l = new ArrayList();
List<Map<String, String>> l = new ArrayList<Map<String, String>>();
l.add(createMap("column", "num",
NumberFormatTransformer.FORMAT_STYLE, NumberFormatTransformer.NUMBER));
Context c = getContext(null, null, null, Context.FULL_DUMP, l, null);
Map m = createMap("num", "123" + GROUPING_SEP + "5a67");
Map<String, Object> m = createMap("num", "123" + GROUPING_SEP + "5a67");
new NumberFormatTransformer().transformRow(m, c);
}
@Test(expected = DataImportHandlerException.class)
@SuppressWarnings("unchecked")
public void testTransformRow_InvalidInput2_Number() {
List l = new ArrayList();
List<Map<String, String>> l = new ArrayList<Map<String, String>>();
l.add(createMap("column", "num",
NumberFormatTransformer.FORMAT_STYLE, NumberFormatTransformer.NUMBER));
Context c = getContext(null, null, null, Context.FULL_DUMP, l, null);
Map m = createMap("num", "123" + GROUPING_SEP + "567b");
Map<String, Object> m = createMap("num", "123" + GROUPING_SEP + "567b");
new NumberFormatTransformer().transformRow(m, c);
}
@Test(expected = DataImportHandlerException.class)
@SuppressWarnings("unchecked")
public void testTransformRow_InvalidInput2_Currency() {
List l = new ArrayList();
List<Map<String, String>> l = new ArrayList<Map<String, String>>();
l.add(createMap("column", "num",
NumberFormatTransformer.FORMAT_STYLE, NumberFormatTransformer.CURRENCY));
Context c = getContext(null, null, null, Context.FULL_DUMP, l, null);
Map m = createMap("num", "123" + GROUPING_SEP + "567b");
Map<String, Object> m = createMap("num", "123" + GROUPING_SEP + "567b");
new NumberFormatTransformer().transformRow(m, c);
}
@Test(expected = DataImportHandlerException.class)
@SuppressWarnings("unchecked")
public void testTransformRow_InvalidInput1_Percent() {
List l = new ArrayList();
List<Map<String, String>> l = new ArrayList<Map<String, String>>();
l.add(createMap("column", "num",
NumberFormatTransformer.FORMAT_STYLE, NumberFormatTransformer.PERCENT));
Context c = getContext(null, null, null, Context.FULL_DUMP, l, null);
Map m = createMap("num", "123" + GROUPING_SEP + "5a67");
Map<String, Object> m = createMap("num", "123" + GROUPING_SEP + "5a67");
new NumberFormatTransformer().transformRow(m, c);
}
@Test(expected = DataImportHandlerException.class)
@SuppressWarnings("unchecked")
public void testTransformRow_InvalidInput3_Currency() {
List l = new ArrayList();
List<Map<String, String>> l = new ArrayList<Map<String, String>>();
l.add(createMap("column", "num",
NumberFormatTransformer.FORMAT_STYLE, NumberFormatTransformer.CURRENCY));
Context c = getContext(null, null, null, Context.FULL_DUMP, l, null);
Map m = createMap("num", "123" + DECIMAL_SEP + "456" + DECIMAL_SEP + "789");
Map<String, Object> m = createMap("num", "123" + DECIMAL_SEP + "456" + DECIMAL_SEP + "789");
new NumberFormatTransformer().transformRow(m, c);
}
@Test(expected = DataImportHandlerException.class)
@SuppressWarnings("unchecked")
public void testTransformRow_InvalidInput3_Number() {
List l = new ArrayList();
List<Map<String, String>> l = new ArrayList<Map<String, String>>();
l.add(createMap("column", "num",
NumberFormatTransformer.FORMAT_STYLE, NumberFormatTransformer.NUMBER));
Context c = getContext(null, null, null, Context.FULL_DUMP, l, null);
Map m = createMap("num", "123" + DECIMAL_SEP + "456" + DECIMAL_SEP + "789");
Map<String, Object> m = createMap("num", "123" + DECIMAL_SEP + "456" + DECIMAL_SEP + "789");
new NumberFormatTransformer().transformRow(m, c);
}
@Test
@SuppressWarnings("unchecked")
public void testTransformRow_MalformedInput_Number() {
List l = new ArrayList();
List<Map<String, String>> l = new ArrayList<Map<String, String>>();
l.add(createMap("column", "num",
NumberFormatTransformer.FORMAT_STYLE, NumberFormatTransformer.NUMBER));
Context c = getContext(null, null, null, Context.FULL_DUMP, l, null);
Map m = createMap("num", "123" + GROUPING_SEP + GROUPING_SEP + "789");
Map<String, Object> m = createMap("num", "123" + GROUPING_SEP + GROUPING_SEP + "789");
new NumberFormatTransformer().transformRow(m, c);
assertEquals(new Long(123789), m.get("num"));
}