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.

TestXWPFFootnotes.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 java.io.IOException;
  17. import java.math.BigInteger;
  18. import java.util.List;
  19. import org.apache.poi.xwpf.XWPFTestDataSamples;
  20. import org.junit.jupiter.api.Test;
  21. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STFtnEdn;
  22. import static org.junit.jupiter.api.Assertions.assertEquals;
  23. import static org.junit.jupiter.api.Assertions.assertNotNull;
  24. import static org.junit.jupiter.api.Assertions.assertSame;
  25. class TestXWPFFootnotes {
  26. @Test
  27. void testCreateFootnotes() throws IOException{
  28. try (XWPFDocument docOut = new XWPFDocument()) {
  29. XWPFAbstractFootnotesEndnotes footnotes = docOut.createFootnotes();
  30. assertNotNull(footnotes);
  31. XWPFAbstractFootnotesEndnotes secondFootnotes = docOut.createFootnotes();
  32. assertSame(footnotes, secondFootnotes);
  33. }
  34. }
  35. @Test
  36. void testAddFootnotesToDocument() throws IOException {
  37. try (XWPFDocument docOut = new XWPFDocument()) {
  38. // NOTE: XWPFDocument.createFootnote() delegates directly
  39. // to XWPFFootnotes.createFootnote() so this tests
  40. // both creation of new XWPFFootnotes in document
  41. // and XWPFFootnotes.createFootnote();
  42. XWPFFootnote footnote = docOut.createFootnote();
  43. BigInteger noteId = footnote.getId();
  44. XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut);
  45. XWPFFootnote note = docIn.getFootnoteByID(noteId.intValue());
  46. assertNotNull(note);
  47. assertEquals(STFtnEdn.NORMAL, note.getCTFtnEdn().getType());
  48. }
  49. }
  50. /**
  51. * Bug 55066 - avoid double loading the footnotes
  52. */
  53. @Test
  54. void testLoadFootnotesOnce() throws IOException {
  55. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54849.docx")) {
  56. List<XWPFFootnote> footnotes = doc.getFootnotes();
  57. int hits = 0;
  58. for (XWPFFootnote fn : footnotes) {
  59. for (IBodyElement e : fn.getBodyElements()) {
  60. if (e instanceof XWPFParagraph) {
  61. String txt = ((XWPFParagraph) e).getText();
  62. if (txt.contains("Footnote_sdt")) {
  63. hits++;
  64. }
  65. }
  66. }
  67. }
  68. assertEquals(1, hits, "Load footnotes once");
  69. }
  70. }
  71. }