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