KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: UpdateDBProcWithoutOutputTest.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
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 update DB procedure without output parameter.
41  *
42  * @version $Id: UpdateDBProcWithoutOutputTest.java,v 1.8 2007/01/07 06:14:51 bastafidli Exp $
43  * @author Julo Legeny
44  * @code.reviewer Miro Halas
45  * @code.reviewed Initial revision
46  */

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

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

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

123       public void testUpdateDBProcWithoutOutputParam(
124       ) throws Throwable JavaDoc
125       {
126          final String JavaDoc INSERT_VALUE = "insert into TRANSACTION_TEST (TEST_ID,TEST_VALUE)" +
127                                      " values (?,?)";
128          final String JavaDoc DELETE_VALUE = "delete from TRANSACTION_TEST where TEST_VALUE in (?, ?)";
129          final String JavaDoc DELETE_ALL = "delete from TRANSACTION_TEST";
130          final String JavaDoc OLD_VALUE_TEST = "test_value";
131          final String JavaDoc NEW_VALUE_TEST = "test_value_updated";
132    
133          PreparedStatement JavaDoc insertStatement = null;
134          PreparedStatement JavaDoc deleteStatement = null;
135          int iInsertCount = 0;
136          int iUpdateCount = 0;
137          int iDeletedCount = 0;
138    
139          try
140          {
141             //******************************************************************
142
// Try to select original record to verify that the database is in OK state
143
m_transaction.begin();
144             try
145             {
146                deleteStatement = m_connection.prepareStatement(DELETE_ALL);
147    
148                iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement);
149    
150                m_transaction.commit();
151             }
152             catch (Throwable JavaDoc throwable)
153             {
154                m_transaction.rollback();
155                throw throwable;
156             }
157             
158             assertEquals("No records should be initially in the database.",
159                                 0, iDeletedCount);
160    
161             // insert value
162
m_transaction.begin();
163             try
164             {
165                try
166                {
167                   insertStatement = m_connection.prepareStatement(INSERT_VALUE);
168                   insertStatement.setInt(1, 100);
169                   insertStatement.setString(2, OLD_VALUE_TEST);
170       
171                   iInsertCount = insertStatement.executeUpdate();
172                }
173                finally
174                {
175                   DatabaseUtils.closeStatement(insertStatement);
176                }
177                m_transaction.commit();
178             }
179             catch (Throwable JavaDoc throwable)
180             {
181                m_transaction.rollback();
182                throw throwable;
183             }
184             assertEquals("Exactly one record have been inserted.",
185                                 iInsertCount, 1);
186    
187    
188             // try to update inserted value using DB procedure
189
m_transaction.begin();
190             try
191             {
192                iUpdateCount = ((DatabaseTestSchema)DatabaseSchemaManager.getInstance(
193                                  DatabaseTestSchema.class)).executeUpdateTestValue(
194                                                                m_connection,
195                                                                OLD_VALUE_TEST,
196                                                                NEW_VALUE_TEST);
197                m_transaction.commit();
198    
199             }
200             catch (Throwable JavaDoc throwable)
201             {
202                m_transaction.rollback();
203                throw throwable;
204             }
205             assertEquals("Exactly one record have been updated.",
206                                 1, iUpdateCount);
207    
208          }
209          finally
210          {
211             // delete inserted data
212
m_transaction.begin();
213             try
214             {
215                deleteStatement = m_connection.prepareStatement(DELETE_VALUE);
216                deleteStatement.setString(1, OLD_VALUE_TEST);
217                deleteStatement.setString(2, NEW_VALUE_TEST);
218                iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement);
219                m_transaction.commit();
220             
221             }
222             catch (Throwable JavaDoc throwable)
223             {
224                m_transaction.rollback();
225                throw throwable;
226             }
227             finally
228             {
229                DatabaseUtils.closeStatement(deleteStatement);
230             }
231             assertEquals("Exactly 1 record with data shoud have been deleted.",
232                                 1, iDeletedCount);
233          }
234       }
235    }
236 }
237
Popular Tags