1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package ifc.sheet;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.util.Random;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import lib.MultiMethodTest;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
31cdf0e10cSrcweir import com.sun.star.sheet.XFunctionDescriptions;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir /**
34cdf0e10cSrcweir * Testing <code>com.sun.star.sheet.XFunctionDescriptions</code>
35cdf0e10cSrcweir * interface methods :
36cdf0e10cSrcweir * <ul>
37cdf0e10cSrcweir *  <li><code> getById()</code></li>
38cdf0e10cSrcweir * </ul> <p>
39cdf0e10cSrcweir * @see com.sun.star.sheet.XFunctionDescriptions
40cdf0e10cSrcweir */
41cdf0e10cSrcweir public class _XFunctionDescriptions extends MultiMethodTest {
42cdf0e10cSrcweir 
43cdf0e10cSrcweir     public XFunctionDescriptions oObj = null;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     /**
46cdf0e10cSrcweir     * Test finds available id, calls method using this id, checks returned
47cdf0e10cSrcweir     * value and then tries to get description with wrong id. <p>
48cdf0e10cSrcweir     * Has <b>OK</b> status if returned value is equal to value obtained by the
49cdf0e10cSrcweir     * method <code>getByIndex()</code> in first call and exception
50cdf0e10cSrcweir     * <code>IllegalArgumentException</code> was thrown in second call.<p>
51cdf0e10cSrcweir     * @see com.sun.star.lang.IllegalArgumentException
52cdf0e10cSrcweir     */
_getById()53cdf0e10cSrcweir     public void _getById() {
54cdf0e10cSrcweir         boolean bResult = true;
55cdf0e10cSrcweir         // Finding available id...
56cdf0e10cSrcweir 
57cdf0e10cSrcweir         int count = oObj.getCount();
58cdf0e10cSrcweir         if (count > 0) {
59cdf0e10cSrcweir             Random rnd = new Random();
60cdf0e10cSrcweir             int nr = rnd.nextInt(count);
61cdf0e10cSrcweir 
62cdf0e10cSrcweir             PropertyValue[] PVals = null;
63cdf0e10cSrcweir             try {
64cdf0e10cSrcweir                 PVals = (PropertyValue[])oObj.getByIndex(nr);
65cdf0e10cSrcweir             } catch(com.sun.star.lang.WrappedTargetException e) {
66cdf0e10cSrcweir                 e.printStackTrace(log);
67cdf0e10cSrcweir                 tRes.tested("getById()", false);
68cdf0e10cSrcweir                 return;
69cdf0e10cSrcweir             } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
70cdf0e10cSrcweir                 e.printStackTrace(log);
71cdf0e10cSrcweir                 tRes.tested("getById()", false);
72cdf0e10cSrcweir                 return;
73cdf0e10cSrcweir             }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir             String FName = null;
76cdf0e10cSrcweir             Integer FId = null;
77cdf0e10cSrcweir 
78cdf0e10cSrcweir             for (int i = 0; i < PVals.length; i++) {
79cdf0e10cSrcweir                 if (PVals[i].Name.equals("Name"))
80cdf0e10cSrcweir                     FName = (String)PVals[i].Value;
81cdf0e10cSrcweir                 if (PVals[i].Name.equals("Id"))
82cdf0e10cSrcweir                     FId = (Integer)PVals[i].Value;
83cdf0e10cSrcweir             }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir             log.println("The id of function '" + FName + "' is " + FId);
86cdf0e10cSrcweir 
87cdf0e10cSrcweir             PropertyValue[] PVals2 = null;
88cdf0e10cSrcweir             try {
89cdf0e10cSrcweir                 PVals2 = oObj.getById(FId.intValue());
90cdf0e10cSrcweir             } catch(com.sun.star.lang.IllegalArgumentException e) {
91cdf0e10cSrcweir                 e.printStackTrace(log);
92cdf0e10cSrcweir                 tRes.tested("getById()", false);
93cdf0e10cSrcweir                 return;
94cdf0e10cSrcweir             }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir             String objFName = null;
97cdf0e10cSrcweir             Integer objFId = null;
98cdf0e10cSrcweir             for (int i = 0; i < PVals2.length; i++) {
99cdf0e10cSrcweir                 if (PVals2[i].Name.equals("Name"))
100cdf0e10cSrcweir                     objFName = (String)PVals[i].Value;
101cdf0e10cSrcweir                 if (PVals2[i].Name.equals("Id"))
102cdf0e10cSrcweir                     objFId = (Integer)PVals[i].Value;
103cdf0e10cSrcweir             }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir             log.println("The id of returned function '" +
106cdf0e10cSrcweir                 objFName + "' is " + objFId);
107cdf0e10cSrcweir 
108cdf0e10cSrcweir             bResult &= FName.equals(objFName);
109cdf0e10cSrcweir             bResult &= FId.equals(objFId);
110cdf0e10cSrcweir         }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir         log.println("OK.");
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         try {
115cdf0e10cSrcweir             log.println("Now trying to get description with wrong id ... ");
116cdf0e10cSrcweir             oObj.getById(-1);
117cdf0e10cSrcweir             bResult = false;
118cdf0e10cSrcweir             log.println("Exception expected! - FAILED");
119cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
120cdf0e10cSrcweir             log.println("Expected exception " + e + " - OK!");
121cdf0e10cSrcweir         }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir         tRes.tested("getById()", bResult);
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir }  // finish class _XFunctionDescriptions
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 
128