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.XScriptNameResolver;
35 import drafts.com.sun.star.script.framework.storage.XScriptInfo;
36 import drafts.com.sun.star.script.framework.storage.XScriptStorageManager;
37 
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.ucb.XSimpleFileAccess;
40 import com.sun.star.beans.XPropertySet;
41 import com.sun.star.uno.XComponentContext;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XInterface;
44 
45 import lib.MultiMethodTest;
46 import lib.StatusException;
47 import lib.Parameters;
48 
49 public class _XScriptNameResolver extends MultiMethodTest {
50 
51     public XScriptNameResolver oObj = null;
52     private XScriptStorageManager storageManager = null;
53 
54     /**
55     * Retrieves object relation.
56     */
57     public void before() throws StatusException {
58     }
59 
60     public void _resolve() {
61         boolean result = true;
62 
63         Collection c =
64             (Collection) tEnv.getObjRelation("_resolve");
65 
66         Iterator tests;
67 
68         if (c != null) {
69             tests = c.iterator();
70 
71             while (tests.hasNext()) {
72                 result &= runResolveTest((Parameters)tests.next());
73             }
74         }
75         else {
76             result = false;
77         }
78 
79         tRes.tested("resolve()", result);
80     }
81 
82     private boolean runResolveTest(Parameters data) {
83         String description = data.get("description");
84         String location = data.get("location");
85         String logicalname = data.get("logicalname");
86         String expected = data.get("expected");
87         String output = "";
88 
89         int storageId = getStorageId(location);
90 
91         log.println(description + ": " + logicalname);
92 
93         HashMap map = new HashMap();
94         map.put("SCRIPTING_DOC_STORAGE_ID", new Integer(storageId));
95         map.put("SCRIPTING_DOC_URI", util.utils.getFullTestURL(location));
96 
97         Parameters params = new Parameters(map);
98         Object[] args = new Object[] {params};
99 
100         try {
101             XInterface ifc = (XInterface) oObj.resolve(logicalname, args);
102 
103             if (ifc == null)
104                 output = "null";
105             else if (UnoRuntime.queryInterface(XScriptInfo.class, ifc) == null)
106                 output = "null";
107             else
108                 output = "XScriptInfo.class";
109         }
110         catch (com.sun.star.lang.IllegalArgumentException iae) {
111             log.println("caught IllegalArgumentException: " + iae);
112             output = "com.sun.star.lang.IllegalArgumentException";
113         }
114         catch (com.sun.star.script.CannotConvertException cce) {
115             log.println("caught CannotConvertException: " + cce);
116             output = "com.sun.star.script.CannotConvertException";
117         }
118         catch (com.sun.star.uno.RuntimeException re) {
119             log.println("caught RuntimeException: " + re);
120             output = "com.sun.star.uno.RuntimeException";
121         }
122 
123         log.println("expected: " + expected + ", output: " + output);
124         if (output.equals(expected))
125             return true;
126         else
127             return false;
128     }
129 
130     private int getStorageId(String location) {
131 
132         if (location.equals("share"))
133             return 0;
134 
135         if (location.equals("user"))
136             return 1;
137 
138         XSimpleFileAccess access = null;
139         String uri = util.utils.getFullTestURL(location);
140 
141         if (storageManager == null) {
142             try {
143                 XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(
144                     XPropertySet.class, tParam.getMSF());
145 
146                 XComponentContext xContext = (XComponentContext)
147                     UnoRuntime.queryInterface(XComponentContext.class,
148                     xProp.getPropertyValue("DefaultContext"));
149 
150                 XInterface ifc = (XInterface)
151                     xContext.getValueByName("/singletons/drafts.com.sun.star." +
152                     "script.framework.storage.theScriptStorageManager");
153 
154                 storageManager = (XScriptStorageManager)
155                     UnoRuntime.queryInterface(XScriptStorageManager.class, ifc);
156             }
157             catch( Exception e ) {
158                 return -1;
159             }
160         }
161 
162         access = getXSimpleFileAccess();
163         if (access == null)
164             return -1;
165 
166         int id = storageManager.createScriptStorageWithURI(access, uri);
167 
168         return id;
169     }
170 
171     private XSimpleFileAccess getXSimpleFileAccess() {
172         XSimpleFileAccess access = null;
173 
174         try {
175             Object fa = tParam.getMSF().createInstance(
176                 "com.sun.star.ucb.SimpleFileAccess");
177 
178             access = (XSimpleFileAccess)
179                 UnoRuntime.queryInterface(XSimpleFileAccess.class, fa);
180         }
181         catch (com.sun.star.uno.Exception e) {
182             return null;
183         }
184         return access;
185     }
186 }
187