xref: /AOO41X/main/qadevOOo/tests/java/ifc/sheet/_XNamedRanges.java (revision ef39d40d3f5e66cf3f035b3e93783012b340500d)
1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package ifc.sheet;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.util.Random;
27cdf0e10cSrcweir import java.util.StringTokenizer;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import lib.MultiMethodTest;
30cdf0e10cSrcweir import lib.Status;
31cdf0e10cSrcweir import lib.StatusException;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
34cdf0e10cSrcweir import com.sun.star.sheet.Border;
35cdf0e10cSrcweir import com.sun.star.sheet.NamedRangeFlag;
36cdf0e10cSrcweir import com.sun.star.sheet.XCellRangeAddressable;
37cdf0e10cSrcweir import com.sun.star.sheet.XCellRangeReferrer;
38cdf0e10cSrcweir import com.sun.star.sheet.XNamedRanges;
39cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet;
40cdf0e10cSrcweir import com.sun.star.table.CellAddress;
41cdf0e10cSrcweir import com.sun.star.table.CellRangeAddress;
42cdf0e10cSrcweir import com.sun.star.table.XCell;
43cdf0e10cSrcweir import com.sun.star.table.XCellRange;
44cdf0e10cSrcweir import com.sun.star.text.XTextRange;
45cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir /**
48cdf0e10cSrcweir * Testing <code>com.sun.star.sheet.XNamedRanges</code>
49cdf0e10cSrcweir * interface methods :
50cdf0e10cSrcweir * <ul>
51cdf0e10cSrcweir *  <li><code> addNewByName()</code></li>
52cdf0e10cSrcweir *  <li><code> addNewFromTitles()</code></li>
53cdf0e10cSrcweir *  <li><code> removeByName()</code></li>
54cdf0e10cSrcweir *  <li><code> outputList()</code></li>
55cdf0e10cSrcweir * </ul> <p>
56cdf0e10cSrcweir * This test needs the following object relations :
57cdf0e10cSrcweir * <ul>
58cdf0e10cSrcweir *  <li> <code>'SHEET'</code> (of type <code>XSpreadsheet</code>):
59cdf0e10cSrcweir *   to have a spreadsheet </li>
60cdf0e10cSrcweir * <ul> <p>
61cdf0e10cSrcweir * @see com.sun.star.sheet.XNamedRanges
62cdf0e10cSrcweir * @see com.sun.star.sheet.XSpreadsheet
63cdf0e10cSrcweir */
64cdf0e10cSrcweir public class _XNamedRanges extends MultiMethodTest {
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     public XNamedRanges oObj = null;
67cdf0e10cSrcweir     String name = "_XNamedRanges";
68cdf0e10cSrcweir     XSpreadsheet oSheet = null;
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     /**
71cdf0e10cSrcweir     * Retrieves object relations.
72cdf0e10cSrcweir     * @throws StatusException If one of relations not found.
73cdf0e10cSrcweir     */
before()74cdf0e10cSrcweir     protected void before() {
75cdf0e10cSrcweir         oSheet = (XSpreadsheet)tEnv.getObjRelation("SHEET");
76cdf0e10cSrcweir         if (oSheet == null) throw new StatusException(Status.failed
77cdf0e10cSrcweir             ("Relation 'SHEET' not found"));
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     /**
81cdf0e10cSrcweir     * Test creates and stores random content and random type, calls the method
82cdf0e10cSrcweir     * and checks that new range exists in collection using method
83cdf0e10cSrcweir     * <code>hasByName()</code>. <p>
84cdf0e10cSrcweir     * Has <b> OK </b> status if new range exists in collection
85cdf0e10cSrcweir     * and no exceptions were thrown. <p>
86cdf0e10cSrcweir     */
_addNewByName()87cdf0e10cSrcweir     public void _addNewByName() {
88cdf0e10cSrcweir         boolean bResult = true;
89cdf0e10cSrcweir         CellAddress aPosition = new CellAddress((short)0, 2, 2);
90cdf0e10cSrcweir         int nType = getRandomType();
91cdf0e10cSrcweir         String sContent = getRandomContent("D3;A6:B9;=F12");
92cdf0e10cSrcweir         name += sContent;
93cdf0e10cSrcweir         log.println("Adding new range with name=\"" + name +
94cdf0e10cSrcweir                     "\", sContent = \"" + sContent +
95cdf0e10cSrcweir                     "\", aPosition = (" + aPosition.Sheet + ", "
96cdf0e10cSrcweir                                         + aPosition.Column + ", "
97cdf0e10cSrcweir                                         + aPosition.Row +
98cdf0e10cSrcweir                     "), Type = " + nType + ".");
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         oObj.addNewByName(name, sContent, aPosition, nType);
101cdf0e10cSrcweir 
102cdf0e10cSrcweir         //inserted for a bug
103cdf0e10cSrcweir         CellAddress listOutputPosition = new CellAddress((short)0, 1, 1);
104cdf0e10cSrcweir         oObj.outputList(listOutputPosition);
105cdf0e10cSrcweir         String s = null;
106cdf0e10cSrcweir         String s1 = null;
107cdf0e10cSrcweir         try {
108cdf0e10cSrcweir             s = oSheet.getCellByPosition(1, 1).getFormula();
109cdf0e10cSrcweir             s1 = oSheet.getCellByPosition(2, 1).getFormula();
110cdf0e10cSrcweir         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
111cdf0e10cSrcweir             log.println("Can't get cell by position: " + e);
112cdf0e10cSrcweir             bResult = false;
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir         log.println("Outputlist returns: " + s + " " + s1);
115cdf0e10cSrcweir         //end of insertion
116cdf0e10cSrcweir 
117cdf0e10cSrcweir         bResult &= oObj.hasByName(name);
118cdf0e10cSrcweir 
119cdf0e10cSrcweir         tRes.tested("addNewByName()", bResult);
120cdf0e10cSrcweir     }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     /**
123cdf0e10cSrcweir     * Test creates a table with left and top titles, creates new ranges from
124cdf0e10cSrcweir     * top titles and left titles, checks all new title ranges. <p>
125cdf0e10cSrcweir     * Has <b> OK </b> status if all required title ranges are present
126cdf0e10cSrcweir     * in collection, if each of them have valid size and position and
127cdf0e10cSrcweir     * no exceptions were thrown. <p>
128cdf0e10cSrcweir     */
_addNewFromTitles()129cdf0e10cSrcweir     public void _addNewFromTitles() {
130cdf0e10cSrcweir         boolean bResult = true;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         // First, create a small table.
133cdf0e10cSrcweir         log.println("Creating a small table.");
134cdf0e10cSrcweir         try {
135cdf0e10cSrcweir             XCell cell = null;
136cdf0e10cSrcweir             XTextRange textrange = null;
137cdf0e10cSrcweir 
138cdf0e10cSrcweir             for (int i = 1; i < 4; i++) {
139cdf0e10cSrcweir                 cell = oSheet.getCellByPosition(0, i);
140cdf0e10cSrcweir                 textrange = (XTextRange)UnoRuntime.
141cdf0e10cSrcweir                                     queryInterface(XTextRange.class, cell);
142cdf0e10cSrcweir                 textrange.setString("Row" + i);
143cdf0e10cSrcweir 
144cdf0e10cSrcweir                 cell = oSheet.getCellByPosition(i, 0);
145cdf0e10cSrcweir                 textrange = (XTextRange)UnoRuntime.
146cdf0e10cSrcweir                                     queryInterface(XTextRange.class, cell);
147cdf0e10cSrcweir                 textrange.setString("Column" + i);
148cdf0e10cSrcweir             }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir             for (int i = 1; i < 4; i++)
151cdf0e10cSrcweir                 for (int j = 1; j < 4; j++) {
152cdf0e10cSrcweir                     cell = oSheet.getCellByPosition(i, j);
153cdf0e10cSrcweir                     textrange = (XTextRange)UnoRuntime.
154cdf0e10cSrcweir                                     queryInterface(XTextRange.class, cell);
155cdf0e10cSrcweir                     textrange.setString("Val" + ((j - 1) * 3 + i));
156cdf0e10cSrcweir                 }
157cdf0e10cSrcweir             log.println("Finished creating table.");
158cdf0e10cSrcweir             log.println("Creating new ranges from titles");
159cdf0e10cSrcweir 
160cdf0e10cSrcweir             CellRangeAddress CRA = new CellRangeAddress((short)0, 0, 0, 3, 3);
161cdf0e10cSrcweir             Border border = Border.TOP;
162cdf0e10cSrcweir             oObj.addNewFromTitles(CRA, border);
163cdf0e10cSrcweir             for (int i = 1; i < 4; i++) {
164cdf0e10cSrcweir                 bResult &= oObj.hasByName("Column" + i);
165cdf0e10cSrcweir 
166cdf0e10cSrcweir                 Object range = oObj.getByName("Column" + i);
167cdf0e10cSrcweir                 XCellRangeReferrer CRR = (XCellRangeReferrer)UnoRuntime.
168cdf0e10cSrcweir                                 queryInterface(XCellRangeReferrer.class,range);
169cdf0e10cSrcweir 
170cdf0e10cSrcweir                 XCellRange CR = CRR.getReferredCells();
171cdf0e10cSrcweir                 XCellRangeAddressable xCRA = (XCellRangeAddressable)
172cdf0e10cSrcweir                     UnoRuntime.queryInterface(XCellRangeAddressable.class, CR);
173cdf0e10cSrcweir 
174cdf0e10cSrcweir                 CellRangeAddress objCRA = xCRA.getRangeAddress();
175cdf0e10cSrcweir 
176cdf0e10cSrcweir                 bResult &= (objCRA.EndColumn == i && objCRA.StartColumn == i);
177cdf0e10cSrcweir                 bResult &= objCRA.StartRow == 1;
178cdf0e10cSrcweir                 bResult &= objCRA.EndRow == 3;
179cdf0e10cSrcweir                 bResult &= objCRA.Sheet == 0;
180cdf0e10cSrcweir             }
181cdf0e10cSrcweir 
182cdf0e10cSrcweir             border = Border.LEFT;
183cdf0e10cSrcweir             oObj.addNewFromTitles(CRA, border);
184cdf0e10cSrcweir             for (int i = 1; i < 4; i++) {
185cdf0e10cSrcweir                 bResult &= oObj.hasByName("Row" + i);
186cdf0e10cSrcweir 
187cdf0e10cSrcweir                 Object range = oObj.getByName("Row" + i);
188cdf0e10cSrcweir                 XCellRangeReferrer CRR = (XCellRangeReferrer)UnoRuntime.
189cdf0e10cSrcweir                                 queryInterface(XCellRangeReferrer.class,range);
190cdf0e10cSrcweir 
191cdf0e10cSrcweir                 XCellRange CR = CRR.getReferredCells();
192cdf0e10cSrcweir                 XCellRangeAddressable xCRA = (XCellRangeAddressable)
193cdf0e10cSrcweir                     UnoRuntime.queryInterface(XCellRangeAddressable.class, CR);
194cdf0e10cSrcweir 
195cdf0e10cSrcweir                 CellRangeAddress objCRA = xCRA.getRangeAddress();
196cdf0e10cSrcweir 
197cdf0e10cSrcweir                 bResult &= (objCRA.EndRow == i && objCRA.StartRow == i);
198cdf0e10cSrcweir                 bResult &= objCRA.StartColumn == 1;
199cdf0e10cSrcweir                 bResult &= objCRA.EndColumn == 3;
200cdf0e10cSrcweir                 bResult &= objCRA.Sheet == 0;
201cdf0e10cSrcweir             }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir             oObj.outputList(new CellAddress((short)0, 5, 5));
204cdf0e10cSrcweir 
205cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
206cdf0e10cSrcweir             e.printStackTrace(log);
207cdf0e10cSrcweir             bResult = false;
208cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
209cdf0e10cSrcweir             e.printStackTrace(log);
210cdf0e10cSrcweir             bResult = false;
211cdf0e10cSrcweir         } catch (com.sun.star.container.NoSuchElementException e) {
212cdf0e10cSrcweir             e.printStackTrace(log);
213cdf0e10cSrcweir             bResult = false;
214cdf0e10cSrcweir         }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir         tRes.tested("addNewFromTitles()", bResult);
217cdf0e10cSrcweir     }
218cdf0e10cSrcweir 
219cdf0e10cSrcweir     /**
220cdf0e10cSrcweir     * Test calls the method and checks existing of named ranges obtained
221cdf0e10cSrcweir     * by relation <code>'SHEET'</code>. <p>
222cdf0e10cSrcweir     * Has <b> OK </b> status if all output named ranges exist
223cdf0e10cSrcweir     * and no exceptions were thrown. <p>
224cdf0e10cSrcweir     */
_outputList()225cdf0e10cSrcweir     public void _outputList() {
226cdf0e10cSrcweir         boolean bResult = true;
227cdf0e10cSrcweir         CellAddress CA = new CellAddress((short)0, 0, 0);
228cdf0e10cSrcweir 
229cdf0e10cSrcweir         XIndexAccess IA = (XIndexAccess)UnoRuntime.
230cdf0e10cSrcweir                             queryInterface(XIndexAccess.class, oObj);
231cdf0e10cSrcweir 
232cdf0e10cSrcweir         int elementsCount = IA.getCount();
233cdf0e10cSrcweir 
234cdf0e10cSrcweir         oObj.outputList(CA);
235cdf0e10cSrcweir 
236cdf0e10cSrcweir         try {
237cdf0e10cSrcweir             for (int i = 0; i < elementsCount; i++) {
238cdf0e10cSrcweir                 XCell cell = oSheet.getCellByPosition(0, i);
239cdf0e10cSrcweir                 XTextRange textrange = (XTextRange)
240cdf0e10cSrcweir                     UnoRuntime.queryInterface(XTextRange.class, cell);
241cdf0e10cSrcweir                 String str = textrange.getString();
242cdf0e10cSrcweir                 bResult &= oObj.hasByName(str);
243cdf0e10cSrcweir             }
244cdf0e10cSrcweir         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
245cdf0e10cSrcweir             e.printStackTrace(log);
246cdf0e10cSrcweir             bResult = false;
247cdf0e10cSrcweir         }
248cdf0e10cSrcweir 
249cdf0e10cSrcweir         tRes.tested("outputList()", bResult);
250cdf0e10cSrcweir     }
251cdf0e10cSrcweir 
252cdf0e10cSrcweir     /**
253cdf0e10cSrcweir     * Test calls the method for existing range, checks number of ranges in
254cdf0e10cSrcweir     * collection after method call, calls method for non-existent named range.
255cdf0e10cSrcweir     * <p>Has <b> OK </b> status if number of named ranges is less by one than
256cdf0e10cSrcweir     * before method call and exception was thrown during second call. <p>
257cdf0e10cSrcweir     * The following method tests are to be completed successfully before :
258cdf0e10cSrcweir     * <ul>
259cdf0e10cSrcweir     *  <li> <code> addNewByName() </code> : to have name of existent
260cdf0e10cSrcweir     *  named range </li>
261cdf0e10cSrcweir     * </ul>
262cdf0e10cSrcweir     */
_removeByName()263cdf0e10cSrcweir     public void _removeByName() {
264cdf0e10cSrcweir         requiredMethod("addNewByName()");
265cdf0e10cSrcweir         boolean bResult = true;
266cdf0e10cSrcweir         XIndexAccess IA = (XIndexAccess)UnoRuntime.
267cdf0e10cSrcweir                             queryInterface(XIndexAccess.class, oObj);
268cdf0e10cSrcweir 
269cdf0e10cSrcweir         int elementsCount = IA.getCount();
270cdf0e10cSrcweir 
271cdf0e10cSrcweir         // Removing existent element
272cdf0e10cSrcweir         oObj.removeByName(name);
273cdf0e10cSrcweir         bResult = elementsCount == IA.getCount() + 1;
274cdf0e10cSrcweir 
275cdf0e10cSrcweir         try {
276cdf0e10cSrcweir             // Removing unexistent element.
277cdf0e10cSrcweir             oObj.removeByName(name);
278cdf0e10cSrcweir             log.println("Exception expected when removed unexistent element!");
279cdf0e10cSrcweir             bResult = false;
280cdf0e10cSrcweir         } catch (com.sun.star.uno.RuntimeException e) {
281cdf0e10cSrcweir             log.println("Expected exception occured while testing" +
282cdf0e10cSrcweir                       "removeByName() when removed unexistent element.");
283cdf0e10cSrcweir 
284cdf0e10cSrcweir         }
285cdf0e10cSrcweir 
286cdf0e10cSrcweir         tRes.tested("removeByName()", bResult);
287cdf0e10cSrcweir     }
288cdf0e10cSrcweir 
289cdf0e10cSrcweir     /**
290cdf0e10cSrcweir     * Method make string of random content.
291cdf0e10cSrcweir     * @return string of random content
292cdf0e10cSrcweir     */
getRandomContent(String str)293cdf0e10cSrcweir     String getRandomContent(String str) {
294cdf0e10cSrcweir         String gRS = "none";
295cdf0e10cSrcweir         Random rnd = new Random();
296cdf0e10cSrcweir 
297cdf0e10cSrcweir         StringTokenizer ST = new StringTokenizer(str, ";");
298cdf0e10cSrcweir         int nr = rnd.nextInt(ST.countTokens());
299cdf0e10cSrcweir         if (nr < 1)
300cdf0e10cSrcweir             nr++;
301cdf0e10cSrcweir 
302cdf0e10cSrcweir         for (int i=1; i < nr + 1; i++)
303cdf0e10cSrcweir             gRS = ST.nextToken();
304cdf0e10cSrcweir 
305cdf0e10cSrcweir         return gRS;
306cdf0e10cSrcweir     }
307cdf0e10cSrcweir 
308cdf0e10cSrcweir     /**
309cdf0e10cSrcweir      * Returns random value of named range flag.
310cdf0e10cSrcweir      */
getRandomType()311cdf0e10cSrcweir     int getRandomType(){
312cdf0e10cSrcweir         int types[] = { 0,
313cdf0e10cSrcweir                         NamedRangeFlag.COLUMN_HEADER,
314cdf0e10cSrcweir                         NamedRangeFlag.FILTER_CRITERIA,
315cdf0e10cSrcweir                         NamedRangeFlag.PRINT_AREA,
316cdf0e10cSrcweir                         NamedRangeFlag.ROW_HEADER
317cdf0e10cSrcweir                       };
318cdf0e10cSrcweir 
319cdf0e10cSrcweir         Random rnd = new Random();
320cdf0e10cSrcweir         return types[rnd.nextInt(5)];
321cdf0e10cSrcweir     }
322cdf0e10cSrcweir }
323cdf0e10cSrcweir 
324