]> source.dussan.org Git - poi.git/commitdiff
Add more unit tests for XWPF Headers and Footers
authorNick Burch <nick@apache.org>
Sat, 8 Mar 2014 20:01:38 +0000 (20:01 +0000)
committerNick Burch <nick@apache.org>
Sat, 8 Mar 2014 20:01:38 +0000 (20:01 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1575599 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java

index a40e5992b0259d3c32fa571c979fa0c990064e2a..6727f5257cedd50d6c9e1ff2b0f7c8b839ae142d 100644 (file)
@@ -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);
index 2a65e8e56007ea17d4bc43e08a2ceae75961bbc8..d2c4698d73cc1b4b788ca695c386f49f4156170a 100644 (file)
@@ -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 {