Applied patch 45898 - fixed XSSFCellFill getFillBackgroundColor and getFillForegroundColor to not throw NPE if fill has not been set. Some other code clean up.

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@699984 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2008-09-29 04:28:55 +00:00
parent 84e3bbd179
commit 72d626f2b2
5 changed files with 1052 additions and 1013 deletions

View File

@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import org.apache.poi.ss.usermodel.CellStyle;
@ -54,21 +55,20 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
/**
* Table of styles shared across all sheets in a workbook.
*
* @version $Id: SharedStringsTable.java 612495 2008-01-16 16:08:22Z ugo $
* @author ugo
*/
public class StylesTable implements StylesSource, XSSFModel {
private final Hashtable<Long,String> numberFormats = new Hashtable<Long,String>();
private final ArrayList<CTFont> fonts = new ArrayList<CTFont>();
private final LinkedList<CTFill> fills = new LinkedList<CTFill>();
private final LinkedList<CTBorder> borders = new LinkedList<CTBorder>();
private final LinkedList<CTXf> styleXfs = new LinkedList<CTXf>();
private final LinkedList<CTXf> xfs = new LinkedList<CTXf>();
private final List<CTFont> fonts = new ArrayList<CTFont>();
private final List<CTFill> fills = new LinkedList<CTFill>();
private final List<CTBorder> borders = new LinkedList<CTBorder>();
private final List<CTXf> styleXfs = new LinkedList<CTXf>();
private final List<CTXf> xfs = new LinkedList<CTXf>();
private final LinkedList<CTDxf> dxfs = new LinkedList<CTDxf>();
private final List<CTDxf> dxfs = new LinkedList<CTDxf>();
/**
* The first style id available for use as a custom style
@ -209,7 +209,7 @@ public class StylesTable implements StylesSource, XSSFModel {
return new XSSFCellFill(fills.get((int) idx));
}
public long putFill(XSSFCellFill fill) {
return putFill(fill, fills);
return fill.putFill(fills);
}
public CTXf getCellXfAt(long idx) {
@ -365,20 +365,14 @@ public class StylesTable implements StylesSource, XSSFModel {
doc.save(out, options);
}
private long putBorder(XSSFCellBorder border, LinkedList<CTBorder> borders) {
return border.putBorder(borders);
private long putBorder(XSSFCellBorder border, List<CTBorder> borders) {
return border.putBorder((LinkedList<CTBorder>) borders); // TODO - use List instead of LinkedList
}
private long putFill(XSSFCellFill fill, LinkedList<CTFill> fills) {
return fill.putFill(fills);
private long putFont(XSSFFont font, List<CTFont> fonts) {
return font.putFont((ArrayList<CTFont>) fonts); // TODO - use List instead of ArrayList
}
private long putFont(XSSFFont font, ArrayList<CTFont> fonts) {
return font.putFont(fonts);
}
private void initialize() {
//CTFont ctFont = createDefaultFont();
XSSFFont xssfFont = createDefaultFont();
@ -435,7 +429,6 @@ public class StylesTable implements StylesSource, XSSFModel {
return xssfFont;
}
public CTDxf getDxf(long idx) {
if(dxfs.size()==0)
return CTDxf.Factory.newInstance();
@ -443,12 +436,8 @@ public class StylesTable implements StylesSource, XSSFModel {
return dxfs.get((int) idx);
}
public long putDxf(CTDxf dxf) {
this.dxfs.add(dxf);
return this.dxfs.size();
}
}

View File

@ -77,12 +77,14 @@ public enum IndexedColors {
BROWN(60),
PLUM(61),
INDIGO(62),
GREY_80_PERCENT(63);
GREY_80_PERCENT(63),
AUTOMATIC(64),
;
private short index;
private int index;
IndexedColors(int idx){
index = (short)idx;
index = idx;
}
/**
@ -91,6 +93,6 @@ public enum IndexedColors {
* @return index of this color
*/
public short getIndex(){
return index;
return (short)index;
}
}

View File

