File size: 9,103 Bytes
2795186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 Copyright  2002-2004 MySQL AB, 2008 Sun Microsystems

 This program is free software; you can redistribute it and/or modify
 it under the terms of version 2 of the GNU General Public License as 
 published by the Free Software Foundation.

 There are special exceptions to the terms and conditions of the GPL 
 as it is applied to this software. View the full text of the 
 exception in file EXCEPTIONS-CONNECTOR-J in the directory of this 
 software distribution.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA



 */
package testsuite.regression;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import testsuite.BaseTestCase;

/**
 * Tests various number-handling issues that have arrisen in the JDBC driver at
 * one time or another.
 * 
 * @author Mark Matthews
 */
public class NumbersRegressionTest extends BaseTestCase {
	/**
	 * Constructor for NumbersRegressionTest.
	 * 
	 * @param name
	 *            the test name
	 */
	public NumbersRegressionTest(String name) {
		super(name);
	}

	/**
	 * Runs all test cases
	 * 
	 * @param args
	 *            command-line args
	 * 
	 * @throws Exception
	 *             if any errors occur
	 */
	public static void main(String[] args) {
		junit.textui.TestRunner.run(NumbersRegressionTest.class);
	}

	/**
	 * Tests that BIGINT retrieval works correctly
	 * 
	 * @throws Exception
	 *             if any errors occur
	 */
	public void testBigInt() throws Exception {
		try {
			this.stmt.executeUpdate("DROP TABLE IF EXISTS bigIntRegression");
			this.stmt
					.executeUpdate("CREATE TABLE bigIntRegression ( val BIGINT NOT NULL)");
			this.stmt
					.executeUpdate("INSERT INTO bigIntRegression VALUES (6692730313872877584)");
			this.rs = this.stmt
					.executeQuery("SELECT val FROM bigIntRegression");

			while (this.rs.next()) {
				// check retrieval
				long retrieveAsLong = this.rs.getLong(1);
				assertTrue(retrieveAsLong == 6692730313872877584L);
			}

			this.rs.close();
			this.stmt.executeUpdate("DROP TABLE IF EXISTS bigIntRegression");

			String bigIntAsString = "6692730313872877584";

			long parsedBigIntAsLong = Long.parseLong(bigIntAsString);

			// check JDK parsing
			assertTrue(bigIntAsString
					.equals(String.valueOf(parsedBigIntAsLong)));
		} finally {
			this.stmt.executeUpdate("DROP TABLE IF EXISTS bigIntRegression");
		}
	}

	/**
	 * Tests correct type assignment for MySQL FLOAT and REAL datatypes.
	 * 
	 * @throws Exception
	 *             if the test fails.
	 */
	public void testFloatsAndReals() throws Exception {
		try {
			this.stmt.executeUpdate("DROP TABLE IF EXISTS floatsAndReals");
			this.stmt
					.executeUpdate("CREATE TABLE IF NOT EXISTS floatsAndReals(floatCol FLOAT, realCol REAL, doubleCol DOUBLE)");
			this.stmt
					.executeUpdate("INSERT INTO floatsAndReals VALUES (0, 0, 0)");

			this.rs = this.stmt
					.executeQuery("SELECT floatCol, realCol, doubleCol FROM floatsAndReals");

			ResultSetMetaData rsmd = this.rs.getMetaData();

			this.rs.next();

			assertTrue(rsmd.getColumnClassName(1).equals("java.lang.Float"));
			assertTrue(this.rs.getObject(1).getClass().getName().equals(
					"java.lang.Float"));

			assertTrue(rsmd.getColumnClassName(2).equals("java.lang.Double"));
			assertTrue(this.rs.getObject(2).getClass().getName().equals(
					"java.lang.Double"));

			assertTrue(rsmd.getColumnClassName(3).equals("java.lang.Double"));
			assertTrue(this.rs.getObject(3).getClass().getName().equals(
					"java.lang.Double"));

		} finally {
			this.stmt.executeUpdate("DROP TABLE IF EXISTS floatsAndReals");
		}
	}

	/**
	 * Tests that ResultSetMetaData precision and scale methods work correctly
	 * for all numeric types.
	 * 
	 * @throws Exception
	 *             if any errors occur
	 */
	public void testPrecisionAndScale() throws Exception {
		testPrecisionForType("TINYINT", 8, -1, false);
		testPrecisionForType("TINYINT", 8, -1, true);
		testPrecisionForType("SMALLINT", 8, -1, false);
		testPrecisionForType("SMALLINT", 8, -1, true);
		testPrecisionForType("MEDIUMINT", 8, -1, false);
		testPrecisionForType("MEDIUMINT", 8, -1, true);
		testPrecisionForType("INT", 8, -1, false);
		testPrecisionForType("INT", 8, -1, true);
		testPrecisionForType("BIGINT", 8, -1, false);
		testPrecisionForType("BIGINT", 8, -1, true);

		testPrecisionForType("FLOAT", 8, 4, false);
		testPrecisionForType("FLOAT", 8, 4, true);
		testPrecisionForType("DOUBLE", 8, 4, false);
		testPrecisionForType("DOUBLE", 8, 4, true);

		testPrecisionForType("DECIMAL", 8, 4, false);
		testPrecisionForType("DECIMAL", 8, 4, true);

		testPrecisionForType("DECIMAL", 9, 0, false);
		testPrecisionForType("DECIMAL", 9, 0, true);
	}

