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.insertBefore() works even if the Range contains a
33 * CharacterRun that uses Unicode characters.
35 public class TestRangeInsertion
extends TestCase
{
37 // u201c and u201d are "smart-quotes"
38 private String originalText
=
39 "It is used to confirm that text insertion 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.\r";
40 private String textToInsert
= "Look at me! I'm cool! ";
41 private int insertionPoint
= 244;
43 private String illustrativeDocFile
;
45 protected void setUp() throws Exception
{
47 String dirname
= System
.getProperty("HWPF.testdata.path");
49 illustrativeDocFile
= dirname
+ "/testRangeInsertion.doc";
53 * Test just opening the files
55 public void testOpen() throws Exception
{
57 HWPFDocument docA
= new HWPFDocument(new FileInputStream(illustrativeDocFile
));
61 * Test (more "confirm" than test) that we have the general structure that we expect to have.
63 public void testDocStructure() throws Exception
{
65 HWPFDocument daDoc
= new HWPFDocument(new FileInputStream(illustrativeDocFile
));
67 Range range
= daDoc
.getRange();
69 assertEquals(1, range
.numSections());
70 Section section
= range
.getSection(0);
72 assertEquals(3, section
.numParagraphs());
73 Paragraph para
= section
.getParagraph(2);
75 assertEquals(3, para
.numCharacterRuns());
76 String text
= para
.getCharacterRun(0).text() + para
.getCharacterRun(1).text() +
77 para
.getCharacterRun(2).text();
79 assertEquals(originalText
, text
);
83 * Test that we can insert text in our CharacterRun with Unicode text.
85 public void testRangeInsertion() throws Exception
{
87 HWPFDocument daDoc
= new HWPFDocument(new FileInputStream(illustrativeDocFile
));
90 Range range = daDoc.getRange();
91 Section section = range.getSection(0);
92 Paragraph para = section.getParagraph(2);
93 String text = para.getCharacterRun(0).text() + para.getCharacterRun(1).text() +
94 para.getCharacterRun(2).text();
96 System.out.println(text);
99 Range range
= new Range(insertionPoint
, (insertionPoint
+ 2), daDoc
);
100 range
.insertBefore(textToInsert
);
102 // we need to let the model re-calculate the Range before we evaluate it
103 range
= daDoc
.getRange();
105 assertEquals(1, range
.numSections());
106 Section section
= range
.getSection(0);
108 assertEquals(3, section
.numParagraphs());
109 Paragraph para
= section
.getParagraph(2);
111 assertEquals(3, para
.numCharacterRuns());
112 String text
= para
.getCharacterRun(0).text() + para
.getCharacterRun(1).text() +
113 para
.getCharacterRun(2).text();
115 // System.out.println(text);
117 assertEquals((textToInsert
+ originalText
), text
);