diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2015-09-07 20:19:50 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2015-09-07 20:19:50 +0000 |
commit | a3d2eb57ff4ac10ae215b9b7404260103eb6f538 (patch) | |
tree | 7b44ec32a7cdd0e0828779c793061ac6a00e4c00 /src/testcases/org/apache/poi/hssf/extractor | |
parent | f2e85496125f7fc68f7434244b9d9d5bb026933d (diff) | |
download | poi-a3d2eb57ff4ac10ae215b9b7404260103eb6f538.tar.gz poi-a3d2eb57ff4ac10ae215b9b7404260103eb6f538.zip |
Bug 58326 - Forbidden APIs patches - first set of changes for locale and timezone settings
also includes fixes for
- name shadowing
- unused deprecated method "getClipRect" in classes extending Graphics2d
- HexDump - replaced intermediate String.format calls with custom padding
- convert testcases to junit4
- closing resources
also tested with an arbitary timezone (PST) and locale (ru)
supresses forbidden apis check for
- LocaleUtil (the only place where Locale.getDefault() and TimeZone.getDefault() should be called)
- Classes using FontMetrics - without the actual text it's difficult to return something sane
Some usage of UTC and Locale.ROOT might be still wrong, e.g. in MapiMessage we don't access the
extended mapi properties, which might contain the timezone
DataFormatter has now a Observable property which need to be observed when custom formats are used
and the Locale changes
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1701688 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org/apache/poi/hssf/extractor')
-rw-r--r-- | src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java | 334 |
1 files changed, 186 insertions, 148 deletions
diff --git a/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java b/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java index 793215a028..15f4a3cb0b 100644 --- a/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java +++ b/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java @@ -17,10 +17,13 @@ package org.apache.poi.hssf.extractor; -import java.io.IOException; -import java.io.InputStream; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; -import junit.framework.TestCase; +import java.io.File; +import java.io.IOException; +import java.util.Locale; import org.apache.poi.POIDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples; @@ -28,24 +31,22 @@ import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.util.LocaleUtil; +import org.junit.Test; /** * */ -public final class TestExcelExtractor extends TestCase { - - private static ExcelExtractor createExtractor(String sampleFileName) { - - InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName); +public final class TestExcelExtractor { - try { - return new ExcelExtractor(new POIFSFileSystem(is)); - } catch (IOException e) { - throw new RuntimeException(e); - } + @SuppressWarnings("resource") + private static ExcelExtractor createExtractor(String sampleFileName) throws IOException { + File file = HSSFTestDataSamples.getSampleFile(sampleFileName); + return new ExcelExtractor(new POIFSFileSystem(file)); } + @Test public void testSimple() throws IOException { ExcelExtractor extractor = createExtractor("Simple.xls"); @@ -60,7 +61,8 @@ public final class TestExcelExtractor extends TestCase { } } - public void testNumericFormula() { + @Test + public void testNumericFormula() throws IOException { ExcelExtractor extractor = createExtractor("sumifformula.xls"); @@ -87,9 +89,12 @@ public final class TestExcelExtractor extends TestCase { "Sheet2\nSheet3\n", extractor.getText() ); + + extractor.close(); } - public void testwithContinueRecords() { + @Test + public void testwithContinueRecords() throws IOException { ExcelExtractor extractor = createExtractor("StringContinueRecords.xls"); @@ -99,9 +104,12 @@ public final class TestExcelExtractor extends TestCase { // Until we fixed bug #41064, this would've // failed by now assertTrue(extractor.getText().length() > 40960); + + extractor.close(); } - public void testStringConcat() { + @Test + public void testStringConcat() throws IOException { ExcelExtractor extractor = createExtractor("SimpleWithFormula.xls"); @@ -112,9 +120,12 @@ public final class TestExcelExtractor extends TestCase { extractor.setFormulasNotResults(true); assertEquals("Sheet1\nreplaceme\nreplaceme\nCONCATENATE(A1,A2)\nSheet2\nSheet3\n", extractor.getText()); + + extractor.close(); } - public void testStringFormula() { + @Test + public void testStringFormula() throws IOException { ExcelExtractor extractor = createExtractor("StringFormulas.xls"); @@ -125,40 +136,49 @@ public final class TestExcelExtractor extends TestCase { extractor.setFormulasNotResults(true); assertEquals("Sheet1\nUPPER(\"xyz\")\nSheet2\nSheet3\n", extractor.getText()); + + extractor.close(); } - public void testEventExtractor() throws Exception { + @Test + public void testEventExtractor() throws Exception { // First up, a simple file with string // based formulas in it - EventBasedExcelExtractor extractor = new EventBasedExcelExtractor( - new POIFSFileSystem( - HSSFTestDataSamples.openSampleFileStream("SimpleWithFormula.xls") - ) - ); + EventBasedExcelExtractor extractor1 = null; try { - extractor.setIncludeSheetNames(true); - - String text = extractor.getText(); + extractor1 = new EventBasedExcelExtractor( + new POIFSFileSystem( + HSSFTestDataSamples.openSampleFileStream("SimpleWithFormula.xls") + ) + ); + extractor1.setIncludeSheetNames(true); + + String text = extractor1.getText(); assertEquals("Sheet1\nreplaceme\nreplaceme\nreplacemereplaceme\nSheet2\nSheet3\n", text); - extractor.setIncludeSheetNames(false); - extractor.setFormulasNotResults(true); + extractor1.setIncludeSheetNames(false); + extractor1.setFormulasNotResults(true); - text = extractor.getText(); + text = extractor1.getText(); assertEquals("replaceme\nreplaceme\nCONCATENATE(A1,A2)\n", text); + } finally { + if (extractor1 != null) extractor1.close(); + } + + // Now, a slightly longer file with numeric formulas + EventBasedExcelExtractor extractor2 = null; + try { + extractor2 = new EventBasedExcelExtractor( + new POIFSFileSystem( + HSSFTestDataSamples.openSampleFileStream("sumifformula.xls") + ) + ); + + extractor2.setIncludeSheetNames(false); + extractor2.setFormulasNotResults(true); - - // Now, a slightly longer file with numeric formulas - extractor = new EventBasedExcelExtractor( - new POIFSFileSystem( - HSSFTestDataSamples.openSampleFileStream("sumifformula.xls") - ) - ); - extractor.setIncludeSheetNames(false); - extractor.setFormulasNotResults(true); - - text = extractor.getText(); + String text = extractor2.getText(); assertEquals( "1000\t1\tSUMIF(A1:A5,\">4000\",B1:B5)\n" + "2000\t2\n" + @@ -168,11 +188,12 @@ public final class TestExcelExtractor extends TestCase { text ); } finally { - extractor.close(); + if (extractor2 != null) extractor2.close(); } } - public void testWithComments() { + @Test + public void testWithComments() throws IOException { ExcelExtractor extractor = createExtractor("SimpleWithComments.xls"); extractor.setIncludeSheetNames(false); @@ -192,9 +213,12 @@ public final class TestExcelExtractor extends TestCase { "3\tthree Comment by Yegor Kozlov: Yegor Kozlov: third cell\n", extractor.getText() ); + + extractor.close(); } - public void testWithBlank() { + @Test + public void testWithBlank() throws IOException { ExcelExtractor extractor = createExtractor("MissingBits.xls"); String def = extractor.getText(); extractor.setIncludeBlankCells(true); @@ -213,136 +237,145 @@ public final class TestExcelExtractor extends TestCase { "Hello\n" + "11\t\t\t23\n" )); + + extractor.close(); } - public void testFormatting() throws Exception { - ExcelExtractor extractor = createExtractor("Formatting.xls"); - extractor.setIncludeBlankCells(false); - extractor.setIncludeSheetNames(false); - String text = extractor.getText(); - - // Note - not all the formats in the file - // actually quite match what they claim to - // be, as some are auto-local builtins... - - assertTrue(text.startsWith( - "Dates, all 24th November 2006\n" - )); - assertTrue( - text.indexOf( - "yyyy/mm/dd\t2006/11/24\n" - ) > -1 - ); - assertTrue( - text.indexOf( - "yyyy-mm-dd\t2006-11-24\n" - ) > -1 - ); - assertTrue( - text.indexOf( - "dd-mm-yy\t24-11-06\n" - ) > -1 - ); - - assertTrue("Had: " + text + ", but should contain 'nn.nn\\t10.52\\n'", - text.indexOf( - "nn.nn\t10.52\n" - ) > -1 - ); - assertTrue( - text.indexOf( - "nn.nnn\t10.520\n" - ) > -1 - ); - assertTrue( - text.indexOf( - "\u00a3nn.nn\t\u00a310.52\n" - ) > -1 - ); + @Test + public void testFormatting() throws Exception { + Locale userLocale = LocaleUtil.getUserLocale(); + LocaleUtil.setUserLocale(Locale.ROOT); + try { + ExcelExtractor extractor = createExtractor("Formatting.xls"); + extractor.setIncludeBlankCells(false); + extractor.setIncludeSheetNames(false); + String text = extractor.getText(); + + // Note - not all the formats in the file + // actually quite match what they claim to + // be, as some are auto-local builtins... + + assertTrue(text.startsWith( + "Dates, all 24th November 2006\n" + )); + assertTrue( + text.indexOf( + "yyyy/mm/dd\t2006/11/24\n" + ) > -1 + ); + assertTrue( + text.indexOf( + "yyyy-mm-dd\t2006-11-24\n" + ) > -1 + ); + assertTrue( + text.indexOf( + "dd-mm-yy\t24-11-06\n" + ) > -1 + ); + + assertTrue("Had: " + text + ", but should contain 'nn.nn\\t10.52\\n'", + text.indexOf( + "nn.nn\t10.52\n" + ) > -1 + ); + assertTrue( + text.indexOf( + "nn.nnn\t10.520\n" + ) > -1 + ); + assertTrue( + text.indexOf( + "\u00a3nn.nn\t\u00a310.52\n" + ) > -1 + ); + extractor.close(); + } finally { + LocaleUtil.setUserLocale(userLocale); + } } /** * Embeded in a non-excel file */ - public void testWithEmbeded() throws Exception { - POIFSFileSystem fs = new POIFSFileSystem( - POIDataSamples.getDocumentInstance().openResourceAsStream("word_with_embeded.doc") - ); - - DirectoryNode objPool = (DirectoryNode) fs.getRoot().getEntry("ObjectPool"); - DirectoryNode dirA = (DirectoryNode) objPool.getEntry("_1269427460"); - DirectoryNode dirB = (DirectoryNode) objPool.getEntry("_1269427461"); + @Test + public void testWithEmbeded() throws Exception { + POIFSFileSystem fs = null; + + HSSFWorkbook wbA = null, wbB = null; + ExcelExtractor exA = null, exB = null; - HSSFWorkbook wbA = new HSSFWorkbook(dirA, fs, true); - HSSFWorkbook wbB = new HSSFWorkbook(dirB, fs, true); - - ExcelExtractor exA = new ExcelExtractor(wbA); try { - ExcelExtractor exB = new ExcelExtractor(wbB); - try { - assertEquals("Sheet1\nTest excel file\nThis is the first file\nSheet2\nSheet3\n", - exA.getText()); - assertEquals("Sample Excel", exA.getSummaryInformation().getTitle()); - - assertEquals("Sheet1\nAnother excel file\nThis is the second file\nSheet2\nSheet3\n", - exB.getText()); - assertEquals("Sample Excel 2", exB.getSummaryInformation().getTitle()); - } finally { - exB.close(); - } + fs = new POIFSFileSystem(POIDataSamples.getDocumentInstance().getFile("word_with_embeded.doc")); + + DirectoryNode objPool = (DirectoryNode) fs.getRoot().getEntry("ObjectPool"); + DirectoryNode dirA = (DirectoryNode) objPool.getEntry("_1269427460"); + DirectoryNode dirB = (DirectoryNode) objPool.getEntry("_1269427461"); + + wbA = new HSSFWorkbook(dirA, fs, true); + exA = new ExcelExtractor(wbA); + wbB = new HSSFWorkbook(dirB, fs, true); + exB = new ExcelExtractor(wbB); + + assertEquals("Sheet1\nTest excel file\nThis is the first file\nSheet2\nSheet3\n", exA.getText()); + assertEquals("Sample Excel", exA.getSummaryInformation().getTitle()); + assertEquals("Sheet1\nAnother excel file\nThis is the second file\nSheet2\nSheet3\n", exB.getText()); + assertEquals("Sample Excel 2", exB.getSummaryInformation().getTitle()); } finally { - exA.close(); + if (exB != null) exB.close(); + if (wbB != null) wbB.close(); + if (exA != null) exA.close(); + if (wbA != null) wbA.close(); + if (fs != null) fs.close(); } } /** * Excel embeded in excel */ - public void testWithEmbededInOwn() throws Exception { + @Test + public void testWithEmbededInOwn() throws Exception { POIDataSamples ssSamples = POIDataSamples.getSpreadSheetInstance(); - POIFSFileSystem fs = new POIFSFileSystem( - ssSamples.openResourceAsStream("excel_with_embeded.xls") - ); + POIFSFileSystem fs = null; + HSSFWorkbook wbA = null, wbB = null; + ExcelExtractor exA = null, exB = null, ex = null; - DirectoryNode dirA = (DirectoryNode) fs.getRoot().getEntry("MBD0000A3B5"); - DirectoryNode dirB = (DirectoryNode) fs.getRoot().getEntry("MBD0000A3B4"); + try { + fs = new POIFSFileSystem(ssSamples.getFile("excel_with_embeded.xls")); - HSSFWorkbook wbA = new HSSFWorkbook(dirA, fs, true); - HSSFWorkbook wbB = new HSSFWorkbook(dirB, fs, true); + DirectoryNode dirA = (DirectoryNode) fs.getRoot().getEntry("MBD0000A3B5"); + DirectoryNode dirB = (DirectoryNode) fs.getRoot().getEntry("MBD0000A3B4"); - ExcelExtractor exA = new ExcelExtractor(wbA); - try { - ExcelExtractor exB = new ExcelExtractor(wbB); - try { - assertEquals("Sheet1\nTest excel file\nThis is the first file\nSheet2\nSheet3\n", - exA.getText()); - assertEquals("Sample Excel", exA.getSummaryInformation().getTitle()); - - assertEquals("Sheet1\nAnother excel file\nThis is the second file\nSheet2\nSheet3\n", - exB.getText()); - assertEquals("Sample Excel 2", exB.getSummaryInformation().getTitle()); + wbA = new HSSFWorkbook(dirA, fs, true); + wbB = new HSSFWorkbook(dirB, fs, true); + + exA = new ExcelExtractor(wbA); + exB = new ExcelExtractor(wbB); + assertEquals("Sheet1\nTest excel file\nThis is the first file\nSheet2\nSheet3\n", exA.getText()); + assertEquals("Sample Excel", exA.getSummaryInformation().getTitle()); + + assertEquals("Sheet1\nAnother excel file\nThis is the second file\nSheet2\nSheet3\n", exB.getText()); + assertEquals("Sample Excel 2", exB.getSummaryInformation().getTitle()); - // And the base file too - ExcelExtractor ex = new ExcelExtractor(fs); - try { - assertEquals("Sheet1\nI have lots of embeded files in me\nSheet2\nSheet3\n", - ex.getText()); - assertEquals("Excel With Embeded", ex.getSummaryInformation().getTitle()); - } finally { - ex.close(); - } - } finally { - exB.close(); - } - } finally { - exA.close(); + // And the base file too + ex = new ExcelExtractor(fs); + assertEquals("Sheet1\nI have lots of embeded files in me\nSheet2\nSheet3\n", ex.getText()); + assertEquals("Excel With Embeded", ex.getSummaryInformation().getTitle()); + } finally { + if (ex != null) ex.close(); + if (exB != null) exB.close(); + if (exA != null) exA.close(); + if (wbB != null) wbB.close(); + if (wbA != null) wbA.close(); + if (fs != null) fs.close(); } } /** * Test that we get text from headers and footers */ - public void test45538() { + @Test + public void test45538() throws IOException { String[] files = { "45538_classic_Footer.xls", "45538_form_Footer.xls", "45538_classic_Header.xls", "45538_form_Header.xls" @@ -352,21 +385,26 @@ public final class TestExcelExtractor extends TestCase { String text = extractor.getText(); assertTrue("Unable to find expected word in text\n" + text, text.indexOf("testdoc") >=0); assertTrue("Unable to find expected word in text\n" + text, text.indexOf("test phrase") >= 0); + extractor.close(); } } - public void testPassword() { + @Test + public void testPassword() throws IOException { Biff8EncryptionKey.setCurrentUserPassword("password"); ExcelExtractor extractor = createExtractor("password.xls"); String text = extractor.getText(); Biff8EncryptionKey.setCurrentUserPassword(null); assertTrue(text.contains("ZIP")); + extractor.close(); } - public void testNullPointerException() { + @Test + public void testNullPointerException() throws IOException { ExcelExtractor extractor = createExtractor("ar.org.apsme.www_Form%20Inscripcion%20Curso%20NO%20Socios.xls"); assertNotNull(extractor); assertNotNull(extractor.getText()); + extractor.close(); } } |