From: Nick Burch Date: Sat, 8 Mar 2014 20:01:38 +0000 (+0000) Subject: Add more unit tests for XWPF Headers and Footers X-Git-Tag: REL_3_11_BETA1~224 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0cf393794913b9ac1c6018ff398975b3cc79f3ef;p=poi.git Add more unit tests for XWPF Headers and Footers git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1575599 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java b/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java index a40e5992b0..6727f5257c 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java +++ b/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java @@ -150,8 +150,8 @@ public class XWPFHeaderFooterPolicy { } /** - * Creates an empty header of the specified type, to - * which you can then create paragraphs and set text etc. + * Creates an empty header of the specified type, containing a single + * empty paragraph, to which you can then set text, add more paragraphs etc. */ public XWPFHeader createHeader(Enum type) throws IOException { return createHeader(type, null); @@ -184,8 +184,8 @@ public class XWPFHeaderFooterPolicy { } /** - * Creates an empty footer of the specified type, to - * which you can then create paragraphs and set text etc. + * Creates an empty footer of the specified type, containing a single + * empty paragraph, to which you can then set text, add more paragraphs etc. */ public XWPFFooter createFooter(Enum type) throws IOException { return createFooter(type, null); diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java index 2a65e8e560..d2c4698d73 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java @@ -62,7 +62,8 @@ public final class TestXWPFHeader extends TestCase { CTP ctP1 = CTP.Factory.newInstance(); CTR ctR1 = ctP1.addNewR(); CTText t = ctR1.addNewT(); - t.setStringValue("Paragraph in header"); + String tText = "Paragraph in header"; + t.setStringValue(tText); // Commented MB 23 May 2010 //CTP ctP2 = CTP.Factory.newInstance(); @@ -93,8 +94,8 @@ public final class TestXWPFHeader extends TestCase { pars2[1] = p3; // Set headers - policy.createHeader(policy.DEFAULT, pars); - policy.createHeader(policy.FIRST); + XWPFHeader headerD = policy.createHeader(policy.DEFAULT, pars); + XWPFHeader headerF = policy.createHeader(policy.FIRST); // Set a default footer and capture the returned XWPFFooter object. XWPFFooter footer = policy.createFooter(policy.DEFAULT, pars2); @@ -106,20 +107,58 @@ public final class TestXWPFHeader extends TestCase { // paragraphs of text. assertEquals(2, footer.getParagraphs().size()); + // Check the header created with the paragraph got them, and the one + // created without got an empty one + assertEquals(1, headerD.getParagraphs().size()); + assertEquals(1, headerF.getParagraphs().size()); + + assertEquals(tText, headerD.getParagraphs().get(0).getText()); + assertEquals("", headerF.getParagraphs().get(0).getText()); + // 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. footer = policy.getDefaultFooter(); - - XWPFParagraph[] paras = new XWPFParagraph[footer.getParagraphs().size()]; - int i=0; - for(XWPFParagraph p : footer.getParagraphs()) { - paras[i++] = p; - } + XWPFParagraph[] paras = footer.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()); + + + // Add some text to the empty header + String fText1 = "New Text!"; + headerF.getParagraphs().get(0).insertNewRun(0).setText(fText1); + // TODO Add another paragraph and check + + // Check it + assertEquals(tText, headerD.getParagraphs().get(0).getText()); + assertEquals(fText1, headerF.getParagraphs().get(0).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()); + assertNull(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()); + + // Check the new footers have their new text too + footer = policy.getDefaultFooter(); + paras = footer.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()); } public void testSetWatermark() throws IOException {