From 80dc4b34c9223f85093eb816c803e479f08722f5 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 27 Mar 2019 21:24:25 +0000 Subject: [PATCH] add test for concurrent use of DataFormatter git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1856439 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/usermodel/TestDataFormatter.java | 50 +++++++++++++++++-- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java b/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java index a4aa5f4ea7..8195a92c09 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java +++ b/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java @@ -28,11 +28,9 @@ import static org.junit.Assert.fail; import java.io.IOException; import java.text.DateFormat; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Date; -import java.util.List; -import java.util.Locale; +import java.util.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.usermodel.HSSFWorkbook; @@ -938,4 +936,46 @@ public class TestDataFormatter { String value = df.formatCellValue(cell, wb.getCreationHelper().createFormulaEvaluator()); assertEquals("-130", value); } + + @Test + public void testConcurrentCellFormat() throws Exception { + int formatIndex = 105; + String formatString = "[$-F400]m/d/yy h:mm:ss\\ AM/PM;[$-F400]m/d/yy h:mm:ss\\ AM/PM;_-* \"\"??_-;_-@_-"; + + DataFormatter formatter = new DataFormatter(); + doFormatTestSequential(formatter, 43551.50990171296, "3/27/19 12:14:15 PM", formatIndex, formatString); + doFormatTestSequential(formatter, 36104.424780092595, "11/5/98 10:11:41 AM", formatIndex, formatString); + + doFormatTestConcurrent(formatter, 43551.50990171296, "3/27/19 12:14:15 PM", formatIndex, formatString); + doFormatTestConcurrent(formatter, 36104.424780092595, "11/5/98 10:11:41 AM", formatIndex, formatString); + } + + private void doFormatTestSequential(DataFormatter formatter, double n, String expected, int formatIndex, + String formatString) { + for (int i = 0; i < 1_000; i++) { + assertTrue(doFormatTest(formatter, n, expected, formatIndex, formatString, i)); + } + } + + private void doFormatTestConcurrent(DataFormatter formatter, double n, String expected, int formatIndex, + String formatString) throws Exception { + ArrayList> futures = new ArrayList<>(); + for (int i = 0; i < 1_000; i++) { + final int iteration = i; + CompletableFuture future = CompletableFuture.supplyAsync( + () -> { return doFormatTest(formatter, n, expected, formatIndex, formatString, iteration); }); + futures.add(future); + } + for (CompletableFuture future : futures) { + assertTrue(future.get(1, TimeUnit.MINUTES)); + } + } + + private static boolean doFormatTest(DataFormatter formatter, double n, String expected, int formatIndex, + String formatString, int iteration) { + String actual = formatter.formatRawCellContents(n, formatIndex, formatString); + assertEquals("Failed on iteration " + iteration, expected, actual); + return true; + } + } \ No newline at end of file -- 2.39.5