diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/processor/TestFormatUtils.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/processor/TestFormatUtils.java index fc80d8561b..deb4b9ed24 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/processor/TestFormatUtils.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/processor/TestFormatUtils.java @@ -19,6 +19,7 @@ package org.apache.nifi.processor; import org.apache.nifi.util.FormatUtils; import org.junit.Test; +import java.text.DecimalFormatSymbols; import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertEquals; @@ -87,18 +88,19 @@ public class TestFormatUtils { @Test public void testFormatDataSize() { + DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(); assertEquals("0 bytes", FormatUtils.formatDataSize(0d)); - assertEquals("10.4 bytes", FormatUtils.formatDataSize(10.4d)); - assertEquals("1,024 bytes", FormatUtils.formatDataSize(1024d)); + assertEquals(String.format("10%s4 bytes", decimalFormatSymbols.getDecimalSeparator()), FormatUtils.formatDataSize(10.4d)); + assertEquals(String.format("1%s024 bytes", decimalFormatSymbols.getGroupingSeparator()), FormatUtils.formatDataSize(1024d)); assertEquals("1 KB", FormatUtils.formatDataSize(1025d)); - assertEquals("1.95 KB", FormatUtils.formatDataSize(2000d)); - assertEquals("195.31 KB", FormatUtils.formatDataSize(200_000d)); + assertEquals(String.format("1%s95 KB", decimalFormatSymbols.getDecimalSeparator()), FormatUtils.formatDataSize(2000d)); + 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)); } } diff --git a/nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-processors/src/test/java/org/apache/nifi/processors/elasticsearch/TestPutElasticsearchHttpRecord.java b/nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-processors/src/test/java/org/apache/nifi/processors/elasticsearch/TestPutElasticsearchHttpRecord.java index 1df672ff2c..afe20550fa 100644 --- a/nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-processors/src/test/java/org/apache/nifi/processors/elasticsearch/TestPutElasticsearchHttpRecord.java +++ b/nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-processors/src/test/java/org/apache/nifi/processors/elasticsearch/TestPutElasticsearchHttpRecord.java @@ -45,6 +45,9 @@ import java.net.ConnectException; import java.sql.Date; import java.sql.Time; import java.sql.Timestamp; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -68,34 +71,36 @@ public class TestPutElasticsearchHttpRecord { @Test public void testPutElasticSearchOnTriggerIndex() throws IOException { 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 -> { assertEquals(1, record.get("id")); assertEquals("reç1", record.get("name")); assertEquals(101, record.get("code")); assertEquals("20/12/2018", record.get("date")); - assertEquals("6:55 PM", record.get("time")); - assertEquals("20/12/2018 6:55 PM", record.get("ts")); + assertEquals(LocalTime.of(18, 55).format(timeFormatter), record.get("time")); + assertEquals(LocalDateTime.of(2018, 12, 20, 18, 55).format(dateTimeFormatter), record.get("ts")); }, record -> { assertEquals(2, record.get("id")); assertEquals("reç2", record.get("name")); assertEquals(102, record.get("code")); assertEquals("20/12/2018", record.get("date")); - assertEquals("6:55 PM", record.get("time")); - assertEquals("20/12/2018 6:55 PM", record.get("ts")); + assertEquals(LocalTime.of(18, 55).format(timeFormatter), record.get("time")); + assertEquals(LocalDateTime.of(2018, 12, 20, 18, 55).format(dateTimeFormatter), record.get("ts")); }, record -> { assertEquals(3, record.get("id")); assertEquals("reç3", record.get("name")); assertEquals(103, record.get("code")); assertEquals("20/12/2018", record.get("date")); - assertEquals("6:55 PM", record.get("time")); - assertEquals("20/12/2018 6:55 PM", record.get("ts")); + assertEquals(LocalTime.of(18, 55).format(timeFormatter), record.get("time")); + assertEquals(LocalDateTime.of(2018, 12, 20, 18, 55).format(dateTimeFormatter), record.get("ts")); }, record -> { assertEquals(4, record.get("id")); assertEquals("reç4", record.get("name")); assertEquals(104, record.get("code")); assertEquals("20/12/2018", record.get("date")); - assertEquals("6:55 PM", record.get("time")); - assertEquals("20/12/2018 6:55 PM", record.get("ts")); + assertEquals(LocalTime.of(18, 55).format(timeFormatter), record.get("time")); + assertEquals(LocalDateTime.of(2018, 12, 20, 18, 55).format(dateTimeFormatter), record.get("ts")); }); runner = TestRunners.newTestRunner(processor); // no failures generateTestData(); diff --git a/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/image/ExtractImageMetadataTest.java b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/image/ExtractImageMetadataTest.java index ee45f76ad8..40af4aeaaf 100644 --- a/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/image/ExtractImageMetadataTest.java +++ b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/image/ExtractImageMetadataTest.java @@ -27,6 +27,7 @@ import java.io.File; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; +import java.text.DecimalFormatSymbols; import java.util.Map; import static org.junit.Assert.assertEquals; @@ -93,7 +94,7 @@ public class ExtractImageMetadataTest { assertEquals("8", attributes.get(PNG_HEADER + "IHDR.Image Width")); 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("No Interlace", attributes.get(PNG_HEADER + "IHDR.Interlace Method")); assertEquals("Perceptual", attributes.get(PNG_HEADER + "sRGB.sRGB Rendering Intent")); diff --git a/nifi-nar-bundles/nifi-poi-bundle/nifi-poi-processors/src/test/java/org/apache/nifi/processors/poi/ConvertExcelToCSVProcessorTest.java b/nifi-nar-bundles/nifi-poi-bundle/nifi-poi-processors/src/test/java/org/apache/nifi/processors/poi/ConvertExcelToCSVProcessorTest.java index 5314781c4c..828e94c07d 100644 --- a/nifi-nar-bundles/nifi-poi-bundle/nifi-poi-processors/src/test/java/org/apache/nifi/processors/poi/ConvertExcelToCSVProcessorTest.java +++ b/nifi-nar-bundles/nifi-poi-bundle/nifi-poi-processors/src/test/java/org/apache/nifi/processors/poi/ConvertExcelToCSVProcessorTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.io.File; +import java.text.DecimalFormatSymbols; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.HashMap; @@ -125,15 +126,24 @@ public class ConvertExcelToCSVProcessorTest { assertTrue(rowsSheet == 9); 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" + - "1234.456," + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + ",$ 123.45\n" + - "1234.46," + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + ",£ 123.45\n" + - "1234.5,\"" + DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy").format(localDt) + "\",¥ 123.45\n" + - "\"1,234.46\"," + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + ",\"$ 1,023.45\"\n" + - "\"1,234.4560\"," + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + ",\"£ 1,023.45\"\n" + - "9.88E+08," + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + ",\"¥ 1,023.45\"\n" + - "9.877E+08,,\n" + - "9.8765E+08,,\n"); + addQuotingIfNeeded(String.format("1234%1$s456", decimalSeparator)) + "," + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + "," + + addQuotingIfNeeded(String.format("$ 123%1$s45", decimalSeparator)) + "\n" + + addQuotingIfNeeded(String.format("1234%1$s46", decimalSeparator)) + "," + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + "," + + addQuotingIfNeeded(String.format("£ 123%1$s45", decimalSeparator)) + "\n" + + addQuotingIfNeeded(String.format("1234%1$s5", decimalSeparator)) + ",\"" + DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy").format(localDt) + "\"," + + addQuotingIfNeeded(String.format("¥ 123%1$s45", decimalSeparator)) + "\n" + + addQuotingIfNeeded(String.format("1%2$s234%1$s46", decimalSeparator, groupingSeparator)) + "," + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + "," + + 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 @@ -154,13 +164,16 @@ public class ConvertExcelToCSVProcessorTest { assertEquals("Row count does match expected value.", "7", rowsSheet.toString()); 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" + - "1234.5," + DateTimeFormatter.ofPattern("EEEE\\, MMMM dd\\, yyyy").format(localDt) + ",¥ 123.45\n" + - "1\\,234.46," + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + ",$ 1\\,023.45\n" + - "1\\,234.4560," + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + ",£ 1\\,023.45\n" + - "9.88E+08," + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + ",¥ 1\\,023.45\n" + - "9.877E+08,,\n" + - "9.8765E+08,,\n"); + DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(); + String decimalSeparator = decimalFormatSymbols.getDecimalSeparator() == ',' ? "\\," : String.valueOf(decimalFormatSymbols.getDecimalSeparator()); + String groupingSeparator = decimalFormatSymbols.getGroupingSeparator() == ',' ? "\\," : String.valueOf(decimalFormatSymbols.getGroupingSeparator()); + ff.assertContentEquals(String.format("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 @@ -183,13 +196,16 @@ public class ConvertExcelToCSVProcessorTest { assertEquals("Row count does match expected value.", "7", rowsSheet.toString()); 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" + - "1234.5," + DateTimeFormatter.ofPattern("EEEE\\, MMMM dd\\, yyyy").format(localDt) + ",¥ 123.45\n" + - "1\\,234.46," + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + ",$ 1\\,023.45\n" + - "1\\,234.4560," + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + ",£ 1\\,023.45\n" + - "9.88E+08," + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + ",¥ 1\\,023.45\n" + - "9.877E+08,,\n" + - "9.8765E+08,,\n"); + DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(); + String decimalSeparator = decimalFormatSymbols.getDecimalSeparator() == ',' ? "\\," : String.valueOf(decimalFormatSymbols.getDecimalSeparator()); + String groupingSeparator = decimalFormatSymbols.getGroupingSeparator() == ',' ? "\\," : String.valueOf(decimalFormatSymbols.getGroupingSeparator()); + ff.assertContentEquals(String.format("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 @@ -209,15 +225,18 @@ public class ConvertExcelToCSVProcessorTest { Long rowsSheet = new Long(ff.getAttribute(ConvertExcelToCSVProcessor.ROW_NUM)); assertTrue(rowsSheet == 9); - ff.assertContentEquals("Numbers,Money\n" + - "1234.456,$ 123.45\n" + - "1234.46,£ 123.45\n" + - "1234.5,¥ 123.45\n" + - "1\\,234.46,$ 1\\,023.45\n" + - "1\\,234.4560,£ 1\\,023.45\n" + - "9.88E+08,¥ 1\\,023.45\n" + - "9.877E+08,\n" + - "9.8765E+08,\n"); + DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(); + String decimalSeparator = decimalFormatSymbols.getDecimalSeparator() == ',' ? "\\," : String.valueOf(decimalFormatSymbols.getDecimalSeparator()); + String groupingSeparator = decimalFormatSymbols.getGroupingSeparator() == ',' ? "\\," : String.valueOf(decimalFormatSymbols.getGroupingSeparator()); + ff.assertContentEquals(String.format("Numbers,Money\n" + + "1234%1$s456,$ 123%1$s45\n" + + "1234%1$s46,£ 123%1$s45\n" + + "1234%1$s5,¥ 123%1$s45\n" + + "1%2$s234%1$s46,$ 1%2$s023%1$s45\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 @@ -239,15 +258,18 @@ public class ConvertExcelToCSVProcessorTest { Long rowsSheet = new Long(ff.getAttribute(ConvertExcelToCSVProcessor.ROW_NUM)); assertTrue(rowsSheet == 9); - ff.assertContentEquals("Numbers,Money\n" + - "1234.456,$ 123.45\n" + - "1234.46,£ 123.45\n" + - "1234.5,¥ 123.45\n" + - "1\\,234.46,$ 1\\,023.45\n" + - "1\\,234.4560,£ 1\\,023.45\n" + - "9.88E+08,¥ 1\\,023.45\n" + - "9.877E+08,\n" + - "9.8765E+08,\n"); + DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(); + String decimalSeparator = decimalFormatSymbols.getDecimalSeparator() == ',' ? "\\," : String.valueOf(decimalFormatSymbols.getDecimalSeparator()); + String groupingSeparator = decimalFormatSymbols.getGroupingSeparator() == ',' ? "\\," : String.valueOf(decimalFormatSymbols.getGroupingSeparator()); + ff.assertContentEquals(String.format("Numbers,Money\n" + + "1234%1$s456,$ 123%1$s45\n" + + "1234%1$s46,£ 123%1$s45\n" + + "1234%1$s5,¥ 123%1$s45\n" + + "1%2$s234%1$s46,$ 1%2$s023%1$s45\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 @@ -269,15 +291,21 @@ public class ConvertExcelToCSVProcessorTest { assertTrue(rowsSheet == 9); LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0); - ff.assertContentEquals("Numbers|Timestamps|Money\r\n" + - "1234.456|" + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + "|$ 123.45\r\n" + - "1234.46|" + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + "|£ 123.45\r\n" + - "1234.5|" + DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy").format(localDt) + "|¥ 123.45\r\n" + - "1,234.46|" + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + "|$ 1,023.45\r\n" + - "1,234.4560|" + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + "|£ 1,023.45\r\n" + - "9.88E+08|" + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + "|¥ 1,023.45\r\n" + - "9.877E+08||\r\n" + - "9.8765E+08||\r\n"); + DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(); + String valueSeparator = testRunner.getProcessContext().getProperty(CSVUtils.VALUE_SEPARATOR).evaluateAttributeExpressions(ff).getValue(); + String decimalSeparator = (String.valueOf(decimalFormatSymbols.getDecimalSeparator()).equals(valueSeparator)) + ? ("\\" + decimalFormatSymbols.getDecimalSeparator()) : String.valueOf(decimalFormatSymbols.getDecimalSeparator()); + String groupingSeparator = String.valueOf(decimalFormatSymbols.getGroupingSeparator()).equals(valueSeparator) + ? "\\" + decimalFormatSymbols.getGroupingSeparator() : String.valueOf(decimalFormatSymbols.getGroupingSeparator()); + ff.assertContentEquals(String.format("Numbers|Timestamps|Money\r\n" + + "1234%1$s456|" + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + "|$ 123%1$s45\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 @@ -300,15 +328,21 @@ public class ConvertExcelToCSVProcessorTest { assertTrue(rowsSheet == 9); LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0); - ff.assertContentEquals("Numbers|Timestamps|Money\n" + - "1234.456|" + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + "|$ 123.45\n" + - "1234.46|" + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + "|£ 123.45\n" + - "1234.5|" + DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy").format(localDt) + "|¥ 123.45\n" + - "1,234.46|" + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + "|$ 1,023.45\n" + - "1,234.4560|" + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + "|£ 1,023.45\n" + - "9.88E+08|" + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + "|¥ 1,023.45\n" + - "9.877E+08||\n" + - "9.8765E+08||\n"); + DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(); + String valueSeparator = testRunner.getProcessContext().getProperty(CSVUtils.VALUE_SEPARATOR).evaluateAttributeExpressions(ff).getValue(); + String decimalSeparator = (String.valueOf(decimalFormatSymbols.getDecimalSeparator()).equals(valueSeparator)) + ? ("\\" + decimalFormatSymbols.getDecimalSeparator()) : String.valueOf(decimalFormatSymbols.getDecimalSeparator()); + String groupingSeparator = String.valueOf(decimalFormatSymbols.getGroupingSeparator()).equals(valueSeparator) + ? "\\" + decimalFormatSymbols.getGroupingSeparator() : String.valueOf(decimalFormatSymbols.getGroupingSeparator()); + ff.assertContentEquals(String.format("Numbers|Timestamps|Money\n" + + "1234%1$s456|" + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + "|$ 123%1$s45\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 @@ -332,15 +366,31 @@ public class ConvertExcelToCSVProcessorTest { assertTrue(rowsSheet == 9); 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" + - "'1234.456','" + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + "','$ 123.45'\n" + - "'1234.46','" + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + "','£ 123.45'\n" + - "'1234.5','" + DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy").format(localDt) + "','¥ 123.45'\n" + - "'1,234.46','" + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + "','$ 1,023.45'\n" + - "'1,234.4560','" + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + "','£ 1,023.45'\n" + - "'9.88E+08','" + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + "','¥ 1,023.45'\n" + - "'9.877E+08',,\n" + - "'9.8765E+08',,\n"); + addQuotingIfNeeded(String.format("1234%1$s456", decimalSeparator), ",", quoteCharValue, true) + "," + quoteCharValue + + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + quoteCharValue + "," + + addQuotingIfNeeded(String.format("$ 123%1$s45", decimalSeparator), ",", quoteCharValue, true) + "\n" + + addQuotingIfNeeded(String.format("1234%1$s46", decimalSeparator), ",", quoteCharValue, true) + "," + quoteCharValue + + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + quoteCharValue + "," + + addQuotingIfNeeded(String.format("£ 123%1$s45", decimalSeparator), ",", quoteCharValue, true) + "\n" + + addQuotingIfNeeded(String.format("1234%1$s5", decimalSeparator), ",", quoteCharValue, true) + "," + quoteCharValue + + 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 @@ -363,15 +413,21 @@ public class ConvertExcelToCSVProcessorTest { assertTrue(rowsSheet == 9); LocalDateTime localDt = LocalDateTime.of(2017, 1, 1, 12, 0, 0); - ff.assertContentEquals("Numbers,Timestamps,Money\n" + - "1234.456," + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + ",$ 123.45\n" + - "1234.46," + DateTimeFormatter.ofPattern("hh:mm:ss a").format(localDt) + ",£ 123.45\n" + - "1234.5," + DateTimeFormatter.ofPattern("EEEE^, MMMM dd^, yyyy").format(localDt) + ",¥ 123.45\n" + - "1^,234.46," + DateTimeFormatter.ofPattern("d/M/yy HH:mm").format(localDt) + ",$ 1^,023.45\n" + - "1^,234.4560," + DateTimeFormatter.ofPattern("hh:mm a").format(localDt) + ",£ 1^,023.45\n" + - "9.88E+08," + DateTimeFormatter.ofPattern("yyyy/MM/dd/ HH:mm").format(localDt) + ",¥ 1^,023.45\n" + - "9.877E+08,,\n" + - "9.8765E+08,,\n"); + DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(); + String escapeCharValue = testRunner.getProcessContext().getProperty(CSVUtils.ESCAPE_CHAR).evaluateAttributeExpressions(ff).getValue(); + String decimalSeparator = String.valueOf(decimalFormatSymbols.getDecimalSeparator()).equals(",") + ? escapeCharValue + decimalFormatSymbols.getDecimalSeparator() : String.valueOf(decimalFormatSymbols.getDecimalSeparator()); + String groupingSeparator = String.valueOf(decimalFormatSymbols.getGroupingSeparator()).equals(",") + ? escapeCharValue + decimalFormatSymbols.getGroupingSeparator() : String.valueOf(decimalFormatSymbols.getGroupingSeparator()); + ff.assertContentEquals(String.format("Numbers,Timestamps,Money\n" + + "1234%1$s456," + DateTimeFormatter.ofPattern("d/M/yy").format(localDt) + ",$ 123%1$s45\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(); 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; + } } \ No newline at end of file