1*cd519653SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*cd519653SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cd519653SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cd519653SAndrew Rist  * distributed with this work for additional information
6*cd519653SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cd519653SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cd519653SAndrew Rist  * "License"); you may not use this file except in compliance
9*cd519653SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cd519653SAndrew Rist  *
11*cd519653SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cd519653SAndrew Rist  *
13*cd519653SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cd519653SAndrew Rist  * software distributed under the License is distributed on an
15*cd519653SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cd519653SAndrew Rist  * KIND, either express or implied.  See the License for the
17*cd519653SAndrew Rist  * specific language governing permissions and limitations
18*cd519653SAndrew Rist  * under the License.
19*cd519653SAndrew Rist  *
20*cd519653SAndrew Rist  *************************************************************/
21*cd519653SAndrew Rist 
22*cd519653SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package ifc.script.framework.runtime;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.util.HashMap;
27cdf0e10cSrcweir import java.util.Iterator;
28cdf0e10cSrcweir import java.util.Collection;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import drafts.com.sun.star.script.framework.runtime.XScriptNameResolver;
31cdf0e10cSrcweir import drafts.com.sun.star.script.framework.storage.XScriptInfo;
32cdf0e10cSrcweir import drafts.com.sun.star.script.framework.storage.XScriptStorageManager;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
35cdf0e10cSrcweir import com.sun.star.ucb.XSimpleFileAccess;
36cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
37cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
38cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
39cdf0e10cSrcweir import com.sun.star.uno.XInterface;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir import lib.MultiMethodTest;
42cdf0e10cSrcweir import lib.StatusException;
43cdf0e10cSrcweir import lib.Parameters;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir public class _XScriptNameResolver extends MultiMethodTest {
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     public XScriptNameResolver oObj = null;
48cdf0e10cSrcweir     private XScriptStorageManager storageManager = null;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir     /**
51cdf0e10cSrcweir     * Retrieves object relation.
52cdf0e10cSrcweir     */
before()53cdf0e10cSrcweir     public void before() throws StatusException {
54cdf0e10cSrcweir     }
55cdf0e10cSrcweir 
_resolve()56cdf0e10cSrcweir     public void _resolve() {
57cdf0e10cSrcweir         boolean result = true;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir         Collection c =
60cdf0e10cSrcweir             (Collection) tEnv.getObjRelation("_resolve");
61cdf0e10cSrcweir 
62cdf0e10cSrcweir         Iterator tests;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir         if (c != null) {
65cdf0e10cSrcweir             tests = c.iterator();
66cdf0e10cSrcweir 
67cdf0e10cSrcweir             while (tests.hasNext()) {
68cdf0e10cSrcweir                 result &= runResolveTest((Parameters)tests.next());
69cdf0e10cSrcweir             }
70cdf0e10cSrcweir         }
71cdf0e10cSrcweir         else {
72cdf0e10cSrcweir             result = false;
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir         tRes.tested("resolve()", result);
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
runResolveTest(Parameters data)78cdf0e10cSrcweir     private boolean runResolveTest(Parameters data) {
79cdf0e10cSrcweir         String description = data.get("description");
80cdf0e10cSrcweir         String location = data.get("location");
81cdf0e10cSrcweir         String logicalname = data.get("logicalname");
82cdf0e10cSrcweir         String expected = data.get("expected");
83cdf0e10cSrcweir         String output = "";
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         int storageId = getStorageId(location);
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         log.println(description + ": " + logicalname);
88cdf0e10cSrcweir 
89cdf0e10cSrcweir         HashMap map = new HashMap();
90cdf0e10cSrcweir         map.put("SCRIPTING_DOC_STORAGE_ID", new Integer(storageId));
91cdf0e10cSrcweir         map.put("SCRIPTING_DOC_URI", util.utils.getFullTestURL(location));
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         Parameters params = new Parameters(map);
94cdf0e10cSrcweir         Object[] args = new Object[] {params};
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         try {
97cdf0e10cSrcweir             XInterface ifc = (XInterface) oObj.resolve(logicalname, args);
98cdf0e10cSrcweir 
99cdf0e10cSrcweir             if (ifc == null)
100cdf0e10cSrcweir                 output = "null";
101cdf0e10cSrcweir             else if (UnoRuntime.queryInterface(XScriptInfo.class, ifc) == null)
102cdf0e10cSrcweir                 output = "null";
103cdf0e10cSrcweir             else
104cdf0e10cSrcweir                 output = "XScriptInfo.class";
105cdf0e10cSrcweir         }
106cdf0e10cSrcweir         catch (com.sun.star.lang.IllegalArgumentException iae) {
107cdf0e10cSrcweir             log.println("caught IllegalArgumentException: " + iae);
108cdf0e10cSrcweir             output = "com.sun.star.lang.IllegalArgumentException";
109cdf0e10cSrcweir         }
110cdf0e10cSrcweir         catch (com.sun.star.script.CannotConvertException cce) {
111cdf0e10cSrcweir             log.println("caught CannotConvertException: " + cce);
112cdf0e10cSrcweir             output = "com.sun.star.script.CannotConvertException";
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir         catch (com.sun.star.uno.RuntimeException re) {
115cdf0e10cSrcweir             log.println("caught RuntimeException: " + re);
116cdf0e10cSrcweir             output = "com.sun.star.uno.RuntimeException";
117cdf0e10cSrcweir         }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir         log.println("expected: " + expected + ", output: " + output);
120cdf0e10cSrcweir         if (output.equals(expected))
121cdf0e10cSrcweir             return true;
122cdf0e10cSrcweir         else
123cdf0e10cSrcweir             return false;
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
getStorageId(String location)126cdf0e10cSrcweir     private int getStorageId(String location) {
127cdf0e10cSrcweir 
128cdf0e10cSrcweir         if (location.equals("share"))
129cdf0e10cSrcweir             return 0;
130cdf0e10cSrcweir 
131cdf0e10cSrcweir         if (location.equals("user"))
132cdf0e10cSrcweir             return 1;
133cdf0e10cSrcweir 
134cdf0e10cSrcweir         XSimpleFileAccess access = null;
135cdf0e10cSrcweir         String uri = util.utils.getFullTestURL(location);
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         if (storageManager == null) {
138cdf0e10cSrcweir             try {
139cdf0e10cSrcweir                 XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(
140cdf0e10cSrcweir                     XPropertySet.class, tParam.getMSF());
141cdf0e10cSrcweir 
142cdf0e10cSrcweir                 XComponentContext xContext = (XComponentContext)
143cdf0e10cSrcweir                     UnoRuntime.queryInterface(XComponentContext.class,
144cdf0e10cSrcweir                     xProp.getPropertyValue("DefaultContext"));
145cdf0e10cSrcweir 
146cdf0e10cSrcweir                 XInterface ifc = (XInterface)
147cdf0e10cSrcweir                     xContext.getValueByName("/singletons/drafts.com.sun.star." +
148cdf0e10cSrcweir                     "script.framework.storage.theScriptStorageManager");
149cdf0e10cSrcweir 
150cdf0e10cSrcweir                 storageManager = (XScriptStorageManager)
151cdf0e10cSrcweir                     UnoRuntime.queryInterface(XScriptStorageManager.class, ifc);
152cdf0e10cSrcweir             }
153cdf0e10cSrcweir             catch( Exception e ) {
154cdf0e10cSrcweir                 return -1;
155cdf0e10cSrcweir             }
156cdf0e10cSrcweir         }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir         access = getXSimpleFileAccess();
159cdf0e10cSrcweir         if (access == null)
160cdf0e10cSrcweir             return -1;
161cdf0e10cSrcweir 
162cdf0e10cSrcweir         int id = storageManager.createScriptStorageWithURI(access, uri);
163cdf0e10cSrcweir 
164cdf0e10cSrcweir         return id;
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir 
getXSimpleFileAccess()167cdf0e10cSrcweir     private XSimpleFileAccess getXSimpleFileAccess() {
168cdf0e10cSrcweir         XSimpleFileAccess access = null;
169cdf0e10cSrcweir 
170cdf0e10cSrcweir         try {
171cdf0e10cSrcweir             Object fa = tParam.getMSF().createInstance(
172cdf0e10cSrcweir                 "com.sun.star.ucb.SimpleFileAccess");
173cdf0e10cSrcweir 
174cdf0e10cSrcweir             access = (XSimpleFileAccess)
175cdf0e10cSrcweir                 UnoRuntime.queryInterface(XSimpleFileAccess.class, fa);
176cdf0e10cSrcweir         }
177cdf0e10cSrcweir         catch (com.sun.star.uno.Exception e) {
178cdf0e10cSrcweir             return null;
179cdf0e10cSrcweir         }
180cdf0e10cSrcweir         return access;
181cdf0e10cSrcweir     }
182cdf0e10cSrcweir }
183