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