Bug 52233: Do not make the XSSFSheet invalid during write(), I could not find out why the ColsArray was set to null in the write() method. By removing this the writing of the sheet does not invalidate the Workbook any more and makes the test case work fine.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1513916 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2013-08-14 14:57:44 +00:00
parent 2834048155
commit 17f3b2cb26
2 changed files with 92 additions and 58 deletions

View File

@ -170,12 +170,12 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
}
@SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support
private void initRows(CTWorksheet worksheet) {
private void initRows(CTWorksheet worksheetParam) {
_rows = new TreeMap<Integer, XSSFRow>();
tables = new TreeMap<String, XSSFTable>();
sharedFormulas = new HashMap<Integer, CTCellFormula>();
arrayFormulas = new ArrayList<CellRangeAddress>();
for (CTRow row : worksheet.getSheetData().getRowArray()) {
for (CTRow row : worksheetParam.getSheetData().getRowArray()) {
XSSFRow r = new XSSFRow(row, this);
_rows.put(r.getRowNum(), r);
}
@ -2412,6 +2412,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
*
* @deprecated Use the version of showInPane() with ints as there can be more than 32767 rows.
*/
@Deprecated
public void showInPane(short toprow, short leftcol) {
showInPane((int)toprow, (int)leftcol);
}
@ -2691,11 +2692,11 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
if(worksheet.sizeOfColsArray() == 1) {
CTCols col = worksheet.getColsArray(0);
if(col.sizeOfColArray() == 0) {
worksheet.setColsArray(null);
} else {
if(col.sizeOfColArray() != 0) {
setColWidthAttribute(col);
}
} /*else {
remove, see Bug 52233: worksheet.setColsArray(null);
}*/
}
// Now re-generate our CTHyperlinks, if needed

View File

@ -17,6 +17,7 @@
package org.apache.poi.xssf.usermodel;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
@ -25,9 +26,18 @@ import java.util.zip.CRC32;
import org.apache.poi.POIXMLProperties;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.openxml4j.opc.*;
import org.apache.poi.openxml4j.opc.ContentTypes;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.BaseTestWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.util.TempFile;
import org.apache.poi.xssf.XSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples;
@ -111,6 +121,8 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
sheet1 = workbook.getSheetAt(0);
assertEquals(1.2, sheet1.getRow(0).getCell(0).getNumericCellValue(), 0.0001);
assertEquals("hello world", sheet1.getRow(1).getCell(0).getRichStringCellValue().getString());
pkg.close();
}
public void testExisting() throws Exception {
@ -128,6 +140,7 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
assertTrue(wbPart.hasRelationships());
assertEquals(6, wbPart.getRelationships().size());
pkg.close();
}
public void testGetCellStyleAt(){
@ -448,4 +461,24 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
assertEquals(IndexedColors.RED.index,
sh.getCTWorksheet().getSheetPr().getTabColor().getIndexed());
}
public void testColumnWidthPOI52233() throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("hello world");
assertEquals("hello world", workbook.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
assertEquals(2048, workbook.getSheetAt(0).getColumnWidth(0)); // <-works
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
workbook.write(stream);
} finally {
stream.close();
}
assertEquals("hello world", workbook.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
assertEquals(2048, workbook.getSheetAt(0).getColumnWidth(0)); // <- did throw IndexOutOfBoundsException before fixing the bug
}
}