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