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