KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: SelectAfterInsertIntoUniqueColumnTest.java,v 1.11 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.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29
30 import junit.extensions.TestSetup;
31 import junit.framework.Test;
32 import junit.framework.TestSuite;
33
34 import org.opensubsystems.core.error.OSSException;
35 import org.opensubsystems.core.persist.db.Database;
36 import org.opensubsystems.core.persist.db.DatabaseImpl;
37 import org.opensubsystems.core.persist.db.DatabaseTest;
38 import org.opensubsystems.core.persist.db.DatabaseTestSetup;
39 import org.opensubsystems.core.persist.db.DatabaseTestSuite;
40 import org.opensubsystems.core.util.DatabaseUtils;
41 import org.opensubsystems.core.util.Log;
42
43 /**
44  * Test for selecting data after inserting records and then inserting duplicate
45  * record into the unique table column.
46  *
47  * @version $Id: SelectAfterInsertIntoUniqueColumnTest.java,v 1.11 2007/01/07 06:14:51 bastafidli Exp $
48  * @author Julo Legeny
49  * @code.reviewer Miro Halas
50  * @code.reviewed 1.8 2005/09/09 06:50:44 bastafidli
51  */

52 public final class SelectAfterInsertIntoUniqueColumnTest
53 {
54    // Constructors /////////////////////////////////////////////////////////////
55

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

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

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

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

88    public static class SelectAfterInsertIntoUniqueColumnTestInternal extends DatabaseTest
89    {
90       // Cached values ////////////////////////////////////////////////////////////
91

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

95       private static Logger JavaDoc s_logger
96          = Log.getInstance(SelectAfterInsertIntoUniqueColumnTestInternal.class);
97       
98       // Constructors //////////////////////////////////////////////////////////
99

100       /**
101        * Static initializer
102        */

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

125       public SelectAfterInsertIntoUniqueColumnTestInternal(
126          String JavaDoc strTestName
127       )
128       {
129          super(strTestName);
130       }
131       
132       /**
133        * Test for selecting data after inserting records and then inserting
134        * duplicate record into the unique table column.
135        *
136        * @throws Throwable - an error has occured during test
137        */

138       public void testSelectAfterInsertIntoUniqueColumn(
139       ) throws Throwable JavaDoc
140       {
141          final String JavaDoc INSERT_VALUE = "insert into UNIQUE_COLUMN_TEST (TEST_ID) values (?)";
142          final String JavaDoc SELECT_VALUE = "select count(*) from UNIQUE_COLUMN_TEST";
143          final String JavaDoc DELETE_ALL = "delete from UNIQUE_COLUMN_TEST";
144    
145          PreparedStatement JavaDoc insertStatement = null;
146          PreparedStatement JavaDoc deleteStatement = null;
147          PreparedStatement JavaDoc selectStatement = null;
148          ResultSet JavaDoc rsResults = null;
149          int iDeletedCount = 0;
150          int iCounter;
151    
152          try
153          {
154             //******************************************************************
155
// Try to select original record to verify that the database is in OK state
156
m_transaction.begin();
157             try
158             {
159                deleteStatement = m_connection.prepareStatement(DELETE_ALL);
160                iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement);
161                m_transaction.commit();
162             }
163             catch (Throwable JavaDoc throwable)
164             {
165                m_transaction.rollback();
166                throw throwable;
167             }
168             finally
169             {
170                DatabaseUtils.closeStatement(deleteStatement);
171                deleteStatement = null;
172             }
173             
174             assertEquals("No records should be initially in the database.",
175                          0, iDeletedCount);
176    
177             // insert value
178
m_transaction.begin();
179             try
180             {
181                try
182                {
183                   // try to insert 5 records
184
for (iCounter = 1; iCounter < 6; iCounter++)
185                   {
186                      insertStatement = m_connection.prepareStatement(INSERT_VALUE);
187                      insertStatement.setInt(1, 100 * iCounter);
188       
189                      insertStatement.executeUpdate();
190                   }
191                   // insert duplicite value into unique column
192
try
193                   {
194                      insertStatement.setInt(1, 100);
195                      insertStatement.executeUpdate();
196                      fail("Database allowed to insert duplicate value into unique column.");
197                   }
198                   catch (SQLException JavaDoc sqlExc)
199                   {
200                      // We are expecting exception here due to duplicate
201
}
202                   
203                   try
204                   {
205                      selectStatement = m_connection.prepareStatement(SELECT_VALUE);
206                      rsResults = selectStatement.executeQuery();
207                      
208                      if (rsResults.next())
209                      {
210                         assertEquals("Incorrect number of selected items",
211                                      5, rsResults.getInt(1));
212                      }
213                   }
214                   catch (SQLException JavaDoc sqleExc)
215                   {
216    // See http://www.talkaboutdatabases.com/group/pgsql.interfaces.jdbc/messages/1547.html
217
if (sqleExc.getMessage().indexOf("ERROR: current transaction" +
218                          " is aborted, commands ignored until end of transaction block")
219                          != -1)
220                      {
221                         s_logger.log(Level.WARNING, "Database doesn't allow execute" +
222                                      " additional commands in a transaction if" +
223                                      " one of the commands failed.", sqleExc);
224                         fail("Database doesn't allow execute additional commands" +
225                              " in a transaction if one of the commands failed.");
226                      }
227                      throw sqleExc;
228                   }
229                   finally
230                   {
231                      DatabaseUtils.closeResultSetAndStatement(rsResults,
232                                                               selectStatement);
233                   }
234                }
235                finally
236                {
237                   DatabaseUtils.closeStatement(insertStatement);
238                }
239                m_transaction.commit();
240             }
241             catch (Throwable JavaDoc throwable)
242             {
243                m_transaction.rollback();
244                throw throwable;
245             }
246          }
247          finally
248          {
249             // delete inserted data
250
m_transaction.begin();
251             try
252             {
253                deleteStatement = m_connection.prepareStatement(DELETE_ALL);
254                iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement);
255                m_transaction.commit();
256             
257             }
258             catch (Throwable JavaDoc throwable)
259             {
260                m_transaction.rollback();
261                throw throwable;
262             }
263             finally
264             {
265                DatabaseUtils.closeStatement(deleteStatement);
266             }
267          }
268       }
269    }
270 }
271
Popular Tags