aboutsummaryrefslogtreecommitdiffstats
path: root/poi-scratchpad/src/test/java/org/apache/poi/hwpf/usermodel/TestRange.java
blob: 4965fc44b0bc7979d20dfa1557cbd56703b52b84 (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
/* ====================================================================
   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.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

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

/**
 * Tests for Range which aren't around deletion, insertion, text replacement or
 * textual contents
 */
public final class TestRange {
    private static final POIDataSamples SAMPLES = POIDataSamples.getDocumentInstance();

    @Test
    void testFieldStripping() {
        String exp = "This is some text.";

        String single = "This is some \u0013Blah!\u0015text.";
        String with14 = "This is \u0013Blah!\u0014some\u0015 text.";
        String withNested = "This is \u0013Blah!\u0013Blah!\u0015\u0015some text.";
        String withNested14 = "This is \u0013Blah!\u0013Blah!\u0014don't see me\u0015 blah!\u0015some text.";
        String withNestedIn14 = "This is \u0013Blah!\u0014some\u0013Blah!\u0015 \u0015text.";

        // Check all comes out right
        assertEquals( exp, Range.stripFields( exp ) );
        assertEquals( exp, Range.stripFields( single ) );
        assertEquals( exp, Range.stripFields( with14 ) );
        assertEquals( exp, Range.stripFields( withNested ) );
        assertEquals( exp, Range.stripFields( withNested14 ) );
        assertEquals( exp, Range.stripFields( withNestedIn14 ) );

        // Ones that are odd and we won't change
        String odd1 = "This\u0015 is \u0013 odd";
        String odd2 = "This\u0015 is \u0014 also \u0013 odd";

        assertEquals( odd1, Range.stripFields( odd1 ) );
        assertEquals( odd2, Range.stripFields( odd2 ) );
    }

    @Test
    void testBug46817() throws IOException {
        try (InputStream is = SAMPLES.openResourceAsStream("Bug46817.doc");
             HWPFDocument hwpfDocument = new HWPFDocument(is)) {

            final List<SEPX> sections = hwpfDocument.getSectionTable().getSections();
            assertEquals( 1, sections.size() );

            // whole document, including additional text from shape
            SEPX sepx = sections.get( 0 );
            assertEquals( 0, sepx.getStart() );
            assertEquals( 1428, sepx.getEnd() );

            // only main range
            Range range = hwpfDocument.getRange();
            assertEquals( 0, range.getStartOffset() );
            assertEquals( 766, range.getEndOffset() );

            Paragraph lastInMainRange = range.getParagraph( range.numParagraphs() - 1);
            assertTrue( lastInMainRange.getEndOffset() <= 766 );

            Section section = range.getSection( 0 );
            assertTrue( section.getEndOffset() <= 766 );

            Paragraph lastInMainSection = section.getParagraph( section
                .numParagraphs() - 1);
            assertTrue( lastInMainSection.getEndOffset() <= 766 );
        }
    }
}