	private void testPrecisionForType(String typeName, int m, int d,
			boolean unsigned) throws Exception {
		try {
			this.stmt
					.executeUpdate("DROP TABLE IF EXISTS precisionAndScaleRegression");

			StringBuffer createStatement = new StringBuffer(
					"CREATE TABLE precisionAndScaleRegression ( val ");
			createStatement.append(typeName);
			createStatement.append("(");
			createStatement.append(m);

			if (d != -1) {
				createStatement.append(",");
				createStatement.append(d);
			}

			createStatement.append(")");

			if (unsigned) {
				createStatement.append(" UNSIGNED ");
			}

			createStatement.append(" NOT NULL)");

			this.stmt.executeUpdate(createStatement.toString());

			this.rs = this.stmt
					.executeQuery("SELECT val FROM precisionAndScaleRegression");

			ResultSetMetaData rsmd = this.rs.getMetaData();
			assertTrue("Precision returned incorrectly for type " + typeName
					+ ", " + m + " != rsmd.getPrecision() = "
					+ rsmd.getPrecision(1), rsmd.getPrecision(1) == m);

			if (d != -1) {
				assertTrue("Scale returned incorrectly for type " + typeName
						+ ", " + d + " != rsmd.getScale() = "
						+ rsmd.getScale(1), rsmd.getScale(1) == d);
			}
		} finally {
			if (this.rs != null) {
				try {
					this.rs.close();
				} catch (Exception ex) {
					// ignore
				}
			}

			this.stmt
					.executeUpdate("DROP TABLE IF EXISTS precisionAndScaleRegression");
		}
	}

	public void testIntShouldReturnLong() throws Exception {
		try {
			this.stmt.executeUpdate("DROP TABLE IF EXISTS testIntRetLong");
			this.stmt.executeUpdate("CREATE TABLE testIntRetLong(field1 INT)");
			this.stmt.executeUpdate("INSERT INTO testIntRetLong VALUES (1)");

			this.rs = this.stmt.executeQuery("SELECT * FROM testIntRetLong");
			this.rs.next();

			assertTrue(this.rs.getObject(1).getClass().equals(
					java.lang.Integer.class));
		} finally {
			if (this.rs != null) {
				try {
					this.rs.close();
				} catch (SQLException sqlEx) {
					// ignore
				}

				this.rs = null;
			}

			this.stmt.executeUpdate("DROP TABLE IF EXISTS testIntRetLong");

		}
	}

	/**
	 * Tests fix for BUG#5729, UNSIGNED BIGINT returned incorrectly
	 * 
	 * @throws Exception
	 *             if the test fails
	 */
	public void testBug5729() throws Exception {
		if (versionMeetsMinimum(4, 1)) {
			String valueAsString = "1095923280000";

			try {
				this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug5729");
				this.stmt
						.executeUpdate("CREATE TABLE testBug5729(field1 BIGINT UNSIGNED)");
				this.stmt.executeUpdate("INSERT INTO testBug5729 VALUES ("
						+ valueAsString + ")");

				this.rs = this.conn.prepareStatement(
						"SELECT * FROM testBug5729").executeQuery();
				this.rs.next();

				assertTrue(this.rs.getObject(1).toString()
						.equals(valueAsString));
			} finally {
				this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug5729");
			}
		}
	}

	/**
	 * Tests fix for BUG#8484 - ResultSet.getBigDecimal() throws exception when
	 * rounding would need to occur to set scale.
	 * 
	 * @throws Exception
	 *             if the test fails
	 * @deprecated
	 */
	public void testBug8484() throws Exception {
		try {
			this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug8484");
			this.stmt
					.executeUpdate("CREATE TABLE testBug8484 (field1 DECIMAL(16, 8), field2 varchar(32))");
			this.stmt
					.executeUpdate("INSERT INTO testBug8484 VALUES (12345678.12345678, '')");
			this.rs = this.stmt
					.executeQuery("SELECT field1, field2 FROM testBug8484");
			this.rs.next();
			assertEquals("12345678.123", this.rs.getBigDecimal(1, 3).toString());
			assertEquals("0.000", this.rs.getBigDecimal(2, 3).toString());

			this.pstmt = this.conn
					.prepareStatement("SELECT field1, field2 FROM testBug8484");
			this.rs = this.pstmt.executeQuery();
			this.rs.next();
			assertEquals("12345678.123", this.rs.getBigDecimal(1, 3).toString());
			assertEquals("0.000", this.rs.getBigDecimal(2, 3).toString());
		} finally {
			this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug8484");
		}
	}
}