KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DBProcReturnInsertedRowsCountTest.java,v 1.12 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
26 import junit.extensions.TestSetup;
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 import org.opensubsystems.core.error.OSSException;
31 import org.opensubsystems.core.persist.db.Database;
32 import org.opensubsystems.core.persist.db.DatabaseImpl;
33 import org.opensubsystems.core.persist.db.DatabaseSchemaManager;
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 returning number of inserted rows from stored procedure.
41  *
42  * @version $Id: DBProcReturnInsertedRowsCountTest.java,v 1.12 2007/01/07 06:14:51 bastafidli Exp $
43  * @author Julo Legeny
44  * @code.reviewer
45  * @code.reviewed TODO: Review this code
46  */

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

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

54    private DBProcReturnInsertedRowsCountTest(
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("DBProcReturnInsertedRowsCountTest");
72       suite.addTestSuite(DBProcReturnInsertedRowsCountTestInternal.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 DBProcReturnInsertedRowsCountTestInternal 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 DBProcReturnInsertedRowsCountTestInternal(
111          String JavaDoc strTestName
112       )
113       {
114          super(strTestName);
115       }
116       
117       /**
118        * Test if the DB procedure will return number of inserted rows.
119        *
120        * @throws Throwable - an error has occured during test
121        */

122       public void testDBProcReturnInsertRowsCount(
123       ) throws Throwable JavaDoc
124       {
125          final String JavaDoc DELETE_VALUE = "delete from GENERATEDKEY_TEST where TEST_VALUE = ?";
126          final String JavaDoc VALUE_TEST = "test value";
127           
128          int iInsertCount = 0;
129          int iInsertedRows = 0;
130    
131          PreparedStatement JavaDoc deleteStatement = null;
132    
133          m_transaction.begin();
134    
135          try
136          {
137             try
138             {
139                int[] returnValues;
140                // Call stored procedure that will insert 1 record into the DB and returns
141
// number of inserted rows
142
returnValues = ((DatabaseTestSchema)DatabaseSchemaManager.getInstance(
143                                  DatabaseTestSchema.class)).executeInsertRow(
144                                     m_connection, VALUE_TEST);
145                if (returnValues != null)
146                {
147                   // value (number of affected rows) returned from insertStatement.executeUpdate();
148
iInsertCount = returnValues[0];
149                   // value (number of inserted rows) returned from stored procedure.
150
iInsertedRows = returnValues[1];
151                }
152                
153                m_transaction.commit();
154             }
155             catch (Throwable JavaDoc throwable)
156             {
157                m_transaction.rollback();
158                throw throwable;
159             }
160             
161             assertEquals("DBS does not support returning number of processed rows using" +
162                          " [ RowCounter = insertStatement.executeUpdate() ]. " +
163                          " Number of inserted rows is incorrect.", 1, iInsertCount);
164             // this assert will be shown if particular DBS doesn't support stored procedures
165
// or doesn't support retrieving number of affected rows.
166
assertTrue("DBS does not support stored procedures or stored procedure" +
167                        " does not support returning number of affected rows. Number" +
168                        " of inserted rows returned from stored procedure must be" +
169                        " greater than 0.",
170                        iInsertedRows > 0);
171                                                                 
172             int iDeletedCount = 0;
173       
174             m_transaction.begin();
175       
176             try
177             {
178                deleteStatement = m_connection.prepareStatement(DELETE_VALUE);
179                deleteStatement.setString(1, VALUE_TEST);
180                iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement);
181                m_transaction.commit();
182                
183                assertEquals("Exactly one record with date data shoud have been deleted.",
184                                    iDeletedCount, 1);
185             }
186             catch (Throwable JavaDoc throwable)
187             {
188                m_transaction.rollback();
189                throw throwable;
190             }
191          }
192          finally
193          {
194             // delete inserted data
195
m_transaction.begin();
196             try
197             {
198                // delete from GENERATEDKEY_TEST table
199
deleteStatement = m_connection.prepareStatement(DELETE_VALUE);
200                deleteStatement.setString(1, VALUE_TEST);
201                DatabaseUtils.executeUpdateAndClose(deleteStatement);
202                m_transaction.commit();
203             }
204             catch (Throwable JavaDoc throwable)
205             {
206                m_transaction.rollback();
207                throw throwable;
208             }
209             finally
210             {
211                DatabaseUtils.closeStatement(deleteStatement);
212             }
213          }
214          
215       }
216    }
217 }
218
Popular Tags