poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ye...@apache.org
Subject svn commit: r759112 [3/3] - in /poi/trunk/src: java/org/apache/poi/hssf/record/aggregates/ java/org/apache/poi/hssf/usermodel/ ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/ ooxml/java/org/apache/poi/xssf/usermodel/ ooxml/testcases/org/apache/poi/...
Date Fri, 27 Mar 2009 11:50:53 GMT
Added: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java?rev=759112&view=auto
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java (added)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java Fri Mar 27 11:50:52
2009
@@ -0,0 +1,565 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.usermodel;
+
+import junit.framework.TestCase;
+import org.apache.poi.ss.ITestDataProvider;
+import org.apache.poi.ss.util.CellRangeAddress;
+
+import java.util.Iterator;
+
+/**
+ * Common superclass for testing {@link org.apache.poi.xssf.usermodel.XSSFCell}  and
+ * {@link org.apache.poi.hssf.usermodel.HSSFCell}
+ */
+public abstract class BaseTestSheet extends TestCase {
+
+    /**
+     * @return an object that provides test data in HSSF / XSSF specific way
+     */
+    protected abstract ITestDataProvider getTestDataProvider();
+
+    public void testCreateRow() {
+        Workbook workbook = getTestDataProvider().createWorkbook();
+        Sheet sheet = workbook.createSheet();
+        assertEquals(0, sheet.getPhysicalNumberOfRows());
+
+        //Test that we get null for undefined rownumber
+        assertNull(sheet.getRow(1));
+
+        // Test row creation with consecutive indexes
+        Row row1 = sheet.createRow(0);
+        Row row2 = sheet.createRow(1);
+        assertEquals(0, row1.getRowNum());
+        assertEquals(1, row2.getRowNum());
+        Iterator<Row> it = sheet.rowIterator();
+        assertTrue(it.hasNext());
+        assertSame(row1, it.next());
+        assertTrue(it.hasNext());
+        assertSame(row2, it.next());
+        assertEquals(1, sheet.getLastRowNum());
+
+        // Test row creation with non consecutive index
+        Row row101 = sheet.createRow(100);
+        assertNotNull(row101);
+        assertEquals(100, sheet.getLastRowNum());
+        assertEquals(3, sheet.getPhysicalNumberOfRows());
+
+        // Test overwriting an existing row
+        Row row2_ovrewritten = sheet.createRow(1);
+        Cell cell = row2_ovrewritten.createCell(0);
+        cell.setCellValue(100);
+        Iterator<Row> it2 = sheet.rowIterator();
+        assertTrue(it2.hasNext());
+        assertSame(row1, it2.next());
+        assertTrue(it2.hasNext());
+        Row row2_ovrewritten_ref = it2.next();
+        assertSame(row2_ovrewritten, row2_ovrewritten_ref);
+        assertEquals(100.0, row2_ovrewritten_ref.getCell(0).getNumericCellValue());
+    }
+
+
+    public void testRemoveRow() {
+        Workbook workbook = getTestDataProvider().createWorkbook();
+        Sheet sheet1 = workbook.createSheet();
+        assertEquals(0, sheet1.getPhysicalNumberOfRows());
+
+        Row row0 = sheet1.createRow(0);
+        assertEquals(1, sheet1.getPhysicalNumberOfRows());
+        sheet1.removeRow(row0);
+        assertEquals(0, sheet1.getPhysicalNumberOfRows());
+
+        Row row1 = sheet1.createRow(1);
+        Row row2 = sheet1.createRow(2);
+        assertEquals(2, sheet1.getPhysicalNumberOfRows());
+
+        assertNotNull(sheet1.getRow(1));
+        assertNotNull(sheet1.getRow(2));
+        sheet1.removeRow(row2);
+        assertNotNull(sheet1.getRow(1));
+        assertNull(sheet1.getRow(2));
+
+        Row row3 = sheet1.createRow(3);
+        Sheet sheet2 = workbook.createSheet();
+        try {
+            sheet2.removeRow(row3);
+            fail("Expected exception");
+        } catch (IllegalArgumentException e){
+            assertEquals("Specified row does not belong to this sheet", e.getMessage());
+        }
+    }
+
+    public void testCloneSheet() {
+        Workbook workbook = getTestDataProvider().createWorkbook();
+        CreationHelper factory = workbook.getCreationHelper();
+        Sheet sheet = workbook.createSheet("Test Clone");
+        Row row = sheet.createRow(0);
+        Cell cell = row.createCell(0);
+        Cell cell2 = row.createCell(1);
+        cell.setCellValue(factory.createRichTextString("clone_test"));
+        cell2.setCellFormula("SIN(1)");
+
+        Sheet clonedSheet = workbook.cloneSheet(0);
+        Row clonedRow = clonedSheet.getRow(0);
+
+        //Check for a good clone
+        assertEquals(clonedRow.getCell(0).getRichStringCellValue().getString(), "clone_test");
+
+        //Check that the cells are not somehow linked
+        cell.setCellValue(factory.createRichTextString("Difference Check"));
+        cell2.setCellFormula("cos(2)");
+        if ("Difference Check".equals(clonedRow.getCell(0).getRichStringCellValue().getString()))
{
+            fail("string cell not properly cloned");
+        }
+        if ("COS(2)".equals(clonedRow.getCell(1).getCellFormula())) {
+            fail("formula cell not properly cloned");
+        }
+        assertEquals(clonedRow.getCell(0).getRichStringCellValue().getString(), "clone_test");
+        assertEquals(clonedRow.getCell(1).getCellFormula(), "SIN(1)");
+    }
+
+    /** tests that the sheet name for multiple clones of the same sheet is unique
+     * BUG 37416
+     */
+    public void testCloneSheetMultipleTimes() {
+        Workbook workbook = getTestDataProvider().createWorkbook();
+        CreationHelper factory = workbook.getCreationHelper();
+        Sheet sheet = workbook.createSheet("Test Clone");
+        Row row = sheet.createRow(0);
+        Cell cell = row.createCell(0);
+        cell.setCellValue(factory.createRichTextString("clone_test"));
+        //Clone the sheet multiple times
+        workbook.cloneSheet(0);
+        workbook.cloneSheet(0);
+
+        assertNotNull(workbook.getSheet("Test Clone"));
+        assertNotNull(workbook.getSheet("Test Clone (2)"));
+        assertEquals("Test Clone (3)", workbook.getSheetName(2));
+        assertNotNull(workbook.getSheet("Test Clone (3)"));
+
+        workbook.removeSheetAt(0);
+        workbook.removeSheetAt(0);
+        workbook.removeSheetAt(0);
+        workbook.createSheet("abc ( 123)");
+        workbook.cloneSheet(0);
+        assertEquals("abc (124)", workbook.getSheetName(1));
+    }
+
+    /**
+     * Setting landscape and portrait stuff on new sheets
+     */
+    public void testPrintSetupLandscapeNew() throws Exception {
+        Workbook workbook = getTestDataProvider().createWorkbook();
+        Sheet sheetL = workbook.createSheet("LandscapeS");
+        Sheet sheetP = workbook.createSheet("LandscapeP");
+
+        // Check two aspects of the print setup
+        assertFalse(sheetL.getPrintSetup().getLandscape());
+        assertFalse(sheetP.getPrintSetup().getLandscape());
+        assertEquals(1, sheetL.getPrintSetup().getCopies());
+        assertEquals(1, sheetP.getPrintSetup().getCopies());
+
+        // Change one on each
+        sheetL.getPrintSetup().setLandscape(true);
+        sheetP.getPrintSetup().setCopies((short)3);
+
+        // Check taken
+        assertTrue(sheetL.getPrintSetup().getLandscape());
+        assertFalse(sheetP.getPrintSetup().getLandscape());
+        assertEquals(1, sheetL.getPrintSetup().getCopies());
+        assertEquals(3, sheetP.getPrintSetup().getCopies());
+
+        // Save and re-load, and check still there
+        workbook = getTestDataProvider().writeOutAndReadBack(workbook);
+        sheetL = workbook.getSheet("LandscapeS");
+        sheetP = workbook.getSheet("LandscapeP");
+
+        assertTrue(sheetL.getPrintSetup().getLandscape());
+        assertFalse(sheetP.getPrintSetup().getLandscape());
+        assertEquals(1, sheetL.getPrintSetup().getCopies());
+        assertEquals(3, sheetP.getPrintSetup().getCopies());
+    }
+
+    /**
+     * When removing one merged region, it would break
+     *
+     */
+    public void testRemoveMerged() {
+        Workbook wb = getTestDataProvider().createWorkbook();
+        Sheet sheet = wb.createSheet();
+        CellRangeAddress region = new CellRangeAddress(0, 1, 0, 1);
+        sheet.addMergedRegion(region);
+        region = new CellRangeAddress(1, 2, 0, 1);
+        sheet.addMergedRegion(region);
+
+        sheet.removeMergedRegion(0);
+
+        region = sheet.getMergedRegion(0);
+        assertEquals("Left over region should be starting at row 1", 1, region.getFirstRow());
+
+        sheet.removeMergedRegion(0);
+
+        assertEquals("there should be no merged regions left!", 0, sheet.getNumMergedRegions());
+
+        //an, add, remove, get(0) would null pointer
+        sheet.addMergedRegion(region);
+        assertEquals("there should now be one merged region!", 1, sheet.getNumMergedRegions());
+        sheet.removeMergedRegion(0);
+        assertEquals("there should now be zero merged regions!", 0, sheet.getNumMergedRegions());
+        //add it again!
+        region.setLastRow(4);
+
+        sheet.addMergedRegion(region);
+        assertEquals("there should now be one merged region!", 1, sheet.getNumMergedRegions());
+
+        //should exist now!
+        assertTrue("there isn't more than one merged region in there", 1 <= sheet.getNumMergedRegions());
+        region = sheet.getMergedRegion(0);
+        assertEquals("the merged row to doesnt match the one we put in ", 4, region.getLastRow());
+    }
+
+    public void testShiftMerged() {
+        Workbook wb = getTestDataProvider().createWorkbook();
+        CreationHelper factory = wb.getCreationHelper();
+        Sheet sheet = wb.createSheet();
+        Row row = sheet.createRow(0);
+        Cell cell = row.createCell(0);
+        cell.setCellValue(factory.createRichTextString("first row, first cell"));
+
+        row = sheet.createRow(1);
+        cell = row.createCell(1);
+        cell.setCellValue(factory.createRichTextString("second row, second cell"));
+
+        CellRangeAddress region = new CellRangeAddress(1, 1, 0, 1);
+        sheet.addMergedRegion(region);
+
+        sheet.shiftRows(1, 1, 1);
+
+        region = sheet.getMergedRegion(0);
+        assertEquals("Merged region not moved over to row 2", 2, region.getFirstRow());
+    }
+
+    /**
+     * Tests the display of gridlines, formulas, and rowcolheadings.
+     * @author Shawn Laubach (slaubach at apache dot org)
+     */
+    public void testDisplayOptions() {
+        Workbook wb = getTestDataProvider().createWorkbook();
+        Sheet sheet = wb.createSheet();
+
+        assertEquals(sheet.isDisplayGridlines(), true);
+        assertEquals(sheet.isDisplayRowColHeadings(), true);
+        assertEquals(sheet.isDisplayFormulas(), false);
+        assertEquals(sheet.isDisplayZeros(), true);
+
+        sheet.setDisplayGridlines(false);
+        sheet.setDisplayRowColHeadings(false);
+        sheet.setDisplayFormulas(true);
+        sheet.setDisplayZeros(false);
+
+        wb = getTestDataProvider().writeOutAndReadBack(wb);
+        sheet = wb.getSheetAt(0);
+
+        assertEquals(sheet.isDisplayGridlines(), false);
+        assertEquals(sheet.isDisplayRowColHeadings(), false);
+        assertEquals(sheet.isDisplayFormulas(), true);
+        assertEquals(sheet.isDisplayZeros(), false);
+    }
+
+    public void testColumnWidth() {
+        Workbook wb = getTestDataProvider().createWorkbook();
+        Sheet sheet = wb.createSheet();
+
+        //default column width measured in characters
+        sheet.setDefaultColumnWidth(10);
+        assertEquals(10, sheet.getDefaultColumnWidth());
+        //columns A-C have default width
+        assertEquals(256*10, sheet.getColumnWidth(0));
+        assertEquals(256*10, sheet.getColumnWidth(1));
+        assertEquals(256*10, sheet.getColumnWidth(2));
+
+        //set custom width for D-F
+        for (char i = 'D'; i <= 'F'; i++) {
+            //Sheet#setColumnWidth accepts the width in units of 1/256th of a character width
+            int w = 256*12;
+            sheet.setColumnWidth(i, w);
+            assertEquals(w, sheet.getColumnWidth(i));
+        }
+        //reset the default column width, columns A-C change, D-F still have custom width
+        sheet.setDefaultColumnWidth(20);
+        assertEquals(20, sheet.getDefaultColumnWidth());
+        assertEquals(256*20, sheet.getColumnWidth(0));
+        assertEquals(256*20, sheet.getColumnWidth(1));
+        assertEquals(256*20, sheet.getColumnWidth(2));
+        for (char i = 'D'; i <= 'F'; i++) {
+            int w = 256*12;
+            assertEquals(w, sheet.getColumnWidth(i));
+        }
+
+        // check for 16-bit signed/unsigned error:
+        sheet.setColumnWidth(10, 40000);
+        assertEquals(40000, sheet.getColumnWidth(10));
+
+        //The maximum column width for an individual cell is 255 characters
+        try {
+            sheet.setColumnWidth(9, 256*256);
+            fail("expected exception");
+        } catch(IllegalArgumentException e){
+            assertEquals("The maximum column width for an individual cell is 255 characters.",
e.getMessage());
+        }
+
+        //serialize and read again
+        wb = getTestDataProvider().writeOutAndReadBack(wb);
+
+        sheet = wb.getSheetAt(0);
+        assertEquals(20, sheet.getDefaultColumnWidth());
+        //columns A-C have default width
+        assertEquals(256*20, sheet.getColumnWidth(0));
+        assertEquals(256*20, sheet.getColumnWidth(1));
+        assertEquals(256*20, sheet.getColumnWidth(2));
+        //columns D-F have custom width
+        for (char i = 'D'; i <= 'F'; i++) {
+            short w = (256*12);
+            assertEquals(w, sheet.getColumnWidth(i));
+        }
+        assertEquals(40000, sheet.getColumnWidth(10));
+    }
+
+    public void testDefaultRowHeight() {
+        Workbook workbook = getTestDataProvider().createWorkbook();
+        Sheet sheet = workbook.createSheet();
+        sheet.setDefaultRowHeightInPoints(15);
+        assertEquals((short) 300, sheet.getDefaultRowHeight());
+        assertEquals((float) 15, sheet.getDefaultRowHeightInPoints());
+
+        // Set a new default row height in twips and test getting the value in points
+        sheet.setDefaultRowHeight((short) 360);
+        assertEquals(18.0f, sheet.getDefaultRowHeightInPoints());
+        assertEquals((short) 360, sheet.getDefaultRowHeight());
+
+        // Test that defaultRowHeight is a truncated short: E.G. 360inPoints -> 18; 361inPoints
-> 18
+        sheet.setDefaultRowHeight((short) 361);
+        assertEquals((float)361/20, sheet.getDefaultRowHeightInPoints());
+        assertEquals((short) 361, sheet.getDefaultRowHeight());
+
+        // Set a new default row height in points and test getting the value in twips
+        sheet.setDefaultRowHeightInPoints(17.5f);
+        assertEquals(17.5f, sheet.getDefaultRowHeightInPoints());
+        assertEquals((short)(17.5f*20), sheet.getDefaultRowHeight());
+    }
+
+    /** cell with formula becomes null on cloning a sheet*/
+     public void test35084() {
+        Workbook wb = getTestDataProvider().createWorkbook();
+        Sheet s = wb.createSheet("Sheet1");
+        Row r = s.createRow(0);
+        r.createCell(0).setCellValue(1);
+        r.createCell(1).setCellFormula("A1*2");
+        Sheet s1 = wb.cloneSheet(0);
+        r = s1.getRow(0);
+        assertEquals("double", r.getCell(0).getNumericCellValue(), 1, 0); // sanity check
+        assertNotNull(r.getCell(1));
+        assertEquals("formula", r.getCell(1).getCellFormula(), "A1*2");
+    }
+
+    /** test that new default column styles get applied */
+    public void testDefaultColumnStyle() {
+        Workbook wb = getTestDataProvider().createWorkbook();
+        CellStyle style = wb.createCellStyle();
+        Sheet sheet = wb.createSheet();
+        sheet.setDefaultColumnStyle(0, style);
+        assertNotNull(sheet.getColumnStyle(0));
+        assertEquals(style.getIndex(), sheet.getColumnStyle(0).getIndex());
+
+        Row row = sheet.createRow(0);
+        Cell cell = row.createCell(0);
+        CellStyle style2 = cell.getCellStyle();
+        assertNotNull(style2);
+        assertEquals("style should match", style.getIndex(), style2.getIndex());
+    }
+
+    public void testOutlineProperties() {
+        Workbook wb = getTestDataProvider().createWorkbook();
+
+        Sheet sheet = wb.createSheet();
+
+        //TODO defaults are different in HSSF and XSSF
+        //assertTrue(sheet.getRowSumsBelow());
+        //assertTrue(sheet.getRowSumsRight());
+
+        sheet.setRowSumsBelow(false);
+        sheet.setRowSumsRight(false);
+
+        assertFalse(sheet.getRowSumsBelow());
+        assertFalse(sheet.getRowSumsRight());
+
+        sheet.setRowSumsBelow(true);
+        sheet.setRowSumsRight(true);
+
+        assertTrue(sheet.getRowSumsBelow());
+        assertTrue(sheet.getRowSumsRight());
+
+        wb = getTestDataProvider().writeOutAndReadBack(wb);
+        sheet = wb.getSheetAt(0);
+        assertTrue(sheet.getRowSumsBelow());
+        assertTrue(sheet.getRowSumsRight());
+    }
+
+    /**
+     * Test basic display properties
+     */
+    public void testSheetProperties() {
+        Workbook wb = getTestDataProvider().createWorkbook();
+        Sheet sheet = wb.createSheet();
+
+        assertFalse(sheet.getHorizontallyCenter());
+        sheet.setHorizontallyCenter(true);
+        assertTrue(sheet.getHorizontallyCenter());
+        sheet.setHorizontallyCenter(false);
+        assertFalse(sheet.getHorizontallyCenter());
+
+        assertFalse(sheet.getVerticallyCenter());
+        sheet.setVerticallyCenter(true);
+        assertTrue(sheet.getVerticallyCenter());
+        sheet.setVerticallyCenter(false);
+        assertFalse(sheet.getVerticallyCenter());
+
+        assertFalse(sheet.isPrintGridlines());
+        sheet.setPrintGridlines(true);
+        assertTrue(sheet.isPrintGridlines());
+
+        assertFalse(sheet.isDisplayFormulas());
+        sheet.setDisplayFormulas(true);
+        assertTrue(sheet.isDisplayFormulas());
+
+        assertTrue(sheet.isDisplayGridlines());
+        sheet.setDisplayGridlines(false);
+        assertFalse(sheet.isDisplayGridlines());
+
+        //TODO: default "guts" is different in HSSF and XSSF
+        //assertTrue(sheet.getDisplayGuts());
+        sheet.setDisplayGuts(false);
+        assertFalse(sheet.getDisplayGuts());
+
+        assertTrue(sheet.isDisplayRowColHeadings());
+        sheet.setDisplayRowColHeadings(false);
+        assertFalse(sheet.isDisplayRowColHeadings());
+
+        //TODO: default "autobreaks" is different in HSSF and XSSF
+        //assertTrue(sheet.getAutobreaks());
+        sheet.setAutobreaks(false);
+        assertFalse(sheet.getAutobreaks());
+
+        assertFalse(sheet.getScenarioProtect());
+
+        //TODO: default "fit-to-page" is different in HSSF and XSSF
+        //assertFalse(sheet.getFitToPage());
+        sheet.setFitToPage(true);
+        assertTrue(sheet.getFitToPage());
+        sheet.setFitToPage(false);
+        assertFalse(sheet.getFitToPage());
+    }
+
+    public void baseTestGetSetMargin(double[] defaultMargins) {
+        double marginLeft = defaultMargins[0];
+        double marginRight = defaultMargins[1];
+        double marginTop = defaultMargins[2];
+        double marginBottom = defaultMargins[3];
+        double marginHeader = defaultMargins[4];
+        double marginFooter = defaultMargins[5];
+
+        Workbook workbook = getTestDataProvider().createWorkbook();
+        Sheet sheet = workbook.createSheet("Sheet 1");
+        assertEquals(marginLeft, sheet.getMargin(Sheet.LeftMargin));
+        sheet.setMargin(Sheet.LeftMargin, 10.0);
+        //left margin is custom, all others are default
+        assertEquals(10.0, sheet.getMargin(Sheet.LeftMargin));
+        assertEquals(marginRight, sheet.getMargin(Sheet.RightMargin));
+        assertEquals(marginTop, sheet.getMargin(Sheet.TopMargin));
+        assertEquals(marginBottom, sheet.getMargin(Sheet.BottomMargin));
+        sheet.setMargin(Sheet.RightMargin, 11.0);
+        assertEquals(11.0, sheet.getMargin(Sheet.RightMargin));
+        sheet.setMargin(Sheet.TopMargin, 12.0);
+        assertEquals(12.0, sheet.getMargin(Sheet.TopMargin));
+        sheet.setMargin(Sheet.BottomMargin, 13.0);
+        assertEquals(13.0, sheet.getMargin(Sheet.BottomMargin));
+
+        // incorrect margin constant
+        try {
+            sheet.setMargin((short) 65, 15);
+            fail("Expected exception");
+        } catch (IllegalArgumentException e){
+            assertEquals("Unknown margin constant:  65", e.getMessage());
+        }
+    }
+
+    public void testRowBreaks() {
+        Workbook workbook = getTestDataProvider().createWorkbook();
+        Sheet sheet = workbook.createSheet();
+        //Sheet#getRowBreaks() returns an empty array if no row breaks are defined
+        assertNotNull(sheet.getRowBreaks());
+        assertEquals(0, sheet.getRowBreaks().length);
+
+        sheet.setRowBreak(1);
+        assertEquals(1, sheet.getRowBreaks().length);
+        sheet.setRowBreak(15);
+        assertEquals(2, sheet.getRowBreaks().length);
+        assertEquals(1, sheet.getRowBreaks()[0]);
+        assertEquals(15, sheet.getRowBreaks()[1]);
+        sheet.setRowBreak(1);
+        assertEquals(2, sheet.getRowBreaks().length);
+        assertTrue(sheet.isRowBroken(1));
+        assertTrue(sheet.isRowBroken(15));
+
+        //now remove the created breaks
+        sheet.removeRowBreak(1);
+        assertEquals(1, sheet.getRowBreaks().length);
+        sheet.removeRowBreak(15);
+        assertEquals(0, sheet.getRowBreaks().length);
+
+        assertFalse(sheet.isRowBroken(1));
+        assertFalse(sheet.isRowBroken(15));
+    }
+
+    public void testColumnBreaks() {
+        Workbook workbook = getTestDataProvider().createWorkbook();
+        Sheet sheet = workbook.createSheet();
+        assertNotNull(sheet.getColumnBreaks());
+        assertEquals(0, sheet.getColumnBreaks().length);
+
+        assertFalse(sheet.isColumnBroken(0));
+
+        sheet.setColumnBreak(11);
+        assertNotNull(sheet.getColumnBreaks());
+        assertEquals(11, sheet.getColumnBreaks()[0]);
+        sheet.setColumnBreak(12);
+        assertEquals(2, sheet.getColumnBreaks().length);
+        assertTrue(sheet.isColumnBroken(11));
+        assertTrue(sheet.isColumnBroken(12));
+
+        sheet.removeColumnBreak((short) 11);
+        assertEquals(1, sheet.getColumnBreaks().length);
+        sheet.removeColumnBreak((short) 15); //remove non-existing
+        assertEquals(1, sheet.getColumnBreaks().length);
+        sheet.removeColumnBreak((short) 12);
+        assertEquals(0, sheet.getColumnBreaks().length);
+
+        assertFalse(sheet.isColumnBroken(11));
+        assertFalse(sheet.isColumnBroken(12));
+    }
+}
\ No newline at end of file

