NIFI-6529 Updated TestFormatUtils.testFormatDataSize to use DecimalFormatSymbols when verifying results of FormatUtils.formatDataSize

NIFI-6529 Updated tests that fail with non en_US locales

Signed-off-by: Pierre Villard <pierre.villard.fr@gmail.com>

This closes #3639.
This commit is contained in:
Jeff Storck 2019-08-05 15:25:13 -04:00 committed by Pierre Villard
parent 2491c51d34
commit 403c07d4e8
No known key found for this signature in database
GPG Key ID: BEE1599F0726E9CD
4 changed files with 171 additions and 91 deletions

View File

@ -19,6 +19,7 @@ package org.apache.nifi.processor;
import org.apache.nifi.util.FormatUtils; import org.apache.nifi.util.FormatUtils;
import org.junit.Test; import org.junit.Test;
import java.text.DecimalFormatSymbols;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -87,18 +88,19 @@ public class TestFormatUtils {
@Test @Test
public void testFormatDataSize() { public void testFormatDataSize() {
DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance();
assertEquals("0 bytes", FormatUtils.formatDataSize(0d)); assertEquals("0 bytes", FormatUtils.formatDataSize(0d));
assertEquals("10.4 bytes", FormatUtils.formatDataSize(10.4d)); assertEquals(String.format("10%s4 bytes", decimalFormatSymbols.getDecimalSeparator()), FormatUtils.formatDataSize(10.4d));
assertEquals("1,024 bytes", FormatUtils.formatDataSize(1024d)); assertEquals(String.format("1%s024 bytes", decimalFormatSymbols.getGroupingSeparator()), FormatUtils.formatDataSize(1024d));
assertEquals("1 KB", FormatUtils.formatDataSize(1025d)); assertEquals("1 KB", FormatUtils.formatDataSize(1025d));
assertEquals("1.95 KB", FormatUtils.formatDataSize(2000d)); assertEquals(String.format("1%s95 KB", decimalFormatSymbols.getDecimalSeparator()), FormatUtils.formatDataSize(2000d));
assertEquals("195.31 KB", FormatUtils.formatDataSize(200_000d)); assertEquals(String.format("195%s31 KB", decimalFormatSymbols.getDecimalSeparator()), FormatUtils.formatDataSize(200_000d));
assertEquals("190.73 MB", FormatUtils.formatDataSize(200_000_000d)); assertEquals(String.format("190%s73 MB", decimalFormatSymbols.getDecimalSeparator()), FormatUtils.formatDataSize(200_000_000d));
assertEquals("186.26 GB", FormatUtils.formatDataSize(200_000_000_000d)); assertEquals(String.format("186%s26 GB", decimalFormatSymbols.getDecimalSeparator()), FormatUtils.formatDataSize(200_000_000_000d));
assertEquals("181.9 TB", FormatUtils.formatDataSize(200_000_000_000_000d)); assertEquals(String.format("181%s9 TB", decimalFormatSymbols.getDecimalSeparator()), FormatUtils.formatDataSize(200_000_000_000_000d));
} }
} }

View File

