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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
/* ====================================================================
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.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.usermodel.XWPFTable.XWPFBorderType;
import org.junit.Test;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblCellMar;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGridCol;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
/**
* Tests for XWPF Tables
*/
public class TestXWPFTable {
@Test
public void testConstructor() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
CTTbl ctTable = CTTbl.Factory.newInstance();
XWPFTable xtab = new XWPFTable(ctTable, doc);
assertNotNull(xtab);
assertEquals(1, ctTable.sizeOfTrArray());
assertEquals(1, ctTable.getTrArray(0).sizeOfTcArray());
assertNotNull(ctTable.getTrArray(0).getTcArray(0).getPArray(0));
ctTable = CTTbl.Factory.newInstance();
xtab = new XWPFTable(ctTable, doc, 3, 2);
assertNotNull(xtab);
assertEquals(3, ctTable.sizeOfTrArray());
assertEquals(2, ctTable.getTrArray(0).sizeOfTcArray());
assertNotNull(ctTable.getTrArray(0).getTcArray(0).getPArray(0));
}
}
@Test
public void testTblGrid() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
CTTbl ctTable = CTTbl.Factory.newInstance();
CTTblGrid cttblgrid = ctTable.addNewTblGrid();
cttblgrid.addNewGridCol().setW(BigInteger.valueOf(123));
cttblgrid.addNewGridCol().setW(BigInteger.valueOf(321));
XWPFTable xtab = new XWPFTable(ctTable, doc);
CTTblGridCol[] ca = xtab.getCTTbl().getTblGrid().getGridColArray();
assertEquals("123", ca[0].getW().toString());
assertEquals("321", ca[1].getW().toString());
}
}
@Test
public void testGetText() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
CTTbl table = CTTbl.Factory.newInstance();
CTRow row = table.addNewTr();
CTTc cell = row.addNewTc();
CTP paragraph = cell.addNewP();
CTR run = paragraph.addNewR();
CTText text = run.addNewT();
text.setStringValue("finally I can write!");
XWPFTable xtab = new XWPFTable(table, doc);
assertEquals("finally I can write!\n", xtab.getText());
}
}
@Test
public void testCreateRow() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
CTTbl table = CTTbl.Factory.newInstance();
CTRow r1 = table.addNewTr();
r1.addNewTc().addNewP();
r1.addNewTc().addNewP();
CTRow r2 = table.addNewTr();
r2.addNewTc().addNewP();
r2.addNewTc().addNewP();
CTRow r3 = table.addNewTr();
r3.addNewTc().addNewP();
r3.addNewTc().addNewP();
XWPFTable xtab = new XWPFTable(table, doc);
assertEquals(3, xtab.getNumberOfRows());
assertNotNull(xtab.getRow(2));
//add a new row
xtab.createRow();
assertEquals(4, xtab.getNumberOfRows());
//check number of cols
assertEquals(2, table.getTrArray(0).sizeOfTcArray());
//check creation of first row
xtab = new XWPFTable(CTTbl.Factory.newInstance(), doc);
assertEquals(1, xtab.getCTTbl().getTrArray(0).sizeOfTcArray());
}
}
@Test
public void testSetGetWidth() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
XWPFTable xtab = doc.createTable();
assertEquals(0, xtab.getWidth());
assertEquals(TableWidthType.AUTO, xtab.getWidthType());
xtab.setWidth(1000);
assertEquals(TableWidthType.DXA, xtab.getWidthType());
assertEquals(1000, xtab.getWidth());
xtab.setWidth("auto");
assertEquals(TableWidthType.AUTO, xtab.getWidthType());
assertEquals(0, xtab.getWidth());
assertEquals(0.0, xtab.getWidthDecimal(), 0.01);
xtab.setWidth("999");
assertEquals(TableWidthType.DXA, xtab.getWidthType());
assertEquals(999, xtab.getWidth());
xtab.setWidth("50.5%");
assertEquals(TableWidthType.PCT, xtab.getWidthType());
assertEquals(50.5, xtab.getWidthDecimal(), 0.01);
// Test effect of setting width type to a new value
// From PCT to NIL:
xtab.setWidthType(TableWidthType.NIL);
assertEquals(TableWidthType.NIL, xtab.getWidthType());
assertEquals(0, xtab.getWidth());
xtab.setWidth("999"); // Sets type to DXA
assertEquals(TableWidthType.DXA, xtab.getWidthType());
// From DXA to AUTO:
xtab.setWidthType(TableWidthType.AUTO);
assertEquals(TableWidthType.AUTO, xtab.getWidthType());
assertEquals(0, xtab.getWidth());
xtab.setWidthType(TableWidthType.PCT);
assertEquals(TableWidthType.PCT, xtab.getWidthType());
// From PCT to DXA:
xtab.setWidth("33.3%");
xtab.setWidthType(TableWidthType.DXA);
assertEquals(TableWidthType.DXA, xtab.getWidthType());
assertEquals(0, xtab.getWidth());
// From DXA to DXA: (value should be unchanged)
xtab.setWidth("999");
xtab.setWidthType(TableWidthType.DXA);
assertEquals(TableWidthType.DXA, xtab.getWidthType());
assertEquals(999, xtab.getWidth());
// From DXA to PCT:
xtab.setWidthType(TableWidthType.PCT);
assertEquals(TableWidthType.PCT, xtab.getWidthType());
assertEquals(100.0, xtab.getWidthDecimal(), 0.0);
}
}
@Test
public void testSetGetHeight() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
CTTbl table = CTTbl.Factory.newInstance();
XWPFTable xtab = new XWPFTable(table, doc);
XWPFTableRow row = xtab.createRow();
row.setHeight(20);
assertEquals(20, row.getHeight());
}
}
@Test
public void testSetGetMargins() throws IOException {
// instantiate the following class so it'll get picked up by
// the XmlBean process and added to the jar file. it's required
// for the following XWPFTable methods.
CTTblCellMar ctm = CTTblCellMar.Factory.newInstance();
assertNotNull(ctm);
// create a table
try (XWPFDocument doc = new XWPFDocument()) {
CTTbl ctTable = CTTbl.Factory.newInstance();
XWPFTable table = new XWPFTable(ctTable, doc);
// set margins
table.setCellMargins(50, 50, 250, 450);
// get margin components
int t = table.getCellMarginTop();
assertEquals(50, t);
int l = table.getCellMarginLeft();
assertEquals(50, l);
int b = table.getCellMarginBottom();
assertEquals(250, b);
int r = table.getCellMarginRight();
assertEquals(450, r);
}
}
@Test
public void testSetGetHBorders() throws IOException {
// instantiate the following classes so they'll get picked up by
// the XmlBean process and added to the jar file. they are required
// for the following XWPFTable methods.
CTTblBorders cttb = CTTblBorders.Factory.newInstance();
assertNotNull(cttb);
STBorder stb = STBorder.Factory.newInstance();
assertNotNull(stb);
// create a table
try (XWPFDocument doc = new XWPFDocument()) {
CTTbl ctTable = CTTbl.Factory.newInstance();
XWPFTable table = new XWPFTable(ctTable, doc);
// check initial state
XWPFBorderType bt = table.getInsideHBorderType();
assertEquals(XWPFBorderType.SINGLE, bt);
int sz = table.getInsideHBorderSize();
assertEquals(-1, sz);
int sp = table.getInsideHBorderSpace();
assertEquals(-1, sp);
String clr = table.getInsideHBorderColor();
assertNull(clr);
// set inside horizontal border
table.setInsideHBorder(XWPFBorderType.SINGLE, 4, 0, "FF0000");
// get inside horizontal border components
bt = table.getInsideHBorderType();
assertEquals(XWPFBorderType.SINGLE, bt);
sz = table.getInsideHBorderSize();
assertEquals(4, sz);
sp = table.getInsideHBorderSpace();
assertEquals(0, sp);
clr = table.getInsideHBorderColor();
assertEquals("FF0000", clr);
// remove the border and verify state
table.removeInsideHBorder();
bt = table.getInsideHBorderType();
assertNull(bt);
sz = table.getInsideHBorderSize();
assertEquals(-1, sz);
sp = table.getInsideHBorderSpace();
assertEquals(-1, sp);
clr = table.getInsideHBorderColor();
assertNull(clr);
// check other borders
bt = table.getInsideVBorderType();
assertEquals(XWPFBorderType.SINGLE, bt);
bt = table.getTopBorderType();
assertEquals(XWPFBorderType.SINGLE, bt);
bt = table.getBottomBorderType();
assertEquals(XWPFBorderType.SINGLE, bt);
bt = table.getLeftBorderType();
assertEquals(XWPFBorderType.SINGLE, bt);
bt = table.getRightBorderType();
assertEquals(XWPFBorderType.SINGLE, bt);
// remove the rest all at once and test
table.removeBorders();
bt = table.getInsideVBorderType();
assertNull(bt);
bt = table.getTopBorderType();
assertNull(bt);
bt = table.getBottomBorderType();
assertNull(bt);
bt = table.getLeftBorderType();
assertNull(bt);
bt = table.getRightBorderType();
assertNull(bt);
}
}
@Test
public void testSetGetVBorders() throws IOException {
// create a table
try (XWPFDocument doc = new XWPFDocument()) {
CTTbl ctTable = CTTbl.Factory.newInstance();
XWPFTable table = new XWPFTable(ctTable, doc);
// check initial state
XWPFBorderType bt = table.getInsideVBorderType();
assertEquals(XWPFBorderType.SINGLE, bt);
int sz = table.getInsideVBorderSize();
assertEquals(-1, sz);
int sp = table.getInsideVBorderSpace();
assertEquals(-1, sp);
String clr = table.getInsideVBorderColor();
assertNull(clr);
// set inside vertical border
table.setInsideVBorder(XWPFBorderType.DOUBLE, 4, 0, "00FF00");
// get inside vertical border components
bt = table.getInsideVBorderType();
assertEquals(XWPFBorderType.DOUBLE, bt);
sz = table.getInsideVBorderSize();
assertEquals(4, sz);
sp = table.getInsideVBorderSpace();
assertEquals(0, sp);
clr = table.getInsideVBorderColor();
assertEquals("00FF00", clr);
// remove the border and verify state
table.removeInsideVBorder();
bt = table.getInsideVBorderType();
assertNull(bt);
sz = table.getInsideVBorderSize();
assertEquals(-1, sz);
sp = table.getInsideVBorderSpace();
assertEquals(-1, sp);
clr = table.getInsideVBorderColor();
assertNull(clr);
// check the rest
bt = table.getInsideHBorderType();
assertEquals(XWPFBorderType.SINGLE, bt);
bt = table.getTopBorderType();
assertEquals(XWPFBorderType.SINGLE, bt);
bt = table.getBottomBorderType();
assertEquals(XWPFBorderType.SINGLE, bt);
bt = table.getLeftBorderType();
assertEquals(XWPFBorderType.SINGLE, bt);
bt = table.getRightBorderType();
assertEquals(XWPFBorderType.SINGLE, bt);
// remove the rest one at a time and test
table.removeInsideHBorder();
table.removeTopBorder();
table.removeBottomBorder();
table.removeLeftBorder();
table.removeRightBorder();
bt = table.getInsideHBorderType();
assertNull(bt);
bt = table.getTopBorderType();
assertNull(bt);
bt = table.getBottomBorderType();
assertNull(bt);
bt = table.getLeftBorderType();
assertNull(bt);
bt = table.getRightBorderType();
assertNull(bt);
}
}
@Test
public void testSetGetTopBorders() throws IOException {
// create a table
try (XWPFDocument doc = new XWPFDocument()) {
CTTbl ctTable = CTTbl.Factory.newInstance();
XWPFTable table = new XWPFTable(ctTable, doc);
// check initial state
XWPFBorderType bt = table.getTopBorderType();
assertEquals(XWPFBorderType.SINGLE, bt);
int sz = table.getTopBorderSize();
assertEquals(-1, sz);
int sp = table.getTopBorderSpace();
assertEquals(-1, sp);
String clr = table.getTopBorderColor();
assertNull(clr);
// set top border
table.setTopBorder(XWPFBorderType.THICK, 4, 0, "00FF00");
// get inside vertical border components
bt = table.getTopBorderType();
assertEquals(XWPFBorderType.THICK, bt);
sz = table.getTopBorderSize();
assertEquals(4, sz);
sp = table.getTopBorderSpace();
assertEquals(0, sp);
clr = table.getTopBorderColor();
assertEquals("00FF00", clr);
// remove the border and verify state
table.removeTopBorder();
bt = table.getTopBorderType();
assertNull(bt);
sz = table.getTopBorderSize();
assertEquals(-1, sz);
sp = table.getTopBorderSpace();
assertEquals(-1, sp);
clr = table.getTopBorderColor();
assertNull(clr);
}
}
@Test
public void testSetGetBottomBorders() throws IOException {
// create a table
try (XWPFDocument doc = new XWPFDocument()) {
CTTbl ctTable = CTTbl.Factory.newInstance();
XWPFTable table = new XWPFTable(ctTable, doc);
// check initial state
XWPFBorderType bt = table.getBottomBorderType();
assertEquals(XWPFBorderType.SINGLE, bt);
int sz = table.getBottomBorderSize();
assertEquals(-1, sz);
int sp = table.getBottomBorderSpace();
assertEquals(-1, sp);
String clr = table.getBottomBorderColor();
assertNull(clr);
// set inside vertical border
table.setBottomBorder(XWPFBorderType.DOTTED, 4, 0, "00FF00");
// get inside vertical border components
bt = table.getBottomBorderType();
assertEquals(XWPFBorderType.DOTTED, bt);
sz = table.getBottomBorderSize();
assertEquals(4, sz);
sp = table.getBottomBorderSpace();
assertEquals(0, sp);
clr = table.getBottomBorderColor();
assertEquals("00FF00", clr);
// remove the border and verify state
table.removeBottomBorder();
bt = table.getBottomBorderType();
assertNull(bt);
sz = table.getBottomBorderSize();
assertEquals(-1, sz);
sp = table.getBottomBorderSpace();
assertEquals(-1, sp);
clr = table.getBottomBorderColor();
assertNull(clr);
}
}
@Test
public void testSetGetLeftBorders() throws IOException {
// create a table
try (XWPFDocument doc = new XWPFDocument()) {
CTTbl ctTable = CTTbl.Factory.newInstance();
XWPFTable table = new XWPFTable(ctTable, doc);
// check initial state
XWPFBorderType bt = table.getLeftBorderType();
assertEquals(XWPFBorderType.SINGLE, bt);
int sz = table.getLeftBorderSize();
assertEquals(-1, sz);
int sp = table.getLeftBorderSpace();
assertEquals(-1, sp);
String clr = table.getLeftBorderColor();
assertNull(clr);
// set inside vertical border
table.setLeftBorder(XWPFBorderType.DASHED, 4, 0, "00FF00");
// get inside vertical border components
bt = table.getLeftBorderType();
assertEquals(XWPFBorderType.DASHED, bt);
sz = table.getLeftBorderSize();
assertEquals(4, sz);
sp = table.getLeftBorderSpace();
assertEquals(0, sp);
clr = table.getLeftBorderColor();
assertEquals("00FF00", clr);
// remove the border and verify state
table.removeLeftBorder();
bt = table.getLeftBorderType();
assertNull(bt);
sz = table.getLeftBorderSize();
assertEquals(-1, sz);
sp = table.getLeftBorderSpace();
assertEquals(-1, sp);
clr = table.getLeftBorderColor();
assertNull(clr);
}
}
@Test
public void testSetGetRightBorders() throws IOException {
// create a table
try (XWPFDocument doc = new XWPFDocument()) {
CTTbl ctTable = CTTbl.Factory.newInstance();
XWPFTable table = new XWPFTable(ctTable, doc);
// check initial state
XWPFBorderType bt = table.getRightBorderType();
assertEquals(XWPFBorderType.SINGLE, bt);
int sz = table.getRightBorderSize();
assertEquals(-1, sz);
int sp = table.getRightBorderSpace();
assertEquals(-1, sp);
String clr = table.getRightBorderColor();
assertNull(clr);
// set inside vertical border
table.setRightBorder(XWPFBorderType.DOT_DASH, 4, 0, "00FF00");
// get inside vertical border components
bt = table.getRightBorderType();
assertEquals(XWPFBorderType.DOT_DASH, bt);
sz = table.getRightBorderSize();
assertEquals(4, sz);
sp = table.getRightBorderSpace();
assertEquals(0, sp);
clr = table.getRightBorderColor();
assertEquals("00FF00", clr);
// remove the border and verify state
table.removeRightBorder();
bt = table.getRightBorderType();
assertNull(bt);
sz = table.getRightBorderSize();
assertEquals(-1, sz);
sp = table.getRightBorderSpace();
assertEquals(-1, sp);
clr = table.getRightBorderColor();
assertNull(clr);
}
}
@Test
public void testSetGetRowBandSize() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
CTTbl ctTable = CTTbl.Factory.newInstance();
XWPFTable table = new XWPFTable(ctTable, doc);
table.setRowBandSize(12);
int sz = table.getRowBandSize();
assertEquals(12, sz);
}
}
@Test
public void testSetGetColBandSize() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
CTTbl ctTable = CTTbl.Factory.newInstance();
XWPFTable table = new XWPFTable(ctTable, doc);
table.setColBandSize(16);
int sz = table.getColBandSize();
assertEquals(16, sz);
}
}
@Test
public void testCreateTable() throws Exception {
// open an empty document
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx")) {
// create a table with 5 rows and 7 columns
int noRows = 5;
int noCols = 7;
XWPFTable table = doc.createTable(noRows, noCols);
// assert the table is empty
List<XWPFTableRow> rows = table.getRows();
assertEquals("Table has less rows than requested.", noRows, rows.size());
for (XWPFTableRow xwpfRow : rows) {
assertNotNull(xwpfRow);
for (int i = 0; i < 7; i++) {
XWPFTableCell xwpfCell = xwpfRow.getCell(i);
assertNotNull(xwpfCell);
assertEquals("Empty cells should not have one paragraph.", 1, xwpfCell.getParagraphs().size());
xwpfCell = xwpfRow.getCell(i);
assertEquals("Calling 'getCell' must not modify cells content.", 1, xwpfCell.getParagraphs().size());
}
}
doc.getPackage().revert();
}
}
@Test
public void testSetGetTableAlignment() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
XWPFTable tbl = doc.createTable(1, 1);
tbl.setTableAlignment(TableRowAlign.LEFT);
assertEquals(TableRowAlign.LEFT, tbl.getTableAlignment());
tbl.setTableAlignment(TableRowAlign.CENTER);
assertEquals(TableRowAlign.CENTER, tbl.getTableAlignment());
tbl.setTableAlignment(TableRowAlign.RIGHT);
assertEquals(TableRowAlign.RIGHT, tbl.getTableAlignment());
tbl.removeTableAlignment();
assertNull(tbl.getTableAlignment());
}
}
}
|