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.table;
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.beans.XPropertySet;
38 import com.sun.star.container.XIndexAccess;
39 import com.sun.star.frame.XStorable;
40 
41 
42 public class TableBorderSpacingtoContent {
43 
44 	private static final UnoApp app = new UnoApp();
45 	private XTextDocument xTextDocument=null;
46 	private XMultiServiceFactory xWriterFactory=null;
47 	private XText xText=null;
48 	@Before
setUp()49 	public void setUp() throws Exception {
50 		app.start();
51 	}
52 
53 	@After
tearDown()54 	public void tearDown() throws Exception {
55 		app.close();
56 	}
57 	/*
58 	 * test table border spacing to content
59 	 * 1.new a text document and create a table
60 	 * 2.set table border spacing to content
61 	 * 3.save to odt/doc,close it and reopen new saved document
62 	 * 4.check the table border spacing to content
63 	 */
64 	@Test
testtableBorderSpacingtoContent()65 	public void testtableBorderSpacingtoContent() throws Exception {
66 		xTextDocument =(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));
67 		xText=xTextDocument.getText();
68 		XTextCursor xTextCursor = xText.createTextCursor();
69 		// get internal service factory of the document
70 		xWriterFactory =(XMultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
71 		// Create a new table from the document's factory
72 		XTextTable xTable = (XTextTable)UnoRuntime.queryInterface(XTextTable.class, xWriterFactory.createInstance("com.sun.star.text.TextTable"));
73 		xText.insertTextContent(xTextCursor,xTable,false);
74 		String[] cellName=xTable.getCellNames();
75 		int i=0;
76 		while(cellName[i] != null)
77 		{
78 		XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable.getCellByName(cellName[i]));
79 		xCursorProps.setPropertyValue("LeftBorderDistance",499);
80 		xCursorProps.setPropertyValue("RightBorderDistance",499);
81 		xCursorProps.setPropertyValue("TopBorderDistance",499);
82 		xCursorProps.setPropertyValue("BottomBorderDistance",499);
83 		i++;
84 		if(i==4)break;
85 		}
86 		//save to odt
87 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
88 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
89 		aStoreProperties_odt[0] = new PropertyValue();
90 		aStoreProperties_odt[1] = new PropertyValue();
91 		aStoreProperties_odt[0].Name = "Override";
92 		aStoreProperties_odt[0].Value = true;
93 		aStoreProperties_odt[1].Name = "FilterName";
94 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
95 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
96 		//save to doc
97 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
98 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
99 		aStoreProperties_doc[0] = new PropertyValue();
100 		aStoreProperties_doc[1] = new PropertyValue();
101 		aStoreProperties_doc[0].Name = "Override";
102 		aStoreProperties_doc[0].Value = true;
103 		aStoreProperties_doc[1].Name = "FilterName";
104 		aStoreProperties_doc[1].Value = "MS Word 97";
105 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
106 		app.closeDocument(xTextDocument);
107 
108 		//reopen the odt document and assert table border spacing to content
109 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
110 		XTextTablesSupplier xTablesSupplier_odt = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_odt );
111 		XIndexAccess xIndexedTables_odt = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_odt.getTextTables());
112 		Object xTable_obj_odt=xIndexedTables_odt.getByIndex(0);
113 		XTextTable xTable_Assert_odt=(XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_odt);
114 		String[] cellName_assert_odt=xTable_Assert_odt.getCellNames();
115 		int j=0;
116 		while(cellName_assert_odt[j] != null)
117 		{
118 		XPropertySet xCursorProps_assert_odt = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable_Assert_odt.getCellByName(cellName_assert_odt[j]));
119 		assertEquals("assert table border spacing to content",499,xCursorProps_assert_odt.getPropertyValue("LeftBorderDistance"));
120 		assertEquals("assert table border spacing to content",499,xCursorProps_assert_odt.getPropertyValue("RightBorderDistance"));
121 		assertEquals("assert table border spacing to content",499,xCursorProps_assert_odt.getPropertyValue("TopBorderDistance"));
122 		assertEquals("assert table border spacing to content",499,xCursorProps_assert_odt.getPropertyValue("BottomBorderDistance"));
123 		j++;
124 		if(j==4)break;
125 		}
126 
127 		//reopen the doc document and assert table border spacing to content
128 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
129 		XTextTablesSupplier xTablesSupplier_doc = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, assertDocument_doc );
130 		XIndexAccess xIndexedTables_doc = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xTablesSupplier_doc.getTextTables());
131 		Object xTable_obj_doc=xIndexedTables_doc.getByIndex(0);
132 		XTextTable xTable_Assert_doc=(XTextTable) UnoRuntime.queryInterface(XTextTable.class, xTable_obj_doc);
133 		String[] cellName_assert_doc=xTable_Assert_doc.getCellNames();
134 		int k=0;
135 		while(cellName_assert_doc[k] != null)
136 		{
137 		XPropertySet xCursorProps_assert_doc = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xTable_Assert_doc.getCellByName(cellName_assert_doc[k]));
138 		assertEquals("assert table border spacing to content",499,xCursorProps_assert_doc.getPropertyValue("LeftBorderDistance"));
139 		assertEquals("assert table border spacing to content",499,xCursorProps_assert_doc.getPropertyValue("RightBorderDistance"));
140 		assertEquals("assert table border spacing to content",499,xCursorProps_assert_doc.getPropertyValue("TopBorderDistance"));
141 		assertEquals("assert table border spacing to content",499,xCursorProps_assert_doc.getPropertyValue("BottomBorderDistance"));
142 		k++;
143 		if(k==4)break;
144 		}
145 	}
146 }
147 
148