mirror of
https://github.com/apache/poi.git
synced 2025-03-05 00:19:07 +00:00
Close more file-handles in tests, convert junit3 tests
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1856688 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
9576ab77f9
commit
097fd7a5e2
@ -194,7 +194,7 @@ public class HSSFFormulaEvaluator extends BaseFormulaEvaluator {
|
||||
|
||||
/**
|
||||
* Returns a CellValue wrapper around the supplied ValueEval instance.
|
||||
* @param cell
|
||||
* @param cell The cell with the formula
|
||||
*/
|
||||
protected CellValue evaluateFormulaCellValue(Cell cell) {
|
||||
ValueEval eval = _bookEvaluator.evaluate(new HSSFEvaluationCell((HSSFCell)cell));
|
||||
|
@ -17,19 +17,24 @@
|
||||
|
||||
package org.apache.poi.xwpf.model;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.apache.poi.xwpf.XWPFTestDataSamples;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFFooter;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFHeader;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
/**
|
||||
* Tests for XWPF Header Footer Stuff
|
||||
*/
|
||||
public class TestXWPFHeaderFooterPolicy extends TestCase {
|
||||
public class TestXWPFHeaderFooterPolicy {
|
||||
private XWPFDocument noHeader;
|
||||
private XWPFDocument header;
|
||||
private XWPFDocument headerFooter;
|
||||
@ -37,8 +42,8 @@ public class TestXWPFHeaderFooterPolicy extends TestCase {
|
||||
private XWPFDocument oddEven;
|
||||
private XWPFDocument diffFirst;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws IOException {
|
||||
@Before
|
||||
public void setUp() throws IOException {
|
||||
noHeader = XWPFTestDataSamples.openSampleDocument("NoHeadFoot.docx");
|
||||
header = XWPFTestDataSamples.openSampleDocument("ThreeColHead.docx");
|
||||
headerFooter = XWPFTestDataSamples.openSampleDocument("SimpleHeadThreeColFoot.docx");
|
||||
@ -47,6 +52,17 @@ public class TestXWPFHeaderFooterPolicy extends TestCase {
|
||||
diffFirst = XWPFTestDataSamples.openSampleDocument("DiffFirstPageHeadFoot.docx");
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws IOException {
|
||||
noHeader.close();
|
||||
header.close();
|
||||
headerFooter.close();
|
||||
footer.close();
|
||||
oddEven.close();
|
||||
diffFirst.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPolicy() {
|
||||
XWPFHeaderFooterPolicy policy;
|
||||
|
||||
@ -127,36 +143,39 @@ public class TestXWPFHeaderFooterPolicy extends TestCase {
|
||||
assertEquals(policy.getDefaultFooter(), policy.getFooter(2));
|
||||
assertEquals(policy.getDefaultFooter(), policy.getFooter(3));
|
||||
}
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
|
||||
@Test
|
||||
public void testCreate() throws Exception {
|
||||
XWPFDocument doc = new XWPFDocument();
|
||||
assertEquals(null, doc.getHeaderFooterPolicy());
|
||||
assertEquals(0, doc.getHeaderList().size());
|
||||
assertEquals(0, doc.getFooterList().size());
|
||||
|
||||
XWPFHeaderFooterPolicy policy = doc.createHeaderFooterPolicy();
|
||||
assertNotNull(doc.getHeaderFooterPolicy());
|
||||
assertEquals(0, doc.getHeaderList().size());
|
||||
assertEquals(0, doc.getFooterList().size());
|
||||
|
||||
// Create a header and a footer
|
||||
XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
|
||||
XWPFFooter footer = policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
|
||||
header.createParagraph().createRun().setText("Header Hello");
|
||||
footer.createParagraph().createRun().setText("Footer Bye");
|
||||
|
||||
|
||||
// Save, re-load, and check
|
||||
doc = XWPFTestDataSamples.writeOutAndReadBack(doc);
|
||||
assertNotNull(doc.getHeaderFooterPolicy());
|
||||
assertEquals(1, doc.getHeaderList().size());
|
||||
assertEquals(1, doc.getFooterList().size());
|
||||
|
||||
assertEquals("Header Hello\n", doc.getHeaderList().get(0).getText());
|
||||
assertEquals("Footer Bye\n", doc.getFooterList().get(0).getText());
|
||||
try (XWPFDocument doc = new XWPFDocument()) {
|
||||
assertNull(doc.getHeaderFooterPolicy());
|
||||
assertEquals(0, doc.getHeaderList().size());
|
||||
assertEquals(0, doc.getFooterList().size());
|
||||
|
||||
XWPFHeaderFooterPolicy policy = doc.createHeaderFooterPolicy();
|
||||
assertNotNull(doc.getHeaderFooterPolicy());
|
||||
assertEquals(0, doc.getHeaderList().size());
|
||||
assertEquals(0, doc.getFooterList().size());
|
||||
|
||||
// Create a header and a footer
|
||||
XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
|
||||
XWPFFooter footer = policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
|
||||
header.createParagraph().createRun().setText("Header Hello");
|
||||
footer.createParagraph().createRun().setText("Footer Bye");
|
||||
|
||||
|
||||
// Save, re-load, and check
|
||||
try (XWPFDocument docBack = XWPFTestDataSamples.writeOutAndReadBack(doc)) {
|
||||
assertNotNull(docBack.getHeaderFooterPolicy());
|
||||
assertEquals(1, docBack.getHeaderList().size());
|
||||
assertEquals(1, docBack.getFooterList().size());
|
||||
|
||||
assertEquals("Header Hello\n", docBack.getHeaderList().get(0).getText());
|
||||
assertEquals("Footer Bye\n", docBack.getFooterList().get(0).getText());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContents() {
|
||||
XWPFHeaderFooterPolicy policy;
|
||||
|
||||
|
@ -28,41 +28,44 @@ import org.junit.Test;
|
||||
public class TestChangeTracking {
|
||||
@Test
|
||||
public void detection() throws Exception {
|
||||
XWPFDocument documentWithoutChangeTracking = XWPFTestDataSamples.openSampleDocument("bug56075-changeTracking_off.docx");
|
||||
assertFalse(documentWithoutChangeTracking.isTrackRevisions());
|
||||
try (XWPFDocument documentWithoutChangeTracking = XWPFTestDataSamples.openSampleDocument("bug56075-changeTracking_off.docx")) {
|
||||
assertFalse(documentWithoutChangeTracking.isTrackRevisions());
|
||||
|
||||
XWPFDocument documentWithChangeTracking = XWPFTestDataSamples.openSampleDocument("bug56075-changeTracking_on.docx");
|
||||
assertTrue(documentWithChangeTracking.isTrackRevisions());
|
||||
try (XWPFDocument documentWithChangeTracking = XWPFTestDataSamples.openSampleDocument("bug56075-changeTracking_on.docx")) {
|
||||
assertTrue(documentWithChangeTracking.isTrackRevisions());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void activateChangeTracking() throws Exception {
|
||||
XWPFDocument document = XWPFTestDataSamples.openSampleDocument("bug56075-changeTracking_off.docx");
|
||||
assertFalse(document.isTrackRevisions());
|
||||
try (XWPFDocument document = XWPFTestDataSamples.openSampleDocument("bug56075-changeTracking_off.docx")) {
|
||||
assertFalse(document.isTrackRevisions());
|
||||
|
||||
document.setTrackRevisions(true);
|
||||
document.setTrackRevisions(true);
|
||||
|
||||
assertTrue(document.isTrackRevisions());
|
||||
assertTrue(document.isTrackRevisions());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("resource")
|
||||
public void integration() throws Exception {
|
||||
XWPFDocument doc = new XWPFDocument();
|
||||
try (XWPFDocument doc = new XWPFDocument()) {
|
||||
|
||||
XWPFParagraph p1 = doc.createParagraph();
|
||||
XWPFParagraph p1 = doc.createParagraph();
|
||||
|
||||
XWPFRun r1 = p1.createRun();
|
||||
r1.setText("Lorem ipsum dolor sit amet.");
|
||||
doc.setTrackRevisions(true);
|
||||
XWPFRun r1 = p1.createRun();
|
||||
r1.setText("Lorem ipsum dolor sit amet.");
|
||||
doc.setTrackRevisions(true);
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
doc.write(out);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
doc.write(out);
|
||||
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray());
|
||||
XWPFDocument document = new XWPFDocument(inputStream);
|
||||
inputStream.close();
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray());
|
||||
XWPFDocument document = new XWPFDocument(inputStream);
|
||||
inputStream.close();
|
||||
|
||||
assertTrue(document.isTrackRevisions());
|
||||
assertTrue(document.isTrackRevisions());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -34,61 +34,65 @@ import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
|
||||
public class TestXWPFPictureData extends TestCase {
|
||||
|
||||
public void testRead() throws InvalidFormatException, IOException {
|
||||
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("VariousPictures.docx");
|
||||
List<XWPFPictureData> pictures = sampleDoc.getAllPictures();
|
||||
try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("VariousPictures.docx")) {
|
||||
List<XWPFPictureData> pictures = sampleDoc.getAllPictures();
|
||||
|
||||
assertEquals(5, pictures.size());
|
||||
String[] ext = {"wmf", "png", "emf", "emf", "jpeg"};
|
||||
for (int i = 0; i < pictures.size(); i++) {
|
||||
assertEquals(ext[i], pictures.get(i).suggestFileExtension());
|
||||
assertEquals(5, pictures.size());
|
||||
String[] ext = {"wmf", "png", "emf", "emf", "jpeg"};
|
||||
for (int i = 0; i < pictures.size(); i++) {
|
||||
assertEquals(ext[i], pictures.get(i).suggestFileExtension());
|
||||
}
|
||||
|
||||
int num = pictures.size();
|
||||
|
||||
byte[] pictureData = XWPFTestDataSamples.getImage("nature1.jpg");
|
||||
|
||||
String relationId = sampleDoc.addPictureData(pictureData, XWPFDocument.PICTURE_TYPE_JPEG);
|
||||
// picture list was updated
|
||||
assertEquals(num + 1, pictures.size());
|
||||
XWPFPictureData pict = (XWPFPictureData) sampleDoc.getRelationById(relationId);
|
||||
assertNotNull(pict);
|
||||
assertEquals("jpeg", pict.suggestFileExtension());
|
||||
assertArrayEquals(pictureData, pict.getData());
|
||||
}
|
||||
|
||||
int num = pictures.size();
|
||||
|
||||
byte[] pictureData = XWPFTestDataSamples.getImage("nature1.jpg");
|
||||
|
||||
String relationId = sampleDoc.addPictureData(pictureData, XWPFDocument.PICTURE_TYPE_JPEG);
|
||||
// picture list was updated
|
||||
assertEquals(num + 1, pictures.size());
|
||||
XWPFPictureData pict = (XWPFPictureData) sampleDoc.getRelationById(relationId);
|
||||
assertEquals("jpeg", pict.suggestFileExtension());
|
||||
assertArrayEquals(pictureData, pict.getData());
|
||||
}
|
||||
|
||||
public void testPictureInHeader() throws IOException {
|
||||
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx");
|
||||
verifyOneHeaderPicture(sampleDoc);
|
||||
try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx")) {
|
||||
verifyOneHeaderPicture(sampleDoc);
|
||||
|
||||
XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
|
||||
verifyOneHeaderPicture(readBack);
|
||||
XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
|
||||
verifyOneHeaderPicture(readBack);
|
||||
}
|
||||
}
|
||||
|
||||
public void testCreateHeaderPicture() throws Exception {
|
||||
XWPFDocument doc = new XWPFDocument();
|
||||
|
||||
// Starts with no header
|
||||
XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
|
||||
assertNull(policy);
|
||||
|
||||
// Add a default header
|
||||
policy = doc.createHeaderFooterPolicy();
|
||||
XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
|
||||
header.createParagraph().createRun().setText("Hello, Header World!");
|
||||
header.createParagraph().createRun().setText("Paragraph 2");
|
||||
assertEquals(0, header.getAllPictures().size());
|
||||
assertEquals(2, header.getParagraphs().size());
|
||||
|
||||
// Add a picture to the first paragraph
|
||||
header.getParagraphs().get(0).getRuns().get(0).addPicture(
|
||||
new ByteArrayInputStream(new byte[] {1,2,3,4}),
|
||||
Document.PICTURE_TYPE_JPEG, "test.jpg", 2, 2);
|
||||
|
||||
// Check
|
||||
verifyOneHeaderPicture(doc);
|
||||
|
||||
// Save, re-load, re-check
|
||||
XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(doc);
|
||||
verifyOneHeaderPicture(readBack);
|
||||
try (XWPFDocument doc = new XWPFDocument()) {
|
||||
|
||||
// Starts with no header
|
||||
XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
|
||||
assertNull(policy);
|
||||
|
||||
// Add a default header
|
||||
policy = doc.createHeaderFooterPolicy();
|
||||
XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
|
||||
header.createParagraph().createRun().setText("Hello, Header World!");
|
||||
header.createParagraph().createRun().setText("Paragraph 2");
|
||||
assertEquals(0, header.getAllPictures().size());
|
||||
assertEquals(2, header.getParagraphs().size());
|
||||
|
||||
// Add a picture to the first paragraph
|
||||
header.getParagraphs().get(0).getRuns().get(0).addPicture(
|
||||
new ByteArrayInputStream(new byte[]{1, 2, 3, 4}),
|
||||
Document.PICTURE_TYPE_JPEG, "test.jpg", 2, 2);
|
||||
|
||||
// Check
|
||||
verifyOneHeaderPicture(doc);
|
||||
|
||||
// Save, re-load, re-check
|
||||
XWPFDocument readBack = XWPFTestDataSamples.writeOutAndReadBack(doc);
|
||||
verifyOneHeaderPicture(readBack);
|
||||
}
|
||||
}
|
||||
|
||||
private void verifyOneHeaderPicture(XWPFDocument sampleDoc) {
|
||||
@ -101,75 +105,78 @@ public class TestXWPFPictureData extends TestCase {
|
||||
}
|
||||
|
||||
public void testNew() throws InvalidFormatException, IOException {
|
||||
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("EmptyDocumentWithHeaderFooter.docx");
|
||||
byte[] jpegData = XWPFTestDataSamples.getImage("nature1.jpg");
|
||||
assertNotNull(jpegData);
|
||||
byte[] gifData = XWPFTestDataSamples.getImage("nature1.gif");
|
||||
assertNotNull(gifData);
|
||||
byte[] pngData = XWPFTestDataSamples.getImage("nature1.png");
|
||||
assertNotNull(pngData);
|
||||
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("EmptyDocumentWithHeaderFooter.docx")) {
|
||||
byte[] jpegData = XWPFTestDataSamples.getImage("nature1.jpg");
|
||||
assertNotNull(jpegData);
|
||||
byte[] gifData = XWPFTestDataSamples.getImage("nature1.gif");
|
||||
assertNotNull(gifData);
|
||||
byte[] pngData = XWPFTestDataSamples.getImage("nature1.png");
|
||||
assertNotNull(pngData);
|
||||
|
||||
List<XWPFPictureData> pictures = doc.getAllPictures();
|
||||
assertEquals(0, pictures.size());
|
||||
List<XWPFPictureData> pictures = doc.getAllPictures();
|
||||
assertEquals(0, pictures.size());
|
||||
|
||||
// Document shouldn't have any image relationships
|
||||
assertEquals(13, doc.getPackagePart().getRelationships().size());
|
||||
for (PackageRelationship rel : doc.getPackagePart().getRelationships()) {
|
||||
if (rel.getRelationshipType().equals(XSSFRelation.IMAGE_JPEG.getRelation())) {
|
||||
fail("Shouldn't have JPEG yet");
|
||||
// Document shouldn't have any image relationships
|
||||
assertEquals(13, doc.getPackagePart().getRelationships().size());
|
||||
for (PackageRelationship rel : doc.getPackagePart().getRelationships()) {
|
||||
if (rel.getRelationshipType().equals(XSSFRelation.IMAGE_JPEG.getRelation())) {
|
||||
fail("Shouldn't have JPEG yet");
|
||||
}
|
||||
}
|
||||
|
||||
// Add the image
|
||||
String relationId = doc.addPictureData(jpegData, XWPFDocument.PICTURE_TYPE_JPEG);
|
||||
assertEquals(1, pictures.size());
|
||||
XWPFPictureData jpgPicData = (XWPFPictureData) doc.getRelationById(relationId);
|
||||
assertNotNull(jpgPicData);
|
||||
assertEquals("jpeg", jpgPicData.suggestFileExtension());
|
||||
assertArrayEquals(jpegData, jpgPicData.getData());
|
||||
|
||||
// Ensure it now has one
|
||||
assertEquals(14, doc.getPackagePart().getRelationships().size());
|
||||
PackageRelationship jpegRel = null;
|
||||
for (PackageRelationship rel : doc.getPackagePart().getRelationships()) {
|
||||
if (rel.getRelationshipType().equals(XWPFRelation.IMAGE_JPEG.getRelation())) {
|
||||
if (jpegRel != null)
|
||||
fail("Found 2 jpegs!");
|
||||
jpegRel = rel;
|
||||
}
|
||||
}
|
||||
assertNotNull("JPEG Relationship not found", jpegRel);
|
||||
|
||||
// Check the details
|
||||
assertNotNull(jpegRel);
|
||||
assertEquals(XWPFRelation.IMAGE_JPEG.getRelation(), jpegRel.getRelationshipType());
|
||||
assertEquals("/word/document.xml", jpegRel.getSource().getPartName().toString());
|
||||
assertEquals("/word/media/image1.jpeg", jpegRel.getTargetURI().getPath());
|
||||
|
||||
XWPFPictureData pictureDataByID = doc.getPictureDataByID(jpegRel.getId());
|
||||
assertArrayEquals(jpegData, pictureDataByID.getData());
|
||||
|
||||
// Save an re-load, check it appears
|
||||
try (XWPFDocument docBack = XWPFTestDataSamples.writeOutAndReadBack(doc)) {
|
||||
assertEquals(1, docBack.getAllPictures().size());
|
||||
assertEquals(1, docBack.getAllPackagePictures().size());
|
||||
|
||||
// verify the picture that we read back in
|
||||
pictureDataByID = docBack.getPictureDataByID(jpegRel.getId());
|
||||
assertArrayEquals(jpegData, pictureDataByID.getData());
|
||||
}
|
||||
}
|
||||
|
||||
// Add the image
|
||||
String relationId = doc.addPictureData(jpegData, XWPFDocument.PICTURE_TYPE_JPEG);
|
||||
assertEquals(1, pictures.size());
|
||||
XWPFPictureData jpgPicData = (XWPFPictureData) doc.getRelationById(relationId);
|
||||
assertEquals("jpeg", jpgPicData.suggestFileExtension());
|
||||
assertArrayEquals(jpegData, jpgPicData.getData());
|
||||
|
||||
// Ensure it now has one
|
||||
assertEquals(14, doc.getPackagePart().getRelationships().size());
|
||||
PackageRelationship jpegRel = null;
|
||||
for (PackageRelationship rel : doc.getPackagePart().getRelationships()) {
|
||||
if (rel.getRelationshipType().equals(XWPFRelation.IMAGE_JPEG.getRelation())) {
|
||||
if (jpegRel != null)
|
||||
fail("Found 2 jpegs!");
|
||||
jpegRel = rel;
|
||||
}
|
||||
}
|
||||
assertNotNull("JPEG Relationship not found", jpegRel);
|
||||
|
||||
// Check the details
|
||||
assertNotNull(jpegRel);
|
||||
assertEquals(XWPFRelation.IMAGE_JPEG.getRelation(), jpegRel.getRelationshipType());
|
||||
assertEquals("/word/document.xml", jpegRel.getSource().getPartName().toString());
|
||||
assertEquals("/word/media/image1.jpeg", jpegRel.getTargetURI().getPath());
|
||||
|
||||
XWPFPictureData pictureDataByID = doc.getPictureDataByID(jpegRel.getId());
|
||||
assertArrayEquals(jpegData, pictureDataByID.getData());
|
||||
|
||||
// Save an re-load, check it appears
|
||||
doc = XWPFTestDataSamples.writeOutAndReadBack(doc);
|
||||
assertEquals(1, doc.getAllPictures().size());
|
||||
assertEquals(1, doc.getAllPackagePictures().size());
|
||||
|
||||
// verify the picture that we read back in
|
||||
pictureDataByID = doc.getPictureDataByID(jpegRel.getId());
|
||||
assertArrayEquals(jpegData, pictureDataByID.getData());
|
||||
|
||||
}
|
||||
|
||||
public void testBug51770() throws IOException {
|
||||
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug51170.docx");
|
||||
XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
|
||||
XWPFHeader header = policy.getDefaultHeader();
|
||||
for (XWPFParagraph paragraph : header.getParagraphs()) {
|
||||
for (XWPFRun run : paragraph.getRuns()) {
|
||||
for (XWPFPicture picture : run.getEmbeddedPictures()) {
|
||||
if (paragraph.getDocument() != null) {
|
||||
XWPFPictureData data = picture.getPictureData();
|
||||
if (data != null) {
|
||||
fail("Should have returned null: "+ data.getFileName());
|
||||
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug51170.docx")) {
|
||||
XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
|
||||
XWPFHeader header = policy.getDefaultHeader();
|
||||
for (XWPFParagraph paragraph : header.getParagraphs()) {
|
||||
for (XWPFRun run : paragraph.getRuns()) {
|
||||
for (XWPFPicture picture : run.getEmbeddedPictures()) {
|
||||
if (paragraph.getDocument() != null) {
|
||||
XWPFPictureData data = picture.getPictureData();
|
||||
if (data != null) {
|
||||
fail("Should have returned null: " + data.getFileName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -875,25 +875,25 @@ public abstract class BaseTestCell {
|
||||
|
||||
@Test
|
||||
public void test57008() throws IOException {
|
||||
Workbook wb1 = _testDataProvider.createWorkbook();
|
||||
Sheet sheet = wb1.createSheet();
|
||||
|
||||
Row row0 = sheet.createRow(0);
|
||||
Cell cell0 = row0.createCell(0);
|
||||
cell0.setCellValue("row 0, cell 0 _x0046_ without changes");
|
||||
|
||||
Cell cell1 = row0.createCell(1);
|
||||
cell1.setCellValue("row 0, cell 1 _x005fx0046_ with changes");
|
||||
|
||||
Cell cell2 = row0.createCell(2);
|
||||
cell2.setCellValue("hgh_x0041_**_x0100_*_x0101_*_x0190_*_x0200_*_x0300_*_x0427_*");
|
||||
try (Workbook wb1 = _testDataProvider.createWorkbook()) {
|
||||
Sheet sheet = wb1.createSheet();
|
||||
|
||||
checkUnicodeValues(wb1);
|
||||
|
||||
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
||||
checkUnicodeValues(wb2);
|
||||
wb2.close();
|
||||
wb1.close();
|
||||
Row row0 = sheet.createRow(0);
|
||||
Cell cell0 = row0.createCell(0);
|
||||
cell0.setCellValue("row 0, cell 0 _x0046_ without changes");
|
||||
|
||||
Cell cell1 = row0.createCell(1);
|
||||
cell1.setCellValue("row 0, cell 1 _x005fx0046_ with changes");
|
||||
|
||||
Cell cell2 = row0.createCell(2);
|
||||
cell2.setCellValue("hgh_x0041_**_x0100_*_x0101_*_x0190_*_x0200_*_x0300_*_x0427_*");
|
||||
|
||||
checkUnicodeValues(wb1);
|
||||
|
||||
try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
|
||||
checkUnicodeValues(wb2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -903,30 +903,29 @@ public abstract class BaseTestCell {
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void testSetCellValueNullRichTextString() throws IOException {
|
||||
Workbook wb = _testDataProvider.createWorkbook();
|
||||
Sheet sheet = wb.createSheet();
|
||||
Cell cell = sheet.createRow(0).createCell(0);
|
||||
try (Workbook wb = _testDataProvider.createWorkbook()) {
|
||||
Sheet sheet = wb.createSheet();
|
||||
Cell cell = sheet.createRow(0).createCell(0);
|
||||
|
||||
RichTextString nullStr = null;
|
||||
cell.setCellValue(nullStr);
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
RichTextString nullStr = null;
|
||||
cell.setCellValue(nullStr);
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
|
||||
cell = sheet.createRow(0).createCell(1);
|
||||
cell.setCellValue(1.2d);
|
||||
assertEquals(CellType.NUMERIC, cell.getCellType());
|
||||
cell.setCellValue(nullStr);
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
cell = sheet.createRow(0).createCell(1);
|
||||
cell.setCellValue(1.2d);
|
||||
assertEquals(CellType.NUMERIC, cell.getCellType());
|
||||
cell.setCellValue(nullStr);
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
|
||||
cell = sheet.createRow(0).createCell(1);
|
||||
cell.setCellValue(wb.getCreationHelper().createRichTextString("Test"));
|
||||
assertEquals(CellType.STRING, cell.getCellType());
|
||||
cell.setCellValue(nullStr);
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
|
||||
wb.close();
|
||||
cell = sheet.createRow(0).createCell(1);
|
||||
cell.setCellValue(wb.getCreationHelper().createRichTextString("Test"));
|
||||
assertEquals(CellType.STRING, cell.getCellType());
|
||||
cell.setCellValue(nullStr);
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
}
|
||||
}
|
||||
|
||||
private void checkUnicodeValues(Workbook wb) {
|
||||
@ -943,33 +942,32 @@ public abstract class BaseTestCell {
|
||||
*/
|
||||
@Test
|
||||
public void testMaxTextLength() throws IOException{
|
||||
Workbook wb = _testDataProvider.createWorkbook();
|
||||
Sheet sheet = wb.createSheet();
|
||||
Cell cell = sheet.createRow(0).createCell(0);
|
||||
try (Workbook wb = _testDataProvider.createWorkbook()) {
|
||||
Sheet sheet = wb.createSheet();
|
||||
Cell cell = sheet.createRow(0).createCell(0);
|
||||
|
||||
int maxlen = wb instanceof HSSFWorkbook ?
|
||||
SpreadsheetVersion.EXCEL97.getMaxTextLength()
|
||||
: SpreadsheetVersion.EXCEL2007.getMaxTextLength();
|
||||
assertEquals(32767, maxlen);
|
||||
int maxlen = wb instanceof HSSFWorkbook ?
|
||||
SpreadsheetVersion.EXCEL97.getMaxTextLength()
|
||||
: SpreadsheetVersion.EXCEL2007.getMaxTextLength();
|
||||
assertEquals(32767, maxlen);
|
||||
|
||||
StringBuilder b = new StringBuilder() ;
|
||||
StringBuilder b = new StringBuilder();
|
||||
|
||||
// 32767 is okay
|
||||
for( int i = 0 ; i < maxlen ; i++ )
|
||||
{
|
||||
b.append( "X" ) ;
|
||||
}
|
||||
cell.setCellValue(b.toString());
|
||||
|
||||
b.append("X");
|
||||
// 32768 produces an invalid XLS file
|
||||
try {
|
||||
// 32767 is okay
|
||||
for (int i = 0; i < maxlen; i++) {
|
||||
b.append("X");
|
||||
}
|
||||
cell.setCellValue(b.toString());
|
||||
fail("Expected exception");
|
||||
} catch (IllegalArgumentException e){
|
||||
assertEquals("The maximum length of cell contents (text) is 32767 characters", e.getMessage());
|
||||
|
||||
b.append("X");
|
||||
// 32768 produces an invalid XLS file
|
||||
try {
|
||||
cell.setCellValue(b.toString());
|
||||
fail("Expected exception");
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertEquals("The maximum length of cell contents (text) is 32767 characters", e.getMessage());
|
||||
}
|
||||
}
|
||||
wb.close();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -977,49 +975,47 @@ public abstract class BaseTestCell {
|
||||
*/
|
||||
@Test
|
||||
public void setAsActiveCell() throws IOException {
|
||||
Workbook wb = _testDataProvider.createWorkbook();
|
||||
Sheet sheet = wb.createSheet();
|
||||
Row row = sheet.createRow(0);
|
||||
Cell A1 = row.createCell(0);
|
||||
Cell B1 = row.createCell(1);
|
||||
try (Workbook wb = _testDataProvider.createWorkbook()) {
|
||||
Sheet sheet = wb.createSheet();
|
||||
Row row = sheet.createRow(0);
|
||||
Cell A1 = row.createCell(0);
|
||||
Cell B1 = row.createCell(1);
|
||||
|
||||
A1.setAsActiveCell();
|
||||
assertEquals(A1.getAddress(), sheet.getActiveCell());
|
||||
A1.setAsActiveCell();
|
||||
assertEquals(A1.getAddress(), sheet.getActiveCell());
|
||||
|
||||
B1.setAsActiveCell();
|
||||
assertEquals(B1.getAddress(), sheet.getActiveCell());
|
||||
|
||||
wb.close();
|
||||
B1.setAsActiveCell();
|
||||
assertEquals(B1.getAddress(), sheet.getActiveCell());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCellComment() throws IOException {
|
||||
Workbook wb = _testDataProvider.createWorkbook();
|
||||
Sheet sheet = wb.createSheet();
|
||||
CreationHelper factory = wb.getCreationHelper();
|
||||
Row row = sheet.createRow(0);
|
||||
Cell cell = row.createCell(1);
|
||||
|
||||
// cell does not have a comment
|
||||
assertNull(cell.getCellComment());
|
||||
|
||||
// add a cell comment
|
||||
ClientAnchor anchor = factory.createClientAnchor();
|
||||
anchor.setCol1(cell.getColumnIndex());
|
||||
anchor.setCol2(cell.getColumnIndex()+1);
|
||||
anchor.setRow1(row.getRowNum());
|
||||
anchor.setRow2(row.getRowNum()+3);
|
||||
try (Workbook wb = _testDataProvider.createWorkbook()) {
|
||||
Sheet sheet = wb.createSheet();
|
||||
CreationHelper factory = wb.getCreationHelper();
|
||||
Row row = sheet.createRow(0);
|
||||
Cell cell = row.createCell(1);
|
||||
|
||||
Drawing<?> drawing = sheet.createDrawingPatriarch();
|
||||
Comment comment = drawing.createCellComment(anchor);
|
||||
RichTextString str = factory.createRichTextString("Hello, World!");
|
||||
comment.setString(str);
|
||||
comment.setAuthor("Apache POI");
|
||||
cell.setCellComment(comment);
|
||||
// ideally assertSame, but XSSFCell creates a new XSSFCellComment wrapping the same bean for every call to getCellComment.
|
||||
assertEquals(comment, cell.getCellComment());
|
||||
// cell does not have a comment
|
||||
assertNull(cell.getCellComment());
|
||||
|
||||
wb.close();
|
||||
// add a cell comment
|
||||
ClientAnchor anchor = factory.createClientAnchor();
|
||||
anchor.setCol1(cell.getColumnIndex());
|
||||
anchor.setCol2(cell.getColumnIndex() + 1);
|
||||
anchor.setRow1(row.getRowNum());
|
||||
anchor.setRow2(row.getRowNum() + 3);
|
||||
|
||||
Drawing<?> drawing = sheet.createDrawingPatriarch();
|
||||
Comment comment = drawing.createCellComment(anchor);
|
||||
RichTextString str = factory.createRichTextString("Hello, World!");
|
||||
comment.setString(str);
|
||||
comment.setAuthor("Apache POI");
|
||||
cell.setCellComment(comment);
|
||||
// ideally assertSame, but XSSFCell creates a new XSSFCellComment wrapping the same bean for every call to getCellComment.
|
||||
assertEquals(comment, cell.getCellComment());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -1080,33 +1076,32 @@ public abstract class BaseTestCell {
|
||||
|
||||
@Test
|
||||
public void testSetNullValues() throws IOException {
|
||||
Workbook wb = _testDataProvider.createWorkbook();
|
||||
Cell cell = wb.createSheet("test").createRow(0).createCell(0);
|
||||
try (Workbook wb = _testDataProvider.createWorkbook()) {
|
||||
Cell cell = wb.createSheet("test").createRow(0).createCell(0);
|
||||
|
||||
cell.setCellValue((Calendar)null);
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
cell.setCellValue((Calendar) null);
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
|
||||
cell.setCellValue((Date)null);
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
cell.setCellValue((Date) null);
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
|
||||
cell.setCellValue((String)null);
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
cell.setCellValue((String) null);
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
|
||||
cell.setCellValue((RichTextString) null);
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
cell.setCellValue((RichTextString) null);
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
|
||||
cell.setCellValue((String)null);
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
|
||||
wb.close();
|
||||
cell.setCellValue((String) null);
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
assertEquals("", cell.getStringCellValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -1140,18 +1135,22 @@ public abstract class BaseTestCell {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNumericCellValueOnABlankCellReturnsZero() {
|
||||
Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0);
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
assertEquals(0, cell.getNumericCellValue(), 0);
|
||||
public void testGetNumericCellValueOnABlankCellReturnsZero() throws IOException {
|
||||
try (Workbook workbook = _testDataProvider.createWorkbook()) {
|
||||
Cell cell = workbook.createSheet().createRow(0).createCell(0);
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
assertEquals(0, cell.getNumericCellValue(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDateCellValue_returnsNull_onABlankCell() {
|
||||
Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0);
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
Date result = cell.getDateCellValue();
|
||||
assertNull(result);
|
||||
public void getDateCellValue_returnsNull_onABlankCell() throws IOException {
|
||||
try (Workbook workbook = _testDataProvider.createWorkbook()) {
|
||||
Cell cell = workbook.createSheet().createRow(0).createCell(0);
|
||||
assertEquals(CellType.BLANK, cell.getCellType());
|
||||
Date result = cell.getDateCellValue();
|
||||
assertNull(result);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -1188,31 +1187,37 @@ public abstract class BaseTestCell {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setStringCellValueWithRichTextString_ifThrows_shallNotChangeCell() {
|
||||
Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0);
|
||||
public void setStringCellValueWithRichTextString_ifThrows_shallNotChangeCell() throws IOException {
|
||||
try (Workbook workbook = _testDataProvider.createWorkbook()) {
|
||||
Cell cell = workbook.createSheet().createRow(0).createCell(0);
|
||||
|
||||
final double value = 2.78;
|
||||
cell.setCellValue(value);
|
||||
assertEquals(CellType.NUMERIC, cell.getCellType());
|
||||
final double value = 2.78;
|
||||
cell.setCellValue(value);
|
||||
assertEquals(CellType.NUMERIC, cell.getCellType());
|
||||
|
||||
int badLength = cell.getSheet().getWorkbook().getSpreadsheetVersion().getMaxTextLength() + 1;
|
||||
RichTextString badStringValue = cell.getSheet().getWorkbook().getCreationHelper().
|
||||
createRichTextString(new String(new byte[badLength], StandardCharsets.UTF_8));
|
||||
int badLength = cell.getSheet().getWorkbook().getSpreadsheetVersion().getMaxTextLength() + 1;
|
||||
RichTextString badStringValue = cell.getSheet().getWorkbook().getCreationHelper().
|
||||
createRichTextString(new String(new byte[badLength], StandardCharsets.UTF_8));
|
||||
|
||||
try {
|
||||
cell.setCellValue(badStringValue);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// no-op, expected to throw but we need to assert something more
|
||||
try {
|
||||
cell.setCellValue(badStringValue);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// no-op, expected to throw but we need to assert something more
|
||||
}
|
||||
|
||||
assertEquals(CellType.NUMERIC, cell.getCellType());
|
||||
assertEquals(value, cell.getNumericCellValue(), 0);
|
||||
}
|
||||
|
||||
assertEquals(CellType.NUMERIC, cell.getCellType());
|
||||
assertEquals(value, cell.getNumericCellValue(), 0);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void setCellType_null_throwsIAE() {
|
||||
public void setCellType_null_throwsIAE() throws IOException {
|
||||
Cell cell = getInstance();
|
||||
cell.setCellType(null);
|
||||
try {
|
||||
cell.setCellType(null);
|
||||
} finally {
|
||||
cell.getSheet().getWorkbook().close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@ -1246,14 +1251,18 @@ public abstract class BaseTestCell {
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void setCellFormula_throwsISE_ifCellIsPartOfAnArrayFormulaGroupContainingOtherCells() {
|
||||
public void setCellFormula_throwsISE_ifCellIsPartOfAnArrayFormulaGroupContainingOtherCells() throws IOException {
|
||||
Cell cell = getInstance();
|
||||
|
||||
cell.getSheet().setArrayFormula("1", CellRangeAddress.valueOf("A1:B1"));
|
||||
assertTrue(cell.isPartOfArrayFormulaGroup());
|
||||
assertEquals(CellType.FORMULA, cell.getCellType());
|
||||
try {
|
||||
cell.getSheet().setArrayFormula("1", CellRangeAddress.valueOf("A1:B1"));
|
||||
assertTrue(cell.isPartOfArrayFormulaGroup());
|
||||
assertEquals(CellType.FORMULA, cell.getCellType());
|
||||
|
||||
cell.setCellFormula("1");
|
||||
cell.setCellFormula("1");
|
||||
} finally {
|
||||
cell.getSheet().getWorkbook().close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -1311,23 +1320,25 @@ public abstract class BaseTestCell {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setCellFormula_onABlankCell_setsValueToZero() {
|
||||
public void setCellFormula_onABlankCell_setsValueToZero() throws IOException {
|
||||
Cell cell = getInstance();
|
||||
cell.setCellFormula("\"foo\"");
|
||||
assertEquals(CellType.FORMULA, cell.getCellType());
|
||||
assertEquals(CellType.NUMERIC, cell.getCachedFormulaResultType());
|
||||
assertEquals(0, cell.getNumericCellValue(), 0);
|
||||
cell.getSheet().getWorkbook().close();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void setCellFormula_onANonBlankCell_preservesTheValue() {
|
||||
public void setCellFormula_onANonBlankCell_preservesTheValue() throws IOException {
|
||||
Cell cell = getInstance();
|
||||
cell.setCellValue(true);
|
||||
cell.setCellFormula("\"foo\"");
|
||||
assertEquals(CellType.FORMULA, cell.getCellType());
|
||||
assertEquals(CellType.BOOLEAN, cell.getCachedFormulaResultType());
|
||||
assertTrue(cell.getBooleanCellValue());
|
||||
cell.getSheet().getWorkbook().close();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
x
Reference in New Issue
Block a user