KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: PreparedStatementTest.java,v 1.8 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.util.logging.Logger JavaDoc;
27
28 import junit.extensions.TestSetup;
29 import junit.framework.Test;
30 import junit.framework.TestSuite;
31
32 import org.opensubsystems.core.error.OSSException;
33 import org.opensubsystems.core.persist.db.Database;
34 import org.opensubsystems.core.persist.db.DatabaseImpl;
35 import org.opensubsystems.core.persist.db.DatabaseSchemaManager;
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 prepared statement.
44  *
45  * @version $Id: PreparedStatementTest.java,v 1.8 2007/01/07 06:14:51 bastafidli Exp $
46  * @author Miro Halas
47  * @code.reviewer Miro Halas
48  * @code.reviewed Initial revision
49  */

50 public final class PreparedStatementTest
51 {
52    // Constructors /////////////////////////////////////////////////////////////
53

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

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

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

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

86    public static class PreparedStatementTestInternal extends DatabaseTest
87    {
88       // Cached values ////////////////////////////////////////////////////////////
89

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

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

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

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

135       public void testRepeatedTransactionInsert(
136       ) throws Throwable JavaDoc
137       {
138          final String JavaDoc INSERT_VALUE = ((DatabaseTestSchema)DatabaseSchemaManager.getInstance(
139                                         DatabaseTestSchema.class)).getInsertGeneratedKey();
140          final String JavaDoc DELETE_VALUE = "delete from GENERATEDKEY_TEST where TEST_VALUE = ?";
141          final String JavaDoc VALUE_TEST = "insert test value";
142          // This should be above 100 to test JOTM/XAPool looping bug
143
final int REPEAT_COUNT = 530;
144           
145          PreparedStatement JavaDoc insertStatement = null;
146          ResultSet JavaDoc rsResults = null;
147          int iInsertCount;
148          int iIndex;
149          
150          for (iIndex = 0; iIndex < REPEAT_COUNT; iIndex++)
151          {
152             m_transaction.begin();
153             try
154             {
155                try
156                {
157                   insertStatement = m_connection.prepareStatement(INSERT_VALUE);
158                   insertStatement.setString(1, VALUE_TEST);
159       
160                   iInsertCount = insertStatement.executeUpdate();
161                }
162                finally
163                {
164                   DatabaseUtils.closeResultSetAndStatement(rsResults, insertStatement);
165                }
166                m_transaction.commit();
167             }
168             catch (Throwable JavaDoc throwable)
169             {
170                m_transaction.rollback();
171                throw throwable;
172             }
173             
174             assertEquals("Exactly one record have been inserted.",
175                                 iInsertCount, 1);
176          }
177          s_logger.finest("Inserted " + iIndex + " records, each in separate transaction.");
178                                                              
179          // Now select it back to be sure it is there so we can delete it
180
int iDeletedCount = 0;
181    
182          m_transaction.begin();
183          try
184          {
185             PreparedStatement JavaDoc deleteStatement;
186             
187             deleteStatement = m_connection.prepareStatement(DELETE_VALUE);
188             deleteStatement.setString(1, VALUE_TEST);
189             iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement);
190             m_transaction.commit();
191             
192             assertEquals("Exactly " + iIndex + " records with data shoud have been deleted.",
193                                 iDeletedCount, iIndex);
194    
195             s_logger.finest("Deleted " + iIndex + " records, all in one transaction.");
196          }
197          catch (Throwable JavaDoc throwable)
198          {
199             m_transaction.rollback();
200             throw throwable;
201          }
202       }
203    }
204 }
205
Popular Tags