]> source.dussan.org Git - poi.git/commitdiff
POI 59030 fix NPE in XWPFTableCell's getVerticalAlignment via Prasad Babu
authorTim Allison <tallison@apache.org>
Fri, 19 Feb 2016 15:46:26 +0000 (15:46 +0000)
committerTim Allison <tallison@apache.org>
Fri, 19 Feb 2016 15:46:26 +0000 (15:46 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1731257 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFTableCell.java
test-data/document/59030.docx [new file with mode: 0644]

index 13cc7f3b7699fccaaf7f722de535485e6561af70..dd011e3679e26defbc4d963d72675a36f85a3b4e 100644 (file)
@@ -224,20 +224,23 @@ public class XWPFTableCell implements IBody, ICell {
         ctshd.setFill(rgbStr);\r
     }\r
 \r
-    /**\r
-     * Get the vertical alignment of the cell.\r
-     *\r
-     * @return the cell alignment enum value\r
-     */\r
-    public XWPFVertAlign getVerticalAlignment() {\r
-        XWPFVertAlign vAlign = null;\r
-        CTTcPr tcpr = ctTc.getTcPr();\r
-        if (tcpr != null) {\r
-            CTVerticalJc va = tcpr.getVAlign();\r
-            vAlign = stVertAlignTypeMap.get(va.getVal().intValue());\r
-        }\r
-        return vAlign;\r
-    }\r
+    /**
+     * Get the vertical alignment of the cell.
+     *
+     * @return the cell alignment enum value or <code>null</code>
+     * if no vertical alignment is set.
+     */
+    public XWPFVertAlign getVerticalAlignment() {
+        XWPFVertAlign vAlign = null;
+        CTTcPr tcpr = ctTc.getTcPr();
+        if (tcpr != null) {
+            CTVerticalJc va = tcpr.getVAlign();
+            if (va != null && va.getVal() != null) {
+                vAlign = stVertAlignTypeMap.get(va.getVal().intValue());
+            }
+        }
+        return vAlign;
+    }
 \r
     /**\r
      * Set the vertical alignment of the cell.\r
index f793742c3f5555c7e5185e45423537bf9bb41835..ab2fad84b38574e26c8a54836bfb2aea348a3050 100644 (file)
 
 package org.apache.poi.xwpf.usermodel;
 
+import java.util.List;
+
 import junit.framework.TestCase;
 
+import org.apache.poi.xwpf.XWPFTestDataSamples;
 import org.apache.poi.xwpf.usermodel.XWPFTableCell.XWPFVertAlign;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHMerge;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
@@ -103,4 +106,20 @@ public class TestXWPFTableCell extends TestCase {
         CTTcBorders tblBorders = tcPr.addNewTcBorders();
         CTVMerge vMerge = tcPr.addNewVMerge();
     }
+
+    public void testCellVerticalAlign() throws Exception{
+        XWPFDocument docx = XWPFTestDataSamples.openSampleDocument("59030.docx");
+        List<XWPFTable> tables = docx.getTables();
+        assertEquals(1, tables.size());
+
+        XWPFTable table = tables.get(0);
+
+        List<XWPFTableRow> tableRows = table.getRows();
+        assertEquals(2, tableRows.size());
+
+        assertNull(tableRows.get(0).getCell(0).getVerticalAlignment());
+        assertEquals(XWPFVertAlign.BOTTOM, tableRows.get(0).getCell(1).getVerticalAlignment());
+        assertEquals(XWPFVertAlign.CENTER, tableRows.get(1).getCell(0).getVerticalAlignment());
+        assertNull(tableRows.get(1).getCell(1).getVerticalAlignment());
+    }
 }
diff --git a/test-data/document/59030.docx b/test-data/document/59030.docx
new file mode 100644 (file)
index 0000000..cf40dd2
Binary files /dev/null and b/test-data/document/59030.docx differ