1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package fvt.uno.sw.puretext;
23 
24 import static org.junit.Assert.*;
25 
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.openoffice.test.common.FileUtil;
30 import org.openoffice.test.common.Testspace;
31 import org.openoffice.test.uno.UnoApp;
32 
33 import com.sun.star.uno.UnoRuntime;
34 import com.sun.star.text.*;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.beans.PropertyValue;
37 import com.sun.star.container.XIndexAccess;
38 import com.sun.star.frame.XStorable;
39 
40 public class InsertCharacterToTable {
41 
42 	private static final UnoApp app = new UnoApp();
43 	private XTextDocument xTextDocument = null;
44 	private XMultiServiceFactory xWriterFactory = null;
45 	private XText xText = null;
46 
47 	@Before
setUp()48 	public void setUp() throws Exception {
49 		app.start();
50 	}
51 
52 	@After
tearDown()53 	public void tearDown() throws Exception {
54 		app.close();
55 	}
56 
57 	@Test
testCreateTable()58 	public void testCreateTable() throws Exception {
59 		xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));
60 		xText = xTextDocument.getText();
61 		XTextCursor xTextCursor = xText.createTextCursor();
62 		// get internal service factory of the document
63 		xWriterFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
64 		// Create a new table from the document's factory
65 		XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class,xWriterFactory.createInstance("com.sun.star.text.TextTable"));
66 		xTable.initialize(4, 4);
67 		xText.insertTextContent(xTextCursor, xTable, false);
68         //insert text in to table cell
69 		insertIntoCell( "A1","test", xTable );
70 		insertIntoCell( "C4","123", xTable );
71 		insertIntoCell( "D2","fsdf132134", xTable );
72 		insertIntoCell( "B3","*^$%^$^$", xTable );
73 
74 		//save to odt
75 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
76 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
77 		aStoreProperties_odt[0] = new PropertyValue();
78 		aStoreProperties_odt[1] = new PropertyValue();
79 		aStoreProperties_odt[0].Name = "Override";
80 		aStoreProperties_odt[0].Value = true;
81 		aStoreProperties_odt[1].Name = "FilterName";
82 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
83 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
84 		//save to doc
85 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
86 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
87 		aStoreProperties_doc[0] = new PropertyValue();
88 		aStoreProperties_doc[1] = new PropertyValue();
89 		aStoreProperties_doc[0].Name = "Override";
90 		aStoreProperties_doc[0].Value = true;
91 		aStoreProperties_doc[1].Name = "FilterName";
92 		aStoreProperties_doc[1].Value = "MS Word 97";
93 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
94 		app.closeDocument(xTextDocument);
95 
96 		// reopen the document and assert create table successfully
97 		XTextDocument assertDocument_odt = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,app.loadDocument(Testspace.getPath("output/test.odt")));
98 		XTextTablesSupplier xTablesSupplier_odt = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_odt);
99 		XIndexAccess xIndexedTables_odt = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_odt.getTextTables());
100 		Object xTable_obj_odt = xIndexedTables_odt.getByIndex(0);
101 		XTextTable xTable_Assert_odt = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_odt);
102 		assertEquals("assert table cell text","test",getFromCell("A1", xTable_Assert_odt));
103 		assertEquals("assert table cell text","*^$%^$^$",getFromCell("B3", xTable_Assert_odt));
104 		assertEquals("assert table cell text","123",getFromCell("C4", xTable_Assert_odt));
105 		assertEquals("assert table cell text","fsdf132134",getFromCell("D2", xTable_Assert_odt));
106 
107 		// reopen the document and assert create table successfully
108 		XTextDocument assertDocument_doc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,app.loadDocument(Testspace.getPath("output/test.doc")));
109 		XTextTablesSupplier xTablesSupplier_doc = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_doc);
110 		XIndexAccess xIndexedTables_doc = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_doc.getTextTables());
111 		Object xTable_obj_doc = xIndexedTables_doc.getByIndex(0);
112 		XTextTable xTable_Assert_doc = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_doc);
113 		assertEquals("assert table cell text","test",getFromCell("A1", xTable_Assert_doc));
114 		assertEquals("assert table cell text","*^$%^$^$",getFromCell("B3", xTable_Assert_doc));
115 		assertEquals("assert table cell text","123",getFromCell("C4", xTable_Assert_doc));
116 		assertEquals("assert table cell text","fsdf132134",getFromCell("D2", xTable_Assert_doc));
117 	}
118 	// This method is inserts string sText in table cell by sCellName.
insertIntoCell(String sCellName, String sText, XTextTable xTable)119 	public static void insertIntoCell(String sCellName, String sText, XTextTable xTable) {
120 		// Access the XText interface of the cell referred to by sCellName
121 		XText xCellText = (XText) UnoRuntime.queryInterface(XText.class, xTable.getCellByName(sCellName));
122 		// Set the text in the cell to sText
123 		xCellText.setString(sText);
124 	}
125 	// This method is get string sText in table cell by sCellName.
getFromCell(String sCellName, XTextTable xTable)126 	public static String getFromCell(String sCellName, XTextTable xTable) {
127 		// Access the XText interface of the cell referred to by sCellName
128 		XText xCellText = (XText) UnoRuntime.queryInterface(XText.class, xTable.getCellByName(sCellName));
129 		// Set the text in the cell to sText
130 		return xCellText.getString();
131 	}
132 }
133