From e47dd63c80d7807381f77ee0d8879332737368cf Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Sun, 29 Mar 2009 16:10:40 +0000 Subject: [PATCH] more refactoring of common ss tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@759714 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/usermodel/HSSFCell.java | 12 - .../poi/hssf/usermodel/HSSFCellStyle.java | 189 ------ .../apache/poi/hssf/usermodel/HSSFFont.java | 7 + .../apache/poi/hssf/usermodel/HSSFName.java | 11 + .../org/apache/poi/ss/usermodel/Font.java | 9 - .../org/apache/poi/ss/usermodel/Picture.java | 3 + .../apache/poi/xssf/model/StylesTable.java | 5 +- .../apache/poi/xssf/usermodel/XSSFFont.java | 17 +- .../apache/poi/xssf/usermodel/XSSFName.java | 1 + .../poi/xssf/usermodel/XSSFWorkbook.java | 5 + .../poi/xssf/XSSFITestDataProvider.java | 5 + .../poi/xssf/usermodel/TestXSSFBugs.java | 32 +- .../poi/xssf/usermodel/TestXSSFFont.java | 215 +------ .../poi/xssf/usermodel/TestXSSFName.java | 42 ++ .../poi/xssf/usermodel/TestXSSFPicture.java | 14 +- .../poi/xssf/usermodel/TestXSSFSheet.java | 408 ++++++------ .../poi/xssf/usermodel/TestXSSFWorkbook.java | 84 --- .../poi/hssf/HSSFITestDataProvider.java | 4 + .../poi/hssf/usermodel/AllUserModelTests.java | 2 +- .../poi/hssf/usermodel/TestBug43093.java | 61 -- .../apache/poi/hssf/usermodel/TestBugs.java | 230 +------ .../poi/hssf/usermodel/TestHSSFFont.java | 56 ++ .../poi/hssf/usermodel/TestHSSFName.java | 203 +++++- .../poi/hssf/usermodel/TestHSSFPicture.java | 28 +- .../poi/hssf/usermodel/TestHSSFWorkbook.java | 26 +- .../poi/hssf/usermodel/TestNamedRange.java | 603 ------------------ .../hssf/usermodel/TestUnicodeWorkbook.java | 113 +--- .../org/apache/poi/ss/ITestDataProvider.java | 6 + .../ss/usermodel/BaseTestBugzillaIssues.java | 298 +++++++++ .../apache/poi/ss/usermodel/BaseTestFont.java | 204 ++++++ .../poi/ss/usermodel/BaseTestNamedRange.java | 330 +++++++++- .../poi/ss/usermodel/BaseTestPicture.java | 53 ++ .../apache/poi/ss/usermodel/BaseTestRow.java | 3 + .../poi/ss/usermodel/BaseTestSheet.java | 27 + .../poi/ss/usermodel/BaseTestWorkbook.java | 73 +++ 35 files changed, 1605 insertions(+), 1774 deletions(-) delete mode 100644 src/testcases/org/apache/poi/hssf/usermodel/TestBug43093.java create mode 100755 src/testcases/org/apache/poi/hssf/usermodel/TestHSSFFont.java delete mode 100644 src/testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java create mode 100755 src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java create mode 100755 src/testcases/org/apache/poi/ss/usermodel/BaseTestFont.java create mode 100755 src/testcases/org/apache/poi/ss/usermodel/BaseTestPicture.java diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index c34587de7c..65736aa9f0 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -74,18 +74,6 @@ import org.apache.poi.ss.formula.FormulaType; * @author Yegor Kozlov cell comments support */ public class HSSFCell implements Cell { - /** Numeric Cell type (0) @see #setCellType(int) @see #getCellType() */ - public final static int CELL_TYPE_NUMERIC = 0; - /** String Cell type (1) @see #setCellType(int) @see #getCellType() */ - public final static int CELL_TYPE_STRING = 1; - /** Formula Cell type (2) @see #setCellType(int) @see #getCellType() */ - public final static int CELL_TYPE_FORMULA = 2; - /** Blank Cell type (3) @see #setCellType(int) @see #getCellType() */ - public final static int CELL_TYPE_BLANK = 3; - /** Boolean Cell type (4) @see #setCellType(int) @see #getCellType() */ - public final static int CELL_TYPE_BOOLEAN = 4; - /** Error Cell type (5) @see #setCellType(int) @see #getCellType() */ - public final static int CELL_TYPE_ERROR = 5; private static final String FILE_FORMAT_NAME = "BIFF8"; /** diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java index d321512070..a56143ff42 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java @@ -44,195 +44,6 @@ public class HSSFCellStyle implements CellStyle private short index = 0; private Workbook workbook = null; - /** - * general (normal) horizontal alignment - */ - - public final static short ALIGN_GENERAL = 0x0; - - /** - * left-justified horizontal alignment - */ - - public final static short ALIGN_LEFT = 0x1; - - /** - * center horizontal alignment - */ - - public final static short ALIGN_CENTER = 0x2; - - /** - * right-justified horizontal alignment - */ - - public final static short ALIGN_RIGHT = 0x3; - - /** - * fill? horizontal alignment - */ - - public final static short ALIGN_FILL = 0x4; - - /** - * justified horizontal alignment - */ - - public final static short ALIGN_JUSTIFY = 0x5; - - /** - * center-selection? horizontal alignment - */ - - public final static short ALIGN_CENTER_SELECTION = 0x6; - - /** - * top-aligned vertical alignment - */ - - public final static short VERTICAL_TOP = 0x0; - - /** - * center-aligned vertical alignment - */ - - public final static short VERTICAL_CENTER = 0x1; - - /** - * bottom-aligned vertical alignment - */ - - public final static short VERTICAL_BOTTOM = 0x2; - - /** - * vertically justified vertical alignment - */ - - public final static short VERTICAL_JUSTIFY = 0x3; - - /** - * No border - */ - - public final static short BORDER_NONE = 0x0; - - /** - * Thin border - */ - - public final static short BORDER_THIN = 0x1; - - /** - * Medium border - */ - - public final static short BORDER_MEDIUM = 0x2; - - /** - * dash border - */ - - public final static short BORDER_DASHED = 0x3; - - /** - * dot border - */ - - public final static short BORDER_HAIR = 0x4; - - /** - * Thick border - */ - - public final static short BORDER_THICK = 0x5; - - /** - * double-line border - */ - - public final static short BORDER_DOUBLE = 0x6; - - /** - * hair-line border - */ - - public final static short BORDER_DOTTED = 0x7; - - /** - * Medium dashed border - */ - - public final static short BORDER_MEDIUM_DASHED = 0x8; - - /** - * dash-dot border - */ - - public final static short BORDER_DASH_DOT = 0x9; - - /** - * medium dash-dot border - */ - - public final static short BORDER_MEDIUM_DASH_DOT = 0xA; - - /** - * dash-dot-dot border - */ - - public final static short BORDER_DASH_DOT_DOT = 0xB; - - /** - * medium dash-dot-dot border - */ - - public final static short BORDER_MEDIUM_DASH_DOT_DOT = 0xC; - - /** - * slanted dash-dot border - */ - - public final static short BORDER_SLANTED_DASH_DOT = 0xD; - - /** No background */ - public final static short NO_FILL = 0 ; - /** Solidly filled */ - public final static short SOLID_FOREGROUND = 1 ; - /** Small fine dots */ - public final static short FINE_DOTS = 2 ; - /** Wide dots */ - public final static short ALT_BARS = 3 ; - /** Sparse dots */ - public final static short SPARSE_DOTS = 4 ; - /** Thick horizontal bands */ - public final static short THICK_HORZ_BANDS = 5 ; - /** Thick vertical bands */ - public final static short THICK_VERT_BANDS = 6 ; - /** Thick backward facing diagonals */ - public final static short THICK_BACKWARD_DIAG = 7 ; - /** Thick forward facing diagonals */ - public final static short THICK_FORWARD_DIAG = 8 ; - /** Large spots */ - public final static short BIG_SPOTS = 9 ; - /** Brick-like layout */ - public final static short BRICKS = 10 ; - /** Thin horizontal bands */ - public final static short THIN_HORZ_BANDS = 11 ; - /** Thin vertical bands */ - public final static short THIN_VERT_BANDS = 12 ; - /** Thin backward diagonal */ - public final static short THIN_BACKWARD_DIAG = 13 ; - /** Thin forward diagonal */ - public final static short THIN_FORWARD_DIAG = 14 ; - /** Squares */ - public final static short SQUARES = 15 ; - /** Diamonds */ - public final static short DIAMONDS = 16 ; - /** Less Dots */ - public final static short LESS_DOTS = 17 ; - /** Least Dots */ - public final static short LEAST_DOTS = 18 ; - /** Creates new HSSFCellStyle why would you want to do this?? */ protected HSSFCellStyle(short index, ExtendedFormatRecord rec, HSSFWorkbook workbook) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java b/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java index 310abecefe..58621bdc9e 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java @@ -31,6 +31,13 @@ import org.apache.poi.ss.usermodel.Font; */ public final class HSSFFont implements Font { + /** + * Arial font + */ + + public final static String FONT_ARIAL = "Arial"; + + private FontRecord font; private short index; diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFName.java b/src/java/org/apache/poi/hssf/usermodel/HSSFName.java index 979f30ecc7..a0f278cc6c 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFName.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFName.java @@ -112,6 +112,8 @@ public final class HSSFName implements Name { * @throws IllegalArgumentException if the name is invalid or the name already exists (case-insensitive) */ public void setNameName(String nameName){ + validateName(nameName); + Workbook wb = _book.getWorkbook(); _definedNameRec.setNameText(nameName); @@ -131,6 +133,15 @@ public final class HSSFName implements Name { } } + private static void validateName(String name){ + if(name.length() == 0) throw new IllegalArgumentException("Name cannot be blank"); + + char c = name.charAt(0); + if(!(c == '_' || Character.isLetter(c)) || name.indexOf(' ') != -1) { + throw new IllegalArgumentException("Invalid name: '"+name+"'; Names must begin with a letter or underscore and not contain spaces"); + } + } + /** * Returns the formula that the name is defined to refer to. * diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Font.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Font.java index 1af438f7ca..b9c10a8b15 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Font.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Font.java @@ -19,13 +19,6 @@ package org.apache.poi.ss.usermodel; public interface Font { - - /** - * Arial font - */ - - public final static String FONT_ARIAL = "Arial"; - /** * Normal boldness (not bold) */ @@ -116,7 +109,6 @@ public interface Font { /** * set the name for the font (i.e. Arial) * @param name String representing the name of the font to use - * @see #FONT_ARIAL */ void setFontName(String name); @@ -124,7 +116,6 @@ public interface Font { /** * get the name for the font (i.e. Arial) * @return String representing the name of the font to use - * @see #FONT_ARIAL */ String getFontName(); diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Picture.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Picture.java index eb5bc8828e..dd6a8e7d31 100755 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Picture.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Picture.java @@ -36,4 +36,7 @@ public interface Picture { * resize(2.0) resizes to 200% of the original. */ void resize(double scale); + + ClientAnchor getPreferredSize(); + } diff --git a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java index 33da9e4ea6..0844f45189 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java @@ -26,6 +26,7 @@ import java.util.Map.Entry; import org.apache.poi.ss.usermodel.FontFamily; import org.apache.poi.ss.usermodel.FontScheme; import org.apache.poi.ss.usermodel.BuiltinFormats; +import org.apache.poi.ss.usermodel.Font; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder; @@ -468,9 +469,9 @@ public class StylesTable extends POIXMLDocumentPart { */ public XSSFFont findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) { for (XSSFFont font : fonts) { - if ( (font.getBold() == (boldWeight == XSSFFont.BOLDWEIGHT_BOLD)) + if ( (font.getBoldweight() == boldWeight) && font.getColor() == color - && font.getFontHeightInPoints() == fontHeight + && font.getFontHeight() == fontHeight && font.getFontName().equals(name) && font.getItalic() == italic && font.getStrikeout() == strikeout diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java index c4af9bdafd..739ef4ca50 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java @@ -66,7 +66,7 @@ public class XSSFFont implements Font { protected XSSFFont() { this.ctFont = CTFont.Factory.newInstance(); setFontName(DEFAULT_FONT_NAME); - setFontHeight(DEFAULT_FONT_SIZE); + setFontHeight((double)DEFAULT_FONT_SIZE); } /** @@ -154,21 +154,16 @@ public class XSSFFont implements Font { CTFontSize size = ctFont.sizeOfSzArray() == 0 ? null : ctFont.getSzArray(0); if (size != null) { double fontHeight = size.getVal(); - return (short) fontHeight; + return (short)(fontHeight*20); } else - return DEFAULT_FONT_SIZE; + return (short)(DEFAULT_FONT_SIZE*20); } /** * @see #getFontHeight() */ public short getFontHeightInPoints() { - CTFontSize size = ctFont.sizeOfSzArray() == 0 ? null : ctFont.getSzArray(0); - if (size != null) { - double fontHeight = size.getVal(); - return (short) fontHeight; - } else - return DEFAULT_FONT_SIZE; + return (short)(getFontHeight()/20); } /** @@ -347,7 +342,7 @@ public class XSSFFont implements Font { * @param height - height in points */ public void setFontHeight(short height) { - setFontHeight((double) height); + setFontHeight((double) height/20); } /** @@ -366,7 +361,7 @@ public class XSSFFont implements Font { * @link #setFontHeight */ public void setFontHeightInPoints(short height) { - setFontHeight(height); + setFontHeight((double)height); } /** diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java index 47fc9ba04a..4517f1292f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java @@ -359,6 +359,7 @@ public final class XSSFName implements Name { } private static void validateName(String name){ + if(name.length() == 0) throw new IllegalArgumentException("Name cannot be blank"); char c = name.charAt(0); if(!(c == '_' || Character.isLetter(c)) || name.indexOf(' ') != -1) { throw new IllegalArgumentException("Invalid name: '"+name+"'; Names must begin with a letter or underscore and not contain spaces"); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 8af2c29a2e..874cd0f385 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -436,6 +436,11 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable=0; i--) { - clone.removeMergedRegion(i); - } - - assertEquals("Original Sheet's Merged Regions were removed", originalMerged, template.getNumMergedRegions()); -// check if template's merged regions are OK - if (template.getNumMergedRegions()>0) { - // fetch the first merged region...EXCEPTION OCCURS HERE - template.getMergedRegion(0); - } - //make sure we dont exception - - } - /**Tests read and write of Unicode strings in formula results * bug and testcase submitted by Sompop Kumnoonsate * The file contains THAI unicode characters. @@ -424,21 +327,6 @@ public final class TestBugs extends TestCase { } } - public void test28031() { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(); - wb.setSheetName(0, "Sheet1"); - - HSSFRow row = sheet.createRow(0); - HSSFCell cell = row.createCell(0); - String formulaText = - "IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))"; - cell.setCellFormula(formulaText); - - assertEquals(formulaText, cell.getCellFormula()); - writeTestOutputFileForViewing(wb, "output28031.xls"); - } - public void test33082() { openSample("33082.xls"); } @@ -486,19 +374,8 @@ public final class TestBugs extends TestCase { */ public void test29206() { //the first check with blank workbook - HSSFWorkbook wb = new HSSFWorkbook(); + HSSFWorkbook wb = openSample("Simple.xls"); HSSFSheet sheet = wb.createSheet(); - - for(int i = 1; i < 400; i++) { - HSSFRow row = sheet.getRow(i); - if(row != null) { - row.getCell(0); - } - } - - //now check on an existing xls file - wb = openSample("Simple.xls"); - for(int i = 1; i < 400; i++) { HSSFRow row = sheet.getRow(i); if(row != null) { @@ -901,23 +778,6 @@ public final class TestBugs extends TestCase { assertTrue("no errors writing sample xls", true); } - /** - * Bug 21334: "File error: data may have been lost" with a file - * that contains macros and this formula: - * {=SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""))>0,1))} - */ - public void test21334() { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sh = wb.createSheet(); - HSSFCell cell = sh.createRow(0).createCell(0); - String formula = "SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"))>0,1))"; - cell.setCellFormula(formula); - - HSSFWorkbook wb_sv = writeOutAndReadBack(wb); - HSSFCell cell_sv = wb_sv.getSheetAt(0).getRow(0).getCell(0); - assertEquals(formula, cell_sv.getCellFormula()); - } - public void test36947() { HSSFWorkbook wb = openSample("36947.xls"); assertTrue("no errors reading sample xls", true); @@ -925,16 +785,6 @@ public final class TestBugs extends TestCase { assertTrue("no errors writing sample xls", true); } - /** - * Bug 42448: Can't parse SUMPRODUCT(A!C7:A!C67, B8:B68) / B69 - */ - public void test42448(){ - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFCell cell = wb.createSheet().createRow(0).createCell(0); - cell.setCellFormula("SUMPRODUCT(A!C7:A!C67, B8:B68) / B69"); - assertTrue("no errors parsing formula", true); - } - public void test39634() { HSSFWorkbook wb = openSample("39634.xls"); assertTrue("no errors reading sample xls", true); @@ -1521,38 +1371,6 @@ public final class TestBugs extends TestCase { assertEquals(1, wb.getNumberOfSheets()); } - /** - * HSSFRichTextString.length() returns negative for really - * long strings - */ - public void test46368() { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet s = wb.createSheet(); - HSSFRow r = s.createRow(0); - for(int i=0; i<15; i++) { - int len = 32760 + i; - HSSFCell c = r.createCell(i); - - StringBuffer sb = new StringBuffer(); - for(int j=0; j + * Addresses Bug #9632 + */ + public void testNamedRead() { + HSSFWorkbook wb = getTestDataProvider().openSampleWorkbook("namedinput.xls"); + + //Get index of the namedrange with the name = "NamedRangeName" , which was defined in input.xls as A1:D10 + int NamedRangeIndex = wb.getNameIndex("NamedRangeName"); + + //Getting NAmed Range + HSSFName namedRange1 = wb.getNameAt(NamedRangeIndex); + String sheetName = wb.getSheetName(0); + + //Getting its reference + String reference = namedRange1.getRefersToFormula(); + + assertEquals(sheetName+"!$A$1:$D$10", reference); + + HSSFName namedRange2 = wb.getNameAt(1); + + assertEquals(sheetName+"!$D$17:$G$27", namedRange2.getRefersToFormula()); + assertEquals("SecondNamedRange", namedRange2.getNameName()); + } + + /** + * Reads an excel file already containing a named range and updates it + *

+ * Addresses Bug #16411 + */ + public void testNamedReadModify() { + HSSFWorkbook wb = getTestDataProvider().openSampleWorkbook("namedinput.xls"); + + HSSFName name = wb.getNameAt(0); + String sheetName = wb.getSheetName(0); + + assertEquals(sheetName+"!$A$1:$D$10", name.getRefersToFormula()); + + name = wb.getNameAt(1); + String newReference = sheetName +"!$A$1:$C$36"; + + name.setRefersToFormula(newReference); + assertEquals(newReference, name.getRefersToFormula()); + } + + /** + * Test to see if the print area can be retrieved from an excel created file + */ + public void testPrintAreaFileRead() { + HSSFWorkbook workbook = getTestDataProvider().openSampleWorkbook("SimpleWithPrintArea.xls"); + + String sheetName = workbook.getSheetName(0); + String reference = sheetName+"!$A$1:$C$5"; + + assertEquals(reference, workbook.getPrintArea(0)); + } + + + public void testDeletedReference() throws Exception { + HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("24207.xls"); + assertEquals(2, wb.getNumberOfNames()); + + HSSFName name1 = wb.getNameAt(0); + assertEquals("a", name1.getNameName()); + assertEquals("Sheet1!$A$1", name1.getRefersToFormula()); + new AreaReference(name1.getRefersToFormula()); + assertTrue("Successfully constructed first reference", true); + + HSSFName name2 = wb.getNameAt(1); + assertEquals("b", name2.getNameName()); + assertEquals("Sheet1!#REF!", name2.getRefersToFormula()); + assertTrue(name2.isDeleted()); + try { + new AreaReference(name2.getRefersToFormula()); + fail("attempt to supply an invalid reference to AreaReference constructor results in exception"); + } catch (StringIndexOutOfBoundsException e) { // TODO - use a different exception for this condition + // expected during successful test + } + } + + /** + * When setting A1 type of referencese HSSFName.setRefersToFormula + * must set the type of operands to Ptg.CLASS_REF, + * otherwise created named don't appear in the dropdown to the left opf formula bar in Excel + */ + public void testTypeOfRootPtg(){ + HSSFWorkbook wb = new HSSFWorkbook(); + wb.createSheet("CSCO"); + + Ptg[] ptgs = HSSFFormulaParser.parse("CSCO!$E$71", wb, FormulaType.NAMEDRANGE, 0); + for (int i = 0; i < ptgs.length; i++) { + assertEquals('R', ptgs[i].getRVAType()); + } + + } } \ No newline at end of file diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java index d1a6719e32..81c8990769 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java @@ -19,33 +19,23 @@ package org.apache.poi.hssf.usermodel; import junit.framework.TestCase; import org.apache.poi.hssf.HSSFTestDataSamples; +import org.apache.poi.hssf.HSSFITestDataProvider; +import org.apache.poi.ss.usermodel.BaseTestPicture; /** * Test HSSFPicture. * * @author Yegor Kozlov (yegor at apache.org) */ -public final class TestHSSFPicture extends TestCase{ +public final class TestHSSFPicture extends BaseTestPicture { + + @Override + protected HSSFITestDataProvider getTestDataProvider(){ + return HSSFITestDataProvider.getInstance(); + } public void testResize() { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sh1 = wb.createSheet(); - HSSFPatriarch p1 = sh1.createDrawingPatriarch(); - - byte[] pictureData = HSSFTestDataSamples.getTestDataFileContent("logoKarmokar4.png"); - int idx1 = wb.addPicture( pictureData, HSSFWorkbook.PICTURE_TYPE_PNG ); - HSSFPicture picture1 = p1.createPicture(new HSSFClientAnchor(), idx1); - HSSFClientAnchor anchor1 = picture1.getPreferredSize(); - - //assert against what would BiffViewer print if we insert the image in xls and dump the file - assertEquals(0, anchor1.getCol1()); - assertEquals(0, anchor1.getRow1()); - assertEquals(1, anchor1.getCol2()); - assertEquals(9, anchor1.getRow2()); - assertEquals(0, anchor1.getDx1()); - assertEquals(0, anchor1.getDy1()); - assertEquals(848, anchor1.getDx2()); - assertEquals(240, anchor1.getDy2()); + baseTestResize(new HSSFClientAnchor(0, 0, 848, 240, (short)0, 0, (short)1, 9)); } /** diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java index 880f3ecf24..484ff70e18 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java @@ -59,23 +59,6 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook { assertEquals(3, nameRecord.getSheetNumber()); } - public void testCaseInsensitiveNames() { - HSSFWorkbook b = new HSSFWorkbook( ); - HSSFSheet originalSheet = b.createSheet("Sheet1"); - HSSFSheet fetchedSheet = b.getSheet("sheet1"); - if(fetchedSheet == null) { - throw new AssertionFailedError("Identified bug 44892"); - } - assertEquals(originalSheet, fetchedSheet); - try { - b.createSheet("sHeeT1"); - 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()); - } - } - public void testWindowOneDefaults() { HSSFWorkbook b = new HSSFWorkbook( ); try { @@ -154,7 +137,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook { // So, start again b = getTestDataProvider().openSampleWorkbook("44010-SingleChart.xls"); - b = writeRead(b); + b = getTestDataProvider().writeOutAndReadBack(b); assertEquals(2, b.getNumberOfSheets()); s = b.getSheetAt(1); assertEquals(0, s.getFirstRowNum()); @@ -184,7 +167,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook { // So, start again b = getTestDataProvider().openSampleWorkbook("44010-TwoCharts.xls"); - b = writeRead(b); + b = getTestDataProvider().writeOutAndReadBack(b); assertEquals(3, b.getNumberOfSheets()); s = b.getSheetAt(1); @@ -195,11 +178,6 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook { assertEquals(8, s.getLastRowNum()); } - private static HSSFWorkbook writeRead(HSSFWorkbook b) { - return HSSFTestDataSamples.writeOutAndReadBack(b); - } - - public void testSelectedSheet_bug44523() { HSSFWorkbook wb=new HSSFWorkbook(); HSSFSheet sheet1 = wb.createSheet("Sheet1"); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java b/src/testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java deleted file mode 100644 index ecb6d7a62b..0000000000 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java +++ /dev/null @@ -1,603 +0,0 @@ -/* ==================================================================== - 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.hssf.usermodel; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; - -import junit.framework.TestCase; - -import org.apache.poi.hssf.HSSFTestDataSamples; -import org.apache.poi.hssf.model.HSSFFormulaParser; -import org.apache.poi.hssf.record.formula.Ptg; -import org.apache.poi.ss.util.AreaReference; -import org.apache.poi.ss.util.CellReference; -import org.apache.poi.ss.formula.FormulaType; - -/** - * - * @author ROMANL - * @author Andrew C. Oliver (acoliver at apache dot org) - * @author Danny Mui (danny at muibros.com) - * @author Amol S. Deshmukh < amol at ap ache dot org > - */ -public final class TestNamedRange extends TestCase { - - private static HSSFWorkbook openSample(String sampleFileName) { - return HSSFTestDataSamples.openSampleWorkbook(sampleFileName); - } - - /** Test of TestCase method, of class test.RangeTest. */ - public void testNamedRange() { - HSSFWorkbook wb = openSample("Simple.xls"); - - //Creating new Named Range - HSSFName newNamedRange = wb.createName(); - - //Getting Sheet Name for the reference - String sheetName = wb.getSheetName(0); - - //Setting its name - newNamedRange.setNameName("RangeTest"); - //Setting its reference - newNamedRange.setRefersToFormula(sheetName + "!$D$4:$E$8"); - - //Getting NAmed Range - HSSFName namedRange1 = wb.getNameAt(0); - //Getting it sheet name - sheetName = namedRange1.getSheetName(); - - // sanity check - SanityChecker c = new SanityChecker(); - c.checkHSSFWorkbook(wb); - - wb = HSSFTestDataSamples.writeOutAndReadBack(wb); - HSSFName nm =wb.getNameAt(wb.getNameIndex("RangeTest")); - assertTrue("Name is "+nm.getNameName(),"RangeTest".equals(nm.getNameName())); - assertEquals(wb.getSheetName(0)+"!$D$4:$E$8", nm.getRefersToFormula()); - } - - /** - * Reads an excel file already containing a named range. - *

- * Addresses Bug #9632 - */ - public void testNamedRead() { - HSSFWorkbook wb = openSample("namedinput.xls"); - - //Get index of the namedrange with the name = "NamedRangeName" , which was defined in input.xls as A1:D10 - int NamedRangeIndex = wb.getNameIndex("NamedRangeName"); - - //Getting NAmed Range - HSSFName namedRange1 = wb.getNameAt(NamedRangeIndex); - String sheetName = wb.getSheetName(0); - - //Getting its reference - String reference = namedRange1.getRefersToFormula(); - - assertEquals(sheetName+"!$A$1:$D$10", reference); - - HSSFName namedRange2 = wb.getNameAt(1); - - assertEquals(sheetName+"!$D$17:$G$27", namedRange2.getRefersToFormula()); - assertEquals("SecondNamedRange", namedRange2.getNameName()); - } - - /** - * Reads an excel file already containing a named range and updates it - *

- * Addresses Bug #16411 - */ - public void testNamedReadModify() { - HSSFWorkbook wb = openSample("namedinput.xls"); - - HSSFName name = wb.getNameAt(0); - String sheetName = wb.getSheetName(0); - - assertEquals(sheetName+"!$A$1:$D$10", name.getRefersToFormula()); - - name = wb.getNameAt(1); - String newReference = sheetName +"!$A$1:$C$36"; - - name.setRefersToFormula(newReference); - assertEquals(newReference, name.getRefersToFormula()); - } - - /** - * Test that multiple named ranges can be added written and read - */ - public void testMultipleNamedWrite() { - HSSFWorkbook wb = new HSSFWorkbook(); - - - wb.createSheet("testSheet1"); - String sheetName = wb.getSheetName(0); - - assertEquals("testSheet1", sheetName); - - //Creating new Named Range - HSSFName newNamedRange = wb.createName(); - - newNamedRange.setNameName("RangeTest"); - newNamedRange.setRefersToFormula(sheetName + "!$D$4:$E$8"); - - //Creating another new Named Range - HSSFName newNamedRange2 = wb.createName(); - - newNamedRange2.setNameName("AnotherTest"); - newNamedRange2.setRefersToFormula(sheetName + "!$F$1:$G$6"); - - wb.getNameAt(0); - - wb = HSSFTestDataSamples.writeOutAndReadBack(wb); - HSSFName nm =wb.getNameAt(wb.getNameIndex("RangeTest")); - assertTrue("Name is "+nm.getNameName(),"RangeTest".equals(nm.getNameName())); - assertTrue("Reference is "+nm.getRefersToFormula(),(wb.getSheetName(0)+"!$D$4:$E$8").equals(nm.getRefersToFormula())); - - nm = wb.getNameAt(wb.getNameIndex("AnotherTest")); - assertTrue("Name is "+nm.getNameName(),"AnotherTest".equals(nm.getNameName())); - assertTrue("Reference is "+nm.getRefersToFormula(),newNamedRange2.getRefersToFormula().equals(nm.getRefersToFormula())); - } - - /** - * Test case provided by czhang@cambian.com (Chun Zhang) - *

- * Addresses Bug #13775 - */ - public void testMultiNamedRange() { - - // Create a new workbook - HSSFWorkbook wb = new HSSFWorkbook (); - - - // Create a worksheet 'sheet1' in the new workbook - wb.createSheet (); - wb.setSheetName (0, "sheet1"); - - // Create another worksheet 'sheet2' in the new workbook - wb.createSheet (); - wb.setSheetName (1, "sheet2"); - - // Create a new named range for worksheet 'sheet1' - HSSFName namedRange1 = wb.createName(); - - // Set the name for the named range for worksheet 'sheet1' - namedRange1.setNameName("RangeTest1"); - - // Set the reference for the named range for worksheet 'sheet1' - namedRange1.setRefersToFormula("sheet1" + "!$A$1:$L$41"); - - // Create a new named range for worksheet 'sheet2' - HSSFName namedRange2 = wb.createName(); - - // Set the name for the named range for worksheet 'sheet2' - namedRange2.setNameName("RangeTest2"); - - // Set the reference for the named range for worksheet 'sheet2' - namedRange2.setRefersToFormula("sheet2" + "!$A$1:$O$21"); - - // Write the workbook to a file - // Read the Excel file and verify its content - wb = HSSFTestDataSamples.writeOutAndReadBack(wb); - HSSFName nm1 =wb.getNameAt(wb.getNameIndex("RangeTest1")); - assertTrue("Name is "+nm1.getNameName(),"RangeTest1".equals(nm1.getNameName())); - assertTrue("Reference is "+nm1.getRefersToFormula(),(wb.getSheetName(0)+"!$A$1:$L$41").equals(nm1.getRefersToFormula())); - - HSSFName nm2 =wb.getNameAt(wb.getNameIndex("RangeTest2")); - assertTrue("Name is "+nm2.getNameName(),"RangeTest2".equals(nm2.getNameName())); - assertTrue("Reference is "+nm2.getRefersToFormula(),(wb.getSheetName(1)+"!$A$1:$O$21").equals(nm2.getRefersToFormula())); - } - - public void testUnicodeNamedRange() { - HSSFWorkbook workBook = new HSSFWorkbook(); - workBook.createSheet("Test"); - HSSFName name = workBook.createName(); - name.setNameName("\u03B1"); - name.setRefersToFormula("Test!$D$3:$E$8"); - - - HSSFWorkbook workBook2 = HSSFTestDataSamples.writeOutAndReadBack(workBook); - HSSFName name2 = workBook2.getNameAt(0); - - assertEquals("\u03B1", name2.getNameName()); - assertEquals("Test!$D$3:$E$8", name2.getRefersToFormula()); - } - - /** - * Test to see if the print areas can be retrieved/created in memory - */ - public void testSinglePrintArea() { - HSSFWorkbook workbook = new HSSFWorkbook(); - workbook.createSheet("Test Print Area"); - String sheetName = workbook.getSheetName(0); - - String reference = "$A$1:$B$1"; - workbook.setPrintArea(0, reference); - - String retrievedPrintArea = workbook.getPrintArea(0); - - assertNotNull("Print Area not defined for first sheet", retrievedPrintArea); - assertEquals("'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea); - } - - /** - * For Convenience, don't force sheet names to be used - */ - public void testSinglePrintAreaWOSheet() - { - HSSFWorkbook workbook = new HSSFWorkbook(); - workbook.createSheet("Test Print Area"); - String sheetName = workbook.getSheetName(0); - - String reference = "$A$1:$B$1"; - workbook.setPrintArea(0, reference); - - String retrievedPrintArea = workbook.getPrintArea(0); - - assertNotNull("Print Area not defined for first sheet", retrievedPrintArea); - assertEquals("'" + sheetName + "'!" + reference, retrievedPrintArea); - } - - /** - * Test to see if the print area can be retrieved from an excel created file - */ - public void testPrintAreaFileRead() { - HSSFWorkbook workbook = openSample("SimpleWithPrintArea.xls"); - - String sheetName = workbook.getSheetName(0); - String reference = sheetName+"!$A$1:$C$5"; - - assertEquals(reference, workbook.getPrintArea(0)); - } - - /** - * Test to see if the print area made it to the file - */ - public void testPrintAreaFile() { - HSSFWorkbook workbook = new HSSFWorkbook(); - workbook.createSheet("Test Print Area"); - String sheetName = workbook.getSheetName(0); - - - String reference = "$A$1:$B$1"; - workbook.setPrintArea(0, reference); - - workbook = HSSFTestDataSamples.writeOutAndReadBack(workbook); - - String retrievedPrintArea = workbook.getPrintArea(0); - assertNotNull("Print Area not defined for first sheet", retrievedPrintArea); - assertEquals("References Match", "'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea); - } - - /** - * Test to see if multiple print areas made it to the file - */ - public void testMultiplePrintAreaFile() { - HSSFWorkbook workbook = new HSSFWorkbook(); - - workbook.createSheet("Sheet1"); - workbook.createSheet("Sheet2"); - workbook.createSheet("Sheet3"); - String reference1 = "$A$1:$B$1"; - String reference2 = "$B$2:$D$5"; - String reference3 = "$D$2:$F$5"; - - workbook.setPrintArea(0, reference1); - workbook.setPrintArea(1, reference2); - workbook.setPrintArea(2, reference3); - - //Check created print areas - String retrievedPrintArea; - - retrievedPrintArea = workbook.getPrintArea(0); - assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea); - assertEquals("Sheet1!" + reference1, retrievedPrintArea); - - retrievedPrintArea = workbook.getPrintArea(1); - assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea); - assertEquals("Sheet2!" + reference2, retrievedPrintArea); - - retrievedPrintArea = workbook.getPrintArea(2); - assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea); - assertEquals("Sheet3!" + reference3, retrievedPrintArea); - - // Check print areas after re-reading workbook - workbook = HSSFTestDataSamples.writeOutAndReadBack(workbook); - - retrievedPrintArea = workbook.getPrintArea(0); - assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea); - assertEquals("Sheet1!" + reference1, retrievedPrintArea); - - retrievedPrintArea = workbook.getPrintArea(1); - assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea); - assertEquals("Sheet2!" + reference2, retrievedPrintArea); - - retrievedPrintArea = workbook.getPrintArea(2); - assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea); - assertEquals("Sheet3!" + reference3, retrievedPrintArea); - } - - /** - * Tests the setting of print areas with coordinates (Row/Column designations) - * - */ - public void testPrintAreaCoords(){ - HSSFWorkbook workbook = new HSSFWorkbook(); - workbook.createSheet("Test Print Area"); - String sheetName = workbook.getSheetName(0); - - workbook.setPrintArea(0, 0, 1, 0, 0); - - String retrievedPrintArea = workbook.getPrintArea(0); - - assertNotNull("Print Area not defined for first sheet", retrievedPrintArea); - assertEquals("'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea); - } - - - /** - * Tests the parsing of union area expressions, and re-display in the presence of sheet names - * with special characters. - */ - public void testPrintAreaUnion(){ - HSSFWorkbook workbook = new HSSFWorkbook(); - workbook.createSheet("Test Print Area"); - - String reference = "$A$1:$B$1,$D$1:$F$2"; - workbook.setPrintArea(0, reference); - String retrievedPrintArea = workbook.getPrintArea(0); - assertNotNull("Print Area not defined for first sheet", retrievedPrintArea); - assertEquals("'Test Print Area'!$A$1:$B$1,'Test Print Area'!$D$1:$F$2", retrievedPrintArea); - } - - /** - * Verifies an existing print area is deleted - * - */ - public void testPrintAreaRemove() { - HSSFWorkbook workbook = new HSSFWorkbook(); - workbook.createSheet("Test Print Area"); - workbook.getSheetName(0); - - workbook.setPrintArea(0, 0, 1, 0, 0); - - String retrievedPrintArea = workbook.getPrintArea(0); - - assertNotNull("Print Area not defined for first sheet", retrievedPrintArea); - - workbook.removePrintArea(0); - assertNull("PrintArea was not removed", workbook.getPrintArea(0)); - } - - /** - * Verifies correct functioning for "single cell named range" (aka "named cell") - */ - public void testNamedCell_1() { - - // setup for this testcase - String sheetName = "Test Named Cell"; - String cellName = "A name for a named cell"; - String cellValue = "TEST Value"; - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(sheetName); - sheet.createRow(0).createCell(0).setCellValue(new HSSFRichTextString(cellValue)); - - // create named range for a single cell using areareference - HSSFName namedCell = wb.createName(); - namedCell.setNameName(cellName); - String reference = "'" + sheetName + "'" + "!A1:A1"; - namedCell.setRefersToFormula(reference); - - // retrieve the newly created named range - int namedCellIdx = wb.getNameIndex(cellName); - HSSFName aNamedCell = wb.getNameAt(namedCellIdx); - assertNotNull(aNamedCell); - - // retrieve the cell at the named range and test its contents - AreaReference aref = new AreaReference(aNamedCell.getRefersToFormula()); - assertTrue("Should be exactly 1 cell in the named cell :'" +cellName+"'", aref.isSingleCell()); - - CellReference cref = aref.getFirstCell(); - assertNotNull(cref); - HSSFSheet s = wb.getSheet(cref.getSheetName()); - assertNotNull(s); - HSSFRow r = sheet.getRow(cref.getRow()); - HSSFCell c = r.getCell(cref.getCol()); - String contents = c.getRichStringCellValue().getString(); - assertEquals("Contents of cell retrieved by its named reference", contents, cellValue); - } - - /** - * Verifies correct functioning for "single cell named range" (aka "named cell") - */ - public void testNamedCell_2() { - - // setup for this testcase - String sname = "TestSheet", cname = "TestName", cvalue = "TestVal"; - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet(sname); - sheet.createRow(0).createCell(0).setCellValue(new HSSFRichTextString(cvalue)); - - // create named range for a single cell using cellreference - HSSFName namedCell = wb.createName(); - namedCell.setNameName(cname); - String reference = sname+"!A1"; - namedCell.setRefersToFormula(reference); - - // retrieve the newly created named range - int namedCellIdx = wb.getNameIndex(cname); - HSSFName aNamedCell = wb.getNameAt(namedCellIdx); - assertNotNull(aNamedCell); - - // retrieve the cell at the named range and test its contents - CellReference cref = new CellReference(aNamedCell.getRefersToFormula()); - assertNotNull(cref); - HSSFSheet s = wb.getSheet(cref.getSheetName()); - HSSFRow r = sheet.getRow(cref.getRow()); - HSSFCell c = r.getCell(cref.getCol()); - String contents = c.getRichStringCellValue().getString(); - assertEquals("Contents of cell retrieved by its named reference", contents, cvalue); - } - - public void testDeletedReference() throws Exception { - HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("24207.xls"); - assertEquals(2, wb.getNumberOfNames()); - - HSSFName name1 = wb.getNameAt(0); - assertEquals("a", name1.getNameName()); - assertEquals("Sheet1!$A$1", name1.getRefersToFormula()); - new AreaReference(name1.getRefersToFormula()); - assertTrue("Successfully constructed first reference", true); - - HSSFName name2 = wb.getNameAt(1); - assertEquals("b", name2.getNameName()); - assertEquals("Sheet1!#REF!", name2.getRefersToFormula()); - assertTrue(name2.isDeleted()); - try { - new AreaReference(name2.getRefersToFormula()); - fail("attempt to supply an invalid reference to AreaReference constructor results in exception"); - } catch (StringIndexOutOfBoundsException e) { // TODO - use a different exception for this condition - // expected during successful test - } - } - - public void testRepeatingRowsAndColumsNames() { - // First test that setting RR&C for same sheet more than once only creates a - // single Print_Titles built-in record - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet = wb.createSheet("FirstSheet"); - - // set repeating rows and columns twice for the first sheet - for (int i = 0; i < 2; i++) { - wb.setRepeatingRowsAndColumns(0, 0, 0, 0, 3-1); - sheet.createFreezePane(0, 3); - } - assertEquals(1, wb.getNumberOfNames()); - HSSFName nr1 = wb.getNameAt(0); - - assertEquals("Print_Titles", nr1.getNameName()); - if (false) { - // TODO - full column references not rendering properly, absolute markers not present either - assertEquals("FirstSheet!$A:$A,FirstSheet!$1:$3", nr1.getRefersToFormula()); - } else { - assertEquals("FirstSheet!A:A,FirstSheet!$A$1:$IV$3", nr1.getRefersToFormula()); - } - - // Save and re-open - HSSFWorkbook nwb = HSSFTestDataSamples.writeOutAndReadBack(wb); - - assertEquals(1, nwb.getNumberOfNames()); - nr1 = nwb.getNameAt(0); - - assertEquals("Print_Titles", nr1.getNameName()); - assertEquals("FirstSheet!A:A,FirstSheet!$A$1:$IV$3", nr1.getRefersToFormula()); - - // check that setting RR&C on a second sheet causes a new Print_Titles built-in - // name to be created - sheet = nwb.createSheet("SecondSheet"); - nwb.setRepeatingRowsAndColumns(1, 1, 2, 0, 0); - - assertEquals(2, nwb.getNumberOfNames()); - HSSFName nr2 = nwb.getNameAt(1); - - assertEquals("Print_Titles", nr2.getNameName()); - assertEquals("SecondSheet!B:C,SecondSheet!$A$1:$IV$1", nr2.getRefersToFormula()); - - if (false) { - // In case you fancy checking in excel, to ensure it - // won't complain about the file now - try { - File tempFile = File.createTempFile("POI-45126-", ".xls"); - FileOutputStream fout = new FileOutputStream(tempFile); - nwb.write(fout); - fout.close(); - System.out.println("check out " + tempFile.getAbsolutePath()); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - } - - /** - * When setting A1 type of referencese HSSFName.setRefersToFormula - * must set the type of operands to Ptg.CLASS_REF, - * otherwise created named don't appear in the dropdown to the left opf formula bar in Excel - */ - public void testTypeOfRootPtg(){ - HSSFWorkbook wb = new HSSFWorkbook(); - wb.createSheet("CSCO"); - - Ptg[] ptgs = HSSFFormulaParser.parse("CSCO!$E$71", wb, FormulaType.NAMEDRANGE, 0); - for (int i = 0; i < ptgs.length; i++) { - assertEquals('R', ptgs[i].getRVAType()); - } - - } - - public void testScope() { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sh1 = wb.createSheet(); - HSSFSheet sh2 = wb.createSheet(); - - HSSFName name; - - name = wb.createName(); - name.setNameName("aaa"); - name = wb.createName(); - try { - name.setNameName("aaa"); - fail("Expected exception"); - } catch(Exception e){ - assertEquals("The workbook already contains this name: aaa", e.getMessage()); - name.setNameName("aaa-2"); - } - - name = wb.createName(); - name.setSheetIndex(0); - name.setNameName("aaa"); - name = wb.createName(); - name.setSheetIndex(0); - try { - name.setNameName("aaa"); - fail("Expected exception"); - } catch(Exception e){ - assertEquals("The sheet already contains this name: aaa", e.getMessage()); - name.setNameName("aaa-2"); - } - - name = wb.createName(); - name.setSheetIndex(1); - name.setNameName("aaa"); - name = wb.createName(); - name.setSheetIndex(1); - try { - name.setNameName("aaa"); - fail("Expected exception"); - } catch(Exception e){ - assertEquals("The sheet already contains this name: aaa", e.getMessage()); - name.setNameName("aaa-2"); - } - - int cnt = 0; - for (int i = 0; i < wb.getNumberOfNames(); i++) { - if("aaa".equals(wb.getNameName(i))) cnt++; - } - assertEquals(3, cnt); - } -} diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java index 2a92152857..a90aa29080 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java @@ -16,132 +16,39 @@ */ package org.apache.poi.hssf.usermodel; -import java.io.File; -import java.io.FileOutputStream; -import java.io.FileInputStream; - -import org.apache.poi.util.TempFile; - import junit.framework.TestCase; +import org.apache.poi.hssf.HSSFTestDataSamples; public class TestUnicodeWorkbook extends TestCase { - public TestUnicodeWorkbook(String s) { - super(s); - } - - /** Tests that all of the unicode capable string fields can be set, written and then read back - * - * - */ - public void testUnicodeInAll() throws Exception { - HSSFWorkbook wb = new HSSFWorkbook(); - //Create a unicode dataformat (contains euro symbol) - HSSFDataFormat df = wb.createDataFormat(); - final String formatStr = "_([$\u20ac-2]\\\\\\ * #,##0.00_);_([$\u20ac-2]\\\\\\ * \\\\\\(#,##0.00\\\\\\);_([$\u20ac-2]\\\\\\ *\\\"\\-\\\\\"??_);_(@_)"; - short fmt = df.getFormat(formatStr); - - //Create a unicode sheet name (euro symbol) - HSSFSheet s = wb.createSheet("\u20ac"); - - //Set a unicode header (you guessed it the euro symbol) - HSSFHeader h = s.getHeader(); - h.setCenter("\u20ac"); - h.setLeft("\u20ac"); - h.setRight("\u20ac"); - - //Set a unicode footer - HSSFFooter f = s.getFooter(); - f.setCenter("\u20ac"); - f.setLeft("\u20ac"); - f.setRight("\u20ac"); - - HSSFRow r = s.createRow(0); - HSSFCell c = r.createCell(1); - c.setCellValue(12.34); - c.getCellStyle().setDataFormat(fmt); - - HSSFCell c2 = r.createCell(2); - c.setCellValue(new HSSFRichTextString("\u20ac")); - - HSSFCell c3 = r.createCell(3); - String formulaString = "TEXT(12.34,\"\u20ac###,##\")"; - c3.setCellFormula(formulaString); - - - File tempFile = TempFile.createTempFile("unicode", "test.xls"); - FileOutputStream stream = new FileOutputStream(tempFile); - wb.write(stream); - - wb = null; - FileInputStream in = new FileInputStream(tempFile); - wb = new HSSFWorkbook(in); - - //Test the sheetname - s = wb.getSheet("\u20ac"); - assertNotNull(s); - - //Test the header - h = s.getHeader(); - assertEquals(h.getCenter(), "\u20ac"); - assertEquals(h.getLeft(), "\u20ac"); - assertEquals(h.getRight(), "\u20ac"); - - //Test the footer - f = s.getFooter(); - assertEquals(f.getCenter(), "\u20ac"); - assertEquals(f.getLeft(), "\u20ac"); - assertEquals(f.getRight(), "\u20ac"); - - //Test the dataformat - r = s.getRow(0); - c = r.getCell(1); - df = wb.createDataFormat(); - assertEquals(formatStr, df.getFormat(c.getCellStyle().getDataFormat())); - - //Test the cell string value - c2 = r.getCell(2); - assertEquals(c.getRichStringCellValue().getString(), "\u20ac"); - - //Test the cell formula - c3 = r.getCell(3); - assertEquals(c3.getCellFormula(), formulaString); - } - /** Tests Bug38230 * That a Umlat is written and then read back. * It should have been written as a compressed unicode. - * - * + * + * * */ public void testUmlatReadWrite() throws Exception { HSSFWorkbook wb = new HSSFWorkbook(); - + //Create a unicode sheet name (euro symbol) HSSFSheet s = wb.createSheet("test"); - + HSSFRow r = s.createRow(0); HSSFCell c = r.createCell(1); c.setCellValue(new HSSFRichTextString("\u00e4")); - + //Confirm that the sring will be compressed assertEquals(c.getRichStringCellValue().getUnicodeString().getOptionFlags(), 0); - - File tempFile = TempFile.createTempFile("umlat", "test.xls"); - FileOutputStream stream = new FileOutputStream(tempFile); - wb.write(stream); - - wb = null; - FileInputStream in = new FileInputStream(tempFile); - wb = new HSSFWorkbook(in); + + wb = HSSFTestDataSamples.writeOutAndReadBack(wb); //Test the sheetname s = wb.getSheet("test"); assertNotNull(s); - + c = r.getCell(1); assertEquals(c.getRichStringCellValue().getString(), "\u00e4"); - } + } } diff --git a/src/testcases/org/apache/poi/ss/ITestDataProvider.java b/src/testcases/org/apache/poi/ss/ITestDataProvider.java index f3ca3b256f..983147691b 100755 --- a/src/testcases/org/apache/poi/ss/ITestDataProvider.java +++ b/src/testcases/org/apache/poi/ss/ITestDataProvider.java @@ -44,4 +44,10 @@ public interface ITestDataProvider { */ Workbook createWorkbook(); + /** + * Opens a sample file from the standard HSSF test data directory + * + * @return an open InputStream for the specified sample file + */ + byte[] getTestDataFileContent(String fileName); } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java new file mode 100755 index 0000000000..ef6d0a14fa --- /dev/null +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java @@ -0,0 +1,298 @@ +/* ==================================================================== + 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 junit.framework.AssertionFailedError; +import org.apache.poi.ss.ITestDataProvider; +import org.apache.poi.ss.util.CellRangeAddress; + +/** + * A base class for bugzilla issues that can be described in terms of common ss interfaces. + * + * @author Yegor Kozlov + */ +public abstract class BaseTestBugzillaIssues extends TestCase { + + protected abstract ITestDataProvider getTestDataProvider(); + + /** + * + * Test writing a hyperlink + * Open resulting sheet in Excel and check that A1 contains a hyperlink + * + * Also tests bug 15353 (problems with hyperlinks to Google) + */ + public void test23094() { + Workbook wb = getTestDataProvider().createWorkbook(); + Sheet s = wb.createSheet(); + Row r = s.createRow(0); + r.createCell(0).setCellFormula("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")"); + r.createCell(1).setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")"); + + wb = getTestDataProvider().writeOutAndReadBack(wb); + r = wb.getSheetAt(0).getRow(0); + + Cell cell_0 = r.getCell(0); + assertEquals("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")", cell_0.getCellFormula()); + Cell cell_1 = r.getCell(1); + assertEquals("HYPERLINK(\"http://google.com\",\"Google\")", cell_1.getCellFormula()); + } + + /** + * test writing a file with large number of unique strings, + * open resulting file in Excel to check results! + * @param num the number of strings to generate + */ + public void baseTest15375(int num) { + Workbook wb = getTestDataProvider().createWorkbook(); + Sheet sheet = wb.createSheet(); + CreationHelper factory = wb.getCreationHelper(); + + String tmp1 = null; + String tmp2 = null; + String tmp3 = null; + + for (int i = 0; i < num; i++) { + tmp1 = "Test1" + i; + tmp2 = "Test2" + i; + tmp3 = "Test3" + i; + + Row row = sheet.createRow(i); + + Cell cell = row.createCell(0); + cell.setCellValue(factory.createRichTextString(tmp1)); + cell = row.createCell(1); + cell.setCellValue(factory.createRichTextString(tmp2)); + cell = row.createCell(2); + cell.setCellValue(factory.createRichTextString(tmp3)); + } + wb = getTestDataProvider().writeOutAndReadBack(wb); + for (int i = 0; i < num; i++) { + tmp1 = "Test1" + i; + tmp2 = "Test2" + i; + tmp3 = "Test3" + i; + + Row row = sheet.getRow(i); + + assertEquals(tmp1, row.getCell(0).getStringCellValue()); + assertEquals(tmp2, row.getCell(1).getStringCellValue()); + assertEquals(tmp3, row.getCell(2).getStringCellValue()); + } + } + + /** + * Merged regions were being removed from the parent in cloned sheets + */ + public void test22720() { + Workbook workBook = getTestDataProvider().createWorkbook(); + workBook.createSheet("TEST"); + Sheet template = workBook.getSheetAt(0); + + template.addMergedRegion(new CellRangeAddress(0, 1, 0, 2)); + template.addMergedRegion(new CellRangeAddress(1, 2, 0, 2)); + + Sheet clone = workBook.cloneSheet(0); + int originalMerged = template.getNumMergedRegions(); + assertEquals("2 merged regions", 2, originalMerged); + + //remove merged regions from clone + for (int i=template.getNumMergedRegions()-1; i>=0; i--) { + clone.removeMergedRegion(i); + } + + assertEquals("Original Sheet's Merged Regions were removed", originalMerged, template.getNumMergedRegions()); + //check if template's merged regions are OK + if (template.getNumMergedRegions()>0) { + // fetch the first merged region...EXCEPTION OCCURS HERE + template.getMergedRegion(0); + } + //make sure we dont exception + + } + + public void test28031() { + Workbook wb = getTestDataProvider().createWorkbook(); + Sheet sheet = wb.createSheet(); + wb.setSheetName(0, "Sheet1"); + + Row row = sheet.createRow(0); + Cell cell = row.createCell(0); + String formulaText = + "IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))"; + cell.setCellFormula(formulaText); + + assertEquals(formulaText, cell.getCellFormula()); + wb = getTestDataProvider().writeOutAndReadBack(wb); + cell = wb.getSheetAt(0).getRow(0).getCell(0); + assertEquals("IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))", cell.getCellFormula()); + } + + /** + * Bug 21334: "File error: data may have been lost" with a file + * that contains macros and this formula: + * {=SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""))>0,1))} + */ + public void test21334() { + Workbook wb = getTestDataProvider().createWorkbook(); + Sheet sh = wb.createSheet(); + Cell cell = sh.createRow(0).createCell(0); + String formula = "SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"))>0,1))"; + cell.setCellFormula(formula); + + Workbook wb_sv = getTestDataProvider().writeOutAndReadBack(wb); + Cell cell_sv = wb_sv.getSheetAt(0).getRow(0).getCell(0); + assertEquals(formula, cell_sv.getCellFormula()); + } + + /** another test for the number of unique strings issue + *test opening the resulting file in Excel*/ + public void test22568() { + int r=2000;int c=3; + + Workbook wb = getTestDataProvider().createWorkbook(); + Sheet sheet = wb.createSheet("ExcelTest") ; + + int col_cnt=0, rw_cnt=0 ; + + col_cnt = c; + rw_cnt = r; + + Row rw ; + rw = sheet.createRow(0) ; + //Header row + for(int j=0; j + * Addresses Bug #13775 + */ + public void testMultiNamedRange() { + + // Create a new workbook + Workbook wb = getTestDataProvider().createWorkbook(); + + // Create a worksheet 'sheet1' in the new workbook + wb.createSheet (); + wb.setSheetName (0, "sheet1"); + + // Create another worksheet 'sheet2' in the new workbook + wb.createSheet (); + wb.setSheetName (1, "sheet2"); + + // Create a new named range for worksheet 'sheet1' + Name namedRange1 = wb.createName(); + + // Set the name for the named range for worksheet 'sheet1' + namedRange1.setNameName("RangeTest1"); + + // Set the reference for the named range for worksheet 'sheet1' + namedRange1.setRefersToFormula("sheet1" + "!$A$1:$L$41"); + + // Create a new named range for worksheet 'sheet2' + Name namedRange2 = wb.createName(); + + // Set the name for the named range for worksheet 'sheet2' + namedRange2.setNameName("RangeTest2"); + + // Set the reference for the named range for worksheet 'sheet2' + namedRange2.setRefersToFormula("sheet2" + "!$A$1:$O$21"); + + // Write the workbook to a file + // Read the Excel file and verify its content + wb = getTestDataProvider().writeOutAndReadBack(wb); + Name nm1 =wb.getNameAt(wb.getNameIndex("RangeTest1")); + assertTrue("Name is "+nm1.getNameName(),"RangeTest1".equals(nm1.getNameName())); + assertTrue("Reference is "+nm1.getRefersToFormula(),(wb.getSheetName(0)+"!$A$1:$L$41").equals(nm1.getRefersToFormula())); + + Name nm2 =wb.getNameAt(wb.getNameIndex("RangeTest2")); + assertTrue("Name is "+nm2.getNameName(),"RangeTest2".equals(nm2.getNameName())); + assertTrue("Reference is "+nm2.getRefersToFormula(),(wb.getSheetName(1)+"!$A$1:$O$21").equals(nm2.getRefersToFormula())); + } + + /** + * Test to see if the print areas can be retrieved/created in memory + */ + public void testSinglePrintArea() { + Workbook workbook = getTestDataProvider().createWorkbook(); + workbook.createSheet("Test Print Area"); + String sheetName = workbook.getSheetName(0); + + String reference = "$A$1:$B$1"; + workbook.setPrintArea(0, reference); + + String retrievedPrintArea = workbook.getPrintArea(0); + + assertNotNull("Print Area not defined for first sheet", retrievedPrintArea); + assertEquals("'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea); + } + + /** + * For Convenience, don't force sheet names to be used + */ + public void testSinglePrintAreaWOSheet() + { + Workbook workbook = getTestDataProvider().createWorkbook(); + workbook.createSheet("Test Print Area"); + String sheetName = workbook.getSheetName(0); + + String reference = "$A$1:$B$1"; + workbook.setPrintArea(0, reference); + + String retrievedPrintArea = workbook.getPrintArea(0); + + assertNotNull("Print Area not defined for first sheet", retrievedPrintArea); + assertEquals("'" + sheetName + "'!" + reference, retrievedPrintArea); + } + + /** + * Test to see if the print area made it to the file + */ + public void testPrintAreaFile() { + Workbook workbook = getTestDataProvider().createWorkbook(); + workbook.createSheet("Test Print Area"); + String sheetName = workbook.getSheetName(0); + + + String reference = "$A$1:$B$1"; + workbook.setPrintArea(0, reference); + + workbook = getTestDataProvider().writeOutAndReadBack(workbook); + + String retrievedPrintArea = workbook.getPrintArea(0); + assertNotNull("Print Area not defined for first sheet", retrievedPrintArea); + assertEquals("References Match", "'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea); + } + + /** + * Test to see if multiple print areas made it to the file + */ + public void testMultiplePrintAreaFile() { + Workbook workbook = getTestDataProvider().createWorkbook(); + + workbook.createSheet("Sheet1"); + workbook.createSheet("Sheet2"); + workbook.createSheet("Sheet3"); + String reference1 = "$A$1:$B$1"; + String reference2 = "$B$2:$D$5"; + String reference3 = "$D$2:$F$5"; + + workbook.setPrintArea(0, reference1); + workbook.setPrintArea(1, reference2); + workbook.setPrintArea(2, reference3); + + //Check created print areas + String retrievedPrintArea; + + retrievedPrintArea = workbook.getPrintArea(0); + assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea); + assertEquals("Sheet1!" + reference1, retrievedPrintArea); + + retrievedPrintArea = workbook.getPrintArea(1); + assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea); + assertEquals("Sheet2!" + reference2, retrievedPrintArea); + + retrievedPrintArea = workbook.getPrintArea(2); + assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea); + assertEquals("Sheet3!" + reference3, retrievedPrintArea); + + // Check print areas after re-reading workbook + workbook = getTestDataProvider().writeOutAndReadBack(workbook); + + retrievedPrintArea = workbook.getPrintArea(0); + assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea); + assertEquals("Sheet1!" + reference1, retrievedPrintArea); + + retrievedPrintArea = workbook.getPrintArea(1); + assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea); + assertEquals("Sheet2!" + reference2, retrievedPrintArea); + + retrievedPrintArea = workbook.getPrintArea(2); + assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea); + assertEquals("Sheet3!" + reference3, retrievedPrintArea); + } + + /** + * Tests the setting of print areas with coordinates (Row/Column designations) + * + */ + public void testPrintAreaCoords(){ + Workbook workbook = getTestDataProvider().createWorkbook(); + workbook.createSheet("Test Print Area"); + String sheetName = workbook.getSheetName(0); + + workbook.setPrintArea(0, 0, 1, 0, 0); + + String retrievedPrintArea = workbook.getPrintArea(0); + + assertNotNull("Print Area not defined for first sheet", retrievedPrintArea); + assertEquals("'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea); + } + + + /** + * Tests the parsing of union area expressions, and re-display in the presence of sheet names + * with special characters. + */ + public void testPrintAreaUnion(){ + Workbook workbook = getTestDataProvider().createWorkbook(); + workbook.createSheet("Test Print Area"); + + String reference = "$A$1:$B$1,$D$1:$F$2"; + workbook.setPrintArea(0, reference); + String retrievedPrintArea = workbook.getPrintArea(0); + assertNotNull("Print Area not defined for first sheet", retrievedPrintArea); + assertEquals("'Test Print Area'!$A$1:$B$1,'Test Print Area'!$D$1:$F$2", retrievedPrintArea); + } + + /** + * Verifies an existing print area is deleted + * + */ + public void testPrintAreaRemove() { + Workbook workbook = getTestDataProvider().createWorkbook(); + workbook.createSheet("Test Print Area"); + workbook.getSheetName(0); + + workbook.setPrintArea(0, 0, 1, 0, 0); + + String retrievedPrintArea = workbook.getPrintArea(0); + + assertNotNull("Print Area not defined for first sheet", retrievedPrintArea); + + workbook.removePrintArea(0); + assertNull("PrintArea was not removed", workbook.getPrintArea(0)); + } + + /** + * Test that multiple named ranges can be added written and read + */ + public void testMultipleNamedWrite() { + Workbook wb = getTestDataProvider().createWorkbook(); + + + wb.createSheet("testSheet1"); + String sheetName = wb.getSheetName(0); + + assertEquals("testSheet1", sheetName); + + //Creating new Named Range + Name newNamedRange = wb.createName(); + + newNamedRange.setNameName("RangeTest"); + newNamedRange.setRefersToFormula(sheetName + "!$D$4:$E$8"); + + //Creating another new Named Range + Name newNamedRange2 = wb.createName(); + + newNamedRange2.setNameName("AnotherTest"); + newNamedRange2.setRefersToFormula(sheetName + "!$F$1:$G$6"); + + wb.getNameAt(0); + + wb = getTestDataProvider().writeOutAndReadBack(wb); + Name nm =wb.getNameAt(wb.getNameIndex("RangeTest")); + assertTrue("Name is "+nm.getNameName(),"RangeTest".equals(nm.getNameName())); + assertTrue("Reference is "+nm.getRefersToFormula(),(wb.getSheetName(0)+"!$D$4:$E$8").equals(nm.getRefersToFormula())); + + nm = wb.getNameAt(wb.getNameIndex("AnotherTest")); + assertTrue("Name is "+nm.getNameName(),"AnotherTest".equals(nm.getNameName())); + assertTrue("Reference is "+nm.getRefersToFormula(),newNamedRange2.getRefersToFormula().equals(nm.getRefersToFormula())); + } + /** + * Verifies correct functioning for "single cell named range" (aka "named cell") + */ + public void testNamedCell_1() { + + // setup for this testcase + String sheetName = "Test Named Cell"; + String cellName = "named_cell"; + String cellValue = "TEST Value"; + Workbook wb = getTestDataProvider().createWorkbook(); + Sheet sheet = wb.createSheet(sheetName); + CreationHelper factory = wb.getCreationHelper(); + sheet.createRow(0).createCell(0).setCellValue(factory.createRichTextString(cellValue)); + + // create named range for a single cell using areareference + Name namedCell = wb.createName(); + namedCell.setNameName(cellName); + String reference = "'" + sheetName + "'" + "!A1:A1"; + namedCell.setRefersToFormula(reference); + + // retrieve the newly created named range + int namedCellIdx = wb.getNameIndex(cellName); + Name aNamedCell = wb.getNameAt(namedCellIdx); + assertNotNull(aNamedCell); + + // retrieve the cell at the named range and test its contents + AreaReference aref = new AreaReference(aNamedCell.getRefersToFormula()); + assertTrue("Should be exactly 1 cell in the named cell :'" +cellName+"'", aref.isSingleCell()); + + CellReference cref = aref.getFirstCell(); + assertNotNull(cref); + Sheet s = wb.getSheet(cref.getSheetName()); + assertNotNull(s); + Row r = sheet.getRow(cref.getRow()); + Cell c = r.getCell(cref.getCol()); + String contents = c.getRichStringCellValue().getString(); + assertEquals("Contents of cell retrieved by its named reference", contents, cellValue); + } + + /** + * Verifies correct functioning for "single cell named range" (aka "named cell") + */ + public void testNamedCell_2() { + + // setup for this testcase + String sname = "TestSheet", cname = "TestName", cvalue = "TestVal"; + Workbook wb = getTestDataProvider().createWorkbook(); + CreationHelper factory = wb.getCreationHelper(); + Sheet sheet = wb.createSheet(sname); + sheet.createRow(0).createCell(0).setCellValue(factory.createRichTextString(cvalue)); + + // create named range for a single cell using cellreference + Name namedCell = wb.createName(); + namedCell.setNameName(cname); + String reference = sname+"!A1"; + namedCell.setRefersToFormula(reference); + + // retrieve the newly created named range + int namedCellIdx = wb.getNameIndex(cname); + Name aNamedCell = wb.getNameAt(namedCellIdx); + assertNotNull(aNamedCell); + + // retrieve the cell at the named range and test its contents + CellReference cref = new CellReference(aNamedCell.getRefersToFormula()); + assertNotNull(cref); + Sheet s = wb.getSheet(cref.getSheetName()); + Row r = sheet.getRow(cref.getRow()); + Cell c = r.getCell(cref.getCol()); + String contents = c.getRichStringCellValue().getString(); + assertEquals("Contents of cell retrieved by its named reference", contents, cvalue); + } + } \ No newline at end of file diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestPicture.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestPicture.java new file mode 100755 index 0000000000..03cea5d5f2 --- /dev/null +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestPicture.java @@ -0,0 +1,53 @@ +/* ==================================================================== + 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; + +/** + * @author Yegor Kozlov + */ +public abstract class BaseTestPicture extends TestCase { + + protected abstract ITestDataProvider getTestDataProvider(); + + public void baseTestResize(ClientAnchor referenceAnchor) { + Workbook wb = getTestDataProvider().createWorkbook(); + Sheet sh1 = wb.createSheet(); + Drawing p1 = sh1.createDrawingPatriarch(); + CreationHelper factory = wb.getCreationHelper(); + + byte[] pictureData = getTestDataProvider().getTestDataFileContent("logoKarmokar4.png"); + int idx1 = wb.addPicture( pictureData, Workbook.PICTURE_TYPE_PNG ); + Picture picture = p1.createPicture(factory.createClientAnchor(), idx1); + picture.resize(); + ClientAnchor anchor1 = picture.getPreferredSize(); + + //assert against what would BiffViewer print if we insert the image in xls and dump the file + assertEquals(referenceAnchor.getCol1(), anchor1.getCol1()); + assertEquals(referenceAnchor.getRow1(), anchor1.getRow1()); + assertEquals(referenceAnchor.getCol2(), anchor1.getCol2()); + assertEquals(referenceAnchor.getRow2(), anchor1.getRow2()); + assertEquals(referenceAnchor.getDx1(), anchor1.getDx1()); + assertEquals(referenceAnchor.getDy1(), anchor1.getDy1()); + assertEquals(referenceAnchor.getDx2(), anchor1.getDx2()); + assertEquals(referenceAnchor.getDy2(), anchor1.getDy2()); + } + + +} \ No newline at end of file diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestRow.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestRow.java index 5aee7a18e4..3c162d7762 100755 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestRow.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestRow.java @@ -41,6 +41,9 @@ public abstract class BaseTestRow extends TestCase { assertEquals(-1, row.getFirstCellNum()); assertEquals(-1, row.getLastCellNum()); + //getting cells from an empty row should returns null + for(int i=0; i < 10; i++) assertNull(row.getCell(i)); + row.createCell(2); assertEquals(2, row.getFirstCellNum()); assertEquals(3, row.getLastCellNum()); diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java index e2e70b4f43..c119ce05fc 100755 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java @@ -562,4 +562,31 @@ public abstract class BaseTestSheet extends TestCase { assertFalse(sheet.isColumnBroken(11)); assertFalse(sheet.isColumnBroken(12)); } + + public void testGetFirstLastRowNum() { + Workbook workbook = getTestDataProvider().createWorkbook(); + Sheet sheet = workbook.createSheet("Sheet 1"); + Row row10 = sheet.createRow(9); + Row row1 = sheet.createRow(0); + Row row2 = sheet.createRow(1); + assertEquals(0, sheet.getFirstRowNum()); + assertEquals(9, sheet.getLastRowNum()); + } + + public void testGetFooter() { + Workbook workbook = getTestDataProvider().createWorkbook(); + Sheet sheet = workbook.createSheet("Sheet 1"); + assertNotNull(sheet.getFooter()); + sheet.getFooter().setCenter("test center footer"); + assertEquals("test center footer", sheet.getFooter().getCenter()); + } + + public void testGetSetColumnHidden() { + Workbook workbook = getTestDataProvider().createWorkbook(); + Sheet sheet = workbook.createSheet("Sheet 1"); + sheet.setColumnHidden(2, true); + assertTrue(sheet.isColumnHidden(2)); + } + + } \ No newline at end of file diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java index eafda064df..1a556dc0ed 100755 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java @@ -273,4 +273,77 @@ public abstract class BaseTestWorkbook extends TestCase { assertSame(sheet, cell.getSheet()); assertSame(row, cell.getRow()); } + + /** Tests that all of the unicode capable string fields can be set, written and then read back + * + * + */ + public void testUnicodeInAll() throws Exception { + Workbook wb = getTestDataProvider().createWorkbook(); + CreationHelper factory = wb.getCreationHelper(); + //Create a unicode dataformat (contains euro symbol) + DataFormat df = wb.createDataFormat(); + final String formatStr = "_([$\u20ac-2]\\\\\\ * #,##0.00_);_([$\u20ac-2]\\\\\\ * \\\\\\(#,##0.00\\\\\\);_([$\u20ac-2]\\\\\\ *\\\"\\-\\\\\"??_);_(@_)"; + short fmt = df.getFormat(formatStr); + + //Create a unicode sheet name (euro symbol) + Sheet s = wb.createSheet("\u20ac"); + + //Set a unicode header (you guessed it the euro symbol) + Header h = s.getHeader(); + h.setCenter("\u20ac"); + h.setLeft("\u20ac"); + h.setRight("\u20ac"); + + //Set a unicode footer + Footer f = s.getFooter(); + f.setCenter("\u20ac"); + f.setLeft("\u20ac"); + f.setRight("\u20ac"); + + Row r = s.createRow(0); + Cell c = r.createCell(1); + c.setCellValue(12.34); + c.getCellStyle().setDataFormat(fmt); + + Cell c2 = r.createCell(2); + c.setCellValue(factory.createRichTextString("\u20ac")); + + Cell c3 = r.createCell(3); + String formulaString = "TEXT(12.34,\"\u20ac###,##\")"; + c3.setCellFormula(formulaString); + + wb = getTestDataProvider().writeOutAndReadBack(wb); + + //Test the sheetname + s = wb.getSheet("\u20ac"); + assertNotNull(s); + + //Test the header + h = s.getHeader(); + assertEquals(h.getCenter(), "\u20ac"); + assertEquals(h.getLeft(), "\u20ac"); + assertEquals(h.getRight(), "\u20ac"); + + //Test the footer + f = s.getFooter(); + assertEquals(f.getCenter(), "\u20ac"); + assertEquals(f.getLeft(), "\u20ac"); + assertEquals(f.getRight(), "\u20ac"); + + //Test the dataformat + r = s.getRow(0); + c = r.getCell(1); + df = wb.createDataFormat(); + assertEquals(formatStr, df.getFormat(c.getCellStyle().getDataFormat())); + + //Test the cell string value + c2 = r.getCell(2); + assertEquals(c.getRichStringCellValue().getString(), "\u20ac"); + + //Test the cell formula + c3 = r.getCell(3); + assertEquals(c3.getCellFormula(), formulaString); + } + }