PageRenderTime 37ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/EQEmuJSM/mysql-connector-java-5.1.13/src/testsuite/regression/MetaDataRegressionTest.java

http://cubbers-eqemu-utils.googlecode.com/
Java | 2200 lines | 1359 code | 375 blank | 466 comment | 185 complexity | 1c36bf4dace643bea4a6c977e290ed70 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. Copyright 2002-2007 MySQL AB, 2008 Sun Microsystems
  3. All rights reserved. Use is subject to license terms.
  4. The MySQL Connector/J is licensed under the terms of the GPL,
  5. like most MySQL Connectors. There are special exceptions to the
  6. terms and conditions of the GPL as it is applied to this software,
  7. see the FLOSS License Exception available on mysql.com.
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation; version 2 of the
  11. License.
  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. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. 02110-1301 USA
  20. */
  21. package testsuite.regression;
  22. import java.lang.reflect.Field;
  23. import java.lang.reflect.Modifier;
  24. import java.sql.Connection;
  25. import java.sql.DatabaseMetaData;
  26. import java.sql.DriverPropertyInfo;
  27. import java.sql.PreparedStatement;
  28. import java.sql.ResultSet;
  29. import java.sql.ResultSetMetaData;
  30. import java.sql.SQLException;
  31. import java.sql.Statement;
  32. import java.sql.Types;
  33. import java.util.HashMap;
  34. import java.util.Map;
  35. import java.util.Properties;
  36. import junit.framework.ComparisonFailure;
  37. import testsuite.BaseTestCase;
  38. import com.mysql.jdbc.Driver;
  39. import com.mysql.jdbc.NonRegisteringDriver;
  40. import com.mysql.jdbc.SQLError;
  41. /**
  42. * Regression tests for DatabaseMetaData
  43. *
  44. * @author Mark Matthews
  45. * @version $Id: MetaDataRegressionTest.java,v 1.1.2.1 2005/05/13 18:58:38
  46. * mmatthews Exp $
  47. */
  48. public class MetaDataRegressionTest extends BaseTestCase {
  49. /**
  50. * Creates a new MetaDataRegressionTest.
  51. *
  52. * @param name
  53. * the name of the test
  54. */
  55. public MetaDataRegressionTest(String name) {
  56. super(name);
  57. }
  58. /**
  59. * Runs all test cases in this test suite
  60. *
  61. * @param args
  62. */
  63. public static void main(String[] args) {
  64. junit.textui.TestRunner.run(MetaDataRegressionTest.class);
  65. }
  66. /**
  67. * DOCUMENT ME!
  68. *
  69. * @throws Exception
  70. * ...
  71. */
  72. public void testBug2607() throws Exception {
  73. try {
  74. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug2607");
  75. this.stmt
  76. .executeUpdate("CREATE TABLE testBug2607 (field1 INT PRIMARY KEY)");
  77. this.rs = this.stmt.executeQuery("SELECT field1 FROM testBug2607");
  78. ResultSetMetaData rsmd = this.rs.getMetaData();
  79. assertTrue(!rsmd.isAutoIncrement(1));
  80. } finally {
  81. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug2607");
  82. }
  83. }
  84. /**
  85. * Tests fix for BUG#2852, where RSMD is not returning correct (or matching)
  86. * types for TINYINT and SMALLINT.
  87. *
  88. * @throws Exception
  89. * if the test fails.
  90. */
  91. public void testBug2852() throws Exception {
  92. try {
  93. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug2852");
  94. this.stmt
  95. .executeUpdate("CREATE TABLE testBug2852 (field1 TINYINT, field2 SMALLINT)");
  96. this.stmt.executeUpdate("INSERT INTO testBug2852 VALUES (1,1)");
  97. this.rs = this.stmt.executeQuery("SELECT * from testBug2852");
  98. assertTrue(this.rs.next());
  99. ResultSetMetaData rsmd = this.rs.getMetaData();
  100. assertTrue(rsmd.getColumnClassName(1).equals(
  101. this.rs.getObject(1).getClass().getName()));
  102. assertTrue("java.lang.Integer".equals(rsmd.getColumnClassName(1)));
  103. assertTrue(rsmd.getColumnClassName(2).equals(
  104. this.rs.getObject(2).getClass().getName()));
  105. assertTrue("java.lang.Integer".equals(rsmd.getColumnClassName(2)));
  106. } finally {
  107. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug2852");
  108. }
  109. }
  110. /**
  111. * Tests fix for BUG#2855, where RSMD is not returning correct (or matching)
  112. * types for FLOAT.
  113. *
  114. * @throws Exception
  115. * if the test fails.
  116. */
  117. public void testBug2855() throws Exception {
  118. try {
  119. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug2855");
  120. this.stmt.executeUpdate("CREATE TABLE testBug2855 (field1 FLOAT)");
  121. this.stmt.executeUpdate("INSERT INTO testBug2855 VALUES (1)");
  122. this.rs = this.stmt.executeQuery("SELECT * from testBug2855");
  123. assertTrue(this.rs.next());
  124. ResultSetMetaData rsmd = this.rs.getMetaData();
  125. assertTrue(rsmd.getColumnClassName(1).equals(
  126. this.rs.getObject(1).getClass().getName()));
  127. assertTrue("java.lang.Float".equals(rsmd.getColumnClassName(1)));
  128. } finally {
  129. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug2855");
  130. }
  131. }
  132. /**
  133. * Tests fix for BUG#3570 -- inconsistent reporting of column type
  134. *
  135. * @throws Exception
  136. * if an error occurs
  137. */
  138. public void testBug3570() throws Exception {
  139. String createTableQuery = " CREATE TABLE testBug3570(field_tinyint TINYINT"
  140. + ",field_smallint SMALLINT"
  141. + ",field_mediumint MEDIUMINT"
  142. + ",field_int INT"
  143. + ",field_integer INTEGER"
  144. + ",field_bigint BIGINT"
  145. + ",field_real REAL"
  146. + ",field_float FLOAT"
  147. + ",field_decimal DECIMAL"
  148. + ",field_numeric NUMERIC"
  149. + ",field_double DOUBLE"
  150. + ",field_char CHAR(3)"
  151. + ",field_varchar VARCHAR(255)"
  152. + ",field_date DATE"
  153. + ",field_time TIME"
  154. + ",field_year YEAR"
  155. + ",field_timestamp TIMESTAMP"
  156. + ",field_datetime DATETIME"
  157. + ",field_tinyblob TINYBLOB"
  158. + ",field_blob BLOB"
  159. + ",field_mediumblob MEDIUMBLOB"
  160. + ",field_longblob LONGBLOB"
  161. + ",field_tinytext TINYTEXT"
  162. + ",field_text TEXT"
  163. + ",field_mediumtext MEDIUMTEXT"
  164. + ",field_longtext LONGTEXT"
  165. + ",field_enum ENUM('1','2','3')"
  166. + ",field_set SET('1','2','3'))";
  167. try {
  168. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3570");
  169. this.stmt.executeUpdate(createTableQuery);
  170. ResultSet dbmdRs = this.conn.getMetaData().getColumns(
  171. this.conn.getCatalog(), null, "testBug3570", "%");
  172. this.rs = this.stmt.executeQuery("SELECT * FROM testBug3570");
  173. ResultSetMetaData rsmd = this.rs.getMetaData();
  174. while (dbmdRs.next()) {
  175. String columnName = dbmdRs.getString(4);
  176. int typeFromGetColumns = dbmdRs.getInt(5);
  177. int typeFromRSMD = rsmd.getColumnType(this.rs
  178. .findColumn(columnName));
  179. //
  180. // TODO: Server needs to send these types correctly....
  181. //
  182. if (!"field_tinyblob".equals(columnName)
  183. && !"field_tinytext".equals(columnName)) {
  184. assertTrue(columnName + " -> type from DBMD.getColumns("
  185. + typeFromGetColumns
  186. + ") != type from RSMD.getColumnType("
  187. + typeFromRSMD + ")",
  188. typeFromGetColumns == typeFromRSMD);
  189. }
  190. }
  191. } finally {
  192. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3570");
  193. }
  194. }
  195. /**
  196. * Tests char/varchar bug
  197. *
  198. * @throws Exception
  199. * if any errors occur
  200. */
  201. public void testCharVarchar() throws Exception {
  202. try {
  203. this.stmt.execute("DROP TABLE IF EXISTS charVarCharTest");
  204. this.stmt.execute("CREATE TABLE charVarCharTest ("
  205. + " TableName VARCHAR(64)," + " FieldName VARCHAR(64),"
  206. + " NextCounter INTEGER);");
  207. String query = "SELECT TableName, FieldName, NextCounter FROM charVarCharTest";
  208. this.rs = this.stmt.executeQuery(query);
  209. ResultSetMetaData rsmeta = this.rs.getMetaData();
  210. assertTrue(rsmeta.getColumnTypeName(1).equalsIgnoreCase("VARCHAR"));
  211. // is "CHAR", expected "VARCHAR"
  212. assertTrue(rsmeta.getColumnType(1) == 12);
  213. // is 1 (java.sql.Types.CHAR), expected 12 (java.sql.Types.VARCHAR)
  214. } finally {
  215. this.stmt.execute("DROP TABLE IF EXISTS charVarCharTest");
  216. }
  217. }
  218. /**
  219. * Tests fix for BUG#1673, where DatabaseMetaData.getColumns() is not
  220. * returning correct column ordinal info for non '%' column name patterns.
  221. *
  222. * @throws Exception
  223. * if the test fails for any reason
  224. */
  225. public void testFixForBug1673() throws Exception {
  226. try {
  227. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug1673");
  228. this.stmt
  229. .executeUpdate("CREATE TABLE testBug1673 (field_1 INT, field_2 INT)");
  230. DatabaseMetaData dbmd = this.conn.getMetaData();
  231. int ordinalPosOfCol2Full = 0;
  232. this.rs = dbmd.getColumns(this.conn.getCatalog(), null,
  233. "testBug1673", null);
  234. while (this.rs.next()) {
  235. if (this.rs.getString(4).equals("field_2")) {
  236. ordinalPosOfCol2Full = this.rs.getInt(17);
  237. }
  238. }
  239. int ordinalPosOfCol2Scoped = 0;
  240. this.rs = dbmd.getColumns(this.conn.getCatalog(), null,
  241. "testBug1673", "field_2");
  242. while (this.rs.next()) {
  243. if (this.rs.getString(4).equals("field_2")) {
  244. ordinalPosOfCol2Scoped = this.rs.getInt(17);
  245. }
  246. }
  247. assertTrue("Ordinal position in full column list of '"
  248. + ordinalPosOfCol2Full
  249. + "' != ordinal position in pattern search, '"
  250. + ordinalPosOfCol2Scoped + "'.",
  251. (ordinalPosOfCol2Full != 0)
  252. && (ordinalPosOfCol2Scoped != 0)
  253. && (ordinalPosOfCol2Scoped == ordinalPosOfCol2Full));
  254. } finally {
  255. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug1673");
  256. }
  257. }
  258. /**
  259. * Tests bug reported by OpenOffice team with getColumns and LONGBLOB
  260. *
  261. * @throws Exception
  262. * if any errors occur
  263. */
  264. public void testGetColumns() throws Exception {
  265. try {
  266. this.stmt
  267. .execute("CREATE TABLE IF NOT EXISTS longblob_regress(field_1 longblob)");
  268. DatabaseMetaData dbmd = this.conn.getMetaData();
  269. ResultSet dbmdRs = null;
  270. try {
  271. dbmdRs = dbmd.getColumns("", "", "longblob_regress", "%");
  272. while (dbmdRs.next()) {
  273. dbmdRs.getInt(7);
  274. }
  275. } finally {
  276. if (dbmdRs != null) {
  277. try {
  278. dbmdRs.close();
  279. } catch (SQLException ex) {
  280. ;
  281. }
  282. }
  283. }
  284. } finally {
  285. this.stmt.execute("DROP TABLE IF EXISTS longblob_regress");
  286. }
  287. }
  288. /**
  289. * Tests fix for Bug#
  290. *
  291. * @throws Exception
  292. * if an error occurs
  293. */
  294. public void testGetColumnsBug1099() throws Exception {
  295. try {
  296. this.stmt
  297. .executeUpdate("DROP TABLE IF EXISTS testGetColumnsBug1099");
  298. DatabaseMetaData dbmd = this.conn.getMetaData();
  299. this.rs = dbmd.getTypeInfo();
  300. StringBuffer types = new StringBuffer();
  301. HashMap alreadyDoneTypes = new HashMap();
  302. while (this.rs.next()) {
  303. String typeName = this.rs.getString("TYPE_NAME");
  304. String createParams = this.rs.getString("CREATE_PARAMS");
  305. if ((typeName.indexOf("BINARY") == -1)
  306. && !typeName.equals("LONG VARCHAR")) {
  307. if (!alreadyDoneTypes.containsKey(typeName)) {
  308. alreadyDoneTypes.put(typeName, null);
  309. if (types.length() != 0) {
  310. types.append(", \n");
  311. }
  312. int typeNameLength = typeName.length();
  313. StringBuffer safeTypeName = new StringBuffer(
  314. typeNameLength);
  315. for (int i = 0; i < typeNameLength; i++) {
  316. char c = typeName.charAt(i);
  317. if (Character.isWhitespace(c)) {
  318. safeTypeName.append("_");
  319. } else {
  320. safeTypeName.append(c);
  321. }
  322. }
  323. types.append(safeTypeName.toString());
  324. types.append("Column ");
  325. types.append(typeName);
  326. if (typeName.indexOf("CHAR") != -1) {
  327. types.append(" (1)");
  328. } else if (typeName.equalsIgnoreCase("enum")
  329. || typeName.equalsIgnoreCase("set")) {
  330. types.append("('a', 'b', 'c')");
  331. }
  332. }
  333. }
  334. }
  335. this.stmt.executeUpdate("CREATE TABLE testGetColumnsBug1099("
  336. + types.toString() + ")");
  337. dbmd.getColumns(null, this.conn.getCatalog(),
  338. "testGetColumnsBug1099", "%");
  339. } finally {
  340. this.stmt
  341. .executeUpdate("DROP TABLE IF EXISTS testGetColumnsBug1099");
  342. }
  343. }
  344. /**
  345. * Tests whether or not unsigned columns are reported correctly in
  346. * DBMD.getColumns
  347. *
  348. * @throws Exception
  349. */
  350. public void testGetColumnsUnsigned() throws Exception {
  351. try {
  352. this.stmt.executeUpdate("DROP TABLE IF EXISTS testGetUnsignedCols");
  353. this.stmt
  354. .executeUpdate("CREATE TABLE testGetUnsignedCols (field1 BIGINT, field2 BIGINT UNSIGNED)");
  355. DatabaseMetaData dbmd = this.conn.getMetaData();
  356. this.rs = dbmd.getColumns(this.conn.getCatalog(), null,
  357. "testGetUnsignedCols", "%");
  358. assertTrue(this.rs.next());
  359. // This row doesn't have 'unsigned' attribute
  360. assertTrue(this.rs.next());
  361. assertTrue(this.rs.getString(6).toLowerCase().indexOf("unsigned") != -1);
  362. } finally {
  363. this.stmt.executeUpdate("DROP TABLE IF EXISTS testGetUnsignedCols");
  364. }
  365. }
  366. /**
  367. * Tests whether bogus parameters break Driver.getPropertyInfo().
  368. *
  369. * @throws Exception
  370. * if an error occurs.
  371. */
  372. public void testGetPropertyInfo() throws Exception {
  373. new Driver().getPropertyInfo("", null);
  374. }
  375. /**
  376. * Tests whether ResultSetMetaData returns correct info for CHAR/VARCHAR
  377. * columns.
  378. *
  379. * @throws Exception
  380. * if the test fails
  381. */
  382. public void testIsCaseSensitive() throws Exception {
  383. try {
  384. this.stmt.executeUpdate("DROP TABLE IF EXISTS testIsCaseSensitive");
  385. this.stmt
  386. .executeUpdate("CREATE TABLE testIsCaseSensitive (bin_char CHAR(1) BINARY, bin_varchar VARCHAR(64) BINARY, ci_char CHAR(1), ci_varchar VARCHAR(64))");
  387. this.rs = this.stmt
  388. .executeQuery("SELECT bin_char, bin_varchar, ci_char, ci_varchar FROM testIsCaseSensitive");
  389. ResultSetMetaData rsmd = this.rs.getMetaData();
  390. assertTrue(rsmd.isCaseSensitive(1));
  391. assertTrue(rsmd.isCaseSensitive(2));
  392. assertTrue(!rsmd.isCaseSensitive(3));
  393. assertTrue(!rsmd.isCaseSensitive(4));
  394. } finally {
  395. this.stmt.executeUpdate("DROP TABLE IF EXISTS testIsCaseSensitive");
  396. }
  397. if (versionMeetsMinimum(4, 1)) {
  398. try {
  399. this.stmt
  400. .executeUpdate("DROP TABLE IF EXISTS testIsCaseSensitiveCs");
  401. this.stmt
  402. .executeUpdate("CREATE TABLE testIsCaseSensitiveCs ("
  403. + "bin_char CHAR(1) CHARACTER SET latin1 COLLATE latin1_general_cs,"
  404. + "bin_varchar VARCHAR(64) CHARACTER SET latin1 COLLATE latin1_general_cs,"
  405. + "ci_char CHAR(1) CHARACTER SET latin1 COLLATE latin1_general_ci,"
  406. + "ci_varchar VARCHAR(64) CHARACTER SET latin1 COLLATE latin1_general_ci, "
  407. + "bin_tinytext TINYTEXT CHARACTER SET latin1 COLLATE latin1_general_cs,"
  408. + "bin_text TEXT CHARACTER SET latin1 COLLATE latin1_general_cs,"
  409. + "bin_med_text MEDIUMTEXT CHARACTER SET latin1 COLLATE latin1_general_cs,"
  410. + "bin_long_text LONGTEXT CHARACTER SET latin1 COLLATE latin1_general_cs,"
  411. + "ci_tinytext TINYTEXT CHARACTER SET latin1 COLLATE latin1_general_ci,"
  412. + "ci_text TEXT CHARACTER SET latin1 COLLATE latin1_general_ci,"
  413. + "ci_med_text MEDIUMTEXT CHARACTER SET latin1 COLLATE latin1_general_ci,"
  414. + "ci_long_text LONGTEXT CHARACTER SET latin1 COLLATE latin1_general_ci)");
  415. this.rs = this.stmt
  416. .executeQuery("SELECT bin_char, bin_varchar, ci_char, ci_varchar, bin_tinytext, bin_text, bin_med_text, bin_long_text, ci_tinytext, ci_text, ci_med_text, ci_long_text FROM testIsCaseSensitiveCs");
  417. ResultSetMetaData rsmd = this.rs.getMetaData();
  418. assertTrue(rsmd.isCaseSensitive(1));
  419. assertTrue(rsmd.isCaseSensitive(2));
  420. assertTrue(!rsmd.isCaseSensitive(3));
  421. assertTrue(!rsmd.isCaseSensitive(4));
  422. assertTrue(rsmd.isCaseSensitive(5));
  423. assertTrue(rsmd.isCaseSensitive(6));
  424. assertTrue(rsmd.isCaseSensitive(7));
  425. assertTrue(rsmd.isCaseSensitive(8));
  426. assertTrue(!rsmd.isCaseSensitive(9));
  427. assertTrue(!rsmd.isCaseSensitive(10));
  428. assertTrue(!rsmd.isCaseSensitive(11));
  429. assertTrue(!rsmd.isCaseSensitive(12));
  430. } finally {
  431. this.stmt
  432. .executeUpdate("DROP TABLE IF EXISTS testIsCaseSensitiveCs");
  433. }
  434. }
  435. }
  436. /**
  437. * Tests whether or not DatabaseMetaData.getColumns() returns the correct
  438. * java.sql.Types info.
  439. *
  440. * @throws Exception
  441. * if the test fails.
  442. */
  443. public void testLongText() throws Exception {
  444. try {
  445. this.stmt.executeUpdate("DROP TABLE IF EXISTS testLongText");
  446. this.stmt
  447. .executeUpdate("CREATE TABLE testLongText (field1 LONGTEXT)");
  448. this.rs = this.conn.getMetaData().getColumns(
  449. this.conn.getCatalog(), null, "testLongText", "%");
  450. assertTrue(this.rs.next());
  451. assertTrue(this.rs.getInt("DATA_TYPE") == java.sql.Types.LONGVARCHAR);
  452. } finally {
  453. this.stmt.executeUpdate("DROP TABLE IF EXISTS testLongText");
  454. }
  455. }
  456. /**
  457. * Tests for types being returned correctly
  458. *
  459. * @throws Exception
  460. * if an error occurs.
  461. */
  462. public void testTypes() throws Exception {
  463. try {
  464. this.stmt.execute("DROP TABLE IF EXISTS typesRegressTest");
  465. this.stmt.execute("CREATE TABLE typesRegressTest ("
  466. + "varcharField VARCHAR(32)," + "charField CHAR(2),"
  467. + "enumField ENUM('1','2'),"
  468. + "setField SET('1','2','3')," + "tinyblobField TINYBLOB,"
  469. + "mediumBlobField MEDIUMBLOB," + "longblobField LONGBLOB,"
  470. + "blobField BLOB)");
  471. this.rs = this.stmt.executeQuery("SELECT * from typesRegressTest");
  472. ResultSetMetaData rsmd = this.rs.getMetaData();
  473. int numCols = rsmd.getColumnCount();
  474. for (int i = 0; i < numCols; i++) {
  475. String columnName = rsmd.getColumnName(i + 1);
  476. String columnTypeName = rsmd.getColumnTypeName(i + 1);
  477. System.out.println(columnName + " -> " + columnTypeName);
  478. }
  479. } finally {
  480. this.stmt.execute("DROP TABLE IF EXISTS typesRegressTest");
  481. }
  482. }
  483. /**
  484. * Tests fix for BUG#4742, 'DOUBLE' mapped twice in getTypeInfo().
  485. *
  486. * @throws Exception
  487. * if the test fails.
  488. */
  489. public void testBug4742() throws Exception {
  490. HashMap clashMap = new HashMap();
  491. this.rs = this.conn.getMetaData().getTypeInfo();
  492. while (this.rs.next()) {
  493. String name = this.rs.getString(1);
  494. assertTrue("Type represented twice in type info, '" + name + "'.",
  495. !clashMap.containsKey(name));
  496. clashMap.put(name, name);
  497. }
  498. }
  499. /**
  500. * Tests fix for BUG#4138, getColumns() returns incorrect JDBC type for
  501. * unsigned columns.
  502. *
  503. * @throws Exception
  504. * if the test fails.
  505. */
  506. public void testBug4138() throws Exception {
  507. try {
  508. String[] typesToTest = new String[] { "TINYINT", "SMALLINT",
  509. "MEDIUMINT", "INT", "BIGINT", "FLOAT", "DOUBLE",
  510. "DECIMAL" };
  511. short[] jdbcMapping = new short[] { Types.TINYINT, Types.SMALLINT,
  512. Types.INTEGER, Types.INTEGER, Types.BIGINT, Types.REAL,
  513. Types.DOUBLE, Types.DECIMAL };
  514. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug4138");
  515. StringBuffer createBuf = new StringBuffer();
  516. createBuf.append("CREATE TABLE testBug4138 (");
  517. boolean firstColumn = true;
  518. for (int i = 0; i < typesToTest.length; i++) {
  519. if (!firstColumn) {
  520. createBuf.append(", ");
  521. } else {
  522. firstColumn = false;
  523. }
  524. createBuf.append("field");
  525. createBuf.append((i + 1));
  526. createBuf.append(" ");
  527. createBuf.append(typesToTest[i]);
  528. createBuf.append(" UNSIGNED");
  529. }
  530. createBuf.append(")");
  531. this.stmt.executeUpdate(createBuf.toString());
  532. DatabaseMetaData dbmd = this.conn.getMetaData();
  533. this.rs = dbmd.getColumns(this.conn.getCatalog(), null,
  534. "testBug4138", "field%");
  535. assertTrue(this.rs.next());
  536. for (int i = 0; i < typesToTest.length; i++) {
  537. assertTrue(
  538. "JDBC Data Type of "
  539. + this.rs.getShort("DATA_TYPE")
  540. + " for MySQL type '"
  541. + this.rs.getString("TYPE_NAME")
  542. + "' from 'DATA_TYPE' column does not match expected value of "
  543. + jdbcMapping[i] + ".",
  544. jdbcMapping[i] == this.rs.getShort("DATA_TYPE"));
  545. this.rs.next();
  546. }
  547. this.rs.close();
  548. StringBuffer queryBuf = new StringBuffer("SELECT ");
  549. firstColumn = true;
  550. for (int i = 0; i < typesToTest.length; i++) {
  551. if (!firstColumn) {
  552. queryBuf.append(", ");
  553. } else {
  554. firstColumn = false;
  555. }
  556. queryBuf.append("field");
  557. queryBuf.append((i + 1));
  558. }
  559. queryBuf.append(" FROM testBug4138");
  560. this.rs = this.stmt.executeQuery(queryBuf.toString());
  561. ResultSetMetaData rsmd = this.rs.getMetaData();
  562. for (int i = 0; i < typesToTest.length; i++) {
  563. assertTrue(jdbcMapping[i] == rsmd.getColumnType(i + 1));
  564. String desiredTypeName = typesToTest[i] + " unsigned";
  565. assertTrue(rsmd.getColumnTypeName((i + 1)) + " != "
  566. + desiredTypeName, desiredTypeName
  567. .equalsIgnoreCase(rsmd.getColumnTypeName(i + 1)));
  568. }
  569. } finally {
  570. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug4138");
  571. }
  572. }
  573. /**
  574. * Here for housekeeping only, the test is actually in testBug4138().
  575. *
  576. * @throws Exception
  577. * if the test fails.
  578. */
  579. public void testBug4860() throws Exception {
  580. testBug4138();
  581. }
  582. /**
  583. * Tests fix for BUG#4880 - RSMD.getPrecision() returns '0' for non-numeric
  584. * types.
  585. *
  586. * Why-oh-why is this not in the spec, nor the api-docs, but in some
  587. * 'optional' book, _and_ it is a variance from both ODBC and the ANSI SQL
  588. * standard :p
  589. *
  590. * (from the CTS testsuite)....
  591. *
  592. * The getPrecision(int colindex) method returns an integer value
  593. * representing the number of decimal digits for number types,maximum length
  594. * in characters for character types,maximum length in bytes for JDBC binary
  595. * datatypes.
  596. *
  597. * (See Section 27.3 of JDBC 2.0 API Reference & Tutorial 2nd edition)
  598. *
  599. * @throws Exception
  600. * if the test fails.
  601. */
  602. public void testBug4880() throws Exception {
  603. try {
  604. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug4880");
  605. this.stmt
  606. .executeUpdate("CREATE TABLE testBug4880 (field1 VARCHAR(80), field2 TINYBLOB, field3 BLOB, field4 MEDIUMBLOB, field5 LONGBLOB)");
  607. this.rs = this.stmt
  608. .executeQuery("SELECT field1, field2, field3, field4, field5 FROM testBug4880");
  609. ResultSetMetaData rsmd = this.rs.getMetaData();
  610. assertEquals(80, rsmd.getPrecision(1));
  611. assertEquals(Types.VARCHAR, rsmd.getColumnType(1));
  612. assertEquals(80, rsmd.getColumnDisplaySize(1));
  613. assertEquals(255, rsmd.getPrecision(2));
  614. assertEquals(Types.VARBINARY, rsmd.getColumnType(2));
  615. assertTrue("TINYBLOB".equalsIgnoreCase(rsmd.getColumnTypeName(2)));
  616. assertEquals(255, rsmd.getColumnDisplaySize(2));
  617. assertEquals(65535, rsmd.getPrecision(3));
  618. assertEquals(Types.LONGVARBINARY, rsmd.getColumnType(3));
  619. assertTrue("BLOB".equalsIgnoreCase(rsmd.getColumnTypeName(3)));
  620. assertEquals(65535, rsmd.getColumnDisplaySize(3));
  621. assertEquals(16777215, rsmd.getPrecision(4));
  622. assertEquals(Types.LONGVARBINARY, rsmd.getColumnType(4));
  623. assertTrue("MEDIUMBLOB".equalsIgnoreCase(rsmd.getColumnTypeName(4)));
  624. assertEquals(16777215, rsmd.getColumnDisplaySize(4));
  625. if (versionMeetsMinimum(4, 1)) {
  626. // Server doesn't send us enough information to detect LONGBLOB
  627. // type
  628. assertEquals(Integer.MAX_VALUE, rsmd.getPrecision(5));
  629. assertEquals(Types.LONGVARBINARY, rsmd.getColumnType(5));
  630. assertTrue("LONGBLOB".equalsIgnoreCase(rsmd
  631. .getColumnTypeName(5)));
  632. assertEquals(Integer.MAX_VALUE, rsmd.getColumnDisplaySize(5));
  633. }
  634. } finally {
  635. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug4880");
  636. }
  637. }
  638. /**
  639. * Tests fix for BUG#6399, ResultSetMetaData.getDisplaySize() is wrong for
  640. * multi-byte charsets.
  641. *
  642. * @throws Exception
  643. * if the test fails
  644. */
  645. public void testBug6399() throws Exception {
  646. if (versionMeetsMinimum(4, 1)) {
  647. try {
  648. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug6399");
  649. this.stmt
  650. .executeUpdate("CREATE TABLE testBug6399 (field1 CHAR(3) CHARACTER SET UTF8, field2 CHAR(3) CHARACTER SET LATIN1, field3 CHAR(3) CHARACTER SET SJIS)");
  651. this.stmt
  652. .executeUpdate("INSERT INTO testBug6399 VALUES ('a', 'a', 'a')");
  653. this.rs = this.stmt
  654. .executeQuery("SELECT field1, field2, field3 FROM testBug6399");
  655. ResultSetMetaData rsmd = this.rs.getMetaData();
  656. assertEquals(3, rsmd.getColumnDisplaySize(1));
  657. assertEquals(3, rsmd.getColumnDisplaySize(2));
  658. assertEquals(3, rsmd.getColumnDisplaySize(3));
  659. } finally {
  660. this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug6399");
  661. }
  662. }
  663. }
  664. /**
  665. * Tests fix for BUG#7081, DatabaseMetaData.getIndexInfo() ignoring 'unique'
  666. * parameters.
  667. *
  668. * @throws Exception
  669. * if the test fails.
  670. */
  671. public void testBug7081() throws Exception {
  672. String tableName = "testBug7081";
  673. try {
  674. createTable(tableName, "(field1 INT, INDEX(field1))");
  675. DatabaseMetaData dbmd = this.conn.getMetaData();
  676. this.rs = dbmd.getIndexInfo(this.conn.getCatalog(), null,
  677. tableName, true, false);
  678. assertTrue(!this.rs.next()); // there should be no rows that meet
  679. // this requirement
  680. this.rs = dbmd.getIndexInfo(this.conn.getCatalog(), null,
  681. tableName, false, false);
  682. assertTrue(this.rs.next()); // there should be one row that meets
  683. // this requirement
  684. assertTrue(!this.rs.next());
  685. } finally {
  686. dropTable(tableName);
  687. }
  688. }
  689. /**
  690. * Tests fix for BUG#7033 - PreparedStatements don't encode Big5 (and other
  691. * multibyte) character sets correctly in static SQL strings.
  692. *
  693. * @throws Exception
  694. * if the test fails.
  695. */
  696. public void testBug7033() throws Exception {
  697. if (false) { // disabled for now
  698. Connection big5Conn = null;
  699. Statement big5Stmt = null;
  700. PreparedStatement big5PrepStmt = null;
  701. String testString = "\u5957 \u9910";
  702. try {
  703. Properties props = new Properties();
  704. props.setProperty("useUnicode", "true");
  705. props.setProperty("characterEncoding", "Big5");
  706. big5Conn = getConnectionWithProps(props);
  707. big5Stmt = big5Conn.createStatement();
  708. byte[] foobar = testString.getBytes("Big5");
  709. System.out.println(foobar);
  710. this.rs = big5Stmt.executeQuery("select 1 as '\u5957 \u9910'");
  711. String retrString = this.rs.getMetaData().getColumnName(1);
  712. assertTrue(testString.equals(retrString));
  713. big5PrepStmt = big5Conn
  714. .prepareStatement("select 1 as '\u5957 \u9910'");
  715. this.rs = big5PrepStmt.executeQuery();
  716. retrString = this.rs.getMetaData().getColumnName(1);
  717. assertTrue(testString.equals(retrString));
  718. } finally {
  719. if (this.rs != null) {
  720. this.rs.close();
  721. this.rs = null;
  722. }
  723. if (big5Stmt != null) {
  724. big5Stmt.close();
  725. }
  726. if (big5PrepStmt != null) {
  727. big5PrepStmt.close();
  728. }
  729. if (big5Conn != null) {
  730. big5Conn.close();
  731. }
  732. }
  733. }
  734. }
  735. /**
  736. * Tests fix for Bug#8812, DBMD.getIndexInfo() returning inverted values for
  737. * 'NON_UNIQUE' column.
  738. *
  739. * @throws Exception
  740. * if the test fails.
  741. */
  742. public void testBug8812() throws Exception {
  743. String tableName = "testBug8812";
  744. try {
  745. createTable(tableName,
  746. "(field1 INT, field2 INT, INDEX(field1), UNIQUE INDEX(field2))");
  747. DatabaseMetaData dbmd = this.conn.getMetaData();
  748. this.rs = dbmd.getIndexInfo(this.conn.getCatalog(), null,
  749. tableName, true, false);
  750. assertTrue(this.rs.next()); // there should be one row that meets
  751. // this requirement
  752. assertEquals(this.rs.getBoolean("NON_UNIQUE"), false);
  753. this.rs = dbmd.getIndexInfo(this.conn.getCatalog(), null,
  754. tableName, false, false);
  755. assertTrue(this.rs.next()); // there should be two rows that meets
  756. // this requirement
  757. assertEquals(this.rs.getBoolean("NON_UNIQUE"), false);
  758. assertTrue(this.rs.next());
  759. assertEquals(this.rs.getBoolean("NON_UNIQUE"), true);
  760. } finally {
  761. dropTable(tableName);
  762. }
  763. }
  764. /**
  765. * Tests fix for BUG#8800 - supportsMixedCase*Identifiers() returns wrong
  766. * value on servers running on case-sensitive filesystems.
  767. */
  768. public void testBug8800() throws Exception {
  769. assertEquals(((com.mysql.jdbc.Connection) this.conn)
  770. .lowerCaseTableNames(), !this.conn.getMetaData()
  771. .supportsMixedCaseIdentifiers());
  772. assertEquals(((com.mysql.jdbc.Connection) this.conn)
  773. .lowerCaseTableNames(), !this.conn.getMetaData()
  774. .supportsMixedCaseQuotedIdentifiers());
  775. }
  776. /**
  777. * Tests fix for BUG#8792 - DBMD.supportsResultSetConcurrency() not
  778. * returning true for forward-only/read-only result sets (we obviously
  779. * support this).
  780. *
  781. * @throws Exception
  782. * if the test fails.
  783. */
  784. public void testBug8792() throws Exception {
  785. DatabaseMetaData dbmd = this.conn.getMetaData();
  786. assertTrue(dbmd.supportsResultSetConcurrency(
  787. ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY));
  788. assertTrue(dbmd.supportsResultSetConcurrency(
  789. ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE));
  790. assertTrue(dbmd.supportsResultSetConcurrency(
  791. ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY));
  792. assertTrue(dbmd.supportsResultSetConcurrency(
  793. ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE));
  794. assertTrue(!dbmd.supportsResultSetConcurrency(
  795. ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY));
  796. assertTrue(!dbmd.supportsResultSetConcurrency(
  797. ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE));
  798. // Check error conditions
  799. try {
  800. dbmd.supportsResultSetConcurrency(ResultSet.TYPE_FORWARD_ONLY,
  801. Integer.MIN_VALUE);
  802. fail("Exception should've been raised for bogus concurrency value");
  803. } catch (SQLException sqlEx) {
  804. assertTrue(SQLError.SQL_STATE_ILLEGAL_ARGUMENT.equals(sqlEx
  805. .getSQLState()));
  806. }
  807. try {
  808. assertTrue(dbmd.supportsResultSetConcurrency(
  809. ResultSet.TYPE_SCROLL_INSENSITIVE, Integer.MIN_VALUE));
  810. fail("Exception should've been raised for bogus concurrency value");
  811. } catch (SQLException sqlEx) {
  812. assertTrue(SQLError.SQL_STATE_ILLEGAL_ARGUMENT.equals(sqlEx
  813. .getSQLState()));
  814. }
  815. try {
  816. assertTrue(dbmd.supportsResultSetConcurrency(Integer.MIN_VALUE,
  817. Integer.MIN_VALUE));
  818. fail("Exception should've been raised for bogus concurrency value");
  819. } catch (SQLException sqlEx) {
  820. assertTrue(SQLError.SQL_STATE_ILLEGAL_ARGUMENT.equals(sqlEx
  821. .getSQLState()));
  822. }
  823. }
  824. /**
  825. * Tests fix for BUG#8803, 'DATA_TYPE' column from
  826. * DBMD.getBestRowIdentifier() causes ArrayIndexOutOfBoundsException when
  827. * accessed (and in fact, didn't return any value).
  828. *
  829. * @throws Exception
  830. * if the test fails.
  831. */
  832. public void testBug8803() throws Exception {
  833. String tableName = "testBug8803";
  834. createTable(tableName, "(field1 INT NOT NULL PRIMARY KEY)");
  835. DatabaseMetaData metadata = this.conn.getMetaData();
  836. try {
  837. this.rs = metadata.getBestRowIdentifier(this.conn.getCatalog(),
  838. null, tableName, DatabaseMetaData.bestRowNotPseudo, true);
  839. assertTrue(this.rs.next());
  840. this.rs.getInt("DATA_TYPE"); // **** Fails here *****
  841. } finally {
  842. if (this.rs != null) {
  843. this.rs.close();
  844. this.rs = null;
  845. }
  846. }
  847. }
  848. /**
  849. * Tests fix for BUG#9320 - PreparedStatement.getMetaData() inserts blank
  850. * row in database under certain conditions when not using server-side
  851. * prepared statements.
  852. *
  853. * @throws Exception
  854. * if the test fails.
  855. */
  856. public void testBug9320() throws Exception {
  857. createTable("testBug9320", "(field1 int)");
  858. testAbsenceOfMetadataForQuery("INSERT INTO testBug9320 VALUES (?)");
  859. testAbsenceOfMetadataForQuery("UPDATE testBug9320 SET field1=?");
  860. testAbsenceOfMetadataForQuery("DELETE FROM testBug9320 WHERE field1=?");
  861. }
  862. /**
  863. * Tests fix for BUG#9778, DBMD.getTables() shouldn't return tables if views
  864. * are asked for, even if the database version doesn't support views.
  865. *
  866. * @throws Exception
  867. * if the test fails.
  868. */
  869. public void testBug9778() throws Exception {
  870. String tableName = "testBug9778";
  871. try {
  872. createTable(tableName, "(field1 int)");
  873. this.rs = this.conn.getMetaData().getTables(null, null, tableName,
  874. new String[] { "VIEW" });
  875. assertEquals(false, this.rs.next());
  876. this.rs = this.conn.getMetaData().getTables(null, null, tableName,
  877. new String[] { "TABLE" });
  878. assertEquals(true, this.rs.next());
  879. } finally {
  880. if (this.rs != null) {
  881. this.rs.close();
  882. this.rs = null;
  883. }
  884. }
  885. }
  886. /**
  887. * Tests fix for BUG#9769 - Should accept null for procedureNamePattern,
  888. * even though it isn't JDBC compliant, for legacy's sake.
  889. *
  890. * @throws Exception
  891. * if the test fails.
  892. */
  893. public void testBug9769() throws Exception {
  894. boolean defaultPatternConfig = ((com.mysql.jdbc.Connection) this.conn)
  895. .getNullNamePatternMatchesAll();
  896. // We're going to change this in 3.2.x, so make that test here, so we
  897. // catch it.
  898. if (this.conn.getMetaData().getDriverMajorVersion() == 3
  899. && this.conn.getMetaData().getDriverMinorVersion() >= 2) {
  900. assertEquals(false, defaultPatternConfig);
  901. } else {
  902. assertEquals(true, defaultPatternConfig);
  903. }
  904. try {
  905. this.conn.getMetaData().getProcedures(this.conn.getCatalog(), "%",
  906. null);
  907. if (!defaultPatternConfig) {
  908. // we shouldn't have gotten here
  909. fail("Exception should've been thrown");
  910. }
  911. } catch (SQLException sqlEx) {
  912. if (!defaultPatternConfig) {
  913. assertEquals(SQLError.SQL_STATE_ILLEGAL_ARGUMENT, sqlEx
  914. .getSQLState());
  915. } else {
  916. throw sqlEx; // we shouldn't have gotten an exception here
  917. }
  918. }
  919. // FIXME: TO test for 3.1.9
  920. // getColumns();
  921. // getTablePrivileges();
  922. // getTables();
  923. }
  924. /**
  925. * Tests fix for BUG#9917 - Should accept null for catalog in DBMD methods,
  926. * even though it's not JDBC-compliant for legacy's sake.
  927. *
  928. * @throws Exception
  929. * if the test fails.
  930. */
  931. public void testBug9917() throws Exception {
  932. String tableName = "testBug9917";
  933. boolean defaultCatalogConfig = ((com.mysql.jdbc.Connection) this.conn)
  934. .getNullCatalogMeansCurrent();
  935. // We're going to change this in 3.2.x, so make that test here, so we
  936. // catch it.
  937. if (this.conn.getMetaData().getDriverMajorVersion() == 3
  938. && this.conn.getMetaData().getDriverMinorVersion() >= 2) {
  939. assertEquals(false, defaultCatalogConfig);
  940. } else {
  941. assertEquals(true, defaultCatalogConfig);
  942. }
  943. try {
  944. createTable(tableName, "(field1 int)");
  945. String currentCatalog = this.conn.getCatalog();
  946. try {
  947. this.rs = this.conn.getMetaData().getTables(null, null,
  948. tableName, new String[] { "TABLE" });
  949. if (!defaultCatalogConfig) {
  950. // we shouldn't have gotten here
  951. fail("Exception should've been thrown");
  952. }
  953. assertEquals(true, this.rs.next());
  954. assertEquals(currentCatalog, this.rs.getString("TABLE_CAT"));
  955. // FIXME: Methods to test for 3.1.9
  956. //
  957. // getBestRowIdentifier()
  958. // getColumns()
  959. // getCrossReference()
  960. // getExportedKeys()
  961. // getImportedKeys()
  962. // getIndexInfo()
  963. // getPrimaryKeys()
  964. // getProcedures()
  965. } catch (SQLException sqlEx) {
  966. if (!defaultCatalogConfig) {
  967. assertEquals(SQLError.SQL_STATE_ILLEGAL_ARGUMENT, sqlEx
  968. .getSQLState());
  969. } else {
  970. throw sqlEx; // we shouldn't have gotten an exception
  971. // here
  972. }
  973. }
  974. } finally {
  975. if (this.rs != null) {
  976. this.rs.close();
  977. this.rs = null;
  978. }
  979. }
  980. }
  981. /**
  982. * Tests fix for BUG#11575 -- DBMD.storesLower/Mixed/UpperIdentifiers()
  983. * reports incorrect values for servers deployed on Windows.
  984. *
  985. * @throws Exception
  986. * if the test fails.
  987. */
  988. public void testBug11575() throws Exception {
  989. DatabaseMetaData dbmd = this.conn.getMetaData();
  990. if (isServerRunningOnWindows()) {
  991. assertEquals(true, dbmd.storesLowerCaseIdentifiers());
  992. assertEquals(true, dbmd.storesLowerCaseQuotedIdentifiers());
  993. assertEquals(false, dbmd.storesMixedCaseIdentifiers());
  994. assertEquals(false, dbmd.storesMixedCaseQuotedIdentifiers());
  995. assertEquals(false, dbmd.storesUpperCaseIdentifiers());
  996. assertEquals(true, dbmd.storesUpperCaseQuotedIdentifiers());
  997. } else {
  998. assertEquals(false, dbmd.storesLowerCaseIdentifiers());
  999. assertEquals(false, dbmd.storesLowerCaseQuotedIdentifiers());
  1000. assertEquals(true, dbmd.storesMixedCaseIdentifiers());
  1001. assertEquals(true, dbmd.storesMixedCaseQuotedIdentifiers());
  1002. assertEquals(false, dbmd.storesUpperCaseIdentifiers());
  1003. assertEquals(true, dbmd.storesUpperCaseQuotedIdentifiers());
  1004. }
  1005. }
  1006. /**
  1007. * Tests fix for BUG#11781, foreign key information that is quoted is parsed
  1008. * incorrectly.
  1009. */
  1010. public void testBug11781() throws Exception {
  1011. if (versionMeetsMinimum(5, 1)) {
  1012. if (!versionMeetsMinimum(5, 2)) {
  1013. // server bug prevents this test from functioning
  1014. return;
  1015. }
  1016. }
  1017. createTable(
  1018. "`app tab`",
  1019. "( C1 int(11) NULL, INDEX NEWINX (C1), INDEX NEWINX2 (C1))", "InnoDB");
  1020. this.stmt
  1021. .executeUpdate("ALTER TABLE `app tab` ADD CONSTRAINT APPFK FOREIGN KEY (C1) REFERENCES `app tab` (C1)");
  1022. /*
  1023. * this.rs = this.conn.getMetaData().getCrossReference(
  1024. * this.conn.getCatalog(), null, "app tab", this.conn.getCatalog(),
  1025. * null, "app tab");
  1026. */
  1027. this.rs = ((com.mysql.jdbc.DatabaseMetaData) this.conn.getMetaData())
  1028. .extractForeignKeyFromCreateTable(this.conn.getCatalog(),
  1029. "app tab");
  1030. assertTrue("must return a row", this.rs.next());
  1031. String catalog = this.conn.getCatalog();
  1032. assertEquals("comment; APPFK(`C1`) REFER `" + catalog
  1033. + "`/ `app tab` (`C1`)", this.rs.getString(3));
  1034. this.rs.close();
  1035. this.rs = this.conn.getMetaData().getImportedKeys(
  1036. this.conn.getCatalog(), null, "app tab");
  1037. assertTrue(this.rs.next());
  1038. this.rs = this.conn.getMetaData().getExportedKeys(
  1039. this.conn.getCatalog(), null, "app tab");
  1040. assertTrue(this.rs.next());
  1041. }
  1042. /**
  1043. * Tests fix for BUG#12970 - java.sql.Types.OTHER returned for binary and
  1044. * varbinary columns.
  1045. *
  1046. */
  1047. public void testBug12970() throws Exception {
  1048. if (versionMeetsMinimum(5, 0, 8)) {
  1049. String tableName = "testBug12970";
  1050. createTable(tableName,
  1051. "(binary_field BINARY(32), varbinary_field VARBINARY(64))");
  1052. try {
  1053. this.rs = this.conn.getMetaData().getColumns(
  1054. this.conn.getCatalog(), null, tableName, "%");
  1055. assertTrue(this.rs.next());
  1056. assertEquals(Types.BINARY, this.rs.getInt("DATA_TYPE"));
  1057. assertEquals(32, this.rs.getInt("COLUMN_SIZE"));
  1058. assertTrue(this.rs.next());
  1059. assertEquals(Types.VARBINARY, this.rs.getInt("DATA_TYPE"));
  1060. assertEquals(64, this.rs.getInt("COLUMN_SIZE"));
  1061. this.rs.close();
  1062. this.rs = this.stmt
  1063. .executeQuery("SELECT binary_field, varbinary_field FROM "
  1064. + tableName);
  1065. ResultSetMetaData rsmd = this.rs.getMetaData();
  1066. assertEquals(Types.BINARY, rsmd.getColumnType(1));
  1067. assertEquals(32, rsmd.getPrecision(1));
  1068. assertEquals(Types.VARBINARY, rsmd.getColumnType(2));
  1069. assertEquals(64, rsmd.getPrecision(2));
  1070. this.rs.close();
  1071. } finally {
  1072. if (this.rs != null) {
  1073. this.rs.close();
  1074. }
  1075. }
  1076. }
  1077. }
  1078. /**
  1079. * Tests fix for BUG#12975 - OpenOffice expects DBMD.supportsIEF() to return
  1080. * "true" if foreign keys are supported by the datasource, even though this
  1081. * method also covers support for check constraints, which MySQL _doesn't_
  1082. * have.
  1083. *
  1084. * @throws Exception
  1085. * if the test fails.
  1086. */
  1087. public void testBug12975() throws Exception {
  1088. assertEquals(false, this.conn.getMetaData()
  1089. .supportsIntegrityEnhancementFacility());
  1090. Connection overrideConn = null;
  1091. try {
  1092. Properties props = new Properties();
  1093. props.setProperty("overrideSupportsIntegrityEnhancementFacility",
  1094. "true");
  1095. overrideConn = getConnectionWithProps(props);
  1096. assertEquals(true, overrideConn.getMetaData()
  1097. .supportsIntegrityEnhancementFacility());
  1098. } finally {
  1099. if (overrideConn != null) {
  1100. overrideConn.close();
  1101. }
  1102. }
  1103. }
  1104. /**
  1105. * Tests fix for BUG#13277 - RSMD for generated keys has NPEs when a
  1106. * connection is referenced.
  1107. *
  1108. * @throws Exception
  1109. */
  1110. public void testBug13277() throws Exception {
  1111. if (isRunningOnJdk131()) {
  1112. return; // test not valid on JDK-1.3.1
  1113. }
  1114. createTable("testBug13277",
  1115. "(field1 INT NOT NULL PRIMARY KEY AUTO_INCREMENT, field2 VARCHAR(32))");
  1116. try {
  1117. this.stmt.executeUpdate(
  1118. "INSERT INTO testBug13277 (field2) VALUES ('abcdefg')",
  1119. Statement.RETURN_GENERATED_KEYS);
  1120. this.rs = this.stmt.getGeneratedKeys();
  1121. ResultSetMetaData rsmd = this.rs.getMetaData();
  1122. checkRsmdForBug13277(rsmd);
  1123. this.rs.close();
  1124. for (int i = 0; i < 5; i++) {
  1125. this.stmt
  1126. .addBatch("INSERT INTO testBug13277 (field2) VALUES ('abcdefg')");
  1127. }
  1128. this.stmt.executeBatch();
  1129. this.rs = this.stmt.getGeneratedKeys();
  1130. rsmd = this.rs.getMetaData();
  1131. checkRsmdForBug13277(rsmd);
  1132. this.rs.close();
  1133. this.pstmt = this.conn.prepareStatement(
  1134. "INSERT INTO testBug13277 (field2) VALUES ('abcdefg')",
  1135. Statement.RETURN_GENERATED_KEYS);
  1136. this.pstmt.executeUpdate();
  1137. this.rs = this.pstmt.getGeneratedKeys();
  1138. rsmd = this.rs.getMetaData();
  1139. checkRsmdForBug13277(rsmd);
  1140. this.rs.close();
  1141. this.pstmt.addBatch();
  1142. this.pstmt.addBatch();
  1143. this.pstmt.executeUpdate();
  1144. this.rs = this.pstmt.getGeneratedKeys();
  1145. rsmd = this.rs.getMetaData();
  1146. checkRsmdForBug13277(rsmd);
  1147. this.rs.close();
  1148. } finally {
  1149. if (this.pstmt != null) {
  1150. this.pstmt.close();
  1151. this.pstmt = null;
  1152. }
  1153. if (this.rs != null) {
  1154. this.rs.close();
  1155. this.rs = null;
  1156. }
  1157. }
  1158. }
  1159. /**
  1160. * Tests BUG13601 (which doesn't seem to be present in 3.1.11, but we'll
  1161. * leave it in here for regression's-sake).
  1162. *
  1163. * @throws Exception
  1164. * if the test fails.
  1165. */
  1166. public void testBug13601() throws Exception {
  1167. if (versionMeetsMinimum(5, 0)) {
  1168. createTable("testBug13601",
  1169. "(field1 BIGINT NOT NULL, field2 BIT default 0 NOT NULL) ENGINE=MyISAM");
  1170. this.rs = this.stmt
  1171. .executeQuery("SELECT field1, field2 FROM testBug13601 WHERE 1=-1");
  1172. ResultSetMetaData rsmd = this.rs.getMetaData();
  1173. assertEquals(Types.BIT, rsmd.getColumnType(2));
  1174. assertEquals(Boolean.class.getName(), rsmd.getColumnClassName(2));
  1175. this.rs = this.conn.prepareStatement(
  1176. "SELECT field1, field2 FROM testBug13601 WHERE 1=-1")
  1177. .executeQuery();
  1178. rsmd = this.rs.getMetaData();
  1179. assertEquals(Types.BIT, rsmd.getColumnType(2));
  1180. assertEquals(Boolean.class.getName(), rsmd.getColumnClassName(2));
  1181. }
  1182. }
  1183. /**
  1184. * Tests fix for BUG#14815 - DBMD.getColumns() doesn't return TABLE_NAME
  1185. * correctly.
  1186. *
  1187. * @throws Exception
  1188. * if the test fails
  1189. */
  1190. public void testBug14815() throws Exception {
  1191. try {
  1192. createTable("testBug14815_1", "(field_1_1 int)");
  1193. createTable("testBug14815_2", "(field_2_1 int)");
  1194. boolean lcTableNames = this.conn.getMetaData()
  1195. .storesLowerCaseIdentifiers();
  1196. String tableName1 = lcTableNames ? "testbug14815_1"
  1197. : "testBug14815_1";
  1198. String tableName2 = lcTableNames ? "testbug14815_2"
  1199. : "testBug14815_2";
  1200. this.rs = this.conn.getMetaData().getColumns(
  1201. this.conn.getCatalog(), null, "testBug14815%", "%");
  1202. assertTrue(this.rs.next());
  1203. assertEquals(tableName1, this.rs.getString("TABLE_NAME"));
  1204. assertEquals("field_1_1", this.rs.getString("COLUMN_NAME"));
  1205. assertTrue(this.rs.next());
  1206. assertEquals(tableName2, this.rs.getString("TABLE_NAME"));
  1207. assertEquals("field_2_1", this.rs.getString("COLUMN_NAME"));
  1208. } finally {
  1209. if (this.rs != null) {
  1210. this.rs.close();
  1211. this.rs = null;
  1212. }
  1213. }
  1214. }
  1215. /**
  1216. * Tests fix for BUG#15854 - DBMD.getColumns() returns wrong type for BIT.
  1217. *
  1218. * @throws Exception
  1219. * if the test fails.
  1220. */
  1221. public void testBug15854() throws Exception {
  1222. if (versionMeetsMinimum(5, 0)) {
  1223. createTable("testBug15854", "(field1 BIT)");
  1224. try {
  1225. this.rs = this.conn.getMetaData().getColumns(
  1226. this.conn.getCatalog(), null, "testBug15854", "field1");
  1227. assertTrue(this.rs.next());
  1228. assertEquals(Types.BIT, this.rs.getInt("DATA_TYPE"));
  1229. } finally {
  1230. if (this.rs != null) {
  1231. ResultSet toClose = this.rs;
  1232. this.rs = null;
  1233. toClose.close();
  1234. }
  1235. }
  1236. }
  1237. }
  1238. /**
  1239. * Tests fix for BUG#16277 - Invalid classname returned for
  1240. * RSMD.getColumnClassName() for BIGINT type.
  1241. *
  1242. * @throws Exception
  1243. * if the test fails.
  1244. */
  1245. public void testBug16277() throws Exception {
  1246. createTable("testBug16277", "(field1 BIGINT, field2 BIGINT UNSIGNED)");
  1247. ResultSetMetaData rsmd = this.stmt.executeQuery(
  1248. "SELECT field1, field2 FROM testBug16277").getMetaData();
  1249. assertEquals("java.lang.Long", rsmd.getColumnClassName(1));
  1250. assertEquals("java.math.BigInteger", rsmd.getColumnClassName(2));
  1251. }
  1252. /**
  1253. * Tests fix for BUG#18554 - Aliased column names where length of name > 251
  1254. * are corrupted.
  1255. *
  1256. * @throws Exception
  1257. * if the test fails.
  1258. */
  1259. public void testBug18554() throws Exception {
  1260. testBug18554(249);
  1261. testBug18554(250);
  1262. testBug18554(251);
  1263. testBug18554(252);
  1264. testBug18554(253);
  1265. testBug18554(254);
  1266. testBug18554(255);
  1267. }
  1268. private void testBug18554(int columnNameLength) throws Exception {
  1269. StringBuffer buf = new StringBuffer(columnNameLength + 2);
  1270. for (int i = 0; i < columnNameLength; i++) {
  1271. buf.append((char) ((Math.random() * 26) + 65));
  1272. }
  1273. String colName = buf.toString();
  1274. this.rs = this.stmt.executeQuery("select curtime() as `" + colName
  1275. + "`");
  1276. ResultSetMetaData meta = this.rs.getMetaData();
  1277. assertEquals(colName, meta.getColumnLabel(1));
  1278. }
  1279. private void checkRsmdForBug13277(ResultSetMetaData rsmd)
  1280. throws SQLException {
  1281. assertEquals(17, rsmd.getColumnDisplaySize(1));
  1282. if (versionMeetsMinimum(4, 1)) {
  1283. assertEquals(false, rsmd.isDefinitelyWritable(1));
  1284. assertEquals(true, rsmd.isReadOnly(1));
  1285. assertEquals(false, rsmd.isWritable(1));
  1286. }
  1287. }
  1288. public void testSupportsCorrelatedSubqueries() throws Exception {
  1289. DatabaseMetaData dbmd = this.conn.getMetaData();
  1290. assertEquals(versionMeetsMinimum(4, 1), dbmd
  1291. .supportsCorrelatedSubqueries());
  1292. }
  1293. public void testSupportesGroupByUnrelated() throws Exception {
  1294. DatabaseMetaData dbmd = this.conn.getMetaData();
  1295. assertEquals(true, dbmd.supportsGroupByUnrelated());
  1296. }
  1297. /**
  1298. * Tests fix for BUG#21267, ParameterMetaData throws NullPointerException
  1299. * when prepared SQL actually has a syntax error
  1300. *
  1301. * @throws Exception
  1302. */
  1303. public void testBug21267() throws Exception {
  1304. if (isRunningOnJdk131()) {
  1305. return; // no parameter metadata on JDK-1.3.1
  1306. }
  1307. createTable(
  1308. "bug21267",
  1309. "(`Col1` int(11) NOT NULL,`Col2` varchar(45) default NULL,`Col3` varchar(45) default NULL,PRIMARY KEY (`Col1`))");
  1310. try {
  1311. this.pstmt = this.conn
  1312. .prepareStatement("SELECT Col1, Col2,Col4 FROM bug21267 WHERE Col1=?");
  1313. this.pstmt.setInt(1, 1);
  1314. java.sql.ParameterMetaData psMeta = this.pstmt
  1315. .getParameterMetaData();
  1316. try {
  1317. assertEquals(0, psMeta.getParameterType(1));
  1318. } catch (SQLException sqlEx) {
  1319. assertEquals(SQLError.SQL_STATE_DRIVER_NOT_CAPABLE, sqlEx.getSQLState());
  1320. }
  1321. this.pstmt.close();
  1322. Properties props = new Properties();
  1323. props.setProperty("generateSimpleParameterMetadata", "true");
  1324. this.pstmt = getConnectionWithProps(props).prepareStatement("SELECT Col1, Col2,Col4 FROM bug21267 WHERE Col1=?");
  1325. psMeta = this.pstmt.getParameterMetaData();
  1326. assertEquals(Types.VARCHAR, psMeta.getParameterType(1));
  1327. } finally {
  1328. closeMemberJDBCResources();
  1329. }
  1330. }
  1331. /**
  1332. * Tests fix for BUG#21544 - When using information_schema for metadata,
  1333. * COLUMN_SIZE for getColumns() is not clamped to range of
  1334. * java.lang.Integer as is the case when not using
  1335. * information_schema, thus leading to a truncation exception that
  1336. * isn't present when not using information_schema.
  1337. *
  1338. * @throws Exception if the test fails
  1339. */
  1340. public void testBug21544() throws Exception {
  1341. if (!versionMeetsMinimum(5, 0)) {
  1342. return;
  1343. }
  1344. createTable("testBug21544",
  1345. "(foo_id INT NOT NULL, stuff LONGTEXT"
  1346. + ", PRIMARY KEY (foo_id))", "INNODB");
  1347. Connection infoSchemConn = null;
  1348. Properties props = new Properties();
  1349. props.setProperty("useInformationSchema", "true");
  1350. props.setProperty("jdbcCompliantTruncation", "false");
  1351. infoSchemConn = getConnectionWithProps(props);
  1352. try {
  1353. this.rs = infoSchemConn.getMetaData().getColumns(null, null,
  1354. "testBug21544",
  1355. null);
  1356. while (rs.next()) {
  1357. rs.getInt("COLUMN_SIZE");
  1358. }
  1359. } finally {
  1360. if (infoSchemConn != null) {
  1361. infoSchemConn.close();
  1362. }
  1363. closeMemberJDBCResources();
  1364. }
  1365. }
  1366. /**
  1367. * Tests fix for BUG#22613 - DBMD.getColumns() does not return expected
  1368. * COLUMN_SIZE for the SET type (fixed to be consistent with the ODBC driver)
  1369. *
  1370. * @throws Exception if the test fails
  1371. */
  1372. public void testBug22613() throws Exception {
  1373. createTable("bug22613", "( s set('a','bc','def','ghij') default NULL, t enum('a', 'ab', 'cdef'), s2 SET('1','2','3','4','1585','ONE','TWO','Y','N','THREE'))");
  1374. try {
  1375. checkMetadataForBug22613(this.conn);
  1376. if (versionMeetsMinimum(5, 0)) {
  1377. Connection infoSchemConn = null;
  1378. try {
  1379. Properties props = new Properties();
  1380. props.setProperty("useInformationSchema", "true");
  1381. infoSchemConn = getConnectionWithProps(props);
  1382. checkMetadataForBug22613(infoSchemConn);
  1383. } finally {
  1384. if (infoSchemConn != null) {
  1385. infoSchemConn.close();
  1386. }
  1387. }
  1388. }
  1389. } finally {
  1390. closeMemberJDBCResources();
  1391. }
  1392. }
  1393. private void checkMetadataForBug22613(Connection c) throws Exception {
  1394. String maxValue = "a,bc,def,ghij";
  1395. String maxValue2 = "1,2,3,4,1585,ONE,TWO,Y,N,THREE";
  1396. try {
  1397. DatabaseMetaData meta = c.getMetaData();
  1398. this.rs = meta.getColumns(null, this.conn.getCatalog(), "bug22613", "s");
  1399. this.rs.first();
  1400. assertEquals(maxValue.length(), rs.getInt("COLUMN_SIZE"));
  1401. this.rs = meta.getColumns(null, this.conn.getCatalog(), "bug22613", "s2");
  1402. this.rs.first();
  1403. assertEquals(maxValue2.length(), rs.getInt("COLUMN_SIZE"));
  1404. this.rs = meta.getColumns(null, c.getCatalog(), "bug22613", "t");
  1405. this.rs.first();
  1406. assertEquals(4, rs.getInt("COLUMN_SIZE"));
  1407. } finally {
  1408. closeMemberJDBCResources();
  1409. }
  1410. }
  1411. /**
  1412. * Fix for BUG#22628 - Driver.getPropertyInfo() throws NullPointerException for URL that only specifies
  1413. * host and/or port.
  1414. *
  1415. * @throws Exception if the test fails.
  1416. */
  1417. public void testBug22628(

Large files files are truncated, but you can click here to view the full file