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 java.util.Random;
27 
28 import lib.MultiMethodTest;
29 
30 import com.sun.star.beans.PropertyValue;
31 import com.sun.star.sheet.XFunctionDescriptions;
32 
33 /**
34 * Testing <code>com.sun.star.sheet.XFunctionDescriptions</code>
35 * interface methods :
36 * <ul>
37 *  <li><code> getById()</code></li>
38 * </ul> <p>
39 * @see com.sun.star.sheet.XFunctionDescriptions
40 */
41 public class _XFunctionDescriptions extends MultiMethodTest {
42 
43     public XFunctionDescriptions oObj = null;
44 
45     /**
46     * Test finds available id, calls method using this id, checks returned
47     * value and then tries to get description with wrong id. <p>
48     * Has <b>OK</b> status if returned value is equal to value obtained by the
49     * method <code>getByIndex()</code> in first call and exception
50     * <code>IllegalArgumentException</code> was thrown in second call.<p>
51     * @see com.sun.star.lang.IllegalArgumentException
52     */
_getById()53     public void _getById() {
54         boolean bResult = true;
55         // Finding available id...
56 
57         int count = oObj.getCount();
58         if (count > 0) {
59             Random rnd = new Random();
60             int nr = rnd.nextInt(count);
61 
62             PropertyValue[] PVals = null;
63             try {
64                 PVals = (PropertyValue[])oObj.getByIndex(nr);
65             } catch(com.sun.star.lang.WrappedTargetException e) {
66                 e.printStackTrace(log);
67                 tRes.tested("getById()", false);
68                 return;
69             } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
70                 e.printStackTrace(log);
71                 tRes.tested("getById()", false);
72                 return;
73             }
74 
75             String FName = null;
76             Integer FId = null;
77 
78             for (int i = 0; i < PVals.length; i++) {
79                 if (PVals[i].Name.equals("Name"))
80                     FName = (String)PVals[i].Value;
81                 if (PVals[i].Name.equals("Id"))
82                     FId = (Integer)PVals[i].Value;
83             }
84 
85             log.println("The id of function '" + FName + "' is " + FId);
86 
87             PropertyValue[] PVals2 = null;
88             try {
89                 PVals2 = oObj.getById(FId.intValue());
90             } catch(com.sun.star.lang.IllegalArgumentException e) {
91                 e.printStackTrace(log);
92                 tRes.tested("getById()", false);
93                 return;
94             }
95 
96             String objFName = null;
97             Integer objFId = null;
98             for (int i = 0; i < PVals2.length; i++) {
99                 if (PVals2[i].Name.equals("Name"))
100                     objFName = (String)PVals[i].Value;
101                 if (PVals2[i].Name.equals("Id"))
102                     objFId = (Integer)PVals[i].Value;
103             }
104 
105             log.println("The id of returned function '" +
106                 objFName + "' is " + objFId);
107 
108             bResult &= FName.equals(objFName);
109             bResult &= FId.equals(objFId);
110         }
111 
112         log.println("OK.");
113 
114         try {
115             log.println("Now trying to get description with wrong id ... ");
116             oObj.getById(-1);
117             bResult = false;
118             log.println("Exception expected! - FAILED");
119         } catch (com.sun.star.lang.IllegalArgumentException e) {
120             log.println("Expected exception " + e + " - OK!");
121         }
122 
123         tRes.tested("getById()", bResult);
124     }
125 }  // finish class _XFunctionDescriptions
126 
127 
128