search = null;
// 6 Simple, at the middle of original Paragraph, split original
// Paragraph, insert before the second Paragraph.
search = new TextNavigation("SIMPLE", doc);
while (search.hasNext()) {
TextSelection item = (TextSelection) search.nextSelection();
table = sourcedoc.getTableByName("Table1");
Cell cell = table.getCellByPosition(0, 0);
cell.setStringValue("SIMPLE");
Table newtable = item.replaceWith(table);
Assert.assertNotNull(newtable);
Assert.assertEquals(1, newtable.getHeaderColumnCount());
Assert.assertEquals(1, newtable.getHeaderRowCount());
Assert.assertEquals(7 + 1, newtable.getRowCount());
Assert.assertEquals(5 + 1, newtable.getColumnCount());
cell = newtable.getCellByPosition(1, 1);
Assert.assertEquals("string", cell.getValueType());
}
// 2 Task1, #1 at the start of original Paragraph, #2 replace original
// Paragraph
search = new TextNavigation("Task1", doc);
while (search.hasNext()) {
TextSelection item = (TextSelection) search.nextSelection();
table = sourcedoc.getTableByName("Table2");
Table newtable = item.replaceWith(table);
Cell cell = newtable.getCellByPosition(0, 0);
cell.setStringValue("From Source Table2");
Assert.assertNotNull(newtable);
Assert.assertEquals(1, newtable.getHeaderColumnCount());
Assert.assertEquals(1, newtable.getHeaderRowCount());
Assert.assertEquals(10 + 1, newtable.getRowCount());
Assert.assertEquals(4 + 1, newtable.getColumnCount());
cell = newtable.getCellByPosition(1, 1);
Assert.assertEquals("float", cell.getValueType());
}
// 1 RESS%>, #1 at the end of original Paragraph,
search = new TextNavigation("RESS%>", doc);
while (search.hasNext()) {
TextSelection item = (TextSelection) search.nextSelection();
table = sourcedoc.getTableByName("Table3");
Table newtable = item.replaceWith(table);
Cell cell = newtable.getCellByPosition(0, 0);
cell.setStringValue("From Source Table3");
Assert.assertNotNull(newtable);
Assert.assertEquals(5, newtable.getRowCount());
Assert.assertEquals(3, newtable.getColumnCount());
}
try {
doc.save(ResourceUtilities.newTestOutputFile("TextSelectionReplacewithTableResult.odt"));
} catch (Exception e) {
Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e);
Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
}
doc = (TextDocument) Document.loadDocument(ResourceUtilities
.getAbsolutePath(TEST_FILE));
search = new TextNavigation("<<target>>", doc);
int i = 0;
try {
while (search.hasNext()) {
i++;
TextSelection item = (TextSelection) search.nextSelection();
table = sourcedoc.getTableByName("Table1");
Table newtable = item.replaceWith(table);
Assert.assertNotNull(newtable);
Assert.assertEquals(1, newtable.getHeaderColumnCount());
Assert.assertEquals(1, newtable.getHeaderRowCount());
Assert.assertEquals(7 + 1, newtable.getRowCount());
Assert.assertEquals(5 + 1, newtable.getColumnCount());