KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: SetNullColumnTest.java,v 1.10 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 setting of NULL value and retrieving it from the varchar type DB column
42  *
43  * @version $Id: SetNullColumnTest.java,v 1.10 2007/01/07 06:14:51 bastafidli Exp $
44  * @author Julo Legeny
45  * @code.reviewer
46  * @code.reviewed TODO: Review this code
47  */

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

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

55    private SetNullColumnTest(
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("SetNullColumnTest");
73       suite.addTestSuite(SetNullColumnTestInternal.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 SetNullColumnTestInternal 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 SetNullColumnTestInternal(
112          String JavaDoc strTestName
113       )
114       {
115          super(strTestName);
116       }
117       
118       /**
119        * Test if there can be stored empty string in the VARCHAR type column
120        * This is not working with some older version of 'com.p6spy.engine.spy.P6SpyDriver'
121        * also Oracle doesn't make any difference betweeen null and empty string
122        * which may cause problems in the code.
123        *
124        * @throws Throwable - an error has occured during test
125        */

126       public void testInsertSelectEmptyString(
127       ) throws Throwable JavaDoc
128       {
129          final String JavaDoc INSERT_VALUE = "insert into NULL_COLUMN_TEST (NAME) values (?)";
130          final String JavaDoc SELECT_VALUE = "select NAME from NULL_COLUMN_TEST";
131          final String JavaDoc DELETE_VALUE = "delete from NULL_COLUMN_TEST";
132          
133          PreparedStatement JavaDoc insertStatement = null;
134          ResultSet JavaDoc rsResults = null;
135          int iInsertCount;
136          
137          String JavaDoc testValue = "";
138          boolean testResult = false;
139          // insert values to the table
140
m_transaction.begin();
141          try
142          {
143             try
144             {
145                // insert empty string
146
insertStatement = m_connection.prepareStatement(INSERT_VALUE);
147                insertStatement.setString(1, "");
148                iInsertCount = insertStatement.executeUpdate();
149                if (GlobalConstants.ERROR_CHECKING)
150                {
151                   // This is here to avoid Checkstyle warning
152
assert iInsertCount >= 0 : "executeUpdate cannot return negative value.";
153                }
154                m_transaction.commit();
155             }
156             catch (Throwable JavaDoc throwable)
157             {
158                m_transaction.rollback();
159                throw throwable;
160             }
161             finally
162             {
163                DatabaseUtils.closeResultSetAndStatement(rsResults, insertStatement);
164                rsResults = null;
165             }
166    
167             // try to select values
168
PreparedStatement JavaDoc selectStatement = null;
169          
170             try
171             {
172                selectStatement = m_connection.prepareStatement(SELECT_VALUE);
173                rsResults = selectStatement.executeQuery();
174             
175                assertTrue("No result in select", rsResults.next());
176                testValue = rsResults.getString(1);
177                assertNotNull("Inserted string shoul be not null or empty string" +
178                              " returned as null", testValue);
179                testResult = rsResults.wasNull();
180                assertFalse("Inserted record shoul be not treated as null", testResult);
181             }
182             finally
183             {
184                DatabaseUtils.closeResultSetAndStatement(rsResults, selectStatement);
185                rsResults = null;
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_VALUE);
197                deleteStatement.execute();
198                m_transaction.commit();
199             }
200             catch (Throwable JavaDoc throwable)
201             {
202                m_transaction.rollback();
203                throw throwable;
204             }
205          }
206       }
207    }
208 }
209
Popular Tags