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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
/* ====================================================================
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.xwpf.usermodel;
import static org.apache.poi.xwpf.XWPFTestDataSamples.writeOutAndReadBack;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import org.apache.poi.openxml4j.util.ZipArchiveFakeEntry;
import org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.usermodel.XWPFRun.FontCharRange;
import org.junit.jupiter.api.Test;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum;
class TestXWPFBugs {
@Test
void bug55802() throws Exception {
String blabla =
"Bir, iki, \u00fc\u00e7, d\u00f6rt, be\u015f,\n" +
"\nalt\u0131, yedi, sekiz, dokuz, on.\n" +
"\nK\u0131rm\u0131z\u0131 don,\n" +
"\ngel bizim bah\u00e7eye kon,\n" +
"\nsar\u0131 limon";
try (XWPFDocument doc = new XWPFDocument()) {
XWPFRun run = doc.createParagraph().createRun();
for (String str : blabla.split("\n")) {
run.setText(str);
run.addBreak();
}
run.setFontFamily("Times New Roman");
run.setFontSize(20);
assertEquals("Times New Roman", run.getFontFamily());
assertEquals("Times New Roman", run.getFontFamily(FontCharRange.cs));
assertEquals("Times New Roman", run.getFontFamily(FontCharRange.eastAsia));
assertEquals("Times New Roman", run.getFontFamily(FontCharRange.hAnsi));
run.setFontFamily("Arial", FontCharRange.hAnsi);
assertEquals("Arial", run.getFontFamily(FontCharRange.hAnsi));
}
}
@Test
void bug57312_NullPointException() throws IOException {
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("57312.docx")) {
assertNotNull(doc);
for (IBodyElement bodyElement : doc.getBodyElements()) {
BodyElementType elementType = bodyElement.getElementType();
if (elementType == BodyElementType.PARAGRAPH) {
XWPFParagraph paragraph = (XWPFParagraph) bodyElement;
for (IRunElement iRunElem : paragraph.getIRuns()) {
if (iRunElem instanceof XWPFRun) {
XWPFRun runElement = (XWPFRun) iRunElem;
UnderlinePatterns underline = runElement.getUnderline();
assertNotNull(underline);
//System.out.println("Found: " + underline + ": " + runElement.getText(0));
}
}
}
}
}
}
@Test
void bug57495_getTableArrayInDoc() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
//let's create a few tables for the test
for (int i = 0; i < 3; i++) {
doc.createTable(2, 2);
}
XWPFTable table = doc.getTableArray(0);
assertNotNull(table);
//let's check also that returns the correct table
XWPFTable same = doc.getTables().get(0);
assertEquals(table, same);
}
}
@Test
void bug57495_getParagraphArrayInTableCell() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
//let's create a table for the test
XWPFTable table = doc.createTable(2, 2);
assertNotNull(table);
XWPFParagraph p = table.getRow(0).getCell(0).getParagraphArray(0);
assertNotNull(p);
//let's check also that returns the correct paragraph
XWPFParagraph same = table.getRow(0).getCell(0).getParagraphs().get(0);
assertEquals(p, same);
}
}
@Test
void bug57495_convertPixelsToEMUs() {
int pixels = 100;
int expectedEMU = 952500;
int result = Units.pixelToEMU(pixels);
assertEquals(expectedEMU, result);
}
@Test
void test56392() throws IOException {
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("56392.docx")) {
assertNotNull(doc);
}
}
/**
* Removing a run needs to remove it from both Runs and IRuns
*/
@Test
void test57829() throws IOException {
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx")) {
assertNotNull(doc);
assertEquals(3, doc.getParagraphs().size());
for (XWPFParagraph paragraph : doc.getParagraphs()) {
paragraph.removeRun(0);
assertNotNull(paragraph.getText());
}
}
}
/**
* Removing a run needs to take into account position of run if paragraph contains hyperlink runs
*/
@Test
void test58618() throws IOException {
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("58618.docx")) {
XWPFParagraph para = (XWPFParagraph) doc.getBodyElements().get(0);
assertNotNull(para);
assertEquals("Some text some hyper links link link and some text.....", para.getText());
XWPFRun run = para.insertNewRun(para.getRuns().size());
run.setText("New Text");
assertEquals("Some text some hyper links link link and some text.....New Text", para.getText());
para.removeRun(para.getRuns().size() - 2);
assertEquals("Some text some hyper links link linkNew Text", para.getText());
}
}
@Test
void test59378() throws IOException {
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("59378.docx")) {
try (XWPFDocument doc2 = writeOutAndReadBack(doc)) {
assertNotNull(doc2);
}
try (XWPFDocument docBack = writeOutAndReadBack(doc)) {
assertNotNull(docBack);
}
}
}
@Test
void test63788() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
XWPFNumbering numbering = doc.createNumbering();
for (int i = 10; i >= 0; i--) {
addNumberingWithAbstractId(numbering, i); //add numbers in reverse order
}
for (int i = 0; i <= 10; i++) {
assertEquals(i, numbering.getAbstractNum(BigInteger.valueOf(i)).getAbstractNum().getAbstractNumId().longValue());
}
//attempt to remove item with numId 2
assertTrue(numbering.removeAbstractNum(BigInteger.valueOf(2)));
//adding one level to numbering with id 1
numbering.getAbstractNum(BigInteger.valueOf(1)).getCTAbstractNum().addNewLvl();
XWPFDocument docReloaded = writeOutAndReadBack(doc);
XWPFNumbering numberingReloaded = docReloaded.getNumbering();
for (int id = 0; id <= 10; id++) {
XWPFAbstractNum abstractNum = numberingReloaded.getAbstractNum(BigInteger.valueOf(id));
// we removed id "2", so this one should be empty, all others not
if (id == 2) {
assertNull(abstractNum, "Failed for " + id);
} else {
assertNotNull(abstractNum, "Failed for " + id);
assertEquals(id, abstractNum.getAbstractNum().getAbstractNumId().longValue());
// we added one level for numbering with id "1"
if (id == 1) {
assertEquals(1, abstractNum.getAbstractNum().getLvlList().size());
} else {
assertEquals(0, abstractNum.getAbstractNum().getLvlList().size());
}
}
}
// removing the same again fails
assertFalse(numberingReloaded.removeAbstractNum(BigInteger.valueOf(2)));
// removing another one works
assertTrue(numberingReloaded.removeAbstractNum(BigInteger.valueOf(4)));
}
}
@Test
public void test65099() throws IOException {
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("65099.docx")) {
XWPFStyles styles = doc.getStyles();
assertNotNull(styles);
XWPFStyle normal = styles.getStyle("Normal");
assertNotNull(normal);
XWPFStyle style1 = styles.getStyle("EdfTitre3Car");
assertNotNull(style1);
List<XWPFStyle> list = styles.getUsedStyleList(normal);
assertNotNull(list);
assertEquals(1, list.size());
list = styles.getUsedStyleList(style1);
assertNotNull(list);
assertEquals(7, list.size());
assertThrows(NullPointerException.class,
() -> styles.getUsedStyleList(null),
"Passing in 'null' triggers an exception");
XWPFStyle style = doc.getStyles().getStyle("TableauGrille41");
doc.getStyles().getUsedStyleList(style);
}
}
@Test
void bug65738() throws Exception {
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("bug65738.docx")) {
XWPFStyles styles = doc.getStyles();
assertNotNull(styles);
assertEquals(22, doc.getParagraphs().size());
}
}
@Test
void test66080() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
XWPFNumbering numbering = doc.createNumbering();
// Add abstract numbering with id 1
addNumberingWithAbstractId(numbering, 1);
// Add abstract numbering with auto-generated id
numbering.addAbstractNum(new XWPFAbstractNum());
// Check that all abstract numbering ids are unique
long uniqueIdCount = numbering
.getAbstractNums().stream()
.map(e -> e.getCTAbstractNum().getAbstractNumId().intValue())
.distinct().count();
assertEquals(numbering.getAbstractNums().size(), uniqueIdCount);
}
}
@Test
void testEditNumberings() throws IOException {
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("NumberingWithOutOfOrderId.docx")) {
XWPFNumbering numbering = doc.createNumbering();
// Abstract numbering with id = 1 already exists in the file, and has 9 levels
XWPFAbstractNum abstractNum1 = numbering.getAbstractNum(BigInteger.ONE);
assertEquals(9, abstractNum1.getAbstractNum().getLvlList().size());
// Remove all levels from this numbering
while (abstractNum1.getAbstractNum().getLvlList().size() > 0) {
abstractNum1.getAbstractNum().removeLvl(0);
}
// Add abstract numbering with id = 0
addNumberingWithAbstractId(numbering, 0);
// Add abstract numbering with auto-generated id, this should be 2
numbering.addAbstractNum(new XWPFAbstractNum());
// Add (id + 1) levels to each abstract numbering
for (int id = 0; id < 3; id++) {
XWPFAbstractNum num = numbering.getAbstractNum(BigInteger.valueOf(id));
for (int j = 0; j < id + 1; j++) {
num.getAbstractNum().addNewLvl();
}
}
// Check that all levels added successfully
for (int id = 0; id < 3; id++) {
assertEquals(id + 1, numbering.getAbstractNum(BigInteger.valueOf(id)).getAbstractNum().getLvlList().size());
}
XWPFDocument docReloaded = writeOutAndReadBack(doc);
XWPFNumbering numberingReloaded = docReloaded.getNumbering();
// Check that all added levels persisted after document reload
for (int id = 0; id < 3; id++) {
assertEquals(id + 1, numberingReloaded.getAbstractNum(BigInteger.valueOf(id)).getAbstractNum().getLvlList().size());
}
}
}
private static void addNumberingWithAbstractId(XWPFNumbering documentNumbering, int id) {
// create a numbering scheme
CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance();
// give the scheme an ID
cTAbstractNum.setAbstractNumId(BigInteger.valueOf(id));
XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum);
BigInteger abstractNumID = documentNumbering.addAbstractNum(abstractNum);
documentNumbering.addNum(abstractNumID);
}
@Test
void testBug69628() throws IOException {
final int expectedParagraphs = 24;
// bug69628.docx has -1 entry sizes in the zip data
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("bug69628.docx")) {
assertEquals(expectedParagraphs, doc.getParagraphs().size());
}
// test again with smaller byte array max
ZipArchiveFakeEntry.setMaxEntrySize(30 * 1024 * 1024);
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("bug69628.docx")) {
assertEquals(expectedParagraphs, doc.getParagraphs().size());
} finally {
ZipArchiveFakeEntry.setMaxEntrySize(-1);
}
// test again with smaller byte array max
IOUtils.setByteArrayMaxOverride(30 * 1024 * 1024);
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("bug69628.docx")) {
assertEquals(expectedParagraphs, doc.getParagraphs().size());
} finally {
IOUtils.setByteArrayMaxOverride(-1);
}
// test again but temp files enabled
ZipInputStreamZipEntrySource.setThresholdBytesForTempFiles(1000);
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("bug69628.docx")) {
assertEquals(expectedParagraphs, doc.getParagraphs().size());
} finally {
ZipInputStreamZipEntrySource.setThresholdBytesForTempFiles(-1);
}
}
}
|