forked from thombergs/docx-stamper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatTableRowTest.java
More file actions
72 lines (61 loc) · 3.93 KB
/
RepeatTableRowTest.java
File metadata and controls
72 lines (61 loc) · 3.93 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
71
72
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.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 RepeatTableRowTest 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("RepeatTableRowTest.docx");
WordprocessingMLPackage document = stampAndLoad(template, context);
final List<TableRowCoordinates> rowCoords = new ArrayList<>();
CoordinatesWalker walker = new BaseCoordinatesWalker(document) {
@Override
protected void onTableRow(TableRowCoordinates tableRowCoordinates) {
rowCoords.add(tableRowCoordinates);
}
};
walker.walk();
// 1 header row + 1 row per character in list
Assert.assertEquals(7, rowCoords.size());
final List<TableCellCoordinates> cells = new ArrayList<>();
CoordinatesWalker cellWalker = new BaseCoordinatesWalker(document) {
@Override
protected void onTableCell(TableCellCoordinates tableCellCoordinates) {
cells.add(tableCellCoordinates);
}
};
cellWalker.walk();
Assert.assertEquals("Homer Simpson", new ParagraphWrapper((P) cells.get(2).getCell().getContent().get(0)).getText());
Assert.assertEquals("Dan Castellaneta", new ParagraphWrapper((P) cells.get(3).getCell().getContent().get(0)).getText());
Assert.assertEquals("Marge Simpson", new ParagraphWrapper((P) cells.get(4).getCell().getContent().get(0)).getText());
Assert.assertEquals("Julie Kavner", new ParagraphWrapper((P) cells.get(5).getCell().getContent().get(0)).getText());
Assert.assertEquals("Bart Simpson", new ParagraphWrapper((P) cells.get(6).getCell().getContent().get(0)).getText());
Assert.assertEquals("Nancy Cartwright", new ParagraphWrapper((P) cells.get(7).getCell().getContent().get(0)).getText());
Assert.assertEquals("Kent Brockman", new ParagraphWrapper((P) cells.get(8).getCell().getContent().get(0)).getText());
Assert.assertEquals("Harry Shearer", new ParagraphWrapper((P) cells.get(9).getCell().getContent().get(0)).getText());
Assert.assertEquals("Disco Stu", new ParagraphWrapper((P) cells.get(10).getCell().getContent().get(0)).getText());
Assert.assertEquals("Hank Azaria", new ParagraphWrapper((P) cells.get(11).getCell().getContent().get(0)).getText());
Assert.assertEquals("Krusty the Clown", new ParagraphWrapper((P) cells.get(12).getCell().getContent().get(0)).getText());
Assert.assertEquals("Dan Castellaneta", new ParagraphWrapper((P) cells.get(13).getCell().getContent().get(0)).getText());
}
}