package testsuite.simple; import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.Statement; import java.util.Properties; import java.sql.Connection; import testsuite.BaseTestCase; /** * @author Tonci * Bug#20913: DatabaseMetaData.getTables fails to list all tables in all catalogs * c/J 3.1, "nullCatalogMeansCurrent" test, comparing with SHOW TABLES */ public class TestBug20913 extends BaseTestCase { public TestBug20913(String name) { super(name); // TODO Auto-generated constructor stub } /** * @throws Exception */ public void testBug20913() throws Exception { System.out.println("java.vm.version : " + System.getProperty("java.vm.version")); System.out.println("java.vm.vendor : " + System.getProperty("java.vm.vendor")); System.out.println("java.runtime.version : " + System.getProperty("java.runtime.version")); System.out.println("os.name : " + System.getProperty("os.name")); System.out.println("os.version : " + System.getProperty("os.version ")); System.out.println("sun.management.compiler : " + System.getProperty("sun.management.compiler")); if (versionMeetsMinimum(5, 0, 7)) { Properties props = new Properties(); props.put("useInformationSchema", "true"); props.put("nullCatalogMeansCurrent", "false"); Connection conn1 = null; String[] tableTypes = { "TABLE" }; try { conn1 = getConnectionWithProps(props); Statement stmt1 = conn1.createStatement(); stmt1.execute("USE mysql"); ResultSet rs1 = stmt1.executeQuery("SHOW TABLES"); DatabaseMetaData metaData = conn1.getMetaData(); int tblsInMysqlDB = 0; while (rs1.next()) { tblsInMysqlDB++; } System.out.println("Tables in mysql DB: " + tblsInMysqlDB); rs1.close(); int countTblsTotal = 0; rs1 = metaData.getTables(null, null, "%", tableTypes);//null); while (rs1.next()) { countTblsTotal++; } System.out.println("Tables total on MySQL server: " + countTblsTotal); assertTrue(countTblsTotal > tblsInMysqlDB); /** * Clean up and check if nullCatalogMeansCurrent = "false" works */ rs1.close(); stmt1.close(); conn1.close(); props.put("nullCatalogMeansCurrent", "false"); conn1 = null; stmt1 = null; rs1 = null; metaData = null; conn1 = getConnectionWithProps(props); stmt1 = conn1.createStatement(); metaData = conn1.getMetaData(); countTblsTotal = 0; rs1 = metaData.getTables("mysql", null, "%", tableTypes);//null); while (rs1.next()) { countTblsTotal++; } System.out.println("Tables total on mysql database: " + countTblsTotal); assertTrue(countTblsTotal == tblsInMysqlDB); rs1.close(); conn1.close(); } finally { closeMemberJDBCResources(); } } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub junit.textui.TestRunner.run(TestBug20913.class); } /* (non-Javadoc) * @see testsuite.BaseTestCase#setUp() * Make sure we have at least 1 table outside mysql database */ public void setUp() throws Exception { super.setUp(); this.stmt.execute("DROP DATABASE IF EXISTS bug20913"); this.stmt.execute("CREATE DATABASE bug20913"); this.stmt.execute("CREATE TABLE bug20913.bug20913tst(Id INT UNSIGNED)"); } public void tearDown() throws Exception { this.stmt.execute("DROP DATABASE IF EXISTS bug20913"); super.tearDown(); } }