2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import java
.util
.Random
;
22 import java
.util
.StringTokenizer
;
24 import lib
.MultiMethodTest
;
26 import lib
.StatusException
;
28 import com
.sun
.star
.container
.XIndexAccess
;
29 import com
.sun
.star
.sheet
.Border
;
30 import com
.sun
.star
.sheet
.NamedRangeFlag
;
31 import com
.sun
.star
.sheet
.XCellRangeAddressable
;
32 import com
.sun
.star
.sheet
.XCellRangeReferrer
;
33 import com
.sun
.star
.sheet
.XNamedRanges
;
34 import com
.sun
.star
.sheet
.XSpreadsheet
;
35 import com
.sun
.star
.table
.CellAddress
;
36 import com
.sun
.star
.table
.CellRangeAddress
;
37 import com
.sun
.star
.table
.XCell
;
38 import com
.sun
.star
.table
.XCellRange
;
39 import com
.sun
.star
.text
.XTextRange
;
40 import com
.sun
.star
.uno
.UnoRuntime
;
43 * Testing <code>com.sun.star.sheet.XNamedRanges</code>
46 * <li><code> addNewByName()</code></li>
47 * <li><code> addNewFromTitles()</code></li>
48 * <li><code> removeByName()</code></li>
49 * <li><code> outputList()</code></li>
51 * This test needs the following object relations :
53 * <li> <code>'SHEET'</code> (of type <code>XSpreadsheet</code>):
54 * to have a spreadsheet </li>
56 * @see com.sun.star.sheet.XNamedRanges
57 * @see com.sun.star.sheet.XSpreadsheet
59 public class _XNamedRanges
extends MultiMethodTest
{
61 public XNamedRanges oObj
= null;
62 String name
= "_XNamedRanges";
63 XSpreadsheet oSheet
= null;
66 * Retrieves object relations.
67 * @throws StatusException If one of relations not found.
70 protected void before() {
71 oSheet
= (XSpreadsheet
)tEnv
.getObjRelation("SHEET");
72 if (oSheet
== null) throw new StatusException(Status
.failed
73 ("Relation 'SHEET' not found"));
77 * Test creates and stores random content and random type, calls the method
78 * and checks that new range exists in collection using method
79 * <code>hasByName()</code>. <p>
80 * Has <b> OK </b> status if new range exists in collection
81 * and no exceptions were thrown. <p>
83 public void _addNewByName() {
84 boolean bResult
= true;
85 CellAddress aPosition
= new CellAddress((short)0, 2, 2);
86 int nType
= getRandomType();
87 String sContent
= getRandomContent("D3;A6:B9;=F12");
89 log
.println("Adding new range with name=\"" + name
+
90 "\", sContent = \"" + sContent
+
91 "\", aPosition = (" + aPosition
.Sheet
+ ", "
92 + aPosition
.Column
+ ", "
94 "), Type = " + nType
+ ".");
96 oObj
.addNewByName(name
, sContent
, aPosition
, nType
);
99 CellAddress listOutputPosition
= new CellAddress((short)0, 1, 1);
100 oObj
.outputList(listOutputPosition
);
104 s
= oSheet
.getCellByPosition(1, 1).getFormula();
105 s1
= oSheet
.getCellByPosition(2, 1).getFormula();
106 } catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
107 log
.println("Can't get cell by position: " + e
);
110 log
.println("Outputlist returns: " + s
+ " " + s1
);
113 bResult
&= oObj
.hasByName(name
);
115 tRes
.tested("addNewByName()", bResult
);
119 * Test creates a table with left and top titles, creates new ranges from
120 * top titles and left titles, checks all new title ranges. <p>
121 * Has <b> OK </b> status if all required title ranges are present
122 * in collection, if each of them have valid size and position and
123 * no exceptions were thrown. <p>
125 public void _addNewFromTitles() {
126 boolean bResult
= true;
128 // First, create a small table.
129 log
.println("Creating a small table.");
132 XTextRange textrange
= null;
134 for (int i
= 1; i
< 4; i
++) {
135 cell
= oSheet
.getCellByPosition(0, i
);
136 textrange
= UnoRuntime
.
137 queryInterface(XTextRange
.class, cell
);
138 textrange
.setString("Row" + i
);
140 cell
= oSheet
.getCellByPosition(i
, 0);
141 textrange
= UnoRuntime
.
142 queryInterface(XTextRange
.class, cell
);
143 textrange
.setString("Column" + i
);
146 for (int i
= 1; i
< 4; i
++)
147 for (int j
= 1; j
< 4; j
++) {
148 cell
= oSheet
.getCellByPosition(i
, j
);
149 textrange
= UnoRuntime
.
150 queryInterface(XTextRange
.class, cell
);
151 textrange
.setString("Val" + ((j
- 1) * 3 + i
));
153 log
.println("Finished creating table.");
154 log
.println("Creating new ranges from titles");
156 CellRangeAddress CRA
= new CellRangeAddress((short)0, 0, 0, 3, 3);
157 Border border
= Border
.TOP
;
158 oObj
.addNewFromTitles(CRA
, border
);
159 for (int i
= 1; i
< 4; i
++) {
160 bResult
&= oObj
.hasByName("Column" + i
);
162 Object range
= oObj
.getByName("Column" + i
);
163 XCellRangeReferrer CRR
= UnoRuntime
.
164 queryInterface(XCellRangeReferrer
.class,range
);
166 XCellRange CR
= CRR
.getReferredCells();
167 XCellRangeAddressable xCRA
= UnoRuntime
.queryInterface(XCellRangeAddressable
.class, CR
);
169 CellRangeAddress objCRA
= xCRA
.getRangeAddress();
171 bResult
&= (objCRA
.EndColumn
== i
&& objCRA
.StartColumn
== i
);
172 bResult
&= objCRA
.StartRow
== 1;
173 bResult
&= objCRA
.EndRow
== 3;
174 bResult
&= objCRA
.Sheet
== 0;
177 border
= Border
.LEFT
;
178 oObj
.addNewFromTitles(CRA
, border
);
179 for (int i
= 1; i
< 4; i
++) {
180 bResult
&= oObj
.hasByName("Row" + i
);
182 Object range
= oObj
.getByName("Row" + i
);
183 XCellRangeReferrer CRR
= UnoRuntime
.
184 queryInterface(XCellRangeReferrer
.class,range
);
186 XCellRange CR
= CRR
.getReferredCells();
187 XCellRangeAddressable xCRA
= UnoRuntime
.queryInterface(XCellRangeAddressable
.class, CR
);
189 CellRangeAddress objCRA
= xCRA
.getRangeAddress();
191 bResult
&= (objCRA
.EndRow
== i
&& objCRA
.StartRow
== i
);
192 bResult
&= objCRA
.StartColumn
== 1;
193 bResult
&= objCRA
.EndColumn
== 3;
194 bResult
&= objCRA
.Sheet
== 0;
197 oObj
.outputList(new CellAddress((short)0, 5, 5));
199 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
200 e
.printStackTrace(log
);
202 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
203 e
.printStackTrace(log
);
205 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
206 e
.printStackTrace(log
);
210 tRes
.tested("addNewFromTitles()", bResult
);
214 * Test calls the method and checks existing of named ranges obtained
215 * by relation <code>'SHEET'</code>. <p>
216 * Has <b> OK </b> status if all output named ranges exist
217 * and no exceptions were thrown. <p>
219 public void _outputList() {
220 boolean bResult
= true;
221 CellAddress CA
= new CellAddress((short)0, 0, 0);
223 XIndexAccess IA
= UnoRuntime
.
224 queryInterface(XIndexAccess
.class, oObj
);
226 int elementsCount
= IA
.getCount();
231 for (int i
= 0; i
< elementsCount
; i
++) {
232 XCell cell
= oSheet
.getCellByPosition(0, i
);
233 XTextRange textrange
= UnoRuntime
.queryInterface(XTextRange
.class, cell
);
234 String str
= textrange
.getString();
235 bResult
&= oObj
.hasByName(str
);
237 } catch(com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
238 e
.printStackTrace(log
);
242 tRes
.tested("outputList()", bResult
);
246 * Test calls the method for existing range, checks number of ranges in
247 * collection after method call, calls method for non-existent named range.
248 * <p>Has <b> OK </b> status if number of named ranges is less by one than
249 * before method call and exception was thrown during second call. <p>
250 * The following method tests are to be completed successfully before :
252 * <li> <code> addNewByName() </code> : to have name of existent
256 public void _removeByName() {
257 requiredMethod("addNewByName()");
258 boolean bResult
= true;
259 XIndexAccess IA
= UnoRuntime
.
260 queryInterface(XIndexAccess
.class, oObj
);
262 int elementsCount
= IA
.getCount();
264 // Removing existent element
265 oObj
.removeByName(name
);
266 bResult
= elementsCount
== IA
.getCount() + 1;
269 // Removing unexistent element.
270 oObj
.removeByName(name
);
271 log
.println("Exception expected when removed unexistent element!");
273 } catch (com
.sun
.star
.uno
.RuntimeException e
) {
274 log
.println("Expected exception occurred while testing" +
275 "removeByName() when removed unexistent element.");
279 tRes
.tested("removeByName()", bResult
);
283 * Method make string of random content.
284 * @return string of random content
286 String
getRandomContent(String str
) {
288 Random rnd
= new Random();
290 StringTokenizer ST
= new StringTokenizer(str
, ";");
291 int nr
= rnd
.nextInt(ST
.countTokens());
295 for (int i
=1; i
< nr
+ 1; i
++)
296 gRS
= ST
.nextToken();
302 * Returns random value of named range flag.
306 NamedRangeFlag
.COLUMN_HEADER
,
307 NamedRangeFlag
.FILTER_CRITERIA
,
308 NamedRangeFlag
.PRINT_AREA
,
309 NamedRangeFlag
.ROW_HEADER
312 Random rnd
= new Random();
313 return types
[rnd
.nextInt(5)];