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