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.

TestHWPFRangeParts.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.hwpf;
  16. import org.apache.poi.hwpf.usermodel.Range;
  17. import junit.framework.TestCase;
  18. /**
  19. * Test that we pull out the right bits of a file into
  20. * the different ranges
  21. */
  22. public final class TestHWPFRangeParts extends TestCase {
  23. private static final char page_break = (char)12;
  24. private static final String headerDef =
  25. "\u0003\r\r" +
  26. "\u0004\r\r" +
  27. "\u0003\r\r" +
  28. "\u0004\r\r"
  29. ;
  30. private static final String footerDef = "\r";
  31. private static final String endHeaderFooter = "\r\r";
  32. private static final String a_page_1 =
  33. "This is a sample word document. It has two pages. It has a three column heading, and a three column footer\r" +
  34. "\r" +
  35. "HEADING TEXT\r" +
  36. "\r" +
  37. "More on page one\r" +
  38. "\r\r" +
  39. "End of page 1\r"
  40. ;
  41. private static final String a_page_2 =
  42. "This is page two. It also has a three column heading, and a three column footer.\r"
  43. ;
  44. private static final String a_header =
  45. "First header column!\tMid header Right header!\r"
  46. ;
  47. private static final String a_footer =
  48. "Footer Left\tFooter Middle Footer Right\r"
  49. ;
  50. private static final String u_page_1 =
  51. "This is a fairly simple word document, over two pages, with headers and footers.\r" +
  52. "The trick with this one is that it contains some Unicode based strings in it.\r" +
  53. "Firstly, some currency symbols:\r" +
  54. "\tGBP - \u00a3\r" +
  55. "\tEUR - \u20ac\r" +
  56. "Now, we\u2019ll have some French text, in bold and big:\r" +
  57. "\tMoli\u00e8re\r" +
  58. "And some normal French text:\r" +
  59. "\tL'Avare ou l'\u00c9cole du mensonge\r" +
  60. "That\u2019s it for page one\r"
  61. ;
  62. private static final String u_page_2 =
  63. "This is page two. Les Pr\u00e9cieuses ridicules. The end.\r"
  64. ;
  65. private static final String u_header =
  66. "\r\r" +
  67. "This is a simple header, with a \u20ac euro symbol in it.\r"
  68. ;
  69. private static final String u_footer =
  70. "\r\r\r" +
  71. "The footer, with Moli\u00e8re, has Unicode in it.\r" +
  72. "\r\r\r\r"
  73. ;
  74. /**
  75. * A document made up only of basic ASCII text
  76. */
  77. private HWPFDocument docAscii;
  78. /**
  79. * A document with some unicode in it too
  80. */
  81. private HWPFDocument docUnicode;
  82. public void setUp() {
  83. docUnicode = HWPFTestDataSamples.openSampleFile("HeaderFooterUnicode.doc");
  84. docAscii = HWPFTestDataSamples.openSampleFile("ThreeColHeadFoot.doc");
  85. }
  86. /**
  87. * Note - this test runs several times, to ensure that things
  88. * don't get broken as we write out and read back in again
  89. * TODO - Make this work with 3+ runs
  90. */
  91. public void testContents() {
  92. HWPFDocument doc = docAscii;
  93. for(int run=0; run<3; run++) {
  94. Range r;
  95. // Now check the real ranges
  96. r = doc.getRange();
  97. assertEquals(
  98. a_page_1 +
  99. page_break + "\r" +
  100. a_page_2,
  101. r.text()
  102. );
  103. r = doc.getHeaderStoryRange();
  104. assertEquals(
  105. headerDef +
  106. a_header +
  107. footerDef +
  108. a_footer +
  109. endHeaderFooter,
  110. r.text()
  111. );
  112. r = doc.getOverallRange();
  113. assertEquals(
  114. a_page_1 +
  115. page_break + "\r" +
  116. a_page_2 +
  117. headerDef +
  118. a_header +
  119. footerDef +
  120. a_footer +
  121. endHeaderFooter +
  122. "\r",
  123. r.text()
  124. );
  125. // Write out and read back in again, ready for
  126. // the next run of the test
  127. // TODO run more than once
  128. if(run < 1)
  129. doc = HWPFTestDataSamples.writeOutAndReadBack(doc);
  130. }
  131. }
  132. public void testContentsUnicode() {
  133. Range r;
  134. // Now check the real ranges
  135. r = docUnicode.getRange();
  136. assertEquals(
  137. u_page_1 +
  138. page_break + "\r" +
  139. u_page_2,
  140. r.text()
  141. );
  142. r = docUnicode.getHeaderStoryRange();
  143. assertEquals(
  144. headerDef +
  145. u_header +
  146. footerDef +
  147. u_footer +
  148. endHeaderFooter,
  149. r.text()
  150. );
  151. r = docUnicode.getOverallRange();
  152. assertEquals(
  153. u_page_1 +
  154. page_break + "\r" +
  155. u_page_2 +
  156. headerDef +
  157. u_header +
  158. footerDef +
  159. u_footer +
  160. endHeaderFooter +
  161. "\r",
  162. r.text()
  163. );
  164. }
  165. }