KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: AddBatchTest.java,v 1.6 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
27 import junit.extensions.TestSetup;
28 import junit.framework.Test;
29 import junit.framework.TestSuite;
30
31 import org.opensubsystems.core.error.OSSException;
32 import org.opensubsystems.core.persist.db.Database;
33 import org.opensubsystems.core.persist.db.DatabaseImpl;
34 import org.opensubsystems.core.persist.db.DatabaseTest;
35 import org.opensubsystems.core.persist.db.DatabaseTestSetup;
36 import org.opensubsystems.core.persist.db.DatabaseTestSuite;
37 import org.opensubsystems.core.util.DatabaseUtils;
38
39 /**
40  * Test for inserting more values into the DB using addBatch() method
41  *
42  * @version $Id: AddBatchTest.java,v 1.6 2007/01/07 06:14:51 bastafidli Exp $
43  * @author Julo Legeny
44  * @code.reviewer Miro Halas
45  * @code.reviewed 1.3 2005/07/29 08:49:49 bastafidli
46  */

47 public final class AddBatchTest
48 {
49    // Constructors /////////////////////////////////////////////////////////////
50

51    /**
52     * Private constructor since this class cannot be instantiated
53     */

54    private AddBatchTest(
55    )
56    {
57       // Do nothing
58
}
59    
60    // Public methods ///////////////////////////////////////////////////////////
61

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

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

83    public static class AddBatchTestInternal extends DatabaseTest
84    {
85       /**
86        * Static initializer
87        */

88       static
89       {
90          // This test use special database schema so make the database aware of it
91
Database dbDatabase;
92    
93          try
94          {
95             dbDatabase = DatabaseImpl.getInstance();
96             // Add schema database tests needs to the database
97
dbDatabase.add(DatabaseTestSchema.class);
98          }
99          catch (OSSException bfeExc)
100          {
101             throw new RuntimeException JavaDoc("Unexpected exception.", bfeExc);
102          }
103       }
104       
105       /**
106        * Create new test.
107        *
108        * @param strTestName - name of the test
109        */

110       public AddBatchTestInternal(
111          String JavaDoc strTestName
112       )
113       {
114          super(strTestName);
115       }
116       
117       /**
118        * Test for inserting more values into the DB using addBatch() method
119        *
120        * @throws Throwable - an error has occured during test
121        */

122       public void testAddBatch(
123       ) throws Throwable JavaDoc
124       {
125          final String JavaDoc INSERT_VALUE = "insert into NULL_COLUMN_TEST (NAME) values (?)";
126          final String JavaDoc SELECT_VALUE = "select count(NAME) from NULL_COLUMN_TEST where " +
127                                      "NAME like 'test_batch_%'";
128          final String JavaDoc DELETE_VALUE = "delete from NULL_COLUMN_TEST";
129          
130          PreparedStatement JavaDoc insertStatement = null;
131          ResultSet JavaDoc rsResults = null;
132          int[] arrInsertCount = new int[10];
133          
134          int testValue = 0;
135    
136          // insert values to the table
137
m_transaction.begin();
138          try
139          {
140             try
141             {
142                insertStatement = m_connection.prepareStatement(INSERT_VALUE);
143                // insert all data in the cycle
144
for (int iIndex = 1; iIndex < 11; iIndex++)
145                {
146                 insertStatement.setString(1, "test_batch_" + iIndex);
147                 insertStatement.addBatch();
148                }
149                arrInsertCount = insertStatement.executeBatch();
150                m_transaction.commit();
151             }
152             catch (Throwable JavaDoc throwable)
153             {
154                m_transaction.rollback();
155                throw throwable;
156             }
157             finally
158             {
159                DatabaseUtils.closeStatement(insertStatement);
160                rsResults = null;
161             }
162    
163             assertEquals("Number of inserted records is incorrect", 10, arrInsertCount.length);
164    
165             // try to select values
166
PreparedStatement JavaDoc selectStatement = null;
167          
168             try
169             {
170                selectStatement = m_connection.prepareStatement(SELECT_VALUE);
171                rsResults = selectStatement.executeQuery();
172             
173                assertTrue("No result in select", rsResults.next());
174                testValue = rsResults.getInt(1);
175                assertEquals("Incorrect number of specified records selected", 10, testValue);
176             }
177             finally
178             {
179                DatabaseUtils.closeResultSetAndStatement(rsResults, selectStatement);
180                rsResults = null;
181             }
182          }
183          finally
184          {
185             // delete test data
186
m_transaction.begin();
187             try
188             {
189                PreparedStatement JavaDoc deleteStatement;
190                
191                deleteStatement = m_connection.prepareStatement(DELETE_VALUE);
192                deleteStatement.execute();
193                m_transaction.commit();
194             }
195             catch (Throwable JavaDoc throwable)
196             {
197                m_transaction.rollback();
198                throw throwable;
199             }
200          }
201       }
202    }
203 }
204
Popular Tags