1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
30 import java
.util
.Random
;
31 import java
.util
.StringTokenizer
;
33 import lib
.MultiMethodTest
;
35 import lib
.StatusException
;
37 import com
.sun
.star
.container
.XIndexAccess
;
38 import com
.sun
.star
.sheet
.Border
;
39 import com
.sun
.star
.sheet
.NamedRangeFlag
;
40 import com
.sun
.star
.sheet
.XCellRangeAddressable
;
41 import com
.sun
.star
.sheet
.XCellRangeReferrer
;
42 import com
.sun
.star
.sheet
.XNamedRanges
;
43 import com
.sun
.star
.sheet
.XSpreadsheet
;
44 import com
.sun
.star
.table
.CellAddress
;
45 import com
.sun
.star
.table
.CellRangeAddress
;
46 import com
.sun
.star
.table
.XCell
;
47 import com
.sun
.star
.table
.XCellRange
;
48 import com
.sun
.star
.text
.XTextRange
;
49 import com
.sun
.star
.uno
.UnoRuntime
;
52 * Testing <code>com.sun.star.sheet.XNamedRanges</code>
55 * <li><code> addNewByName()</code></li>
56 * <li><code> addNewFromTitles()</code></li>
57 * <li><code> removeByName()</code></li>
58 * <li><code> outputList()</code></li>
60 * This test needs the following object relations :
62 * <li> <code>'SHEET'</code> (of type <code>XSpreadsheet</code>):
63 * to have a spreadsheet </li>
65 * @see com.sun.star.sheet.XNamedRanges
66 * @see com.sun.star.sheet.XSpreadsheet
68 public class _XNamedRanges
extends MultiMethodTest
{
70 public XNamedRanges oObj
= null;
71 String name
= "_XNamedRanges";
72 XSpreadsheet oSheet
= null;
75 * Retrieves object relations.
76 * @throws StatusException If one of relations not found.
78 protected void before() {
79 oSheet
= (XSpreadsheet
)tEnv
.getObjRelation("SHEET");
80 if (oSheet
== null) throw new StatusException(Status
.failed
81 ("Relation 'SHEET' not found"));
85 * Test creates and stores random content and random type, calls the method
86 * and checks that new range exists in collection using method
87 * <code>hasByName()</code>. <p>
88 * Has <b> OK </b> status if new range exists in collection
89 * and no exceptions were thrown. <p>
91 public void _addNewByName() {
92 boolean bResult
= true;
93 CellAddress aPosition
= new CellAddress((short)0, 2, 2);
94 int nType
= getRandomType();
95 String sContent
= getRandomContent("D3;A6:B9;=F12");
97 log
.println("Adding new range with name=\"" + name
+
98 "\", sContent = \"" + sContent
+
99 "\", aPosition = (" + aPosition
.Sheet
+ ", "
100 + aPosition
.Column
+ ", "
102 "), Type = " + nType
+ ".");
104 oObj
.addNewByName(name
, sContent
, aPosition
, nType
);
107 CellAddress listOutputPosition
= new CellAddress((short)0, 1, 1);
108 oObj
.outputList(listOutputPosition
);
112 s
= oSheet
.getCellByPosition(1, 1).getFormula();
113 s1
= oSheet
.getCellByPosition(2, 1).getFormula();
114 } catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
115 log
.println("Can't get cell by position: " + e
);
118 log
.println("Outputlist returns: " + s
+ " " + s1
);
121 bResult
&= oObj
.hasByName(name
);
123 tRes
.tested("addNewByName()", bResult
);
127 * Test creates a table with left and top titles, creates new ranges from
128 * top titles and left titles, checks all new title ranges. <p>
129 * Has <b> OK </b> status if all required title ranges are present
130 * in collection, if each of them have valid size and position and
131 * no exceptions were thrown. <p>
133 public void _addNewFromTitles() {
134 boolean bResult
= true;
136 // First, create a small table.
137 log
.println("Creating a small table.");
140 XTextRange textrange
= null;
142 for (int i
= 1; i
< 4; i
++) {
143 cell
= oSheet
.getCellByPosition(0, i
);
144 textrange
= (XTextRange
)UnoRuntime
.
145 queryInterface(XTextRange
.class, cell
);
146 textrange
.setString("Row" + i
);
148 cell
= oSheet
.getCellByPosition(i
, 0);
149 textrange
= (XTextRange
)UnoRuntime
.
150 queryInterface(XTextRange
.class, cell
);
151 textrange
.setString("Column" + i
);
154 for (int i
= 1; i
< 4; i
++)
155 for (int j
= 1; j
< 4; j
++) {
156 cell
= oSheet
.getCellByPosition(i
, j
);
157 textrange
= (XTextRange
)UnoRuntime
.
158 queryInterface(XTextRange
.class, cell
);
159 textrange
.setString("Val" + ((j
- 1) * 3 + i
));
161 log
.println("Finished creating table.");
162 log
.println("Creating new ranges from titles");
164 CellRangeAddress CRA
= new CellRangeAddress((short)0, 0, 0, 3, 3);
165 Border border
= Border
.TOP
;
166 oObj
.addNewFromTitles(CRA
, border
);
167 for (int i
= 1; i
< 4; i
++) {
168 bResult
&= oObj
.hasByName("Column" + i
);
170 Object range
= oObj
.getByName("Column" + i
);
171 XCellRangeReferrer CRR
= (XCellRangeReferrer
)UnoRuntime
.
172 queryInterface(XCellRangeReferrer
.class,range
);
174 XCellRange CR
= CRR
.getReferredCells();
175 XCellRangeAddressable xCRA
= (XCellRangeAddressable
)
176 UnoRuntime
.queryInterface(XCellRangeAddressable
.class, CR
);
178 CellRangeAddress objCRA
= xCRA
.getRangeAddress();
180 bResult
&= (objCRA
.EndColumn
== i
&& objCRA
.StartColumn
== i
);
181 bResult
&= objCRA
.StartRow
== 1;
182 bResult
&= objCRA
.EndRow
== 3;
183 bResult
&= objCRA
.Sheet
== 0;
186 border
= Border
.LEFT
;
187 oObj
.addNewFromTitles(CRA
, border
);
188 for (int i
= 1; i
< 4; i
++) {
189 bResult
&= oObj
.hasByName("Row" + i
);
191 Object range
= oObj
.getByName("Row" + i
);
192 XCellRangeReferrer CRR
= (XCellRangeReferrer
)UnoRuntime
.
193 queryInterface(XCellRangeReferrer
.class,range
);
195 XCellRange CR
= CRR
.getReferredCells();
196 XCellRangeAddressable xCRA
= (XCellRangeAddressable
)
197 UnoRuntime
.queryInterface(XCellRangeAddressable
.class, CR
);
199 CellRangeAddress objCRA
= xCRA
.getRangeAddress();
201 bResult
&= (objCRA
.EndRow
== i
&& objCRA
.StartRow
== i
);
202 bResult
&= objCRA
.StartColumn
== 1;
203 bResult
&= objCRA
.EndColumn
== 3;
204 bResult
&= objCRA
.Sheet
== 0;
207 oObj
.outputList(new CellAddress((short)0, 5, 5));
209 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
210 e
.printStackTrace(log
);
212 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
213 e
.printStackTrace(log
);
215 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
216 e
.printStackTrace(log
);
220 tRes
.tested("addNewFromTitles()", bResult
);
224 * Test calls the method and checks existing of named ranges obtained
225 * by relation <code>'SHEET'</code>. <p>
226 * Has <b> OK </b> status if all output named ranges exist
227 * and no exceptions were thrown. <p>
229 public void _outputList() {
230 boolean bResult
= true;
231 CellAddress CA
= new CellAddress((short)0, 0, 0);
233 XIndexAccess IA
= (XIndexAccess
)UnoRuntime
.
234 queryInterface(XIndexAccess
.class, oObj
);
236 int elementsCount
= IA
.getCount();
241 for (int i
= 0; i
< elementsCount
; i
++) {
242 XCell cell
= oSheet
.getCellByPosition(0, i
);
243 XTextRange textrange
= (XTextRange
)
244 UnoRuntime
.queryInterface(XTextRange
.class, cell
);
245 String str
= textrange
.getString();
246 bResult
&= oObj
.hasByName(str
);
248 } catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
249 e
.printStackTrace(log
);
253 tRes
.tested("outputList()", bResult
);
257 * Test calls the method for existing range, checks number of ranges in
258 * collection after method call, calls method for non-existent named range.
259 * <p>Has <b> OK </b> status if number of named ranges is less by one than
260 * before method call and exception was thrown during second call. <p>
261 * The following method tests are to be completed successfully before :
263 * <li> <code> addNewByName() </code> : to have name of existent
267 public void _removeByName() {
268 requiredMethod("addNewByName()");
269 boolean bResult
= true;
270 XIndexAccess IA
= (XIndexAccess
)UnoRuntime
.
271 queryInterface(XIndexAccess
.class, oObj
);
273 int elementsCount
= IA
.getCount();
275 // Removing existent element
276 oObj
.removeByName(name
);
277 bResult
= elementsCount
== IA
.getCount() + 1;
280 // Removing unexistent element.
281 oObj
.removeByName(name
);
282 log
.println("Exception expected when removed unexistent element!");
284 } catch (com
.sun
.star
.uno
.RuntimeException e
) {
285 log
.println("Expected exception occurred while testing" +
286 "removeByName() when removed unexistent element.");
290 tRes
.tested("removeByName()", bResult
);
294 * Method make string of random content.
295 * @return string of random content
297 String
getRandomContent(String str
) {
299 Random rnd
= new Random();
301 StringTokenizer ST
= new StringTokenizer(str
, ";");
302 int nr
= rnd
.nextInt(ST
.countTokens());
306 for (int i
=1; i
< nr
+ 1; i
++)
307 gRS
= ST
.nextToken();
313 * Returns random value of named range flag.
317 NamedRangeFlag
.COLUMN_HEADER
,
318 NamedRangeFlag
.FILTER_CRITERIA
,
319 NamedRangeFlag
.PRINT_AREA
,
320 NamedRangeFlag
.ROW_HEADER
323 Random rnd
= new Random();
324 return types
[rnd
.nextInt(5)];