KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > persist > db > driver > UpdatableResultSetTest


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: UpdatableResultSetTest.java,v 1.7 2007/01/07 06:14:51 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.persist.db.driver;
23
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 import junit.extensions.TestSetup;
30 import junit.framework.Test;
31 import junit.framework.TestSuite;
32
33 import org.opensubsystems.core.error.OSSException;
34 import org.opensubsystems.core.persist.db.Database;
35 import org.opensubsystems.core.persist.db.DatabaseImpl;
36 import org.opensubsystems.core.persist.db.DatabaseTest;
37 import org.opensubsystems.core.persist.db.DatabaseTestSetup;
38 import org.opensubsystems.core.persist.db.DatabaseTestSuite;
39 import org.opensubsystems.core.util.DatabaseUtils;
40 import org.opensubsystems.core.util.Log;
41
42 /**
43  * All tests related to result set functionality. Mainly it tests if result set
44  * is updatable.
45  *
46  * @version $Id: UpdatableResultSetTest.java,v 1.7 2007/01/07 06:14:51 bastafidli Exp $
47  * @author Miro Halas
48  * @code.reviewer Miro Halas
49  * @code.reviewed Initial revision
50  */

51 public final class UpdatableResultSetTest
52 {
53    // Constructors /////////////////////////////////////////////////////////////
54

55    /**
56     * Private constructor since this class cannot be instantiated
57     */

58    private UpdatableResultSetTest(
59    )
60    {
61       // Do nothing
62
}
63    
64    // Public methods ///////////////////////////////////////////////////////////
65

66    /**
67     * Create the suite for this test since this is the only way how to create
68     * test setup which can initialize and shutdown the database for us
69     *
70     * @return Test - suite of tests to run for this database
71     */

72    public static Test suite(
73    )
74    {
75       TestSuite suite = new DatabaseTestSuite("UpdatableResultSetTest");
76       suite.addTestSuite(UpdatableResultSetTestInternal.class);
77       TestSetup wrapper = new DatabaseTestSetup(suite);
78
79       return wrapper;
80    }
81
82    /**
83     * Internal class which can be included in other test suites directly without
84     * including the above suite. This allows us to group multiple tests
85     * together and the execute the DatabaseTestSetup only once
86     */

87    public static class UpdatableResultSetTestInternal extends DatabaseTest
88    {
89       // Cached values ////////////////////////////////////////////////////////////
90

91       /**
92        * Logger for this class
93        */

94       private static Logger JavaDoc s_logger = Log.getInstance(UpdatableResultSetTestInternal.class);
95    
96       /**
97        * Static initializer
98        */

99       static
100       {
101          // This test use special database schema so make the database aware of it
102
Database dbDatabase;
103    
104          try
105          {
106             dbDatabase = DatabaseImpl.getInstance();
107             // Add schema database tests needs to the database
108
dbDatabase.add(DatabaseTestSchema.class);
109          }
110          catch (OSSException bfeExc)
111          {
112             throw new RuntimeException JavaDoc("Unexpected exception.", bfeExc);
113          }
114       }
115    
116       /**
117        * Create new result set test.
118        *
119        * @param strTestName - name of the test
120        */

121       public UpdatableResultSetTestInternal(
122          String JavaDoc strTestName
123       )
124       {
125          super(strTestName);
126       }
127       
128       /**
129        * Test if the database driver support updatable result set.
130        *
131        * Uses the already setup connection and transaction.
132        * No need to close the connection since base class is doing it for us.
133        *
134        * @throws Throwable - an error has occured during test
135        */

136       public void testDeleteRow(
137       ) throws Throwable JavaDoc
138       {
139          final String JavaDoc INSERT_VALUE = "insert into RESULTSET_TEST(RESULTSET_TEST) values (?)";
140          // See OracleTests class why we need to select tablename.*
141
final String JavaDoc SELECT_VALUE
142                           = "select RESULTSET_TEST.* from RESULTSET_TEST where RESULTSET_TEST = ?";
143          final String JavaDoc DELETE_VALUE = "delete from RESULTSET_TEST where RESULTSET_TEST = ?";
144          final String JavaDoc VALUE_TEST = "test value";
145           
146          PreparedStatement JavaDoc insertStatement;
147          int iUpdateCount;
148          
149          m_transaction.begin();
150    
151          try
152          {
153             insertStatement = m_connection.prepareStatement(INSERT_VALUE);
154             insertStatement.setString(1, VALUE_TEST);
155    
156             iUpdateCount = DatabaseUtils.executeUpdateAndClose(insertStatement);
157    
158             m_transaction.commit();
159          }
160          catch (Throwable JavaDoc throwable)
161          {
162             m_transaction.rollback();
163             throw throwable;
164          }
165          
166          assertEquals("Exactly one record have been inserted.",
167                              iUpdateCount, 1);
168          
169          // Now select it back to be sure it is there so we cna delete it
170
PreparedStatement JavaDoc selectStatement = null;
171          PreparedStatement JavaDoc deleteStatement = null;
172          
173          ResultSet JavaDoc results = null;
174          int iDeletedCount = 0;
175          boolean bUpdatableResultSet = true;
176    
177          boolean bHasMoreThanOne;
178          String JavaDoc retrievedValue;
179          
180          m_transaction.begin();
181    
182          try
183          {
184             SQLException JavaDoc sqleThrownExc = null;
185             
186             try
187             {
188                selectStatement = m_connection.prepareStatement(
189                                     SELECT_VALUE,
190                                     ResultSet.TYPE_SCROLL_SENSITIVE,
191                                     ResultSet.CONCUR_UPDATABLE);
192             }
193             catch (SQLException JavaDoc sqleExc)
194             {
195                // Maybe it doesn't support updatable results set (e.g HSQLDB)
196
// so try to work around it
197
bUpdatableResultSet = false;
198                sqleThrownExc = sqleExc;
199                selectStatement = m_connection.prepareStatement(SELECT_VALUE);
200                // DOn't catch any exception here, if this doesn't work, something
201
// is really wrong
202
}
203             selectStatement.setString(1, VALUE_TEST);
204             results = selectStatement.executeQuery();
205             
206             assertTrue("The inserted record is not in the database.",
207                               results.next());
208             retrievedValue = results.getString(1);
209             
210             // Delete the inserted row, this method shouldn't cause exception
211
if (bUpdatableResultSet)
212             {
213                // This is the optimal way to do it, but not every database driver supports
214
// updatable ResultSet (e.g. HSQLDB, ResultSetUnitTest tests for this)
215
// so provide an alternative
216
try
217                {
218                   results.deleteRow();
219                   iDeletedCount = 1;
220                }
221                catch (SQLException JavaDoc sqleExc)
222                {
223                   // Maybe it doesn't support updatable results set (e.g Firebird)
224
// so try to work around it
225
bUpdatableResultSet = false;
226                   sqleThrownExc = sqleExc;
227                }
228             }
229             if (!bUpdatableResultSet)
230             {
231                // I really don't want to fail this test, just to detect that
232
// this database driver still doesn't support the updatable result set
233
s_logger.warning("Database driver " + getDataSourceName()
234                                          + " still doesn't support updatable resultset: "
235                                          + sqleThrownExc);
236                
237                deleteStatement = m_connection.prepareStatement(DELETE_VALUE);
238                deleteStatement.setString(1, VALUE_TEST);
239                iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement);
240             }
241             // Get the assert check without throwing exception so we can correctly
242
// cleanup
243
bHasMoreThanOne = results.next();
244             m_transaction.commit();
245          }
246          catch (Throwable JavaDoc throwable)
247          {
248             m_transaction.rollback();
249             throw throwable;
250          }
251          finally
252          {
253             DatabaseUtils.closeResultSetAndStatement(results, selectStatement);
254             
255             m_transaction.begin();
256             try
257             {
258                deleteStatement = m_connection.prepareStatement(
259                         "delete from RESULTSET_TEST");
260                deleteStatement.execute();
261                m_transaction.commit();
262             }
263             catch (Exception JavaDoc eExc)
264             {
265                m_transaction.rollback();
266                throw eExc;
267             }
268             finally
269             {
270                DatabaseUtils.closeStatement(deleteStatement);
271             }
272          }
273          
274          assertFalse("There should be only one inserted record in the database.",
275                             bHasMoreThanOne);
276       
277          assertEquals("Exactly one record with date data shoud have been deleted.",
278                              iDeletedCount, 1);
279    
280          // And now test the value
281
assertNotNull("The inserted value shouldn't be retrieved as null" +
282                               " from the database",
283                               retrievedValue);
284          assertEquals("The value retrieved from database "
285                              + " is not the same as the inserted one ",
286                              VALUE_TEST, retrievedValue);
287    
288          
289       }
290    }
291 }
292
Popular Tags