mirror of
https://github.com/apache/poi.git
synced 2026-02-27 20:40:08 +08:00
Close more file-handles in tests and convert some more tests to junit-4
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1857066 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ec2db31b43
commit
9b726e88b7
@ -19,126 +19,128 @@ package org.apache.poi.xssf.model;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Name;
|
||||
import org.apache.poi.xssf.XSSFTestDataSamples;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public final class TestExternalLinksTable {
|
||||
@Test
|
||||
public void none() {
|
||||
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("SampleSS.xlsx");
|
||||
assertNotNull(wb.getExternalLinksTable());
|
||||
assertEquals(0, wb.getExternalLinksTable().size());
|
||||
public void none() throws IOException {
|
||||
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("SampleSS.xlsx")) {
|
||||
assertNotNull(wb.getExternalLinksTable());
|
||||
assertEquals(0, wb.getExternalLinksTable().size());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basicRead() {
|
||||
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("ref-56737.xlsx");
|
||||
assertNotNull(wb.getExternalLinksTable());
|
||||
Name name = null;
|
||||
public void basicRead() throws IOException {
|
||||
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("ref-56737.xlsx")) {
|
||||
assertNotNull(wb.getExternalLinksTable());
|
||||
assertEquals(1, wb.getExternalLinksTable().size());
|
||||
|
||||
assertEquals(1, wb.getExternalLinksTable().size());
|
||||
ExternalLinksTable links = wb.getExternalLinksTable().get(0);
|
||||
assertEquals(3, links.getSheetNames().size());
|
||||
assertEquals(2, links.getDefinedNames().size());
|
||||
|
||||
ExternalLinksTable links = wb.getExternalLinksTable().get(0);
|
||||
assertEquals(3, links.getSheetNames().size());
|
||||
assertEquals(2, links.getDefinedNames().size());
|
||||
|
||||
assertEquals("Uses", links.getSheetNames().get(0));
|
||||
assertEquals("Defines", links.getSheetNames().get(1));
|
||||
assertEquals("56737", links.getSheetNames().get(2));
|
||||
|
||||
name = links.getDefinedNames().get(0);
|
||||
assertEquals("NR_Global_B2", name.getNameName());
|
||||
assertEquals(-1, name.getSheetIndex());
|
||||
assertEquals(null, name.getSheetName());
|
||||
assertEquals("'Defines'!$B$2", name.getRefersToFormula());
|
||||
|
||||
name = links.getDefinedNames().get(1);
|
||||
assertEquals("NR_To_A1", name.getNameName());
|
||||
assertEquals(1, name.getSheetIndex());
|
||||
assertEquals("Defines", name.getSheetName());
|
||||
assertEquals("'Defines'!$A$1", name.getRefersToFormula());
|
||||
|
||||
assertEquals("56737.xlsx", links.getLinkedFileName());
|
||||
assertEquals("Uses", links.getSheetNames().get(0));
|
||||
assertEquals("Defines", links.getSheetNames().get(1));
|
||||
assertEquals("56737", links.getSheetNames().get(2));
|
||||
|
||||
Name name = links.getDefinedNames().get(0);
|
||||
assertEquals("NR_Global_B2", name.getNameName());
|
||||
assertEquals(-1, name.getSheetIndex());
|
||||
assertNull(name.getSheetName());
|
||||
assertEquals("'Defines'!$B$2", name.getRefersToFormula());
|
||||
|
||||
name = links.getDefinedNames().get(1);
|
||||
assertEquals("NR_To_A1", name.getNameName());
|
||||
assertEquals(1, name.getSheetIndex());
|
||||
assertEquals("Defines", name.getSheetName());
|
||||
assertEquals("'Defines'!$A$1", name.getRefersToFormula());
|
||||
|
||||
assertEquals("56737.xlsx", links.getLinkedFileName());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void basicReadWriteRead() {
|
||||
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("ref-56737.xlsx");
|
||||
Name name = wb.getExternalLinksTable().get(0).getDefinedNames().get(1);
|
||||
name.setNameName("Testing");
|
||||
name.setRefersToFormula("$A$1");
|
||||
|
||||
wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
|
||||
assertEquals(1, wb.getExternalLinksTable().size());
|
||||
ExternalLinksTable links = wb.getExternalLinksTable().get(0);
|
||||
|
||||
name = links.getDefinedNames().get(0);
|
||||
assertEquals("NR_Global_B2", name.getNameName());
|
||||
assertEquals(-1, name.getSheetIndex());
|
||||
assertEquals(null, name.getSheetName());
|
||||
assertEquals("'Defines'!$B$2", name.getRefersToFormula());
|
||||
|
||||
name = links.getDefinedNames().get(1);
|
||||
assertEquals("Testing", name.getNameName());
|
||||
assertEquals(1, name.getSheetIndex());
|
||||
assertEquals("Defines", name.getSheetName());
|
||||
assertEquals("$A$1", name.getRefersToFormula());
|
||||
public void basicReadWriteRead() throws IOException {
|
||||
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("ref-56737.xlsx")) {
|
||||
Name name = wb.getExternalLinksTable().get(0).getDefinedNames().get(1);
|
||||
name.setNameName("Testing");
|
||||
name.setRefersToFormula("$A$1");
|
||||
|
||||
XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
|
||||
assertEquals(1, wbBack.getExternalLinksTable().size());
|
||||
ExternalLinksTable links = wbBack.getExternalLinksTable().get(0);
|
||||
|
||||
name = links.getDefinedNames().get(0);
|
||||
assertEquals("NR_Global_B2", name.getNameName());
|
||||
assertEquals(-1, name.getSheetIndex());
|
||||
assertNull(name.getSheetName());
|
||||
assertEquals("'Defines'!$B$2", name.getRefersToFormula());
|
||||
|
||||
name = links.getDefinedNames().get(1);
|
||||
assertEquals("Testing", name.getNameName());
|
||||
assertEquals(1, name.getSheetIndex());
|
||||
assertEquals("Defines", name.getSheetName());
|
||||
assertEquals("$A$1", name.getRefersToFormula());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readWithReferencesToTwoExternalBooks() {
|
||||
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("ref2-56737.xlsx");
|
||||
|
||||
assertNotNull(wb.getExternalLinksTable());
|
||||
Name name = null;
|
||||
public void readWithReferencesToTwoExternalBooks() throws IOException {
|
||||
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("ref2-56737.xlsx")) {
|
||||
assertNotNull(wb.getExternalLinksTable());
|
||||
assertEquals(2, wb.getExternalLinksTable().size());
|
||||
|
||||
assertEquals(2, wb.getExternalLinksTable().size());
|
||||
// Check the first one, links to 56737.xlsx
|
||||
ExternalLinksTable links = wb.getExternalLinksTable().get(0);
|
||||
assertEquals("56737.xlsx", links.getLinkedFileName());
|
||||
assertEquals(3, links.getSheetNames().size());
|
||||
assertEquals(2, links.getDefinedNames().size());
|
||||
|
||||
// Check the first one, links to 56737.xlsx
|
||||
ExternalLinksTable links = wb.getExternalLinksTable().get(0);
|
||||
assertEquals("56737.xlsx", links.getLinkedFileName());
|
||||
assertEquals(3, links.getSheetNames().size());
|
||||
assertEquals(2, links.getDefinedNames().size());
|
||||
|
||||
assertEquals("Uses", links.getSheetNames().get(0));
|
||||
assertEquals("Defines", links.getSheetNames().get(1));
|
||||
assertEquals("56737", links.getSheetNames().get(2));
|
||||
|
||||
name = links.getDefinedNames().get(0);
|
||||
assertEquals("NR_Global_B2", name.getNameName());
|
||||
assertEquals(-1, name.getSheetIndex());
|
||||
assertEquals(null, name.getSheetName());
|
||||
assertEquals("'Defines'!$B$2", name.getRefersToFormula());
|
||||
|
||||
name = links.getDefinedNames().get(1);
|
||||
assertEquals("NR_To_A1", name.getNameName());
|
||||
assertEquals(1, name.getSheetIndex());
|
||||
assertEquals("Defines", name.getSheetName());
|
||||
assertEquals("'Defines'!$A$1", name.getRefersToFormula());
|
||||
assertEquals("Uses", links.getSheetNames().get(0));
|
||||
assertEquals("Defines", links.getSheetNames().get(1));
|
||||
assertEquals("56737", links.getSheetNames().get(2));
|
||||
|
||||
|
||||
// Check the second one, links to 56737.xls, slightly differently
|
||||
links = wb.getExternalLinksTable().get(1);
|
||||
assertEquals("56737.xls", links.getLinkedFileName());
|
||||
assertEquals(2, links.getSheetNames().size());
|
||||
assertEquals(2, links.getDefinedNames().size());
|
||||
|
||||
assertEquals("Uses", links.getSheetNames().get(0));
|
||||
assertEquals("Defines", links.getSheetNames().get(1));
|
||||
|
||||
name = links.getDefinedNames().get(0);
|
||||
assertEquals("NR_Global_B2", name.getNameName());
|
||||
assertEquals(-1, name.getSheetIndex());
|
||||
assertEquals(null, name.getSheetName());
|
||||
assertEquals("'Defines'!$B$2", name.getRefersToFormula());
|
||||
|
||||
name = links.getDefinedNames().get(1);
|
||||
assertEquals("NR_To_A1", name.getNameName());
|
||||
assertEquals(1, name.getSheetIndex());
|
||||
assertEquals("Defines", name.getSheetName());
|
||||
assertEquals("'Defines'!$A$1", name.getRefersToFormula());
|
||||
Name name = links.getDefinedNames().get(0);
|
||||
assertEquals("NR_Global_B2", name.getNameName());
|
||||
assertEquals(-1, name.getSheetIndex());
|
||||
assertNull(name.getSheetName());
|
||||
assertEquals("'Defines'!$B$2", name.getRefersToFormula());
|
||||
|
||||
name = links.getDefinedNames().get(1);
|
||||
assertEquals("NR_To_A1", name.getNameName());
|
||||
assertEquals(1, name.getSheetIndex());
|
||||
assertEquals("Defines", name.getSheetName());
|
||||
assertEquals("'Defines'!$A$1", name.getRefersToFormula());
|
||||
|
||||
|
||||
// Check the second one, links to 56737.xls, slightly differently
|
||||
links = wb.getExternalLinksTable().get(1);
|
||||
assertEquals("56737.xls", links.getLinkedFileName());
|
||||
assertEquals(2, links.getSheetNames().size());
|
||||
assertEquals(2, links.getDefinedNames().size());
|
||||
|
||||
assertEquals("Uses", links.getSheetNames().get(0));
|
||||
assertEquals("Defines", links.getSheetNames().get(1));
|
||||
|
||||
name = links.getDefinedNames().get(0);
|
||||
assertEquals("NR_Global_B2", name.getNameName());
|
||||
assertEquals(-1, name.getSheetIndex());
|
||||
assertNull(name.getSheetName());
|
||||
assertEquals("'Defines'!$B$2", name.getRefersToFormula());
|
||||
|
||||
name = links.getDefinedNames().get(1);
|
||||
assertEquals("NR_To_A1", name.getNameName());
|
||||
assertEquals(1, name.getSheetIndex());
|
||||
assertEquals("Defines", name.getSheetName());
|
||||
assertEquals("'Defines'!$A$1", name.getRefersToFormula());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,59 +16,62 @@
|
||||
==================================================================== */
|
||||
package org.apache.poi.xssf.model;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.apache.poi.ooxml.POIXMLDocumentPart;
|
||||
import org.apache.poi.xssf.XSSFTestDataSamples;
|
||||
import org.apache.poi.xssf.usermodel.XSSFMap;
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.junit.Test;
|
||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMapInfo;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* @author Roberto Manicardi
|
||||
*/
|
||||
public final class TestMapInfo extends TestCase {
|
||||
public final class TestMapInfo {
|
||||
@Test
|
||||
public void testMapInfoExists() throws IOException {
|
||||
try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx")) {
|
||||
|
||||
MapInfo mapInfo = null;
|
||||
SingleXmlCells singleXMLCells = null;
|
||||
|
||||
for (POIXMLDocumentPart p : wb.getRelations()) {
|
||||
|
||||
|
||||
public void testMapInfoExists() {
|
||||
|
||||
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx");
|
||||
|
||||
MapInfo mapInfo = null;
|
||||
SingleXmlCells singleXMLCells = null;
|
||||
|
||||
for (POIXMLDocumentPart p : wb.getRelations()) {
|
||||
if (p instanceof MapInfo) {
|
||||
mapInfo = (MapInfo) p;
|
||||
|
||||
|
||||
if (p instanceof MapInfo) {
|
||||
mapInfo = (MapInfo) p;
|
||||
CTMapInfo ctMapInfo = mapInfo.getCTMapInfo();
|
||||
|
||||
assertNotNull(ctMapInfo);
|
||||
|
||||
CTMapInfo ctMapInfo = mapInfo.getCTMapInfo();
|
||||
assertEquals(1, ctMapInfo.sizeOfSchemaArray());
|
||||
|
||||
assertNotNull(ctMapInfo);
|
||||
|
||||
assertEquals(1, ctMapInfo.sizeOfSchemaArray());
|
||||
|
||||
for (XSSFMap map : mapInfo.getAllXSSFMaps()) {
|
||||
Node xmlSchema = map.getSchema();
|
||||
assertNotNull(xmlSchema);
|
||||
for (XSSFMap map : mapInfo.getAllXSSFMaps()) {
|
||||
Node xmlSchema = map.getSchema();
|
||||
assertNotNull(xmlSchema);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
XSSFSheet sheet1 = wb.getSheetAt(0);
|
||||
XSSFSheet sheet1 = wb.getSheetAt(0);
|
||||
|
||||
for (POIXMLDocumentPart p : sheet1.getRelations()) {
|
||||
for (POIXMLDocumentPart p : sheet1.getRelations()) {
|
||||
|
||||
if (p instanceof SingleXmlCells) {
|
||||
singleXMLCells = (SingleXmlCells) p;
|
||||
}
|
||||
|
||||
if (p instanceof SingleXmlCells) {
|
||||
singleXMLCells = (SingleXmlCells) p;
|
||||
}
|
||||
|
||||
assertNotNull(mapInfo);
|
||||
assertNotNull(singleXMLCells);
|
||||
}
|
||||
assertNotNull(mapInfo);
|
||||
assertNotNull(singleXMLCells);
|
||||
}
|
||||
}
|
||||
|
||||
@ -57,47 +57,50 @@ public final class TestStylesTable {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateSaveLoad() {
|
||||
XSSFWorkbook wb = new XSSFWorkbook();
|
||||
StylesTable st = wb.getStylesSource();
|
||||
public void testCreateSaveLoad() throws IOException {
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook()) {
|
||||
StylesTable st = wb.getStylesSource();
|
||||
|
||||
assertNotNull(st.getCTStylesheet());
|
||||
assertEquals(1, st._getXfsSize());
|
||||
assertEquals(1, st._getStyleXfsSize());
|
||||
assertEquals(0, st.getNumDataFormats());
|
||||
assertNotNull(st.getCTStylesheet());
|
||||
assertEquals(1, st._getXfsSize());
|
||||
assertEquals(1, st._getStyleXfsSize());
|
||||
assertEquals(0, st.getNumDataFormats());
|
||||
|
||||
st = XSSFTestDataSamples.writeOutAndReadBack(wb).getStylesSource();
|
||||
st = XSSFTestDataSamples.writeOutAndReadBack(wb).getStylesSource();
|
||||
|
||||
assertNotNull(st.getCTStylesheet());
|
||||
assertEquals(1, st._getXfsSize());
|
||||
assertEquals(1, st._getStyleXfsSize());
|
||||
assertEquals(0, st.getNumDataFormats());
|
||||
|
||||
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
|
||||
assertNotNull(st.getCTStylesheet());
|
||||
assertEquals(1, st._getXfsSize());
|
||||
assertEquals(1, st._getStyleXfsSize());
|
||||
assertEquals(0, st.getNumDataFormats());
|
||||
|
||||
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadExisting() {
|
||||
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile);
|
||||
assertNotNull(workbook.getStylesSource());
|
||||
public void testLoadExisting() throws IOException {
|
||||
try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile)) {
|
||||
assertNotNull(workbook.getStylesSource());
|
||||
|
||||
StylesTable st = workbook.getStylesSource();
|
||||
StylesTable st = workbook.getStylesSource();
|
||||
|
||||
doTestExisting(st);
|
||||
|
||||
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
|
||||
doTestExisting(st);
|
||||
|
||||
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadSaveLoad() {
|
||||
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile);
|
||||
assertNotNull(workbook.getStylesSource());
|
||||
public void testLoadSaveLoad() throws IOException {
|
||||
try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile)) {
|
||||
assertNotNull(workbook.getStylesSource());
|
||||
|
||||
StylesTable st = workbook.getStylesSource();
|
||||
doTestExisting(st);
|
||||
StylesTable st = workbook.getStylesSource();
|
||||
doTestExisting(st);
|
||||
|
||||
st = XSSFTestDataSamples.writeOutAndReadBack(workbook).getStylesSource();
|
||||
doTestExisting(st);
|
||||
st = XSSFTestDataSamples.writeOutAndReadBack(workbook).getStylesSource();
|
||||
doTestExisting(st);
|
||||
}
|
||||
}
|
||||
|
||||
public void doTestExisting(StylesTable st) {
|
||||
@ -128,61 +131,63 @@ public final class TestStylesTable {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void populateNew() {
|
||||
XSSFWorkbook wb = new XSSFWorkbook();
|
||||
StylesTable st = wb.getStylesSource();
|
||||
public void populateNew() throws IOException {
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook()) {
|
||||
StylesTable st = wb.getStylesSource();
|
||||
|
||||
assertNotNull(st.getCTStylesheet());
|
||||
assertEquals(1, st._getXfsSize());
|
||||
assertEquals(1, st._getStyleXfsSize());
|
||||
assertEquals(0, st.getNumDataFormats());
|
||||
assertNotNull(st.getCTStylesheet());
|
||||
assertEquals(1, st._getXfsSize());
|
||||
assertEquals(1, st._getStyleXfsSize());
|
||||
assertEquals(0, st.getNumDataFormats());
|
||||
|
||||
int nf1 = st.putNumberFormat("yyyy-mm-dd");
|
||||
int nf2 = st.putNumberFormat("yyyy-mm-DD");
|
||||
assertEquals(nf1, st.putNumberFormat("yyyy-mm-dd"));
|
||||
int nf1 = st.putNumberFormat("yyyy-mm-dd");
|
||||
int nf2 = st.putNumberFormat("yyyy-mm-DD");
|
||||
assertEquals(nf1, st.putNumberFormat("yyyy-mm-dd"));
|
||||
|
||||
st.putStyle(new XSSFCellStyle(st));
|
||||
st.putStyle(new XSSFCellStyle(st));
|
||||
|
||||
// Save and re-load
|
||||
st = XSSFTestDataSamples.writeOutAndReadBack(wb).getStylesSource();
|
||||
// Save and re-load
|
||||
st = XSSFTestDataSamples.writeOutAndReadBack(wb).getStylesSource();
|
||||
|
||||
assertNotNull(st.getCTStylesheet());
|
||||
assertEquals(2, st._getXfsSize());
|
||||
assertEquals(1, st._getStyleXfsSize());
|
||||
assertEquals(2, st.getNumDataFormats());
|
||||
assertNotNull(st.getCTStylesheet());
|
||||
assertEquals(2, st._getXfsSize());
|
||||
assertEquals(1, st._getStyleXfsSize());
|
||||
assertEquals(2, st.getNumDataFormats());
|
||||
|
||||
assertEquals("yyyy-mm-dd", st.getNumberFormatAt((short)nf1));
|
||||
assertEquals(nf1, st.putNumberFormat("yyyy-mm-dd"));
|
||||
assertEquals(nf2, st.putNumberFormat("yyyy-mm-DD"));
|
||||
|
||||
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
|
||||
assertEquals("yyyy-mm-dd", st.getNumberFormatAt((short) nf1));
|
||||
assertEquals(nf1, st.putNumberFormat("yyyy-mm-dd"));
|
||||
assertEquals(nf2, st.putNumberFormat("yyyy-mm-DD"));
|
||||
|
||||
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void populateExisting() {
|
||||
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile);
|
||||
assertNotNull(workbook.getStylesSource());
|
||||
public void populateExisting() throws IOException {
|
||||
try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile)) {
|
||||
assertNotNull(workbook.getStylesSource());
|
||||
|
||||
StylesTable st = workbook.getStylesSource();
|
||||
assertEquals(11, st._getXfsSize());
|
||||
assertEquals(1, st._getStyleXfsSize());
|
||||
assertEquals(8, st.getNumDataFormats());
|
||||
StylesTable st = workbook.getStylesSource();
|
||||
assertEquals(11, st._getXfsSize());
|
||||
assertEquals(1, st._getStyleXfsSize());
|
||||
assertEquals(8, st.getNumDataFormats());
|
||||
|
||||
int nf1 = st.putNumberFormat("YYYY-mm-dd");
|
||||
int nf2 = st.putNumberFormat("YYYY-mm-DD");
|
||||
assertEquals(nf1, st.putNumberFormat("YYYY-mm-dd"));
|
||||
int nf1 = st.putNumberFormat("YYYY-mm-dd");
|
||||
int nf2 = st.putNumberFormat("YYYY-mm-DD");
|
||||
assertEquals(nf1, st.putNumberFormat("YYYY-mm-dd"));
|
||||
|
||||
st = XSSFTestDataSamples.writeOutAndReadBack(workbook).getStylesSource();
|
||||
st = XSSFTestDataSamples.writeOutAndReadBack(workbook).getStylesSource();
|
||||
|
||||
assertEquals(11, st._getXfsSize());
|
||||
assertEquals(1, st._getStyleXfsSize());
|
||||
assertEquals(10, st.getNumDataFormats());
|
||||
assertEquals(11, st._getXfsSize());
|
||||
assertEquals(1, st._getStyleXfsSize());
|
||||
assertEquals(10, st.getNumDataFormats());
|
||||
|
||||
assertEquals("YYYY-mm-dd", st.getNumberFormatAt((short)nf1));
|
||||
assertEquals(nf1, st.putNumberFormat("YYYY-mm-dd"));
|
||||
assertEquals(nf2, st.putNumberFormat("YYYY-mm-DD"));
|
||||
|
||||
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
|
||||
assertEquals("YYYY-mm-dd", st.getNumberFormatAt((short) nf1));
|
||||
assertEquals(nf1, st.putNumberFormat("YYYY-mm-dd"));
|
||||
assertEquals(nf2, st.putNumberFormat("YYYY-mm-DD"));
|
||||
|
||||
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -298,7 +303,6 @@ public final class TestStylesTable {
|
||||
|
||||
@Test
|
||||
public void addDataFormatsBeyondUpperLimit() throws IOException {
|
||||
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook()) {
|
||||
StylesTable styles = wb.getStylesSource();
|
||||
styles.setMaxNumberOfDataFormats(0);
|
||||
@ -317,7 +321,6 @@ public final class TestStylesTable {
|
||||
|
||||
@Test
|
||||
public void decreaseUpperLimitBelowCurrentNumDataFormats() throws IOException {
|
||||
|
||||
try (XSSFWorkbook wb = new XSSFWorkbook()) {
|
||||
StylesTable styles = wb.getStylesSource();
|
||||
styles.putNumberFormat(customDataFormat);
|
||||
@ -335,27 +338,29 @@ public final class TestStylesTable {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadWithAlternateContent() {
|
||||
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("style-alternate-content.xlsx");
|
||||
assertNotNull(workbook.getStylesSource());
|
||||
public void testLoadWithAlternateContent() throws IOException {
|
||||
try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("style-alternate-content.xlsx")) {
|
||||
assertNotNull(workbook.getStylesSource());
|
||||
|
||||
StylesTable st = workbook.getStylesSource();
|
||||
assertNotNull(st);
|
||||
StylesTable st = workbook.getStylesSource();
|
||||
assertNotNull(st);
|
||||
|
||||
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
|
||||
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReplaceStyle() {
|
||||
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("style-alternate-content.xlsx");
|
||||
assertNotNull(workbook.getStylesSource());
|
||||
public void testReplaceStyle() throws IOException {
|
||||
try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("style-alternate-content.xlsx")) {
|
||||
assertNotNull(workbook.getStylesSource());
|
||||
|
||||
StylesTable st = workbook.getStylesSource();
|
||||
assertNotNull(st);
|
||||
StylesTable st = workbook.getStylesSource();
|
||||
assertNotNull(st);
|
||||
|
||||
st.replaceCellStyleXfAt(0, st.getCellStyleXfAt(1));
|
||||
st.replaceCellStyleXfAt(1, st.getCellStyleXfAt(1));
|
||||
st.replaceCellStyleXfAt(0, st.getCellStyleXfAt(1));
|
||||
st.replaceCellStyleXfAt(1, st.getCellStyleXfAt(1));
|
||||
|
||||
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
|
||||
assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,6 +24,7 @@ import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||
import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData;
|
||||
import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
|
||||
import org.apache.poi.xwpf.XWPFTestDataSamples;
|
||||
import org.junit.Test;
|
||||
import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
|
||||
import org.openxmlformats.schemas.drawingml.x2006.chart.CTTitle;
|
||||
import org.openxmlformats.schemas.drawingml.x2006.chart.CTTx;
|
||||
@ -31,20 +32,23 @@ import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
|
||||
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
|
||||
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class TestXWPFChart extends TestCase {
|
||||
public class TestXWPFChart {
|
||||
|
||||
/**
|
||||
* test method to check charts are not null
|
||||
*/
|
||||
@Test
|
||||
public void testRead() throws IOException {
|
||||
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("61745.docx");
|
||||
List<XWPFChart> charts = sampleDoc.getCharts();
|
||||
assertNotNull(charts);
|
||||
assertEquals(2, charts.size());
|
||||
checkData(charts.get(0));
|
||||
checkData(charts.get(1));
|
||||
try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("61745.docx")) {
|
||||
List<XWPFChart> charts = sampleDoc.getCharts();
|
||||
assertNotNull(charts);
|
||||
assertEquals(2, charts.size());
|
||||
checkData(charts.get(0));
|
||||
checkData(charts.get(1));
|
||||
}
|
||||
}
|
||||
|
||||
private void checkData(XWPFChart chart) {
|
||||
@ -58,58 +62,63 @@ public class TestXWPFChart extends TestCase {
|
||||
/**
|
||||
* test method to add chart title and check whether it's set
|
||||
*/
|
||||
@Test
|
||||
public void testChartTitle() throws IOException {
|
||||
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("61745.docx");
|
||||
List<XWPFChart> charts = sampleDoc.getCharts();
|
||||
XWPFChart chart = charts.get(0);
|
||||
CTChart ctChart = chart.getCTChart();
|
||||
CTTitle title = ctChart.getTitle();
|
||||
CTTx tx = title.addNewTx();
|
||||
CTTextBody rich = tx.addNewRich();
|
||||
rich.addNewBodyPr();
|
||||
rich.addNewLstStyle();
|
||||
CTTextParagraph p = rich.addNewP();
|
||||
CTRegularTextRun r = p.addNewR();
|
||||
r.addNewRPr();
|
||||
r.setT("XWPF CHART");
|
||||
assertEquals("XWPF CHART", chart.getCTChart().getTitle().getTx().getRich().getPArray(0).getRArray(0).getT());
|
||||
try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("61745.docx")) {
|
||||
List<XWPFChart> charts = sampleDoc.getCharts();
|
||||
XWPFChart chart = charts.get(0);
|
||||
CTChart ctChart = chart.getCTChart();
|
||||
CTTitle title = ctChart.getTitle();
|
||||
CTTx tx = title.addNewTx();
|
||||
CTTextBody rich = tx.addNewRich();
|
||||
rich.addNewBodyPr();
|
||||
rich.addNewLstStyle();
|
||||
CTTextParagraph p = rich.addNewP();
|
||||
CTRegularTextRun r = p.addNewR();
|
||||
r.addNewRPr();
|
||||
r.setT("XWPF CHART");
|
||||
assertEquals("XWPF CHART", chart.getCTChart().getTitle().getTx().getRich().getPArray(0).getRArray(0).getT());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* test method to check relationship
|
||||
*/
|
||||
@Test
|
||||
public void testChartRelation() throws IOException {
|
||||
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("61745.docx");
|
||||
List<XWPFChart> charts = sampleDoc.getCharts();
|
||||
XWPFChart chart = charts.get(0);
|
||||
assertEquals(XWPFRelation.CHART.getContentType(), chart.getPackagePart().getContentType());
|
||||
assertEquals("/word/document.xml", chart.getParent().getPackagePart().getPartName().getName());
|
||||
assertEquals("/word/charts/chart1.xml", chart.getPackagePart().getPartName().getName());
|
||||
try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("61745.docx")) {
|
||||
List<XWPFChart> charts = sampleDoc.getCharts();
|
||||
XWPFChart chart = charts.get(0);
|
||||
assertEquals(XWPFRelation.CHART.getContentType(), chart.getPackagePart().getContentType());
|
||||
assertEquals("/word/document.xml", chart.getParent().getPackagePart().getPartName().getName());
|
||||
assertEquals("/word/charts/chart1.xml", chart.getPackagePart().getPartName().getName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* test method to check adding chart in document
|
||||
*/
|
||||
public static void testAddChartsToNewDocument() throws InvalidFormatException, IOException {
|
||||
@Test
|
||||
public void testAddChartsToNewDocument() throws InvalidFormatException, IOException {
|
||||
try (XWPFDocument document = new XWPFDocument()) {
|
||||
|
||||
XWPFDocument document = new XWPFDocument();
|
||||
XWPFChart chart = document.createChart();
|
||||
assertEquals(1, document.getCharts().size());
|
||||
assertNotNull(chart);
|
||||
assertNotNull(chart.getCTChartSpace());
|
||||
assertNotNull(chart.getCTChart());
|
||||
assertEquals(XWPFChart.DEFAULT_HEIGHT, chart.getChartHeight());
|
||||
assertEquals(XWPFChart.DEFAULT_WIDTH, chart.getChartWidth());
|
||||
|
||||
XWPFChart chart = document.createChart();
|
||||
assertEquals(1, document.getCharts().size());
|
||||
assertNotNull(chart);
|
||||
assertNotNull(chart.getCTChartSpace());
|
||||
assertNotNull(chart.getCTChart());
|
||||
assertEquals(XWPFChart.DEFAULT_HEIGHT, chart.getChartHeight());
|
||||
assertEquals(XWPFChart.DEFAULT_WIDTH, chart.getChartWidth());
|
||||
XWPFChart chart2 = document.createChart();
|
||||
assertEquals(2, document.getCharts().size());
|
||||
assertNotNull(chart2);
|
||||
assertNotNull(chart2.getCTChartSpace());
|
||||
assertNotNull(chart2.getCTChart());
|
||||
chart.setChartHeight(500500);
|
||||
assertEquals(500500, chart.getChartHeight());
|
||||
|
||||
XWPFChart chart2 = document.createChart();
|
||||
assertEquals(2, document.getCharts().size());
|
||||
assertNotNull(chart2);
|
||||
assertNotNull(chart2.getCTChartSpace());
|
||||
assertNotNull(chart2.getCTChart());
|
||||
chart.setChartHeight(500500);
|
||||
assertEquals(500500, chart.getChartHeight());
|
||||
|
||||
assertNotNull(XWPFTestDataSamples.writeOutAndReadBack(document));
|
||||
assertNotNull(XWPFTestDataSamples.writeOutAndReadBack(document));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,61 +22,66 @@ import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.poi.xwpf.XWPFTestDataSamples;
|
||||
import org.junit.Test;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STFtnEdn;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
public class TestXWPFFootnotes extends TestCase {
|
||||
|
||||
public class TestXWPFFootnotes {
|
||||
@Test
|
||||
public void testCreateFootnotes() throws IOException{
|
||||
XWPFDocument docOut = new XWPFDocument();
|
||||
try (XWPFDocument docOut = new XWPFDocument()) {
|
||||
|
||||
XWPFAbstractFootnotesEndnotes footnotes = docOut.createFootnotes();
|
||||
|
||||
assertNotNull(footnotes);
|
||||
|
||||
XWPFAbstractFootnotesEndnotes secondFootnotes = docOut.createFootnotes();
|
||||
|
||||
assertSame(footnotes, secondFootnotes);
|
||||
|
||||
docOut.close();
|
||||
XWPFAbstractFootnotesEndnotes footnotes = docOut.createFootnotes();
|
||||
|
||||
assertNotNull(footnotes);
|
||||
|
||||
XWPFAbstractFootnotesEndnotes secondFootnotes = docOut.createFootnotes();
|
||||
|
||||
assertSame(footnotes, secondFootnotes);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddFootnotesToDocument() throws IOException {
|
||||
XWPFDocument docOut = new XWPFDocument();
|
||||
try (XWPFDocument docOut = new XWPFDocument()) {
|
||||
|
||||
// NOTE: XWPFDocument.createFootnote() delegates directly
|
||||
// to XWPFFootnotes.createFootnote() so this tests
|
||||
// both creation of new XWPFFootnotes in document
|
||||
// and XWPFFootnotes.createFootnote();
|
||||
XWPFFootnote footnote = docOut.createFootnote();
|
||||
BigInteger noteId = footnote.getId();
|
||||
// NOTE: XWPFDocument.createFootnote() delegates directly
|
||||
// to XWPFFootnotes.createFootnote() so this tests
|
||||
// both creation of new XWPFFootnotes in document
|
||||
// and XWPFFootnotes.createFootnote();
|
||||
XWPFFootnote footnote = docOut.createFootnote();
|
||||
BigInteger noteId = footnote.getId();
|
||||
|
||||
XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut);
|
||||
XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut);
|
||||
|
||||
XWPFFootnote note = docIn.getFootnoteByID(noteId.intValue());
|
||||
assertNotNull(note);
|
||||
assertEquals(STFtnEdn.NORMAL, note.getCTFtnEdn().getType());
|
||||
XWPFFootnote note = docIn.getFootnoteByID(noteId.intValue());
|
||||
assertNotNull(note);
|
||||
assertEquals(STFtnEdn.NORMAL, note.getCTFtnEdn().getType());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bug 55066 - avoid double loading the footnotes
|
||||
*/
|
||||
@Test
|
||||
public void testLoadFootnotesOnce() throws IOException {
|
||||
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54849.docx");
|
||||
List<XWPFFootnote> footnotes = doc.getFootnotes();
|
||||
int hits = 0;
|
||||
for (XWPFFootnote fn : footnotes) {
|
||||
for (IBodyElement e : fn.getBodyElements()) {
|
||||
if (e instanceof XWPFParagraph) {
|
||||
String txt = ((XWPFParagraph) e).getText();
|
||||
if (txt.contains("Footnote_sdt")) {
|
||||
hits++;
|
||||
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54849.docx")) {
|
||||
List<XWPFFootnote> footnotes = doc.getFootnotes();
|
||||
int hits = 0;
|
||||
for (XWPFFootnote fn : footnotes) {
|
||||
for (IBodyElement e : fn.getBodyElements()) {
|
||||
if (e instanceof XWPFParagraph) {
|
||||
String txt = ((XWPFParagraph) e).getText();
|
||||
if (txt.contains("Footnote_sdt")) {
|
||||
hits++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assertEquals("Load footnotes once", 1, hits);
|
||||
}
|
||||
assertEquals("Load footnotes once", 1, hits);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -34,171 +34,175 @@ public final class TestXWPFHeader {
|
||||
|
||||
@Test
|
||||
public void testSimpleHeader() throws IOException {
|
||||
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerFooter.docx");
|
||||
try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerFooter.docx")) {
|
||||
|
||||
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
|
||||
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
|
||||
|
||||
XWPFHeader header = policy.getDefaultHeader();
|
||||
XWPFFooter footer = policy.getDefaultFooter();
|
||||
assertNotNull(header);
|
||||
assertNotNull(footer);
|
||||
XWPFHeader header = policy.getDefaultHeader();
|
||||
XWPFFooter footer = policy.getDefaultFooter();
|
||||
assertNotNull(header);
|
||||
assertNotNull(footer);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImageInHeader() throws IOException {
|
||||
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx");
|
||||
try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx")) {
|
||||
|
||||
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
|
||||
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
|
||||
|
||||
XWPFHeader header = policy.getDefaultHeader();
|
||||
XWPFHeader header = policy.getDefaultHeader();
|
||||
|
||||
assertNotNull(header.getRelations());
|
||||
assertEquals(1, header.getRelations().size());
|
||||
assertNotNull(header.getRelations());
|
||||
assertEquals(1, header.getRelations().size());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetHeader() throws IOException {
|
||||
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx");
|
||||
// no header is set (yet)
|
||||
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
|
||||
assertNull(policy.getDefaultHeader());
|
||||
assertNull(policy.getFirstPageHeader());
|
||||
assertNull(policy.getDefaultFooter());
|
||||
assertNull(policy.getFirstPageFooter());
|
||||
try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx")) {
|
||||
// no header is set (yet)
|
||||
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
|
||||
assertNull(policy.getDefaultHeader());
|
||||
assertNull(policy.getFirstPageHeader());
|
||||
assertNull(policy.getDefaultFooter());
|
||||
assertNull(policy.getFirstPageFooter());
|
||||
|
||||
CTP ctP1 = CTP.Factory.newInstance();
|
||||
CTR ctR1 = ctP1.addNewR();
|
||||
CTText t = ctR1.addNewT();
|
||||
String tText = "Paragraph in header";
|
||||
t.setStringValue(tText);
|
||||
CTP ctP1 = CTP.Factory.newInstance();
|
||||
CTR ctR1 = ctP1.addNewR();
|
||||
CTText t = ctR1.addNewT();
|
||||
String tText = "Paragraph in header";
|
||||
t.setStringValue(tText);
|
||||
|
||||
// Commented MB 23 May 2010
|
||||
//CTP ctP2 = CTP.Factory.newInstance();
|
||||
//CTR ctR2 = ctP2.addNewR();
|
||||
//CTText t2 = ctR2.addNewT();
|
||||
//t2.setStringValue("Second paragraph.. for footer");
|
||||
// Commented MB 23 May 2010
|
||||
//CTP ctP2 = CTP.Factory.newInstance();
|
||||
//CTR ctR2 = ctP2.addNewR();
|
||||
//CTText t2 = ctR2.addNewT();
|
||||
//t2.setStringValue("Second paragraph.. for footer");
|
||||
|
||||
// Create two paragraphs for insertion into the footer.
|
||||
// Previously only one was inserted MB 23 May 2010
|
||||
CTP ctP2 = CTP.Factory.newInstance();
|
||||
CTR ctR2 = ctP2.addNewR();
|
||||
CTText t2 = ctR2.addNewT();
|
||||
t2.setStringValue("First paragraph for the footer");
|
||||
// Create two paragraphs for insertion into the footer.
|
||||
// Previously only one was inserted MB 23 May 2010
|
||||
CTP ctP2 = CTP.Factory.newInstance();
|
||||
CTR ctR2 = ctP2.addNewR();
|
||||
CTText t2 = ctR2.addNewT();
|
||||
t2.setStringValue("First paragraph for the footer");
|
||||
|
||||
CTP ctP3 = CTP.Factory.newInstance();
|
||||
CTR ctR3 = ctP3.addNewR();
|
||||
CTText t3 = ctR3.addNewT();
|
||||
t3.setStringValue("Second paragraph for the footer");
|
||||
CTP ctP3 = CTP.Factory.newInstance();
|
||||
CTR ctR3 = ctP3.addNewR();
|
||||
CTText t3 = ctR3.addNewT();
|
||||
t3.setStringValue("Second paragraph for the footer");
|
||||
|
||||
XWPFParagraph p1 = new XWPFParagraph(ctP1, sampleDoc);
|
||||
XWPFParagraph[] pars = new XWPFParagraph[1];
|
||||
pars[0] = p1;
|
||||
XWPFParagraph p1 = new XWPFParagraph(ctP1, sampleDoc);
|
||||
XWPFParagraph[] pars = new XWPFParagraph[1];
|
||||
pars[0] = p1;
|
||||
|
||||
XWPFParagraph p2 = new XWPFParagraph(ctP2, sampleDoc);
|
||||
XWPFParagraph p3 = new XWPFParagraph(ctP3, sampleDoc);
|
||||
XWPFParagraph[] pars2 = new XWPFParagraph[2];
|
||||
pars2[0] = p2;
|
||||
pars2[1] = p3;
|
||||
XWPFParagraph p2 = new XWPFParagraph(ctP2, sampleDoc);
|
||||
XWPFParagraph p3 = new XWPFParagraph(ctP3, sampleDoc);
|
||||
XWPFParagraph[] pars2 = new XWPFParagraph[2];
|
||||
pars2[0] = p2;
|
||||
pars2[1] = p3;
|
||||
|
||||
// Set headers
|
||||
XWPFHeader headerD = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, pars);
|
||||
XWPFHeader headerF = policy.createHeader(XWPFHeaderFooterPolicy.FIRST);
|
||||
// Set a default footer and capture the returned XWPFFooter object.
|
||||
XWPFFooter footerD = policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, pars2);
|
||||
XWPFFooter footerF = policy.createFooter(XWPFHeaderFooterPolicy.FIRST);
|
||||
// Set headers
|
||||
XWPFHeader headerD = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, pars);
|
||||
XWPFHeader headerF = policy.createHeader(XWPFHeaderFooterPolicy.FIRST);
|
||||
// Set a default footer and capture the returned XWPFFooter object.
|
||||
XWPFFooter footerD = policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, pars2);
|
||||
XWPFFooter footerF = policy.createFooter(XWPFHeaderFooterPolicy.FIRST);
|
||||
|
||||
// Ensure the headers and footer were set correctly....
|
||||
assertNotNull(policy.getDefaultHeader());
|
||||
assertNotNull(policy.getFirstPageHeader());
|
||||
assertNotNull(policy.getDefaultFooter());
|
||||
assertNotNull(policy.getFirstPageFooter());
|
||||
// ....and that the footer object captured above contains two
|
||||
// paragraphs of text.
|
||||
assertEquals(2, footerD.getParagraphs().size());
|
||||
assertEquals(0, footerF.getParagraphs().size());
|
||||
// Ensure the headers and footer were set correctly....
|
||||
assertNotNull(policy.getDefaultHeader());
|
||||
assertNotNull(policy.getFirstPageHeader());
|
||||
assertNotNull(policy.getDefaultFooter());
|
||||
assertNotNull(policy.getFirstPageFooter());
|
||||
// ....and that the footer object captured above contains two
|
||||
// paragraphs of text.
|
||||
assertEquals(2, footerD.getParagraphs().size());
|
||||
assertEquals(0, footerF.getParagraphs().size());
|
||||
|
||||
// Check the header created with the paragraph got them, and the one
|
||||
// created without got none
|
||||
assertEquals(1, headerD.getParagraphs().size());
|
||||
assertEquals(tText, headerD.getParagraphs().get(0).getText());
|
||||
// Check the header created with the paragraph got them, and the one
|
||||
// created without got none
|
||||
assertEquals(1, headerD.getParagraphs().size());
|
||||
assertEquals(tText, headerD.getParagraphs().get(0).getText());
|
||||
|
||||
assertEquals(0, headerF.getParagraphs().size());
|
||||
assertEquals(0, headerF.getParagraphs().size());
|
||||
|
||||
// As an additional check, recover the defauls footer and
|
||||
// make sure that it contains two paragraphs of text and that
|
||||
// both do hold what is expected.
|
||||
footerD = policy.getDefaultFooter();
|
||||
XWPFParagraph[] paras = footerD.getParagraphs().toArray(new XWPFParagraph[0]);
|
||||
// As an additional check, recover the defauls footer and
|
||||
// make sure that it contains two paragraphs of text and that
|
||||
// both do hold what is expected.
|
||||
footerD = policy.getDefaultFooter();
|
||||
XWPFParagraph[] paras = footerD.getParagraphs().toArray(new XWPFParagraph[0]);
|
||||
|
||||
assertEquals(2, paras.length);
|
||||
assertEquals("First paragraph for the footer", paras[0].getText());
|
||||
assertEquals("Second paragraph for the footer", paras[1].getText());
|
||||
assertEquals(2, paras.length);
|
||||
assertEquals("First paragraph for the footer", paras[0].getText());
|
||||
assertEquals("Second paragraph for the footer", paras[1].getText());
|
||||
|
||||
|
||||
// Add some text to the empty header
|
||||
String fText1 = "New Text!";
|
||||
String fText2 = "More Text!";
|
||||
headerF.createParagraph().insertNewRun(0).setText(fText1);
|
||||
headerF.createParagraph().insertNewRun(0).setText(fText2);
|
||||
// Add some text to the empty header
|
||||
String fText1 = "New Text!";
|
||||
String fText2 = "More Text!";
|
||||
headerF.createParagraph().insertNewRun(0).setText(fText1);
|
||||
headerF.createParagraph().insertNewRun(0).setText(fText2);
|
||||
// headerF.getParagraphs().get(0).insertNewRun(0).setText(fText1);
|
||||
|
||||
// Check it
|
||||
assertEquals(tText, headerD.getParagraphs().get(0).getText());
|
||||
assertEquals(fText1, headerF.getParagraphs().get(0).getText());
|
||||
assertEquals(fText2, headerF.getParagraphs().get(1).getText());
|
||||
|
||||
// Check it
|
||||
assertEquals(tText, headerD.getParagraphs().get(0).getText());
|
||||
assertEquals(fText1, headerF.getParagraphs().get(0).getText());
|
||||
assertEquals(fText2, headerF.getParagraphs().get(1).getText());
|
||||
|
||||
|
||||
// Save, re-open, ensure it's all still there
|
||||
XWPFDocument reopened = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
|
||||
policy = reopened.getHeaderFooterPolicy();
|
||||
assertNotNull(policy.getDefaultHeader());
|
||||
assertNotNull(policy.getFirstPageHeader());
|
||||
assertNull(policy.getEvenPageHeader());
|
||||
assertNotNull(policy.getDefaultFooter());
|
||||
assertNotNull(policy.getFirstPageFooter());
|
||||
assertNull(policy.getEvenPageFooter());
|
||||
// Save, re-open, ensure it's all still there
|
||||
XWPFDocument reopened = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
|
||||
policy = reopened.getHeaderFooterPolicy();
|
||||
assertNotNull(policy.getDefaultHeader());
|
||||
assertNotNull(policy.getFirstPageHeader());
|
||||
assertNull(policy.getEvenPageHeader());
|
||||
assertNotNull(policy.getDefaultFooter());
|
||||
assertNotNull(policy.getFirstPageFooter());
|
||||
assertNull(policy.getEvenPageFooter());
|
||||
|
||||
// Check the new headers still have their text
|
||||
headerD = policy.getDefaultHeader();
|
||||
headerF = policy.getFirstPageHeader();
|
||||
assertEquals(tText, headerD.getParagraphs().get(0).getText());
|
||||
assertEquals(fText1, headerF.getParagraphs().get(0).getText());
|
||||
assertEquals(fText2, headerF.getParagraphs().get(1).getText());
|
||||
// Check the new headers still have their text
|
||||
headerD = policy.getDefaultHeader();
|
||||
headerF = policy.getFirstPageHeader();
|
||||
assertEquals(tText, headerD.getParagraphs().get(0).getText());
|
||||
assertEquals(fText1, headerF.getParagraphs().get(0).getText());
|
||||
assertEquals(fText2, headerF.getParagraphs().get(1).getText());
|
||||
|
||||
// Check the new footers have their new text too
|
||||
footerD = policy.getDefaultFooter();
|
||||
paras = footerD.getParagraphs().toArray(new XWPFParagraph[0]);
|
||||
footerF = policy.getFirstPageFooter();
|
||||
// Check the new footers have their new text too
|
||||
footerD = policy.getDefaultFooter();
|
||||
paras = footerD.getParagraphs().toArray(new XWPFParagraph[0]);
|
||||
footerF = policy.getFirstPageFooter();
|
||||
|
||||
assertEquals(2, paras.length);
|
||||
assertEquals("First paragraph for the footer", paras[0].getText());
|
||||
assertEquals("Second paragraph for the footer", paras[1].getText());
|
||||
assertEquals(1, footerF.getParagraphs().size());
|
||||
assertEquals(2, paras.length);
|
||||
assertEquals("First paragraph for the footer", paras[0].getText());
|
||||
assertEquals("Second paragraph for the footer", paras[1].getText());
|
||||
assertEquals(1, footerF.getParagraphs().size());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetWatermark() throws IOException {
|
||||
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx");
|
||||
try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx")) {
|
||||
|
||||
// No header is set (yet)
|
||||
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
|
||||
assertNull(policy.getDefaultHeader());
|
||||
assertNull(policy.getFirstPageHeader());
|
||||
assertNull(policy.getDefaultFooter());
|
||||
// No header is set (yet)
|
||||
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
|
||||
assertNull(policy.getDefaultHeader());
|
||||
assertNull(policy.getFirstPageHeader());
|
||||
assertNull(policy.getDefaultFooter());
|
||||
|
||||
policy.createWatermark("DRAFT");
|
||||
policy.createWatermark("DRAFT");
|
||||
|
||||
assertNotNull(policy.getDefaultHeader());
|
||||
assertNotNull(policy.getFirstPageHeader());
|
||||
assertNotNull(policy.getEvenPageHeader());
|
||||
assertNotNull(policy.getDefaultHeader());
|
||||
assertNotNull(policy.getFirstPageHeader());
|
||||
assertNotNull(policy.getEvenPageHeader());
|
||||
|
||||
// Re-open, and check
|
||||
XWPFDocument reopened = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
|
||||
policy = reopened.getHeaderFooterPolicy();
|
||||
// Re-open, and check
|
||||
XWPFDocument reopened = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
|
||||
policy = reopened.getHeaderFooterPolicy();
|
||||
|
||||
assertNotNull(policy.getDefaultHeader());
|
||||
assertNotNull(policy.getFirstPageHeader());
|
||||
assertNotNull(policy.getEvenPageHeader());
|
||||
assertNotNull(policy.getDefaultHeader());
|
||||
assertNotNull(policy.getFirstPageHeader());
|
||||
assertNotNull(policy.getEvenPageHeader());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -224,7 +228,8 @@ public final class TestXWPFHeader {
|
||||
@Test
|
||||
public void bug60293() throws Exception {
|
||||
//test handling of non-standard header/footer options
|
||||
XWPFDocument xwpf = XWPFTestDataSamples.openSampleDocument("60293.docx");
|
||||
assertEquals(3, xwpf.getHeaderList().size());
|
||||
try (XWPFDocument xwpf = XWPFTestDataSamples.openSampleDocument("60293.docx")) {
|
||||
assertEquals(3, xwpf.getHeaderList().size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,31 +18,36 @@ package org.apache.poi.xwpf.usermodel;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.poi.xwpf.XWPFTestDataSamples;
|
||||
import org.apache.xmlbeans.XmlException;
|
||||
import org.junit.Test;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
/**
|
||||
* @author Paolo Mottadelli
|
||||
*/
|
||||
public final class TestXWPFHeadings extends TestCase {
|
||||
public final class TestXWPFHeadings {
|
||||
private static final String HEADING1 = "Heading1";
|
||||
|
||||
public void testSetParagraphStyle() throws IOException, XmlException {
|
||||
@Test
|
||||
public void testSetParagraphStyle() throws IOException {
|
||||
//new clean instance of paragraph
|
||||
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("heading123.docx");
|
||||
XWPFParagraph p = doc.createParagraph();
|
||||
XWPFRun run = p.createRun();
|
||||
run.setText("Heading 1");
|
||||
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("heading123.docx")) {
|
||||
XWPFParagraph p = doc.createParagraph();
|
||||
XWPFRun run = p.createRun();
|
||||
run.setText("Heading 1");
|
||||
|
||||
CTSdtBlock block = doc.getDocument().getBody().addNewSdt();
|
||||
CTSdtBlock block = doc.getDocument().getBody().addNewSdt();
|
||||
assertNotNull(block);
|
||||
|
||||
assertNull(p.getStyle());
|
||||
p.setStyle(HEADING1);
|
||||
assertEquals(HEADING1, p.getCTP().getPPr().getPStyle().getVal());
|
||||
assertNull(p.getStyle());
|
||||
p.setStyle(HEADING1);
|
||||
assertEquals(HEADING1, p.getCTP().getPPr().getPStyle().getVal());
|
||||
|
||||
doc.createTOC();
|
||||
doc.createTOC();
|
||||
/*
|
||||
// TODO - finish this test
|
||||
if (false) {
|
||||
@ -59,5 +64,6 @@ public final class TestXWPFHeadings extends TestCase {
|
||||
out.close();
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -28,15 +28,16 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumLvl;
|
||||
public class TestXWPFNumbering extends TestCase {
|
||||
|
||||
public void testCompareAbstractNum() throws IOException {
|
||||
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Numbering.docx");
|
||||
XWPFNumbering numbering = doc.getNumbering();
|
||||
BigInteger numId = BigInteger.valueOf(1);
|
||||
assertTrue(numbering.numExist(numId));
|
||||
XWPFNum num = numbering.getNum(numId);
|
||||
BigInteger abstrNumId = num.getCTNum().getAbstractNumId().getVal();
|
||||
XWPFAbstractNum abstractNum = numbering.getAbstractNum(abstrNumId);
|
||||
BigInteger compareAbstractNum = numbering.getIdOfAbstractNum(abstractNum);
|
||||
assertEquals(abstrNumId, compareAbstractNum);
|
||||
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Numbering.docx")) {
|
||||
XWPFNumbering numbering = doc.getNumbering();
|
||||
BigInteger numId = BigInteger.valueOf(1);
|
||||
assertTrue(numbering.numExist(numId));
|
||||
XWPFNum num = numbering.getNum(numId);
|
||||
BigInteger abstrNumId = num.getCTNum().getAbstractNumId().getVal();
|
||||
XWPFAbstractNum abstractNum = numbering.getAbstractNum(abstrNumId);
|
||||
BigInteger compareAbstractNum = numbering.getIdOfAbstractNum(abstractNum);
|
||||
assertEquals(abstrNumId, compareAbstractNum);
|
||||
}
|
||||
}
|
||||
|
||||
public void testAddNumberingToDoc() throws IOException {
|
||||
@ -58,45 +59,48 @@ public class TestXWPFNumbering extends TestCase {
|
||||
}
|
||||
|
||||
public void testGetNumIlvl() throws IOException {
|
||||
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Numbering.docx");
|
||||
BigInteger numIlvl = BigInteger.valueOf(0);
|
||||
assertEquals(numIlvl, doc.getParagraphs().get(0).getNumIlvl());
|
||||
numIlvl = BigInteger.valueOf(1);
|
||||
assertEquals(numIlvl, doc.getParagraphs().get(5).getNumIlvl());
|
||||
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Numbering.docx")) {
|
||||
BigInteger numIlvl = BigInteger.valueOf(0);
|
||||
assertEquals(numIlvl, doc.getParagraphs().get(0).getNumIlvl());
|
||||
numIlvl = BigInteger.valueOf(1);
|
||||
assertEquals(numIlvl, doc.getParagraphs().get(5).getNumIlvl());
|
||||
}
|
||||
}
|
||||
|
||||
public void testGetNumFmt() throws IOException {
|
||||
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Numbering.docx");
|
||||
assertEquals("bullet", doc.getParagraphs().get(0).getNumFmt());
|
||||
assertEquals("bullet", doc.getParagraphs().get(1).getNumFmt());
|
||||
assertEquals("bullet", doc.getParagraphs().get(2).getNumFmt());
|
||||
assertEquals("bullet", doc.getParagraphs().get(3).getNumFmt());
|
||||
assertEquals("decimal", doc.getParagraphs().get(4).getNumFmt());
|
||||
assertEquals("lowerLetter", doc.getParagraphs().get(5).getNumFmt());
|
||||
assertEquals("lowerRoman", doc.getParagraphs().get(6).getNumFmt());
|
||||
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Numbering.docx")) {
|
||||
assertEquals("bullet", doc.getParagraphs().get(0).getNumFmt());
|
||||
assertEquals("bullet", doc.getParagraphs().get(1).getNumFmt());
|
||||
assertEquals("bullet", doc.getParagraphs().get(2).getNumFmt());
|
||||
assertEquals("bullet", doc.getParagraphs().get(3).getNumFmt());
|
||||
assertEquals("decimal", doc.getParagraphs().get(4).getNumFmt());
|
||||
assertEquals("lowerLetter", doc.getParagraphs().get(5).getNumFmt());
|
||||
assertEquals("lowerRoman", doc.getParagraphs().get(6).getNumFmt());
|
||||
}
|
||||
}
|
||||
|
||||
public void testLvlText() throws IOException {
|
||||
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Numbering.docx");
|
||||
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Numbering.docx")) {
|
||||
|
||||
assertEquals("%1.%2.%3.", doc.getParagraphs().get(12).getNumLevelText());
|
||||
assertEquals("%1.%2.%3.", doc.getParagraphs().get(12).getNumLevelText());
|
||||
|
||||
assertEquals("NEW-%1-FORMAT", doc.getParagraphs().get(14).getNumLevelText());
|
||||
assertEquals("NEW-%1-FORMAT", doc.getParagraphs().get(14).getNumLevelText());
|
||||
|
||||
XWPFParagraph p = doc.getParagraphs().get(18);
|
||||
assertEquals("%1.", p.getNumLevelText());
|
||||
//test that null doesn't throw NPE
|
||||
assertNull(p.getNumFmt());
|
||||
XWPFParagraph p = doc.getParagraphs().get(18);
|
||||
assertEquals("%1.", p.getNumLevelText());
|
||||
//test that null doesn't throw NPE
|
||||
assertNull(p.getNumFmt());
|
||||
}
|
||||
}
|
||||
|
||||
public void testOverrideList() throws IOException {
|
||||
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("NumberingWOverrides.docx");
|
||||
XWPFParagraph p = doc.getParagraphs().get(4);
|
||||
XWPFNumbering numbering = doc.getNumbering();
|
||||
CTNum ctNum = numbering.getNum(p.getNumID()).getCTNum();
|
||||
assertEquals(9, ctNum.sizeOfLvlOverrideArray());
|
||||
CTNumLvl ctNumLvl = ctNum.getLvlOverrideArray(0);
|
||||
assertEquals("upperLetter", ctNumLvl.getLvl().getNumFmt().getVal().toString());
|
||||
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("NumberingWOverrides.docx")) {
|
||||
XWPFParagraph p = doc.getParagraphs().get(4);
|
||||
XWPFNumbering numbering = doc.getNumbering();
|
||||
CTNum ctNum = numbering.getNum(p.getNumID()).getCTNum();
|
||||
assertEquals(9, ctNum.sizeOfLvlOverrideArray());
|
||||
CTNumLvl ctNumLvl = ctNum.getLvlOverrideArray(0);
|
||||
assertEquals("upperLetter", ctNumLvl.getLvl().getNumFmt().getVal().toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user