preparation for #54916 - commit unrelated changes

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1722538 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2016-01-01 19:05:50 +00:00
parent 4803a0c355
commit 9072855208
5 changed files with 297 additions and 212 deletions

View File

@ -17,6 +17,12 @@
package org.apache.poi; package org.apache.poi;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
@ -25,8 +31,6 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import junit.framework.TestCase;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
@ -34,11 +38,12 @@ import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.PackageRelationshipTypes; import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
import org.apache.poi.util.PackageHelper; import org.apache.poi.util.PackageHelper;
import org.apache.poi.util.TempFile; import org.apache.poi.util.TempFile;
import org.junit.Test;
/** /**
* Test recursive read and write of OPC packages * Test recursive read and write of OPC packages
*/ */
public final class TestPOIXMLDocument extends TestCase { public final class TestPOIXMLDocument {
private static class OPCParser extends POIXMLDocument { private static class OPCParser extends POIXMLDocument {
@ -65,6 +70,7 @@ public final class TestPOIXMLDocument extends TestCase {
public TestFactory() { public TestFactory() {
// //
} }
@Override @Override
public POIXMLDocumentPart createDocumentPart(POIXMLDocumentPart parent, PackageRelationship rel, PackagePart part){ public POIXMLDocumentPart createDocumentPart(POIXMLDocumentPart parent, PackageRelationship rel, PackagePart part){
return new POIXMLDocumentPart(part, rel); return new POIXMLDocumentPart(part, rel);
@ -74,18 +80,20 @@ public final class TestPOIXMLDocument extends TestCase {
public POIXMLDocumentPart newDocumentPart(POIXMLRelation descriptor){ public POIXMLDocumentPart newDocumentPart(POIXMLRelation descriptor){
throw new RuntimeException("not supported"); throw new RuntimeException("not supported");
} }
} }
/** /**
* Recursively traverse a OOXML document and assert that same logical parts have the same physical instances * Recursively traverse a OOXML document and assert that same logical parts have the same physical instances
*/ */
private static void traverse(POIXMLDocumentPart part, HashMap<String,POIXMLDocumentPart> context) throws IOException{ private static void traverse(POIXMLDocumentPart part, HashMap<String,POIXMLDocumentPart> context) throws IOException{
context.put(part.getPackageRelationship().getTargetURI().toString(), part); assertEquals(part.getPackageRelationship().getTargetURI().toString(), part.getPackagePart().getPartName().getName());
context.put(part.getPackagePart().getPartName().getName(), part);
for(POIXMLDocumentPart p : part.getRelations()){ for(POIXMLDocumentPart p : part.getRelations()){
assertNotNull(p.toString()); assertNotNull(p.toString());
String uri = p.getPackageRelationship().getTargetURI().toString(); String uri = p.getPackagePart().getPartName().getURI().toString();
assertEquals(uri, p.getPackageRelationship().getTargetURI().toString());
if (!context.containsKey(uri)) { if (!context.containsKey(uri)) {
traverse(p, context); traverse(p, context);
} else { } else {
@ -108,7 +116,9 @@ public final class TestPOIXMLDocument extends TestCase {
FileOutputStream out = new FileOutputStream(tmp); FileOutputStream out = new FileOutputStream(tmp);
doc.write(out); doc.write(out);
out.close(); out.close();
doc.close();
@SuppressWarnings("resource")
OPCPackage pkg2 = OPCPackage.open(tmp.getAbsolutePath()); OPCPackage pkg2 = OPCPackage.open(tmp.getAbsolutePath());
try { try {
doc = new OPCParser(pkg1); doc = new OPCParser(pkg1);
@ -135,30 +145,33 @@ public final class TestPOIXMLDocument extends TestCase {
assertEquals(p1.getPartName(), p2.getPartName()); assertEquals(p1.getPartName(), p2.getPartName());
} }
} finally { } finally {
pkg2.close(); doc.close();
} }
} }
@Test
public void testPPTX() throws Exception { public void testPPTX() throws Exception {
assertReadWrite( POIDataSamples pds = POIDataSamples.getSlideShowInstance();
PackageHelper.open(POIDataSamples.getSlideShowInstance().openResourceAsStream("PPTWithAttachments.pptm")) assertReadWrite(PackageHelper.open(pds.openResourceAsStream("PPTWithAttachments.pptm")));
);
} }
@Test
public void testXLSX() throws Exception { public void testXLSX() throws Exception {
assertReadWrite( POIDataSamples pds = POIDataSamples.getSpreadSheetInstance();
PackageHelper.open(POIDataSamples.getSpreadSheetInstance().openResourceAsStream("ExcelWithAttachments.xlsm")) assertReadWrite(PackageHelper.open(pds.openResourceAsStream("ExcelWithAttachments.xlsm")));
);
} }
@Test
public void testDOCX() throws Exception { public void testDOCX() throws Exception {
assertReadWrite( POIDataSamples pds = POIDataSamples.getDocumentInstance();
PackageHelper.open(POIDataSamples.getDocumentInstance().openResourceAsStream("WordWithAttachments.docx")) assertReadWrite(PackageHelper.open(pds.openResourceAsStream("WordWithAttachments.docx")));
);
} }
@Test
public void testRelationOrder() throws Exception { public void testRelationOrder() throws Exception {
OPCPackage pkg = PackageHelper.open(POIDataSamples.getDocumentInstance().openResourceAsStream("WordWithAttachments.docx")); POIDataSamples pds = POIDataSamples.getDocumentInstance();
@SuppressWarnings("resource")
OPCPackage pkg = PackageHelper.open(pds.openResourceAsStream("WordWithAttachments.docx"));
OPCParser doc = new OPCParser(pkg); OPCParser doc = new OPCParser(pkg);
try { try {
doc.parse(new TestFactory()); doc.parse(new TestFactory());
@ -172,6 +185,7 @@ public final class TestPOIXMLDocument extends TestCase {
} }
} }
@Test
public void testCommitNullPart() throws IOException, InvalidFormatException { public void testCommitNullPart() throws IOException, InvalidFormatException {
POIXMLDocumentPart part = new POIXMLDocumentPart(); POIXMLDocumentPart part = new POIXMLDocumentPart();
part.prepareForCommit(); part.prepareForCommit();
@ -187,32 +201,40 @@ public final class TestPOIXMLDocument extends TestCase {
//part.getTargetPart(null); //part.getTargetPart(null);
} }
@Test
public void testVSDX() throws Exception { public void testVSDX() throws Exception {
OPCPackage open = PackageHelper.open(POIDataSamples.getDiagramInstance().openResourceAsStream("test.vsdx")); POIDataSamples pds = POIDataSamples.getDiagramInstance();
@SuppressWarnings("resource")
OPCPackage open = PackageHelper.open(pds.openResourceAsStream("test.vsdx"));
POIXMLDocument part = new OPCParser(open, PackageRelationshipTypes.VISIO_CORE_DOCUMENT); POIXMLDocument part = new OPCParser(open, PackageRelationshipTypes.VISIO_CORE_DOCUMENT);
assertNotNull(part); assertNotNull(part);
assertEquals(0, part.getRelationCounter()); assertEquals(0, part.getRelationCounter());
part.close();
} }
public void testVSDXPart() throws Exception { @Test
OPCPackage open = PackageHelper.open(POIDataSamples.getDiagramInstance().openResourceAsStream("test.vsdx")); public void testVSDXPart() throws IOException {
POIDataSamples pds = POIDataSamples.getDiagramInstance();
OPCPackage open = PackageHelper.open(pds.openResourceAsStream("test.vsdx"));
POIXMLDocumentPart part = new POIXMLDocumentPart(open, PackageRelationshipTypes.VISIO_CORE_DOCUMENT); POIXMLDocumentPart part = new POIXMLDocumentPart(open, PackageRelationshipTypes.VISIO_CORE_DOCUMENT);
assertNotNull(part); assertNotNull(part);
assertEquals(0, part.getRelationCounter()); assertEquals(0, part.getRelationCounter());
open.close();
} }
public void testInvalidCoreRel() throws Exception { @Test(expected=POIXMLException.class)
OPCPackage open = PackageHelper.open(POIDataSamples.getDiagramInstance().openResourceAsStream("test.vsdx")); public void testInvalidCoreRel() throws IOException {
POIDataSamples pds = POIDataSamples.getDiagramInstance();
OPCPackage open = PackageHelper.open(pds.openResourceAsStream("test.vsdx"));
try { try {
new POIXMLDocumentPart(open, "somethingillegal"); new POIXMLDocumentPart(open, "somethingillegal");
fail("Unknown core ref will throw exception"); } finally {
} catch (POIXMLException e) { open.close();
// expected here
} }
} }
} }

View File

@ -25,19 +25,25 @@ import static org.junit.Assert.fail;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints; import java.awt.RenderingHints;
import java.awt.geom.AffineTransform; import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.util.Collection;
import java.util.List; import java.util.List;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import org.apache.poi.POIDataSamples;
import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.sl.usermodel.PictureData.PictureType; import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xslf.usermodel.DrawingParagraph; import org.apache.poi.xslf.usermodel.DrawingParagraph;
import org.apache.poi.xslf.usermodel.DrawingTextBody; import org.apache.poi.xslf.usermodel.DrawingTextBody;
import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XMLSlideShow;
@ -64,11 +70,11 @@ public class TestXSLFBugs {
// Note - rId3 is a self reference // Note - rId3 is a self reference
XSLFSlide slide0 = ss1.getSlides().get(0); XSLFSlide slide0 = ss1.getSlides().get(0);
assertEquals("/ppt/slides/slide1.xml", slide0.getPackagePart().getPartName().toString()); assertRelation(slide0, "/ppt/slides/slide1.xml", null);
assertEquals("/ppt/slideLayouts/slideLayout12.xml", slide0.getRelationById("rId1").getPackageRelationship().getTargetURI().toString()); assertRelation(slide0, "/ppt/slideLayouts/slideLayout12.xml", "rId1");
assertEquals("/ppt/notesSlides/notesSlide1.xml", slide0.getRelationById("rId2").getPackageRelationship().getTargetURI().toString()); assertRelation(slide0, "/ppt/notesSlides/notesSlide1.xml", "rId2");
assertEquals("/ppt/slides/slide1.xml", slide0.getRelationById("rId3").getPackageRelationship().getTargetURI().toString()); assertRelation(slide0, "/ppt/slides/slide1.xml", "rId3");
assertEquals("/ppt/media/image1.png", slide0.getRelationById("rId4").getPackageRelationship().getTargetURI().toString()); assertRelation(slide0, "/ppt/media/image1.png", "rId4");
// Save and re-load // Save and re-load
XMLSlideShow ss2 = XSLFTestDataSamples.writeOutAndReadBack(ss1); XMLSlideShow ss2 = XSLFTestDataSamples.writeOutAndReadBack(ss1);
@ -76,16 +82,21 @@ public class TestXSLFBugs {
assertEquals(1, ss2.getSlides().size()); assertEquals(1, ss2.getSlides().size());
slide0 = ss2.getSlides().get(0); slide0 = ss2.getSlides().get(0);
assertEquals("/ppt/slides/slide1.xml", slide0.getPackagePart().getPartName().toString()); assertRelation(slide0, "/ppt/slides/slide1.xml", null);
assertEquals("/ppt/slideLayouts/slideLayout12.xml", slide0.getRelationById("rId1").getPackageRelationship().getTargetURI().toString()); assertRelation(slide0, "/ppt/slideLayouts/slideLayout12.xml", "rId1");
assertEquals("/ppt/notesSlides/notesSlide1.xml", slide0.getRelationById("rId2").getPackageRelationship().getTargetURI().toString()); assertRelation(slide0, "/ppt/notesSlides/notesSlide1.xml", "rId2");
// TODO Fix this // TODO Fix this
assertEquals("/ppt/slides/slide1.xml", slide0.getRelationById("rId3").getPackageRelationship().getTargetURI().toString()); assertRelation(slide0, "/ppt/slides/slide1.xml", "rId3");
assertEquals("/ppt/media/image1.png", slide0.getRelationById("rId4").getPackageRelationship().getTargetURI().toString()); assertRelation(slide0, "/ppt/media/image1.png", "rId4");
ss2.close(); ss2.close();
} }
private static void assertRelation(XSLFSlide slide, String exp, String rId) {
POIXMLDocumentPart pd = (rId != null) ? slide.getRelationById(rId) : slide;
assertEquals(exp, pd.getPackagePart().getPartName().getName());
}
/** /**
* Slide relations with anchors in them * Slide relations with anchors in them
*/ */
@ -149,17 +160,17 @@ public class TestXSLFBugs {
// Check the text, to see we got them in order // Check the text, to see we got them in order
slide = ss.getSlides().get(0); slide = ss.getSlides().get(0);
assertContains("POI cannot read this", getSlideText(slide)); assertContains(getSlideText(slide), "POI cannot read this");
slide = ss.getSlides().get(1); slide = ss.getSlides().get(1);
assertContains("POI can read this", getSlideText(slide)); assertContains(getSlideText(slide), "POI can read this");
assertContains("Has a relationship to another slide", getSlideText(slide)); assertContains(getSlideText(slide), "Has a relationship to another slide");
slide = ss.getSlides().get(2); slide = ss.getSlides().get(2);
assertContains("POI can read this", getSlideText(slide)); assertContains(getSlideText(slide), "POI can read this");
slide = ss.getSlides().get(3); slide = ss.getSlides().get(3);
assertContains("POI can read this", getSlideText(slide)); assertContains(getSlideText(slide), "POI can read this");
ss.close(); ss.close();
} }
@ -407,4 +418,19 @@ public class TestXSLFBugs {
ss.close(); ss.close();
} }
@Test
public void testLayout() throws IOException {
XMLSlideShow ppt = new XMLSlideShow();
InputStream picIn = POIDataSamples.getSlideShowInstance().openResourceAsStream("clock.jpg");
XSLFPictureData pd = ppt.addPicture(IOUtils.toByteArray(picIn), PictureType.JPEG);
picIn.close();
XSLFSlide slide = ppt.createSlide();
XSLFPictureShape ps = slide.createPicture(pd);
ps.setAnchor(new Rectangle(100, 100, 100, 100));
FileOutputStream fos = new FileOutputStream("picbla.pptx");
ppt.write(fos);
fos.close();
ppt.close();
}
} }

View File

@ -319,20 +319,15 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
for(Row r : s) { for(Row r : s) {
for(Cell c : r) { for(Cell c : r) {
if(c.getCellType() == Cell.CELL_TYPE_FORMULA) { if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
String formula = c.getCellFormula(); CellValue cv = eval.evaluate(c);
CellValue cv;
try {
cv = eval.evaluate(c);
} catch (Exception e) {
throw new RuntimeException("Can't evaluate formula: " + formula, e);
}
if(cv.getCellType() == Cell.CELL_TYPE_NUMERIC) { if(cv.getCellType() == Cell.CELL_TYPE_NUMERIC) {
// assert that the calculated value agrees with // assert that the calculated value agrees with
// the cached formula result calculated by Excel // the cached formula result calculated by Excel
String formula = c.getCellFormula();
double cachedFormulaResult = c.getNumericCellValue(); double cachedFormulaResult = c.getNumericCellValue();
double evaluatedFormulaResult = cv.getNumberValue(); double evaluatedFormulaResult = cv.getNumberValue();
assertEquals(c.getCellFormula(), cachedFormulaResult, evaluatedFormulaResult, 1E-7); assertEquals(formula, cachedFormulaResult, evaluatedFormulaResult, 1E-7);
} }
} }
} }
@ -2354,12 +2349,6 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
} }
} }
// OutputStream out = new FileOutputStream("/tmp/56467.xls");
// try {
// wb.write(out);
// } finally {
// out.close();
// }
} finally { } finally {
wb.close(); wb.close();
} }

View File

@ -17,28 +17,30 @@
package org.apache.poi.xssf.usermodel; package org.apache.poi.xssf.usermodel;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.awt.Color; import java.awt.Color;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import junit.framework.TestCase;
import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.FontUnderline; import org.apache.poi.ss.usermodel.FontUnderline;
import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.XSSFTestDataSamples;
import org.junit.Test;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties; import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph; import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType; import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing; import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing;
/** public class TestXSSFDrawing {
* @author Yegor Kozlov @Test
*/ public void testRead() throws IOException {
public class TestXSSFDrawing extends TestCase {
public void testRead() throws IOException{
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx");
XSSFSheet sheet = wb.getSheetAt(0); XSSFSheet sheet = wb.getSheetAt(0);
//the sheet has one relationship and it is XSSFDrawing //the sheet has one relationship and it is XSSFDrawing
@ -67,12 +69,14 @@ public class TestXSSFDrawing extends TestCase {
for(XSSFShape sh : shapes) assertNotNull(sh.getAnchor()); for(XSSFShape sh : shapes) assertNotNull(sh.getAnchor());
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); checkRewrite(wb);
wb.close();
} }
public void testNew() throws Exception { @Test
XSSFWorkbook wb = new XSSFWorkbook(); public void testNew() throws IOException {
XSSFSheet sheet = wb.createSheet(); XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet = wb1.createSheet();
//multiple calls of createDrawingPatriarch should return the same instance of XSSFDrawing //multiple calls of createDrawingPatriarch should return the same instance of XSSFDrawing
XSSFDrawing dr1 = sheet.createDrawingPatriarch(); XSSFDrawing dr1 = sheet.createDrawingPatriarch();
XSSFDrawing dr2 = sheet.createDrawingPatriarch(); XSSFDrawing dr2 = sheet.createDrawingPatriarch();
@ -102,8 +106,8 @@ public class TestXSSFDrawing extends TestCase {
XSSFTextBox c4 = drawing.createTextbox(new XSSFClientAnchor(0,0,0,0,4,4,5,6)); XSSFTextBox c4 = drawing.createTextbox(new XSSFClientAnchor(0,0,0,0,4,4,5,6));
XSSFRichTextString rt = new XSSFRichTextString("Test String"); XSSFRichTextString rt = new XSSFRichTextString("Test String");
rt.applyFont(0, 5, wb.createFont()); rt.applyFont(0, 5, wb1.createFont());
rt.applyFont(5, 6, wb.createFont()); rt.applyFont(5, 6, wb1.createFont());
c4.setText(rt); c4.setText(rt);
c4.setNoFill(true); c4.setNoFill(true);
@ -117,8 +121,9 @@ public class TestXSSFDrawing extends TestCase {
assertTrue(shapes.get(3) instanceof XSSFSimpleShape); // assertTrue(shapes.get(3) instanceof XSSFSimpleShape); //
// Save and re-load it // Save and re-load it
wb = XSSFTestDataSamples.writeOutAndReadBack(wb); XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
sheet = wb.getSheetAt(0); wb1.close();
sheet = wb2.getSheetAt(0);
// Check // Check
dr1 = sheet.createDrawingPatriarch(); dr1 = sheet.createDrawingPatriarch();
@ -141,9 +146,11 @@ public class TestXSSFDrawing extends TestCase {
assertTrue(xml.contains("xmlns:xdr=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\"")); assertTrue(xml.contains("xmlns:xdr=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\""));
assertTrue(xml.contains("xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\"")); assertTrue(xml.contains("xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\""));
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); checkRewrite(wb2);
wb2.close();
} }
@Test
public void testMultipleDrawings() throws IOException{ public void testMultipleDrawings() throws IOException{
XSSFWorkbook wb = new XSSFWorkbook(); XSSFWorkbook wb = new XSSFWorkbook();
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
@ -154,13 +161,14 @@ public class TestXSSFDrawing extends TestCase {
OPCPackage pkg = wb.getPackage(); OPCPackage pkg = wb.getPackage();
try { try {
assertEquals(3, pkg.getPartsByContentType(XSSFRelation.DRAWINGS.getContentType()).size()); assertEquals(3, pkg.getPartsByContentType(XSSFRelation.DRAWINGS.getContentType()).size());
checkRewrite(wb);
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
} finally { } finally {
pkg.close(); pkg.close();
} }
wb.close();
} }
@Test
public void testClone() throws Exception{ public void testClone() throws Exception{
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx");
XSSFSheet sheet1 = wb.getSheetAt(0); XSSFSheet sheet1 = wb.getSheetAt(0);
@ -192,7 +200,8 @@ public class TestXSSFDrawing extends TestCase {
assertEquals(sh1.getShapeProperties().toString(), sh2.getShapeProperties().toString()); assertEquals(sh1.getShapeProperties().toString(), sh2.getShapeProperties().toString());
} }
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); checkRewrite(wb);
wb.close();
} }
/** /**
@ -201,7 +210,8 @@ public class TestXSSFDrawing extends TestCase {
* *
* See Bugzilla 52219. * See Bugzilla 52219.
*/ */
public void testRichText(){ @Test
public void testRichText() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook(); XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(); XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFDrawing drawing = sheet.createDrawingPatriarch();
@ -229,15 +239,17 @@ public class TestXSSFDrawing extends TestCase {
new byte[]{0, (byte)128, (byte)128} , new byte[]{0, (byte)128, (byte)128} ,
rPr.getSolidFill().getSrgbClr().getVal()); rPr.getSolidFill().getSrgbClr().getVal());
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); checkRewrite(wb);
wb.close();
} }
/** /**
* test that anchor is not null when reading shapes from existing drawings * test that anchor is not null when reading shapes from existing drawings
*/ */
public void testReadAnchors(){ @Test
XSSFWorkbook wb = new XSSFWorkbook(); public void testReadAnchors() throws IOException {
XSSFSheet sheet = wb.createSheet(); XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet = wb1.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor1 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4); XSSFClientAnchor anchor1 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4);
@ -248,20 +260,22 @@ public class TestXSSFDrawing extends TestCase {
XSSFShape shape2 = drawing.createTextbox(anchor2); XSSFShape shape2 = drawing.createTextbox(anchor2);
assertNotNull(shape2); assertNotNull(shape2);
int pictureIndex= wb.addPicture(new byte[]{}, XSSFWorkbook.PICTURE_TYPE_PNG); int pictureIndex= wb1.addPicture(new byte[]{}, XSSFWorkbook.PICTURE_TYPE_PNG);
XSSFClientAnchor anchor3 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 6); XSSFClientAnchor anchor3 = new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 6);
XSSFShape shape3 = drawing.createPicture(anchor3, pictureIndex); XSSFShape shape3 = drawing.createPicture(anchor3, pictureIndex);
assertNotNull(shape3); assertNotNull(shape3);
wb = XSSFTestDataSamples.writeOutAndReadBack(wb); XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
sheet = wb.getSheetAt(0); wb1.close();
sheet = wb2.getSheetAt(0);
drawing = sheet.createDrawingPatriarch(); drawing = sheet.createDrawingPatriarch();
List<XSSFShape> shapes = drawing.getShapes(); List<XSSFShape> shapes = drawing.getShapes();
assertEquals(shapes.get(0).getAnchor(), anchor1); assertEquals(shapes.get(0).getAnchor(), anchor1);
assertEquals(shapes.get(1).getAnchor(), anchor2); assertEquals(shapes.get(1).getAnchor(), anchor2);
assertEquals(shapes.get(2).getAnchor(), anchor3); assertEquals(shapes.get(2).getAnchor(), anchor3);
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); checkRewrite(wb2);
wb2.close();
} }
/** /**
@ -270,7 +284,8 @@ public class TestXSSFDrawing extends TestCase {
* *
* See Bugzilla 54969. * See Bugzilla 54969.
*/ */
public void testRichTextFontAndColor() { @Test
public void testRichTextFontAndColor() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook(); XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(); XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFDrawing drawing = sheet.createDrawingPatriarch();
@ -293,14 +308,15 @@ public class TestXSSFDrawing extends TestCase {
assertArrayEquals( assertArrayEquals(
new byte[]{0, (byte)128, (byte)128} , new byte[]{0, (byte)128, (byte)128} ,
rPr.getSolidFill().getSrgbClr().getVal()); rPr.getSolidFill().getSrgbClr().getVal());
checkRewrite(wb);
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); wb.close();
} }
/** /**
* Test setText single paragraph to ensure backwards compatibility * Test setText single paragraph to ensure backwards compatibility
*/ */
public void testSetTextSingleParagraph() { @Test
public void testSetTextSingleParagraph() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook(); XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(); XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFDrawing drawing = sheet.createDrawingPatriarch();
@ -328,13 +344,15 @@ public class TestXSSFDrawing extends TestCase {
new int[] { 0, 255, 255 } , new int[] { 0, 255, 255 } ,
new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() }); new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); checkRewrite(wb);
wb.close();
} }
/** /**
* Test addNewTextParagraph * Test addNewTextParagraph
*/ */
public void testAddNewTextParagraph() { @Test
public void testAddNewTextParagraph() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook(); XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(); XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFDrawing drawing = sheet.createDrawingPatriarch();
@ -351,34 +369,37 @@ public class TestXSSFDrawing extends TestCase {
assertEquals(1, runs.size()); assertEquals(1, runs.size());
assertEquals("Line 1", runs.get(0).getText()); assertEquals("Line 1", runs.get(0).getText());
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); checkRewrite(wb);
wb.close();
} }
/** /**
* Test addNewTextParagraph using RichTextString * Test addNewTextParagraph using RichTextString
*/ */
public void testAddNewTextParagraphWithRTS() { @Test
XSSFWorkbook wb = new XSSFWorkbook(); public void testAddNewTextParagraphWithRTS() throws IOException {
XSSFSheet sheet = wb.createSheet(); XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet = wb1.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4)); XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));
XSSFRichTextString rt = new XSSFRichTextString("Test Rich Text String"); XSSFRichTextString rt = new XSSFRichTextString("Test Rich Text String");
XSSFFont font = wb.createFont(); XSSFFont font = wb1.createFont();
font.setColor(new XSSFColor(new Color(0, 255, 255))); font.setColor(new XSSFColor(new Color(0, 255, 255)));
font.setFontName("Arial"); font.setFontName("Arial");
rt.applyFont(font); rt.applyFont(font);
XSSFFont midfont = wb.createFont(); XSSFFont midfont = wb1.createFont();
midfont.setColor(new XSSFColor(new Color(0, 255, 0))); midfont.setColor(new XSSFColor(new Color(0, 255, 0)));
rt.applyFont(5, 14, midfont); // set the text "Rich Text" to be green and the default font rt.applyFont(5, 14, midfont); // set the text "Rich Text" to be green and the default font
XSSFTextParagraph para = shape.addNewTextParagraph(rt); XSSFTextParagraph para = shape.addNewTextParagraph(rt);
// Save and re-load it // Save and re-load it
wb = XSSFTestDataSamples.writeOutAndReadBack(wb); XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
sheet = wb.getSheetAt(0); wb1.close();
sheet = wb2.getSheetAt(0);
// Check // Check
drawing = sheet.createDrawingPatriarch(); drawing = sheet.createDrawingPatriarch();
@ -421,13 +442,15 @@ public class TestXSSFDrawing extends TestCase {
new int[] { 0, 255, 255 } , new int[] { 0, 255, 255 } ,
new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() }); new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); checkRewrite(wb2);
wb2.close();
} }
/** /**
* Test add multiple paragraphs and retrieve text * Test add multiple paragraphs and retrieve text
*/ */
public void testAddMultipleParagraphs() { @Test
public void testAddMultipleParagraphs() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook(); XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(); XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFDrawing drawing = sheet.createDrawingPatriarch();
@ -447,13 +470,15 @@ public class TestXSSFDrawing extends TestCase {
assertEquals(4, paras.size()); // this should be 4 as XSSFSimpleShape creates a default paragraph (no text), and then we added 3 paragraphs assertEquals(4, paras.size()); // this should be 4 as XSSFSimpleShape creates a default paragraph (no text), and then we added 3 paragraphs
assertEquals("Line 1\nLine 2\nLine 3", shape.getText()); assertEquals("Line 1\nLine 2\nLine 3", shape.getText());
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); checkRewrite(wb);
wb.close();
} }
/** /**
* Test setting the text, then adding multiple paragraphs and retrieve text * Test setting the text, then adding multiple paragraphs and retrieve text
*/ */
public void testSetAddMultipleParagraphs() { @Test
public void testSetAddMultipleParagraphs() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook(); XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(); XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFDrawing drawing = sheet.createDrawingPatriarch();
@ -472,13 +497,15 @@ public class TestXSSFDrawing extends TestCase {
assertEquals(3, paras.size()); // this should be 3 as we overwrote the default paragraph with setText, then added 2 new paragraphs assertEquals(3, paras.size()); // this should be 3 as we overwrote the default paragraph with setText, then added 2 new paragraphs
assertEquals("Line 1\nLine 2\nLine 3", shape.getText()); assertEquals("Line 1\nLine 2\nLine 3", shape.getText());
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); checkRewrite(wb);
wb.close();
} }
/** /**
* Test reading text from a textbox in an existing file * Test reading text from a textbox in an existing file
*/ */
public void testReadTextBox(){ @Test
public void testReadTextBox() throws IOException {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx");
XSSFSheet sheet = wb.getSheetAt(0); XSSFSheet sheet = wb.getSheetAt(0);
//the sheet has one relationship and it is XSSFDrawing //the sheet has one relationship and it is XSSFDrawing
@ -503,14 +530,16 @@ public class TestXSSFDrawing extends TestCase {
XSSFSimpleShape textbox = (XSSFSimpleShape) shapes.get(4); XSSFSimpleShape textbox = (XSSFSimpleShape) shapes.get(4);
assertEquals("Sheet with various pictures\n(jpeg, png, wmf, emf and pict)", textbox.getText()); assertEquals("Sheet with various pictures\n(jpeg, png, wmf, emf and pict)", textbox.getText());
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); checkRewrite(wb);
wb.close();
} }
/** /**
* Test reading multiple paragraphs from a textbox in an existing file * Test reading multiple paragraphs from a textbox in an existing file
*/ */
public void testReadTextBoxParagraphs(){ @Test
public void testReadTextBoxParagraphs() throws IOException {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithTextBox.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithTextBox.xlsx");
XSSFSheet sheet = wb.getSheetAt(0); XSSFSheet sheet = wb.getSheetAt(0);
//the sheet has one relationship and it is XSSFDrawing //the sheet has one relationship and it is XSSFDrawing
@ -563,16 +592,18 @@ public class TestXSSFDrawing extends TestCase {
new int[] { 0, 0, 255 } , new int[] { 0, 0, 255 } ,
new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() }); new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); checkRewrite(wb);
wb.close();
} }
/** /**
* Test adding and reading back paragraphs as bullet points * Test adding and reading back paragraphs as bullet points
*/ */
public void testAddBulletParagraphs() { @Test
public void testAddBulletParagraphs() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook(); XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(); XSSFSheet sheet = wb1.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 10, 20)); XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 10, 20));
@ -623,8 +654,9 @@ public class TestXSSFDrawing extends TestCase {
para.setBullet(ListAutoNumber.ARABIC_PERIOD); para.setBullet(ListAutoNumber.ARABIC_PERIOD);
// Save and re-load it // Save and re-load it
wb = XSSFTestDataSamples.writeOutAndReadBack(wb); XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
sheet = wb.getSheetAt(0); wb1.close();
sheet = wb2.getSheetAt(0);
// Check // Check
drawing = sheet.createDrawingPatriarch(); drawing = sheet.createDrawingPatriarch();
@ -672,13 +704,15 @@ public class TestXSSFDrawing extends TestCase {
assertEquals(builder.toString(), sshape.getText()); assertEquals(builder.toString(), sshape.getText());
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); checkRewrite(wb2);
wb2.close();
} }
/** /**
* Test reading bullet numbering from a textbox in an existing file * Test reading bullet numbering from a textbox in an existing file
*/ */
public void testReadTextBox2(){ @Test
public void testReadTextBox2() throws IOException {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithTextBox2.xlsx"); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithTextBox2.xlsx");
XSSFSheet sheet = wb.getSheetAt(0); XSSFSheet sheet = wb.getSheetAt(0);
XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFDrawing drawing = sheet.createDrawingPatriarch();
@ -697,33 +731,32 @@ public class TestXSSFDrawing extends TestCase {
sb.append("\t\n\t\n\t\n\t"); sb.append("\t\n\t\n\t\n\t");
assertEquals(sb.toString(), extracted); assertEquals(sb.toString(), extracted);
checkRewrite(wb);
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); wb.close();
} }
public void testXSSFSimpleShapeCausesNPE56514() throws Exception { @Test
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56514.xlsx"); public void testXSSFSimpleShapeCausesNPE56514() throws IOException {
XSSFSheet sheet = wb.getSheetAt(0); XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("56514.xlsx");
XSSFSheet sheet = wb1.getSheetAt(0);
XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFDrawing drawing = sheet.createDrawingPatriarch();
List<XSSFShape> shapes = drawing.getShapes(); List<XSSFShape> shapes = drawing.getShapes();
assertEquals(4, shapes.size()); assertEquals(4, shapes.size());
wb = XSSFTestDataSamples.writeOutAndReadBack(wb); XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sheet = wb2.getSheetAt(0);
drawing = sheet.createDrawingPatriarch();
shapes = drawing.getShapes(); shapes = drawing.getShapes();
assertEquals(4, shapes.size()); assertEquals(4, shapes.size());
wb2.close();
/* OutputStream stream = new FileOutputStream(new File("C:\\temp\\56514.xlsx"));
try {
wb.write(stream);
} finally {
stream.close();
}*/
} }
public void testBug56835CellComment() throws Exception { @Test(expected=IllegalArgumentException.class)
public void testBug56835CellComment() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook(); XSSFWorkbook wb = new XSSFWorkbook();
try {
XSSFSheet sheet = wb.createSheet(); XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFDrawing drawing = sheet.createDrawingPatriarch();
@ -732,14 +765,17 @@ public class TestXSSFDrawing extends TestCase {
XSSFComment comment = drawing.createCellComment(anchor); XSSFComment comment = drawing.createCellComment(anchor);
assertNotNull(comment); assertNotNull(comment);
// Should fail if we try to add the same comment for the same cell
try { try {
drawing.createCellComment(anchor); drawing.createCellComment(anchor);
fail("Should fail if we try to add the same comment for the same cell");
} catch (IllegalArgumentException e) {
// expected
}
} finally { } finally {
wb.close(); wb.close();
} }
} }
private static void checkRewrite(XSSFWorkbook wb) throws IOException {
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb);
assertNotNull(wb2);
wb2.close();
}
} }

