1*07d7dbdcSHerbert Dürr /************************************************************** 2*07d7dbdcSHerbert Dürr * 3*07d7dbdcSHerbert Dürr * Licensed to the Apache Software Foundation (ASF) under one 4*07d7dbdcSHerbert Dürr * or more contributor license agreements. See the NOTICE file 5*07d7dbdcSHerbert Dürr * distributed with this work for additional information 6*07d7dbdcSHerbert Dürr * regarding copyright ownership. The ASF licenses this file 7*07d7dbdcSHerbert Dürr * to you under the Apache License, Version 2.0 (the 8*07d7dbdcSHerbert Dürr * "License"); you may not use this file except in compliance 9*07d7dbdcSHerbert Dürr * with the License. You may obtain a copy of the License at 10*07d7dbdcSHerbert Dürr * 11*07d7dbdcSHerbert Dürr * http://www.apache.org/licenses/LICENSE-2.0 12*07d7dbdcSHerbert Dürr * 13*07d7dbdcSHerbert Dürr * Unless required by applicable law or agreed to in writing, 14*07d7dbdcSHerbert Dürr * software distributed under the License is distributed on an 15*07d7dbdcSHerbert Dürr * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*07d7dbdcSHerbert Dürr * KIND, either express or implied. See the License for the 17*07d7dbdcSHerbert Dürr * specific language governing permissions and limitations 18*07d7dbdcSHerbert Dürr * under the License. 19*07d7dbdcSHerbert Dürr * 20*07d7dbdcSHerbert Dürr *************************************************************/ 21*07d7dbdcSHerbert Dürr 22eba4d44aSLiu Zhe package fvt.uno.sw.puretext; 23352c0eacSLiu Zhe 24352c0eacSLiu Zhe import static org.junit.Assert.*; 25352c0eacSLiu Zhe 26352c0eacSLiu Zhe import org.junit.After; 27352c0eacSLiu Zhe import org.junit.Before; 28352c0eacSLiu Zhe import org.junit.Test; 29352c0eacSLiu Zhe import org.openoffice.test.common.FileUtil; 30352c0eacSLiu Zhe import org.openoffice.test.common.Testspace; 31352c0eacSLiu Zhe import org.openoffice.test.uno.UnoApp; 32352c0eacSLiu Zhe 33352c0eacSLiu Zhe import com.sun.star.uno.UnoRuntime; 34352c0eacSLiu Zhe import com.sun.star.text.*; 35352c0eacSLiu Zhe import com.sun.star.lang.XMultiServiceFactory; 36352c0eacSLiu Zhe import com.sun.star.beans.PropertyValue; 37352c0eacSLiu Zhe import com.sun.star.container.XIndexAccess; 38352c0eacSLiu Zhe import com.sun.star.frame.XStorable; 39352c0eacSLiu Zhe 40352c0eacSLiu Zhe public class InsertCharacterToTable { 41352c0eacSLiu Zhe 42352c0eacSLiu Zhe private static final UnoApp app = new UnoApp(); 43352c0eacSLiu Zhe private XTextDocument xTextDocument = null; 44352c0eacSLiu Zhe private XMultiServiceFactory xWriterFactory = null; 45352c0eacSLiu Zhe private XText xText = null; 46352c0eacSLiu Zhe 47352c0eacSLiu Zhe @Before setUp()48352c0eacSLiu Zhe public void setUp() throws Exception { 49352c0eacSLiu Zhe app.start(); 50352c0eacSLiu Zhe } 51352c0eacSLiu Zhe 52352c0eacSLiu Zhe @After tearDown()53352c0eacSLiu Zhe public void tearDown() throws Exception { 54352c0eacSLiu Zhe app.close(); 55352c0eacSLiu Zhe } 56352c0eacSLiu Zhe 57352c0eacSLiu Zhe @Test testCreateTable()58352c0eacSLiu Zhe public void testCreateTable() throws Exception { 59352c0eacSLiu Zhe xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter")); 60352c0eacSLiu Zhe xText = xTextDocument.getText(); 61352c0eacSLiu Zhe XTextCursor xTextCursor = xText.createTextCursor(); 62352c0eacSLiu Zhe // get internal service factory of the document 63352c0eacSLiu Zhe xWriterFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument); 64352c0eacSLiu Zhe // Create a new table from the document's factory 65352c0eacSLiu Zhe XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class,xWriterFactory.createInstance("com.sun.star.text.TextTable")); 66352c0eacSLiu Zhe xTable.initialize(4, 4); 67352c0eacSLiu Zhe xText.insertTextContent(xTextCursor, xTable, false); 68352c0eacSLiu Zhe //insert text in to table cell 69352c0eacSLiu Zhe insertIntoCell( "A1","test", xTable ); 70352c0eacSLiu Zhe insertIntoCell( "C4","123", xTable ); 71352c0eacSLiu Zhe insertIntoCell( "D2","fsdf132134", xTable ); 72352c0eacSLiu Zhe insertIntoCell( "B3","*^$%^$^$", xTable ); 73352c0eacSLiu Zhe 74352c0eacSLiu Zhe //save to odt 75352c0eacSLiu Zhe XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument); 76352c0eacSLiu Zhe PropertyValue[] aStoreProperties_odt = new PropertyValue[2]; 77352c0eacSLiu Zhe aStoreProperties_odt[0] = new PropertyValue(); 78352c0eacSLiu Zhe aStoreProperties_odt[1] = new PropertyValue(); 79352c0eacSLiu Zhe aStoreProperties_odt[0].Name = "Override"; 80352c0eacSLiu Zhe aStoreProperties_odt[0].Value = true; 81352c0eacSLiu Zhe aStoreProperties_odt[1].Name = "FilterName"; 82352c0eacSLiu Zhe aStoreProperties_odt[1].Value = "StarOffice XML (Writer)"; 83352c0eacSLiu Zhe xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt); 84352c0eacSLiu Zhe //save to doc 85352c0eacSLiu Zhe XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument); 86352c0eacSLiu Zhe PropertyValue[] aStoreProperties_doc = new PropertyValue[2]; 87352c0eacSLiu Zhe aStoreProperties_doc[0] = new PropertyValue(); 88352c0eacSLiu Zhe aStoreProperties_doc[1] = new PropertyValue(); 89352c0eacSLiu Zhe aStoreProperties_doc[0].Name = "Override"; 90352c0eacSLiu Zhe aStoreProperties_doc[0].Value = true; 91352c0eacSLiu Zhe aStoreProperties_doc[1].Name = "FilterName"; 92352c0eacSLiu Zhe aStoreProperties_doc[1].Value = "MS Word 97"; 93352c0eacSLiu Zhe xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc); 94352c0eacSLiu Zhe app.closeDocument(xTextDocument); 95352c0eacSLiu Zhe 96352c0eacSLiu Zhe // reopen the document and assert create table successfully 97352c0eacSLiu Zhe XTextDocument assertDocument_odt = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,app.loadDocument(Testspace.getPath("output/test.odt"))); 98352c0eacSLiu Zhe XTextTablesSupplier xTablesSupplier_odt = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_odt); 99352c0eacSLiu Zhe XIndexAccess xIndexedTables_odt = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_odt.getTextTables()); 100352c0eacSLiu Zhe Object xTable_obj_odt = xIndexedTables_odt.getByIndex(0); 101352c0eacSLiu Zhe XTextTable xTable_Assert_odt = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_odt); 102352c0eacSLiu Zhe assertEquals("assert table cell text","test",getFromCell("A1", xTable_Assert_odt)); 103352c0eacSLiu Zhe assertEquals("assert table cell text","*^$%^$^$",getFromCell("B3", xTable_Assert_odt)); 104352c0eacSLiu Zhe assertEquals("assert table cell text","123",getFromCell("C4", xTable_Assert_odt)); 105352c0eacSLiu Zhe assertEquals("assert table cell text","fsdf132134",getFromCell("D2", xTable_Assert_odt)); 106352c0eacSLiu Zhe 107352c0eacSLiu Zhe // reopen the document and assert create table successfully 108352c0eacSLiu Zhe XTextDocument assertDocument_doc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,app.loadDocument(Testspace.getPath("output/test.doc"))); 109352c0eacSLiu Zhe XTextTablesSupplier xTablesSupplier_doc = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_doc); 110352c0eacSLiu Zhe XIndexAccess xIndexedTables_doc = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_doc.getTextTables()); 111352c0eacSLiu Zhe Object xTable_obj_doc = xIndexedTables_doc.getByIndex(0); 112352c0eacSLiu Zhe XTextTable xTable_Assert_doc = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_doc); 113352c0eacSLiu Zhe assertEquals("assert table cell text","test",getFromCell("A1", xTable_Assert_doc)); 114352c0eacSLiu Zhe assertEquals("assert table cell text","*^$%^$^$",getFromCell("B3", xTable_Assert_doc)); 115352c0eacSLiu Zhe assertEquals("assert table cell text","123",getFromCell("C4", xTable_Assert_doc)); 116352c0eacSLiu Zhe assertEquals("assert table cell text","fsdf132134",getFromCell("D2", xTable_Assert_doc)); 117352c0eacSLiu Zhe } 118352c0eacSLiu Zhe // This method is inserts string sText in table cell by sCellName. insertIntoCell(String sCellName, String sText, XTextTable xTable)119352c0eacSLiu Zhe public static void insertIntoCell(String sCellName, String sText, XTextTable xTable) { 120352c0eacSLiu Zhe // Access the XText interface of the cell referred to by sCellName 121352c0eacSLiu Zhe XText xCellText = (XText) UnoRuntime.queryInterface(XText.class, xTable.getCellByName(sCellName)); 122352c0eacSLiu Zhe // Set the text in the cell to sText 123352c0eacSLiu Zhe xCellText.setString(sText); 124352c0eacSLiu Zhe } 125352c0eacSLiu Zhe // This method is get string sText in table cell by sCellName. getFromCell(String sCellName, XTextTable xTable)126352c0eacSLiu Zhe public static String getFromCell(String sCellName, XTextTable xTable) { 127352c0eacSLiu Zhe // Access the XText interface of the cell referred to by sCellName 128352c0eacSLiu Zhe XText xCellText = (XText) UnoRuntime.queryInterface(XText.class, xTable.getCellByName(sCellName)); 129352c0eacSLiu Zhe // Set the text in the cell to sText 130352c0eacSLiu Zhe return xCellText.getString(); 131352c0eacSLiu Zhe } 132*07d7dbdcSHerbert Dürr } 133