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.runtime;
25 
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.Collection;
29 
30 import drafts.com.sun.star.script.framework.runtime.XScriptInvocation;
31 import drafts.com.sun.star.script.framework.storage.XScriptStorageManager;
32 
33 import com.sun.star.lang.XMultiServiceFactory;
34 import com.sun.star.ucb.XSimpleFileAccess;
35 import com.sun.star.beans.XPropertySet;
36 import com.sun.star.uno.XComponentContext;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XInterface;
39 import com.sun.star.frame.XModel;
40 
41 import lib.MultiMethodTest;
42 import lib.StatusException;
43 import lib.Parameters;
44 import util.SOfficeFactory;
45 
46 public class _XScriptInvocation extends MultiMethodTest {
47 
48     public XScriptInvocation oObj = null;
49     private XScriptStorageManager storageManager = null;
50 
51     /**
52     * Retrieves object relation.
53     */
before()54     public void before() throws StatusException {
55     }
56 
after()57     public void after() throws StatusException {
58     }
59 
_invoke()60     public void _invoke() {
61         boolean result = true;
62 
63         Collection c =
64             (Collection) tEnv.getObjRelation("_invoke");
65 
66         Iterator tests;
67 
68         if (c != null) {
69             tests = c.iterator();
70 
71             while (tests.hasNext()) {
72                 result &= runInvokeTest((Parameters)tests.next());
73             }
74         }
75         else {
76             result = false;
77         }
78 
79         tRes.tested("invoke()", result);
80     }
81 
runInvokeTest(Parameters testdata)82     private boolean runInvokeTest(Parameters testdata) {
83         String description = testdata.get("description");
84         String logicalname = testdata.get("logicalname");
85         String context = testdata.get("context");
86         String location = testdata.get("location");
87         String expected = testdata.get("expected");
88         String output = "";
89 
90         int storageId = getStorageId(location);
91 
92         XModel ctx = null;
93         if (!context.equals("null"))
94             ctx = loadDocument(context);
95 
96         HashMap map = new HashMap();
97         map.put("SCRIPTING_DOC_STORAGE_ID", new Integer(storageId));
98         map.put("SCRIPTING_DOC_URI", "hahaha");
99         if (ctx != null)
100             map.put("SCRIPTING_DOC_REF", ctx);
101 
102         Parameters params = new Parameters(map);
103         Object[] args = new Object[0];
104 
105         Object[][] result = new Object[1][0];
106         result[0] = new Object[0];
107 
108         short[][] num = new short[1][0];
109         num[0] = new short[0];
110 
111         log.println(description + ": " + logicalname);
112 
113         try {
114             Object ret = oObj.invoke(logicalname, params, args, num, result);
115             log.println("return type is: " + ret.getClass().getName() +
116                 ", value is: " + ret.toString());
117             output = "success";
118         }
119         catch (com.sun.star.lang.IllegalArgumentException iae) {
120             log.println("Couldn't invoke script:" + iae);
121             output = "com.sun.star.lang.IllegalArgumentException";
122         }
123         catch (com.sun.star.script.CannotConvertException cce) {
124             log.println("Couldn't invoke script:" + cce);
125             output = "com.sun.star.script.CannotConvertException";
126         }
127         catch (com.sun.star.reflection.InvocationTargetException ite) {
128             log.println("Couldn't invoke script:" + ite);
129             output = "com.sun.star.reflection.InvocationTargetException";
130         }
131         catch (com.sun.star.uno.RuntimeException re) {
132             log.println("Couldn't invoke script:" + re);
133             output = "com.sun.star.uno.RuntimeException";
134         }
135 
136         if (ctx != null)
137             ctx.dispose();
138 
139         log.println("expected: " + expected + ", output: " + output);
140         if (output.equals(expected))
141             return true;
142         else
143             return false;
144     }
145 
getStorageId(String location)146     private int getStorageId(String location) {
147 
148         if (location.equals("share"))
149             return 0;
150 
151         if (location.equals("user"))
152             return 1;
153 
154         XSimpleFileAccess access = null;
155         String uri = util.utils.getFullTestURL(location);
156 
157         if (storageManager == null) {
158             try {
159                 XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(
160                     XPropertySet.class, tParam.getMSF());
161 
162                 XComponentContext xContext = (XComponentContext)
163                     UnoRuntime.queryInterface(XComponentContext.class,
164                     xProp.getPropertyValue("DefaultContext"));
165 
166                 XInterface ifc = (XInterface)
167                     xContext.getValueByName("/singletons/drafts.com.sun.star." +
168                     "script.framework.storage.theScriptStorageManager");
169 
170                 storageManager = (XScriptStorageManager)
171                     UnoRuntime.queryInterface(XScriptStorageManager.class, ifc);
172             }
173             catch( Exception e ) {
174                 return -1;
175             }
176         }
177 
178         access = getXSimpleFileAccess();
179         if (access == null)
180             return -1;
181 
182         int id = storageManager.createScriptStorageWithURI(access, uri);
183 
184         return id;
185     }
186 
getXSimpleFileAccess()187     private XSimpleFileAccess getXSimpleFileAccess() {
188         XSimpleFileAccess access = null;
189 
190         try {
191             Object fa = tParam.getMSF().createInstance(
192                 "com.sun.star.ucb.SimpleFileAccess");
193 
194             access = (XSimpleFileAccess)
195                 UnoRuntime.queryInterface(XSimpleFileAccess.class, fa);
196         }
197         catch (com.sun.star.uno.Exception e) {
198             return null;
199         }
200         return access;
201     }
202 
loadDocument(String name)203     private XModel loadDocument(String name) {
204         XModel model = null;
205         SOfficeFactory factory = SOfficeFactory.getFactory(tParam.getMSF());
206 
207         String fullname = util.utils.getFullTestURL(name);
208 
209         try {
210             Object obj = factory.loadDocument(fullname);
211             model = (XModel) UnoRuntime.queryInterface(XModel.class, obj);
212         }
213         catch (com.sun.star.lang.IllegalArgumentException iae) {
214             return null;
215         }
216         catch (Exception e) {
217             return null;
218         }
219 
220         try {
221             Thread.sleep(5000);
222         }
223         catch (InterruptedException ie) {
224         }
225 
226         return model;
227     }
228 }
229