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.storage;
29 
30 import ifc.script.framework.ScriptingUtils;
31 
32 import drafts.com.sun.star.script.framework.storage.XScriptInfoAccess;
33 import drafts.com.sun.star.script.framework.storage.XScriptInfo;
34 
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.uno.XInterface;
38 import com.sun.star.ucb.XSimpleFileAccess;
39 import com.sun.star.uno.Exception;
40 import com.sun.star.beans.XPropertySet;
41 
42 import java.io.PrintWriter;
43 import lib.MultiMethodTest;
44 import lib.StatusException;
45 import lib.Parameters;
46 
47 import java.util.Collection;
48 import java.util.Iterator;
49 
50 public class _XScriptInfoAccess extends MultiMethodTest {
51 
52     public XScriptInfoAccess oObj = null;
53 
54     /**
55     * Retrieves object relation.
56     */
57     public void before() throws StatusException {
58     }
59 
60     public void _getScriptLogicalNames() {
61         boolean result = true;
62 
63         Collection c =
64             (Collection) tEnv.getObjRelation("_getScriptLogicalNames");
65 
66         Iterator tests;
67 
68         if (c != null) {
69             tests = c.iterator();
70 
71             while (tests.hasNext()) {
72                 result &= runGetScriptLogicalNamesTest((Parameters)tests.next());
73             }
74         }
75         else {
76             result = false;
77         }
78 
79         tRes.tested("getScriptLogicalNames()", result);
80     }
81 
82     private boolean runGetScriptLogicalNamesTest(Parameters testdata) {
83         String description = testdata.get("description");
84         String expected = testdata.get("expected");
85         String output = "";
86 
87         log.println(testdata.get("description"));
88 
89         // try {
90             log.println("In _XScriptInfoAccess.getScriptLogicalNames()");
91             String[] logicalNames = oObj.getScriptLogicalNames();
92 
93             if (logicalNames == null)
94                 output = "null";
95             else if (logicalNames.length == 0)
96                 output = "empty";
97             else {
98                 for (int i = 0; i < logicalNames.length; i++) {
99                     if (logicalNames[i].equals(expected)) {
100                         output = logicalNames[i];
101                         break;
102                     }
103                 }
104             }
105         // }
106         // catch (com.sun.star.uno.Exception e) {
107             // log.println("Caught UNO Exception :" + e);
108             // output = "com.sun.star.uno.Exception";
109         // }
110 
111         log.println("expected: " + expected + ", output: " + output);
112         if (output.equals(expected))
113             return true;
114         else
115             return false;
116     }
117 
118     public void _getImplementations() {
119         boolean result = true;
120 
121         Collection c =
122             (Collection) tEnv.getObjRelation("_getImplementations");
123 
124         Iterator tests;
125 
126         if (c != null) {
127             tests = c.iterator();
128 
129             while (tests.hasNext()) {
130                 result &= runGetImplementationsTest((Parameters)tests.next());
131             }
132         }
133         else {
134             result = false;
135         }
136 
137         tRes.tested("getImplementations()", result);
138     }
139 
140     private boolean runGetImplementationsTest(Parameters testdata) {
141         String description = testdata.get("description");
142         String logicalname = testdata.get("logicalname");
143         String expected = testdata.get("expected");
144         String output = "";
145 
146         log.println(testdata.get("description"));
147 
148 	// performs a basic check to see if 1 match (XScriptInfo) is returned
149 	// the XScriptInfo object is tested more completely in _XScriptInfo
150 	// which is drive from ScriptInfo
151 
152         try {
153             XScriptInfo[] impls = oObj.getImplementations(logicalname);
154 
155             // should only be one match
156             if (impls == null)
157                 output = "null";
158             else if (impls.length == 0)
159                 output = "empty";
160             else
161                 output = impls[0].getLogicalName();
162         }
163         catch (com.sun.star.uno.Exception e) {
164             log.println("Caught UNO Exception:" + e);
165             output = "com.sun.star.uno.Exception";
166         }
167 
168         log.println("expected: " + expected + ", output: " + output);
169         if (output.equals(expected))
170             return true;
171         else
172             return false;
173     }
174 
175     public void _getAllImplementations() {
176         boolean result = true;
177 
178         Collection c =
179             (Collection) tEnv.getObjRelation("_getAllImplementations");
180 
181         Iterator tests;
182 
183         if (c != null) {
184             tests = c.iterator();
185 
186             while (tests.hasNext()) {
187                 result &= runGetAllImplementationsTest((Parameters)tests.next());
188             }
189         }
190         else {
191             result = false;
192         }
193 
194         tRes.tested("getAllImplementations()", result);
195     }
196 
197     private boolean runGetAllImplementationsTest(Parameters testdata) {
198         String description = testdata.get("description");
199         String location = testdata.get("location");
200         String expected = testdata.get("expected");
201         String output = "";
202 
203         log.println(testdata.get("description"));
204 
205         Object obj = ScriptingUtils.getDefault().getScriptStorage(
206             tParam.getMSF(), location);
207 
208         XScriptInfoAccess access = (XScriptInfoAccess)
209             UnoRuntime.queryInterface(XScriptInfoAccess.class, obj);
210 
211         XScriptInfo[] impls = access.getAllImplementations();
212 
213         if (impls == null || impls.length == 0) {
214             output = "empty";
215         }
216         else {
217             for (int i = 0; i < impls.length - 1; i++)
218                 output += impls[i].getLogicalName() + ",";
219             output += impls[impls.length - 1].getLogicalName();
220         }
221 
222         log.println("expected: " + expected + ", output: " + output);
223         if (output.equals(expected))
224             return true;
225         else
226             return false;
227     }
228 }
229