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.script.framework.provider;
25 
26 import drafts.com.sun.star.script.framework.provider.XFunction;
27 import drafts.com.sun.star.script.framework.provider.XFunctionProvider;
28 
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.uno.XInterface;
32 import com.sun.star.ucb.XSimpleFileAccess;
33 import com.sun.star.uno.Exception;
34 import com.sun.star.beans.XPropertySet;
35 
36 import java.io.PrintWriter;
37 import lib.MultiMethodTest;
38 import lib.StatusException;
39 import lib.Parameters;
40 
41 import java.util.Collection;
42 import java.util.Iterator;
43 
44 public class _XFunction extends MultiMethodTest {
45 
46     public XFunction oObj = null;
47     public XFunctionProvider oProvider = null;
48 
49     /**
50     * Retrieves object relation.
51     */
before()52     public void before() throws StatusException {
53         log.println("getting provider");
54         oProvider = (XFunctionProvider) tEnv.getObjRelation("provider");
55         if (oProvider == null)
56             log.println("it's null");
57         else
58             log.println("it's not null");
59     }
60 
_invoke()61     public void _invoke() {
62         boolean result = true;
63 
64         Collection c =
65             (Collection) tEnv.getObjRelation("_invoke");
66 
67         Iterator tests;
68 
69         if (c != null) {
70             tests = c.iterator();
71 
72             while (tests.hasNext()) {
73                 result &= runInvokeTest((Parameters)tests.next());
74             }
75         }
76         else {
77             result = false;
78         }
79 
80         tRes.tested("invoke()", result);
81     }
82 
runInvokeTest(Parameters testdata)83     private boolean runInvokeTest(Parameters testdata) {
84         String description = testdata.get("description");
85         String logicalname = testdata.get("logicalname");
86 
87         String expreturntype = testdata.get("returntype");
88         String expreturnvalue = testdata.get("returnvalue");
89         String gotreturntype = "null";
90         String gotreturnvalue = "null";
91 
92         String location = testdata.get("location");
93 
94         String expected = testdata.get("expected");
95         String output = "";
96         boolean result = true;
97 
98         log.println(testdata.get("description"));
99 
100         try{
101             Object[] aParams = new Object[0];
102             short[][] aOutParamIndex = new short[1][];
103             aOutParamIndex[0] = new short[0];
104             Object[][] aOutParam = new Object[1][];
105             aOutParam[0] = new Object[0];
106 
107             XFunction func = oProvider.getFunction(logicalname);
108             if (func == null) {
109                 log.println("Couldn't get XFunction for:" + logicalname);
110                 return false;
111             }
112 
113             Object ret = func.invoke( aParams, aOutParamIndex, aOutParam );
114 
115             if (ret != null) {
116                 gotreturntype = ret.getClass().getName();
117                 gotreturnvalue = ret.toString();
118             }
119 
120             output = "success";
121         }
122         catch (com.sun.star.lang.IllegalArgumentException iae) {
123             log.println("Couldn't invoke script:" + iae);
124             output = "com.sun.star.lang.IllegalArgumentException";
125         }
126         catch (com.sun.star.script.CannotConvertException cce) {
127             log.println("Couldn't invoke script:" + cce);
128             output = "com.sun.star.script.CannotConvertException";
129         }
130         catch (com.sun.star.reflection.InvocationTargetException ite) {
131             log.println("Couldn't invoke script:" + ite);
132             output = "com.sun.star.reflection.InvocationTargetException";
133         }
134         catch (com.sun.star.uno.RuntimeException re) {
135             log.println("Couldn't invoke script:" + re);
136             output = "com.sun.star.uno.RuntimeException";
137         }
138         catch(java.lang.Exception e){
139             log.println("Couldn't invoke script:" + e);
140             output = "java.lang.Exception";
141         }
142 
143         if (expreturntype != null) {
144             log.println("expected return type: " + expreturntype +
145                 ", got return type: " + gotreturntype);
146 
147             if (!gotreturntype.equals(expreturntype))
148                 result = false;
149         }
150 
151         if (expreturnvalue != null) {
152             log.println("expected return value: " + expreturnvalue +
153                 ", got return value: " + gotreturnvalue);
154 
155             if (!gotreturnvalue.equals(expreturnvalue))
156                 result = false;
157         }
158 
159         log.println("expected: " + expected + ", output: " + output);
160         if (!output.equals(expected))
161             result = false;
162 
163         return result;
164     }
165 }
166