1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package ifc.script.framework.runtime;
29 
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.Collection;
33 
34 import drafts.com.sun.star.script.framework.runtime.XScriptInvocation;
35 import drafts.com.sun.star.script.framework.storage.XScriptStorageManager;
36 
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.ucb.XSimpleFileAccess;
39 import com.sun.star.beans.XPropertySet;
40 import com.sun.star.uno.XComponentContext;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XInterface;
43 import com.sun.star.frame.XModel;
44 
45 import lib.MultiMethodTest;
46 import lib.StatusException;
47 import lib.Parameters;
48 import util.SOfficeFactory;
49 
50 public class _XScriptInvocation extends MultiMethodTest {
51 
52     public XScriptInvocation oObj = null;
53     private XScriptStorageManager storageManager = null;
54 
55     /**
56     * Retrieves object relation.
57     */
58     public void before() throws StatusException {
59     }
60 
61     public void after() throws StatusException {
62     }
63 
64     public void _invoke() {
65         boolean result = true;
66 
67         Collection c =
68             (Collection) tEnv.getObjRelation("_invoke");
69 
70         Iterator tests;
71 
72         if (c != null) {
73             tests = c.iterator();
74 
75             while (tests.hasNext()) {
76                 result &= runInvokeTest((Parameters)tests.next());
77             }
78         }
79         else {
80             result = false;
81         }
82 
83         tRes.tested("invoke()", result);
84     }
85 
86     private boolean runInvokeTest(Parameters testdata) {
87         String description = testdata.get("description");
88         String logicalname = testdata.get("logicalname");
89         String context = testdata.get("context");
90         String location = testdata.get("location");
91         String expected = testdata.get("expected");
92         String output = "";
93 
94         int storageId = getStorageId(location);
95 
96         XModel ctx = null;
97         if (!context.equals("null"))
98             ctx = loadDocument(context);
99 
100         HashMap map = new HashMap();
101         map.put("SCRIPTING_DOC_STORAGE_ID", new Integer(storageId));
102         map.put("SCRIPTING_DOC_URI", "hahaha");
103         if (ctx != null)
104             map.put("SCRIPTING_DOC_REF", ctx);
105 
106         Parameters params = new Parameters(map);
107         Object[] args = new Object[0];
108 
109         Object[][] result = new Object[1][0];
110         result[0] = new Object[0];
111 
112         short[][] num = new short[1][0];
113         num[0] = new short[0];
114 
115         log.println(description + ": " + logicalname);
116 
117         try {
118             Object ret = oObj.invoke(logicalname, params, args, num, result);
119             log.println("return type is: " + ret.getClass().getName() +
120                 ", value is: " + ret.toString());
121             output = "success";
122         }
123         catch (com.sun.star.lang.IllegalArgumentException iae) {
124             log.println("Couldn't invoke script:" + iae);
125             output = "com.sun.star.lang.IllegalArgumentException";
126         }
127         catch (com.sun.star.script.CannotConvertException cce) {
128             log.println("Couldn't invoke script:" + cce);
129             output = "com.sun.star.script.CannotConvertException";
130         }
131         catch (com.sun.star.reflection.InvocationTargetException ite) {
132             log.println("Couldn't invoke script:" + ite);
133             output = "com.sun.star.reflection.InvocationTargetException";
134         }
135         catch (com.sun.star.uno.RuntimeException re) {
136             log.println("Couldn't invoke script:" + re);
137             output = "com.sun.star.uno.RuntimeException";
138         }
139 
140         if (ctx != null)
141             ctx.dispose();
142 
143         log.println("expected: " + expected + ", output: " + output);
144         if (output.equals(expected))
145             return true;
146         else
147             return false;
148     }
149 
150     private int getStorageId(String location) {
151 
152         if (location.equals("share"))
153             return 0;
154 
155         if (location.equals("user"))
156             return 1;
157 
158         XSimpleFileAccess access = null;
159         String uri = util.utils.getFullTestURL(location);
160 
161         if (storageManager == null) {
162             try {
163                 XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(
164                     XPropertySet.class, tParam.getMSF());
165 
166                 XComponentContext xContext = (XComponentContext)
167                     UnoRuntime.queryInterface(XComponentContext.class,
168                     xProp.getPropertyValue("DefaultContext"));
169 
170                 XInterface ifc = (XInterface)
171                     xContext.getValueByName("/singletons/drafts.com.sun.star." +
172                     "script.framework.storage.theScriptStorageManager");
173 
174                 storageManager = (XScriptStorageManager)
175                     UnoRuntime.queryInterface(XScriptStorageManager.class, ifc);
176             }
177             catch( Exception e ) {
178                 return -1;
179             }
180         }
181 
182         access = getXSimpleFileAccess();
183         if (access == null)
184             return -1;
185 
186         int id = storageManager.createScriptStorageWithURI(access, uri);
187 
188         return id;
189     }
190 
191     private XSimpleFileAccess getXSimpleFileAccess() {
192         XSimpleFileAccess access = null;
193 
194         try {
195             Object fa = tParam.getMSF().createInstance(
196                 "com.sun.star.ucb.SimpleFileAccess");
197 
198             access = (XSimpleFileAccess)
199                 UnoRuntime.queryInterface(XSimpleFileAccess.class, fa);
200         }
201         catch (com.sun.star.uno.Exception e) {
202             return null;
203         }
204         return access;
205     }
206 
207     private XModel loadDocument(String name) {
208         XModel model = null;
209         SOfficeFactory factory = SOfficeFactory.getFactory(tParam.getMSF());
210 
211         String fullname = util.utils.getFullTestURL(name);
212 
213         try {
214             Object obj = factory.loadDocument(fullname);
215             model = (XModel) UnoRuntime.queryInterface(XModel.class, obj);
216         }
217         catch (com.sun.star.lang.IllegalArgumentException iae) {
218             return null;
219         }
220         catch (Exception e) {
221             return null;
222         }
223 
224         try {
225             Thread.sleep(5000);
226         }
227         catch (InterruptedException ie) {
228         }
229 
230         return model;
231     }
232 }
233