You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ce...@apache.org on 2017/09/26 12:40:39 UTC

svn commit: r1809739 - in /poi/trunk/src: scratchpad/testcases/org/apache/poi/hwpf/usermodel/ testcases/org/apache/poi/hssf/usermodel/ testcases/org/apache/poi/ss/usermodel/

Author: centic
Date: Tue Sep 26 12:40:38 2017
New Revision: 1809739

URL: http://svn.apache.org/viewvc?rev=1809739&view=rev
Log:
Adjust some unit-tests, use try-with-resource, enable test which works now, ...

Modified:
    poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java
    poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
    poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java
    poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java

Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java?rev=1809739&r1=1809738&r2=1809739&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java (original)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestBugs.java Tue Sep 26 12:40:38 2017
@@ -188,17 +188,14 @@ public class TestBugs{
         WordExtractor extractor1 = new WordExtractor(doc1);
         try {
             HWPFDocument doc2 = HWPFTestDataSamples.writeOutAndReadBack(doc1);
-            
-            WordExtractor extractor2 = new WordExtractor(doc2);
-            try {
+
+            try (WordExtractor extractor2 = new WordExtractor(doc2)) {
                 assertEqualsIgnoreNewline(extractor1.getFooterText(), extractor2.getFooterText());
                 assertEqualsIgnoreNewline(extractor1.getHeaderText(), extractor2.getHeaderText());
-                assertEqualsIgnoreNewline(Arrays.toString(extractor1.getParagraphText() ),
+                assertEqualsIgnoreNewline(Arrays.toString(extractor1.getParagraphText()),
                         Arrays.toString(extractor2.getParagraphText()));
-        
+
                 assertEqualsIgnoreNewline(extractor1.getText(), extractor2.getText());
-            } finally {
-                extractor2.close();
             }
         } finally {
             extractor1.close();
@@ -451,16 +448,13 @@ public class TestBugs{
 
         // (2) read text from text document (retrieved by saving the word
         // document as text file using encoding UTF-8)
-        InputStream is = POIDataSamples.getDocumentInstance()
-                .openResourceAsStream("Bug47742-text.txt");
-        try {
+        try (InputStream is = POIDataSamples.getDocumentInstance()
+                .openResourceAsStream("Bug47742-text.txt")) {
             byte[] expectedBytes = IOUtils.toByteArray(is);
-            String expectedText = new String(expectedBytes, "utf-8" )
+            String expectedText = new String(expectedBytes, "utf-8")
                     .substring(1); // strip-off the unicode marker
-    
+
             assertEqualsIgnoreNewline(expectedText, foundText);
-        } finally {
-            is.close();
         }
     }
 
@@ -636,14 +630,11 @@ public class TestBugs{
     {
         InputStream is = POIDataSamples.getDocumentInstance()
                 .openResourceAsStream("empty.doc");
-        NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(is);
-        try {
+        try (NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(is)) {
             HWPFDocument hwpfDocument = new HWPFDocument(
                     npoifsFileSystem.getRoot());
             hwpfDocument.write(new ByteArrayOutputStream());
             hwpfDocument.close();
-        } finally {
-            npoifsFileSystem.close();
         }
     }
 
@@ -663,11 +654,8 @@ public class TestBugs{
             HWPFDocument hwpfDocument = HWPFTestDataSamples
                     .openRemoteFile(href);
 
-            WordExtractor wordExtractor = new WordExtractor(hwpfDocument);
-            try {
+            try (WordExtractor wordExtractor = new WordExtractor(hwpfDocument)) {
                 wordExtractor.getText();
-            } finally {
-                wordExtractor.close();
             }
         }
     }
@@ -903,13 +891,10 @@ public class TestBugs{
     @Test(expected=ArrayIndexOutOfBoundsException.class)
     public void test57843() throws IOException {
         File f = POIDataSamples.getDocumentInstance().getFile("57843.doc");
-        POIFSFileSystem fs = new POIFSFileSystem(f, true);
-        try {
+        try (POIFSFileSystem fs = new POIFSFileSystem(f, true)) {
             HWPFOldDocument doc = new HWPFOldDocument(fs);
             assertNotNull(doc);
             doc.close();
-        } finally {
-            fs.close();
         }
     }
 

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java?rev=1809739&r1=1809738&r2=1809739&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java Tue Sep 26 12:40:38 2017
@@ -62,7 +62,6 @@ import org.apache.poi.hssf.record.TabIdR
 import org.apache.poi.hssf.record.UnknownRecord;
 import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
 import org.apache.poi.hssf.record.aggregates.PageSettingsBlock;
-import org.apache.poi.hssf.record.aggregates.RecordAggregate;
 import org.apache.poi.hssf.record.common.UnicodeString;
 import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
 import org.apache.poi.poifs.filesystem.DocumentEntry;
@@ -1637,13 +1636,10 @@ public final class TestBugs extends Base
             ));
         }
         try {
-            NPOIFSFileSystem fs = new NPOIFSFileSystem(
-                    HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"));
-            try {
+            try (NPOIFSFileSystem fs = new NPOIFSFileSystem(
+                    HSSFITestDataProvider.instance.openWorkbookStream("46904.xls"))) {
                 new HSSFWorkbook(fs.getRoot(), false).close();
                 fail("Should catch exception here");
-            } finally {
-                fs.close();
             }
         } catch (OldExcelFormatException e) {
             assertTrue(e.getMessage().startsWith(
@@ -2497,12 +2493,7 @@ public final class TestBugs extends Base
         HSSFSheet sh = wb.getSheetAt(0);
         InternalSheet ish = HSSFTestHelper.getSheetForTest(sh);
         PageSettingsBlock psb = (PageSettingsBlock) ish.getRecords().get(13);
-        psb.visitContainedRecords(new RecordAggregate.RecordVisitor() {
-            @Override
-            public void visitRecord(Record r) {
-                list.add(r.getSid());
-            }
-        });
+        psb.visitContainedRecords(r -> list.add(r.getSid()));
         assertEquals(UnknownRecord.BITMAP_00E9, list.get(list.size() - 1).intValue());
         assertEquals(UnknownRecord.HEADER_FOOTER_089C, list.get(list.size() - 2).intValue());
         wb.close();
@@ -2664,12 +2655,9 @@ public final class TestBugs extends Base
         POIFSFileSystem fs;
 
         File file = HSSFTestDataSamples.getSampleFile("56325.xls");
-        InputStream stream = new FileInputStream(file);
-        try {
+        try (InputStream stream = new FileInputStream(file)) {
             fs = new POIFSFileSystem(stream);
             wb1 = new HSSFWorkbook(fs);
-        } finally {
-            stream.close();
         }
 
         assertEquals(3, wb1.getNumberOfSheets());
@@ -2836,22 +2824,18 @@ public final class TestBugs extends Base
      * Read, write, read for formulas point to cells in other files.
      * See {@link #bug46670()} for the main test, this just
      * covers reading an existing file and checking it.
-     * TODO Fix this so that it works - formulas are ending up as
-     * #REF when being changed
+     *
+     * See base-test-class for some related tests that still fail
      */
     @Test
-    @Ignore
     public void bug46670_existing() throws Exception {
-        Sheet s;
-        Cell c;
-
         // Expected values
-        String refLocal = "'[refs/airport.xls]Sheet1'!$A$2";
+        String refLocal = "'[refs" + File.separator + "airport.xls]Sheet1'!$A$2";
         String refHttp = "'[9http://www.principlesofeconometrics.com/excel/airline.xls]Sheet1'!$A$2";
 
         // Check we can read them correctly
-        HSSFWorkbook wb1 = openSample("46670_local.xls");
-        s = wb1.getSheetAt(0);
+        Workbook wb1 = openSample("46670_local.xls");
+        Sheet s = wb1.getSheetAt(0);
         assertEquals(refLocal, s.getRow(0).getCell(0).getCellFormula());
         wb1.close();
 
@@ -2864,7 +2848,7 @@ public final class TestBugs extends Base
         //  they end up as they did before, even with a save and re-load
         HSSFWorkbook wb3 = openSample("46670_local.xls");
         s = wb3.getSheetAt(0);
-        c = s.getRow(0).getCell(0);
+        Cell c = s.getRow(0).getCell(0);
         c.setCellFormula(refLocal);
         assertEquals(refLocal, c.getCellFormula());
 
@@ -2880,7 +2864,7 @@ public final class TestBugs extends Base
         c.setCellFormula(refHttp);
         assertEquals(refHttp, c.getCellFormula());
 
-        HSSFWorkbook wb6 = HSSFTestDataSamples.writeOutAndReadBack(wb5);
+        Workbook wb6 = HSSFTestDataSamples.writeOutAndReadBack(wb5);
         wb5.close();
         s = wb6.getSheetAt(0);
         assertEquals(refHttp, s.getRow(0).getCell(0).getCellFormula());
@@ -3149,9 +3133,8 @@ public final class TestBugs extends Base
 
         DocumentEntry entry =
                 (DocumentEntry) npoifs.getRoot().getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
-        PropertySet properties =
-                new PropertySet(new DocumentInputStream(entry));
 
+        // this will throw an Exception "RuntimeException: Can't read negative number of bytes"
+        new PropertySet(new DocumentInputStream(entry));
     }
-
 }

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java?rev=1809739&r1=1809738&r2=1809739&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java Tue Sep 26 12:40:38 2017
@@ -137,36 +137,33 @@ public final class TestHSSFComment exten
 
     @Test
     public void testBug56380InsertTooManyComments() throws Exception {
-        HSSFWorkbook workbook = new HSSFWorkbook();
-        try {
+        try (HSSFWorkbook workbook = new HSSFWorkbook()) {
             Sheet sheet = workbook.createSheet();
             Drawing<?> drawing = sheet.createDrawingPatriarch();
             String comment = "c";
-    
-            for(int rowNum = 0;rowNum < 258;rowNum++) {
-            	sheet.createRow(rowNum);
+
+            for (int rowNum = 0; rowNum < 258; rowNum++) {
+                sheet.createRow(rowNum);
             }
-            
+
             // should still work, for some reason DrawingManager2.allocateShapeId() skips the first 1024...
-            for(int count = 1025;count < 65535;count++) {
-            	int rowNum = count / 255;
-            	int cellNum = count % 255;
+            for (int count = 1025; count < 65535; count++) {
+                int rowNum = count / 255;
+                int cellNum = count % 255;
                 Cell cell = sheet.getRow(rowNum).createCell(cellNum);
                 try {
-                	Comment commentObj = insertComment(drawing, cell, comment + cellNum);
-                	
-                	assertEquals(count, ((HSSFComment)commentObj).getNoteRecord().getShapeId());
+                    Comment commentObj = insertComment(drawing, cell, comment + cellNum);
+
+                    assertEquals(count, ((HSSFComment) commentObj).getNoteRecord().getShapeId());
                 } catch (IllegalArgumentException e) {
-                	throw new IllegalArgumentException("While adding shape number " + count, e);
+                    throw new IllegalArgumentException("While adding shape number " + count, e);
                 }
-            }        	
-            
+            }
+
             // this should now fail to insert
             Row row = sheet.createRow(257);
             Cell cell = row.createCell(0);
             insertComment(drawing, cell, comment + 0);
-        } finally {
-        	workbook.close();
         }
     }
 

Modified: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java?rev=1809739&r1=1809738&r2=1809739&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java Tue Sep 26 12:40:38 2017
@@ -1517,8 +1517,7 @@ public abstract class BaseTestBugzillaIs
     
     @Ignore("bug 59393")
     @Test
-    public void bug59393_commentsCanHaveSameAnchor() throws IOException
-    {
+    public void bug59393_commentsCanHaveSameAnchor() throws IOException {
         Workbook wb = _testDataProvider.createWorkbook();
         
         Sheet sheet = wb.createSheet();
@@ -1597,7 +1596,7 @@ public abstract class BaseTestBugzillaIs
 
         workbook.close();
     }
-    
+
     @Ignore
     @Test
     public void test57929() throws IOException {
@@ -1631,11 +1630,10 @@ public abstract class BaseTestBugzillaIs
 
     @Test
     public void test55384() throws Exception {
-        Workbook wb = _testDataProvider.createWorkbook();
-        try {
+        try (Workbook wb = _testDataProvider.createWorkbook()) {
             Sheet sh = wb.createSheet();
             for (int rownum = 0; rownum < 10; rownum++) {
-                org.apache.poi.ss.usermodel.Row row = sh.createRow(rownum);
+                Row row = sh.createRow(rownum);
                 for (int cellnum = 0; cellnum < 3; cellnum++) {
                     Cell cell = row.createCell(cellnum);
                     cell.setCellValue(rownum + cellnum);
@@ -1676,8 +1674,6 @@ public abstract class BaseTestBugzillaIs
             Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb);
             checkFormulaPreevaluatedString(wbBack);
             wbBack.close();
-        } finally {
-            wb.close();
         }
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org