forked from thombergs/docx-stamper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatParagraphTest.java
More file actions
70 lines (61 loc) · 4 KB
/
RepeatParagraphTest.java
File metadata and controls
70 lines (61 loc) · 4 KB
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
package org.wickedsource.docxstamper;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.wml.P;
import org.junit.Assert;
import org.junit.Test;
import org.wickedsource.docxstamper.api.coordinates.ParagraphCoordinates;
import org.wickedsource.docxstamper.api.coordinates.TableCellCoordinates;
import org.wickedsource.docxstamper.api.coordinates.TableRowCoordinates;
import org.wickedsource.docxstamper.context.Character;
import org.wickedsource.docxstamper.context.CharactersContext;
import org.wickedsource.docxstamper.util.ParagraphWrapper;
import org.wickedsource.docxstamper.util.walk.BaseCoordinatesWalker;
import org.wickedsource.docxstamper.util.walk.CoordinatesWalker;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class RepeatParagraphTest extends AbstractDocx4jTest {
@Test
public void test() throws Docx4JException, IOException {
CharactersContext context = new CharactersContext();
context.getCharacters().add(new Character("Homer Simpson", "Dan Castellaneta"));
context.getCharacters().add(new Character("Marge Simpson", "Julie Kavner"));
context.getCharacters().add(new Character("Bart Simpson", "Nancy Cartwright"));
context.getCharacters().add(new Character("Kent Brockman", "Harry Shearer"));
context.getCharacters().add(new Character("Disco Stu", "Hank Azaria"));
context.getCharacters().add(new Character("Krusty the Clown", "Dan Castellaneta"));
InputStream template = getClass().getResourceAsStream("RepeatParagraphTest.docx");
WordprocessingMLPackage document = stampAndLoad(template, context);
final List<ParagraphCoordinates> titleCoords = new ArrayList<>();
final List<ParagraphCoordinates> quotationCoords = new ArrayList<>();
CoordinatesWalker walker = new BaseCoordinatesWalker(document) {
@Override
protected void onParagraph(ParagraphCoordinates paragraphCoordinates) {
if ("Titre2".equals(paragraphCoordinates.getParagraph().getPPr().getPStyle().getVal())) {
titleCoords.add(paragraphCoordinates);
} else if ("Quotations".equals(paragraphCoordinates.getParagraph().getPPr().getPStyle().getVal())) {
quotationCoords.add(paragraphCoordinates);
}
}
};
walker.walk();
// 6 titles and 6 quotations are expected
Assert.assertEquals(6, titleCoords.size());
Assert.assertEquals(6, quotationCoords.size());
// Check paragraph's content
Assert.assertEquals("Homer Simpson", new ParagraphWrapper(titleCoords.get(0).getParagraph()).getText());
Assert.assertEquals("Dan Castellaneta", new ParagraphWrapper(quotationCoords.get(0).getParagraph()).getText());
Assert.assertEquals("Marge Simpson", new ParagraphWrapper(titleCoords.get(1).getParagraph()).getText());
Assert.assertEquals("Julie Kavner", new ParagraphWrapper(quotationCoords.get(1).getParagraph()).getText());
Assert.assertEquals("Bart Simpson", new ParagraphWrapper(titleCoords.get(2).getParagraph()).getText());
Assert.assertEquals("Nancy Cartwright", new ParagraphWrapper(quotationCoords.get(2).getParagraph()).getText());
Assert.assertEquals("Kent Brockman", new ParagraphWrapper(titleCoords.get(3).getParagraph()).getText());
Assert.assertEquals("Harry Shearer", new ParagraphWrapper(quotationCoords.get(3).getParagraph()).getText());
Assert.assertEquals("Disco Stu", new ParagraphWrapper(titleCoords.get(4).getParagraph()).getText());
Assert.assertEquals("Hank Azaria", new ParagraphWrapper(quotationCoords.get(4).getParagraph()).getText());
Assert.assertEquals("Krusty the Clown", new ParagraphWrapper(titleCoords.get(5).getParagraph()).getText());
Assert.assertEquals("Dan Castellaneta", new ParagraphWrapper(quotationCoords.get(5).getParagraph()).getText());
}
}