@ -45,6 +45,9 @@ import java.net.ConnectException;
import java.sql.Date; import java.sql.Date;
import java.sql.Time; import java.sql.Time;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -68,34 +71,36 @@ public class TestPutElasticsearchHttpRecord {
@Test @Test
public void testPutElasticSearchOnTriggerIndex() throws IOException { public void testPutElasticSearchOnTriggerIndex() throws IOException {
PutElasticsearchHttpRecordTestProcessor processor = new PutElasticsearchHttpRecordTestProcessor(false); PutElasticsearchHttpRecordTestProcessor processor = new PutElasticsearchHttpRecordTestProcessor(false);
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("h:m a");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("d/M/YYYY h:m a");
processor.setRecordChecks(record -> { processor.setRecordChecks(record -> {
assertEquals(1, record.get("id")); assertEquals(1, record.get("id"));
assertEquals("reç1", record.get("name")); assertEquals("reç1", record.get("name"));
assertEquals(101, record.get("code")); assertEquals(101, record.get("code"));
assertEquals("20/12/2018", record.get("date")); assertEquals("20/12/2018", record.get("date"));
assertEquals("6:55 PM", record.get("time")); assertEquals(LocalTime.of(18, 55).format(timeFormatter), record.get("time"));
assertEquals("20/12/2018 6:55 PM", record.get("ts")); assertEquals(LocalDateTime.of(2018, 12, 20, 18, 55).format(dateTimeFormatter), record.get("ts"));
}, record -> { }, record -> {
assertEquals(2, record.get("id")); assertEquals(2, record.get("id"));
assertEquals("reç2", record.get("name")); assertEquals("reç2", record.get("name"));
assertEquals(102, record.get("code")); assertEquals(102, record.get("code"));
assertEquals("20/12/2018", record.get("date")); assertEquals("20/12/2018", record.get("date"));
assertEquals("6:55 PM", record.get("time")); assertEquals(LocalTime.of(18, 55).format(timeFormatter), record.get("time"));
assertEquals("20/12/2018 6:55 PM", record.get("ts")); assertEquals(LocalDateTime.of(2018, 12, 20, 18, 55).format(dateTimeFormatter), record.get("ts"));
}, record -> { }, record -> {
assertEquals(3, record.get("id")); assertEquals(3, record.get("id"));
assertEquals("reç3", record.get("name")); assertEquals("reç3", record.get("name"));
assertEquals(103, record.get("code")); assertEquals(103, record.get("code"));
assertEquals("20/12/2018", record.get("date")); assertEquals("20/12/2018", record.get("date"));
assertEquals("6:55 PM", record.get("time")); assertEquals(LocalTime.of(18, 55).format(timeFormatter), record.get("time"));
assertEquals("20/12/2018 6:55 PM", record.get("ts")); assertEquals(LocalDateTime.of(2018, 12, 20, 18, 55).format(dateTimeFormatter), record.get("ts"));
}, record -> { }, record -> {
assertEquals(4, record.get("id")); assertEquals(4, record.get("id"));
assertEquals("reç4", record.get("name")); assertEquals("reç4", record.get("name"));
assertEquals(104, record.get("code")); assertEquals(104, record.get("code"));
assertEquals("20/12/2018", record.get("date")); assertEquals("20/12/2018", record.get("date"));
assertEquals("6:55 PM", record.get("time")); assertEquals(LocalTime.of(18, 55).format(timeFormatter), record.get("time"));
assertEquals("20/12/2018 6:55 PM", record.get("ts")); assertEquals(LocalDateTime.of(2018, 12, 20, 18, 55).format(dateTimeFormatter), record.get("ts"));
}); });
runner = TestRunners.newTestRunner(processor); // no failures runner = TestRunners.newTestRunner(processor); // no failures
generateTestData(); generateTestData();

View File

@ -27,6 +27,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.text.DecimalFormatSymbols;
import java.util.Map; import java.util.Map;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -93,7 +94,7 @@ public class ExtractImageMetadataTest {
assertEquals("8", attributes.get(PNG_HEADER + "IHDR.Image Width")); assertEquals("8", attributes.get(PNG_HEADER + "IHDR.Image Width"));
assertEquals("12", attributes.get(PNG_HEADER + "IHDR.Image Height")); assertEquals("12", attributes.get(PNG_HEADER + "IHDR.Image Height"));
assertEquals("0.455", attributes.get(PNG_HEADER + "gAMA.Image Gamma")); assertEquals(String.format("0%s455", DecimalFormatSymbols.getInstance().getDecimalSeparator()), attributes.get(PNG_HEADER + "gAMA.Image Gamma"));
assertEquals("Deflate", attributes.get(PNG_HEADER + "IHDR.Compression Type")); assertEquals("Deflate", attributes.get(PNG_HEADER + "IHDR.Compression Type"));
assertEquals("No Interlace", attributes.get(PNG_HEADER + "IHDR.Interlace Method")); assertEquals("No Interlace", attributes.get(PNG_HEADER + "IHDR.Interlace Method"));
assertEquals("Perceptual", attributes.get(PNG_HEADER + "sRGB.sRGB Rendering Intent")); assertEquals("Perceptual", attributes.get(PNG_HEADER + "sRGB.sRGB Rendering Intent"));

View File

@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.File; import java.io.File;
import java.text.DecimalFormatSymbols;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.HashMap; import java.util.HashMap;
@ -125,15 +126,24 @@ public class ConvertExcelToCSVProcessorTest {
assertTrue(rowsSheet == 9); assertTrue(rowsSheet == 9);
LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0); LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0);
DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance();
char decimalSeparator = decimalFormatSymbols.getDecimalSeparator();
char groupingSeparator = decimalFormatSymbols.getGroupingSeparator();
ff.assertContentEquals("Numbers,Timestamps,Money\n" + ff.assertContentEquals("Numbers,Timestamps,Money\n" +
"1234.456," + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + ",$ 123.45\n" + addQuotingIfNeeded(String.format("1234%1$s456", decimalSeparator)) + "," + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + "," +
"1234.46," + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + ",£ 123.45\n" + addQuotingIfNeeded(String.format("$ 123%1$s45", decimalSeparator)) + "\n" +
"1234.5,\"" + DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy").format(localDt) + "\",¥ 123.45\n" + addQuotingIfNeeded(String.format("1234%1$s46", decimalSeparator)) + "," + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + "," +
"\"1,234.46\"," + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + ",\"$ 1,023.45\"\n" + addQuotingIfNeeded(String.format("£ 123%1$s45", decimalSeparator)) + "\n" +
"\"1,234.4560\"," + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + ",\"£ 1,023.45\"\n" + addQuotingIfNeeded(String.format("1234%1$s5", decimalSeparator)) + ",\"" + DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy").format(localDt) + "\"," +
"9.88E+08," + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + ",\"¥ 1,023.45\"\n" + addQuotingIfNeeded(String.format("¥ 123%1$s45", decimalSeparator)) + "\n" +
"9.877E+08,,\n" + addQuotingIfNeeded(String.format("1%2$s234%1$s46", decimalSeparator, groupingSeparator)) + "," + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + "," +
"9.8765E+08,,\n"); addQuotingIfNeeded(String.format("$ 1%2$s023%1$s45", decimalSeparator, groupingSeparator)) + "\n" +
addQuotingIfNeeded(String.format("1%2$s234%1$s4560", decimalSeparator, groupingSeparator)) + "," + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + "," +
addQuotingIfNeeded(String.format("£ 1%2$s023%1$s45", decimalSeparator, groupingSeparator)) + "\n" +
addQuotingIfNeeded(String.format("9%1$s88E+08", decimalSeparator)) + "," + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + "," +
addQuotingIfNeeded(String.format("¥ 1%2$s023%1$s45", decimalSeparator, groupingSeparator)) + "\n" +
addQuotingIfNeeded(String.format("9%1$s877E+08", decimalSeparator)) + ",,\n" +
addQuotingIfNeeded(String.format("9%1$s8765E+08", decimalSeparator)) + ",,\n");
} }
@Test @Test
@ -154,13 +164,16 @@ public class ConvertExcelToCSVProcessorTest {
assertEquals("Row count does match expected value.", "7", rowsSheet.toString()); assertEquals("Row count does match expected value.", "7", rowsSheet.toString());
LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0); LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0);
ff.assertContentEquals("1234.46," + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + ",£ 123.45\n" + DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance();
"1234.5," + DateTimeFormatter.ofPattern("EEEE\\, MMMM dd\\, yyyy").format(localDt) + ",¥ 123.45\n" + String decimalSeparator = decimalFormatSymbols.getDecimalSeparator() == ',' ? "\\," : String.valueOf(decimalFormatSymbols.getDecimalSeparator());
"1\\,234.46," + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + ",$ 1\\,023.45\n" + String groupingSeparator = decimalFormatSymbols.getGroupingSeparator() == ',' ? "\\," : String.valueOf(decimalFormatSymbols.getGroupingSeparator());
"1\\,234.4560," + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + ",£ 1\\,023.45\n" + ff.assertContentEquals(String.format("1234%1$s46," + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + ",£ 123%1$s45\n" +
"9.88E+08," + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + ",¥ 1\\,023.45\n" + "1234%1$s5," + DateTimeFormatter.ofPattern("EEEE\\, MMMM dd\\, yyyy").format(localDt) + ",¥ 123%1$s45\n" +
"9.877E+08,,\n" + "1%2$s234%1$s46," + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + ",$ 1%2$s023%1$s45\n" +
"9.8765E+08,,\n"); "1%2$s234%1$s4560," + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + ",£ 1%2$s023%1$s45\n" +
"9%1$s88E+08," + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + ",¥ 1%2$s023%1$s45\n" +
"9%1$s877E+08,,\n" +
"9%1$s8765E+08,,\n", decimalSeparator, groupingSeparator));
} }
@Test @Test
@ -183,13 +196,16 @@ public class ConvertExcelToCSVProcessorTest {
assertEquals("Row count does match expected value.", "7", rowsSheet.toString()); assertEquals("Row count does match expected value.", "7", rowsSheet.toString());
LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0); LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0);
ff.assertContentEquals("1234.46," + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + ",£ 123.45\n" + DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance();
"1234.5," + DateTimeFormatter.ofPattern("EEEE\\, MMMM dd\\, yyyy").format(localDt) + ",¥ 123.45\n" + String decimalSeparator = decimalFormatSymbols.getDecimalSeparator() == ',' ? "\\," : String.valueOf(decimalFormatSymbols.getDecimalSeparator());
"1\\,234.46," + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + ",$ 1\\,023.45\n" + String groupingSeparator = decimalFormatSymbols.getGroupingSeparator() == ',' ? "\\," : String.valueOf(decimalFormatSymbols.getGroupingSeparator());
"1\\,234.4560," + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + ",£ 1\\,023.45\n" + ff.assertContentEquals(String.format("1234%1$s46," + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + ",£ 123%1$s45\n" +
"9.88E+08," + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + ",¥ 1\\,023.45\n" + "1234%1$s5," + DateTimeFormatter.ofPattern("EEEE\\, MMMM dd\\, yyyy").format(localDt) + ",¥ 123%1$s45\n" +
"9.877E+08,,\n" + "1%2$s234%1$s46," + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + ",$ 1%2$s023%1$s45\n" +
"9.8765E+08,,\n"); "1%2$s234%1$s4560," + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + ",£ 1%2$s023%1$s45\n" +
"9%1$s88E+08," + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + ",¥ 1%2$s023%1$s45\n" +
"9%1$s877E+08,,\n" +
"9%1$s8765E+08,,\n", decimalSeparator, groupingSeparator));
} }
@Test @Test
@ -209,15 +225,18 @@ public class ConvertExcelToCSVProcessorTest {
Long rowsSheet = new Long(ff.getAttribute(ConvertExcelToCSVProcessor.ROW_NUM)); Long rowsSheet = new Long(ff.getAttribute(ConvertExcelToCSVProcessor.ROW_NUM));
assertTrue(rowsSheet == 9); assertTrue(rowsSheet == 9);
ff.assertContentEquals("Numbers,Money\n" + DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance();
"1234.456,$ 123.45\n" + String decimalSeparator = decimalFormatSymbols.getDecimalSeparator() == ',' ? "\\," : String.valueOf(decimalFormatSymbols.getDecimalSeparator());
"1234.46,£ 123.45\n" + String groupingSeparator = decimalFormatSymbols.getGroupingSeparator() == ',' ? "\\," : String.valueOf(decimalFormatSymbols.getGroupingSeparator());
"1234.5,¥ 123.45\n" + ff.assertContentEquals(String.format("Numbers,Money\n" +
"1\\,234.46,$ 1\\,023.45\n" + "1234%1$s456,$ 123%1$s45\n" +
"1\\,234.4560,£ 1\\,023.45\n" + "1234%1$s46,£ 123%1$s45\n" +
"9.88E+08,¥ 1\\,023.45\n" + "1234%1$s5,¥ 123%1$s45\n" +
"9.877E+08,\n" + "1%2$s234%1$s46,$ 1%2$s023%1$s45\n" +
"9.8765E+08,\n"); "1%2$s234%1$s4560,£ 1%2$s023%1$s45\n" +
"9%1$s88E+08,¥ 1%2$s023%1$s45\n" +
"9%1$s877E+08,\n" +
"9%1$s8765E+08,\n", decimalSeparator, groupingSeparator));
} }
@Test @Test
@ -239,15 +258,18 @@ public class ConvertExcelToCSVProcessorTest {
Long rowsSheet = new Long(ff.getAttribute(ConvertExcelToCSVProcessor.ROW_NUM)); Long rowsSheet = new Long(ff.getAttribute(ConvertExcelToCSVProcessor.ROW_NUM));
assertTrue(rowsSheet == 9); assertTrue(rowsSheet == 9);
ff.assertContentEquals("Numbers,Money\n" + DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance();
"1234.456,$ 123.45\n" + String decimalSeparator = decimalFormatSymbols.getDecimalSeparator() == ',' ? "\\," : String.valueOf(decimalFormatSymbols.getDecimalSeparator());
"1234.46,£ 123.45\n" + String groupingSeparator = decimalFormatSymbols.getGroupingSeparator() == ',' ? "\\," : String.valueOf(decimalFormatSymbols.getGroupingSeparator());
"1234.5,¥ 123.45\n" + ff.assertContentEquals(String.format("Numbers,Money\n" +
"1\\,234.46,$ 1\\,023.45\n" + "1234%1$s456,$ 123%1$s45\n" +
"1\\,234.4560,£ 1\\,023.45\n" + "1234%1$s46,£ 123%1$s45\n" +
"9.88E+08,¥ 1\\,023.45\n" + "1234%1$s5,¥ 123%1$s45\n" +
"9.877E+08,\n" + "1%2$s234%1$s46,$ 1%2$s023%1$s45\n" +
"9.8765E+08,\n"); "1%2$s234%1$s4560,£ 1%2$s023%1$s45\n" +
"9%1$s88E+08,¥ 1%2$s023%1$s45\n" +
"9%1$s877E+08,\n" +
"9%1$s8765E+08,\n", decimalSeparator, groupingSeparator));
} }
@Test @Test
@ -269,15 +291,21 @@ public class ConvertExcelToCSVProcessorTest {
assertTrue(rowsSheet == 9); assertTrue(rowsSheet == 9);
LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0); LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0);
ff.assertContentEquals("Numbers|Timestamps|Money\r\n" + DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance();
"1234.456|" + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + "|$ 123.45\r\n" + String valueSeparator = testRunner.getProcessContext().getProperty(CSVUtils.VALUE_SEPARATOR).evaluateAttributeExpressions(ff).getValue();
"1234.46|" + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + "|£ 123.45\r\n" + String decimalSeparator = (String.valueOf(decimalFormatSymbols.getDecimalSeparator()).equals(valueSeparator))
"1234.5|" + DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy").format(localDt) + "|¥ 123.45\r\n" + ? ("\\" + decimalFormatSymbols.getDecimalSeparator()) : String.valueOf(decimalFormatSymbols.getDecimalSeparator());
"1,234.46|" + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + "|$ 1,023.45\r\n" + String groupingSeparator = String.valueOf(decimalFormatSymbols.getGroupingSeparator()).equals(valueSeparator)
"1,234.4560|" + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + "|£ 1,023.45\r\n" + ? "\\" + decimalFormatSymbols.getGroupingSeparator() : String.valueOf(decimalFormatSymbols.getGroupingSeparator());
"9.88E+08|" + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + "|¥ 1,023.45\r\n" + ff.assertContentEquals(String.format("Numbers|Timestamps|Money\r\n" +
"9.877E+08||\r\n" + "1234%1$s456|" + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + "|$ 123%1$s45\r\n" +
"9.8765E+08||\r\n"); "1234%1$s46|" + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + "|£ 123%1$s45\r\n" +
"1234%1$s5|" + DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy").format(localDt) + "|¥ 123%1$s45\r\n" +
"1%2$s234%1$s46|" + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + "|$ 1%2$s023%1$s45\r\n" +
"1%2$s234%1$s4560|" + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + "|£ 1%2$s023%1$s45\r\n" +
"9%1$s88E+08|" + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + "|¥ 1%2$s023%1$s45\r\n" +
"9%1$s877E+08||\r\n" +
"9%1$s8765E+08||\r\n", decimalSeparator, groupingSeparator));
} }
@Test @Test
@ -300,15 +328,21 @@ public class ConvertExcelToCSVProcessorTest {
assertTrue(rowsSheet == 9); assertTrue(rowsSheet == 9);
LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0); LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0);
ff.assertContentEquals("Numbers|Timestamps|Money\n" + DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance();
"1234.456|" + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + "|$ 123.45\n" + String valueSeparator = testRunner.getProcessContext().getProperty(CSVUtils.VALUE_SEPARATOR).evaluateAttributeExpressions(ff).getValue();
"1234.46|" + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + "|£ 123.45\n" + String decimalSeparator = (String.valueOf(decimalFormatSymbols.getDecimalSeparator()).equals(valueSeparator))
"1234.5|" + DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy").format(localDt) + "|¥ 123.45\n" + ? ("\\" + decimalFormatSymbols.getDecimalSeparator()) : String.valueOf(decimalFormatSymbols.getDecimalSeparator());
"1,234.46|" + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + "|$ 1,023.45\n" + String groupingSeparator = String.valueOf(decimalFormatSymbols.getGroupingSeparator()).equals(valueSeparator)
"1,234.4560|" + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + "|£ 1,023.45\n" + ? "\\" + decimalFormatSymbols.getGroupingSeparator() : String.valueOf(decimalFormatSymbols.getGroupingSeparator());
"9.88E+08|" + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + "|¥ 1,023.45\n" + ff.assertContentEquals(String.format("Numbers|Timestamps|Money\n" +
"9.877E+08||\n" + "1234%1$s456|" + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + "|$ 123%1$s45\n" +
"9.8765E+08||\n"); "1234%1$s46|" + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + "|£ 123%1$s45\n" +
"1234%1$s5|" + DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy").format(localDt) + "|¥ 123%1$s45\n" +
"1%2$s234%1$s46|" + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + "|$ 1%2$s023%1$s45\n" +
"1%2$s234%1$s4560|" + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + "|£ 1%2$s023%1$s45\n" +
"9%1$s88E+08|" + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + "|¥ 1%2$s023%1$s45\n" +
"9%1$s877E+08||\n" +
"9%1$s8765E+08||\n", decimalSeparator, groupingSeparator));
} }
@Test @Test
@ -332,15 +366,31 @@ public class ConvertExcelToCSVProcessorTest {
assertTrue(rowsSheet == 9); assertTrue(rowsSheet == 9);
LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0); LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0);
String quoteCharValue = testRunner.getProcessContext().getProperty(CSVUtils.QUOTE_CHAR).evaluateAttributeExpressions(ff).getValue();
DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance();
char decimalSeparator = decimalFormatSymbols.getDecimalSeparator();
char groupingSeparator = decimalFormatSymbols.getGroupingSeparator();
ff.assertContentEquals("'Numbers','Timestamps','Money'\n" + ff.assertContentEquals("'Numbers','Timestamps','Money'\n" +
"'1234.456','" + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + "','$ 123.45'\n" + addQuotingIfNeeded(String.format("1234%1$s456", decimalSeparator), ",", quoteCharValue, true) + "," + quoteCharValue +
"'1234.46','" + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + "','£ 123.45'\n" + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + quoteCharValue + "," +
"'1234.5','" + DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy").format(localDt) + "','¥ 123.45'\n" + addQuotingIfNeeded(String.format("$ 123%1$s45", decimalSeparator), ",", quoteCharValue, true) + "\n" +
"'1,234.46','" + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + "','$ 1,023.45'\n" + addQuotingIfNeeded(String.format("1234%1$s46", decimalSeparator), ",", quoteCharValue, true) + "," + quoteCharValue +
"'1,234.4560','" + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + "','£ 1,023.45'\n" + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + quoteCharValue + "," +
"'9.88E+08','" + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + "','¥ 1,023.45'\n" + addQuotingIfNeeded(String.format("£ 123%1$s45", decimalSeparator), ",", quoteCharValue, true) + "\n" +
"'9.877E+08',,\n" + addQuotingIfNeeded(String.format("1234%1$s5", decimalSeparator), ",", quoteCharValue, true) + "," + quoteCharValue +
"'9.8765E+08',,\n"); DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy").format(localDt) + quoteCharValue + "," +
addQuotingIfNeeded(String.format("¥ 123%1$s45", decimalSeparator), ",", quoteCharValue, true) + "\n" +
addQuotingIfNeeded(String.format("1%2$s234%1$s46", decimalSeparator, groupingSeparator), ",", quoteCharValue, true) + "," + quoteCharValue +
DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + quoteCharValue + "," +
addQuotingIfNeeded(String.format("$ 1%2$s023%1$s45", decimalSeparator, groupingSeparator), ",", quoteCharValue, true) + "\n" +
addQuotingIfNeeded(String.format("1%2$s234%1$s4560", decimalSeparator, groupingSeparator), ",", quoteCharValue, true) + "," + quoteCharValue +
DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + quoteCharValue + "," +
addQuotingIfNeeded(String.format("£ 1%2$s023%1$s45", decimalSeparator, groupingSeparator), ",", quoteCharValue, true) + "\n" +
addQuotingIfNeeded(String.format("9%1$s88E+08", decimalSeparator), ",", quoteCharValue, true) + "," + quoteCharValue +
DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + quoteCharValue + "," +
addQuotingIfNeeded(String.format("¥ 1%2$s023%1$s45", decimalSeparator, groupingSeparator), ",", quoteCharValue, true) + "\n" +
addQuotingIfNeeded(String.format("9%1$s877E+08", decimalSeparator), ",", quoteCharValue, true) + ",,\n" +
addQuotingIfNeeded(String.format("9%1$s8765E+08", decimalSeparator), ",", quoteCharValue, true) + ",,\n");
} }
@Test @Test
@ -363,15 +413,21 @@ public class ConvertExcelToCSVProcessorTest {
assertTrue(rowsSheet == 9); assertTrue(rowsSheet == 9);
LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0); LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0);
ff.assertContentEquals("Numbers,Timestamps,Money\n" + DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance();
"1234.456," + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + ",$ 123.45\n" + String escapeCharValue = testRunner.getProcessContext().getProperty(CSVUtils.ESCAPE_CHAR).evaluateAttributeExpressions(ff).getValue();
"1234.46," + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + ",£ 123.45\n" + String decimalSeparator = String.valueOf(decimalFormatSymbols.getDecimalSeparator()).equals(",")
"1234.5," + DateTimeFormatter.ofPattern("EEEE^, MMMM dd^, yyyy").format(localDt) + ",¥ 123.45\n" + ? escapeCharValue + decimalFormatSymbols.getDecimalSeparator() : String.valueOf(decimalFormatSymbols.getDecimalSeparator());
"1^,234.46," + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + ",$ 1^,023.45\n" + String groupingSeparator = String.valueOf(decimalFormatSymbols.getGroupingSeparator()).equals(",")
"1^,234.4560," + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + ",£ 1^,023.45\n" + ? escapeCharValue + decimalFormatSymbols.getGroupingSeparator() : String.valueOf(decimalFormatSymbols.getGroupingSeparator());
"9.88E+08," + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + ",¥ 1^,023.45\n" + ff.assertContentEquals(String.format("Numbers,Timestamps,Money\n" +
"9.877E+08,,\n" + "1234%1$s456," + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + ",$ 123%1$s45\n" +
"9.8765E+08,,\n"); "1234%1$s46," + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + ",£ 123%1$s45\n" +
"1234%1$s5," + DateTimeFormatter.ofPattern(String.format("EEEE%1$s, MMMM dd%1$s, yyyy", escapeCharValue)).format(localDt) + ",¥ 123%1$s45\n" +
"1%2$s234%1$s46," + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + ",$ 1%2$s023%1$s45\n" +
"1%2$s234%1$s4560," + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + ",£ 1%2$s023%1$s45\n" +
"9%1$s88E+08," + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + ",¥ 1%2$s023%1$s45\n" +
"9%1$s877E+08,,\n" +
"9%1$s8765E+08,,\n", decimalSeparator, groupingSeparator));
} }
/** /**
@ -496,4 +552,20 @@ public class ConvertExcelToCSVProcessorTest {
String messageText = errorMessages.get(0).getMsg(); String messageText = errorMessages.get(0).getMsg();
Assert.assertTrue(messageText.contains("Excel") && messageText.contains("OLE2")); Assert.assertTrue(messageText.contains("Excel") && messageText.contains("OLE2"));
} }
private String addQuotingIfNeeded(String csvField) {
return addQuotingIfNeeded(csvField, ",");
}
private String addQuotingIfNeeded(String csvField, String csvSeparator) {
return addQuotingIfNeeded(csvField, csvSeparator, "\"");
}
private String addQuotingIfNeeded(String csvField, String csvSeparator, String csvQuote) {
return addQuotingIfNeeded(csvField, csvSeparator, csvQuote, false);
}
private String addQuotingIfNeeded(String csvField, String csvSeparator, String csvQuote, boolean force) {
return csvField.contains(csvSeparator) || force ? String.format("%2$s%1$s%2$s", csvField, csvQuote) : csvField;
}
} }