From aac3f879099a5cd67824f24b8de9b2905cb7e2d2 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 15 Aug 2018 18:31:36 +0000 Subject: [PATCH] [github-121] Fix issue with setting vertical alignment and emphasis mark. This closes #121 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1838133 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/xwpf/usermodel/XWPFRun.java | 4 ++-- .../apache/poi/xwpf/usermodel/TestXWPFRun.java | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java index e4920d718d..840e533ed9 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java @@ -1485,7 +1485,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { */ public void setVerticalAlignment(String verticalAlignment) { CTRPr pr = getRunProperties(true); - CTVerticalAlignRun vertAlign = pr.getVertAlign(); + CTVerticalAlignRun vertAlign = pr.isSetVertAlign() ? pr.getVertAlign() : pr.addNewVertAlign(); STVerticalAlignRun align = vertAlign.xgetVal(); if (align == null) { align = STVerticalAlignRun.Factory.newInstance(); @@ -1525,7 +1525,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { */ public void setEmphasisMark(String markType) { CTRPr pr = getRunProperties(true); - CTEm emphasisMark = pr.getEm(); + CTEm emphasisMark = pr.isSetEm() ? pr.getEm() : pr.addNewEm(); STEm mark = emphasisMark.xgetVal(); if (mark == null) { mark = STEm.Factory.newInstance(); diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java index be169977fb..78596f9a3b 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java @@ -689,8 +689,10 @@ public class TestXWPFRun { @Test public void testSetGetVerticalAlignment() throws IOException { XWPFDocument document = new XWPFDocument(); - final XWPFRun run = document.createParagraph().createRun(); + XWPFRun run = document.createParagraph().createRun(); assertEquals(STVerticalAlignRun.BASELINE, run.getVerticalAlignment()); + // Reset to a fresh run so we test case of run not having vertical alignment at all + run = document.createParagraph().createRun(); run.setVerticalAlignment("subscript"); assertEquals(STVerticalAlignRun.SUBSCRIPT, run.getVerticalAlignment()); run.setVerticalAlignment("superscript"); @@ -714,8 +716,10 @@ public class TestXWPFRun { @Test public void testSetGetEmphasisMark() throws IOException { XWPFDocument document = new XWPFDocument(); - final XWPFRun run = document.createParagraph().createRun(); + XWPFRun run = document.createParagraph().createRun(); assertEquals(STEm.NONE, run.getEmphasisMark()); + // Reset to a fresh run so we test case of run not having property at all + run = document.createParagraph().createRun(); run.setEmphasisMark("dot"); assertEquals(STEm.DOT, run.getEmphasisMark()); document.close(); @@ -724,8 +728,10 @@ public class TestXWPFRun { @Test public void testSetGetUnderlineColor() throws IOException { XWPFDocument document = new XWPFDocument(); - final XWPFRun run = document.createParagraph().createRun(); + XWPFRun run = document.createParagraph().createRun(); assertEquals("auto", run.getUnderlineColor()); + // Reset to a fresh run so we test case of run not having property at all + run = document.createParagraph().createRun(); String colorRgb = "C0F1a2"; run.setUnderlineColor(colorRgb); assertEquals(colorRgb.toUpperCase(LocaleUtil.getUserLocale()), run.getUnderlineColor()); @@ -737,8 +743,10 @@ public class TestXWPFRun { @Test public void testSetGetUnderlineThemeColor() throws IOException { XWPFDocument document = new XWPFDocument(); - final XWPFRun run = document.createParagraph().createRun(); + XWPFRun run = document.createParagraph().createRun(); assertEquals(STThemeColor.NONE, run.getUnderlineThemeColor()); + // Reset to a fresh run so we test case of run not having property at all + run = document.createParagraph().createRun(); String colorName = "accent4"; run.setUnderlineThemeColor(colorName); assertEquals(STThemeColor.Enum.forString(colorName), run.getUnderlineThemeColor());