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.sdbc; 29 30 import java.util.Vector; 31 32 import lib.MultiMethodTest; 33 import lib.Status; 34 35 import com.sun.star.io.XDataInputStream; 36 import com.sun.star.io.XInputStream; 37 import com.sun.star.io.XTextInputStream; 38 import com.sun.star.lang.XMultiServiceFactory; 39 import com.sun.star.sdbc.DataType; 40 import com.sun.star.sdbc.SQLException; 41 import com.sun.star.sdbc.XParameters; 42 import com.sun.star.uno.UnoRuntime; 43 import com.sun.star.util.Date; 44 import com.sun.star.util.DateTime; 45 import com.sun.star.util.Time; 46 47 /** 48 /** 49 * Testing <code>com.sun.star.sdbc.XParameters</code> 50 * interface methods : 51 * <ul> 52 * <li><code> setNull()</code></li> 53 * <li><code> setObjectNull()</code></li> 54 * <li><code> setBoolean()</code></li> 55 * <li><code> setByte()</code></li> 56 * <li><code> setShort()</code></li> 57 * <li><code> setInt()</code></li> 58 * <li><code> setLong()</code></li> 59 * <li><code> setFloat()</code></li> 60 * <li><code> setDouble()</code></li> 61 * <li><code> setString()</code></li> 62 * <li><code> setBytes()</code></li> 63 * <li><code> setDate()</code></li> 64 * <li><code> setTime()</code></li> 65 * <li><code> setTimestamp()</code></li> 66 * <li><code> setBinaryStream()</code></li> 67 * <li><code> setCharacterStream()</code></li> 68 * <li><code> setObject()</code></li> 69 * <li><code> setObjectWithInfo()</code></li> 70 * <li><code> setRef()</code></li> 71 * <li><code> setBlob()</code></li> 72 * <li><code> setClob()</code></li> 73 * <li><code> setArray()</code></li> 74 * <li><code> clearParameters()</code></li> 75 * </ul> <p> 76 * Object relations required : 77 * <ul> 78 * <li> <code>'XParameters.ParamValues'</code> : is a 79 * <code>java.util.Vector</code> object 80 * that contains parameter types and values of the statement. Each 81 * element of vector corresponds to appropriate parameter (element 82 * with index 0 to parameter #1, 1 -> #2, etc.). <p> 83 * The following <code>XParameters</code> methods correspond to classes 84 * in Vector : 85 * <ul> 86 * <li> <code>setBinaryStream</code> - 87 * <code>com.sun.star.io.XDataInputStream</code> class. </li> 88 * <li> <code>setCharacterStream</code> - 89 * <code>com.sun.star.io.XTextInputStream</code> class. </li> 90 * <li> <code>setObject</code> - 91 * <code>java.lang.Object[]</code> class, the element with 92 * index 0 must be used. </li> 93 * </ul> 94 * Other methods uses types of their arguments (i.e. 95 * <code>java.lang.String</code> 96 * for <code>setString</code> method, <code>com.sun.star.sdbc.XRef</code> 97 * for <code>setRef</code> method). 98 * </li> 99 * </ul> 100 * @see com.sun.star.sdbc.XParameters 101 */ 102 public class _XParameters extends MultiMethodTest { 103 104 // oObj filled by MultiMethodTest 105 public XParameters oObj = null ; 106 107 private Vector data = null ; 108 109 /** 110 * Gets object relation 111 */ 112 public void before() { 113 data = (Vector) tEnv.getObjRelation("XParameters.ParamValues") ; 114 if (data == null) { 115 log.println("!!! Relation not found !!!") ; 116 } 117 } 118 119 /** 120 * Sets String parameter (if exists) to SQL NULL value. <p> 121 * Has OK status if no exceptions occured. 122 */ 123 public void _setNull() { 124 boolean result = true ; 125 int idx = findParamOfType(String.class) ; 126 if (idx < 0) log.println("Type not found in relation: not tested"); 127 else { 128 try { 129 oObj.setNull(idx, DataType.VARCHAR) ; 130 } catch (SQLException e) { 131 log.println("Unexpected SQL exception:") ; 132 log.println(e) ; 133 result = false ; 134 } 135 } 136 137 tRes.tested("setNull()", result) ; 138 } 139 140 public void _setObjectNull() { 141 /* 142 !!! TO DO !!! 143 */ 144 tRes.tested("setObjectNull()", Status.skipped(true)) ; 145 } 146 147 /** 148 * Sets String parameter (if exists) to new value. <p> 149 * Has OK status if no exceptions occured. 150 */ 151 public void _setString() { 152 boolean result = true ; 153 int idx = findParamOfType(String.class) ; 154 if (idx < 0) log.println("Type not found in relation: not tested"); 155 else { 156 try { 157 oObj.setString(idx, "XParameters") ; 158 } catch (SQLException e) { 159 log.println("Unexpected SQL exception:") ; 160 log.println(e) ; 161 result = false ; 162 } 163 } 164 165 tRes.tested("setString()", result) ; 166 } 167 168 /** 169 * Sets parameter (if exists) to new value. <p> 170 * Has OK status if no exceptions occured. 171 */ 172 public void _setBoolean() { 173 boolean result = true ; 174 int idx = findParamOfType(Boolean.class) ; 175 if (idx < 0) log.println("Type not found in relation: not tested"); 176 else { 177 try { 178 oObj.setBoolean(idx, true) ; 179 } catch (SQLException e) { 180 log.println("Unexpected SQL exception:") ; 181 log.println(e) ; 182 result = false ; 183 } 184 } 185 186 tRes.tested("setBoolean()", result) ; 187 } 188 189 /** 190 * Sets parameter (if exists) to new value. <p> 191 * Has OK status if no exceptions occured. 192 */ 193 public void _setByte() { 194 boolean result = true ; 195 int idx = findParamOfType(Byte.class) ; 196 if (idx < 0) log.println("Type not found in relation: not tested"); 197 else { 198 try { 199 oObj.setByte(idx, (byte)122) ; 200 } catch (SQLException e) { 201 log.println("Unexpected SQL exception:") ; 202 log.println(e) ; 203 result = false ; 204 } 205 } 206 207 tRes.tested("setByte()", result) ; 208 } 209 210 /** 211 * Sets parameter (if exists) to new value. <p> 212 * Has OK status if no exceptions occured. 213 */ 214 public void _setShort() { 215 boolean result = true ; 216 int idx = findParamOfType(Short.class) ; 217 if (idx < 0) log.println("Type not found in relation: not tested"); 218 else { 219 try { 220 oObj.setShort(idx, (short)133) ; 221 } catch (SQLException e) { 222 log.println("Unexpected SQL exception:") ; 223 log.println(e) ; 224 result = false ; 225 } 226 } 227 228 tRes.tested("setShort()", result) ; 229 } 230 231 /** 232 * Sets parameter (if exists) to new value. <p> 233 * Has OK status if no exceptions occured. 234 */ 235 public void _setInt() { 236 boolean result = true ; 237 int idx = findParamOfType(Integer.class) ; 238 if (idx < 0) log.println("Type not found in relation: not tested"); 239 else { 240 try { 241 oObj.setInt(idx, 13300) ; 242 } catch (SQLException e) { 243 log.println("Unexpected SQL exception:") ; 244 log.println(e) ; 245 result = false ; 246 } 247 } 248 249 tRes.tested("setInt()", result) ; 250 } 251 252 /** 253 * Sets parameter (if exists) to new value. <p> 254 * Has OK status if no exceptions occured. 255 */ 256 public void _setLong() { 257 boolean result = true ; 258 int idx = findParamOfType(Long.class) ; 259 if (idx < 0) log.println("Type not found in relation: not tested"); 260 else { 261 try { 262 oObj.setLong(idx, 13362453) ; 263 } catch (SQLException e) { 264 log.println("Unexpected SQL exception:") ; 265 log.println(e) ; 266 result = false ; 267 } 268 } 269 270 tRes.tested("setLong()", result) ; 271 } 272 273 /** 274 * Sets parameter (if exists) to new value. <p> 275 * Has OK status if no exceptions occured. 276 */ 277 public void _setFloat() { 278 boolean result = true ; 279 int idx = findParamOfType(Float.class) ; 280 if (idx < 0) log.println("Type not found in relation: not tested"); 281 else { 282 try { 283 oObj.setFloat(idx, (float)133.55) ; 284 } catch (SQLException e) { 285 log.println("Unexpected SQL exception:") ; 286 log.println(e) ; 287 result = false ; 288 } 289 } 290 291 tRes.tested("setFloat()", result) ; 292 } 293 294 /** 295 * Sets parameter (if exists) to new value. <p> 296 * Has OK status if no exceptions occured. 297 */ 298 public void _setDouble() { 299 boolean result = true ; 300 int idx = findParamOfType(Double.class) ; 301 if (idx < 0) log.println("Type not found in relation: not tested"); 302 else { 303 try { 304 oObj.setDouble(idx, 133) ; 305 } catch (SQLException e) { 306 log.println("Unexpected SQL exception:") ; 307 log.println(e) ; 308 result = false ; 309 } 310 } 311 312 tRes.tested("setDouble()", result) ; 313 } 314 315 /** 316 * Sets parameter (if exists) to new value. <p> 317 * Has OK status if no exceptions occured. 318 */ 319 public void _setBytes() { 320 boolean result = true ; 321 int idx = findParamOfType(byte[].class) ; 322 if (idx < 0) log.println("Type not found in relation: not tested"); 323 else { 324 try { 325 oObj.setBytes(idx, new byte[] {5}) ; 326 } catch (SQLException e) { 327 log.println("Unexpected SQL exception:") ; 328 log.println(e) ; 329 result = false ; 330 } 331 } 332 333 tRes.tested("setBytes()", result) ; 334 } 335 336 /** 337 * Sets parameter (if exists) to new value. <p> 338 * Has OK status if no exceptions occured. 339 */ 340 public void _setDate() { 341 boolean result = true ; 342 int idx = findParamOfType(Date.class) ; 343 if (idx < 0) log.println("Type not found in relation: not tested"); 344 else { 345 try { 346 oObj.setDate( 347 idx, new Date ((short)19, (short)01, (short)1979)) ; 348 } catch (SQLException e) { 349 log.println("Unexpected SQL exception:") ; 350 log.println(e) ; 351 result = false ; 352 } 353 } 354 355 tRes.tested("setDate()", result) ; 356 } 357 358 /** 359 * Sets parameter (if exists) to new value. <p> 360 * Has OK status if no exceptions occured. 361 */ 362 public void _setTime() { 363 boolean result = true ; 364 int idx = findParamOfType(Time.class) ; 365 if (idx < 0) log.println("Type not found in relation: not tested"); 366 else { 367 try { 368 oObj.setTime( 369 idx, new Time((short)1,(short)2,(short)3,(short)44)) ; 370 } catch (SQLException e) { 371 log.println("Unexpected SQL exception:") ; 372 log.println(e) ; 373 result = false ; 374 } 375 } 376 377 tRes.tested("setTime()", result) ; 378 } 379 380 /** 381 * Sets parameter (if exists) to new value. <p> 382 * Has OK status if no exceptions occured. 383 */ 384 public void _setTimestamp() { 385 boolean result = true ; 386 int idx = findParamOfType(DateTime.class) ; 387 if (idx < 0) log.println("Type not found in relation: not tested"); 388 else { 389 try { 390 oObj.setTimestamp(idx, new DateTime((short)1,(short)2,(short)3, 391 (short)4, (short)19, (short)01, (short)1979)) ; 392 } catch (SQLException e) { 393 log.println("Unexpected SQL exception:") ; 394 log.println(e) ; 395 result = false ; 396 } 397 } 398 399 tRes.tested("setTimestamp()", result) ; 400 } 401 402 /** 403 * Sets parameter (if exists) to new value. <p> 404 * Has OK status if no exceptions occured. 405 */ 406 public void _setBinaryStream() { 407 boolean result = true ; 408 int idx = findParamOfType(XDataInputStream.class) ; 409 if (idx < 0) log.println("Type not found in relation: not tested"); 410 else { 411 try { 412 Object oStream = ((XMultiServiceFactory)tParam.getMSF()). 413 createInstance("com.sun.star.io.DataInputStream") ; 414 XInputStream xStream = (XInputStream)UnoRuntime.queryInterface 415 (XInputStream.class, oStream); 416 417 oObj.setBinaryStream(idx, xStream, 2) ; 418 } catch (SQLException e) { 419 log.println("Unexpected SQL exception:") ; 420 log.println(e) ; 421 result = false ; 422 } catch (com.sun.star.uno.Exception e) { 423 log.println("Unexpected exception:") ; 424 log.println(e) ; 425 result = false ; 426 } 427 } 428 429 tRes.tested("setBinaryStream()", result) ; 430 } 431 432 /** 433 * Sets parameter (if exists) to new value. <p> 434 * Has OK status if no exceptions occured. 435 */ 436 public void _setCharacterStream() { 437 boolean result = true ; 438 int idx = findParamOfType(XTextInputStream.class) ; 439 if (idx < 0) log.println("Type not found in relation: not tested"); 440 else { 441 try { 442 Object oStream = ((XMultiServiceFactory)tParam.getMSF()) 443 .createInstance("com.sun.star.io.TextInputStream") ; 444 XInputStream xStream = (XInputStream)UnoRuntime.queryInterface 445 (XInputStream.class, oStream); 446 447 oObj.setCharacterStream(idx, xStream, 2) ; 448 } catch (SQLException e) { 449 log.println("Unexpected SQL exception:") ; 450 log.println(e) ; 451 result = false ; 452 } catch (com.sun.star.uno.Exception e) { 453 log.println("Unexpected exception:") ; 454 log.println(e) ; 455 result = false ; 456 } 457 } 458 459 tRes.tested("setCharacterStream()", result) ; 460 } 461 462 /** 463 * Sets parameter (if exists) to new value. <p> 464 * Has OK status if no exceptions occured. 465 */ 466 public void _setObject() { 467 boolean result = true ; 468 int idx = findParamOfType(Object[].class) ; 469 if (idx < 0) log.println("Type not found in relation: not tested"); 470 else { 471 try { 472 Object obj = ((XMultiServiceFactory)tParam.getMSF()). 473 createInstance("com.sun.star.io.Pipe") ; 474 475 oObj.setObject(idx, obj) ; 476 } catch (SQLException e) { 477 log.println("Unexpected SQL exception:") ; 478 log.println(e) ; 479 result = false ; 480 } catch (com.sun.star.uno.Exception e) { 481 log.println("Unexpected exception:") ; 482 log.println(e) ; 483 result = false ; 484 } 485 } 486 487 tRes.tested("setObject()", result) ; 488 } 489 490 /** 491 * Sets parameter (if exists) to new value. <p> 492 * Has OK status if no exceptions occured. 493 */ 494 public void _setObjectWithInfo() { 495 boolean result = true ; 496 int idx = findParamOfType(Object[].class) ; 497 if (idx < 0) log.println("Type not found in relation: not tested"); 498 else { 499 try { 500 Object obj = ((XMultiServiceFactory)tParam.getMSF()). 501 createInstance("com.sun.star.io.Pipe") ; 502 503 oObj.setObjectWithInfo(idx, obj, DataType.OBJECT, 0) ; 504 } catch (SQLException e) { 505 log.println("Unexpected SQL exception:") ; 506 log.println(e) ; 507 result = false ; 508 } catch (com.sun.star.uno.Exception e) { 509 log.println("Unexpected exception:") ; 510 log.println(e) ; 511 result = false ; 512 } 513 } 514 515 tRes.tested("setObjectWithInfo()", result) ; 516 } 517 518 public void _setRef() { 519 /* 520 !!! TO DO !!! 521 */ 522 tRes.tested("setRef()", Status.skipped(true)) ; 523 } 524 public void _setBlob() { 525 /* 526 !!! TO DO !!! 527 */ 528 tRes.tested("setBlob()", Status.skipped(true)) ; 529 } 530 public void _setClob() { 531 /* 532 !!! TO DO !!! 533 */ 534 tRes.tested("setClob()", Status.skipped(true)) ; 535 } 536 public void _setArray() { 537 /* 538 !!! TO DO !!! 539 */ 540 tRes.tested("setArray()", Status.skipped(true)) ; 541 } 542 543 /** 544 * Calls method. <p> 545 * Has OK status if no exceptions occured. 546 */ 547 public void _clearParameters() { 548 boolean result = true ; 549 try { 550 oObj.clearParameters() ; 551 } catch (SQLException e) { 552 log.println("Unexpected SQL exception:") ; 553 log.println(e) ; 554 result = false ; 555 } 556 557 tRes.tested("clearParameters()", result) ; 558 } 559 560 561 /** 562 * Finds in relation vector index of parameter of the appropriate 563 * type. 564 */ 565 private int findParamOfType(Class clz) { 566 567 for (int i = 0; i < data.size(); i++) 568 if (clz.isInstance(data.get(i))) return i + 1 ; 569 return -1 ; 570 } 571 572 } // finish class _XParameters 573 574 575