You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestXWPFStyles.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xwpf.usermodel;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.assertNull;
  20. import static org.junit.Assert.assertTrue;
  21. import static org.junit.Assert.fail;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import org.apache.poi.xwpf.XWPFTestDataSamples;
  26. import org.junit.Test;
  27. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
  28. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLatentStyles;
  29. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLsdException;
  30. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
  31. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
  32. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType;
  33. public final class TestXWPFStyles {
  34. @Test
  35. public void testGetUsedStyles() throws IOException {
  36. try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("Styles.docx")) {
  37. List<XWPFStyle> testUsedStyleList = new ArrayList<>();
  38. XWPFStyles styles = sampleDoc.getStyles();
  39. XWPFStyle style = styles.getStyle("berschrift1");
  40. testUsedStyleList.add(style);
  41. testUsedStyleList.add(styles.getStyle("Standard"));
  42. testUsedStyleList.add(styles.getStyle("berschrift1Zchn"));
  43. testUsedStyleList.add(styles.getStyle("Absatz-Standardschriftart"));
  44. style.hasSameName(style);
  45. List<XWPFStyle> usedStyleList = styles.getUsedStyleList(style);
  46. assertEquals(usedStyleList, testUsedStyleList);
  47. }
  48. }
  49. @Test
  50. public void testAddStylesToDocument() throws IOException {
  51. XWPFDocument docOut = new XWPFDocument();
  52. XWPFStyles styles = docOut.createStyles();
  53. String strStyleId = "headline1";
  54. CTStyle ctStyle = CTStyle.Factory.newInstance();
  55. ctStyle.setStyleId(strStyleId);
  56. XWPFStyle s = new XWPFStyle(ctStyle);
  57. styles.addStyle(s);
  58. assertTrue(styles.styleExist(strStyleId));
  59. XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut);
  60. styles = docIn.getStyles();
  61. assertTrue(styles.styleExist(strStyleId));
  62. }
  63. /**
  64. * Bug #52449 - We should be able to write a file containing
  65. * both regular and glossary styles without error
  66. */
  67. @Test
  68. public void test52449() throws Exception {
  69. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("52449.docx")) {
  70. XWPFStyles styles = doc.getStyles();
  71. assertNotNull(styles);
  72. XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(doc);
  73. styles = docIn.getStyles();
  74. assertNotNull(styles);
  75. }
  76. }
  77. /**
  78. * YK: tests below don't make much sense,
  79. * they exist only to copy xml beans to pi-ooxml-lite.jar
  80. */
  81. @SuppressWarnings("resource")
  82. @Test
  83. public void testLanguages() {
  84. XWPFDocument docOut = new XWPFDocument();
  85. XWPFStyles styles = docOut.createStyles();
  86. styles.setEastAsia("Chinese");
  87. styles.setSpellingLanguage("English");
  88. CTFonts def = CTFonts.Factory.newInstance();
  89. styles.setDefaultFonts(def);
  90. }
  91. @Test
  92. public void testType() {
  93. CTStyle ctStyle = CTStyle.Factory.newInstance();
  94. XWPFStyle style = new XWPFStyle(ctStyle);
  95. style.setType(STStyleType.PARAGRAPH);
  96. assertEquals(STStyleType.PARAGRAPH, style.getType());
  97. }
  98. @Test
  99. public void testLatentStyles() {
  100. CTLatentStyles latentStyles = CTLatentStyles.Factory.newInstance();
  101. CTLsdException ex = latentStyles.addNewLsdException();
  102. ex.setName("ex1");
  103. XWPFLatentStyles ls = new XWPFLatentStyles(latentStyles);
  104. assertTrue(ls.isLatentStyle("ex1"));
  105. assertFalse(ls.isLatentStyle("notex1"));
  106. }
  107. @Test
  108. public void testSetStyles_Bug57254() throws IOException {
  109. XWPFDocument docOut = new XWPFDocument();
  110. XWPFStyles styles = docOut.createStyles();
  111. CTStyles ctStyles = CTStyles.Factory.newInstance();
  112. String strStyleId = "headline1";
  113. CTStyle ctStyle = ctStyles.addNewStyle();
  114. ctStyle.setStyleId(strStyleId);
  115. styles.setStyles(ctStyles);
  116. assertTrue(styles.styleExist(strStyleId));
  117. XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut);
  118. styles = docIn.getStyles();
  119. assertTrue(styles.styleExist(strStyleId));
  120. }
  121. @Test
  122. public void testEasyAccessToStyles() throws IOException {
  123. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx")) {
  124. XWPFStyles styles = doc.getStyles();
  125. assertNotNull(styles);
  126. // Has 3 paragraphs on page one, a break, and 3 on page 2
  127. assertEquals(7, doc.getParagraphs().size());
  128. // Check the first three have no run styles, just default paragraph style
  129. for (int i = 0; i < 3; i++) {
  130. XWPFParagraph p = doc.getParagraphs().get(i);
  131. assertNull(p.getStyle());
  132. assertNull(p.getStyleID());
  133. assertEquals(1, p.getRuns().size());
  134. XWPFRun r = p.getRuns().get(0);
  135. assertNull(r.getColor());
  136. assertNull(r.getFontFamily());
  137. assertNull(r.getFontName());
  138. assertEquals(-1, r.getFontSize());
  139. }
  140. // On page two, has explicit styles, but on runs not on
  141. // the paragraph itself
  142. for (int i = 4; i < 7; i++) {
  143. XWPFParagraph p = doc.getParagraphs().get(i);
  144. assertNull(p.getStyle());
  145. assertNull(p.getStyleID());
  146. assertEquals(1, p.getRuns().size());
  147. XWPFRun r = p.getRuns().get(0);
  148. assertEquals("Arial Black", r.getFontFamily());
  149. assertEquals("Arial Black", r.getFontName());
  150. assertEquals(16, r.getFontSize());
  151. assertEquals("548DD4", r.getColor());
  152. }
  153. // Check the document styles
  154. // Should have a style defined for each type
  155. assertEquals(4, styles.getNumberOfStyles());
  156. assertNotNull(styles.getStyle("Normal"));
  157. assertNotNull(styles.getStyle("DefaultParagraphFont"));
  158. assertNotNull(styles.getStyle("TableNormal"));
  159. assertNotNull(styles.getStyle("NoList"));
  160. // We can't do much yet with latent styles
  161. assertEquals(137, styles.getLatentStyles().getNumberOfStyles());
  162. // Check the default styles
  163. assertNotNull(styles.getDefaultRunStyle());
  164. assertNotNull(styles.getDefaultParagraphStyle());
  165. assertEquals(11, styles.getDefaultRunStyle().getFontSize());
  166. assertEquals(11.0, styles.getDefaultRunStyle().getFontSizeAsDouble(), 0.01);
  167. assertEquals(200, styles.getDefaultParagraphStyle().getSpacingAfter());
  168. }
  169. }
  170. // Bug 60329: style with missing StyleID throws NPE
  171. @Test
  172. public void testMissingStyleId() throws IOException {
  173. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("60329.docx")) {
  174. XWPFStyles styles = doc.getStyles();
  175. // Styles exist in the test document in this order, EmptyCellLayoutStyle
  176. // is missing a StyleId
  177. try {
  178. assertNotNull(styles.getStyle("NoList"));
  179. assertNull(styles.getStyle("EmptyCellLayoutStyle"));
  180. assertNotNull(styles.getStyle("BalloonText"));
  181. // Bug 64600: styleExist throws NPE
  182. assertTrue(styles.styleExist("NoList"));
  183. assertFalse(styles.styleExist("EmptyCellLayoutStyle"));
  184. assertTrue(styles.styleExist("BalloonText"));
  185. } catch (NullPointerException e) {
  186. fail(e.toString());
  187. }
  188. }
  189. }
  190. @Test
  191. public void testGetStyleByName() throws IOException {
  192. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx")) {
  193. XWPFStyles styles = doc.getStyles();
  194. assertNotNull(styles);
  195. String styleName = "Normal Table";
  196. XWPFStyle style = styles.getStyleWithName(styleName);
  197. assertNotNull("Expected to find style \"" + styleName + "\"", style);
  198. assertEquals(styleName, style.getName());
  199. }
  200. }
  201. }