Line 0
Link Here
|
|
|
1 |
/* Licensed to the Apache Software Foundation (ASF) under one |
2 |
* or more contributor license agreements. See the NOTICE file |
3 |
* distributed with this work for additional information |
4 |
* regarding copyright ownership. The ASF licenses this file |
5 |
* to you under the Apache License, Version 2.0 (the |
6 |
* "License"); you may not use this file except in compliance |
7 |
* with the License. You may obtain a copy of the License at |
8 |
* |
9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
10 |
* |
11 |
* Unless required by applicable law or agreed to in writing, |
12 |
* software distributed under the License is distributed on an |
13 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 |
* KIND, either express or implied. See the License for the |
15 |
* specific language governing permissions and limitations |
16 |
* under the License. |
17 |
* |
18 |
*************************************************************/ |
19 |
|
20 |
package testcase.uno.sc.sheet; |
21 |
|
22 |
import static org.junit.Assert.*; |
23 |
import org.junit.After; |
24 |
import org.junit.AfterClass; |
25 |
import org.junit.Before; |
26 |
import org.junit.BeforeClass; |
27 |
import org.junit.Test; |
28 |
import org.openoffice.test.common.FileUtil; |
29 |
import org.openoffice.test.common.Testspace; |
30 |
import org.openoffice.test.uno.UnoApp; |
31 |
|
32 |
import com.sun.star.beans.PropertyValue; |
33 |
import com.sun.star.container.XIndexAccess; |
34 |
import com.sun.star.container.XNamed; |
35 |
import com.sun.star.frame.XModel; |
36 |
import com.sun.star.frame.XStorable; |
37 |
import com.sun.star.io.IOException; |
38 |
import com.sun.star.lang.IndexOutOfBoundsException; |
39 |
import com.sun.star.lang.WrappedTargetException; |
40 |
import com.sun.star.lang.XComponent; |
41 |
import com.sun.star.sheet.XSpreadsheet; |
42 |
import com.sun.star.sheet.XSpreadsheetDocument; |
43 |
import com.sun.star.sheet.XSpreadsheetView; |
44 |
import com.sun.star.sheet.XSpreadsheets; |
45 |
import com.sun.star.table.XCell; |
46 |
import com.sun.star.uno.UnoRuntime; |
47 |
|
48 |
public class SheetBasicTest { |
49 |
UnoApp unoApp = new UnoApp(); |
50 |
|
51 |
XSpreadsheetDocument scDocument = null; |
52 |
XComponent scComponent = null; |
53 |
|
54 |
|
55 |
@BeforeClass |
56 |
public static void setUpBeforeClass() throws Exception { |
57 |
|
58 |
} |
59 |
|
60 |
@AfterClass |
61 |
public static void tearDownAfterClass() throws Exception { |
62 |
} |
63 |
|
64 |
@Before |
65 |
public void setUp() throws Exception { |
66 |
unoApp.start(); |
67 |
//New a SC document |
68 |
scComponent = unoApp.newDocument("scalc"); |
69 |
} |
70 |
|
71 |
@After |
72 |
public void tearDown() throws Exception { |
73 |
unoApp.closeDocument(scComponent); |
74 |
unoApp.close(); |
75 |
} |
76 |
|
77 |
@Test |
78 |
public void insertRenameDeleteSheet() throws Exception { |
79 |
//Insert a sheet named aa after first sheet |
80 |
String sheetname ="aa"; |
81 |
scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, scComponent); |
82 |
XSpreadsheets spreadsheets = scDocument.getSheets(); |
83 |
spreadsheets.insertNewByName(sheetname, (short) 1); |
84 |
|
85 |
//active the sheet second sheet aa |
86 |
XIndexAccess xspreadsheetIndex = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class,spreadsheets); |
87 |
XSpreadsheet newSpreadSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xspreadsheetIndex.getByIndex(1)); |
88 |
XModel xSpreadsheetModel = (XModel)UnoRuntime.queryInterface(XModel.class, scDocument); |
89 |
XSpreadsheetView xSpeadsheetView = (XSpreadsheetView) UnoRuntime.queryInterface(XSpreadsheetView.class ,xSpreadsheetModel.getCurrentController()); |
90 |
xSpeadsheetView.setActiveSheet(newSpreadSheet); |
91 |
|
92 |
//get the new speadsheet name |
93 |
XNamed xsheetname = (XNamed)UnoRuntime.queryInterface(XNamed.class,newSpreadSheet); |
94 |
assertEquals("actual should equals aa",sheetname,xsheetname.getName()); |
95 |
|
96 |
|
97 |
//Change the Spreadsheet name |
98 |
String changedname="SpeadsheetAfterChange"; |
99 |
xsheetname.setName(changedname); |
100 |
|
101 |
//Save and reload document |
102 |
reloadSpreadsheet("TestSpreadsheet.xls"); |
103 |
|
104 |
//Verify the changed Spreadsheet name |
105 |
assertEquals("actual should equals SpeadsheetAfterChange",changedname,xsheetname.getName()); |
106 |
|
107 |
|
108 |
spreadsheets.removeByName(changedname); |
109 |
|
110 |
assertFalse("actual should equals false",spreadsheets.hasByName(changedname)); |
111 |
|
112 |
} |
113 |
|
114 |
@Test |
115 |
public void copypastesheet() throws Exception{ |
116 |
//Insert some value into cells |
117 |
scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, scComponent); |
118 |
XSpreadsheets spreadsheets = scDocument.getSheets(); |
119 |
XIndexAccess xspreadsheetIndex = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class,spreadsheets); |
120 |
XSpreadsheet spreadSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xspreadsheetIndex.getByIndex(0)); |
121 |
XNamed xsheetname = (XNamed)UnoRuntime.queryInterface(XNamed.class,spreadSheet); |
122 |
xsheetname.setName("sourcesheet"); |
123 |
String[][] stringValues = { |
124 |
{ "", "Jan", "Feb", "Mar", "Apr", "Mai" }, |
125 |
{ "Profit", "12.3", "43.2", "5.1", "76", "56.8" }, |
126 |
{ "Rival in business", "12.2", "12.6", "17.7", "20.4", "100" }, |
127 |
}; |
128 |
//input strings into sheet1 |
129 |
for ( int intY = 0; intY < stringValues.length; intY++ ) { |
130 |
for ( int intX = 0; intX < stringValues[ intY ].length; |
131 |
intX++ ) { |
132 |
// Insert the value to the cell, specified by intY and intX. |
133 |
this.insertIntoCell( intY, intX, |
134 |
stringValues[ intY ][ intX ], spreadSheet, "" ); |
135 |
} |
136 |
} |
137 |
|
138 |
//copy the sheet from sourcesheet to copysheet |
139 |
String newcopysheet = "copysheet"; |
140 |
spreadsheets.copyByName(xsheetname.getName(), newcopysheet, (short) 2); |
141 |
|
142 |
|
143 |
//Save and reload document |
144 |
reloadSpreadsheet("TestCopysheet.xls"); |
145 |
XModel xSpreadsheetModel = (XModel)UnoRuntime.queryInterface(XModel.class, scDocument); |
146 |
XSpreadsheetView xSpeadsheetView = (XSpreadsheetView) UnoRuntime.queryInterface(XSpreadsheetView.class ,xSpreadsheetModel.getCurrentController()); |
147 |
XSpreadsheet copysheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xspreadsheetIndex.getByIndex(2)); |
148 |
for ( int intY = 0; intY < stringValues.length; intY++ ) { |
149 |
for ( int intX = 0; intX < stringValues[ intY ].length; |
150 |
intX++ ) { |
151 |
XCell xcell = null; |
152 |
xcell = copysheet.getCellByPosition(intY, intX); |
153 |
assertEquals(stringValues[ intY ][ intX ] , xcell.getFormula()); |
154 |
} |
155 |
} |
156 |
|
157 |
} |
158 |
|
159 |
@Test |
160 |
public void movesheet() throws Exception{ |
161 |
|
162 |
//new sc document |
163 |
scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, scComponent); |
164 |
|
165 |
//change the first sheet name and input same value into the sheet cell |
166 |
XSpreadsheets spreadsheets = scDocument.getSheets(); |
167 |
XIndexAccess xspreadsheetIndex = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class,spreadsheets); |
168 |
XSpreadsheet spreadSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xspreadsheetIndex.getByIndex(0)); |
169 |
XNamed xsheetname = (XNamed)UnoRuntime.queryInterface(XNamed.class,spreadSheet); |
170 |
String sheetname = "sourcesheet"; |
171 |
xsheetname.setName(sheetname); |
172 |
String[][] stringValues = { |
173 |
{ "", "Jan", "Feb", "Mar", "Apr", "Mai" }, |
174 |
{ "Profit", "12.3", "43.2", "5.1", "76", "56.8" }, |
175 |
{ "Rival in business", "12.2", "12.6", "17.7", "20.4", "100" }, |
176 |
}; |
177 |
|
178 |
for ( int intY = 0; intY < stringValues.length; intY++ ) { |
179 |
for ( int intX = 0; intX < stringValues[ intY ].length; |
180 |
intX++ ) { |
181 |
// Insert the value to the cell, specified by intY and intX. |
182 |
this.insertIntoCell( intY, intX, |
183 |
stringValues[ intY ][ intX ], spreadSheet, "" ); |
184 |
} |
185 |
} |
186 |
|
187 |
//Before move, get the 2nd sheet name |
188 |
XSpreadsheet secondSheetBeforeMove = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xspreadsheetIndex.getByIndex(1)); |
189 |
XNamed secondSheetNameBeforeMove = (XNamed)UnoRuntime.queryInterface(XNamed.class,secondSheetBeforeMove); |
190 |
|
191 |
//move the first sheet |
192 |
spreadsheets.moveByName(sheetname, (short) 2); |
193 |
|
194 |
//Save and reload document |
195 |
reloadSpreadsheet("Testmovesheet.xls"); |
196 |
|
197 |
//After move, get the first sheet name, and verify it same as 2nd sheet name before move |
198 |
XSpreadsheet firstSheetAfterMove = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xspreadsheetIndex.getByIndex(0)); |
199 |
XNamed xfirstsheetnameAfterMove = (XNamed)UnoRuntime.queryInterface(XNamed.class,firstSheetAfterMove); |
200 |
assertEquals("Expect result should be Sheet2", secondSheetNameBeforeMove.getName(),xfirstsheetnameAfterMove.getName()); |
201 |
|
202 |
//Get the target sheet name after move |
203 |
XSpreadsheet sheetAfterMove = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xspreadsheetIndex.getByIndex(1)); |
204 |
XNamed xsheetnameAfterMove = (XNamed)UnoRuntime.queryInterface(XNamed.class,sheetAfterMove); |
205 |
assertEquals("Expect result should be sourcesheet",sheetname,xsheetnameAfterMove.getName()); |
206 |
|
207 |
//Check the cell value after move |
208 |
XModel xSpreadsheetModel = (XModel)UnoRuntime.queryInterface(XModel.class, scDocument); |
209 |
XSpreadsheetView xSpeadsheetView = (XSpreadsheetView) UnoRuntime.queryInterface(XSpreadsheetView.class ,xSpreadsheetModel.getCurrentController()); |
210 |
XSpreadsheet movesheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xspreadsheetIndex.getByIndex(1)); |
211 |
for ( int intY = 0; intY < stringValues.length; intY++ ) { |
212 |
for ( int intX = 0; intX < stringValues[ intY ].length; |
213 |
intX++ ) { |
214 |
XCell xcell = null; |
215 |
xcell = movesheet.getCellByPosition(intY, intX); |
216 |
assertEquals(stringValues[ intY ][ intX ] , xcell.getFormula()); |
217 |
} |
218 |
} |
219 |
|
220 |
} |
221 |
|
222 |
//input value into sheet cell |
223 |
public static void insertIntoCell(int intX, int intY, String stringValue, |
224 |
XSpreadsheet xspreadsheet, String stringFlag) { |
225 |
XCell xcell = null; |
226 |
|
227 |
try { |
228 |
xcell = xspreadsheet.getCellByPosition(intX, intY); |
229 |
} catch (com.sun.star.lang.IndexOutOfBoundsException exception) { |
230 |
System.out.println("Could not get cell."); |
231 |
} |
232 |
if (stringFlag.equals("V")) { |
233 |
xcell.setValue((new Float(stringValue)).floatValue()); |
234 |
} else { |
235 |
xcell.setFormula(stringValue); |
236 |
} |
237 |
} |
238 |
|
239 |
|
240 |
//Save and load the document |
241 |
public XSpreadsheetDocument reloadSpreadsheet(String spreadSheetname) throws Exception { |
242 |
String filePath = Testspace.getPath("output/" +spreadSheetname); |
243 |
XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, scDocument); |
244 |
PropertyValue[] aStoreProperties = new PropertyValue[2]; |
245 |
aStoreProperties[0] = new PropertyValue(); |
246 |
aStoreProperties[1] = new PropertyValue(); |
247 |
aStoreProperties[0].Name = "Override"; |
248 |
aStoreProperties[0].Value = true; |
249 |
aStoreProperties[1].Name = "FilterName"; |
250 |
aStoreProperties[1].Value = "MS Excel 97"; |
251 |
xStorable.storeAsURL(FileUtil.getUrl(filePath), aStoreProperties); |
252 |
|
253 |
return UnoRuntime.queryInterface(XSpreadsheetDocument.class, unoApp.loadDocument(filePath)); |
254 |
} |
255 |
} |