xref: /AOO41X/main/qadevOOo/tests/java/ifc/sdbc/_XResultSetUpdate.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package ifc.sdbc;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import lib.MultiMethodTest;
31*cdf0e10cSrcweir import lib.StatusException;
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir import com.sun.star.sdbc.SQLException;
34*cdf0e10cSrcweir import com.sun.star.sdbc.XResultSetUpdate;
35*cdf0e10cSrcweir import com.sun.star.sdbc.XRowUpdate;
36*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir /**
39*cdf0e10cSrcweir /**
40*cdf0e10cSrcweir * Testing <code>com.sun.star.sdbc.XResultSetUpdate</code>
41*cdf0e10cSrcweir * interface methods :
42*cdf0e10cSrcweir * <ul>
43*cdf0e10cSrcweir *  <li><code> insertRow()</code></li>
44*cdf0e10cSrcweir *  <li><code> updateRow()</code></li>
45*cdf0e10cSrcweir *  <li><code> deleteRow()</code></li>
46*cdf0e10cSrcweir *  <li><code> cancelRowUpdates()</code></li>
47*cdf0e10cSrcweir *  <li><code> moveToInsertRow()</code></li>
48*cdf0e10cSrcweir *  <li><code> moveToCurrentRow()</code></li>
49*cdf0e10cSrcweir * </ul> <p>
50*cdf0e10cSrcweir * The test requires the following object relations :
51*cdf0e10cSrcweir * <ul>
52*cdf0e10cSrcweir *  <li><code>'XResultSetUpdate.UpdateTester'</code>
53*cdf0e10cSrcweir *   inner <code>UpdateTester</code> interface implementation :
54*cdf0e10cSrcweir *   is used for checking test results. See interface
55*cdf0e10cSrcweir *   documentation for more information.</li>
56*cdf0e10cSrcweir * </ul>
57*cdf0e10cSrcweir * The test is <b>not designed</b> for multithreaded testing. <p>
58*cdf0e10cSrcweir * After it's execution environment must be recreated.
59*cdf0e10cSrcweir * @see com.sun.star.sdbc.XResultSetUpdate
60*cdf0e10cSrcweir */
61*cdf0e10cSrcweir public class _XResultSetUpdate extends MultiMethodTest {
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     // oObj filled by MultiMethodTest
64*cdf0e10cSrcweir     public XResultSetUpdate oObj = null ;
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir     private UpdateTester tester = null ;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     /**
69*cdf0e10cSrcweir     * Interface contains some methods for checking
70*cdf0e10cSrcweir     * test results. It's implementation must be passed
71*cdf0e10cSrcweir     * to this test.
72*cdf0e10cSrcweir     */
73*cdf0e10cSrcweir     public static interface UpdateTester {
74*cdf0e10cSrcweir         /**
75*cdf0e10cSrcweir         * @return Current number of rows.
76*cdf0e10cSrcweir         */
77*cdf0e10cSrcweir         public int rowCount() throws SQLException ;
78*cdf0e10cSrcweir         /**
79*cdf0e10cSrcweir         * Updates some data in the current row but doesn't commit
80*cdf0e10cSrcweir         * changes to the source.
81*cdf0e10cSrcweir         */
82*cdf0e10cSrcweir         public void update() throws SQLException ;
83*cdf0e10cSrcweir         /**
84*cdf0e10cSrcweir         * Checks if updates made by method <code>update</code> was
85*cdf0e10cSrcweir         * commited to the data source.
86*cdf0e10cSrcweir         */
87*cdf0e10cSrcweir         public boolean wasUpdated() throws SQLException ;
88*cdf0e10cSrcweir         /**
89*cdf0e10cSrcweir         * Returns current row number. Really it must returns value
90*cdf0e10cSrcweir         * < 1 if the current position is on insert row.
91*cdf0e10cSrcweir         */
92*cdf0e10cSrcweir         public int currentRow() throws SQLException ;
93*cdf0e10cSrcweir     }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     /**
96*cdf0e10cSrcweir     * Retrieves relation.
97*cdf0e10cSrcweir     * @throw StatusException If relation not found.
98*cdf0e10cSrcweir     */
99*cdf0e10cSrcweir     public void before() throws StatusException {
100*cdf0e10cSrcweir         tester = (UpdateTester)tEnv.getObjRelation
101*cdf0e10cSrcweir             ("XResultSetUpdate.UpdateTester") ;
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir         if (tester == null) {
104*cdf0e10cSrcweir             log.println("Required relation not found !!!") ;
105*cdf0e10cSrcweir             throw new StatusException("Required relation not found !!!",
106*cdf0e10cSrcweir                 new NullPointerException()) ;
107*cdf0e10cSrcweir         }
108*cdf0e10cSrcweir     }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir     /**
111*cdf0e10cSrcweir     * Calls method when the cursor position is on existing row.
112*cdf0e10cSrcweir     * Checks total number of rows before and after method call. <p>
113*cdf0e10cSrcweir     * Executes <code>moveToCurrentRow</code> method test before to
114*cdf0e10cSrcweir     * be sure that cursor is not on the insert row. <p>
115*cdf0e10cSrcweir     * Has OK status if after method execution number of rows decreased
116*cdf0e10cSrcweir     * by one.
117*cdf0e10cSrcweir     */
118*cdf0e10cSrcweir     public void _deleteRow() {
119*cdf0e10cSrcweir         executeMethod("moveToCurrentRow()") ;
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir         //temporary to avoid SOffice hanging
122*cdf0e10cSrcweir         executeMethod("updateRow()") ;
123*cdf0e10cSrcweir         executeMethod("cancelRowUpdates()") ;
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir         boolean result = true ;
126*cdf0e10cSrcweir         try {
127*cdf0e10cSrcweir             int rowsBefore = tester.rowCount() ;
128*cdf0e10cSrcweir             oObj.deleteRow() ;
129*cdf0e10cSrcweir             int rowsAfter = tester.rowCount() ;
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir             result = rowsBefore == rowsAfter + 1 ;
132*cdf0e10cSrcweir         } catch (SQLException e) {
133*cdf0e10cSrcweir             e.printStackTrace(log) ;
134*cdf0e10cSrcweir             result = false ;
135*cdf0e10cSrcweir         }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir         tRes.tested("deleteRow()", result) ;
138*cdf0e10cSrcweir     }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir     /**
141*cdf0e10cSrcweir     * Using relation methods first updates some data in the current
142*cdf0e10cSrcweir     * row, then calls <code>updateRow</code> method to commit data.
143*cdf0e10cSrcweir     * Then checks if the data changed was commited. <p>
144*cdf0e10cSrcweir     * Executes <code>moveToCurrentRow</code> method test before to
145*cdf0e10cSrcweir     * be sure that cursor is not on the insert row. <p>
146*cdf0e10cSrcweir     * Has OK status if data in the source was changed.
147*cdf0e10cSrcweir     */
148*cdf0e10cSrcweir     public void _updateRow() {
149*cdf0e10cSrcweir         executeMethod("moveToCurrentRow()") ;
150*cdf0e10cSrcweir         boolean result = true ;
151*cdf0e10cSrcweir         try {
152*cdf0e10cSrcweir             tester.update() ;
153*cdf0e10cSrcweir             oObj.updateRow() ;
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir             result = tester.wasUpdated() ;
156*cdf0e10cSrcweir         } catch (SQLException e) {
157*cdf0e10cSrcweir             e.printStackTrace(log) ;
158*cdf0e10cSrcweir             result = false ;
159*cdf0e10cSrcweir         }
160*cdf0e10cSrcweir         tRes.tested("updateRow()", result) ;
161*cdf0e10cSrcweir     }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     /**
164*cdf0e10cSrcweir     * Using relation methods first updates some data in the current
165*cdf0e10cSrcweir     * row, then calls <code>cancelRowUpdates</code> method.
166*cdf0e10cSrcweir     * Then checks if the data changed was not commited. <p>
167*cdf0e10cSrcweir     * Executes <code>moveToCurrentRow</code> method test before to
168*cdf0e10cSrcweir     * be sure that cursor is not on the insert row. <p>
169*cdf0e10cSrcweir     * Has OK status if data in the source was not changed.
170*cdf0e10cSrcweir     */
171*cdf0e10cSrcweir     public void _cancelRowUpdates() {
172*cdf0e10cSrcweir         executeMethod("moveToCurrentRow()") ;
173*cdf0e10cSrcweir         boolean result = true ;
174*cdf0e10cSrcweir         try {
175*cdf0e10cSrcweir             tester.update() ;
176*cdf0e10cSrcweir             oObj.cancelRowUpdates() ;
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir             result = !tester.wasUpdated() ;
179*cdf0e10cSrcweir         } catch (SQLException e) {
180*cdf0e10cSrcweir             e.printStackTrace(log) ;
181*cdf0e10cSrcweir             result = false ;
182*cdf0e10cSrcweir         }
183*cdf0e10cSrcweir         tRes.tested("cancelRowUpdates()", result) ;
184*cdf0e10cSrcweir     }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir     /**
187*cdf0e10cSrcweir     * Tries to move cursor to insert row. Then checks current row
188*cdf0e10cSrcweir     * number. It must be less than 1. (0 as I know) <p>
189*cdf0e10cSrcweir     */
190*cdf0e10cSrcweir     public void _moveToInsertRow() {
191*cdf0e10cSrcweir         boolean result = true ;
192*cdf0e10cSrcweir         try {
193*cdf0e10cSrcweir             oObj.moveToInsertRow() ;
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir             result = tester.currentRow() < 1 ;
196*cdf0e10cSrcweir         } catch (SQLException e) {
197*cdf0e10cSrcweir             e.printStackTrace(log) ;
198*cdf0e10cSrcweir             result = false ;
199*cdf0e10cSrcweir         }
200*cdf0e10cSrcweir         tRes.tested("moveToInsertRow()", result) ;
201*cdf0e10cSrcweir     }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir     /**
204*cdf0e10cSrcweir     * Returns cursor from insert row back to previous row. <p>
205*cdf0e10cSrcweir     * <code>moveToInsertRow</code> method test must be executed
206*cdf0e10cSrcweir     * first for positioning curosr to insert row. <p>
207*cdf0e10cSrcweir     * Has OK status if after method call current row number is
208*cdf0e10cSrcweir     * above 0.
209*cdf0e10cSrcweir     */
210*cdf0e10cSrcweir     public void _moveToCurrentRow() {
211*cdf0e10cSrcweir         boolean result = true ;
212*cdf0e10cSrcweir         try {
213*cdf0e10cSrcweir             oObj.moveToCurrentRow() ;
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir             result = tester.currentRow() >= 1 ;
216*cdf0e10cSrcweir         } catch (SQLException e) {
217*cdf0e10cSrcweir             e.printStackTrace(log) ;
218*cdf0e10cSrcweir             result = false ;
219*cdf0e10cSrcweir         }
220*cdf0e10cSrcweir         tRes.tested("moveToCurrentRow()", result) ;
221*cdf0e10cSrcweir     }
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir     /**
224*cdf0e10cSrcweir     * Moves cursor to the insert row, then calls the method
225*cdf0e10cSrcweir     * <code>insertRow</code>. Before and after call stores
226*cdf0e10cSrcweir     * total number of rows. <p>
227*cdf0e10cSrcweir     * Has OK status if after method call rows number increases
228*cdf0e10cSrcweir     * by one.
229*cdf0e10cSrcweir     */
230*cdf0e10cSrcweir     public void _insertRow() {
231*cdf0e10cSrcweir         executeMethod("moveToInsertRow()") ;
232*cdf0e10cSrcweir         boolean result = true ;
233*cdf0e10cSrcweir         try {
234*cdf0e10cSrcweir             oObj.moveToCurrentRow();
235*cdf0e10cSrcweir             int rowsBefore = tester.rowCount() ;
236*cdf0e10cSrcweir             oObj.moveToInsertRow() ;
237*cdf0e10cSrcweir             XRowUpdate rowU = (XRowUpdate)
238*cdf0e10cSrcweir                             UnoRuntime.queryInterface(XRowUpdate.class, oObj);
239*cdf0e10cSrcweir             rowU.updateString(1,"open");
240*cdf0e10cSrcweir             rowU.updateInt(2,5);
241*cdf0e10cSrcweir             rowU.updateDouble(5,3.4);
242*cdf0e10cSrcweir             rowU.updateBoolean(10,true);
243*cdf0e10cSrcweir             oObj.insertRow() ;
244*cdf0e10cSrcweir             oObj.moveToCurrentRow();
245*cdf0e10cSrcweir             int rowsAfter = tester.rowCount() ;
246*cdf0e10cSrcweir             result = rowsBefore + 1 == rowsAfter ;
247*cdf0e10cSrcweir         } catch (SQLException e) {
248*cdf0e10cSrcweir             e.printStackTrace(log) ;
249*cdf0e10cSrcweir             log.println("******"+e.getMessage());
250*cdf0e10cSrcweir             result = false ;
251*cdf0e10cSrcweir         }
252*cdf0e10cSrcweir         tRes.tested("insertRow()", result) ;
253*cdf0e10cSrcweir     }
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir     /**
256*cdf0e10cSrcweir     * Forces environment to be recreated.
257*cdf0e10cSrcweir     */
258*cdf0e10cSrcweir     public void after() {
259*cdf0e10cSrcweir         //disposeEnvironment() ;
260*cdf0e10cSrcweir     }
261*cdf0e10cSrcweir }  // finish class _XResultSetUpdate
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir 
264