1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package ifc.registry;
24 
25 import lib.MultiMethodTest;
26 import lib.Status;
27 import lib.StatusException;
28 import util.RegistryTools;
29 
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.registry.InvalidRegistryException;
32 import com.sun.star.registry.XRegistryKey;
33 import com.sun.star.registry.XSimpleRegistry;
34 
35 
36 /**
37 * Testing <code>com.sun.star.registry.XSimpleRegistry</code>
38 * interface methods :
39 * <ul>
40 *  <li><code> getURL()</code></li>
41 *  <li><code> open()</code></li>
42 *  <li><code> isValid()</code></li>
43 *  <li><code> close()</code></li>
44 *  <li><code> destroy()</code></li>
45 *  <li><code> getRootKey()</code></li>
46 *  <li><code> isReadOnly()</code></li>
47 *  <li><code> mergeKey()</code></li>
48 * </ul> <p>
49 * This test needs the following object relations :
50 * <ul>
51 *  <li> <code>'NR'</code> <b>optional</b> (of type <code>String</code>):
52 *   if this object relation isn't null than the testing component
53 *   doesn't support some methods of the interface
54 *   (<code>open(), close(), destroy()</code>)</li>
55 *  <li> <code>'XSimpleRegistry.open'</code> (of type <code>String</code>):
56 *    The full system path to the registry file which is opened and modified.
57 *  </li>
58 *  <li> <code>'XSimpleRegistry.destroy'</code> (of type <code>String</code>):
59 *    The full system path to the registry fiel which is destroyed.
60 *  </li>
61 *  <li> <code>'XSimpleRegistry.merge'</code> (of type <code>String</code>):
62 *    The full system path to the registry file which is merged with the
63 *    registry tested.
64 *  </li>
65 * </ul> <p>
66 * @see com.sun.star.registry.XSimpleRegistry
67 */
68 public class _XSimpleRegistry extends MultiMethodTest {
69     public XSimpleRegistry oObj = null;
70     protected String nr = null;
71     protected boolean configuration = false;
72     protected String openF = null;
73     protected String destroyF = null;
74     protected String mergeF = null;
75 
76     /**
77     * Retrieves object relations.
78     * @throws StatusException If one of required relations not found.
79     */
80     protected void before() {
81         if (tEnv.getObjRelation("configuration") != null) {
82             configuration = true;
83         }
84 
85         nr = (String) tEnv.getObjRelation("NR");
86 
87         openF = (String) tEnv.getObjRelation("XSimpleRegistry.open");
88 
89         if (openF == null) {
90             throw new StatusException(Status.failed(
91                                               "Relation 'XSimpleRegistry.open' not found"));
92         }
93 
94         destroyF = (String) tEnv.getObjRelation("XSimpleRegistry.destroy");
95 
96         if (destroyF == null) {
97             throw new StatusException(Status.failed(
98                                               "Relation 'XSimpleRegistry.destroy' not found"));
99         }
100 
101         mergeF = (String) tEnv.getObjRelation("XSimpleRegistry.merge");
102 
103         if (mergeF == null) {
104             throw new StatusException(Status.failed(
105                                               "Relation 'XSimpleRegistry.merge' not found"));
106         }
107     }
108 
109     /**
110     * If the method is supported opens the registry key with the URL
111     * from <code>'XSimpleRegistry.open'</code> relation, then closes it. <p>
112     *
113     * Has <b> OK </b> status if the method isn't supported by the component
114     * (the object relation <code>'NR'</code> isn't null) or no exceptions were
115     * thrown during open/close operations. <p>
116     */
117     public void _open() {
118         if (nr != null) {
119             log.println("'open()' isn't supported by '" + nr + "'");
120             tRes.tested("open()", true);
121 
122             return;
123         }
124 
125         log.println("Trying to open registry :" + openF);
126 
127         try {
128             oObj.open(openF, false, true);
129             oObj.close();
130         } catch (InvalidRegistryException e) {
131             e.printStackTrace(log);
132             tRes.tested("open()", false);
133 
134             return;
135         }
136 
137         tRes.tested("open()", true);
138     }
139 
140     /**
141     * Test opens the registry key with the URL from
142     * <code>'XSimpleRegistry.open'</code> relation not only for read,
143     * calls the method, checks returned value and closes the registry. <p>
144     *
145     * Has <b> OK </b> status if returned value is false and no exceptions were
146     * thrown. <p>
147     */
148     public void _isReadOnly() {
149         boolean result = false;
150 
151         try {
152             openReg(oObj, openF, false, true);
153             result = !oObj.isReadOnly();
154             closeReg(oObj);
155         } catch (InvalidRegistryException e) {
156             e.printStackTrace(log);
157             result = false;
158         }
159 
160         tRes.tested("isReadOnly()", result);
161     }
162 
163     /**
164     * Test opens the registry key with the URL from
165     * <code>'XSimpleRegistry.open'</code> relation, calls the method,
166     * checks returned value and closes the registry key. <p>
167     *
168     * Has <b>OK</b> status if returned value isn't null and no exceptions were
169     * thrown. <p>
170     */
171     public void _getRootKey() {
172         boolean result = false;
173 
174         try {
175             openReg(oObj, openF, false, true);
176 
177             XRegistryKey rootKey = oObj.getRootKey();
178             result = rootKey != null;
179             closeReg(oObj);
180         } catch (InvalidRegistryException e) {
181             e.printStackTrace(log);
182             result = false;
183         }
184 
185         tRes.tested("getRootKey()", result);
186     }
187 
188     /**
189     * Merges the current registry with the registry from URL got from
190     * <code>'XSimpleRegistry.merge'</code> relation under 'MergeKey' key.
191     * Then the keys of these two registries retrieved :
192     * <ul>
193     *  <li> Root key from 'XSimpleRegistry.merge' registry </li>
194     *  <li> 'MergeKey' key from the current registry </li>
195     * </ul>
196     * Then these two keys are recursively compared. <p>
197     *
198     * Has <b> OK </b> status if the method isn't supported by the component
199     * (the object relation <code>'NR'</code> isn't null)
200     * or
201     * if it's supported and after successfull merging the keys mentioned
202     * above are recursively equal. <p>
203     */
204     public void _mergeKey() {
205         if (configuration) {
206             log.println(
207                     "You can't merge into this registry. It's just a wrapper for a configuration node, which has a fixed structure which can not be modified");
208             tRes.tested("mergeKey()", true);
209 
210             return;
211         }
212 
213         if (nr != null) {
214             log.println("'mergeKey()' isn't supported by '" + nr + "'");
215             tRes.tested("mergeKey()", true);
216 
217             return;
218         }
219 
220         openReg(oObj, openF, false, true);
221 
222         try {
223             RegistryTools.printRegistryInfo(oObj.getRootKey(), log);
224             oObj.mergeKey("MergeKey", mergeF);
225             RegistryTools.printRegistryInfo(oObj.getRootKey(), log);
226         } catch (com.sun.star.registry.MergeConflictException e) {
227             e.printStackTrace(log);
228             tRes.tested("mergeKey()", false);
229 
230             return;
231         } catch (com.sun.star.registry.InvalidRegistryException e) {
232             e.printStackTrace(log);
233             tRes.tested("mergeKey()", false);
234 
235             return;
236         }
237 
238         boolean isEqual = false;
239         XSimpleRegistry reg = null;
240 
241         try {
242             reg = RegistryTools.createRegistryService((XMultiServiceFactory) tParam.getMSF());
243         } catch (com.sun.star.uno.Exception e) {
244             log.print("Can't create registry service: ");
245             e.printStackTrace(log);
246             tRes.tested("mergeKey()", false);
247 
248             return;
249         }
250 
251         openReg(reg, mergeF, false, true);
252 
253         try {
254             XRegistryKey key = oObj.getRootKey().openKey("MergeKey");
255             XRegistryKey mergeKey = reg.getRootKey();
256             isEqual = RegistryTools.compareKeyTrees(key, mergeKey);
257         } catch (com.sun.star.registry.InvalidRegistryException e) {
258             log.print("Can't get root key: ");
259             e.printStackTrace(log);
260             tRes.tested("mergeKey()", false);
261 
262             return;
263         }
264 
265         closeReg(reg);
266         closeReg(oObj);
267 
268         tRes.tested("mergeKey()", isEqual);
269     }
270 
271     /**
272     * Test opens the registry key with the URL from
273     * <code>'XSimpleRegistry.open'</code> relation, calls the method,
274     * checks returned value and closes the registry key. <p>
275     *
276     * Has <b> OK </b> status if returned value isn't null and if length of the
277     * returned string is greater than 0. <p>
278     */
279     public void _getURL() {
280         openReg(oObj, openF, false, true);
281 
282         String url = oObj.getURL();
283         closeReg(oObj);
284         log.println("Getting URL: " + url+";");
285         tRes.tested("getURL()", (url != null));
286     }
287 
288     /**
289     * Test checks value returned by the object relation <code>'NR'</code>,
290     * opens the registry key with the URL from
291     * <code>XSimpleRegistry.open'</code> relation, calls the method
292     * and checks the validity of the registry key. <p>
293     *
294     * Has <b> OK </b> status if the registry key isn't valid after the method
295     * call, or if the method isn't supported by the component (the object
296     * relation <code>'NR'</code> isn't null). <p>
297     */
298     public void _close() {
299         if (nr != null) {
300             log.println("'close()' isn't supported by '" + nr + "'");
301             tRes.tested("close()", true);
302 
303             return;
304         }
305 
306         try {
307             oObj.open(openF, false, true);
308             oObj.close();
309         } catch (com.sun.star.registry.InvalidRegistryException e) {
310             e.printStackTrace(log);
311             tRes.tested("close()", false);
312 
313             return;
314         }
315 
316         tRes.tested("close()", !oObj.isValid());
317     }
318 
319     /**
320     * Test checks value returned by the object relation <code>'NR'</code>,
321     * opens the registry key with the URL from
322     * <code>'XSimpleRegistry.destroy'</code> relation, calls the method
323     * and checks the validity of the registry key. <p>
324     *
325     * Has <b> OK </b> status if the registry key isn't valid after the method
326     * call, or if the method isn't supported by the component (the object
327     * relation <code>'NR'</code> isn't null). <p>
328     */
329     public void _destroy() {
330         if (configuration) {
331             log.println(
332                     "This registry is a wrapper for a configuration access. It can not be destroyed.");
333             tRes.tested("destroy()", true);
334 
335             return;
336         }
337 
338         if (nr != null) {
339             log.println("'destroy()' isn't supported by '" + nr + "'");
340             tRes.tested("destroy()", true);
341 
342             return;
343         }
344 
345         try {
346             oObj.open(destroyF, false, true);
347             oObj.destroy();
348         } catch (com.sun.star.registry.InvalidRegistryException e) {
349             e.printStackTrace(log);
350             tRes.tested("destroy()", false);
351 
352             return;
353         }
354 
355         tRes.tested("destroy()", !oObj.isValid());
356     }
357 
358     /**
359     * Test opens the registry key with the URL from
360     * <code>'XSimpleRegistry.open'</code> relation, calls the method,
361     * checks returned value and closes the registry key. <p>
362     * Has <b> OK </b> status if returned value is true. <p>
363     */
364     public void _isValid() {
365         boolean valid = true;
366 
367         openReg(oObj, openF, false, true);
368         valid = oObj.isValid();
369         closeReg(oObj);
370 
371         tRes.tested("isValid()", valid);
372     }
373 
374     /**
375     * Method calls <code>close()</code> of the interface
376     * <code>com.sun.star.registry.XRegistryKey</code>. <p>
377     * @param reg interface <code>com.sun.star.registry.XRegistryKey</code>
378     * @param url specifies the complete URL to access the data source
379     * @param arg1 specifies if the data source should be opened for read only
380     * @param arg2 specifies if the data source should be created if it does not
381     * already exist
382     */
383     public void openReg(XSimpleRegistry reg, String url, boolean arg1,
384                         boolean arg2) {
385         if (nr == null) {
386             try {
387                 reg.open(url, arg1, arg2);
388             } catch (com.sun.star.registry.InvalidRegistryException e) {
389                 log.print("Couldn't open registry:");
390                 e.printStackTrace(log);
391             }
392         }
393     }
394 
395     /**
396     * Method calls <code>close()</code> of the interface
397     * <code>com.sun.star.registry.XRegistryKey</code>. <p>
398     * @param reg <code>com.sun.star.registry.XRegistryKey</code>
399     */
400     public void closeReg(XSimpleRegistry reg) {
401         if (nr == null) {
402             try {
403                 reg.close();
404             } catch (com.sun.star.registry.InvalidRegistryException e) {
405                 log.print("Couldn't close registry:");
406                 e.printStackTrace(log);
407             }
408         }
409     }
410 }
411