aboutsummaryrefslogtreecommitdiffstats
path: root/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestRangeDelete.java
blob: 840a45bae80050b2bcaf2b6ff30d5204c57e1db1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.hwpf.usermodel;

import static org.apache.poi.hwpf.HWPFTestDataSamples.openSampleFile;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.PAPX;
import org.junit.jupiter.api.Test;

/**
 *  Test to see if Range.delete() works even if the Range contains a
 *  CharacterRun that uses Unicode characters.
 */
public final class TestRangeDelete {

    // u201c and u201d are "smart-quotes"
    private static final String introText =
        "Introduction\r";
    private static final String fillerText =
        "${delete} This is an MS-Word 97 formatted document created using NeoOffice v. 2.2.4 Patch 0 (OpenOffice.org v. 2.2.1).\r";
    private static final String originalText =
        "It is used to confirm that text delete 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} ${delete} and all the POI contributors for their assistance in this matter.\r";
    private static final String lastText =
        "Thank you, ${organization} ${delete}!\r";
    private static final String searchText = "${delete}";
    private static final String expectedText1 = " This is an MS-Word 97 formatted document created using NeoOffice v. 2.2.4 Patch 0 (OpenOffice.org v. 2.2.1).\r";
    private static final String expectedText2 =
        "It is used to confirm that text delete 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";
    private static final String expectedText3 = "Thank you, ${organization} !\r";

    private static final String illustrativeDocFile = "testRangeDelete.doc";

    /**
     * Test just opening the files
     */
    @Test
    void testOpen() throws IOException {
        try (HWPFDocument doc = openSampleFile(illustrativeDocFile)) {
            assertEquals(5, doc.getParagraphTable().getParagraphs().size());
        }
    }

    /**
     * Test (more "confirm" than test) that we have the general structure that we expect to have.
     */
    @Test
    void testDocStructure() throws IOException {

        try (HWPFDocument daDoc = openSampleFile(illustrativeDocFile)) {
            Range range;
            Section section;
            Paragraph para;
            PAPX paraDef;

            // First, check overall
            range = daDoc.getOverallRange();
            assertEquals(1, range.numSections());
            assertEquals(5, range.numParagraphs());


            // Now, onto just the doc bit
            range = daDoc.getRange();

            assertEquals(1, range.numSections());
            assertEquals(1, daDoc.getSectionTable().getSections().size());
            section = range.getSection(0);

            assertEquals(5, section.numParagraphs());

            para = section.getParagraph(0);
            assertEquals(1, para.numCharacterRuns());
            assertEquals(introText, para.text());

            para = section.getParagraph(1);
            assertEquals(5, para.numCharacterRuns());
            assertEquals(fillerText, para.text());


            paraDef = daDoc.getParagraphTable().getParagraphs().get(2);
            assertEquals(132, paraDef.getStart());
            assertEquals(400, paraDef.getEnd());

            para = section.getParagraph(2);
            assertEquals(5, para.numCharacterRuns());
            assertEquals(originalText, para.text());


            paraDef = daDoc.getParagraphTable().getParagraphs().get(3);
            assertEquals(400, paraDef.getStart());
            assertEquals(438, paraDef.getEnd());

            para = section.getParagraph(3);
            assertEquals(1, para.numCharacterRuns());
            assertEquals(lastText, para.text());


            // Check things match on text length
            assertEquals(439, range.text().length());
            assertEquals(439, section.text().length());
            assertEquals(439,
                         section.getParagraph(0).text().length() +
                                 section.getParagraph(1).text().length() +
                                 section.getParagraph(2).text().length() +
                                 section.getParagraph(3).text().length() +
                                 section.getParagraph(4).text().length()
            );
        }
    }

    /**
     * Test that we can delete text (one instance) from our Range with Unicode text.
     */
    @Test
    void testRangeDeleteOne() throws IOException {
        try (HWPFDocument daDoc = openSampleFile(illustrativeDocFile)) {

            Range range = daDoc.getOverallRange();
            assertEquals(1, range.numSections());

            Section section = range.getSection(0);
            assertEquals(5, section.numParagraphs());

            Paragraph para = section.getParagraph(2);

            String text = para.text();
            assertEquals(originalText, text);

            int offset = text.indexOf(searchText);
            assertEquals(192, offset);

            int absOffset = para.getStartOffset() + offset;
            Range subRange = new Range(absOffset, (absOffset + searchText.length()), para.getDocument());

            assertEquals(searchText, subRange.text());

            subRange.delete();
            daDoc.getOverallRange().sanityCheck();
            daDoc.getRange().sanityCheck();

            // we need to let the model re-calculate the Range before we evaluate it
            range = daDoc.getRange();

            assertEquals(1, range.numSections());
            section = range.getSection(0);

            assertEquals(5, section.numParagraphs());
            para = section.getParagraph(2);

            text = para.text();
            assertEquals(expectedText2, text);

            // this can lead to a StringBufferOutOfBoundsException, so we will add it
            // even though we don't have an assertion for it
            Range daRange = daDoc.getRange();
            daRange.sanityCheck();
            daRange.text();
        }
    }

    /**
     * Test that we can delete text (all instances of) from our Range with Unicode text.
     */
    @Test
    void testRangeDeleteAll() throws IOException {
        try (HWPFDocument daDoc = openSampleFile(illustrativeDocFile)) {

            Range range = daDoc.getRange();
            assertEquals(1, range.numSections());

            Section section = range.getSection(0);
            assertEquals(5, section.numParagraphs());

            Paragraph para = section.getParagraph(2);

            String text = para.text();
            assertEquals(originalText, text);

            boolean keepLooking = true;
            while (keepLooking) {
                // Reload the range every time
                range = daDoc.getRange();
                int offset = range.text().indexOf(searchText);
                if (offset >= 0) {

                    int absOffset = range.getStartOffset() + offset;

                    Range subRange = new Range(
                            absOffset, (absOffset + searchText.length()), range.getDocument());

                    assertEquals(searchText, subRange.text());

                    subRange.delete();

                } else {
                    keepLooking = false;
                }
            }

            // we need to let the model re-calculate the Range before we use it
            range = daDoc.getRange();

            assertEquals(1, range.numSections());
            section = range.getSection(0);

            assertEquals(5, section.numParagraphs());

            para = section.getParagraph(0);
            text = para.text();
            assertEquals(introText, text);

            para = section.getParagraph(1);
            text = para.text();
            assertEquals(expectedText1, text);

            para = section.getParagraph(2);
            text = para.text();
            assertEquals(expectedText2, text);

            para = section.getParagraph(3);
            text = para.text();
            assertEquals(expectedText3, text);
        }
    }
}