xref: /trunk/test/testuno/source/fvt/uno/sw/puretext/InsertCharacterToTable.java (revision 45e2f390c041b30a26ca2aee1e99e305783ca105)
1 package fvt.uno.sw.puretext;
2 
3 import static org.junit.Assert.*;
4 
5 import org.junit.After;
6 import org.junit.Before;
7 import org.junit.Test;
8 import org.openoffice.test.common.FileUtil;
9 import org.openoffice.test.common.Testspace;
10 import org.openoffice.test.uno.UnoApp;
11 
12 import com.sun.star.uno.UnoRuntime;
13 import com.sun.star.text.*;
14 import com.sun.star.lang.XMultiServiceFactory;
15 import com.sun.star.beans.PropertyValue;
16 import com.sun.star.container.XIndexAccess;
17 import com.sun.star.frame.XStorable;
18 
19 public class InsertCharacterToTable {
20 
21     private static final UnoApp app = new UnoApp();
22     private XTextDocument xTextDocument = null;
23     private XMultiServiceFactory xWriterFactory = null;
24     private XText xText = null;
25 
26     @Before
27     public void setUp() throws Exception {
28         app.start();
29     }
30 
31     @After
32     public void tearDown() throws Exception {
33         app.close();
34     }
35 
36     @Test
37     public void testCreateTable() throws Exception {
38         xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));
39         xText = xTextDocument.getText();
40         XTextCursor xTextCursor = xText.createTextCursor();
41         // get internal service factory of the document
42         xWriterFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
43         // Create a new table from the document's factory
44         XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class,xWriterFactory.createInstance("com.sun.star.text.TextTable"));
45         xTable.initialize(4, 4);
46         xText.insertTextContent(xTextCursor, xTable, false);
47         //insert text in to table cell
48         insertIntoCell( "A1","test", xTable );
49         insertIntoCell( "C4","123", xTable );
50         insertIntoCell( "D2","fsdf132134", xTable );
51         insertIntoCell( "B3","*^$%^$^$", xTable );
52 
53         //save to odt
54         XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
55         PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
56         aStoreProperties_odt[0] = new PropertyValue();
57         aStoreProperties_odt[1] = new PropertyValue();
58         aStoreProperties_odt[0].Name = "Override";
59         aStoreProperties_odt[0].Value = true;
60         aStoreProperties_odt[1].Name = "FilterName";
61         aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
62         xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
63         //save to doc
64         XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
65         PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
66         aStoreProperties_doc[0] = new PropertyValue();
67         aStoreProperties_doc[1] = new PropertyValue();
68         aStoreProperties_doc[0].Name = "Override";
69         aStoreProperties_doc[0].Value = true;
70         aStoreProperties_doc[1].Name = "FilterName";
71         aStoreProperties_doc[1].Value = "MS Word 97";
72         xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
73         app.closeDocument(xTextDocument);
74 
75         // reopen the document and assert create table successfully
76         XTextDocument assertDocument_odt = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,app.loadDocument(Testspace.getPath("output/test.odt")));
77         XTextTablesSupplier xTablesSupplier_odt = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_odt);
78         XIndexAccess xIndexedTables_odt = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_odt.getTextTables());
79         Object xTable_obj_odt = xIndexedTables_odt.getByIndex(0);
80         XTextTable xTable_Assert_odt = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_odt);
81         assertEquals("assert table cell text","test",getFromCell("A1", xTable_Assert_odt));
82         assertEquals("assert table cell text","*^$%^$^$",getFromCell("B3", xTable_Assert_odt));
83         assertEquals("assert table cell text","123",getFromCell("C4", xTable_Assert_odt));
84         assertEquals("assert table cell text","fsdf132134",getFromCell("D2", xTable_Assert_odt));
85 
86         // reopen the document and assert create table successfully
87         XTextDocument assertDocument_doc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,app.loadDocument(Testspace.getPath("output/test.doc")));
88         XTextTablesSupplier xTablesSupplier_doc = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_doc);
89         XIndexAccess xIndexedTables_doc = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_doc.getTextTables());
90         Object xTable_obj_doc = xIndexedTables_doc.getByIndex(0);
91         XTextTable xTable_Assert_doc = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_doc);
92         assertEquals("assert table cell text","test",getFromCell("A1", xTable_Assert_doc));
93         assertEquals("assert table cell text","*^$%^$^$",getFromCell("B3", xTable_Assert_doc));
94         assertEquals("assert table cell text","123",getFromCell("C4", xTable_Assert_doc));
95         assertEquals("assert table cell text","fsdf132134",getFromCell("D2", xTable_Assert_doc));
96     }
97     // This method is inserts string sText in table cell by sCellName.
98     public static void insertIntoCell(String sCellName, String sText, XTextTable xTable) {
99         // Access the XText interface of the cell referred to by sCellName
100         XText xCellText = (XText) UnoRuntime.queryInterface(XText.class, xTable.getCellByName(sCellName));
101         // Set the text in the cell to sText
102         xCellText.setString(sText);
103     }
104     // This method is get string sText in table cell by sCellName.
105     public static String getFromCell(String sCellName, XTextTable xTable) {
106         // Access the XText interface of the cell referred to by sCellName
107         XText xCellText = (XText) UnoRuntime.queryInterface(XText.class, xTable.getCellByName(sCellName));
108         // Set the text in the cell to sText
109         return xCellText.getString();
110     }
111 }