Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / registry / _XSimpleRegistry.java
blob93dfeb53367de07c1f5d3a71bee09b640ce3eb38
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package ifc.registry;
20 import lib.MultiMethodTest;
21 import lib.Status;
22 import lib.StatusException;
23 import util.RegistryTools;
25 import com.sun.star.registry.InvalidRegistryException;
26 import com.sun.star.registry.XRegistryKey;
27 import com.sun.star.registry.XSimpleRegistry;
30 /**
31 * Testing <code>com.sun.star.registry.XSimpleRegistry</code>
32 * interface methods :
33 * <ul>
34 * <li><code> getURL()</code></li>
35 * <li><code> open()</code></li>
36 * <li><code> isValid()</code></li>
37 * <li><code> close()</code></li>
38 * <li><code> destroy()</code></li>
39 * <li><code> getRootKey()</code></li>
40 * <li><code> isReadOnly()</code></li>
41 * <li><code> mergeKey()</code></li>
42 * </ul> <p>
43 * This test needs the following object relations :
44 * <ul>
45 * <li> <code>'NR'</code> <b>optional</b> (of type <code>String</code>):
46 * if this object relation isn't null than the testing component
47 * doesn't support some methods of the interface
48 * (<code>open(), close(), destroy()</code>)</li>
49 * <li> <code>'XSimpleRegistry.open'</code> (of type <code>String</code>):
50 * The full system path to the registry file which is opened and modified.
51 * </li>
52 * <li> <code>'XSimpleRegistry.destroy'</code> (of type <code>String</code>):
53 * The full system path to the registry file which is destroyed.
54 * </li>
55 * <li> <code>'XSimpleRegistry.merge'</code> (of type <code>String</code>):
56 * The full system path to the registry file which is merged with the
57 * registry tested.
58 * </li>
59 * </ul> <p>
60 * @see com.sun.star.registry.XSimpleRegistry
62 public class _XSimpleRegistry extends MultiMethodTest {
63 public XSimpleRegistry oObj = null;
64 protected String nr = null;
65 protected boolean configuration = false;
66 protected String openF = null;
67 protected String destroyF = null;
68 protected String mergeF = null;
70 /**
71 * Retrieves object relations.
72 * @throws StatusException If one of required relations not found.
74 @Override
75 protected void before() {
76 if (tEnv.getObjRelation("configuration") != null) {
77 configuration = true;
80 nr = (String) tEnv.getObjRelation("NR");
82 openF = (String) tEnv.getObjRelation("XSimpleRegistry.open");
84 if (openF == null) {
85 throw new StatusException(Status.failed(
86 "Relation 'XSimpleRegistry.open' not found"));
89 destroyF = (String) tEnv.getObjRelation("XSimpleRegistry.destroy");
91 if (destroyF == null) {
92 throw new StatusException(Status.failed(
93 "Relation 'XSimpleRegistry.destroy' not found"));
96 mergeF = (String) tEnv.getObjRelation("XSimpleRegistry.merge");
98 if (mergeF == null) {
99 throw new StatusException(Status.failed(
100 "Relation 'XSimpleRegistry.merge' not found"));
105 * If the method is supported opens the registry key with the URL
106 * from <code>'XSimpleRegistry.open'</code> relation, then closes it. <p>
108 * Has <b> OK </b> status if the method isn't supported by the component
109 * (the object relation <code>'NR'</code> isn't null) or no exceptions were
110 * thrown during open/close operations. <p>
112 public void _open() {
113 if (nr != null) {
114 log.println("'open()' isn't supported by '" + nr + "'");
115 tRes.tested("open()", true);
117 return;
120 log.println("Trying to open registry :" + openF);
122 try {
123 oObj.open(openF, false, true);
124 oObj.close();
125 } catch (InvalidRegistryException e) {
126 e.printStackTrace(log);
127 tRes.tested("open()", false);
129 return;
132 tRes.tested("open()", true);
136 * Test opens the registry key with the URL from
137 * <code>'XSimpleRegistry.open'</code> relation not only for read,
138 * calls the method, checks returned value and closes the registry. <p>
140 * Has <b> OK </b> status if returned value is false and no exceptions were
141 * thrown. <p>
143 public void _isReadOnly() {
144 boolean result = false;
146 try {
147 openReg(oObj, openF, false, true);
148 result = !oObj.isReadOnly();
149 closeReg(oObj);
150 } catch (InvalidRegistryException e) {
151 e.printStackTrace(log);
152 result = false;
155 tRes.tested("isReadOnly()", result);
159 * Test opens the registry key with the URL from
160 * <code>'XSimpleRegistry.open'</code> relation, calls the method,
161 * checks returned value and closes the registry key. <p>
163 * Has <b>OK</b> status if returned value isn't null and no exceptions were
164 * thrown. <p>
166 public void _getRootKey() {
167 boolean result = false;
169 try {
170 openReg(oObj, openF, false, true);
172 XRegistryKey rootKey = oObj.getRootKey();
173 result = rootKey != null;
174 closeReg(oObj);
175 } catch (InvalidRegistryException e) {
176 e.printStackTrace(log);
177 result = false;
180 tRes.tested("getRootKey()", result);
184 * Merges the current registry with the registry from URL got from
185 * <code>'XSimpleRegistry.merge'</code> relation under 'MergeKey' key.
186 * Then the keys of these two registries retrieved :
187 * <ul>
188 * <li> Root key from 'XSimpleRegistry.merge' registry </li>
189 * <li> 'MergeKey' key from the current registry </li>
190 * </ul>
191 * Then these two keys are recursively compared. <p>
193 * Has <b> OK </b> status if the method isn't supported by the component
194 * (the object relation <code>'NR'</code> isn't null)
195 * or
196 * if it's supported and after successful merging the keys mentioned
197 * above are recursively equal. <p>
199 public void _mergeKey() {
200 if (configuration) {
201 log.println(
202 "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");
203 tRes.tested("mergeKey()", true);
205 return;
208 if (nr != null) {
209 log.println("'mergeKey()' isn't supported by '" + nr + "'");
210 tRes.tested("mergeKey()", true);
212 return;
215 openReg(oObj, openF, false, true);
217 try {
218 RegistryTools.printRegistryInfo(oObj.getRootKey(), log);
219 oObj.mergeKey("MergeKey", mergeF);
220 RegistryTools.printRegistryInfo(oObj.getRootKey(), log);
221 } catch (com.sun.star.registry.MergeConflictException e) {
222 e.printStackTrace(log);
223 tRes.tested("mergeKey()", false);
225 return;
226 } catch (com.sun.star.registry.InvalidRegistryException e) {
227 e.printStackTrace(log);
228 tRes.tested("mergeKey()", false);
230 return;
233 boolean isEqual = false;
234 XSimpleRegistry reg = null;
236 try {
237 reg = RegistryTools.createRegistryService(tParam.getMSF());
238 } catch (com.sun.star.uno.Exception e) {
239 log.print("Can't create registry service: ");
240 e.printStackTrace(log);
241 tRes.tested("mergeKey()", false);
243 return;
246 openReg(reg, mergeF, false, true);
248 try {
249 XRegistryKey key = oObj.getRootKey().openKey("MergeKey");
250 XRegistryKey mergeKey = reg.getRootKey();
251 isEqual = RegistryTools.compareKeyTrees(key, mergeKey);
252 } catch (com.sun.star.registry.InvalidRegistryException e) {
253 log.print("Can't get root key: ");
254 e.printStackTrace(log);
255 tRes.tested("mergeKey()", false);
257 return;
260 closeReg(reg);
261 closeReg(oObj);
263 tRes.tested("mergeKey()", isEqual);
267 * Test opens the registry key with the URL from
268 * <code>'XSimpleRegistry.open'</code> relation, calls the method,
269 * checks returned value and closes the registry key. <p>
271 * Has <b> OK </b> status if returned value isn't null and if length of the
272 * returned string is greater than 0. <p>
274 public void _getURL() {
275 openReg(oObj, openF, false, true);
277 String url = oObj.getURL();
278 closeReg(oObj);
279 log.println("Getting URL: " + url+";");
280 tRes.tested("getURL()", (url != null));
284 * Test checks value returned by the object relation <code>'NR'</code>,
285 * opens the registry key with the URL from
286 * <code>XSimpleRegistry.open'</code> relation, calls the method
287 * and checks the validity of the registry key. <p>
289 * Has <b> OK </b> status if the registry key isn't valid after the method
290 * call, or if the method isn't supported by the component (the object
291 * relation <code>'NR'</code> isn't null). <p>
293 public void _close() {
294 if (nr != null) {
295 log.println("'close()' isn't supported by '" + nr + "'");
296 tRes.tested("close()", true);
298 return;
301 try {
302 oObj.open(openF, false, true);
303 oObj.close();
304 } catch (com.sun.star.registry.InvalidRegistryException e) {
305 e.printStackTrace(log);
306 tRes.tested("close()", false);
308 return;
311 tRes.tested("close()", !oObj.isValid());
315 * Test checks value returned by the object relation <code>'NR'</code>,
316 * opens the registry key with the URL from
317 * <code>'XSimpleRegistry.destroy'</code> relation, calls the method
318 * and checks the validity of the registry key. <p>
320 * Has <b> OK </b> status if the registry key isn't valid after the method
321 * call, or if the method isn't supported by the component (the object
322 * relation <code>'NR'</code> isn't null). <p>
324 public void _destroy() {
325 if (configuration) {
326 log.println(
327 "This registry is a wrapper for a configuration access. It can not be destroyed.");
328 tRes.tested("destroy()", true);
330 return;
333 if (nr != null) {
334 log.println("'destroy()' isn't supported by '" + nr + "'");
335 tRes.tested("destroy()", true);
337 return;
340 try {
341 oObj.open(destroyF, false, true);
342 oObj.destroy();
343 } catch (com.sun.star.registry.InvalidRegistryException e) {
344 e.printStackTrace(log);
345 tRes.tested("destroy()", false);
347 return;
350 tRes.tested("destroy()", !oObj.isValid());
354 * Test opens the registry key with the URL from
355 * <code>'XSimpleRegistry.open'</code> relation, calls the method,
356 * checks returned value and closes the registry key. <p>
357 * Has <b> OK </b> status if returned value is true. <p>
359 public void _isValid() {
360 boolean valid = true;
362 openReg(oObj, openF, false, true);
363 valid = oObj.isValid();
364 closeReg(oObj);
366 tRes.tested("isValid()", valid);
370 * Method calls <code>close()</code> of the interface
371 * <code>com.sun.star.registry.XRegistryKey</code>. <p>
372 * @param reg interface <code>com.sun.star.registry.XRegistryKey</code>
373 * @param url specifies the complete URL to access the data source
374 * @param arg1 specifies if the data source should be opened for read only
375 * @param arg2 specifies if the data source should be created if it does not
376 * already exist
378 public void openReg(XSimpleRegistry reg, String url, boolean arg1,
379 boolean arg2) {
380 if (nr == null) {
381 try {
382 reg.open(url, arg1, arg2);
383 } catch (com.sun.star.registry.InvalidRegistryException e) {
384 log.print("Couldn't open registry:");
385 e.printStackTrace(log);
391 * Method calls <code>close()</code> of the interface
392 * <code>com.sun.star.registry.XRegistryKey</code>. <p>
393 * @param reg <code>com.sun.star.registry.XRegistryKey</code>
395 public void closeReg(XSimpleRegistry reg) {
396 if (nr == null) {
397 try {
398 reg.close();
399 } catch (com.sun.star.registry.InvalidRegistryException e) {
400 log.print("Couldn't close registry:");
401 e.printStackTrace(log);