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