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 lib.MultiMethodTest; 31 import lib.StatusException; 32 33 import com.sun.star.sdbc.SQLException; 34 import com.sun.star.sdbc.XResultSetUpdate; 35 import com.sun.star.sdbc.XRowUpdate; 36 import com.sun.star.uno.UnoRuntime; 37 38 /** 39 /** 40 * Testing <code>com.sun.star.sdbc.XResultSetUpdate</code> 41 * interface methods : 42 * <ul> 43 * <li><code> insertRow()</code></li> 44 * <li><code> updateRow()</code></li> 45 * <li><code> deleteRow()</code></li> 46 * <li><code> cancelRowUpdates()</code></li> 47 * <li><code> moveToInsertRow()</code></li> 48 * <li><code> moveToCurrentRow()</code></li> 49 * </ul> <p> 50 * The test requires the following object relations : 51 * <ul> 52 * <li><code>'XResultSetUpdate.UpdateTester'</code> 53 * inner <code>UpdateTester</code> interface implementation : 54 * is used for checking test results. See interface 55 * documentation for more information.</li> 56 * </ul> 57 * The test is <b>not designed</b> for multithreaded testing. <p> 58 * After it's execution environment must be recreated. 59 * @see com.sun.star.sdbc.XResultSetUpdate 60 */ 61 public class _XResultSetUpdate extends MultiMethodTest { 62 63 // oObj filled by MultiMethodTest 64 public XResultSetUpdate oObj = null ; 65 66 private UpdateTester tester = null ; 67 68 /** 69 * Interface contains some methods for checking 70 * test results. It's implementation must be passed 71 * to this test. 72 */ 73 public static interface UpdateTester { 74 /** 75 * @return Current number of rows. 76 */ 77 public int rowCount() throws SQLException ; 78 /** 79 * Updates some data in the current row but doesn't commit 80 * changes to the source. 81 */ 82 public void update() throws SQLException ; 83 /** 84 * Checks if updates made by method <code>update</code> was 85 * commited to the data source. 86 */ 87 public boolean wasUpdated() throws SQLException ; 88 /** 89 * Returns current row number. Really it must returns value 90 * < 1 if the current position is on insert row. 91 */ 92 public int currentRow() throws SQLException ; 93 } 94 95 /** 96 * Retrieves relation. 97 * @throw StatusException If relation not found. 98 */ 99 public void before() throws StatusException { 100 tester = (UpdateTester)tEnv.getObjRelation 101 ("XResultSetUpdate.UpdateTester") ; 102 103 if (tester == null) { 104 log.println("Required relation not found !!!") ; 105 throw new StatusException("Required relation not found !!!", 106 new NullPointerException()) ; 107 } 108 } 109 110 /** 111 * Calls method when the cursor position is on existing row. 112 * Checks total number of rows before and after method call. <p> 113 * Executes <code>moveToCurrentRow</code> method test before to 114 * be sure that cursor is not on the insert row. <p> 115 * Has OK status if after method execution number of rows decreased 116 * by one. 117 */ 118 public void _deleteRow() { 119 executeMethod("moveToCurrentRow()") ; 120 121 //temporary to avoid SOffice hanging 122 executeMethod("updateRow()") ; 123 executeMethod("cancelRowUpdates()") ; 124 125 boolean result = true ; 126 try { 127 int rowsBefore = tester.rowCount() ; 128 oObj.deleteRow() ; 129 int rowsAfter = tester.rowCount() ; 130 131 result = rowsBefore == rowsAfter + 1 ; 132 } catch (SQLException e) { 133 e.printStackTrace(log) ; 134 result = false ; 135 } 136 137 tRes.tested("deleteRow()", result) ; 138 } 139 140 /** 141 * Using relation methods first updates some data in the current 142 * row, then calls <code>updateRow</code> method to commit data. 143 * Then checks if the data changed was commited. <p> 144 * Executes <code>moveToCurrentRow</code> method test before to 145 * be sure that cursor is not on the insert row. <p> 146 * Has OK status if data in the source was changed. 147 */ 148 public void _updateRow() { 149 executeMethod("moveToCurrentRow()") ; 150 boolean result = true ; 151 try { 152 tester.update() ; 153 oObj.updateRow() ; 154 155 result = tester.wasUpdated() ; 156 } catch (SQLException e) { 157 e.printStackTrace(log) ; 158 result = false ; 159 } 160 tRes.tested("updateRow()", result) ; 161 } 162 163 /** 164 * Using relation methods first updates some data in the current 165 * row, then calls <code>cancelRowUpdates</code> method. 166 * Then checks if the data changed was not commited. <p> 167 * Executes <code>moveToCurrentRow</code> method test before to 168 * be sure that cursor is not on the insert row. <p> 169 * Has OK status if data in the source was not changed. 170 */ 171 public void _cancelRowUpdates() { 172 executeMethod("moveToCurrentRow()") ; 173 boolean result = true ; 174 try { 175 tester.update() ; 176 oObj.cancelRowUpdates() ; 177 178 result = !tester.wasUpdated() ; 179 } catch (SQLException e) { 180 e.printStackTrace(log) ; 181 result = false ; 182 } 183 tRes.tested("cancelRowUpdates()", result) ; 184 } 185 186 /** 187 * Tries to move cursor to insert row. Then checks current row 188 * number. It must be less than 1. (0 as I know) <p> 189 */ 190 public void _moveToInsertRow() { 191 boolean result = true ; 192 try { 193 oObj.moveToInsertRow() ; 194 195 result = tester.currentRow() < 1 ; 196 } catch (SQLException e) { 197 e.printStackTrace(log) ; 198 result = false ; 199 } 200 tRes.tested("moveToInsertRow()", result) ; 201 } 202 203 /** 204 * Returns cursor from insert row back to previous row. <p> 205 * <code>moveToInsertRow</code> method test must be executed 206 * first for positioning curosr to insert row. <p> 207 * Has OK status if after method call current row number is 208 * above 0. 209 */ 210 public void _moveToCurrentRow() { 211 boolean result = true ; 212 try { 213 oObj.moveToCurrentRow() ; 214 215 result = tester.currentRow() >= 1 ; 216 } catch (SQLException e) { 217 e.printStackTrace(log) ; 218 result = false ; 219 } 220 tRes.tested("moveToCurrentRow()", result) ; 221 } 222 223 /** 224 * Moves cursor to the insert row, then calls the method 225 * <code>insertRow</code>. Before and after call stores 226 * total number of rows. <p> 227 * Has OK status if after method call rows number increases 228 * by one. 229 */ 230 public void _insertRow() { 231 executeMethod("moveToInsertRow()") ; 232 boolean result = true ; 233 try { 234 oObj.moveToCurrentRow(); 235 int rowsBefore = tester.rowCount() ; 236 oObj.moveToInsertRow() ; 237 XRowUpdate rowU = (XRowUpdate) 238 UnoRuntime.queryInterface(XRowUpdate.class, oObj); 239 rowU.updateString(1,"open"); 240 rowU.updateInt(2,5); 241 rowU.updateDouble(5,3.4); 242 rowU.updateBoolean(10,true); 243 oObj.insertRow() ; 244 oObj.moveToCurrentRow(); 245 int rowsAfter = tester.rowCount() ; 246 result = rowsBefore + 1 == rowsAfter ; 247 } catch (SQLException e) { 248 e.printStackTrace(log) ; 249 log.println("******"+e.getMessage()); 250 result = false ; 251 } 252 tRes.tested("insertRow()", result) ; 253 } 254 255 /** 256 * Forces environment to be recreated. 257 */ 258 public void after() { 259 //disposeEnvironment() ; 260 } 261 } // finish class _XResultSetUpdate 262 263 264