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;
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.FileOutputStream;
import java.io.IOException;
@ -25,8 +31,6 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import junit.framework.TestCase;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
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.util.PackageHelper;
import org.apache.poi.util.TempFile;
import org.junit.Test;
/**
* Test recursive read and write of OPC packages
*/
public final class TestPOIXMLDocument extends TestCase {
public final class TestPOIXMLDocument {
private static class OPCParser extends POIXMLDocument {
@ -65,6 +70,7 @@ public final class TestPOIXMLDocument extends TestCase {
public TestFactory() {
//
}
@Override
public POIXMLDocumentPart createDocumentPart(POIXMLDocumentPart parent, PackageRelationship rel, PackagePart part){
return new POIXMLDocumentPart(part, rel);
@ -74,18 +80,20 @@ public final class TestPOIXMLDocument extends TestCase {
public POIXMLDocumentPart newDocumentPart(POIXMLRelation descriptor){
throw new RuntimeException("not supported");
}
}
/**
* 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{
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()){
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)) {
traverse(p, context);
} else {
@ -108,7 +116,9 @@ public final class TestPOIXMLDocument extends TestCase {
FileOutputStream out = new FileOutputStream(tmp);
doc.write(out);
out.close();
doc.close();
@SuppressWarnings("resource")
OPCPackage pkg2 = OPCPackage.open(tmp.getAbsolutePath());
try {
doc = new OPCParser(pkg1);
@ -135,30 +145,33 @@ public final class TestPOIXMLDocument extends TestCase {
assertEquals(p1.getPartName(), p2.getPartName());
}
} finally {
pkg2.close();
doc.close();
}
}
@Test
public void testPPTX() throws Exception {
assertReadWrite(
PackageHelper.open(POIDataSamples.getSlideShowInstance().openResourceAsStream("PPTWithAttachments.pptm"))
);
POIDataSamples pds = POIDataSamples.getSlideShowInstance();
assertReadWrite(PackageHelper.open(pds.openResourceAsStream("PPTWithAttachments.pptm")));
}
@Test
public void testXLSX() throws Exception {
assertReadWrite(
PackageHelper.open(POIDataSamples.getSpreadSheetInstance().openResourceAsStream("ExcelWithAttachments.xlsm"))
);
POIDataSamples pds = POIDataSamples.getSpreadSheetInstance();
assertReadWrite(PackageHelper.open(pds.openResourceAsStream("ExcelWithAttachments.xlsm")));
}
@Test
public void testDOCX() throws Exception {
assertReadWrite(
PackageHelper.open(POIDataSamples.getDocumentInstance().openResourceAsStream("WordWithAttachments.docx"))
);
POIDataSamples pds = POIDataSamples.getDocumentInstance();
assertReadWrite(PackageHelper.open(pds.openResourceAsStream("WordWithAttachments.docx")));
}
@Test
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);
try {
doc.parse(new TestFactory());
@ -172,6 +185,7 @@ public final class TestPOIXMLDocument extends TestCase {
}
}
@Test
public void testCommitNullPart() throws IOException, InvalidFormatException {
POIXMLDocumentPart part = new POIXMLDocumentPart();
part.prepareForCommit();
@ -187,32 +201,40 @@ public final class TestPOIXMLDocument extends TestCase {
//part.getTargetPart(null);
}
@Test
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);
assertNotNull(part);
assertEquals(0, part.getRelationCounter());
part.close();
}
public void testVSDXPart() throws Exception {
OPCPackage open = PackageHelper.open(POIDataSamples.getDiagramInstance().openResourceAsStream("test.vsdx"));
@Test
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);
assertNotNull(part);
assertEquals(0, part.getRelationCounter());
open.close();
}
public void testInvalidCoreRel() throws Exception {
OPCPackage open = PackageHelper.open(POIDataSamples.getDiagramInstance().openResourceAsStream("test.vsdx"));
@Test(expected=POIXMLException.class)
public void testInvalidCoreRel() throws IOException {
POIDataSamples pds = POIDataSamples.getDiagramInstance();
OPCPackage open = PackageHelper.open(pds.openResourceAsStream("test.vsdx"));
try {
new POIXMLDocumentPart(open, "somethingillegal");
fail("Unknown core ref will throw exception");
} catch (POIXMLException e) {
// expected here
} finally {
open.close();
}
}
}

View File

@ -25,19 +25,25 @@ import static org.junit.Assert.fail;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Collection;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.poi.POIDataSamples;
import org.apache.poi.POIXMLDocumentPart;
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.DrawingTextBody;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
@ -63,12 +69,12 @@ public class TestXSLFBugs {
// Check the relations on it
// Note - rId3 is a self reference
XSLFSlide slide0 = ss1.getSlides().get(0);
assertEquals("/ppt/slides/slide1.xml", slide0.getPackagePart().getPartName().toString());
assertEquals("/ppt/slideLayouts/slideLayout12.xml", slide0.getRelationById("rId1").getPackageRelationship().getTargetURI().toString());
assertEquals("/ppt/notesSlides/notesSlide1.xml", slide0.getRelationById("rId2").getPackageRelationship().getTargetURI().toString());
assertEquals("/ppt/slides/slide1.xml", slide0.getRelationById("rId3").getPackageRelationship().getTargetURI().toString());
assertEquals("/ppt/media/image1.png", slide0.getRelationById("rId4").getPackageRelationship().getTargetURI().toString());
assertRelation(slide0, "/ppt/slides/slide1.xml", null);
assertRelation(slide0, "/ppt/slideLayouts/slideLayout12.xml", "rId1");
assertRelation(slide0, "/ppt/notesSlides/notesSlide1.xml", "rId2");
assertRelation(slide0, "/ppt/slides/slide1.xml", "rId3");
assertRelation(slide0, "/ppt/media/image1.png", "rId4");
// Save and re-load
XMLSlideShow ss2 = XSLFTestDataSamples.writeOutAndReadBack(ss1);
@ -76,16 +82,21 @@ public class TestXSLFBugs {
assertEquals(1, ss2.getSlides().size());
slide0 = ss2.getSlides().get(0);
assertEquals("/ppt/slides/slide1.xml", slide0.getPackagePart().getPartName().toString());
assertEquals("/ppt/slideLayouts/slideLayout12.xml", slide0.getRelationById("rId1").getPackageRelationship().getTargetURI().toString());
assertEquals("/ppt/notesSlides/notesSlide1.xml", slide0.getRelationById("rId2").getPackageRelationship().getTargetURI().toString());
assertRelation(slide0, "/ppt/slides/slide1.xml", null);
assertRelation(slide0, "/ppt/slideLayouts/slideLayout12.xml", "rId1");
assertRelation(slide0, "/ppt/notesSlides/notesSlide1.xml", "rId2");
// TODO Fix this
assertEquals("/ppt/slides/slide1.xml", slide0.getRelationById("rId3").getPackageRelationship().getTargetURI().toString());
assertEquals("/ppt/media/image1.png", slide0.getRelationById("rId4").getPackageRelationship().getTargetURI().toString());
assertRelation(slide0, "/ppt/slides/slide1.xml", "rId3");
assertRelation(slide0, "/ppt/media/image1.png", "rId4");
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
*/
@ -149,17 +160,17 @@ public class TestXSLFBugs {
// Check the text, to see we got them in order
slide = ss.getSlides().get(0);
assertContains("POI cannot read this", getSlideText(slide));
assertContains(getSlideText(slide), "POI cannot read this");
slide = ss.getSlides().get(1);
assertContains("POI can read this", getSlideText(slide));
assertContains("Has a relationship to another slide", getSlideText(slide));
assertContains(getSlideText(slide), "POI can read this");
assertContains(getSlideText(slide), "Has a relationship to another slide");
slide = ss.getSlides().get(2);
assertContains("POI can read this", getSlideText(slide));
assertContains(getSlideText(slide), "POI can read this");
slide = ss.getSlides().get(3);
assertContains("POI can read this", getSlideText(slide));
assertContains(getSlideText(slide), "POI can read this");
ss.close();
}
@ -407,4 +418,19 @@ public class TestXSLFBugs {
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(Cell c : r) {
if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
String formula = c.getCellFormula();
CellValue cv;
try {
cv = eval.evaluate(c);
} catch (Exception e) {
throw new RuntimeException("Can't evaluate formula: " + formula, e);
}
CellValue cv = eval.evaluate(c);
if(cv.getCellType() == Cell.CELL_TYPE_NUMERIC) {
// assert that the calculated value agrees with
// the cached formula result calculated by Excel
String formula = c.getCellFormula();
double cachedFormulaResult = c.getNumericCellValue();
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 {
wb.close();
}

View File

@ -17,28 +17,30 @@
package org.apache.poi.xssf.usermodel;
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.io.IOException;
import java.util.List;
import junit.framework.TestCase;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.FontUnderline;
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.CTTextParagraph;
import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing;
/**
* @author Yegor Kozlov
*/
public class TestXSSFDrawing extends TestCase {
public void testRead() throws IOException{
public class TestXSSFDrawing {
@Test
public void testRead() throws IOException {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx");
XSSFSheet sheet = wb.getSheetAt(0);
//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());
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
checkRewrite(wb);
wb.close();
}
public void testNew() throws Exception {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
@Test
public void testNew() throws IOException {
XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet = wb1.createSheet();
//multiple calls of createDrawingPatriarch should return the same instance of XSSFDrawing
XSSFDrawing dr1 = 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));
XSSFRichTextString rt = new XSSFRichTextString("Test String");
rt.applyFont(0, 5, wb.createFont());
rt.applyFont(5, 6, wb.createFont());
rt.applyFont(0, 5, wb1.createFont());
rt.applyFont(5, 6, wb1.createFont());
c4.setText(rt);
c4.setNoFill(true);
@ -117,8 +121,9 @@ public class TestXSSFDrawing extends TestCase {
assertTrue(shapes.get(3) instanceof XSSFSimpleShape); //
// Save and re-load it
wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
sheet = wb.getSheetAt(0);
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sheet = wb2.getSheetAt(0);
// Check
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:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\""));
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
checkRewrite(wb2);
wb2.close();
}
@Test
public void testMultipleDrawings() throws IOException{
XSSFWorkbook wb = new XSSFWorkbook();
for (int i = 0; i < 3; i++) {
@ -154,13 +161,14 @@ public class TestXSSFDrawing extends TestCase {
OPCPackage pkg = wb.getPackage();
try {
assertEquals(3, pkg.getPartsByContentType(XSSFRelation.DRAWINGS.getContentType()).size());
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
checkRewrite(wb);
} finally {
pkg.close();
}
wb.close();
}
@Test
public void testClone() throws Exception{
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx");
XSSFSheet sheet1 = wb.getSheetAt(0);
@ -192,7 +200,8 @@ public class TestXSSFDrawing extends TestCase {
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.
*/
public void testRichText(){
@Test
public void testRichText() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
@ -229,15 +239,17 @@ public class TestXSSFDrawing extends TestCase {
new byte[]{0, (byte)128, (byte)128} ,
rPr.getSolidFill().getSrgbClr().getVal());
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
checkRewrite(wb);
wb.close();
}
/**
* test that anchor is not null when reading shapes from existing drawings
*/
public void testReadAnchors(){
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
@Test
public void testReadAnchors() throws IOException {
XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet = wb1.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
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);
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);
XSSFShape shape3 = drawing.createPicture(anchor3, pictureIndex);
assertNotNull(shape3);
wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
sheet = wb.getSheetAt(0);
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sheet = wb2.getSheetAt(0);
drawing = sheet.createDrawingPatriarch();
List<XSSFShape> shapes = drawing.getShapes();
assertEquals(shapes.get(0).getAnchor(), anchor1);
assertEquals(shapes.get(1).getAnchor(), anchor2);
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.
*/
public void testRichTextFontAndColor() {
@Test
public void testRichTextFontAndColor() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
@ -293,14 +308,15 @@ public class TestXSSFDrawing extends TestCase {
assertArrayEquals(
new byte[]{0, (byte)128, (byte)128} ,
rPr.getSolidFill().getSrgbClr().getVal());
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
checkRewrite(wb);
wb.close();
}
/**
* Test setText single paragraph to ensure backwards compatibility
*/
public void testSetTextSingleParagraph() {
@Test
public void testSetTextSingleParagraph() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
@ -328,13 +344,15 @@ public class TestXSSFDrawing extends TestCase {
new int[] { 0, 255, 255 } ,
new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
checkRewrite(wb);
wb.close();
}
/**
* Test addNewTextParagraph
*/
public void testAddNewTextParagraph() {
@Test
public void testAddNewTextParagraph() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
@ -351,34 +369,37 @@ public class TestXSSFDrawing extends TestCase {
assertEquals(1, runs.size());
assertEquals("Line 1", runs.get(0).getText());
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
checkRewrite(wb);
wb.close();
}
/**
* Test addNewTextParagraph using RichTextString
*/
public void testAddNewTextParagraphWithRTS() {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
@Test
public void testAddNewTextParagraphWithRTS() throws IOException {
XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet = wb1.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFTextBox shape = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));
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.setFontName("Arial");
rt.applyFont(font);
XSSFFont midfont = wb.createFont();
XSSFFont midfont = wb1.createFont();
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
XSSFTextParagraph para = shape.addNewTextParagraph(rt);
// Save and re-load it
wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
sheet = wb.getSheetAt(0);
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sheet = wb2.getSheetAt(0);
// Check
drawing = sheet.createDrawingPatriarch();
@ -421,13 +442,15 @@ public class TestXSSFDrawing extends TestCase {
new int[] { 0, 255, 255 } ,
new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
checkRewrite(wb2);
wb2.close();
}
/**
* Test add multiple paragraphs and retrieve text
*/
public void testAddMultipleParagraphs() {
@Test
public void testAddMultipleParagraphs() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
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("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
*/
public void testSetAddMultipleParagraphs() {
@Test
public void testSetAddMultipleParagraphs() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
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("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
*/
public void testReadTextBox(){
@Test
public void testReadTextBox() throws IOException {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx");
XSSFSheet sheet = wb.getSheetAt(0);
//the sheet has one relationship and it is XSSFDrawing
@ -503,14 +530,16 @@ public class TestXSSFDrawing extends TestCase {
XSSFSimpleShape textbox = (XSSFSimpleShape) shapes.get(4);
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
*/
public void testReadTextBoxParagraphs(){
@Test
public void testReadTextBoxParagraphs() throws IOException {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithTextBox.xlsx");
XSSFSheet sheet = wb.getSheetAt(0);
//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[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
checkRewrite(wb);
wb.close();
}
/**
* Test adding and reading back paragraphs as bullet points
*/
public void testAddBulletParagraphs() {
@Test
public void testAddBulletParagraphs() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet = wb1.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
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);
// Save and re-load it
wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
sheet = wb.getSheetAt(0);
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sheet = wb2.getSheetAt(0);
// Check
drawing = sheet.createDrawingPatriarch();
@ -672,13 +704,15 @@ public class TestXSSFDrawing extends TestCase {
assertEquals(builder.toString(), sshape.getText());
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
checkRewrite(wb2);
wb2.close();
}
/**
* 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");
XSSFSheet sheet = wb.getSheetAt(0);
XSSFDrawing drawing = sheet.createDrawingPatriarch();
@ -697,49 +731,51 @@ public class TestXSSFDrawing extends TestCase {
sb.append("\t\n\t\n\t\n\t");
assertEquals(sb.toString(), extracted);
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
checkRewrite(wb);
wb.close();
}
public void testXSSFSimpleShapeCausesNPE56514() throws Exception {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56514.xlsx");
XSSFSheet sheet = wb.getSheetAt(0);
@Test
public void testXSSFSimpleShapeCausesNPE56514() throws IOException {
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("56514.xlsx");
XSSFSheet sheet = wb1.getSheetAt(0);
XSSFDrawing drawing = sheet.createDrawingPatriarch();
List<XSSFShape> shapes = drawing.getShapes();
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();
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();
XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
// first comment works
ClientAnchor anchor = new XSSFClientAnchor(1, 1, 2, 2, 3, 3, 4, 4);
XSSFComment comment = drawing.createCellComment(anchor);
assertNotNull(comment);
// Should fail if we try to add the same comment for the same cell
try {
XSSFSheet sheet = wb.createSheet();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
// first comment works
ClientAnchor anchor = new XSSFClientAnchor(1, 1, 2, 2, 3, 3, 4, 4);
XSSFComment comment = drawing.createCellComment(anchor);
assertNotNull(comment);
try {
drawing.createCellComment(anchor);
fail("Should fail if we try to add the same comment for the same cell");
} catch (IllegalArgumentException e) {
// expected
}
drawing.createCellComment(anchor);
} finally {
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;
@SuppressWarnings("resource")
public final class TestXSSFSheet extends BaseTestSheet {
public TestXSSFSheet() {
@ -437,8 +436,8 @@ public final class TestXSSFSheet extends BaseTestSheet {
*/
@Test
public void setColumnGroupCollapsed() throws IOException {
Workbook wb = new XSSFWorkbook();
XSSFSheet sheet1 =(XSSFSheet) wb.createSheet();
XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet1 = wb1.createSheet();
CTCols cols=sheet1.getCTWorksheet().getColsArray(0);
assertEquals(0,cols.sizeOfColArray());
@ -630,8 +629,9 @@ public final class TestXSSFSheet extends BaseTestSheet {
// write out and give back
// Save and re-load
wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
sheet1 = (XSSFSheet)wb.getSheetAt(0);
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sheet1 = wb2.getSheetAt(0);
assertEquals(6,cols.sizeOfColArray());
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).getMax()); // 1 based
wb.close();
wb2.close();
}
/**
@ -670,8 +670,8 @@ public final class TestXSSFSheet extends BaseTestSheet {
*/
@Test
public void setRowGroupCollapsed() throws IOException {
Workbook wb = new XSSFWorkbook();
XSSFSheet sheet1 = (XSSFSheet)wb.createSheet();
XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet1 = wb1.createSheet();
sheet1.groupRow( 5, 14 );
sheet1.groupRow( 7, 14 );
@ -725,8 +725,9 @@ public final class TestXSSFSheet extends BaseTestSheet {
// Save and re-load
wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
sheet1 = (XSSFSheet)wb.getSheetAt(0);
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sheet1 = wb2.getSheetAt(0);
assertFalse(sheet1.getRow(6).getCTRow().isSetCollapsed());
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().isSetHidden());
wb.close();
wb2.close();
}
/**
@ -797,8 +798,8 @@ public final class TestXSSFSheet extends BaseTestSheet {
*/
@Test
public void bug47862() throws IOException {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("47862.xlsx");
XSSFSheet sheet = wb.getSheetAt(0);
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("47862.xlsx");
XSSFSheet sheet = wb1.getSheetAt(0);
CTCols cols = sheet.getCTWorksheet().getColsArray(0);
//<cols>
// <col min="1" max="5" width="15.77734375" customWidth="1"/>
@ -839,8 +840,9 @@ public final class TestXSSFSheet extends BaseTestSheet {
}
//serialize and check again
wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
sheet = wb.getSheetAt(0);
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sheet = wb2.getSheetAt(0);
cols = sheet.getCTWorksheet().getColsArray(0);
assertEquals(5, cols.sizeOfColArray());
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);
}
wb.close();
wb2.close();
}
/**
@ -856,8 +858,8 @@ public final class TestXSSFSheet extends BaseTestSheet {
*/
@Test
public void bug47804() throws IOException {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("47804.xlsx");
XSSFSheet sheet = wb.getSheetAt(0);
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("47804.xlsx");
XSSFSheet sheet = wb1.getSheetAt(0);
CTCols cols = sheet.getCTWorksheet().getColsArray(0);
assertEquals(2, cols.sizeOfColArray());
CTCol col;
@ -912,8 +914,9 @@ public final class TestXSSFSheet extends BaseTestSheet {
assertEquals(7, col.getMax());
//serialize and check again
wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
sheet = wb.getSheetAt(0);
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sheet = wb2.getSheetAt(0);
assertTrue(sheet.isColumnHidden(2));
assertTrue(sheet.isColumnHidden(6));
assertFalse(sheet.isColumnHidden(1));
@ -921,24 +924,24 @@ public final class TestXSSFSheet extends BaseTestSheet {
assertFalse(sheet.isColumnHidden(4));
assertFalse(sheet.isColumnHidden(5));
wb.close();
wb2.close();
}
@Test
public void commentsTable() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet1 = workbook.createSheet();
XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet1 = wb1.createSheet();
CommentsTable comment1 = sheet1.getCommentsTable(false);
assertNull(comment1);
comment1 = sheet1.getCommentsTable(true);
assertNotNull(comment1);
assertEquals("/xl/comments1.xml", comment1.getPackageRelationship().getTargetURI().toString());
assertEquals("/xl/comments1.xml", comment1.getPackagePart().getPartName().getName());
assertSame(comment1, sheet1.getCommentsTable(true));
//second sheet
XSSFSheet sheet2 = workbook.createSheet();
XSSFSheet sheet2 = wb1.createSheet();
CommentsTable comment2 = sheet2.getCommentsTable(false);
assertNull(comment2);
@ -946,20 +949,21 @@ public final class TestXSSFSheet extends BaseTestSheet {
assertNotNull(comment2);
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
assertNotSame(comment1, comment2);
wb1.close();
//now test against a workbook containing cell comments
workbook = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx");
sheet1 = workbook.getSheetAt(0);
XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx");
sheet1 = wb2.getSheetAt(0);
comment1 = sheet1.getCommentsTable(true);
assertNotNull(comment1);
assertEquals("/xl/comments1.xml", comment1.getPackageRelationship().getTargetURI().toString());
assertEquals("/xl/comments1.xml", comment1.getPackagePart().getPartName().getName());
assertSame(comment1, sheet1.getCommentsTable(true));
workbook.close();
wb2.close();
}
/**
@ -969,8 +973,8 @@ public final class TestXSSFSheet extends BaseTestSheet {
@Override
@Test
public void createRow() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet = wb1.createSheet();
CTWorksheet wsh = sheet.getCTWorksheet();
CTSheetData sheetData = wsh.getSheetData();
assertEquals(0, sheetData.sizeOfRowArray());
@ -1020,8 +1024,9 @@ public final class TestXSSFSheet extends BaseTestSheet {
assertEquals(0, sheetData.getRowArray(1).sizeOfCArray());
assertEquals(0, row2.getPhysicalNumberOfCells());
workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
sheet = workbook.getSheetAt(0);
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sheet = wb2.getSheetAt(0);
wsh = sheet.getCTWorksheet();
xrow = sheetData.getRowArray();
assertEquals(3, xrow.length);
@ -1043,7 +1048,7 @@ public final class TestXSSFSheet extends BaseTestSheet {
assertEquals(2, xrow[2].sizeOfCArray());
assertEquals(3, xrow[2].getR());
workbook.close();
wb2.close();
}
@Test(expected=IllegalArgumentException.class)
@ -1097,16 +1102,17 @@ public final class TestXSSFSheet extends BaseTestSheet {
@Test
public void protectSheet_lowlevel_2013() throws IOException {
String password = "test";
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet xs = wb.createSheet();
XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet xs = wb1.createSheet();
xs.setSheetPassword(password, HashAlgorithm.sha384);
wb = writeOutAndReadBack(wb);
assertTrue(wb.getSheetAt(0).validateSheetPassword(password));
XSSFWorkbook wb2 = writeOutAndReadBack(wb1);
wb1.close();
assertTrue(wb2.getSheetAt(0).validateSheetPassword(password));
wb2.close();
wb = openSampleWorkbook("workbookProtection-sheet_password-2013.xlsx");
assertTrue(wb.getSheetAt(0).validateSheetPassword("pwd"));
wb.close();
XSSFWorkbook wb3 = openSampleWorkbook("workbookProtection-sheet_password-2013.xlsx");
assertTrue(wb3.getSheetAt(0).validateSheetPassword("pwd"));
wb3.close();
}
@ -1209,12 +1215,10 @@ public final class TestXSSFSheet extends BaseTestSheet {
}
private void runGetTopRow(String file, boolean isXSSF, int... topRows) throws IOException {
final Workbook wb;
if(isXSSF) {
wb = XSSFTestDataSamples.openSampleWorkbook(file);
} else {
wb = HSSFTestDataSamples.openSampleWorkbook(file);
}
final Workbook wb = (isXSSF)
? XSSFTestDataSamples.openSampleWorkbook(file)
: HSSFTestDataSamples.openSampleWorkbook(file);
for (int si = 0; si < wb.getNumberOfSheets(); si++) {
Sheet sh = wb.getSheetAt(si);
assertNotNull(sh.getSheetName());
@ -1222,27 +1226,24 @@ public final class TestXSSFSheet extends BaseTestSheet {
}
// for XSSF also test with SXSSF
if(isXSSF) {
if (isXSSF) {
Workbook swb = new SXSSFWorkbook((XSSFWorkbook) wb);
try {
for (int si = 0; si < swb.getNumberOfSheets(); si++) {
Sheet sh = swb.getSheetAt(si);
assertNotNull(sh.getSheetName());
assertEquals("Did not match for sheet " + si, topRows[si], sh.getTopRow());
}
} finally {
swb.close();
for (int si = 0; si < swb.getNumberOfSheets(); si++) {
Sheet sh = swb.getSheetAt(si);
assertNotNull(sh.getSheetName());
assertEquals("Did not match for sheet " + si, topRows[si], sh.getTopRow());
}
swb.close();
}
wb.close();
}
private void runGetLeftCol(String file, boolean isXSSF, int... topRows) throws IOException {
final Workbook wb;
if(isXSSF) {
wb = XSSFTestDataSamples.openSampleWorkbook(file);
} else {
wb = HSSFTestDataSamples.openSampleWorkbook(file);
}
final Workbook wb = (isXSSF)
? XSSFTestDataSamples.openSampleWorkbook(file)
: HSSFTestDataSamples.openSampleWorkbook(file);
for (int si = 0; si < wb.getNumberOfSheets(); si++) {
Sheet sh = wb.getSheetAt(si);
assertNotNull(sh.getSheetName());
@ -1259,6 +1260,8 @@ public final class TestXSSFSheet extends BaseTestSheet {
}
swb.close();
}
wb.close();
}
@Test
@ -1427,6 +1430,7 @@ public final class TestXSSFSheet extends BaseTestSheet {
new AreaReference(sheet.getSheetName()+"!A$1:B$2", SpreadsheetVersion.EXCEL2007),
new CellReference("H5"),
sheet2);
wb.close();
}
@Test(expected=POIXMLException.class)
@ -1435,7 +1439,11 @@ public final class TestXSSFSheet extends BaseTestSheet {
XSSFSheet sheet = wb.createSheet();
// Throws exception because we cannot read here
sheet.onDocumentRead();
try {
sheet.onDocumentRead();
} finally {
wb.close();
}
}
/**
@ -1456,8 +1464,8 @@ public final class TestXSSFSheet extends BaseTestSheet {
protected void testCopyOneRow(String copyRowsTestWorkbook) throws IOException {
final double FLOAT_PRECISION = 1e-9;
final XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(copyRowsTestWorkbook);
final XSSFSheet sheet = workbook.getSheetAt(0);
final XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook(copyRowsTestWorkbook);
final XSSFSheet sheet = wb.getSheetAt(0);
final CellCopyPolicy defaultCopyPolicy = new CellCopyPolicy();
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)
assertNull(sheet.getRow(5));
assertNull(sheet.getRow(7));
wb.close();
}
public void testCopyMultipleRows(String copyRowsTestWorkbook) throws IOException {
final double FLOAT_PRECISION = 1e-9;
final XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(copyRowsTestWorkbook);
final XSSFSheet sheet = workbook.getSheetAt(0);
final XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook(copyRowsTestWorkbook);
final XSSFSheet sheet = wb.getSheetAt(0);
final CellCopyPolicy defaultCopyPolicy = new CellCopyPolicy();
sheet.copyRows(0, 3, 8, defaultCopyPolicy);
@ -1751,7 +1761,7 @@ public final class TestXSSFSheet extends BaseTestSheet {
// Array Formula
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()
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)
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
wb.close();
}
@Test