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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
/*
Copyright (C) 2002-2004 MySQL AB
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.simple;
import testsuite.BaseTestCase;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.BitSet;
/**
* Tests DatabaseMetaData methods.
*
* @author Mark Matthews
* @version $Id: MetadataTest.java,v 1.8.4.11 2005/02/17 21:42:28 mmatthews Exp $
*/
public class MetadataTest extends BaseTestCase {
//~ Constructors -----------------------------------------------------------
/**
* Creates a new MetadataTest object.
*
* @param name DOCUMENT ME!
*/
public MetadataTest(String name) {
super(name);
}
//~ Methods ----------------------------------------------------------------
/**
* Runs all test cases in this test suite
*
* @param args
*/
public static void main(String[] args) {
junit.textui.TestRunner.run(MetadataTest.class);
}
/**
* DOCUMENT ME!
*
* @throws Exception DOCUMENT ME!
*/
public void setUp() throws Exception {
super.setUp();
createTestTable();
}
/**
* DOCUMENT ME!
*
* @throws SQLException DOCUMENT ME!
*/
public void testForeignKeys() throws SQLException {
DatabaseMetaData dbmd = this.conn.getMetaData();
this.rs = dbmd.getImportedKeys(null, null, "child");
while (this.rs.next()) {
String pkColumnName = this.rs.getString("PKCOLUMN_NAME");
String fkColumnName = this.rs.getString("FKCOLUMN_NAME");
assertTrue("Primary Key not returned correctly ('" + pkColumnName
+ "' != 'parent_id')",
pkColumnName.equalsIgnoreCase("parent_id"));
assertTrue("Foreign Key not returned correctly ('" + fkColumnName
+ "' != 'parent_id_fk')",
fkColumnName.equalsIgnoreCase("parent_id_fk"));
}
this.rs.close();
this.rs = dbmd.getExportedKeys(null, null, "parent");
while (this.rs.next()) {
String pkColumnName = this.rs.getString("PKCOLUMN_NAME");
String fkColumnName = this.rs.getString("FKCOLUMN_NAME");
String fkTableName = this.rs.getString("FKTABLE_NAME");
assertTrue("Primary Key not returned correctly ('" + pkColumnName
+ "' != 'parent_id')",
pkColumnName.equalsIgnoreCase("parent_id"));
assertTrue(
"Foreign Key table not returned correctly for getExportedKeys ('"
+ fkTableName + "' != 'child')",
fkTableName.equalsIgnoreCase("child"));
assertTrue(
"Foreign Key not returned correctly for getExportedKeys ('"
+ fkColumnName + "' != 'parent_id_fk')",
fkColumnName.equalsIgnoreCase("parent_id_fk"));
}
this.rs.close();
this.rs = dbmd.getCrossReference(null, null, "cpd_foreign_3", null, null,
"cpd_foreign_4");
while (this.rs.next()) {
String pkColumnName = this.rs.getString("PKCOLUMN_NAME");
String pkTableName = this.rs.getString("PKTABLE_NAME");
String fkColumnName = this.rs.getString("FKCOLUMN_NAME");
String fkTableName = this.rs.getString("FKTABLE_NAME");
String deleteAction = cascadeOptionToString(this.rs.getInt("DELETE_RULE"));
String updateAction = cascadeOptionToString(this.rs.getInt("UPDATE_RULE"));
System.out.println("[D] " + deleteAction);
System.out.println("[U] " + updateAction);
System.out.println(pkTableName + "(" + pkColumnName + ") -> "
+ fkTableName + "(" + fkColumnName + ")");
}
this.rs.close();
this.rs = dbmd.getImportedKeys(null, null, "fktable2");
}
/**
* DOCUMENT ME!
*
* @throws SQLException DOCUMENT ME!
*/
public void testGetPrimaryKeys() throws SQLException {
try {
DatabaseMetaData dbmd = this.conn.getMetaData();
this.rs = dbmd.getPrimaryKeys(this.conn.getCatalog(), "", "multikey");
short[] keySeqs = new short[4];
String[] columnNames = new String[4];
int i = 0;
while (this.rs.next()) {
this.rs.getString("TABLE_NAME");
columnNames[i] = this.rs.getString("COLUMN_NAME");
this.rs.getString("PK_NAME");
keySeqs[i] = this.rs.getShort("KEY_SEQ");
i++;
}
if ((keySeqs[0] != 3) && (keySeqs[1] != 2) && (keySeqs[2] != 4)
&& (keySeqs[4] != 1)) {
fail("Keys returned in wrong order");
}
} finally {
if (this.rs != null) {
try {
this.rs.close();
} catch (SQLException sqlEx) {
/* ignore */
}
}
}
}
private static String cascadeOptionToString(int option) {
switch (option) {
case DatabaseMetaData.importedKeyCascade:
return "CASCADE";
case DatabaseMetaData.importedKeySetNull:
return "SET NULL";
case DatabaseMetaData.importedKeyRestrict:
return "RESTRICT";
case DatabaseMetaData.importedKeyNoAction:
return "NO ACTION";
}
return "SET DEFAULT";
}
private void createTestTable() throws SQLException {
this.stmt.executeUpdate("DROP TABLE IF EXISTS child");
this.stmt.executeUpdate("DROP TABLE IF EXISTS parent");
this.stmt.executeUpdate("DROP TABLE IF EXISTS multikey");
this.stmt.executeUpdate("DROP TABLE IF EXISTS cpd_foreign_4");
this.stmt.executeUpdate("DROP TABLE IF EXISTS cpd_foreign_3");
this.stmt.executeUpdate("DROP TABLE IF EXISTS cpd_foreign_2");
this.stmt.executeUpdate("DROP TABLE IF EXISTS cpd_foreign_1");
this.stmt.executeUpdate("DROP TABLE IF EXISTS fktable2");
this.stmt.executeUpdate("DROP TABLE IF EXISTS fktable1");
this.stmt.executeUpdate(
"CREATE TABLE parent(parent_id INT NOT NULL, PRIMARY KEY (parent_id)) TYPE=INNODB");
this.stmt.executeUpdate(
"CREATE TABLE child(child_id INT, parent_id_fk INT, INDEX par_ind (parent_id_fk), "
+ "FOREIGN KEY (parent_id_fk) REFERENCES parent(parent_id)) TYPE=INNODB");
this.stmt.executeUpdate(
"CREATE TABLE multikey(d INT NOT NULL, b INT NOT NULL, a INT NOT NULL, c INT NOT NULL, PRIMARY KEY (d, b, a, c))");
// Test compound foreign keys
this.stmt.executeUpdate("create table cpd_foreign_1("
+ "id int(8) not null auto_increment primary key,"
+ "name varchar(255) not null unique," + "key (id)"
+ ") type=InnoDB");
this.stmt.executeUpdate("create table cpd_foreign_2("
+ "id int(8) not null auto_increment primary key," + "key (id),"
+ "name varchar(255)" + ") type=InnoDB");
this.stmt.executeUpdate("create table cpd_foreign_3("
+ "cpd_foreign_1_id int(8) not null,"
+ "cpd_foreign_2_id int(8) not null," + "key(cpd_foreign_1_id),"
+ "key(cpd_foreign_2_id),"
+ "primary key (cpd_foreign_1_id, cpd_foreign_2_id),"
+ "foreign key (cpd_foreign_1_id) references cpd_foreign_1(id),"
+ "foreign key (cpd_foreign_2_id) references cpd_foreign_2(id)"
+ ") type=InnoDB");
this.stmt.executeUpdate("create table cpd_foreign_4("
+ "cpd_foreign_1_id int(8) not null,"
+ "cpd_foreign_2_id int(8) not null," + "key(cpd_foreign_1_id),"
+ "key(cpd_foreign_2_id),"
+ "primary key (cpd_foreign_1_id, cpd_foreign_2_id),"
+ "foreign key (cpd_foreign_1_id, cpd_foreign_2_id) "
+ "references cpd_foreign_3(cpd_foreign_1_id, cpd_foreign_2_id) "
+ "ON DELETE RESTRICT ON UPDATE CASCADE" + ") type=InnoDB");
this.stmt.executeUpdate(
"create table fktable1 (TYPE_ID int not null, TYPE_DESC varchar(32), primary key(TYPE_ID)) TYPE=InnoDB");
this.stmt.executeUpdate(
"create table fktable2 (KEY_ID int not null, COF_NAME varchar(32), PRICE float, TYPE_ID int, primary key(KEY_ID), "
+ "index(TYPE_ID), foreign key(TYPE_ID) references fktable1(TYPE_ID)) TYPE=InnoDB");
}
/**
* Tests the implementation of metadata for views.
*
* This test automatically detects whether or not the
* server it is running against supports the creation of
* views.
*
* @throws SQLException if the test fails.
*/
public void testViewMetaData() throws SQLException {
try {
this.rs = this.conn.getMetaData().getTableTypes();
while (this.rs.next()) {
if ("VIEW".equalsIgnoreCase(this.rs.getString(1))) {
this.stmt.executeUpdate("DROP VIEW IF EXISTS vTestViewMetaData");
this.stmt.executeUpdate("DROP TABLE IF EXISTS testViewMetaData");
this.stmt.executeUpdate("CREATE TABLE testViewMetaData (field1 INT)");
this.stmt.executeUpdate("CREATE VIEW vTestViewMetaData AS SELECT field1 FROM testViewMetaData");
ResultSet tablesRs = null;
try {
tablesRs = this.conn.getMetaData().getTables(this.conn.getCatalog(), null, "%ViewMetaData", new String[] {"TABLE", "VIEW"});
assertTrue(tablesRs.next());
assertTrue("testViewMetaData".equalsIgnoreCase(tablesRs.getString(3)));
assertTrue(tablesRs.next());
assertTrue("vTestViewMetaData".equalsIgnoreCase(tablesRs.getString(3)));
} finally {
if (tablesRs != null) {
tablesRs.close();
}
}
break;
}
}
} finally {
if (this.rs != null) {
this.rs.close();
}
}
}
/**
* Tests detection of read-only fields when the server is 4.1.0 or newer.
*
* @throws Exception if the test fails.
*/
public void testRSMDIsReadOnly() throws Exception {
try {
this.rs = this.stmt.executeQuery("SELECT 1");
ResultSetMetaData rsmd = this.rs.getMetaData();
if (versionMeetsMinimum(4, 1)) {
assertTrue(rsmd.isReadOnly(1));
try {
this.stmt.executeUpdate("DROP TABLE IF EXISTS testRSMDIsReadOnly");
this.stmt.executeUpdate("CREATE TABLE testRSMDIsReadOnly (field1 INT)");
this.stmt.executeUpdate("INSERT INTO testRSMDIsReadOnly VALUES (1)");
this.rs = this.stmt.executeQuery("SELECT 1, field1 + 1, field1 FROM testRSMDIsReadOnly");
rsmd = this.rs.getMetaData();
assertTrue(rsmd.isReadOnly(1));
assertTrue(rsmd.isReadOnly(2));
assertTrue(!rsmd.isReadOnly(3));
} finally {
this.stmt.executeUpdate("DROP TABLE IF EXISTS testRSMDIsReadOnly");
}
} else {
assertTrue(rsmd.isReadOnly(1) == false);
}
} finally {
if (this.rs != null) {
this.rs.close();
}
}
}
public void testBitType() throws Exception {
if (versionMeetsMinimum(5, 0, 3)) {
try {
this.stmt.executeUpdate("DROP TABLE IF EXISTS testBitType");
this.stmt.executeUpdate("CREATE TABLE testBitType (field1 BIT, field2 BIT, field3 BIT)");
this.stmt.executeUpdate("INSERT INTO testBitType VALUES (1, 0, NULL)");
this.rs = this.stmt.executeQuery("SELECT field1, field2, field3 FROM testBitType");
this.rs.next();
assertTrue(((Boolean)this.rs.getObject(1)).booleanValue());
assertTrue(!((Boolean)this.rs.getObject(2)).booleanValue());
assertEquals(this.rs.getObject(3), null);
System.out.println(this.rs.getObject(1) + ", " + this.rs.getObject(2) + ", " + this.rs.getObject(3));
this.rs = this.conn.prepareStatement("SELECT field1, field2, field3 FROM testBitType").executeQuery();
this.rs.next();
assertTrue(((Boolean)this.rs.getObject(1)).booleanValue());
assertTrue(!((Boolean)this.rs.getObject(2)).booleanValue());
assertEquals(this.rs.getObject(3), null);
byte[] asBytesTrue = this.rs.getBytes(1);
byte[] asBytesFalse = this.rs.getBytes(2);
byte[] asBytesNull = this.rs.getBytes(3);
assertEquals(asBytesTrue[0], 1);
assertEquals(asBytesFalse[0], 0);
assertEquals(asBytesNull, null);
this.stmt.executeUpdate("DROP TABLE IF EXISTS testBitField");
this.stmt.executeUpdate("CREATE TABLE testBitField(field1 BIT(9))");
this.rs = this.stmt.executeQuery("SELECT field1 FROM testBitField");
System.out.println(this.rs.getMetaData().getColumnClassName(1));
} finally {
this.stmt.executeUpdate("DROP TABLE IF EXISTS testBitType");
}
}
}
}
|