View File

@ -77,7 +77,6 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCalcMode;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPane; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPane;
@SuppressWarnings("resource")
public final class TestXSSFSheet extends BaseTestSheet { public final class TestXSSFSheet extends BaseTestSheet {
public TestXSSFSheet() { public TestXSSFSheet() {
@ -437,8 +436,8 @@ public final class TestXSSFSheet extends BaseTestSheet {
*/ */
@Test @Test
public void setColumnGroupCollapsed() throws IOException { public void setColumnGroupCollapsed() throws IOException {
Workbook wb = new XSSFWorkbook(); XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet1 =(XSSFSheet) wb.createSheet(); XSSFSheet sheet1 = wb1.createSheet();
CTCols cols=sheet1.getCTWorksheet().getColsArray(0); CTCols cols=sheet1.getCTWorksheet().getColsArray(0);
assertEquals(0,cols.sizeOfColArray()); assertEquals(0,cols.sizeOfColArray());
@ -630,8 +629,9 @@ public final class TestXSSFSheet extends BaseTestSheet {
// write out and give back // write out and give back
// Save and re-load // Save and re-load
wb = XSSFTestDataSamples.writeOutAndReadBack(wb); XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
sheet1 = (XSSFSheet)wb.getSheetAt(0); wb1.close();
sheet1 = wb2.getSheetAt(0);
assertEquals(6,cols.sizeOfColArray()); assertEquals(6,cols.sizeOfColArray());
assertEquals(false,cols.getColArray(0).isSetHidden()); assertEquals(false,cols.getColArray(0).isSetHidden());
@ -659,7 +659,7 @@ public final class TestXSSFSheet extends BaseTestSheet {
assertEquals(14, cols.getColArray(5).getMin()); // 1 based assertEquals(14, cols.getColArray(5).getMin()); // 1 based
assertEquals(14, cols.getColArray(5).getMax()); // 1 based assertEquals(14, cols.getColArray(5).getMax()); // 1 based
wb.close(); wb2.close();
} }
/** /**
@ -670,8 +670,8 @@ public final class TestXSSFSheet extends BaseTestSheet {
*/ */
@Test @Test
public void setRowGroupCollapsed() throws IOException { public void setRowGroupCollapsed() throws IOException {
Workbook wb = new XSSFWorkbook(); XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet1 = (XSSFSheet)wb.createSheet(); XSSFSheet sheet1 = wb1.createSheet();
sheet1.groupRow( 5, 14 ); sheet1.groupRow( 5, 14 );
sheet1.groupRow( 7, 14 ); sheet1.groupRow( 7, 14 );
@ -725,8 +725,9 @@ public final class TestXSSFSheet extends BaseTestSheet {
// Save and re-load // Save and re-load
wb = XSSFTestDataSamples.writeOutAndReadBack(wb); XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
sheet1 = (XSSFSheet)wb.getSheetAt(0); wb1.close();
sheet1 = wb2.getSheetAt(0);
assertFalse(sheet1.getRow(6).getCTRow().isSetCollapsed()); assertFalse(sheet1.getRow(6).getCTRow().isSetCollapsed());
assertFalse(sheet1.getRow(6).getCTRow().isSetHidden()); assertFalse(sheet1.getRow(6).getCTRow().isSetHidden());
@ -741,7 +742,7 @@ public final class TestXSSFSheet extends BaseTestSheet {
assertFalse(sheet1.getRow(18).getCTRow().isSetCollapsed()); assertFalse(sheet1.getRow(18).getCTRow().isSetCollapsed());
assertFalse(sheet1.getRow(18).getCTRow().isSetHidden()); assertFalse(sheet1.getRow(18).getCTRow().isSetHidden());
wb.close(); wb2.close();
} }
/** /**
@ -797,8 +798,8 @@ public final class TestXSSFSheet extends BaseTestSheet {
*/ */
@Test @Test
public void bug47862() throws IOException { public void bug47862() throws IOException {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("47862.xlsx"); XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("47862.xlsx");
XSSFSheet sheet = wb.getSheetAt(0); XSSFSheet sheet = wb1.getSheetAt(0);
CTCols cols = sheet.getCTWorksheet().getColsArray(0); CTCols cols = sheet.getCTWorksheet().getColsArray(0);
//<cols> //<cols>
// <col min="1" max="5" width="15.77734375" customWidth="1"/> // <col min="1" max="5" width="15.77734375" customWidth="1"/>
@ -839,8 +840,9 @@ public final class TestXSSFSheet extends BaseTestSheet {
} }
//serialize and check again //serialize and check again
wb = XSSFTestDataSamples.writeOutAndReadBack(wb); XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
sheet = wb.getSheetAt(0); wb1.close();
sheet = wb2.getSheetAt(0);
cols = sheet.getCTWorksheet().getColsArray(0); cols = sheet.getCTWorksheet().getColsArray(0);
assertEquals(5, cols.sizeOfColArray()); assertEquals(5, cols.sizeOfColArray());
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
@ -848,7 +850,7 @@ public final class TestXSSFSheet extends BaseTestSheet {
assertEquals(cw[i], cols.getColArray(i).getWidth(), 0.0); assertEquals(cw[i], cols.getColArray(i).getWidth(), 0.0);
} }
wb.close(); wb2.close();
} }
/** /**
@ -856,8 +858,8 @@ public final class TestXSSFSheet extends BaseTestSheet {
*/ */
@Test @Test
public void bug47804() throws IOException { public void bug47804() throws IOException {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("47804.xlsx"); XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("47804.xlsx");
XSSFSheet sheet = wb.getSheetAt(0); XSSFSheet sheet = wb1.getSheetAt(0);
CTCols cols = sheet.getCTWorksheet().getColsArray(0); CTCols cols = sheet.getCTWorksheet().getColsArray(0);
assertEquals(2, cols.sizeOfColArray()); assertEquals(2, cols.sizeOfColArray());
CTCol col; CTCol col;
@ -912,8 +914,9 @@ public final class TestXSSFSheet extends BaseTestSheet {
assertEquals(7, col.getMax()); assertEquals(7, col.getMax());
//serialize and check again //serialize and check again
wb = XSSFTestDataSamples.writeOutAndReadBack(wb); XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
sheet = wb.getSheetAt(0); wb1.close();
sheet = wb2.getSheetAt(0);
assertTrue(sheet.isColumnHidden(2)); assertTrue(sheet.isColumnHidden(2));
assertTrue(sheet.isColumnHidden(6)); assertTrue(sheet.isColumnHidden(6));
assertFalse(sheet.isColumnHidden(1)); assertFalse(sheet.isColumnHidden(1));
@ -921,24 +924,24 @@ public final class TestXSSFSheet extends BaseTestSheet {
assertFalse(sheet.isColumnHidden(4)); assertFalse(sheet.isColumnHidden(4));
assertFalse(sheet.isColumnHidden(5)); assertFalse(sheet.isColumnHidden(5));
wb.close(); wb2.close();
} }
@Test @Test
public void commentsTable() throws IOException { public void commentsTable() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet1 = workbook.createSheet(); XSSFSheet sheet1 = wb1.createSheet();
CommentsTable comment1 = sheet1.getCommentsTable(false); CommentsTable comment1 = sheet1.getCommentsTable(false);
assertNull(comment1); assertNull(comment1);
comment1 = sheet1.getCommentsTable(true); comment1 = sheet1.getCommentsTable(true);
assertNotNull(comment1); assertNotNull(comment1);
assertEquals("/xl/comments1.xml", comment1.getPackageRelationship().getTargetURI().toString()); assertEquals("/xl/comments1.xml", comment1.getPackagePart().getPartName().getName());
assertSame(comment1, sheet1.getCommentsTable(true)); assertSame(comment1, sheet1.getCommentsTable(true));
//second sheet //second sheet
XSSFSheet sheet2 = workbook.createSheet(); XSSFSheet sheet2 = wb1.createSheet();
CommentsTable comment2 = sheet2.getCommentsTable(false); CommentsTable comment2 = sheet2.getCommentsTable(false);
assertNull(comment2); assertNull(comment2);
@ -946,20 +949,21 @@ public final class TestXSSFSheet extends BaseTestSheet {
assertNotNull(comment2); assertNotNull(comment2);
assertSame(comment2, sheet2.getCommentsTable(true)); assertSame(comment2, sheet2.getCommentsTable(true));
assertEquals("/xl/comments2.xml", comment2.getPackageRelationship().getTargetURI().toString()); assertEquals("/xl/comments2.xml", comment2.getPackagePart().getPartName().getName());
//comment1 and comment2 are different objects //comment1 and comment2 are different objects
assertNotSame(comment1, comment2); assertNotSame(comment1, comment2);
wb1.close();
//now test against a workbook containing cell comments //now test against a workbook containing cell comments
workbook = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx"); XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx");
sheet1 = workbook.getSheetAt(0); sheet1 = wb2.getSheetAt(0);
comment1 = sheet1.getCommentsTable(true); comment1 = sheet1.getCommentsTable(true);
assertNotNull(comment1); assertNotNull(comment1);
assertEquals("/xl/comments1.xml", comment1.getPackageRelationship().getTargetURI().toString()); assertEquals("/xl/comments1.xml", comment1.getPackagePart().getPartName().getName());
assertSame(comment1, sheet1.getCommentsTable(true)); assertSame(comment1, sheet1.getCommentsTable(true));
workbook.close(); wb2.close();
} }
/** /**
@ -969,8 +973,8 @@ public final class TestXSSFSheet extends BaseTestSheet {
@Override @Override
@Test @Test
public void createRow() throws IOException { public void createRow() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet(); XSSFSheet sheet = wb1.createSheet();
CTWorksheet wsh = sheet.getCTWorksheet(); CTWorksheet wsh = sheet.getCTWorksheet();
CTSheetData sheetData = wsh.getSheetData(); CTSheetData sheetData = wsh.getSheetData();
assertEquals(0, sheetData.sizeOfRowArray()); assertEquals(0, sheetData.sizeOfRowArray());
@ -1020,8 +1024,9 @@ public final class TestXSSFSheet extends BaseTestSheet {
assertEquals(0, sheetData.getRowArray(1).sizeOfCArray()); assertEquals(0, sheetData.getRowArray(1).sizeOfCArray());
assertEquals(0, row2.getPhysicalNumberOfCells()); assertEquals(0, row2.getPhysicalNumberOfCells());
workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook); XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
sheet = workbook.getSheetAt(0); wb1.close();
sheet = wb2.getSheetAt(0);
wsh = sheet.getCTWorksheet(); wsh = sheet.getCTWorksheet();
xrow = sheetData.getRowArray(); xrow = sheetData.getRowArray();
assertEquals(3, xrow.length); assertEquals(3, xrow.length);
@ -1043,7 +1048,7 @@ public final class TestXSSFSheet extends BaseTestSheet {
assertEquals(2, xrow[2].sizeOfCArray()); assertEquals(2, xrow[2].sizeOfCArray());
assertEquals(3, xrow[2].getR()); assertEquals(3, xrow[2].getR());
workbook.close(); wb2.close();
} }
@Test(expected=IllegalArgumentException.class) @Test(expected=IllegalArgumentException.class)
@ -1097,16 +1102,17 @@ public final class TestXSSFSheet extends BaseTestSheet {
@Test @Test
public void protectSheet_lowlevel_2013() throws IOException { public void protectSheet_lowlevel_2013() throws IOException {
String password = "test"; String password = "test";
XSSFWorkbook wb = new XSSFWorkbook(); XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet xs = wb.createSheet(); XSSFSheet xs = wb1.createSheet();
xs.setSheetPassword(password, HashAlgorithm.sha384); xs.setSheetPassword(password, HashAlgorithm.sha384);
wb = writeOutAndReadBack(wb); XSSFWorkbook wb2 = writeOutAndReadBack(wb1);
assertTrue(wb.getSheetAt(0).validateSheetPassword(password)); wb1.close();
assertTrue(wb2.getSheetAt(0).validateSheetPassword(password));
wb2.close();
wb = openSampleWorkbook("workbookProtection-sheet_password-2013.xlsx"); XSSFWorkbook wb3 = openSampleWorkbook("workbookProtection-sheet_password-2013.xlsx");
assertTrue(wb.getSheetAt(0).validateSheetPassword("pwd")); assertTrue(wb3.getSheetAt(0).validateSheetPassword("pwd"));
wb3.close();
wb.close();
} }
@ -1209,12 +1215,10 @@ public final class TestXSSFSheet extends BaseTestSheet {
} }
private void runGetTopRow(String file, boolean isXSSF, int... topRows) throws IOException { private void runGetTopRow(String file, boolean isXSSF, int... topRows) throws IOException {
final Workbook wb; final Workbook wb = (isXSSF)
if(isXSSF) { ? XSSFTestDataSamples.openSampleWorkbook(file)
wb = XSSFTestDataSamples.openSampleWorkbook(file); : HSSFTestDataSamples.openSampleWorkbook(file);
} else {
wb = HSSFTestDataSamples.openSampleWorkbook(file);
}
for (int si = 0; si < wb.getNumberOfSheets(); si++) { for (int si = 0; si < wb.getNumberOfSheets(); si++) {
Sheet sh = wb.getSheetAt(si); Sheet sh = wb.getSheetAt(si);
assertNotNull(sh.getSheetName()); assertNotNull(sh.getSheetName());
@ -1222,27 +1226,24 @@ public final class TestXSSFSheet extends BaseTestSheet {
} }
// for XSSF also test with SXSSF // for XSSF also test with SXSSF
if(isXSSF) { if (isXSSF) {
Workbook swb = new SXSSFWorkbook((XSSFWorkbook) wb); Workbook swb = new SXSSFWorkbook((XSSFWorkbook) wb);
try {
for (int si = 0; si < swb.getNumberOfSheets(); si++) { for (int si = 0; si < swb.getNumberOfSheets(); si++) {
Sheet sh = swb.getSheetAt(si); Sheet sh = swb.getSheetAt(si);
assertNotNull(sh.getSheetName()); assertNotNull(sh.getSheetName());
assertEquals("Did not match for sheet " + si, topRows[si], sh.getTopRow()); assertEquals("Did not match for sheet " + si, topRows[si], sh.getTopRow());
} }
} finally {
swb.close(); swb.close();
} }
}
wb.close();
} }
private void runGetLeftCol(String file, boolean isXSSF, int... topRows) throws IOException { private void runGetLeftCol(String file, boolean isXSSF, int... topRows) throws IOException {
final Workbook wb; final Workbook wb = (isXSSF)
if(isXSSF) { ? XSSFTestDataSamples.openSampleWorkbook(file)
wb = XSSFTestDataSamples.openSampleWorkbook(file); : HSSFTestDataSamples.openSampleWorkbook(file);
} else {
wb = HSSFTestDataSamples.openSampleWorkbook(file);
}
for (int si = 0; si < wb.getNumberOfSheets(); si++) { for (int si = 0; si < wb.getNumberOfSheets(); si++) {
Sheet sh = wb.getSheetAt(si); Sheet sh = wb.getSheetAt(si);
assertNotNull(sh.getSheetName()); assertNotNull(sh.getSheetName());
@ -1259,6 +1260,8 @@ public final class TestXSSFSheet extends BaseTestSheet {
} }
swb.close(); swb.close();
} }
wb.close();
} }
@Test @Test
@ -1427,6 +1430,7 @@ public final class TestXSSFSheet extends BaseTestSheet {
new AreaReference(sheet.getSheetName()+"!A$1:B$2", SpreadsheetVersion.EXCEL2007), new AreaReference(sheet.getSheetName()+"!A$1:B$2", SpreadsheetVersion.EXCEL2007),
new CellReference("H5"), new CellReference("H5"),
sheet2); sheet2);
wb.close();
} }
@Test(expected=POIXMLException.class) @Test(expected=POIXMLException.class)
@ -1435,7 +1439,11 @@ public final class TestXSSFSheet extends BaseTestSheet {
XSSFSheet sheet = wb.createSheet(); XSSFSheet sheet = wb.createSheet();
// Throws exception because we cannot read here // Throws exception because we cannot read here
try {
sheet.onDocumentRead(); sheet.onDocumentRead();
} finally {
wb.close();
}
} }
/** /**
@ -1456,8 +1464,8 @@ public final class TestXSSFSheet extends BaseTestSheet {
protected void testCopyOneRow(String copyRowsTestWorkbook) throws IOException { protected void testCopyOneRow(String copyRowsTestWorkbook) throws IOException {
final double FLOAT_PRECISION = 1e-9; final double FLOAT_PRECISION = 1e-9;
final XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(copyRowsTestWorkbook); final XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook(copyRowsTestWorkbook);
final XSSFSheet sheet = workbook.getSheetAt(0); final XSSFSheet sheet = wb.getSheetAt(0);
final CellCopyPolicy defaultCopyPolicy = new CellCopyPolicy(); final CellCopyPolicy defaultCopyPolicy = new CellCopyPolicy();
sheet.copyRows(1, 1, 6, defaultCopyPolicy); sheet.copyRows(1, 1, 6, defaultCopyPolicy);
@ -1587,12 +1595,14 @@ public final class TestXSSFSheet extends BaseTestSheet {
// Make sure other rows are blank (off-by-one errors) // Make sure other rows are blank (off-by-one errors)
assertNull(sheet.getRow(5)); assertNull(sheet.getRow(5));
assertNull(sheet.getRow(7)); assertNull(sheet.getRow(7));
wb.close();
} }
public void testCopyMultipleRows(String copyRowsTestWorkbook) throws IOException { public void testCopyMultipleRows(String copyRowsTestWorkbook) throws IOException {
final double FLOAT_PRECISION = 1e-9; final double FLOAT_PRECISION = 1e-9;
final XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(copyRowsTestWorkbook); final XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook(copyRowsTestWorkbook);
final XSSFSheet sheet = workbook.getSheetAt(0); final XSSFSheet sheet = wb.getSheetAt(0);
final CellCopyPolicy defaultCopyPolicy = new CellCopyPolicy(); final CellCopyPolicy defaultCopyPolicy = new CellCopyPolicy();
sheet.copyRows(0, 3, 8, defaultCopyPolicy); sheet.copyRows(0, 3, 8, defaultCopyPolicy);
@ -1751,7 +1761,7 @@ public final class TestXSSFSheet extends BaseTestSheet {
// Array Formula // Array Formula
col++; col++;
cell = CellUtil.getCell(destRow1, col); cell = CellUtil.getCell(destRow1, col);
System.out.println("Array formulas currently unsupported"); // System.out.println("Array formulas currently unsupported");
/* /*
// FIXME: Array Formula set with Sheet.setArrayFormula() instead of cell.setFormula() // FIXME: Array Formula set with Sheet.setArrayFormula() instead of cell.setFormula()
assertEquals("[Array Formula] N10 cell type", Cell.CELL_TYPE_FORMULA, cell.getCellType()); assertEquals("[Array Formula] N10 cell type", Cell.CELL_TYPE_FORMULA, cell.getCellType());
@ -1805,6 +1815,8 @@ public final class TestXSSFSheet extends BaseTestSheet {
// Make sure other rows are blank (off-by-one errors) // Make sure other rows are blank (off-by-one errors)
assertNull("Off-by-one lower edge case", sheet.getRow(7)); //one row above destHeaderRow assertNull("Off-by-one lower edge case", sheet.getRow(7)); //one row above destHeaderRow
assertNull("Off-by-one upper edge case", sheet.getRow(12)); //one row below destRow3 assertNull("Off-by-one upper edge case", sheet.getRow(12)); //one row below destRow3
wb.close();
} }
@Test @Test