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