]> source.dussan.org Git - poi.git/commitdiff
Bug 57373: Fix get/setFirstVisibleTab() for XSSFWorkbook
authorDominik Stadler <centic@apache.org>
Mon, 22 Dec 2014 14:37:37 +0000 (14:37 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 22 Dec 2014 14:37:37 +0000 (14:37 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1647322 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java

index 88581d9ddb78c4051cc706ecfe22bf28f6e2bd08..f3efe946638192a5dd212388dd09fa8ccd0095a3 100644 (file)
@@ -1192,7 +1192,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
     public int getFirstVisibleTab() {
         CTBookViews bookViews = workbook.getBookViews();
         CTBookView bookView = bookViews.getWorkbookViewArray(0);
-        return (short) bookView.getActiveTab();
+        return (short) bookView.getFirstSheet();
     }
 
     /**
@@ -1204,7 +1204,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
     public void setFirstVisibleTab(int index) {
         CTBookViews bookViews = workbook.getBookViews();
         CTBookView bookView= bookViews.getWorkbookViewArray(0);
-        bookView.setActiveTab(index);
+        bookView.setFirstSheet(index);
     }
 
     /**
index fd41ac672140407a746fa4c188768e9a5a862fe9..2f615a57cc98357c1da72432fc50987044dbf2fc 100644 (file)
@@ -166,47 +166,59 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
        }
 
     @Test
-       public void getCellStyleAt(){
+       public void getCellStyleAt() throws IOException{
                XSSFWorkbook workbook = new XSSFWorkbook();
-               short i = 0;
-               //get default style
-               CellStyle cellStyleAt = workbook.getCellStyleAt(i);
-               assertNotNull(cellStyleAt);
-
-               //get custom style
-               StylesTable styleSource = workbook.getStylesSource();
-               XSSFCellStyle customStyle = new XSSFCellStyle(styleSource);
-               XSSFFont font = new XSSFFont();
-               font.setFontName("Verdana");
-               customStyle.setFont(font);
-               int x = styleSource.putStyle(customStyle);
-               cellStyleAt = workbook.getCellStyleAt((short)x);
-               assertNotNull(cellStyleAt);
+               try {
+               short i = 0;
+               //get default style
+               CellStyle cellStyleAt = workbook.getCellStyleAt(i);
+               assertNotNull(cellStyleAt);
+    
+               //get custom style
+               StylesTable styleSource = workbook.getStylesSource();
+               XSSFCellStyle customStyle = new XSSFCellStyle(styleSource);
+               XSSFFont font = new XSSFFont();
+               font.setFontName("Verdana");
+               customStyle.setFont(font);
+               int x = styleSource.putStyle(customStyle);
+               cellStyleAt = workbook.getCellStyleAt((short)x);
+               assertNotNull(cellStyleAt);
+               } finally {
+                   workbook.close();
+               }
        }
 
     @Test
-       public void getFontAt(){
+       public void getFontAt() throws IOException{
                XSSFWorkbook workbook = new XSSFWorkbook();
-               StylesTable styleSource = workbook.getStylesSource();
-               short i = 0;
-               //get default font
-               Font fontAt = workbook.getFontAt(i);
-               assertNotNull(fontAt);
-
-               //get customized font
-               XSSFFont customFont = new XSSFFont();
-               customFont.setItalic(true);
-               int x = styleSource.putFont(customFont);
-               fontAt = workbook.getFontAt((short)x);
-               assertNotNull(fontAt);
+               try {
+               StylesTable styleSource = workbook.getStylesSource();
+               short i = 0;
+               //get default font
+               Font fontAt = workbook.getFontAt(i);
+               assertNotNull(fontAt);
+    
+               //get customized font
+               XSSFFont customFont = new XSSFFont();
+               customFont.setItalic(true);
+               int x = styleSource.putFont(customFont);
+               fontAt = workbook.getFontAt((short)x);
+               assertNotNull(fontAt);
+               } finally {
+                   workbook.close();
+               }
        }
 
     @Test
-       public void getNumCellStyles(){
+       public void getNumCellStyles() throws IOException{
                XSSFWorkbook workbook = new XSSFWorkbook();
-               short i = workbook.getNumCellStyles();
-               //get default cellStyles
-               assertEquals(1, i);
+               try {
+               short i = workbook.getNumCellStyles();
+               //get default cellStyles
+               assertEquals(1, i);
+               } finally {
+                   workbook.close();
+               }
        }
 
     @Test
@@ -456,30 +468,33 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
        }
 
     @Test
-    public void recalcId() {
+    public void recalcId() throws IOException {
         XSSFWorkbook wb = new XSSFWorkbook();
-        assertFalse(wb.getForceFormulaRecalculation());
-        CTWorkbook ctWorkbook = wb.getCTWorkbook();
-        assertFalse(ctWorkbook.isSetCalcPr());
-
-        wb.setForceFormulaRecalculation(true); // resets the EngineId flag to zero
-
-        CTCalcPr calcPr = ctWorkbook.getCalcPr();
-        assertNotNull(calcPr);
-        assertEquals(0, (int) calcPr.getCalcId());
-
-        calcPr.setCalcId(100);
-        assertTrue(wb.getForceFormulaRecalculation());
-
-        wb.setForceFormulaRecalculation(true); // resets the EngineId flag to zero
-        assertEquals(0, (int) calcPr.getCalcId());
-        assertFalse(wb.getForceFormulaRecalculation());
-
-        // calcMode="manual" is unset when forceFormulaRecalculation=true
-        calcPr.setCalcMode(STCalcMode.MANUAL);
-        wb.setForceFormulaRecalculation(true);
-        assertEquals(STCalcMode.AUTO, calcPr.getCalcMode());
-
+        try {
+            assertFalse(wb.getForceFormulaRecalculation());
+            CTWorkbook ctWorkbook = wb.getCTWorkbook();
+            assertFalse(ctWorkbook.isSetCalcPr());
+    
+            wb.setForceFormulaRecalculation(true); // resets the EngineId flag to zero
+    
+            CTCalcPr calcPr = ctWorkbook.getCalcPr();
+            assertNotNull(calcPr);
+            assertEquals(0, (int) calcPr.getCalcId());
+    
+            calcPr.setCalcId(100);
+            assertTrue(wb.getForceFormulaRecalculation());
+    
+            wb.setForceFormulaRecalculation(true); // resets the EngineId flag to zero
+            assertEquals(0, (int) calcPr.getCalcId());
+            assertFalse(wb.getForceFormulaRecalculation());
+    
+            // calcMode="manual" is unset when forceFormulaRecalculation=true
+            calcPr.setCalcMode(STCalcMode.MANUAL);
+            wb.setForceFormulaRecalculation(true);
+            assertEquals(STCalcMode.AUTO, calcPr.getCalcMode());
+        } finally {
+            wb.close();
+        }
     }
 
     @Test
@@ -488,14 +503,18 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
     }
 
     @Test
-    public void setTabColor() {
+    public void setTabColor() throws IOException {
         XSSFWorkbook wb = new XSSFWorkbook();
-        XSSFSheet sh = wb.createSheet();
-        assertTrue(sh.getCTWorksheet().getSheetPr() == null || !sh.getCTWorksheet().getSheetPr().isSetTabColor());
-        sh.setTabColor(IndexedColors.RED.index);
-        assertTrue(sh.getCTWorksheet().getSheetPr().isSetTabColor());
-        assertEquals(IndexedColors.RED.index,
-                sh.getCTWorksheet().getSheetPr().getTabColor().getIndexed());
+        try {
+            XSSFSheet sh = wb.createSheet();
+            assertTrue(sh.getCTWorksheet().getSheetPr() == null || !sh.getCTWorksheet().getSheetPr().isSetTabColor());
+            sh.setTabColor(IndexedColors.RED.index);
+            assertTrue(sh.getCTWorksheet().getSheetPr().isSetTabColor());
+            assertEquals(IndexedColors.RED.index,
+                    sh.getCTWorksheet().getSheetPr().getTabColor().getIndexed());
+        } finally {
+            wb.close();
+        }
     }
 
     @Test
@@ -655,24 +674,28 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
        public void bug51158a() throws IOException {
         // create a workbook
         final XSSFWorkbook workbook = new XSSFWorkbook();
-        workbook.createSheet("Test Sheet");
-
-        XSSFSheet sheetBack = workbook.getSheetAt(0);
-
-        // committing twice did add the XML twice without clearing the part in between
-        sheetBack.commit();
-
-        // ensure that a memory based package part does not have lingering data from previous commit() calls
-        if(sheetBack.getPackagePart() instanceof MemoryPackagePart) {
-            ((MemoryPackagePart)sheetBack.getPackagePart()).clear();
+        try {
+            workbook.createSheet("Test Sheet");
+    
+            XSSFSheet sheetBack = workbook.getSheetAt(0);
+    
+            // committing twice did add the XML twice without clearing the part in between
+            sheetBack.commit();
+    
+            // ensure that a memory based package part does not have lingering data from previous commit() calls
+            if(sheetBack.getPackagePart() instanceof MemoryPackagePart) {
+                ((MemoryPackagePart)sheetBack.getPackagePart()).clear();
+            }
+    
+            sheetBack.commit();
+    
+            String str = new String(IOUtils.toByteArray(sheetBack.getPackagePart().getInputStream()));
+            System.out.println(str);
+            
+            assertEquals(1, countMatches(str, "<worksheet"));
+        } finally {
+            workbook.close();
         }
-
-        sheetBack.commit();
-
-        String str = new String(IOUtils.toByteArray(sheetBack.getPackagePart().getInputStream()));
-        System.out.println(str);
-        
-        assertEquals(1, countMatches(str, "<worksheet"));
     }  
        
     private static final int INDEX_NOT_FOUND = -1;
@@ -698,18 +721,23 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
         return cs.toString().indexOf(searchChar.toString(), start);
     }
 
-    public void testAddPivotCache() {
+    @Test
+    public void testAddPivotCache() throws IOException {
         XSSFWorkbook wb = new XSSFWorkbook();
-        CTWorkbook ctWb = wb.getCTWorkbook();
-        CTPivotCache pivotCache = wb.addPivotCache("0");
-        //Ensures that pivotCaches is initiated
-        assertTrue(ctWb.isSetPivotCaches());
-        assertSame(pivotCache, ctWb.getPivotCaches().getPivotCacheArray(0));
-        assertEquals("0", pivotCache.getId());
+        try {
+            CTWorkbook ctWb = wb.getCTWorkbook();
+            CTPivotCache pivotCache = wb.addPivotCache("0");
+            //Ensures that pivotCaches is initiated
+            assertTrue(ctWb.isSetPivotCaches());
+            assertSame(pivotCache, ctWb.getPivotCaches().getPivotCacheArray(0));
+            assertEquals("0", pivotCache.getId());
+        } finally {
+            wb.close();
+        }
     }
 
     public void setPivotData(XSSFWorkbook wb){
-        XSSFSheet sheet = (XSSFSheet) wb.createSheet();
+        XSSFSheet sheet = wb.createSheet();
 
         Row row1 = sheet.createRow(0);
         // Create a cell and put a value in it.
@@ -740,6 +768,7 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
         sheet.createPivotTable(source, new CellReference("H5"));
     }
 
+    @Test
     public void testLoadWorkbookWithPivotTable() throws Exception {
         String fileName = "ooxml-pivottable.xlsx";
 
@@ -754,6 +783,7 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
         assertTrue(wb2.getPivotTables().size() == 1);
     }
 
+    @Test
     public void testAddPivotTableToWorkbookWithLoadedPivotTable() throws Exception {
         String fileName = "ooxml-pivottable.xlsx";
 
@@ -768,4 +798,39 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
         setPivotData(wb2);
         assertTrue(wb2.getPivotTables().size() == 2);
     }
+    
+    @Test
+    public void testSetFirstVisibleTab_57373() throws IOException {
+        Workbook wb = new XSSFWorkbook();
+        
+        try {
+            /*Sheet sheet1 =*/ wb.createSheet();
+            Sheet sheet2 = wb.createSheet();
+            int idx2 = wb.getSheetIndex(sheet2);
+            Sheet sheet3 = wb.createSheet();
+            int idx3 = wb.getSheetIndex(sheet3);
+            
+            // add many sheets so "first visible" is relevant
+            for(int i = 0; i < 30;i++) {
+                wb.createSheet();
+            }
+            
+            wb.setFirstVisibleTab(idx2);
+            wb.setActiveSheet(idx3);
+            
+            //wb.write(new FileOutputStream(new File("C:\\temp\\test.xlsx")));
+
+            assertEquals(idx2, wb.getFirstVisibleTab());
+            assertEquals(idx3, wb.getActiveSheetIndex());
+
+            Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
+
+            sheet2 = wbBack.getSheetAt(idx2);
+            sheet3 = wbBack.getSheetAt(idx3);
+            assertEquals(idx2, wb.getFirstVisibleTab());
+            assertEquals(idx3, wb.getActiveSheetIndex());
+        } finally {
+            wb.close();
+        }
+    }
 }