2 /* ====================================================================
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================== */
19 package org
.apache
.poi
.hwpf
.usermodel
;
21 import java
.io
.ByteArrayOutputStream
;
22 import java
.io
.FileInputStream
;
23 import java
.util
.List
;
25 import org
.apache
.poi
.hwpf
.HWPFDocument
;
26 import org
.apache
.poi
.hwpf
.model
.PicturesTable
;
27 import org
.apache
.poi
.hwpf
.usermodel
.Picture
;
29 import junit
.framework
.TestCase
;
32 * Test to see if Range.replaceText() works even if the Range contains a
33 * CharacterRun that uses Unicode characters.
35 public class TestRangeReplacement
extends TestCase
{
37 // u201c and u201d are "smart-quotes"
38 private String originalText
=
39 "It is used to confirm that text replacement works even if Unicode characters (such as \u201c\u2014\u201d (U+2014), \u201c\u2e8e\u201d (U+2E8E), or \u201c\u2714\u201d (U+2714)) are present. Everybody should be thankful to the ${organization} and all the POI contributors for their assistance in this matter.\r";
40 private String searchText
= "${organization}";
41 private String replacementText
= "Apache Software Foundation";
42 private String expectedText
=
43 "It is used to confirm that text replacement works even if Unicode characters (such as \u201c\u2014\u201d (U+2014), \u201c\u2e8e\u201d (U+2E8E), or \u201c\u2714\u201d (U+2714)) are present. Everybody should be thankful to the Apache Software Foundation and all the POI contributors for their assistance in this matter.\r";
45 private String illustrativeDocFile
;
47 protected void setUp() throws Exception
{
49 String dirname
= System
.getProperty("HWPF.testdata.path");
51 illustrativeDocFile
= dirname
+ "/testRangeReplacement.doc";
55 * Test just opening the files
57 public void testOpen() throws Exception
{
59 HWPFDocument docA
= new HWPFDocument(new FileInputStream(illustrativeDocFile
));
63 * Test (more "confirm" than test) that we have the general structure that we expect to have.
65 public void testDocStructure() throws Exception
{
67 HWPFDocument daDoc
= new HWPFDocument(new FileInputStream(illustrativeDocFile
));
69 Range range
= daDoc
.getRange();
71 assertEquals(1, range
.numSections());
72 Section section
= range
.getSection(0);
74 assertEquals(5, section
.numParagraphs());
75 Paragraph para
= section
.getParagraph(2);
77 assertEquals(5, para
.numCharacterRuns());
78 String text
= para
.getCharacterRun(0).text() + para
.getCharacterRun(1).text() +
79 para
.getCharacterRun(2).text() + para
.getCharacterRun(3).text() + para
.getCharacterRun(4).text();
81 assertEquals(originalText
, text
);
85 * Test that we can replace text in our Range with Unicode text.
87 public void testRangeReplacement() throws Exception
{
89 HWPFDocument daDoc
= new HWPFDocument(new FileInputStream(illustrativeDocFile
));
91 Range range
= daDoc
.getRange();
92 assertEquals(1, range
.numSections());
94 Section section
= range
.getSection(0);
95 assertEquals(5, section
.numParagraphs());
97 Paragraph para
= section
.getParagraph(2);
99 String text
= para
.text();
100 assertEquals(originalText
, text
);
102 int offset
= text
.indexOf(searchText
);
103 assertEquals(181, offset
);
105 para
.replaceText(searchText
, replacementText
, offset
);
107 // we need to let the model re-calculate the Range before we evaluate it
108 range
= daDoc
.getRange();
110 assertEquals(1, range
.numSections());
111 section
= range
.getSection(0);
113 assertEquals(5, section
.numParagraphs());
114 para
= section
.getParagraph(2);
117 assertEquals(expectedText
, text
);