KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > testsuite > regression > NumbersRegressionTest


1 /*
2  Copyright (C) 2002-2004 MySQL AB
3
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of version 2 of the GNU General Public License as
6  published by the Free Software Foundation.
7
8  There are special exceptions to the terms and conditions of the GPL
9  as it is applied to this software. View the full text of the
10  exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11  software distribution.
12
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22
23
24  */

25 package testsuite.regression;
26
27 import java.sql.ResultSetMetaData JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 import testsuite.BaseTestCase;
31
32 /**
33  * Tests various number-handling issues that have arrisen in the JDBC driver at
34  * one time or another.
35  *
36  * @author Mark Matthews
37  */

38 public class NumbersRegressionTest extends BaseTestCase {
39     /**
40      * Constructor for NumbersRegressionTest.
41      *
42      * @param name
43      * the test name
44      */

45     public NumbersRegressionTest(String JavaDoc name) {
46         super(name);
47     }
48
49     /**
50      * Runs all test cases
51      *
52      * @param args
53      * command-line args
54      *
55      * @throws Exception
56      * if any errors occur
57      */

58     public static void main(String JavaDoc[] args) {
59         junit.textui.TestRunner.run(NumbersRegressionTest.class);
60     }
61
62     /**
63      * Tests that BIGINT retrieval works correctly
64      *
65      * @throws Exception
66      * if any errors occur
67      */

68     public void testBigInt() throws Exception JavaDoc {
69         try {
70             this.stmt.executeUpdate("DROP TABLE IF EXISTS bigIntRegression");
71             this.stmt
72                     .executeUpdate("CREATE TABLE bigIntRegression ( val BIGINT NOT NULL)");
73             this.stmt
74                     .executeUpdate("INSERT INTO bigIntRegression VALUES (6692730313872877584)");
75             this.rs = this.stmt
76                     .executeQuery("SELECT val FROM bigIntRegression");
77
78             while (this.rs.next()) {
79                 // check retrieval
80
long retrieveAsLong = this.rs.getLong(1);
81                 assertTrue(retrieveAsLong == 6692730313872877584L);
82             }
83
84             this.rs.close();
85             this.stmt.executeUpdate("DROP TABLE IF EXISTS bigIntRegression");
86
87             String JavaDoc bigIntAsString = "6692730313872877584";
88
89             long parsedBigIntAsLong = Long.parseLong(bigIntAsString);
90
91             // check JDK parsing
92
assertTrue(bigIntAsString
93                     .equals(String.valueOf(parsedBigIntAsLong)));
94         } finally {
95             this.stmt.executeUpdate("DROP TABLE IF EXISTS bigIntRegression");
96         }
97     }
98
99     /**
100      * Tests correct type assignment for MySQL FLOAT and REAL datatypes.
101      *
102      * @throws Exception
103      * if the test fails.
104      */

105     public void testFloatsAndReals() throws Exception JavaDoc {
106         try {
107             this.stmt.executeUpdate("DROP TABLE IF EXISTS floatsAndReals");
108             this.stmt
109                     .executeUpdate("CREATE TABLE IF NOT EXISTS floatsAndReals(floatCol FLOAT, realCol REAL, doubleCol DOUBLE)");
110             this.stmt
111                     .executeUpdate("INSERT INTO floatsAndReals VALUES (0, 0, 0)");
112
113             this.rs = this.stmt
114                     .executeQuery("SELECT floatCol, realCol, doubleCol FROM floatsAndReals");
115
116             ResultSetMetaData JavaDoc rsmd = this.rs.getMetaData();
117
118             this.rs.next();
119
120             assertTrue(rsmd.getColumnClassName(1).equals("java.lang.Float"));
121             assertTrue(this.rs.getObject(1).getClass().getName().equals(
122                     "java.lang.Float"));
123
124             assertTrue(rsmd.getColumnClassName(2).equals("java.lang.Double"));
125             assertTrue(this.rs.getObject(2).getClass().getName().equals(
126                     "java.lang.Double"));
127
128             assertTrue(rsmd.getColumnClassName(3).equals("java.lang.Double"));
129             assertTrue(this.rs.getObject(3).getClass().getName().equals(
130                     "java.lang.Double"));
131
132         } finally {
133             this.stmt.executeUpdate("DROP TABLE IF EXISTS floatsAndReals");
134         }
135     }
136
137     /**
138      * Tests that ResultSetMetaData precision and scale methods work correctly
139      * for all numeric types.
140      *
141      * @throws Exception
142      * if any errors occur
143      */

144     public void testPrecisionAndScale() throws Exception JavaDoc {
145         testPrecisionForType("TINYINT", 8, -1, false);
146         testPrecisionForType("TINYINT", 8, -1, true);
147         testPrecisionForType("SMALLINT", 8, -1, false);
148         testPrecisionForType("SMALLINT", 8, -1, true);
149         testPrecisionForType("MEDIUMINT", 8, -1, false);
150         testPrecisionForType("MEDIUMINT", 8, -1, true);
151         testPrecisionForType("INT", 8, -1, false);
152         testPrecisionForType("INT", 8, -1, true);
153         testPrecisionForType("BIGINT", 8, -1, false);
154         testPrecisionForType("BIGINT", 8, -1, true);
155
156         testPrecisionForType("FLOAT", 8, 4, false);
157         testPrecisionForType("FLOAT", 8, 4, true);
158         testPrecisionForType("DOUBLE", 8, 4, false);
159         testPrecisionForType("DOUBLE", 8, 4, true);
160
161         testPrecisionForType("DECIMAL", 8, 4, false);
162         testPrecisionForType("DECIMAL", 8, 4, true);
163
164         testPrecisionForType("DECIMAL", 9, 0, false);
165         testPrecisionForType("DECIMAL", 9, 0, true);
166     }
167
168     private void testPrecisionForType(String JavaDoc typeName, int m, int d,
169             boolean unsigned) throws Exception JavaDoc {
170         try {
171             this.stmt
172                     .executeUpdate("DROP TABLE IF EXISTS precisionAndScaleRegression");
173
174             StringBuffer JavaDoc createStatement = new StringBuffer JavaDoc(
175                     "CREATE TABLE precisionAndScaleRegression ( val ");
176             createStatement.append(typeName);
177             createStatement.append("(");
178             createStatement.append(m);
179
180             if (d != -1) {
181                 createStatement.append(",");
182                 createStatement.append(d);
183             }
184
185             createStatement.append(")");
186
187             if (unsigned) {
188                 createStatement.append(" UNSIGNED ");
189             }
190
191             createStatement.append(" NOT NULL)");
192
193             this.stmt.executeUpdate(createStatement.toString());
194
195             this.rs = this.stmt
196                     .executeQuery("SELECT val FROM precisionAndScaleRegression");
197
198             ResultSetMetaData JavaDoc rsmd = this.rs.getMetaData();
199             assertTrue("Precision returned incorrectly for type " + typeName
200                     + ", " + m + " != rsmd.getPrecision() = "
201                     + rsmd.getPrecision(1), rsmd.getPrecision(1) == m);
202
203             if (d != -1) {
204                 assertTrue("Scale returned incorrectly for type " + typeName
205                         + ", " + d + " != rsmd.getScale() = "
206                         + rsmd.getScale(1), rsmd.getScale(1) == d);
207             }
208         } finally {
209             if (this.rs != null) {
210                 try {
211                     this.rs.close();
212                 } catch (Exception JavaDoc ex) {
213                     // ignore
214
}
215             }
216
217             this.stmt
218                     .executeUpdate("DROP TABLE IF EXISTS precisionAndScaleRegression");
219         }
220     }
221
222     public void testIntShouldReturnLong() throws Exception JavaDoc {
223         try {
224             this.stmt.executeUpdate("DROP TABLE IF EXISTS testIntRetLong");
225             this.stmt.executeUpdate("CREATE TABLE testIntRetLong(field1 INT)");
226             this.stmt.executeUpdate("INSERT INTO testIntRetLong VALUES (1)");
227
228             this.rs = this.stmt.executeQuery("SELECT * FROM testIntRetLong");
229             this.rs.next();
230
231             assertTrue(this.rs.getObject(1).getClass().equals(
232                     java.lang.Integer JavaDoc.class));
233         } finally {
234             if (this.rs != null) {
235                 try {
236                     this.rs.close();
237                 } catch (SQLException JavaDoc sqlEx) {
238                     // ignore
239
}
240
241                 this.rs = null;
242             }
243
244             this.stmt.executeUpdate("DROP TABLE IF EXISTS testIntRetLong");
245
246         }
247     }
248
249     /**
250      * Tests fix for BUG#5729, UNSIGNED BIGINT returned incorrectly
251      *
252      * @throws Exception
253      * if the test fails
254      */

255     public void testBug5729() throws Exception JavaDoc {
256         if (versionMeetsMinimum(4, 1)) {
257             String JavaDoc valueAsString = "1095923280000";
258
259             try {
260                 this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug5729");
261                 this.stmt
262                         .executeUpdate("CREATE TABLE testBug5729(field1 BIGINT UNSIGNED)");
263                 this.stmt.executeUpdate("INSERT INTO testBug5729 VALUES ("
264                         + valueAsString + ")");
265
266                 this.rs = this.conn.prepareStatement(
267                         "SELECT * FROM testBug5729").executeQuery();
268                 this.rs.next();
269
270                 assertTrue(this.rs.getObject(1).toString()
271                         .equals(valueAsString));
272             } finally {
273                 this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug5729");
274             }
275         }
276     }
277
278     /**
279      * Tests fix for BUG#8484 - ResultSet.getBigDecimal() throws exception when
280      * rounding would need to occur to set scale.
281      *
282      * @throws Exception
283      * if the test fails
284      * @deprecated
285      */

286     public void testBug8484() throws Exception JavaDoc {
287         try {
288             this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug8484");
289             this.stmt
290                     .executeUpdate("CREATE TABLE testBug8484 (field1 DECIMAL(16, 8), field2 varchar(32))");
291             this.stmt
292                     .executeUpdate("INSERT INTO testBug8484 VALUES (12345678.12345678, '')");
293             this.rs = this.stmt
294                     .executeQuery("SELECT field1, field2 FROM testBug8484");
295             this.rs.next();
296             assertEquals("12345678.123", this.rs.getBigDecimal(1, 3).toString());
297             assertEquals("0.000", this.rs.getBigDecimal(2, 3).toString());
298
299             this.pstmt = this.conn
300                     .prepareStatement("SELECT field1, field2 FROM testBug8484");
301             this.rs = this.pstmt.executeQuery();
302             this.rs.next();
303             assertEquals("12345678.123", this.rs.getBigDecimal(1, 3).toString());
304             assertEquals("0.000", this.rs.getBigDecimal(2, 3).toString());
305         } finally {
306             this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug8484");
307         }
308     }
309 }
310
Popular Tags