]> source.dussan.org Git - poi.git/commitdiff
Always close Reader in test
authorDominik Stadler <centic@apache.org>
Mon, 15 Jun 2020 20:51:43 +0000 (20:51 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 15 Jun 2020 20:51:43 +0000 (20:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1878869 13f79535-47bb-0310-9956-ffa450edef68

src/testcases/org/apache/poi/ss/usermodel/TestFractionFormat.java

index 95e2947007615e7c1d0e01f4e403601f6ac939d8..ec8359a46d67cc09f9a46461ec14f44125bf0efa 100644 (file)
@@ -65,52 +65,52 @@ public final class TestFractionFormat {
     @Test
     public void testWithBigWholePart() {
         FractionFormat f = new FractionFormat("#", "???/???");
-        
+
         assertEquals("10100136259702", f.format(10100136259702d));
         assertEquals("-10100136259702", f.format(-10100136259702d));
-        
+
         // Excel displays fraction: 51/512
         assertEquals("10100136259702 10/100", f.format(10100136259702.1d));
         assertEquals("-10100136259702 10/100", f.format(-10100136259702.1d));
-        
+
         // Excel displays fraction: 461/512
         assertEquals("10100136259702 90/100", f.format(10100136259702.9d));
         assertEquals("-10100136259702 90/100", f.format(-10100136259702.9d));
     }
-     
+
     @Test
     public void testTruthFile() throws Exception {
         File truthFile = HSSFTestDataSamples.getSampleFile("54686_fraction_formats.txt");
-        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(truthFile), LocaleUtil.CHARSET_1252));
-        Workbook wb = HSSFTestDataSamples.openSampleWorkbook("54686_fraction_formats.xls");
-        Sheet sheet = wb.getSheetAt(0);
-        DataFormatter formatter = new DataFormatter();
-        FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
-
-        // Skip over the header row
-        String truthLine = reader.readLine();
-        String[] headers = truthLine.split("\t");
-        truthLine = reader.readLine();
-
-        for (int i = 1; i < sheet.getLastRowNum() && truthLine != null; i++){
-            Row r = sheet.getRow(i);
-            String[] truths = truthLine.split("\t");
-            // Intentionally ignore the last column (tika-1132), for now
-            for (short j = 3; j < 12; j++){
-                Cell cell = r.getCell(j, MissingCellPolicy.CREATE_NULL_AS_BLANK);
-                String formatted = clean(formatter.formatCellValue(cell, evaluator));
-                if (truths.length <= j){
-                    continue;
-                }
+        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(truthFile), LocaleUtil.CHARSET_1252))) {
+            Workbook wb = HSSFTestDataSamples.openSampleWorkbook("54686_fraction_formats.xls");
+            Sheet sheet = wb.getSheetAt(0);
+            DataFormatter formatter = new DataFormatter();
+            FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
 
-                String truth = clean(truths[j]);
-                String testKey = truths[0]+":"+truths[1]+":"+headers[j];
-                assertEquals(testKey, truth, formatted);
-            }
+            // Skip over the header row
+            String truthLine = reader.readLine();
+            String[] headers = truthLine.split("\t");
             truthLine = reader.readLine();
+
+            for (int i = 1; i < sheet.getLastRowNum() && truthLine != null; i++) {
+                Row r = sheet.getRow(i);
+                String[] truths = truthLine.split("\t");
+                // Intentionally ignore the last column (tika-1132), for now
+                for (short j = 3; j < 12; j++) {
+                    Cell cell = r.getCell(j, MissingCellPolicy.CREATE_NULL_AS_BLANK);
+                    String formatted = clean(formatter.formatCellValue(cell, evaluator));
+                    if (truths.length <= j) {
+                        continue;
+                    }
+
+                    String truth = clean(truths[j]);
+                    String testKey = truths[0] + ":" + truths[1] + ":" + headers[j];
+                    assertEquals(testKey, truth, formatted);
+                }
+                truthLine = reader.readLine();
+            }
+            wb.close();
         }
-        wb.close();
-        reader.close();
     }
 
     private String clean(String s){