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