1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 package ifc.sheet;
29 
30 import lib.MultiMethodTest;
31 import lib.Status;
32 import lib.StatusException;
33 
34 import com.sun.star.beans.XPropertySet;
35 import com.sun.star.container.XIndexAccess;
36 import com.sun.star.container.XNamed;
37 import com.sun.star.sheet.DataPilotFieldOrientation;
38 import com.sun.star.sheet.XDataPilotDescriptor;
39 import com.sun.star.table.CellRangeAddress;
40 import com.sun.star.uno.UnoRuntime;
41 
42 /**
43 * Testing <code>com.sun.star.sheet.XDataPilotDescriptor</code>
44 * interface methods :
45 * <ul>
46 *  <li><code> getTag()</code></li>
47 *  <li><code> setTag()</code></li>
48 *  <li><code> getSourceRange()</code></li>
49 *  <li><code> setSourceRange()</code></li>
50 *  <li><code> getFilterDescriptor()</code></li>
51 *  <li><code> getDataPilotFields()</code></li>
52 *  <li><code> getColumnFields()</code></li>
53 *  <li><code> getRowFields()</code></li>
54 *  <li><code> getPageFields()</code></li>
55 *  <li><code> getDataFields()</code></li>
56 *  <li><code> getHiddenFields()</code></li>
57 * </ul> <p>
58 * This test needs the following object relations :
59 * <ul>
60 *  <li> <code>'FIELDSAMOUNT'</code> (of type <code>Integer</code>):
61 *   to have number of fields </li>
62 * <ul> <p>
63 * @see com.sun.star.sheet.XDataPilotDescriptor
64 */
65 public class _XDataPilotDescriptor extends MultiMethodTest {
66 
67     public XDataPilotDescriptor oObj = null;
68     CellRangeAddress CRA = new CellRangeAddress((short)1, 1, 1, 5, 5);
69     CellRangeAddress oldCRA = null ;
70     String sTag = new String ("XDataPilotDescriptor_Tag");
71     String fieldsNames[];
72     int fieldsAmount = 0;
73     int tEnvFieldsAmount = 0;
74 
75     /**
76     * Retrieves object relations.
77     * @throws StatusException If one of relations not found.
78     */
79     protected void before() {
80         Integer amount = (Integer)tEnv.getObjRelation("FIELDSAMOUNT");
81         if (amount == null) throw new StatusException(Status.failed
82             ("Relation 'FIELDSAMOUNT' not found"));
83         tEnvFieldsAmount = amount.intValue();
84     }
85 
86     /**
87     * Test calls the method and compares returned value with value that was set
88     * in method <code>setSourceRange()</code>. <p>
89     * Has <b> OK </b> status if all fields of cell range addresses are equal. <p>
90     * The following method tests are to be completed successfully before :
91     * <ul>
92     *  <li> <code> setSourceRange() </code> : to have current source range </li>
93     * </ul>
94     */
95     public void _getSourceRange(){
96         requiredMethod("setSourceRange()");
97         boolean bResult = true;
98 
99         CellRangeAddress objRA = oObj.getSourceRange();
100         bResult &= objRA.Sheet == CRA.Sheet;
101         bResult &= objRA.StartRow == CRA.StartRow;
102         bResult &= objRA.StartColumn == CRA.StartColumn;
103         bResult &= objRA.EndRow == CRA.EndRow;
104         bResult &= objRA.EndColumn == CRA.EndColumn;
105 
106         tRes.tested("getSourceRange()", bResult);
107     }
108 
109     /**
110     * Test gets the current source range, stores it and sets new source range.<p>
111     * Has <b> OK </b> status if the method successfully returns. <p>
112     * The following method tests are to be executed before :
113     * <ul>
114     *  <li> <code> getColumnFields() </code> </li>
115     *  <li> <code> getRowFields() </code> </li>
116     *  <li> <code> getDataFields() </code> </li>
117     *  <li> <code> getHiddenFields() </code> </li>
118     *  <li> <code> getPageFields() </code> </li>
119     * </ul>
120     */
121     public void _setSourceRange(){
122         executeMethod("getColumnFields()") ;
123         executeMethod("getRowFields()") ;
124         executeMethod("getDataFields()") ;
125         executeMethod("getHiddenFields()") ;
126         executeMethod("getPageFields()") ;
127 
128         oldCRA = oObj.getSourceRange() ;
129         oObj.setSourceRange(CRA);
130 
131         tRes.tested("setSourceRange()", true);
132     }
133 
134     /**
135     * Test calls the method and checks returned value with value that was set
136     * by method <code>setTag()</code>. <p>
137     * Has <b> OK </b> status if returned value is equal to value that was set
138     * by method <code>setTag()</code>. <p>
139     * The following method tests are to be completed successfully before :
140     * <ul>
141     *  <li> <code> setTag() </code> : to have current tag </li>
142     * </ul>
143     */
144     public void _getTag(){
145         requiredMethod("setTag()");
146         boolean bResult = true;
147 
148         String objTag = oObj.getTag();
149         bResult &= objTag.equals(sTag);
150 
151         tRes.tested("getTag()", bResult);
152     }
153 
154     /**
155     * Test just calls the method. <p>
156     * Has <b> OK </b> status if the method successfully returns. <p>
157     */
158     public void _setTag(){
159         oObj.setTag(sTag);
160         tRes.tested("setTag()", true);
161     }
162 
163     /**
164     * Test calls the method , checks returned value, compares
165     * number of fields goten from returned value and obtained by object
166     * relation <code>'FIELDSAMOUNT'</code> and set property
167     * <code>Orientation</code> to one of DataPilotFieldOrientation values. <p>
168     * Has <b> OK </b> status if returned value isn't null, number of fields
169     * goten from returned value is less than number of fields obtained by relation
170     * and no exceptions were thrown. <p>
171     */
172     public void _getDataPilotFields(){
173         boolean bResult = true;
174         XIndexAccess IA = null;
175 
176         IA = oObj.getDataPilotFields();
177         if (IA == null) {
178             log.println("Returned value is null.");
179             tRes.tested("getDataPilotFields()", false);
180             return;
181         } else {log.println("getDataPilotFields returned not Null value -- OK");}
182 
183         fieldsAmount = IA.getCount();
184         if (fieldsAmount < tEnvFieldsAmount) {
185             log.println("Number of fields is less than number goten by relation.");
186             tRes.tested("getDataPilotFields()", false);
187             return;
188         } else {log.println("count of returned fields -- OK");}
189 
190         fieldsNames = new String[tEnvFieldsAmount];
191         int i = -1 ;
192         int cnt = 0 ;
193         while (++i < fieldsAmount) {
194             Object field;
195             try {
196                 field = IA.getByIndex(i);
197             } catch(com.sun.star.lang.WrappedTargetException e) {
198                 e.printStackTrace(log);
199                 tRes.tested("getDataPilotFields()", false);
200                 return;
201             } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
202                 e.printStackTrace(log);
203                 tRes.tested("getDataPilotFields()", false);
204                 return;
205             }
206 
207             XNamed named = (XNamed)
208                 UnoRuntime.queryInterface(XNamed.class, field);
209             String name = named.getName();
210 
211             log.print("Field : '" + name + "' ... ") ;
212 
213             if (!name.equals("Data")) {
214 
215                 fieldsNames[cnt] = name ;
216 
217                 XPropertySet props = (XPropertySet)
218                     UnoRuntime.queryInterface(XPropertySet.class, field);
219 
220                 try {
221                   switch (cnt % 5) {
222                     case 0 :
223                         props.setPropertyValue("Orientation",
224                             DataPilotFieldOrientation.COLUMN);
225                         log.println("  Column") ;
226                         break;
227                     case 1 :
228                         props.setPropertyValue("Orientation",
229                             DataPilotFieldOrientation.ROW);
230                         log.println("  Row") ;
231                         break;
232                     case 2 :
233                         props.setPropertyValue("Orientation",
234                             DataPilotFieldOrientation.DATA);
235                         log.println("  Data") ;
236                         break;
237                     case 3 :
238                         props.setPropertyValue("Orientation",
239                             DataPilotFieldOrientation.HIDDEN);
240                         log.println("  Hidden") ;
241                         break;
242                     case 4 :
243                         props.setPropertyValue("Orientation",
244                             DataPilotFieldOrientation.PAGE);
245                         log.println("  Page") ;
246                         break;
247                 } } catch (com.sun.star.lang.WrappedTargetException e) {
248                     e.printStackTrace(log);
249                     tRes.tested("getDataPilotFields()", false);
250                     return;
251                 } catch (com.sun.star.lang.IllegalArgumentException e) {
252                     e.printStackTrace(log);
253                     tRes.tested("getDataPilotFields()", false);
254                     return;
255                 } catch (com.sun.star.beans.PropertyVetoException e) {
256                     e.printStackTrace(log);
257                     tRes.tested("getDataPilotFields()", false);
258                     return;
259                 } catch (com.sun.star.beans.UnknownPropertyException e) {
260                     e.printStackTrace(log);
261                     tRes.tested("getDataPilotFields()", false);
262                     return;
263                 }
264                 if (++cnt > 4) break;
265             } else {
266                 tRes.tested("getDataPilotFields()", false);
267                 return;
268             }
269         }
270 
271         tRes.tested("getDataPilotFields()", bResult);
272     }
273 
274     /**
275     * Test calls the method and checks returned collection using the method
276     * <code>CheckNames()</code>. <p>
277     * Has <b> OK </b> status if the method <code>CheckNames()</code> returns true
278     * and no exceptions were thrown. <p>
279     * The following method tests are to be completed successfully before :
280     * <ul>
281     *  <li> <code> getDataPilotFields() </code> : to have array of field names </li>
282     * </ul>
283     */
284     public void _getColumnFields(){
285         requiredMethod("getDataPilotFields()");
286         XIndexAccess IA = oObj.getColumnFields();
287         boolean bResult = CheckNames(IA, 0);
288         tRes.tested("getColumnFields()", bResult);
289     }
290 
291     /**
292     * Test calls the method and checks returned collection using the method
293     * <code>CheckNames()</code>. <p>
294     * Has <b> OK </b> status if the method <code>CheckNames()</code> returned true
295     * and no exceptions were thrown. <p>
296     * The following method tests are to be completed successfully before :
297     * <ul>
298     *  <li> <code> getDataPilotFields() </code> : to have array of field names </li>
299     * </ul>
300     */
301     public void _getDataFields(){
302         requiredMethod("getDataPilotFields()");
303         XIndexAccess IA = oObj.getDataFields();
304         boolean bResult = CheckNames(IA, 2);
305         tRes.tested("getDataFields()", bResult);
306     }
307 
308     /**
309     * Test calls the method and checks returned collection using the method
310     * <code>CheckNames()</code>. <p>
311     * Has <b> OK </b> status if the method <code>CheckNames()</code> returned true
312     * and no exceptions were thrown. <p>
313     * The following method tests are to be completed successfully before :
314     * <ul>
315     *  <li> <code> getDataPilotFields() </code> : to have array of field names </li>
316     * </ul>
317     */
318     public void _getHiddenFields(){
319         requiredMethod("getDataPilotFields()");
320         XIndexAccess IA = oObj.getHiddenFields();
321         boolean bResult = CheckNames(IA, 3);
322         tRes.tested("getHiddenFields()", bResult);
323     }
324 
325     /**
326     * Test calls the method and checks returned collection using the method
327     * <code>CheckNames()</code>. <p>
328     * Has <b> OK </b> status if the method <code>CheckNames()</code> returned true
329     * and no exceptions were thrown. <p>
330     * The following method tests are to be completed successfully before :
331     * <ul>
332     *  <li> <code> getDataPilotFields() </code> : to have array of field names </li>
333     * </ul>
334     */
335     public void _getRowFields(){
336         requiredMethod("getDataPilotFields()");
337         XIndexAccess IA = oObj.getRowFields();
338         boolean bResult = CheckNames(IA, 1);
339         tRes.tested("getRowFields()", bResult);
340     }
341 
342     /**
343     * setting of PageFields isn't supported by StarOffice Calc
344     * Has <b> OK </b> status if the returned IndexAccess
345     * isn't NULL. <p>
346     * The following method tests are to be completed successfully before :
347     * <ul>
348     *  <li> <code> getDataPilotFields() </code> : to have array of field names </li>
349     * </ul>
350     */
351     public void _getPageFields(){
352         requiredMethod("getDataPilotFields()");
353         XIndexAccess IA = oObj.getPageFields();
354         boolean bResult = (IA != null);
355         //        boolean bResult = CheckNames(IA, 4);
356         tRes.tested("getPageFields()", bResult);
357     }
358 
359     /**
360     * Test calls the method and checks returned value. <p>
361     * Has <b> OK </b> status if returned value isn't null
362     * and no exceptions were thrown. <p>
363     */
364     public void _getFilterDescriptor(){
365         boolean bResult = oObj.getFilterDescriptor() != null;
366         tRes.tested("getFilterDescriptor()", bResult);
367     }
368 
369     /**
370     * Method checks that the field with index <code>rem</code> exists
371     * in the array <code>IA</code>.
372     * @param IA collection of elements that support interface <code>XNamed</code>
373     * @param rem index of field in the array of field names that was stored in
374     * the method <code>getDataPilotFields()</code>
375     * @return true if required field name exists in passed collection;
376     * false otherwise
377     * @see com.sun.star.container.XNamed
378     */
379     boolean CheckNames(XIndexAccess IA, int rem) {
380         String name = null;
381 
382         if (IA == null) {
383             log.println("Null retruned.") ;
384             return false ;
385         }
386 
387         if (fieldsNames[rem] == null) {
388             log.println("No fields were set to this orientation - cann't check result") ;
389             return true ;
390         }
391 
392         if (IA.getCount() == 0) {
393             log.println("No fields found. Must be at least '"
394                 + fieldsNames[rem] + "'") ;
395             return false ;
396         }
397 
398         try {
399             log.print("Fields returned ") ;
400             for (int i = 0; i < IA.getCount(); i++) {
401                 Object field = IA.getByIndex(i);
402                 XNamed named = (XNamed)UnoRuntime.queryInterface
403                     (XNamed.class, field);
404                 name = named.getName();
405                 log.print(" " + name) ;
406                 if (fieldsNames[rem].equals(name)) {
407                     log.println(" - OK") ;
408                     return true ;
409                 }
410             }
411         } catch (com.sun.star.lang.WrappedTargetException e) {
412             e.printStackTrace(log) ;
413             return false ;
414         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
415             e.printStackTrace(log) ;
416             return false ;
417         }
418         log.println(" - FAILED (field " + fieldsNames[rem] + " was not found.") ;
419         return false ;
420     }
421 
422     /**
423      * Recreates object(to back old orientations of the fields).
424      */
425     protected void after() {
426         disposeEnvironment();
427     }
428 }
429 
430