@ -16,80 +16,94 @@
==================================================================== */
package org.apache.poi.xssf.usermodel.extensions;
import java.util.LinkedList;
import java.util.List;
import org.apache.poi.xssf.usermodel.IndexedColors;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType.Enum;
public class XSSFCellFill {
public final class XSSFCellFill {
private CTFill fill;
private CTFill _fill;
public XSSFCellFill(CTFill fill) {
this.fill = fill;
_fill = fill;
}
public XSSFCellFill() {
this.fill = CTFill.Factory.newInstance();
_fill = CTFill.Factory.newInstance();
}
public XSSFColor getFillBackgroundColor() {
return new XSSFColor(getPatternFill().getBgColor());
CTColor ctColor = getPatternFill().getBgColor();
if (ctColor == null) {
XSSFColor result = new XSSFColor();
result.setIndexed(IndexedColors.AUTOMATIC.getIndex());
return result;
}
return new XSSFColor(ctColor);
}
public XSSFColor getFillForegroundColor() {
return new XSSFColor(getPatternFill().getFgColor());
CTColor ctColor = getPatternFill().getFgColor();
if (ctColor == null) {
XSSFColor result = new XSSFColor();
result.setIndexed(IndexedColors.AUTOMATIC.getIndex());
return result;
}
return new XSSFColor(ctColor);
}
public Enum getPatternType() {
return getPatternFill().getPatternType();
}
public long putFill(LinkedList<CTFill> fills) {
if (fills.contains(fill)) {
return fills.indexOf(fill);
/**
* @return the index of the just added fill
*/
public int putFill(List<CTFill> fills) {
if (fills.contains(_fill)) {
return fills.indexOf(_fill);
}
fills.add(fill);
fills.add(_fill);
return fills.size() - 1;
}
private CTPatternFill getPatternFill() {
CTPatternFill patternFill = fill.getPatternFill();
CTPatternFill patternFill = _fill.getPatternFill();
if (patternFill == null) {
patternFill = fill.addNewPatternFill();
patternFill = _fill.addNewPatternFill();
}
return patternFill;
}
public CTFill getCTFill() {
return this.fill;
return _fill;
}
public void setFillBackgroundColor(long index) {
CTColor ctColor=getPatternFill().addNewBgColor();
ctColor.setIndexed(index);
fill.getPatternFill().setBgColor(ctColor);
_fill.getPatternFill().setBgColor(ctColor);
}
public void setFillForegroundColor(long index) {
CTColor ctColor=getPatternFill().addNewFgColor();
ctColor.setIndexed(index);
fill.getPatternFill().setFgColor(ctColor);
_fill.getPatternFill().setFgColor(ctColor);
}
public void setFillBackgroundRgbColor(XSSFColor color) {
fill.getPatternFill().setBgColor(color.getCTColor());
_fill.getPatternFill().setBgColor(color.getCTColor());
}
public void setFillForegroundRgbColor(XSSFColor color) {
fill.getPatternFill().setFgColor(color.getCTColor());
_fill.getPatternFill().setFgColor(color.getCTColor());
}
public void setPatternType(Enum patternType) {
getPatternFill().setPatternType(patternType);
}
}

View File

@ -17,6 +17,7 @@
package org.apache.poi.xssf.usermodel;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.poi.ss.usermodel.CellStyle;
@ -30,6 +31,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
public class TestXSSFCellStyle extends TestCase {
private static final int AUTO_COLOR_INDEX = 64;
private StylesTable stylesTable;
private CTBorder ctBorderA;
private CTFill ctFill;
@ -40,7 +42,8 @@ public class TestXSSFCellStyle extends TestCase {
private XSSFCellStyle cellStyle;
private CTStylesheet ctStylesheet;
public void setUp() {
@Override
protected void setUp() {
stylesTable = new StylesTable();
ctStylesheet = stylesTable._getRawStylesheet();
@ -76,7 +79,7 @@ public class TestXSSFCellStyle extends TestCase {
cellXf.setFillId(1);
cellXf.setFontId(1);
stylesTable.putCellStyleXf(cellStyleXf);
long id=stylesTable.putCellXf(cellXf);
stylesTable.putCellXf(cellXf);
cellStyle = new XSSFCellStyle(1, 1, stylesTable);
}
@ -139,7 +142,6 @@ public class TestXSSFCellStyle extends TestCase {
public void testGetSetBottomBorderColor() {
CTColor ctColor = ctBorderA.addNewBottom().addNewColor();
ctColor.setIndexed(2);
XSSFColor color = new XSSFColor(ctColor);
assertEquals((short)2, cellStyle.getBottomBorderColor());
CTColor anotherCtColor = CTColor.Factory.newInstance();
anotherCtColor.setIndexed(4);
@ -148,13 +150,12 @@ public class TestXSSFCellStyle extends TestCase {
XSSFColor anotherColor = new XSSFColor(anotherCtColor);
cellStyle.setBorderColor(BorderSide.BOTTOM, anotherColor);
assertEquals((short)4, cellStyle.getBottomBorderColor());
assertEquals(new String("1234".getBytes()), new String(cellStyle.getBorderColor(BorderSide.BOTTOM).getRgb()));
assertEquals("1234", new String(cellStyle.getBorderColor(BorderSide.BOTTOM).getRgb()));
}
public void testGetSetTopBorderColor() {
CTColor ctColor = ctBorderA.addNewTop().addNewColor();
ctColor.setIndexed(5);
XSSFColor color = new XSSFColor(ctColor);
assertEquals((short)5, cellStyle.getTopBorderColor());
CTColor anotherCtColor = CTColor.Factory.newInstance();
anotherCtColor.setIndexed(7);
@ -163,13 +164,12 @@ public class TestXSSFCellStyle extends TestCase {
XSSFColor anotherColor = new XSSFColor(anotherCtColor);
cellStyle.setBorderColor(BorderSide.TOP, anotherColor);
assertEquals((short)7, cellStyle.getTopBorderColor());
assertEquals(new String("abcd".getBytes()), new String(cellStyle.getBorderColor(BorderSide.TOP).getRgb()));
assertEquals("abcd", new String(cellStyle.getBorderColor(BorderSide.TOP).getRgb()));
}
public void testGetSetLeftBorderColor() {
CTColor ctColor = ctBorderA.addNewLeft().addNewColor();
ctColor.setIndexed(2);
XSSFColor color = new XSSFColor(ctColor);
assertEquals((short)2, cellStyle.getLeftBorderColor());
CTColor anotherCtColor = CTColor.Factory.newInstance();
anotherCtColor.setIndexed(4);
@ -178,13 +178,12 @@ public class TestXSSFCellStyle extends TestCase {
XSSFColor anotherColor = new XSSFColor(anotherCtColor);
cellStyle.setBorderColor(BorderSide.LEFT, anotherColor);
assertEquals((short)4, cellStyle.getLeftBorderColor());
assertEquals(new String("1234".getBytes()), new String(cellStyle.getBorderColor(BorderSide.LEFT).getRgb()));
assertEquals("1234", new String(cellStyle.getBorderColor(BorderSide.LEFT).getRgb()));
}
public void testGetSetRightBorderColor() {
CTColor ctColor = ctBorderA.addNewRight().addNewColor();
ctColor.setIndexed(8);
XSSFColor color = new XSSFColor(ctColor);
assertEquals((short)8, cellStyle.getRightBorderColor());
CTColor anotherCtColor = CTColor.Factory.newInstance();
anotherCtColor.setIndexed(14);
@ -193,11 +192,11 @@ public class TestXSSFCellStyle extends TestCase {
XSSFColor anotherColor = new XSSFColor(anotherCtColor);
cellStyle.setBorderColor(BorderSide.RIGHT, anotherColor);
assertEquals((short)14, cellStyle.getRightBorderColor());
assertEquals(new String("af67".getBytes()), new String(cellStyle.getBorderColor(BorderSide.RIGHT).getRgb()));
assertEquals("af67", new String(cellStyle.getBorderColor(BorderSide.RIGHT).getRgb()));
}
public void testGetFillBackgroundColor() {
setUp();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
CTColor ctBgColor = ctPatternFill.addNewBgColor();
ctBgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex());
@ -205,7 +204,7 @@ public class TestXSSFCellStyle extends TestCase {
XSSFCellFill cellFill=new XSSFCellFill(ctFill);
long index=stylesTable.putFill(cellFill);
((XSSFCellStyle)cellStyle).getCoreXf().setFillId(index);
cellStyle.getCoreXf().setFillId(index);
assertEquals(2,cellStyle.getCoreXf().getFillId());
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), cellStyle.getFillBackgroundColor());
@ -226,8 +225,26 @@ public class TestXSSFCellStyle extends TestCase {
assertEquals(ctColor.getRgb()[3], ctPatternFill.getBgColor().getRgb()[3]);
}
public void testGetFillBackgroundColor_default() {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFCellStyle style = wb.createCellStyle();
short color;
try {
color = style.getFillBackgroundColor();
} catch (NullPointerException e) {
throw new AssertionFailedError("Identified bug 45898");
}
assertEquals(AUTO_COLOR_INDEX, color);
XSSFColor xcolor=style.getFillBackgroundRgbColor();
assertEquals(xcolor.getIndexed(), AUTO_COLOR_INDEX);
}
public void testGetFillForegroundColor() {
setUp();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
CTColor ctFgColor = ctPatternFill.addNewFgColor();
ctFgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex());
@ -235,7 +252,7 @@ public class TestXSSFCellStyle extends TestCase {
XSSFCellFill cellFill=new XSSFCellFill(ctFill);
long index=stylesTable.putFill(cellFill);
((XSSFCellStyle)cellStyle).getCoreXf().setFillId(index);
cellStyle.getCoreXf().setFillId(index);
assertEquals(2,cellStyle.getCoreXf().getFillId());
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), cellStyle.getFillForegroundColor());
@ -256,13 +273,31 @@ public class TestXSSFCellStyle extends TestCase {
assertEquals(ctColor.getRgb()[3], ctPatternFill.getFgColor().getRgb()[3]);
}
public void testGetFillForegroundColor_default() {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFCellStyle style = wb.createCellStyle();
short color;
try {
color = style.getFillForegroundColor();
} catch (NullPointerException e) {
throw new AssertionFailedError("Identified bug 45898");
}
assertEquals(AUTO_COLOR_INDEX, color);
XSSFColor xcolor=style.getFillForegroundRgbColor();
assertEquals(xcolor.getIndexed(), AUTO_COLOR_INDEX);
}
public void testGetFillPattern() {
setUp();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
ctPatternFill.setPatternType(STPatternType.DARK_DOWN);
XSSFCellFill cellFill=new XSSFCellFill(ctFill);
long index=stylesTable.putFill(cellFill);
((XSSFCellStyle)cellStyle).getCoreXf().setFillId(index);
cellStyle.getCoreXf().setFillId(index);
assertEquals(CellStyle.THICK_FORWARD_DIAG, cellStyle.getFillPattern());

View File

@ -17,20 +17,21 @@
package org.apache.poi.xssf.usermodel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.StylesSource;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.model.StylesTable;
import org.openxml4j.opc.ContentTypes;
import org.openxml4j.opc.Package;
@ -39,10 +40,10 @@ import org.openxml4j.opc.PackagingURIHelper;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
public class TestXSSFWorkbook extends TestCase {
public TestXSSFWorkbook(String name) {
super(name);
public final class TestXSSFWorkbook extends TestCase {
@Override
protected void setUp() throws Exception {
// Use system out logger
System.setProperty(
"org.apache.poi.util.POILogger",
@ -61,7 +62,7 @@ public class TestXSSFWorkbook extends TestCase {
assertEquals(-1, workbook.getSheetIndex("noSheet"));
}
public void testSetSheetOrder() throws Exception {
public void testSetSheetOrder() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet1 = workbook.createSheet("sheet1");
Sheet sheet2 = workbook.createSheet("sheet2");
@ -84,10 +85,10 @@ public class TestXSSFWorkbook extends TestCase {
assertSame(sheet1, workbook.getSheetAt(0));
}
public void testSetSelectedTab() throws Exception {
public void testSetSelectedTab() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet1 = workbook.createSheet("sheet1");
Sheet sheet2 = workbook.createSheet("sheet2");
workbook.createSheet("sheet1");
workbook.createSheet("sheet2");
assertEquals(0, workbook.getSelectedTab());
workbook.setSelectedTab((short) 0);
assertEquals(0, workbook.getSelectedTab());
@ -95,22 +96,22 @@ public class TestXSSFWorkbook extends TestCase {
assertEquals(1, workbook.getSelectedTab());
}
public void testSetSheetName() throws Exception {
public void testSetSheetName() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet1 = workbook.createSheet("sheet1");
workbook.createSheet("sheet1");
assertEquals("sheet1", workbook.getSheetName(0));
workbook.setSheetName(0, "sheet2");
assertEquals("sheet2", workbook.getSheetName(0));
}
public void testCloneSheet() throws Exception {
public void testCloneSheet() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet1 = workbook.createSheet("sheet");
Sheet sheet2 = workbook.cloneSheet(0);
workbook.createSheet("sheet");
workbook.cloneSheet(0);
assertEquals(2, workbook.getNumberOfSheets());
assertEquals("sheet(1)", workbook.getSheetName(1));
workbook.setSheetName(1, "clonedsheet");
Sheet sheet3 = workbook.cloneSheet(1);
workbook.cloneSheet(1);
assertEquals(3, workbook.getNumberOfSheets());
assertEquals("clonedsheet(1)", workbook.getSheetName(2));
}
@ -124,11 +125,11 @@ public class TestXSSFWorkbook extends TestCase {
assertNull(workbook.getSheet("nosheet"));
}
public void testRemoveSheetAt() throws Exception {
public void testRemoveSheetAt() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet1 = workbook.createSheet("sheet1");
Sheet sheet2 = workbook.createSheet("sheet2");
Sheet sheet3 = workbook.createSheet("sheet3");
workbook.createSheet("sheet1");
workbook.createSheet("sheet2");
workbook.createSheet("sheet3");
workbook.removeSheetAt(1);
assertEquals(2, workbook.getNumberOfSheets());
assertEquals("sheet3", workbook.getSheetName(1));
@ -142,11 +143,11 @@ public class TestXSSFWorkbook extends TestCase {
/**
* Tests that we can save a new document
*/
public void testSaveNew() throws Exception {
public void testSaveNew() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet1 = workbook.createSheet("sheet1");
Sheet sheet2 = workbook.createSheet("sheet2");
Sheet sheet3 = workbook.createSheet("sheet3");
workbook.createSheet("sheet1");
workbook.createSheet("sheet2");
workbook.createSheet("sheet3");
File file = File.createTempFile("poi-", ".xlsx");
System.out.println("Saving newly created file to " + file.getAbsolutePath());
OutputStream out = new FileOutputStream(file);
@ -161,7 +162,7 @@ public class TestXSSFWorkbook extends TestCase {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet1 = workbook.createSheet("sheet1");
Sheet sheet2 = workbook.createSheet("sheet2");
Sheet sheet3 = workbook.createSheet("sheet3");
workbook.createSheet("sheet3");
RichTextString rts = workbook.getCreationHelper().createRichTextString("hello world");
@ -218,18 +219,13 @@ public class TestXSSFWorkbook extends TestCase {
}
public void testExisting() throws Exception {
File xml = new File(
System.getProperty("HSSF.testdata.path") +
File.separator + "Formatting.xlsx"
);
assertTrue(xml.exists());
XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx");
assertNotNull(workbook.getSharedStringSource());
assertNotNull(workbook.getStylesSource());
// And check a few low level bits too
Package pkg = Package.open(xml.toString());
Package pkg = Package.open(HSSFTestDataSamples.openSampleFileStream("Formatting.xlsx"));
PackagePart wbPart =
pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
@ -337,25 +333,16 @@ public class TestXSSFWorkbook extends TestCase {
}
public void testLoadSave() throws Exception {
File xml = new File(
System.getProperty("HSSF.testdata.path") +
File.separator + "Formatting.xlsx"
);
assertTrue(xml.exists());
XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
public void testLoadSave() {
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx");
assertEquals(3, workbook.getNumberOfSheets());
assertEquals("dd/mm/yyyy", workbook.getSheetAt(0).getRow(1).getCell(0).getRichStringCellValue().getString());
assertNotNull(workbook.getSharedStringSource());
assertNotNull(workbook.getStylesSource());
// Write out, and check
File tmpFile = File.createTempFile("poi-tmp", ".xlsx");
workbook.write(new FileOutputStream(tmpFile));
// Load up again, check all still there
XSSFWorkbook wb2 = new XSSFWorkbook(tmpFile.toString());
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(workbook);
assertEquals(3, wb2.getNumberOfSheets());
assertNotNull(wb2.getSheetAt(0));
assertNotNull(wb2.getSheetAt(1));
@ -369,14 +356,8 @@ public class TestXSSFWorkbook extends TestCase {
assertNotNull(wb2.getStylesSource());
}
public void testStyles() throws Exception {
File xml = new File(
System.getProperty("HSSF.testdata.path") +
File.separator + "Formatting.xlsx"
);
assertTrue(xml.exists());
XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
public void testStyles() {
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx");
StylesSource ss = workbook.getStylesSource();
assertNotNull(ss);
@ -403,10 +384,7 @@ public class TestXSSFWorkbook extends TestCase {
// Save, load back in again, and check
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
workbook = new XSSFWorkbook(Package.open(bais));
workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
ss = workbook.getStylesSource();
assertNotNull(ss);
@ -419,7 +397,7 @@ public class TestXSSFWorkbook extends TestCase {
assertEquals(1, st._getBordersSize());
}
public void testNamedRanges() throws Exception {
public void testNamedRanges() {
// First up, a new file
XSSFWorkbook workbook = new XSSFWorkbook();
assertEquals(0, workbook.getNumberOfNames());
@ -434,10 +412,7 @@ public class TestXSSFWorkbook extends TestCase {
nameB.setComment("B3 Comment");
// Save and re-load
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
workbook = new XSSFWorkbook(Package.open(bais));
workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
assertEquals(2, workbook.getNumberOfNames());
assertEquals("A2", workbook.getNameAt(0).getReference());
@ -454,13 +429,7 @@ public class TestXSSFWorkbook extends TestCase {
// Now, an existing file with named ranges
File xml = new File(
System.getProperty("HSSF.testdata.path") +
File.separator + "WithVariousData.xlsx"
);
assertTrue(xml.exists());
workbook = new XSSFWorkbook(xml.toString());
workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx");
assertEquals(2, workbook.getNumberOfNames());
assertEquals("Sheet1!$A$2:$A$7", workbook.getNameAt(0).getReference());
@ -474,10 +443,7 @@ public class TestXSSFWorkbook extends TestCase {
// Tweak, save, and re-check
workbook.getNameAt(1).setNameName("BStringsFun");
baos = new ByteArrayOutputStream();
workbook.write(baos);
bais = new ByteArrayInputStream(baos.toByteArray());
workbook = new XSSFWorkbook(Package.open(bais));
workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
assertEquals(2, workbook.getNumberOfNames());
assertEquals("Sheet1!$A$2:$A$7", workbook.getNameAt(0).getReference());
@ -488,4 +454,37 @@ public class TestXSSFWorkbook extends TestCase {
assertEquals("BStringsFun", workbook.getNameAt(1).getNameName());
assertEquals("All the strings in B", workbook.getNameAt(1).getComment());
}
public void testDuplicateNames() {
XSSFWorkbook wb = new XSSFWorkbook();
wb.createSheet("Sheet1");
wb.createSheet();
wb.createSheet("name1");
try {
wb.createSheet("name1");
fail();
} catch (IllegalArgumentException e) {
assertEquals("The workbook already contains a sheet of this name", e.getMessage());
}
wb.createSheet();
try {
wb.setSheetName(3, "name1");
fail();
} catch (IllegalArgumentException e) {
assertEquals("The workbook already contains a sheet of this name", e.getMessage());
}
try {
wb.setSheetName(3, "Sheet1");
fail();
} catch (IllegalArgumentException e) {
assertEquals("The workbook already contains a sheet of this name", e.getMessage());
}
wb.setSheetName(3, "name2");
wb.setSheetName(3, "Sheet3");
}
}