Update ooo320-m1
[ooovba.git] / qadevOOo / tests / java / ifc / sheet / _XNamedRanges.java
blobab5b064bc52d90345512fd6aca0278c7b4c86487
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XNamedRanges.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc.sheet;
33 import java.util.Random;
34 import java.util.StringTokenizer;
36 import lib.MultiMethodTest;
37 import lib.Status;
38 import lib.StatusException;
40 import com.sun.star.container.XIndexAccess;
41 import com.sun.star.sheet.Border;
42 import com.sun.star.sheet.NamedRangeFlag;
43 import com.sun.star.sheet.XCellRangeAddressable;
44 import com.sun.star.sheet.XCellRangeReferrer;
45 import com.sun.star.sheet.XNamedRanges;
46 import com.sun.star.sheet.XSpreadsheet;
47 import com.sun.star.table.CellAddress;
48 import com.sun.star.table.CellRangeAddress;
49 import com.sun.star.table.XCell;
50 import com.sun.star.table.XCellRange;
51 import com.sun.star.text.XTextRange;
52 import com.sun.star.uno.UnoRuntime;
54 /**
55 * Testing <code>com.sun.star.sheet.XNamedRanges</code>
56 * interface methods :
57 * <ul>
58 * <li><code> addNewByName()</code></li>
59 * <li><code> addNewFromTitles()</code></li>
60 * <li><code> removeByName()</code></li>
61 * <li><code> outputList()</code></li>
62 * </ul> <p>
63 * This test needs the following object relations :
64 * <ul>
65 * <li> <code>'SHEET'</code> (of type <code>XSpreadsheet</code>):
66 * to have a spreadsheet </li>
67 * <ul> <p>
68 * @see com.sun.star.sheet.XNamedRanges
69 * @see com.sun.star.sheet.XSpreadsheet
71 public class _XNamedRanges extends MultiMethodTest {
73 public XNamedRanges oObj = null;
74 String name = "_XNamedRanges";
75 XSpreadsheet oSheet = null;
77 /**
78 * Retrieves object relations.
79 * @throws StatusException If one of relations not found.
81 protected void before() {
82 oSheet = (XSpreadsheet)tEnv.getObjRelation("SHEET");
83 if (oSheet == null) throw new StatusException(Status.failed
84 ("Relation 'SHEET' not found"));
87 /**
88 * Test creates and stores random content and random type, calls the method
89 * and checks that new range exists in collection using method
90 * <code>hasByName()</code>. <p>
91 * Has <b> OK </b> status if new range exists in collection
92 * and no exceptions were thrown. <p>
94 public void _addNewByName() {
95 boolean bResult = true;
96 CellAddress aPosition = new CellAddress((short)0, 2, 2);
97 int nType = getRandomType();
98 String sContent = getRandomContent("D3;A6:B9;=F12");
99 name += sContent;
100 log.println("Adding new range with name=\"" + name +
101 "\", sContent = \"" + sContent +
102 "\", aPosition = (" + aPosition.Sheet + ", "
103 + aPosition.Column + ", "
104 + aPosition.Row +
105 "), Type = " + nType + ".");
107 oObj.addNewByName(name, sContent, aPosition, nType);
109 //inserted for a bug
110 CellAddress listOutputPosition = new CellAddress((short)0, 1, 1);
111 oObj.outputList(listOutputPosition);
112 String s = null;
113 String s1 = null;
114 try {
115 s = oSheet.getCellByPosition(1, 1).getFormula();
116 s1 = oSheet.getCellByPosition(2, 1).getFormula();
117 } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
118 log.println("Can't get cell by position: " + e);
119 bResult = false;
121 log.println("Outputlist returns: " + s + " " + s1);
122 //end of insertion
124 bResult &= oObj.hasByName(name);
126 tRes.tested("addNewByName()", bResult);
130 * Test creates a table with left and top titles, creates new ranges from
131 * top titles and left titles, checks all new title ranges. <p>
132 * Has <b> OK </b> status if all required title ranges are present
133 * in collection, if each of them have valid size and position and
134 * no exceptions were thrown. <p>
136 public void _addNewFromTitles() {
137 boolean bResult = true;
139 // First, create a small table.
140 log.println("Creating a small table.");
141 try {
142 XCell cell = null;
143 XTextRange textrange = null;
145 for (int i = 1; i < 4; i++) {
146 cell = oSheet.getCellByPosition(0, i);
147 textrange = (XTextRange)UnoRuntime.
148 queryInterface(XTextRange.class, cell);
149 textrange.setString("Row" + i);
151 cell = oSheet.getCellByPosition(i, 0);
152 textrange = (XTextRange)UnoRuntime.
153 queryInterface(XTextRange.class, cell);
154 textrange.setString("Column" + i);
157 for (int i = 1; i < 4; i++)
158 for (int j = 1; j < 4; j++) {
159 cell = oSheet.getCellByPosition(i, j);
160 textrange = (XTextRange)UnoRuntime.
161 queryInterface(XTextRange.class, cell);
162 textrange.setString("Val" + ((j - 1) * 3 + i));
164 log.println("Finished creating table.");
165 log.println("Creating new ranges from titles");
167 CellRangeAddress CRA = new CellRangeAddress((short)0, 0, 0, 3, 3);
168 Border border = Border.TOP;
169 oObj.addNewFromTitles(CRA, border);
170 for (int i = 1; i < 4; i++) {
171 bResult &= oObj.hasByName("Column" + i);
173 Object range = oObj.getByName("Column" + i);
174 XCellRangeReferrer CRR = (XCellRangeReferrer)UnoRuntime.
175 queryInterface(XCellRangeReferrer.class,range);
177 XCellRange CR = CRR.getReferredCells();
178 XCellRangeAddressable xCRA = (XCellRangeAddressable)
179 UnoRuntime.queryInterface(XCellRangeAddressable.class, CR);
181 CellRangeAddress objCRA = xCRA.getRangeAddress();
183 bResult &= (objCRA.EndColumn == i && objCRA.StartColumn == i);
184 bResult &= objCRA.StartRow == 1;
185 bResult &= objCRA.EndRow == 3;
186 bResult &= objCRA.Sheet == 0;
189 border = Border.LEFT;
190 oObj.addNewFromTitles(CRA, border);
191 for (int i = 1; i < 4; i++) {
192 bResult &= oObj.hasByName("Row" + i);
194 Object range = oObj.getByName("Row" + i);
195 XCellRangeReferrer CRR = (XCellRangeReferrer)UnoRuntime.
196 queryInterface(XCellRangeReferrer.class,range);
198 XCellRange CR = CRR.getReferredCells();
199 XCellRangeAddressable xCRA = (XCellRangeAddressable)
200 UnoRuntime.queryInterface(XCellRangeAddressable.class, CR);
202 CellRangeAddress objCRA = xCRA.getRangeAddress();
204 bResult &= (objCRA.EndRow == i && objCRA.StartRow == i);
205 bResult &= objCRA.StartColumn == 1;
206 bResult &= objCRA.EndColumn == 3;
207 bResult &= objCRA.Sheet == 0;
210 oObj.outputList(new CellAddress((short)0, 5, 5));
212 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
213 e.printStackTrace(log);
214 bResult = false;
215 } catch (com.sun.star.lang.WrappedTargetException e) {
216 e.printStackTrace(log);
217 bResult = false;
218 } catch (com.sun.star.container.NoSuchElementException e) {
219 e.printStackTrace(log);
220 bResult = false;
223 tRes.tested("addNewFromTitles()", bResult);
227 * Test calls the method and checks existing of named ranges obtained
228 * by relation <code>'SHEET'</code>. <p>
229 * Has <b> OK </b> status if all output named ranges exist
230 * and no exceptions were thrown. <p>
232 public void _outputList() {
233 boolean bResult = true;
234 CellAddress CA = new CellAddress((short)0, 0, 0);
236 XIndexAccess IA = (XIndexAccess)UnoRuntime.
237 queryInterface(XIndexAccess.class, oObj);
239 int elementsCount = IA.getCount();
241 oObj.outputList(CA);
243 try {
244 for (int i = 0; i < elementsCount; i++) {
245 XCell cell = oSheet.getCellByPosition(0, i);
246 XTextRange textrange = (XTextRange)
247 UnoRuntime.queryInterface(XTextRange.class, cell);
248 String str = textrange.getString();
249 bResult &= oObj.hasByName(str);
251 } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
252 e.printStackTrace(log);
253 bResult = false;
256 tRes.tested("outputList()", bResult);
260 * Test calls the method for existing range, checks number of ranges in
261 * collection after method call, calls method for non-existent named range.
262 * <p>Has <b> OK </b> status if number of named ranges is less by one than
263 * before method call and exception was thrown during second call. <p>
264 * The following method tests are to be completed successfully before :
265 * <ul>
266 * <li> <code> addNewByName() </code> : to have name of existent
267 * named range </li>
268 * </ul>
270 public void _removeByName() {
271 requiredMethod("addNewByName()");
272 boolean bResult = true;
273 XIndexAccess IA = (XIndexAccess)UnoRuntime.
274 queryInterface(XIndexAccess.class, oObj);
276 int elementsCount = IA.getCount();
278 // Removing existent element
279 oObj.removeByName(name);
280 bResult = elementsCount == IA.getCount() + 1;
282 try {
283 // Removing unexistent element.
284 oObj.removeByName(name);
285 log.println("Exception expected when removed unexistent element!");
286 bResult = false;
287 } catch (com.sun.star.uno.RuntimeException e) {
288 log.println("Expected exception occured while testing" +
289 "removeByName() when removed unexistent element.");
293 tRes.tested("removeByName()", bResult);
297 * Method make string of random content.
298 * @return string of random content
300 String getRandomContent(String str) {
301 String gRS = "none";
302 Random rnd = new Random();
304 StringTokenizer ST = new StringTokenizer(str, ";");
305 int nr = rnd.nextInt(ST.countTokens());
306 if (nr < 1)
307 nr++;
309 for (int i=1; i < nr + 1; i++)
310 gRS = ST.nextToken();
312 return gRS;
316 * Returns random value of named range flag.
318 int getRandomType(){
319 int types[] = { 0,
320 NamedRangeFlag.COLUMN_HEADER,
321 NamedRangeFlag.FILTER_CRITERIA,
322 NamedRangeFlag.PRINT_AREA,
323 NamedRangeFlag.ROW_HEADER
326 Random rnd = new Random();
327 return types[rnd.nextInt(5)];