1 
2 
3 
4 
5 package fvt.uno.sc.rowcolumn;
6 
7 import static org.junit.Assert.*;
8 
9 
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 
14 import org.openoffice.test.uno.UnoApp;
15 
16 
17 import com.sun.star.lang.XComponent;
18 import com.sun.star.sheet.XSpreadsheet;
19 import com.sun.star.sheet.XSpreadsheetDocument;
20 import com.sun.star.sheet.XSpreadsheets;
21 import com.sun.star.table.XCell;
22 import com.sun.star.uno.UnoRuntime;
23 import com.sun.star.table.XTableColumns;
24 import com.sun.star.table.XTableRows;
25 import com.sun.star.table.XColumnRowRange;
26 
27 /**
28  * Test insert or delete rows and columns
29  * @author test
30  *
31  */
32 
33 public class InsertDeleteRowAndColumn {
34 
35 	UnoApp unoApp = new UnoApp();
36 	XSpreadsheetDocument scDocument = null;
37 	XComponent scComponent = null;
38 
39 	@Before
40 	public void setUp() throws Exception {
41 		unoApp.start();
42 	}
43 
44 	@After
45 	public void tearDown() throws Exception {
46 		unoApp.closeDocument(scComponent);
47 		unoApp.close();
48 		}
49 
50 	@Test
51 	public void testInsertDeleteRows() throws Exception {
52 
53 		String sheetname = "sheet1";
54 		scComponent = unoApp.newDocument("scalc");
55 		scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, scComponent);
56 		XSpreadsheets spreadsheets = scDocument.getSheets();
57 		Object sheetObj = spreadsheets.getByName(sheetname);
58 
59 
60 		XSpreadsheet sheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
61 		XColumnRowRange xCRRange = (XColumnRowRange) UnoRuntime.queryInterface( XColumnRowRange.class, sheet );
62 	    XTableRows xRows = xCRRange.getRows();
63 
64 		// Create a cell series "A2:A8" with the values 1 ... 7.
65 	    int nRow = 1;
66 	    for (int i = 1; i < 8; ++i) {
67 	        sheet.getCellByPosition( 0, nRow ).setValue( nRow );
68             nRow += 1;
69 		}
70 
71         //Insert a row between row 2 and row 3
72    		xRows.insertByIndex( 2, 1 );
73 
74         //Get value of cell A3
75         XCell cell = sheet.getCellByPosition(0, 2);
76         double checkvalue = 0.0;
77 
78         //Verify after insert row
79         assertEquals("Verify one new row inserted after Row 2",checkvalue, cell.getValue(),0);
80 
81         //Delete the row 3 and row 4
82         xRows.removeByIndex( 2, 2 );
83 
84         //Get value of cell A3 and A4
85         XCell cellA3 = sheet.getCellByPosition(0, 2);
86         XCell cellA4 = sheet.getCellByPosition(0, 3);
87         double checkvalueA3 = 3.0;
88         double checkvalueA4 = 4.0;
89 
90         //Verify after delete row3 and row4
91         assertEquals("Verify tow rows deleted the value of row 3",checkvalueA3, cellA3.getValue(),0);
92         assertEquals("Verify tow rows deleted the value of row 4",checkvalueA4, cellA4.getValue(),0);
93 
94 }
95 
96 @Test
97 public void testInsertDeleteColumns() throws Exception {
98 
99 	String sheetname = "sheet1";
100 	scComponent = unoApp.newDocument("scalc");
101 	scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, scComponent);
102 	XSpreadsheets spreadsheets = scDocument.getSheets();
103 	Object sheetObj = spreadsheets.getByName(sheetname);
104 
105 
106 	XSpreadsheet sheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
107 	XColumnRowRange xCRRange = (XColumnRowRange) UnoRuntime.queryInterface( XColumnRowRange.class, sheet );
108     XTableColumns xColumns = xCRRange.getColumns();
109 
110 	// Create a cell series "A2:A8" with the values 1 ... 7.
111     int nRow = 1;
112     for (int i = 1; i < 8; ++i) {
113         sheet.getCellByPosition( 1, nRow ).setValue( nRow );
114         nRow += 1;
115 	}
116 
117     //Insert a row between row 2 and row 3
118 	xColumns.insertByIndex( 0, 1 );
119 
120     //Get value of cell C2
121     XCell cell = sheet.getCellByPosition(2, 1);
122     double checkvalue = 1.0;
123 
124     //Verify after insert row
125     assertEquals("Verify if one new column inserted after Column A",checkvalue, cell.getValue(),0);
126 
127     //Delete the row 3 and row 4
128     xColumns.removeByIndex( 0, 1 );
129 
130     //Get value of cell A3 and A4
131     XCell cellA3 = sheet.getCellByPosition(1, 2);
132     XCell cellA4 = sheet.getCellByPosition(1, 3);
133     double checkvalueA3 = 2.0;
134     double checkvalueA4 = 3.0;
135 
136     //Verify after delete row3 and row4
137     assertEquals("Verify after tow rows deleted, the value of A3",checkvalueA3, cellA3.getValue(),0);
138     assertEquals("Verify after tow rows deleted, the value of A4",checkvalueA4, cellA4.getValue(),0);
139 
140   }
141 
142 }
143 
144