Propchange: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java
------------------------------------------------------------------------------
    svn:executable = *

Added: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java?rev=759112&view=auto
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java (added)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java Fri Mar 27 11:50:52
2009
@@ -0,0 +1,276 @@
+package org.apache.poi.ss.usermodel;
+
+import junit.framework.TestCase;
+import junit.framework.AssertionFailedError;
+import org.apache.poi.ss.ITestDataProvider;
+import org.apache.poi.ss.util.CellRangeAddress;
+
+/**
+ * @author Yegor Kozlov
+ */
+public abstract class BaseTestWorkbook extends TestCase {
+
+    protected abstract ITestDataProvider getTestDataProvider();
+
+    public void testCreateSheet() {
+        Workbook wb = getTestDataProvider().createWorkbook();
+        assertEquals(0, wb.getNumberOfSheets());
+
+        //getting a sheet by invalid index or non-existing name
+        assertNull(wb.getSheet("Sheet1"));
+        try {
+            wb.getSheetAt(0);
+            fail("should have thrown exceptiuon due to invalid sheet index");
+        } catch (IllegalArgumentException e) {
+            // expected during successful test
+            ;
+        }
+
+        Sheet sheet0 = wb.createSheet();
+        Sheet sheet1 = wb.createSheet();
+        assertEquals("Sheet0", sheet0.getSheetName());
+        assertEquals("Sheet1", sheet1.getSheetName());
+        assertEquals(2, wb.getNumberOfSheets());
+
+        //fetching sheets by name is case-insensitive
+        Sheet originalSheet = wb.createSheet("Sheet3");
+        Sheet fetchedSheet = wb.getSheet("sheet3");
+        if (fetchedSheet == null) {
+            throw new AssertionFailedError("Identified bug 44892");
+        }
+        assertEquals("Sheet3", fetchedSheet.getSheetName());
+        assertEquals(3, wb.getNumberOfSheets());
+        assertSame(originalSheet, fetchedSheet);
+        try {
+            wb.createSheet("sHeeT3");
+            fail("should have thrown exceptiuon due to duplicate sheet name");
+        } catch (IllegalArgumentException e) {
+            // expected during successful test
+            assertEquals("The workbook already contains a sheet of this name", e.getMessage());
+        }
+
+        //names cannot be blank or contain any of /\*?[]
+        String[] invalidNames = {"", "Sheet/", "Sheet\\",
+                "Sheet?", "Sheet*", "Sheet[", "Sheet]"};
+        for (String sheetName : invalidNames) {
+            try {
+                wb.createSheet(sheetName);
+                fail("should have thrown exceptiuon due to invalid sheet name: " + sheetName);
+            } catch (IllegalArgumentException e) {
+                // expected during successful test
+                ;
+            }
+        }
+        //still have 3 sheets
+        assertEquals(3, wb.getNumberOfSheets());
+
+        //change the name of the 3rd sheet
+        wb.setSheetName(2, "I changed!");
+
+        //try to assign an invalid name to the 2nd sheet
+        try {
+            wb.setSheetName(1, "[I'm invalid]");
+            fail("should have thrown exceptiuon due to invalid sheet name");
+        } catch (IllegalArgumentException e) {
+            ; // expected during successful test
+        }
+
+        //check
+        assertEquals(0, wb.getSheetIndex("sheet0"));
+        assertEquals(1, wb.getSheetIndex("sheet1"));
+        assertEquals(2, wb.getSheetIndex("I changed!"));
+
+        assertSame(sheet0, wb.getSheet("sheet0"));
+        assertSame(sheet1, wb.getSheet("sheet1"));
+        assertSame(originalSheet, wb.getSheet("I changed!"));
+        assertNull(wb.getSheet("unknown"));
+
+        //serialize and read again
+        wb = getTestDataProvider().writeOutAndReadBack(wb);
+        assertEquals(3, wb.getNumberOfSheets());
+        assertEquals(0, wb.getSheetIndex("sheet0"));
+        assertEquals(1, wb.getSheetIndex("sheet1"));
+        assertEquals(2, wb.getSheetIndex("I changed!"));
+
+    }
+
+    public void testRemoveSheetAt() {
+        Workbook workbook = getTestDataProvider().createWorkbook();
+        workbook.createSheet("sheet1");
+        workbook.createSheet("sheet2");
+        workbook.createSheet("sheet3");
+        assertEquals(3, workbook.getNumberOfSheets());
+        workbook.removeSheetAt(1);
+        assertEquals(2, workbook.getNumberOfSheets());
+        assertEquals("sheet3", workbook.getSheetName(1));
+        workbook.removeSheetAt(0);
+        assertEquals(1, workbook.getNumberOfSheets());
+        assertEquals("sheet3", workbook.getSheetName(0));
+        workbook.removeSheetAt(0);
+        assertEquals(0, workbook.getNumberOfSheets());
+    }
+
+    public void testDefaultValues() {
+        Workbook b = getTestDataProvider().createWorkbook();
+        assertEquals(0, b.getActiveSheetIndex());
+        assertEquals(0, b.getFirstVisibleTab());
+        assertEquals(0, b.getNumberOfNames());
+        assertEquals(0, b.getNumberOfSheets());
+    }
+
+
+    public void testSheetSelection() {
+        Workbook b = getTestDataProvider().createWorkbook();
+        b.createSheet("Sheet One");
+        b.createSheet("Sheet Two");
+        b.setActiveSheet(1);
+        b.setSelectedTab(1);
+        b.setFirstVisibleTab(1);
+        assertEquals(1, b.getActiveSheetIndex());
+        assertEquals(1, b.getFirstVisibleTab());
+    }
+
+    public void testPrintArea() {
+        Workbook workbook = getTestDataProvider().createWorkbook();
+        Sheet sheet1 = workbook.createSheet("Test Print Area");
+        String sheetName1 = sheet1.getSheetName();
+
+        // workbook.setPrintArea(0, reference);
+        workbook.setPrintArea(0, 1, 5, 4, 9);
+        String retrievedPrintArea = workbook.getPrintArea(0);
+        assertEquals("'" + sheetName1 + "'!$B$5:$F$10", retrievedPrintArea);
+
+        String reference = "$A$1:$B$1";
+        workbook.setPrintArea(0, reference);
+        retrievedPrintArea = workbook.getPrintArea(0);
+        assertEquals("'" + sheetName1 + "'!" + reference, retrievedPrintArea);
+
+        workbook.removePrintArea(0);
+        assertNull(workbook.getPrintArea(0));
+    }
+
+    public void testGetSetActiveSheet(){
+        Workbook workbook = getTestDataProvider().createWorkbook();
+        assertEquals(0, workbook.getActiveSheetIndex());
+
+		workbook.createSheet("sheet1");
+		workbook.createSheet("sheet2");
+		workbook.createSheet("sheet3");
+		// set second sheet
+		workbook.setActiveSheet(1);
+		// test if second sheet is set up
+		assertEquals(1, workbook.getActiveSheetIndex());
+
+        workbook.setActiveSheet(0);
+        // test if second sheet is set up
+        assertEquals(0, workbook.getActiveSheetIndex());
+	}
+
+    public void testSetSheetOrder() {
+        Workbook wb = getTestDataProvider().createWorkbook();
+
+        for (int i=0; i < 10; i++) {
+			Sheet sh = wb.createSheet("Sheet " + i);
+        }
+
+        // Check the initial order
+        assertEquals(0, wb.getSheetIndex("Sheet 0"));
+        assertEquals(1, wb.getSheetIndex("Sheet 1"));
+        assertEquals(2, wb.getSheetIndex("Sheet 2"));
+        assertEquals(3, wb.getSheetIndex("Sheet 3"));
+        assertEquals(4, wb.getSheetIndex("Sheet 4"));
+        assertEquals(5, wb.getSheetIndex("Sheet 5"));
+        assertEquals(6, wb.getSheetIndex("Sheet 6"));
+        assertEquals(7, wb.getSheetIndex("Sheet 7"));
+        assertEquals(8, wb.getSheetIndex("Sheet 8"));
+        assertEquals(9, wb.getSheetIndex("Sheet 9"));
+
+        // Change
+        wb.setSheetOrder("Sheet 6", 0);
+        wb.setSheetOrder("Sheet 3", 7);
+        wb.setSheetOrder("Sheet 1", 9);
+
+        // Check they're currently right
+        assertEquals(0, wb.getSheetIndex("Sheet 6"));
+        assertEquals(1, wb.getSheetIndex("Sheet 0"));
+        assertEquals(2, wb.getSheetIndex("Sheet 2"));
+        assertEquals(3, wb.getSheetIndex("Sheet 4"));
+        assertEquals(4, wb.getSheetIndex("Sheet 5"));
+        assertEquals(5, wb.getSheetIndex("Sheet 7"));
+        assertEquals(6, wb.getSheetIndex("Sheet 3"));
+        assertEquals(7, wb.getSheetIndex("Sheet 8"));
+        assertEquals(8, wb.getSheetIndex("Sheet 9"));
+        assertEquals(9, wb.getSheetIndex("Sheet 1"));
+
+        Workbook wbr = getTestDataProvider().writeOutAndReadBack(wb);
+
+        assertEquals(0, wbr.getSheetIndex("Sheet 6"));
+        assertEquals(1, wbr.getSheetIndex("Sheet 0"));
+        assertEquals(2, wbr.getSheetIndex("Sheet 2"));
+        assertEquals(3, wbr.getSheetIndex("Sheet 4"));
+        assertEquals(4, wbr.getSheetIndex("Sheet 5"));
+        assertEquals(5, wbr.getSheetIndex("Sheet 7"));
+        assertEquals(6, wbr.getSheetIndex("Sheet 3"));
+        assertEquals(7, wbr.getSheetIndex("Sheet 8"));
+        assertEquals(8, wbr.getSheetIndex("Sheet 9"));
+        assertEquals(9, wbr.getSheetIndex("Sheet 1"));
+
+        // Now get the index by the sheet, not the name
+        for(int i=0; i<10; i++) {
+        	Sheet s = wbr.getSheetAt(i);
+        	assertEquals(i, wbr.getSheetIndex(s));
+        }
+    }
+
+    public void testCloneSheet() {
+        Workbook book = getTestDataProvider().createWorkbook();
+        Sheet sheet = book.createSheet("TEST");
+        sheet.createRow(0).createCell(0).setCellValue("Test");
+        sheet.createRow(1).createCell(0).setCellValue(36.6);
+        sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 2));
+        sheet.addMergedRegion(new CellRangeAddress(1, 2, 0, 2));
+        assertTrue(sheet.isSelected());
+
+        Sheet clonedSheet = book.cloneSheet(0);
+        assertEquals("TEST (2)", clonedSheet.getSheetName());
+        assertEquals(2, clonedSheet.getPhysicalNumberOfRows());
+        assertEquals(2, clonedSheet.getNumMergedRegions());
+        assertFalse(clonedSheet.isSelected());
+
+        //cloned sheet is a deep copy, adding rows in the original does not affect the clone
+        sheet.createRow(2).createCell(0).setCellValue(1);
+        sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 2));
+        assertEquals(2, clonedSheet.getPhysicalNumberOfRows());
+        assertEquals(2, clonedSheet.getPhysicalNumberOfRows());
+
+        clonedSheet.createRow(2).createCell(0).setCellValue(1);
+        clonedSheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 2));
+        assertEquals(3, clonedSheet.getPhysicalNumberOfRows());
+        assertEquals(3, clonedSheet.getPhysicalNumberOfRows());
+
+    }
+
+    public void testParentReferences(){
+        Workbook workbook = getTestDataProvider().createWorkbook();
+        Sheet sheet = workbook.createSheet();
+        assertSame(workbook, sheet.getWorkbook());
+
+        Row row = sheet.createRow(0);
+        assertSame(sheet, row.getSheet());
+
+        Cell cell = row.createCell(1);
+        assertSame(sheet, cell.getSheet());
+        assertSame(row, cell.getRow());
+
+        workbook = getTestDataProvider().writeOutAndReadBack(workbook);
+        sheet = workbook.getSheetAt(0);
+        assertSame(workbook, sheet.getWorkbook());
+
+        row = sheet.getRow(0);
+        assertSame(sheet, row.getSheet());
+
+        cell = row.getCell(1);
+        assertSame(sheet, cell.getSheet());
+        assertSame(row, cell.getRow());
+    }
+}

Propchange: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java
------------------------------------------------------------------------------
    svn:executable = *



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


Mime
View raw message