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
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */
17 package org
.apache
.poi
.hwpf
.extractor
;
21 import org
.apache
.poi
.hwpf
.HWPFXML
;
22 import org
.apache
.poi
.hwpf
.usermodel
.HWPFXMLDocument
;
23 import org
.apache
.poi
.hxf
.HXFDocument
;
25 import junit
.framework
.TestCase
;
28 * Tests for HXFWordExtractor
30 public class TestHXFWordExtractor
extends TestCase
{
36 * A fairly complex file
40 protected void setUp() throws Exception
{
43 File fileA
= new File(
44 System
.getProperty("HWPF.testdata.path") +
45 File
.separator
+ "sample.docx"
47 File fileB
= new File(
48 System
.getProperty("HWPF.testdata.path") +
49 File
.separator
+ "IllustrativeCases.docx"
52 xmlA
= new HWPFXML(HXFDocument
.openPackage(fileA
));
53 xmlB
= new HWPFXML(HXFDocument
.openPackage(fileB
));
57 * Get text out of the simple file
59 public void testGetSimpleText() throws Exception
{
60 new HXFWordExtractor(xmlA
.getPackage());
61 new HXFWordExtractor(new HWPFXMLDocument(xmlA
));
63 HXFWordExtractor extractor
=
64 new HXFWordExtractor(xmlA
.getPackage());
67 String text
= extractor
.getText();
68 assertTrue(text
.length() > 0);
71 assertTrue(text
.startsWith(
72 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nunc at risus vel erat tempus posuere. Aenean non ante. Suspendisse vehicula dolor sit amet odio."
74 assertTrue(text
.endsWith(
75 "Phasellus ultricies mi nec leo. Sed tempus. In sit amet lorem at velit faucibus vestibulum.\n"
78 // Check number of paragraphs
80 char[] t
= text
.toCharArray();
81 for (int i
= 0; i
< t
.length
; i
++) {
82 if(t
[i
] == '\n') { ps
++; }
88 * Tests getting the text out of a complex file
90 public void testGetComplexText() throws Exception
{
91 HXFWordExtractor extractor
=
92 new HXFWordExtractor(xmlB
.getPackage());
95 String text
= extractor
.getText();
96 assertTrue(text
.length() > 0);
99 System
.err
.println("'"+text
.substring(text
.length() - 20) + "'");
102 assertTrue(text
.startsWith(
103 " \n(V) ILLUSTRATIVE CASES\n\n"
105 assertTrue(text
.endsWith(
106 "As well as gaining "+euro
+"90 from child benefit increases, he will also receive the early childhood supplement of "+euro
+"250 per quarter for Vincent for the full four quarters of the year.\n\n\n\n \n\n\n"
109 // Check number of paragraphs
111 char[] t
= text
.toCharArray();
112 for (int i
= 0; i
< t
.length
; i
++) {
113 if(t
[i
] == '\n') { ps
++; }
115 assertEquals(79, ps
);