KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: SameColumnTest.java,v 1.7 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 import org.opensubsystems.core.util.GlobalConstants;
39
40 /**
41  * Test for same columns
42  *
43  * @version $Id: SameColumnTest.java,v 1.7 2007/01/07 06:14:51 bastafidli Exp $
44  * @author Peter Satury
45  * @code.reviewer Miro Halas
46  * @code.reviewed Initial revision
47  */

48 public final class SameColumnTest
49 {
50    // Constructors /////////////////////////////////////////////////////////////
51

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

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

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

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

84    public static class SameColumnTestInternal extends DatabaseTest
85    {
86       /**
87        * Static initializer
88        */

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

111       public SameColumnTestInternal(
112          String JavaDoc strTestName
113       )
114       {
115          super(strTestName);
116       }
117       
118       /**
119        * Test if DB supports same column names in select query
120        * This is not working with some older version of 'com.p6spy.engine.spy.P6SpyDriver'
121        *
122        * @throws Throwable - an error has occured during test
123        */

124       public void testSameColumSelect(
125       ) throws Throwable JavaDoc
126       {
127          final String JavaDoc INSERT_VALUE1 = "insert into SAME_TEST1 (ID) values (?)";
128          final String JavaDoc INSERT_VALUE2 = "insert into SAME_TEST2 (ID) values (?)";
129          final String JavaDoc SELECT_VALUE = "select SAME_TEST1.ID,SAME_TEST2.ID" +
130                                      " from SAME_TEST1, SAME_TEST2";
131          final String JavaDoc DELETE_VALUE1 = "delete from SAME_TEST1";
132          final String JavaDoc DELETE_VALUE2 = "delete from SAME_TEST2";
133          
134          // two different values
135
final int VALUE1 = 1;
136          final int VALUE2 = 2;
137          
138          PreparedStatement JavaDoc insertStatement = null;
139          ResultSet JavaDoc rsResults = null;
140          int iInsertCount;
141          
142          // insert values to each table
143
m_transaction.begin();
144          try
145          {
146             try
147             {
148                insertStatement = m_connection.prepareStatement(INSERT_VALUE1);
149                insertStatement.setInt(1, VALUE1);
150                iInsertCount = insertStatement.executeUpdate();
151                if (GlobalConstants.ERROR_CHECKING)
152                {
153                   // This is here to avoid Checkstyle warning
154
assert iInsertCount >= 0 : "executeUpdate cannot return negative value.";
155                }
156
157                insertStatement = m_connection.prepareStatement(INSERT_VALUE2);
158                insertStatement.setInt(1, VALUE2);
159                iInsertCount = insertStatement.executeUpdate();
160                if (GlobalConstants.ERROR_CHECKING)
161                {
162                   // This is here to avoid Checkstyle warning
163
assert iInsertCount >= 0 : "executeUpdate cannot return negative value.";
164                }
165             }
166             catch (Throwable JavaDoc throwable)
167             {
168                m_transaction.rollback();
169                throw throwable;
170             }
171             finally
172             {
173                DatabaseUtils.closeResultSetAndStatement(rsResults, insertStatement);
174             }
175             m_transaction.commit();
176    
177             // try to select values
178
PreparedStatement JavaDoc selectStatement = null;
179          
180             selectStatement = m_connection.prepareStatement(SELECT_VALUE);
181             rsResults = selectStatement.executeQuery();
182          
183             assertTrue("No result in select", rsResults.next());
184             assertEquals("The first returned value is not correct", VALUE1, rsResults.getInt(1));
185             assertEquals("The second returned value is not correct", VALUE2, rsResults.getInt(2));
186    
187          }
188          finally
189          {
190             // delete test data
191
m_transaction.begin();
192             try
193             {
194                PreparedStatement JavaDoc deleteStatement;
195                
196                deleteStatement = m_connection.prepareStatement(DELETE_VALUE1);
197                deleteStatement.execute();
198                deleteStatement = m_connection.prepareStatement(DELETE_VALUE2);
199                deleteStatement.execute();
200       
201                m_transaction.commit();
202             }
203             catch (Throwable JavaDoc throwable)
204             {
205                m_transaction.rollback();
206                throw throwable;
207             }
208          }
209       }
210    }
211 }
212
Popular Tags