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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
|
/* ====================================================================
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.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.Units;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture;
import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STOnOff1;
import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STVerticalAlignRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrClear;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STEm;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHighlightColor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STThemeColor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline;
/**
* Tests for XWPF Run
*/
public class TestXWPFRun {
private CTR ctRun;
private XWPFParagraph p;
private IRunBody irb;
private XWPFDocument doc;
@Before
public void setUp() {
doc = new XWPFDocument();
p = doc.createParagraph();
irb = p;
this.ctRun = CTR.Factory.newInstance();
}
@After
public void tearDown() throws Exception {
doc.close();
}
@Test
public void testSetGetText() {
ctRun.addNewT().setStringValue("TEST STRING");
ctRun.addNewT().setStringValue("TEST2 STRING");
ctRun.addNewT().setStringValue("TEST3 STRING");
assertEquals(3, ctRun.sizeOfTArray());
XWPFRun run = new XWPFRun(ctRun, irb);
assertEquals("TEST2 STRING", run.getText(1));
run.setText("NEW STRING", 0);
assertEquals("NEW STRING", run.getText(0));
//run.setText("xxx",14);
//fail("Position wrong");
}
/*
* bug 59208
* Purpose: test all valid boolean-like values
* exercise isCTOnOff(CTOnOff) through all valid permutations
*/
@Test
public void testCTOnOff() {
CTRPr rpr = ctRun.addNewRPr();
CTOnOff bold = rpr.addNewB();
XWPFRun run = new XWPFRun(ctRun, irb);
// True values: "true", "1", "on"
bold.setVal(STOnOff1.ON);
assertTrue(run.isBold());
bold.setVal(STOnOff1.ON);
assertTrue(run.isBold());
bold.setVal(STOnOff1.ON);
assertTrue(run.isBold());
// False values: "false", "0", "off"
bold.setVal(STOnOff1.OFF);
assertFalse(run.isBold());
bold.setVal(STOnOff1.OFF);
assertFalse(run.isBold());
bold.setVal(STOnOff1.OFF);
assertFalse(run.isBold());
}
@Test
public void testSetGetBold() {
CTRPr rpr = ctRun.addNewRPr();
rpr.addNewB().setVal(STOnOff1.ON);
XWPFRun run = new XWPFRun(ctRun, irb);
assertTrue(run.isBold());
run.setBold(false);
// Implementation detail: POI natively prefers <w:b w:val="false"/>,
// but should correctly read val="0" and val="off"
assertEquals("off", rpr.getBArray(0).getVal());
}
@Test
public void testSetGetItalic() {
CTRPr rpr = ctRun.addNewRPr();
rpr.addNewI().setVal(STOnOff1.ON);
XWPFRun run = new XWPFRun(ctRun, irb);
assertTrue(run.isItalic());
run.setItalic(false);
assertEquals("off", rpr.getIArray(0).getVal());
}
@Test
public void testSetGetStrike() {
CTRPr rpr = ctRun.addNewRPr();
rpr.addNewStrike().setVal(STOnOff1.ON);
XWPFRun run = new XWPFRun(ctRun, irb);
assertTrue(run.isStrikeThrough());
run.setStrikeThrough(false);
assertEquals("off", rpr.getStrikeArray(0).getVal());
}
@Test
public void testSetGetUnderline() {
CTRPr rpr = ctRun.addNewRPr();
XWPFRun run = new XWPFRun(ctRun, irb);
rpr.addNewU().setVal(STUnderline.DASH);
assertEquals(UnderlinePatterns.DASH.getValue(), run.getUnderline()
.getValue());
run.setUnderline(UnderlinePatterns.NONE);
assertEquals(STUnderline.NONE.intValue(), rpr.getUArray(0).getVal()
.intValue());
}
@Test
public void testSetGetFontFamily() {
CTRPr rpr = ctRun.addNewRPr();
rpr.addNewRFonts().setAscii("Times New Roman");
XWPFRun run = new XWPFRun(ctRun, irb);
assertEquals("Times New Roman", run.getFontFamily());
run.setFontFamily("Verdana");
assertEquals("Verdana", rpr.getRFontsArray(0).getAscii());
}
@Test
public void testSetGetFontSize() {
CTRPr rpr = ctRun.addNewRPr();
rpr.addNewSz().setVal(BigInteger.valueOf(14));
XWPFRun run = new XWPFRun(ctRun, irb);
assertEquals(7, run.getFontSize());
assertEquals(7.0, run.getFontSizeAsDouble(), 0.01);
run.setFontSize(24);
assertEquals("48", rpr.getSzArray(0).getVal().toString());
run.setFontSize(24.5f);
assertEquals("49", rpr.getSzArray(0).getVal().toString());
assertEquals(25, run.getFontSize());
assertEquals(24.5, run.getFontSizeAsDouble(), 0.01);
}
@Test
public void testSetGetTextForegroundBackground() {
CTRPr rpr = ctRun.addNewRPr();
rpr.addNewPosition().setVal(new BigInteger("4000"));
XWPFRun run = new XWPFRun(ctRun, irb);
assertEquals(4000, run.getTextPosition());
run.setTextPosition(2400);
assertEquals("2400", rpr.getPositionArray(0).getVal().toString());
}
@Test
public void testSetGetColor() {
XWPFRun run = new XWPFRun(ctRun, irb);
run.setColor("0F0F0F");
String clr = run.getColor();
assertEquals("0F0F0F", clr);
}
@Test
public void testAddCarriageReturn() {
ctRun.addNewT().setStringValue("TEST STRING");
ctRun.addNewCr();
ctRun.addNewT().setStringValue("TEST2 STRING");
ctRun.addNewCr();
ctRun.addNewT().setStringValue("TEST3 STRING");
assertEquals(2, ctRun.sizeOfCrArray());
XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), irb);
run.setText("T1");
run.addCarriageReturn();
run.addCarriageReturn();
run.setText("T2");
run.addCarriageReturn();
assertEquals(3, run.getCTR().sizeOfCrArray());
assertEquals("T1\n\nT2\n", run.toString());
}
@Test
public void testAddTabsAndLineBreaks() {
ctRun.addNewT().setStringValue("TEST STRING");
ctRun.addNewCr();
ctRun.addNewT().setStringValue("TEST2 STRING");
ctRun.addNewTab();
ctRun.addNewT().setStringValue("TEST3 STRING");
assertEquals(1, ctRun.sizeOfCrArray());
assertEquals(1, ctRun.sizeOfTabArray());
XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), irb);
run.setText("T1");
run.addCarriageReturn();
run.setText("T2");
run.addTab();
run.setText("T3");
assertEquals(1, run.getCTR().sizeOfCrArray());
assertEquals(1, run.getCTR().sizeOfTabArray());
assertEquals("T1\nT2\tT3", run.toString());
}
@Test
public void testAddPageBreak() {
ctRun.addNewT().setStringValue("TEST STRING");
ctRun.addNewBr();
ctRun.addNewT().setStringValue("TEST2 STRING");
CTBr breac = ctRun.addNewBr();
breac.setClear(STBrClear.LEFT);
ctRun.addNewT().setStringValue("TEST3 STRING");
assertEquals(2, ctRun.sizeOfBrArray());
XWPFRun run = new XWPFRun(CTR.Factory.newInstance(), irb);
run.setText("TEXT1");
run.addBreak();
run.setText("TEXT2");
run.addBreak(BreakType.TEXT_WRAPPING);
assertEquals(2, run.getCTR().sizeOfBrArray());
}
/**
* Test that on an existing document, we do the
* right thing with it
*/
@Test
public void testExisting() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx");
XWPFParagraph p;
XWPFRun run;
// First paragraph is simple
p = doc.getParagraphArray(0);
assertEquals("This is a test document.", p.getText());
assertEquals(2, p.getRuns().size());
run = p.getRuns().get(0);
assertEquals("This is a test document", run.toString());
assertFalse(run.isBold());
assertFalse(run.isItalic());
assertFalse(run.isStrikeThrough());
assertNull(run.getCTR().getRPr());
run = p.getRuns().get(1);
assertEquals(".", run.toString());
assertFalse(run.isBold());
assertFalse(run.isItalic());
assertFalse(run.isStrikeThrough());
assertNull(run.getCTR().getRPr());
// Next paragraph is all in one style, but a different one
p = doc.getParagraphArray(1);
assertEquals("This bit is in bold and italic", p.getText());
assertEquals(1, p.getRuns().size());
run = p.getRuns().get(0);
assertEquals("This bit is in bold and italic", run.toString());
assertTrue(run.isBold());
assertTrue(run.isItalic());
assertFalse(run.isStrikeThrough());
assertTrue(run.getCTR().getRPr().sizeOfBArray() > 0);
assertFalse(run.getCTR().getRPr().getBArray(0).isSetVal());
// Back to normal
p = doc.getParagraphArray(2);
assertEquals("Back to normal", p.getText());
assertEquals(1, p.getRuns().size());
run = p.getRuns().get(0);
assertEquals("Back to normal", run.toString());
assertFalse(run.isBold());
assertFalse(run.isItalic());
assertFalse(run.isStrikeThrough());
assertNull(run.getCTR().getRPr());
// Different styles in one paragraph
p = doc.getParagraphArray(3);
assertEquals("This contains BOLD, ITALIC and BOTH, as well as RED and YELLOW text.", p.getText());
assertEquals(11, p.getRuns().size());
run = p.getRuns().get(0);
assertEquals("This contains ", run.toString());
assertFalse(run.isBold());
assertFalse(run.isItalic());
assertFalse(run.isStrikeThrough());
assertNull(run.getCTR().getRPr());
run = p.getRuns().get(1);
assertEquals("BOLD", run.toString());
assertTrue(run.isBold());
assertFalse(run.isItalic());
assertFalse(run.isStrikeThrough());
run = p.getRuns().get(2);
assertEquals(", ", run.toString());
assertFalse(run.isBold());
assertFalse(run.isItalic());
assertFalse(run.isStrikeThrough());
assertNull(run.getCTR().getRPr());
run = p.getRuns().get(3);
assertEquals("ITALIC", run.toString());
assertFalse(run.isBold());
assertTrue(run.isItalic());
assertFalse(run.isStrikeThrough());
run = p.getRuns().get(4);
assertEquals(" and ", run.toString());
assertFalse(run.isBold());
assertFalse(run.isItalic());
assertFalse(run.isStrikeThrough());
assertNull(run.getCTR().getRPr());
run = p.getRuns().get(5);
assertEquals("BOTH", run.toString());
assertTrue(run.isBold());
assertTrue(run.isItalic());
assertFalse(run.isStrikeThrough());
run = p.getRuns().get(6);
assertEquals(", as well as ", run.toString());
assertFalse(run.isBold());
assertFalse(run.isItalic());
assertFalse(run.isStrikeThrough());
assertNull(run.getCTR().getRPr());
run = p.getRuns().get(7);
assertEquals("RED", run.toString());
assertFalse(run.isBold());
assertFalse(run.isItalic());
assertFalse(run.isStrikeThrough());
run = p.getRuns().get(8);
assertEquals(" and ", run.toString());
assertFalse(run.isBold());
assertFalse(run.isItalic());
assertFalse(run.isStrikeThrough());
assertNull(run.getCTR().getRPr());
run = p.getRuns().get(9);
assertEquals("YELLOW", run.toString());
assertFalse(run.isBold());
assertFalse(run.isItalic());
assertFalse(run.isStrikeThrough());
run = p.getRuns().get(10);
assertEquals(" text.", run.toString());
assertFalse(run.isBold());
assertFalse(run.isItalic());
assertFalse(run.isStrikeThrough());
assertNull(run.getCTR().getRPr());
doc.close();
}
@Test
public void testPictureInHeader() throws IOException {
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx");
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
XWPFHeader header = policy.getDefaultHeader();
int count = 0;
for (XWPFParagraph p : header.getParagraphs()) {
for (XWPFRun r : p.getRuns()) {
List<XWPFPicture> pictures = r.getEmbeddedPictures();
for (XWPFPicture pic : pictures) {
assertNotNull(pic.getPictureData());
assertEquals("DOZOR", pic.getDescription());
}
count += pictures.size();
}
}
assertEquals(1, count);
sampleDoc.close();
}
@Test
public void testSetGetLang() {
XWPFRun run = p.createRun();
assertNull(run.getLang());
run.setLang("en-CA");
assertEquals("en-CA", run.getLang());
run.setLang("fr-CA");
assertEquals("fr-CA", run.getLang());
run.setLang(null);
assertNull(run.getLang());
}
@Test
public void testSetGetLang2() {
XWPFRun run = p.createRun();
assertNull(run.getLang());
run.getCTR().addNewRPr().addNewLang().setVal("en-CA");
assertEquals("en-CA", run.getLang());
run.getCTR().getRPr().getLangArray(0).setVal("fr-CA");
assertEquals("fr-CA", run.getLang());
run.getCTR().getRPr().getLangArray(0).setVal(null);
assertNull(run.getLang());
}
@Test
public void testAddPicture() throws Exception {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx");
XWPFParagraph p = doc.getParagraphArray(2);
XWPFRun r = p.getRuns().get(0);
assertEquals(0, doc.getAllPictures().size());
assertEquals(0, r.getEmbeddedPictures().size());
r.addPicture(new ByteArrayInputStream(new byte[0]), Document.PICTURE_TYPE_JPEG, "test.jpg", 21, 32);
assertEquals(1, doc.getAllPictures().size());
assertEquals(1, r.getEmbeddedPictures().size());
XWPFDocument docBack = XWPFTestDataSamples.writeOutAndReadBack(doc);
XWPFParagraph pBack = docBack.getParagraphArray(2);
XWPFRun rBack = pBack.getRuns().get(0);
assertEquals(1, docBack.getAllPictures().size());
assertEquals(1, rBack.getEmbeddedPictures().size());
docBack.close();
doc.close();
}
/**
* Bugzilla #58237 - Unable to add image to word document header
*/
@Test
public void testAddPictureInHeader() throws IOException, InvalidFormatException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx");
XWPFHeader hdr = doc.createHeader(HeaderFooterType.DEFAULT);
XWPFParagraph p = hdr.createParagraph();
XWPFRun r = p.createRun();
assertEquals(0, hdr.getAllPictures().size());
assertEquals(0, r.getEmbeddedPictures().size());
r.addPicture(new ByteArrayInputStream(new byte[0]), Document.PICTURE_TYPE_JPEG, "test.jpg", 21, 32);
assertEquals(1, hdr.getAllPictures().size());
assertEquals(1, r.getEmbeddedPictures().size());
XWPFPicture pic = r.getEmbeddedPictures().get(0);
CTPicture ctPic = pic.getCTPicture();
CTBlipFillProperties ctBlipFill = ctPic.getBlipFill();
assertNotNull(ctBlipFill);
CTBlip ctBlip = ctBlipFill.getBlip();
assertNotNull(ctBlip);
assertEquals("rId1", ctBlip.getEmbed());
XWPFDocument docBack = XWPFTestDataSamples.writeOutAndReadBack(doc);
XWPFHeader hdrBack = docBack.getHeaderArray(0);
XWPFParagraph pBack = hdrBack.getParagraphArray(0);
XWPFRun rBack = pBack.getRuns().get(0);
assertEquals(1, hdrBack.getAllPictures().size());
assertEquals(1, rBack.getEmbeddedPictures().size());
docBack.close();
doc.close();
}
/**
* Bugzilla #52288 - setting the font family on the
* run mustn't NPE
*/
@Test
public void testSetFontFamily_52288() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("52288.docx");
final Iterator<XWPFParagraph> paragraphs = doc.getParagraphsIterator();
while (paragraphs.hasNext()) {
final XWPFParagraph paragraph = paragraphs.next();
for (final XWPFRun run : paragraph.getRuns()) {
if (run != null) {
final String text = run.getText(0);
if (text != null) {
run.setFontFamily("Times New Roman");
}
}
}
}
doc.close();
}
@Test
public void testBug55476() throws IOException, InvalidFormatException {
byte[] image = XWPFTestDataSamples.getImage("abstract1.jpg");
XWPFDocument document = new XWPFDocument();
document.createParagraph().createRun().addPicture(
new ByteArrayInputStream(image), Document.PICTURE_TYPE_JPEG, "test.jpg", Units.toEMU(300), Units.toEMU(100));
XWPFDocument docBack = XWPFTestDataSamples.writeOutAndReadBack(document);
List<XWPFPicture> pictures = docBack.getParagraphArray(0).getRuns().get(0).getEmbeddedPictures();
assertEquals(1, pictures.size());
docBack.close();
/*OutputStream stream = new FileOutputStream("c:\\temp\\55476.docx");
try {
document.write(stream);
} finally {
stream.close();
}*/
document.close();
}
@Test
public void testBug58922() throws IOException {
XWPFDocument document = new XWPFDocument();
final XWPFRun run = document.createParagraph().createRun();
assertEquals(-1, run.getFontSize());
run.setFontSize(10);
assertEquals(10, run.getFontSize());
run.setFontSize(Short.MAX_VALUE-1);
assertEquals(Short.MAX_VALUE-1, run.getFontSize());
run.setFontSize(Short.MAX_VALUE);
assertEquals(Short.MAX_VALUE, run.getFontSize());
run.setFontSize(Short.MAX_VALUE+1);
assertEquals(Short.MAX_VALUE+1, run.getFontSize());
run.setFontSize(Integer.MAX_VALUE-1);
assertEquals(Integer.MAX_VALUE-1, run.getFontSize());
run.setFontSize(Integer.MAX_VALUE);
assertEquals(Integer.MAX_VALUE, run.getFontSize());
run.setFontSize(-1);
assertEquals(-1, run.getFontSize());
assertEquals(-1, run.getTextPosition());
run.setTextPosition(10);
assertEquals(10, run.getTextPosition());
run.setTextPosition(Short.MAX_VALUE-1);
assertEquals(Short.MAX_VALUE-1, run.getTextPosition());
run.setTextPosition(Short.MAX_VALUE);
assertEquals(Short.MAX_VALUE, run.getTextPosition());
run.setTextPosition(Short.MAX_VALUE+1);
assertEquals(Short.MAX_VALUE+1, run.getTextPosition());
run.setTextPosition(Short.MAX_VALUE+1);
assertEquals(Short.MAX_VALUE+1, run.getTextPosition());
run.setTextPosition(Integer.MAX_VALUE-1);
assertEquals(Integer.MAX_VALUE-1, run.getTextPosition());
run.setTextPosition(Integer.MAX_VALUE);
assertEquals(Integer.MAX_VALUE, run.getTextPosition());
run.setTextPosition(-1);
assertEquals(-1, run.getTextPosition());
document.close();
}
@Test
public void testSetters() {
XWPFDocument document = new XWPFDocument();
final XWPFRun run = document.createParagraph().createRun();
// at least trigger some of the setters to ensure classes are included in
// the poi-ooxml-lite
run.setBold(true);
run.setCapitalized(true);
run.setCharacterSpacing(2);
assertEquals(2, run.getCharacterSpacing());
run.setColor("000000");
run.setDoubleStrikethrough(true);
run.setEmbossed(true);
run.setFontFamily("Calibri");
assertEquals("Calibri", run.getFontFamily());
run.setFontSize(10);
assertEquals(10, run.getFontSize());
run.setImprinted(true);
run.setItalic(true);
}
@Test
public void testSetGetTextScale() throws IOException {
XWPFDocument document = new XWPFDocument();
final XWPFRun run = document.createParagraph().createRun();
assertEquals(100, run.getTextScale());
run.setTextScale(200);
assertEquals(200, run.getTextScale());
document.close();
}
@Test
public void testSetGetTextHighlightColor() throws IOException {
XWPFDocument document = new XWPFDocument();
final XWPFRun run = document.createParagraph().createRun();
assertEquals(STHighlightColor.NONE, run.getTextHightlightColor());
assertFalse(run.isHighlighted());
run.setTextHighlightColor("darkGreen"); // See 17.18.40 ST_HighlightColor (Text Highlight Colors)
assertEquals(STHighlightColor.DARK_GREEN, run.getTextHightlightColor());
assertTrue(run.isHighlighted());
run.setTextHighlightColor("none");
assertFalse(run.isHighlighted());
document.close();
}
@Test
public void testSetGetVanish() throws IOException {
XWPFDocument document = new XWPFDocument();
final XWPFRun run = document.createParagraph().createRun();
assertFalse(run.isVanish());
run.setVanish(true);
assertTrue(run.isVanish());
run.setVanish(false);
assertFalse(run.isVanish());
document.close();
}
@Test
public void testSetGetVerticalAlignment() throws IOException {
XWPFDocument document = new XWPFDocument();
XWPFRun run = document.createParagraph().createRun();
assertEquals(STVerticalAlignRun.BASELINE, run.getVerticalAlignment());
// Reset to a fresh run so we test case of run not having vertical alignment at all
run = document.createParagraph().createRun();
run.setVerticalAlignment("subscript");
assertEquals(STVerticalAlignRun.SUBSCRIPT, run.getVerticalAlignment());
run.setVerticalAlignment("superscript");
assertEquals(STVerticalAlignRun.SUPERSCRIPT, run.getVerticalAlignment());
document.close();
}
@Test
public void testSetGetVAlign() {
CTRPr rpr = ctRun.addNewRPr();
rpr.addNewVertAlign().setVal(STVerticalAlignRun.SUBSCRIPT);
XWPFRun run = new XWPFRun(ctRun, irb);
run.setSubscript(VerticalAlign.BASELINE);
assertEquals(STVerticalAlignRun.BASELINE, rpr.getVertAlignArray(0).getVal());
}
@Test
public void testSetGetEmphasisMark() throws IOException {
XWPFDocument document = new XWPFDocument();
XWPFRun run = document.createParagraph().createRun();
assertEquals(STEm.NONE, run.getEmphasisMark());
// Reset to a fresh run so we test case of run not having property at all
run = document.createParagraph().createRun();
run.setEmphasisMark("dot");
assertEquals(STEm.DOT, run.getEmphasisMark());
document.close();
}
@Test
public void testSetGetUnderlineColor() throws IOException {
XWPFDocument document = new XWPFDocument();
XWPFRun run = document.createParagraph().createRun();
assertEquals("auto", run.getUnderlineColor());
// Reset to a fresh run so we test case of run not having property at all
run = document.createParagraph().createRun();
String colorRgb = "C0F1a2";
run.setUnderlineColor(colorRgb);
assertEquals(colorRgb.toUpperCase(LocaleUtil.getUserLocale()), run.getUnderlineColor());
run.setUnderlineColor("auto");
assertEquals("auto", run.getUnderlineColor());
document.close();
}
@Test
public void testSetGetUnderlineThemeColor() throws IOException {
XWPFDocument document = new XWPFDocument();
XWPFRun run = document.createParagraph().createRun();
assertEquals(STThemeColor.NONE, run.getUnderlineThemeColor());
// Reset to a fresh run so we test case of run not having property at all
run = document.createParagraph().createRun();
String colorName = "accent4";
run.setUnderlineThemeColor(colorName);
assertEquals(STThemeColor.Enum.forString(colorName), run.getUnderlineThemeColor());
run.setUnderlineThemeColor("none");
assertEquals(STThemeColor.NONE, run.getUnderlineThemeColor());
document.close();
}
@Test
public void testSetStyleId() throws IOException {
XWPFDocument document = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx");
final XWPFRun run = document.createParagraph().createRun();
String styleId = "bolditalic";
run.setStyle(styleId);
String candStyleId = run.getCTR().getRPr().getRStyleArray(0).getVal();
assertNotNull("Expected to find a run style ID", candStyleId);
assertEquals(styleId, candStyleId);
assertEquals(styleId, run.getStyle());
document.close();
}
@Test
public void testGetDepthWidth() throws IOException, InvalidFormatException {
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx")) {
XWPFHeader hdr = doc.createHeader(HeaderFooterType.DEFAULT);
XWPFParagraph p = hdr.createParagraph();
XWPFRun r = p.createRun();
assertEquals(0, hdr.getAllPictures().size());
assertEquals(0, r.getEmbeddedPictures().size());
r.addPicture(new ByteArrayInputStream(new byte[0]), Document.PICTURE_TYPE_JPEG, "test.jpg", 21, 32);
assertEquals(1, hdr.getAllPictures().size());
assertEquals(1, r.getEmbeddedPictures().size());
XWPFPicture pic = r.getEmbeddedPictures().get(0);
assertEquals(pic.getWidth(), Units.toPoints(21), 0.0);
assertEquals(pic.getDepth(), Units.toPoints(32), 0.0);
}
}
@Test
public void testWhitespace() throws IOException {
String[] text = new String[] {
" The quick brown fox",
"\t\tjumped over the lazy dog"
};
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (XWPFDocument doc = new XWPFDocument();) {
for(String s : text) {
XWPFParagraph p1 = doc.createParagraph();
XWPFRun r1 = p1.createRun();
r1.setText(s);
}
doc.write(bos);
bos.flush();
}
try (
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
XWPFDocument doc = new XWPFDocument(bis)
) {
List<XWPFParagraph> paragraphs = doc.getParagraphs();
assertEquals(2, paragraphs.size());
for (int i = 0; i < text.length; i++) {
XWPFParagraph p1 = paragraphs.get(i);
String expected = text[i];
assertEquals(expected, p1.getText());
CTP ctp = p1.getCTP();
CTR ctr = ctp.getRArray(0);
CTText ctText = ctr.getTArray(0);
// if text has leading whitespace then expect xml-fragment to have xml:space="preserve" set
// <xml-fragment xml:space="preserve" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
boolean isWhitespace = Character.isWhitespace(expected.charAt(0));
assertEquals(isWhitespace, ctText.isSetSpace());
}
}
}
}
|