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.storage;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import drafts.com.sun.star.script.framework.storage.XScriptStorageManager;
27cdf0e10cSrcweir import drafts.com.sun.star.script.framework.storage.XScriptInfoAccess;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import java.util.Iterator;
30cdf0e10cSrcweir import java.util.Collection;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
33cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
34cdf0e10cSrcweir import com.sun.star.uno.XInterface;
35cdf0e10cSrcweir import com.sun.star.ucb.XSimpleFileAccess;
36cdf0e10cSrcweir import com.sun.star.uno.Exception;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir import java.io.PrintWriter;
39cdf0e10cSrcweir import lib.MultiMethodTest;
40cdf0e10cSrcweir import lib.StatusException;
41cdf0e10cSrcweir import lib.Parameters;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir public class _XScriptStorageManager extends MultiMethodTest {
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     public XScriptStorageManager oObj = null;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     /**
48cdf0e10cSrcweir     * Retrieves object relation.
49cdf0e10cSrcweir     */
before()50cdf0e10cSrcweir     public void before() throws StatusException {
51cdf0e10cSrcweir     }
52cdf0e10cSrcweir 
_createScriptStorage()53cdf0e10cSrcweir     public void _createScriptStorage() {
54cdf0e10cSrcweir         boolean result = true;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir         Collection c =
57cdf0e10cSrcweir             (Collection) tEnv.getObjRelation("_createScriptStorage");
58cdf0e10cSrcweir 
59cdf0e10cSrcweir         if (c == null) {
60cdf0e10cSrcweir             tRes.tested("createScriptStorage()", false);
61cdf0e10cSrcweir             return;
62cdf0e10cSrcweir         }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir         Iterator tests = c.iterator();
65cdf0e10cSrcweir 
66cdf0e10cSrcweir         while (tests.hasNext()) {
67cdf0e10cSrcweir             Parameters testdata = (Parameters)tests.next();
68cdf0e10cSrcweir             String expected = testdata.get("expected");
69cdf0e10cSrcweir             String output = "";
70cdf0e10cSrcweir 
71cdf0e10cSrcweir             log.println(testdata.get("description"));
72cdf0e10cSrcweir 
73cdf0e10cSrcweir             XSimpleFileAccess access = getXSimpleFileAccess();
74cdf0e10cSrcweir 
75cdf0e10cSrcweir             if (access == null) {
76cdf0e10cSrcweir                 output = "Couldn't create XSimpleFileAccess";
77cdf0e10cSrcweir             }
78cdf0e10cSrcweir             else {
79cdf0e10cSrcweir                 try {
80cdf0e10cSrcweir                     int id = oObj.createScriptStorage(access);
81cdf0e10cSrcweir                     output = "success";
82cdf0e10cSrcweir                 }
83cdf0e10cSrcweir                 catch (com.sun.star.uno.RuntimeException re) {
84cdf0e10cSrcweir                     log.println("Exception from createScriptStorage: " + re);
85cdf0e10cSrcweir                     output = "com.sun.star.uno.RuntimeException";
86cdf0e10cSrcweir                 }
87cdf0e10cSrcweir             }
88cdf0e10cSrcweir             log.println("expected: " + expected + ", output: " + output);
89cdf0e10cSrcweir             result &= output.equals(expected);
90cdf0e10cSrcweir         }
91cdf0e10cSrcweir         tRes.tested("createScriptStorage()", result);
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir 
_createScriptStorageWithURI()94cdf0e10cSrcweir     public void _createScriptStorageWithURI() {
95cdf0e10cSrcweir         boolean result = true;
96cdf0e10cSrcweir 
97cdf0e10cSrcweir         Collection c =
98cdf0e10cSrcweir             (Collection) tEnv.getObjRelation("_createScriptStorageWithURI");
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         if (c == null) {
101cdf0e10cSrcweir             tRes.tested("createScriptStorageWithURI()", false);
102cdf0e10cSrcweir             return;
103cdf0e10cSrcweir         }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         Iterator tests = c.iterator();
106cdf0e10cSrcweir 
107cdf0e10cSrcweir         while (tests.hasNext()) {
108cdf0e10cSrcweir             Parameters testdata = (Parameters)tests.next();
109cdf0e10cSrcweir             String expected = testdata.get("expected");
110cdf0e10cSrcweir             String location = testdata.get("location");
111cdf0e10cSrcweir             String output = "";
112cdf0e10cSrcweir 
113cdf0e10cSrcweir             log.println(testdata.get("description"));
114cdf0e10cSrcweir 
115cdf0e10cSrcweir             String uri = util.utils.getFullTestURL(location);
116cdf0e10cSrcweir             XSimpleFileAccess access = getXSimpleFileAccess();
117cdf0e10cSrcweir 
118cdf0e10cSrcweir             try {
119cdf0e10cSrcweir                 int id = oObj.createScriptStorageWithURI(access, uri);
120cdf0e10cSrcweir 
121cdf0e10cSrcweir                 XInterface ifc = (XInterface)oObj.getScriptStorage(id);
122cdf0e10cSrcweir 
123cdf0e10cSrcweir                 if (ifc == null)
124cdf0e10cSrcweir                     output = "null";
125cdf0e10cSrcweir                 else {
126cdf0e10cSrcweir                     Object info = UnoRuntime.queryInterface(
127cdf0e10cSrcweir                         XScriptInfoAccess.class, ifc);
128cdf0e10cSrcweir 
129cdf0e10cSrcweir                     if (info == null)
130cdf0e10cSrcweir                         output = "null";
131cdf0e10cSrcweir                     else
132cdf0e10cSrcweir                         output = "XScriptInfoAccess.class";
133cdf0e10cSrcweir                 }
134cdf0e10cSrcweir             }
135cdf0e10cSrcweir             catch (com.sun.star.uno.RuntimeException re) {
136cdf0e10cSrcweir                 log.println("Caught RuntimeException: " + re);
137cdf0e10cSrcweir                 output = "com.sun.star.uno.RuntimeException";
138cdf0e10cSrcweir             }
139cdf0e10cSrcweir             log.println("expected: " + expected + ", output: " + output);
140cdf0e10cSrcweir             result &= output.equals(expected);
141cdf0e10cSrcweir         }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir         tRes.tested("createScriptStorageWithURI()", result);
144cdf0e10cSrcweir     }
145cdf0e10cSrcweir 
_getScriptStorage()146cdf0e10cSrcweir     public void _getScriptStorage() {
147cdf0e10cSrcweir         boolean result = true;
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         Collection c =
150cdf0e10cSrcweir             (Collection) tEnv.getObjRelation("_getScriptStorage");
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         if (c == null) {
153cdf0e10cSrcweir             tRes.tested("getScriptStorage()", false);
154cdf0e10cSrcweir             return;
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir         Iterator tests = c.iterator();
158cdf0e10cSrcweir 
159cdf0e10cSrcweir         while (tests.hasNext()) {
160cdf0e10cSrcweir             Parameters testdata = (Parameters)tests.next();
161cdf0e10cSrcweir             String expected = testdata.get("expected");
162cdf0e10cSrcweir             String location = testdata.get("location");
163cdf0e10cSrcweir             String output = "";
164cdf0e10cSrcweir 
165cdf0e10cSrcweir             log.println(testdata.get("description"));
166cdf0e10cSrcweir 
167cdf0e10cSrcweir             try {
168cdf0e10cSrcweir                 int storageid = getStorageId(location);
169cdf0e10cSrcweir 
170cdf0e10cSrcweir                 XInterface ifc = (XInterface)oObj.getScriptStorage(storageid);
171cdf0e10cSrcweir 
172cdf0e10cSrcweir                 if (ifc == null)
173cdf0e10cSrcweir                     output = "null";
174cdf0e10cSrcweir                 else {
175cdf0e10cSrcweir                     Object info = UnoRuntime.queryInterface(
176cdf0e10cSrcweir                         XScriptInfoAccess.class, ifc);
177cdf0e10cSrcweir 
178cdf0e10cSrcweir                     if (info == null)
179cdf0e10cSrcweir                         output = "null";
180cdf0e10cSrcweir                     else
181cdf0e10cSrcweir                         output = "XScriptInfoAccess.class";
182cdf0e10cSrcweir                 }
183cdf0e10cSrcweir             }
184cdf0e10cSrcweir             catch (com.sun.star.uno.RuntimeException re) {
185cdf0e10cSrcweir                 log.println("Caught RuntimeException: " + re);
186cdf0e10cSrcweir                 output = "com.sun.star.uno.RuntimeException";
187cdf0e10cSrcweir             }
188cdf0e10cSrcweir             log.println("expected: " + expected + ", output: " + output);
189cdf0e10cSrcweir             result &= output.equals(expected);
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir         tRes.tested("getScriptStorage()", result);
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir 
_refreshScriptStorage()194cdf0e10cSrcweir     public void _refreshScriptStorage() {
195cdf0e10cSrcweir         boolean result = true;
196cdf0e10cSrcweir 
197cdf0e10cSrcweir         Collection c =
198cdf0e10cSrcweir             (Collection) tEnv.getObjRelation("_refreshScriptStorage");
199cdf0e10cSrcweir 
200cdf0e10cSrcweir         if (c == null) {
201cdf0e10cSrcweir             tRes.tested("refreshScriptStorage()", false);
202cdf0e10cSrcweir             return;
203cdf0e10cSrcweir         }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir         Iterator tests = c.iterator();
206cdf0e10cSrcweir 
207cdf0e10cSrcweir         while (tests.hasNext()) {
208cdf0e10cSrcweir             Parameters testdata = (Parameters)tests.next();
209cdf0e10cSrcweir             String expected = testdata.get("expected");
210cdf0e10cSrcweir             String location = testdata.get("location");
211cdf0e10cSrcweir             String output = "";
212cdf0e10cSrcweir 
213cdf0e10cSrcweir             log.println(testdata.get("description"));
214cdf0e10cSrcweir 
215cdf0e10cSrcweir             try {
216cdf0e10cSrcweir                 String uri = util.utils.getFullTestURL(location);
217cdf0e10cSrcweir                 log.println("calling refreshScriptStorage with URI: " + uri);
218cdf0e10cSrcweir                 oObj.refreshScriptStorage(uri);
219cdf0e10cSrcweir                 output = "success";
220cdf0e10cSrcweir             }
221cdf0e10cSrcweir             catch (com.sun.star.uno.RuntimeException re) {
222cdf0e10cSrcweir                 log.println("Caught RuntimeException: " + re);
223cdf0e10cSrcweir                 output = "com.sun.star.uno.RuntimeException";
224cdf0e10cSrcweir             }
225cdf0e10cSrcweir             log.println("expected: " + expected + ", output: " + output);
226cdf0e10cSrcweir             result &= output.equals(expected);
227cdf0e10cSrcweir         }
228cdf0e10cSrcweir         tRes.tested("refreshScriptStorage()", result);
229cdf0e10cSrcweir     }
230cdf0e10cSrcweir 
getStorageId(String location)231cdf0e10cSrcweir     private int getStorageId(String location) {
232cdf0e10cSrcweir 
233cdf0e10cSrcweir         if (location.equals("share"))
234cdf0e10cSrcweir             return 0;
235cdf0e10cSrcweir 
236cdf0e10cSrcweir         if (location.equals("user"))
237cdf0e10cSrcweir             return 1;
238cdf0e10cSrcweir 
239cdf0e10cSrcweir         String uri = util.utils.getFullTestURL(location);
240cdf0e10cSrcweir 
241cdf0e10cSrcweir         XSimpleFileAccess access = getXSimpleFileAccess();
242cdf0e10cSrcweir         if (access == null)
243cdf0e10cSrcweir             return -1;
244cdf0e10cSrcweir 
245cdf0e10cSrcweir         return oObj.createScriptStorageWithURI(access, uri);
246cdf0e10cSrcweir     }
247cdf0e10cSrcweir 
getXSimpleFileAccess()248cdf0e10cSrcweir     private XSimpleFileAccess getXSimpleFileAccess() {
249cdf0e10cSrcweir         XSimpleFileAccess access = null;
250cdf0e10cSrcweir 
251cdf0e10cSrcweir         try {
252cdf0e10cSrcweir             Object fa = tParam.getMSF().createInstance(
253cdf0e10cSrcweir                 "com.sun.star.ucb.SimpleFileAccess");
254cdf0e10cSrcweir 
255cdf0e10cSrcweir             access = (XSimpleFileAccess)
256cdf0e10cSrcweir                 UnoRuntime.queryInterface(XSimpleFileAccess.class, fa);
257cdf0e10cSrcweir         }
258cdf0e10cSrcweir         catch (com.sun.star.uno.Exception e) {
259cdf0e10cSrcweir             return null;
260cdf0e10cSrcweir         }
261cdf0e10cSrcweir         return access;
262cdf0e10cSrcweir     }
263cdf0e10cSrcweir }
264