From da8d4c9bada8d2a7cc35fb3ec83d0a0460d30411 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sat, 23 Feb 2019 07:51:57 +0000 Subject: [PATCH] Add some more testing for bug 63151: NullPointerException in HSSFOptimiser.optimiseFonts(), also execute it as part of large regression tests in the future git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1854190 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/stress/HSSFFileHandler.java | 7 +++-- .../poi/hssf/record/common/UnicodeString.java | 2 +- .../hssf/record/common/TestUnicodeString.java | 27 ++++++++++--------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/integrationtest/org/apache/poi/stress/HSSFFileHandler.java b/src/integrationtest/org/apache/poi/stress/HSSFFileHandler.java index 119175b82e..603231022e 100644 --- a/src/integrationtest/org/apache/poi/stress/HSSFFileHandler.java +++ b/src/integrationtest/org/apache/poi/stress/HSSFFileHandler.java @@ -64,6 +64,8 @@ public class HSSFFileHandler extends SpreadsheetHandler { } } } + + HSSFOptimiser.optimiseFonts(wb); } private static final Set EXPECTED_ADDITIONAL_FAILURES = new HashSet<>(); @@ -85,6 +87,7 @@ public class HSSFFileHandler extends SpreadsheetHandler { public void handleAdditional(File file) throws Exception { // redirect stdout as the examples often write lots of text PrintStream oldOut = System.out; + String fileWithParent = file.getParentFile().getName() + "/" + file.getName(); try { System.setOut(new PrintStream(new OutputStream() { @Override @@ -95,11 +98,11 @@ public class HSSFFileHandler extends SpreadsheetHandler { BiffViewer.main(new String[]{file.getAbsolutePath()}); assertFalse("Expected Extraction to fail for file " + file + " and handler " + this + ", but did not fail!", - EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())); + EXPECTED_ADDITIONAL_FAILURES.contains(fileWithParent)); } catch (OldExcelFormatException e) { // old excel formats are not supported here } catch (RuntimeException e) { - if(!EXPECTED_ADDITIONAL_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) { + if(!EXPECTED_ADDITIONAL_FAILURES.contains(fileWithParent)) { throw e; } } finally { diff --git a/src/java/org/apache/poi/hssf/record/common/UnicodeString.java b/src/java/org/apache/poi/hssf/record/common/UnicodeString.java index ef4016e598..80689564b9 100644 --- a/src/java/org/apache/poi/hssf/record/common/UnicodeString.java +++ b/src/java/org/apache/poi/hssf/record/common/UnicodeString.java @@ -712,7 +712,7 @@ public class UnicodeString implements Comparable { if (field_4_format_runs != null) { for (int i = 0; i < field_4_format_runs.size();i++) { FormatRun r = field_4_format_runs.get(i); - buffer.append(" .format_run"+i+" = ").append(r).append("\n"); + buffer.append(" .format_run").append(i).append(" = ").append(r).append("\n"); } } if (field_5_ext_rst != null) { diff --git a/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java b/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java index e9373049d4..1a823e3aaa 100644 --- a/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java +++ b/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java @@ -59,9 +59,10 @@ public final class TestUnicodeString { private static void confirmSize(int expectedSize, UnicodeString s) { confirmSize(expectedSize, s, 0); } + /** * Note - a value of zero for amountUsedInCurrentRecord would only ever occur just - * after a {@link ContinueRecord} had been started. In the initial {@link SSTRecord} this + * after a {@link ContinueRecord} had been started. In the initial {@link SSTRecord} this * value starts at 8 (for the first {@link UnicodeString} written). In general, it can be * any value between 0 and {@link #MAX_DATA_SIZE} */ @@ -181,7 +182,7 @@ public final class TestUnicodeString { } @Test - public void formatRun() throws Exception { + public void formatRun() { FormatRun fr = new FormatRun((short)4, (short)0x15c); assertEquals(4, fr.getCharacterPos()); assertEquals(0x15c, fr.getFontIndex()); @@ -207,7 +208,7 @@ public final class TestUnicodeString { } @Test - public void extRstFromEmpty() throws Exception { + public void extRstFromEmpty() { ExtRst ext = new ExtRst(); assertEquals(0, ext.getNumberOfRuns()); @@ -231,14 +232,14 @@ public final class TestUnicodeString { assertEquals(-1, b[0]); assertEquals(-1, b[1]); assertEquals(14, b[2]); - assertEquals(00, b[3]); + assertEquals(0, b[3]); // Reserved assertEquals(1, b[4]); assertEquals(0, b[5]); // Data size assertEquals(10, b[6]); - assertEquals(00, b[7]); + assertEquals(0, b[7]); // Font*2 assertEquals(0, b[8]); assertEquals(0, b[9]); @@ -274,13 +275,13 @@ public final class TestUnicodeString { } @Test - public void extRstFromData() throws Exception { + public void extRstFromData() { byte[] data = new byte[] { - 01, 00, 0x0C, 00, - 00, 00, 0x37, 00, - 00, 00, - 00, 00, 00, 00, - 00, 00 // Cruft at the end, as found from real files + 1, 0, 0x0C, 0, + 0, 0, 0x37, 0, + 0, 0, + 0, 0, 0, 0, + 0, 0 // Cruft at the end, as found from real files }; assertEquals(16, data.length); @@ -298,7 +299,7 @@ public final class TestUnicodeString { } @Test - public void corruptExtRstDetection() throws Exception { + public void corruptExtRstDetection() { byte[] data = new byte[] { 0x79, 0x79, 0x11, 0x11, 0x22, 0x22, 0x33, 0x33, @@ -385,7 +386,7 @@ public final class TestUnicodeString { * @return a string of the specified number of characters */ private static UnicodeString makeUnicodeString(int numChars, boolean is16Bit) { - StringBuilder b = new StringBuilder(numChars); + StringBuilder b = new StringBuilder(numChars); int charBase = is16Bit ? 0x8A00 : 'A'; for (int i = 0; i < numChars; i++) { char ch = (char) ((i % 16) + charBase); -- 2.39.5