Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

TestXWPFStyles.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertFalse;
  18. import static org.junit.jupiter.api.Assertions.assertNotNull;
  19. import static org.junit.jupiter.api.Assertions.assertNull;
  20. import static org.junit.jupiter.api.Assertions.assertTrue;
  21. import java.io.IOException;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import org.apache.poi.xwpf.XWPFTestDataSamples;
  25. import org.junit.jupiter.api.Test;
  26. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
  27. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLatentStyles;
  28. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLsdException;
  29. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
  30. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
  31. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STStyleType;
  32. public final class TestXWPFStyles {
  33. @Test
  34. void testGetUsedStyles() throws IOException {
  35. try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("Styles.docx")) {
  36. List<XWPFStyle> testUsedStyleList = new ArrayList<>();
  37. XWPFStyles styles = sampleDoc.getStyles();
  38. XWPFStyle style = styles.getStyle("berschrift1");
  39. testUsedStyleList.add(style);
  40. testUsedStyleList.add(styles.getStyle("Standard"));
  41. testUsedStyleList.add(styles.getStyle("berschrift1Zchn"));
  42. testUsedStyleList.add(styles.getStyle("Absatz-Standardschriftart"));
  43. style.hasSameName(style);
  44. List<XWPFStyle> usedStyleList = styles.getUsedStyleList(style);
  45. assertEquals(usedStyleList, testUsedStyleList);
  46. }
  47. }
  48. @Test
  49. void testAddStylesToDocument() throws IOException {
  50. try (XWPFDocument docOut = new XWPFDocument()) {
  51. XWPFStyles styles = docOut.createStyles();
  52. String strStyleId = "headline1";
  53. CTStyle ctStyle = CTStyle.Factory.newInstance();
  54. ctStyle.setStyleId(strStyleId);
  55. XWPFStyle s = new XWPFStyle(ctStyle);
  56. styles.addStyle(s);
  57. assertTrue(styles.styleExist(strStyleId));
  58. XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut);
  59. styles = docIn.getStyles();
  60. assertTrue(styles.styleExist(strStyleId));
  61. }
  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. 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. @Test
  82. void testLanguages() throws IOException {
  83. try (XWPFDocument docOut = new XWPFDocument()) {
  84. XWPFStyles styles = docOut.createStyles();
  85. styles.setEastAsia("Chinese");
  86. styles.setSpellingLanguage("English");
  87. CTFonts def = CTFonts.Factory.newInstance();
  88. styles.setDefaultFonts(def);
  89. assertEquals(1, styles.getDefaultRunStyle().getRPr().sizeOfRFontsArray());
  90. }
  91. }
  92. @Test
  93. void testType() {
  94. CTStyle ctStyle = CTStyle.Factory.newInstance();
  95. XWPFStyle style = new XWPFStyle(ctStyle);
  96. style.setType(STStyleType.PARAGRAPH);
  97. assertEquals(STStyleType.PARAGRAPH, style.getType());
  98. }
  99. @Test
  100. void testLatentStyles() {
  101. CTLatentStyles latentStyles = CTLatentStyles.Factory.newInstance();
  102. CTLsdException ex = latentStyles.addNewLsdException();
  103. ex.setName("ex1");
  104. XWPFLatentStyles ls = new XWPFLatentStyles(latentStyles);
  105. assertTrue(ls.isLatentStyle("ex1"));
  106. assertFalse(ls.isLatentStyle("notex1"));
  107. }
  108. @Test
  109. void testSetStyles_Bug57254() throws IOException {
  110. try (XWPFDocument docOut = new XWPFDocument()) {
  111. XWPFStyles styles = docOut.createStyles();
  112. CTStyles ctStyles = CTStyles.Factory.newInstance();
  113. String strStyleId = "headline1";
  114. CTStyle ctStyle = ctStyles.addNewStyle();
  115. ctStyle.setStyleId(strStyleId);
  116. styles.setStyles(ctStyles);
  117. assertTrue(styles.styleExist(strStyleId));
  118. XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut);
  119. styles = docIn.getStyles();
  120. assertTrue(styles.styleExist(strStyleId));
  121. }
  122. }
  123. @Test
  124. void testEasyAccessToStyles() throws IOException {
  125. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx")) {
  126. XWPFStyles styles = doc.getStyles();
  127. assertNotNull(styles);
  128. // Has 3 paragraphs on page one, a break, and 3 on page 2
  129. assertEquals(7, doc.getParagraphs().size());
  130. // Check the first three have no run styles, just default paragraph style
  131. for (int i = 0; i < 3; i++) {
  132. XWPFParagraph p = doc.getParagraphs().get(i);
  133. assertNull(p.getStyle());
  134. assertNull(p.getStyleID());
  135. assertEquals(1, p.getRuns().size());
  136. XWPFRun r = p.getRuns().get(0);
  137. assertNull(r.getColor());
  138. assertNull(r.getFontFamily());
  139. assertNull(r.getFontName());
  140. assertNull(r.getFontSizeAsDouble());
  141. }
  142. // On page two, has explicit styles, but on runs not on
  143. // the paragraph itself
  144. for (int i = 4; i < 7; i++) {
  145. XWPFParagraph p = doc.getParagraphs().get(i);
  146. assertNull(p.getStyle());
  147. assertNull(p.getStyleID());
  148. assertEquals(1, p.getRuns().size());
  149. XWPFRun r = p.getRuns().get(0);
  150. assertEquals("Arial Black", r.getFontFamily());
  151. assertEquals("Arial Black", r.getFontName());
  152. assertEquals(16.0, r.getFontSizeAsDouble(), 0.01);
  153. assertEquals("548DD4", r.getColor());
  154. }
  155. // Check the document styles
  156. // Should have a style defined for each type
  157. assertEquals(4, styles.getNumberOfStyles());
  158. assertNotNull(styles.getStyle("Normal"));
  159. assertNotNull(styles.getStyle("DefaultParagraphFont"));
  160. assertNotNull(styles.getStyle("TableNormal"));
  161. assertNotNull(styles.getStyle("NoList"));
  162. // We can't do much yet with latent styles
  163. assertEquals(137, styles.getLatentStyles().getNumberOfStyles());
  164. // Check the default styles
  165. assertNotNull(styles.getDefaultRunStyle());
  166. assertNotNull(styles.getDefaultParagraphStyle());
  167. assertEquals(11.0, styles.getDefaultRunStyle().getFontSizeAsDouble(), 0.01);
  168. assertEquals(11.0, styles.getDefaultRunStyle().getFontSizeAsDouble(), 0.01);
  169. assertEquals(200, styles.getDefaultParagraphStyle().getSpacingAfter());
  170. }
  171. }
  172. // Bug 60329: style with missing StyleID throws NPE
  173. @Test
  174. void testMissingStyleId() throws IOException {
  175. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("60329.docx")) {
  176. XWPFStyles styles = doc.getStyles();
  177. // Styles exist in the test document in this order, EmptyCellLayoutStyle
  178. // is missing a StyleId
  179. assertNotNull(styles.getStyle("NoList"));
  180. assertNull(styles.getStyle("EmptyCellLayoutStyle"));
  181. assertNotNull(styles.getStyle("BalloonText"));
  182. // Bug 64600: styleExist throws NPE
  183. assertTrue(styles.styleExist("NoList"));
  184. assertFalse(styles.styleExist("EmptyCellLayoutStyle"));
  185. assertTrue(styles.styleExist("BalloonText"));
  186. }
  187. }
  188. @Test
  189. void testGetStyleByName() throws IOException {
  190. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx")) {
  191. XWPFStyles styles = doc.getStyles();
  192. assertNotNull(styles);
  193. String styleName = "Normal Table";
  194. XWPFStyle style = styles.getStyleWithName(styleName);
  195. assertNotNull(style, "Expected to find style \"" + styleName + "\"");
  196. assertEquals(styleName, style.getName());
  197. }
  198. }
  199. }