DatabaseMetaDataTest.java 12.3 KB
Newer Older
1 2
package org.postgresql.test.jdbc2;

3
import org.postgresql.test.TestUtil;
4 5 6
import junit.framework.TestCase;
import java.sql.*;

B
Bruce Momjian 已提交
7
/*
8 9 10 11
 * TestCase to test the internal functionality of org.postgresql.jdbc2.DatabaseMetaData
 *
 * PS: Do you know how difficult it is to type on a train? ;-)
 *
12
 * $Id: DatabaseMetaDataTest.java,v 1.19 2003/11/03 15:22:07 davec Exp $
13 14
 */

15 16 17
public class DatabaseMetaDataTest extends TestCase
{

18
	private Connection con;
B
Bruce Momjian 已提交
19
	/*
20 21 22 23 24 25 26
	 * Constructor
	 */
	public DatabaseMetaDataTest(String name)
	{
		super(name);
	}

27 28
	protected void setUp() throws Exception
	{
29 30
		con = TestUtil.openDB();
		TestUtil.createTable( con, "testmetadata", "id int4, name text, updated timestamp" );
31 32 33 34 35
		Statement stmt = con.createStatement();
		//we add the following comments to ensure the joins to the comments
		//are done correctly. This ensures we correctly test that case.
		stmt.execute("comment on table testmetadata is 'this is a table comment'");
		stmt.execute("comment on column testmetadata.id is 'this is a column comment'");
36 37 38
	}
	protected void tearDown() throws Exception
	{
39
		TestUtil.dropTable( con, "testmetadata" );
40

41
		TestUtil.closeDB( con );
42
	}
43 44

	public void testTables()
45 46 47 48 49 50 51
	{
		try
		{

			DatabaseMetaData dbmd = con.getMetaData();
			assertNotNull(dbmd);

52
			ResultSet rs = dbmd.getTables( null, null, "testmetadat%", new String[] {"TABLE"});
53
			assertTrue( rs.next() );
B
Bruce Momjian 已提交
54
			String tableName = rs.getString("TABLE_NAME");
D
Dave Cramer 已提交
55
			assertTrue( tableName.equals("testmetadata") );
56 57
			String tableType = rs.getString("TABLE_TYPE");
			assertTrue( tableType.equals("TABLE") );
58 59
            //There should only be one row returned 
            assertTrue( "getTables() returned too many rows", rs.next() == false);
60
			rs.close();
61

62 63 64 65 66
			rs = dbmd.getColumns("", "", "test%", "%" );
			assertTrue( rs.next() );
			assertTrue( rs.getString("TABLE_NAME").equals("testmetadata") );
			assertTrue( rs.getString("COLUMN_NAME").equals("id") );
			assertTrue( rs.getInt("DATA_TYPE") == java.sql.Types.INTEGER );
67

68 69 70 71
			assertTrue( rs.next() );
			assertTrue( rs.getString("TABLE_NAME").equals("testmetadata") );
			assertTrue( rs.getString("COLUMN_NAME").equals("name") );
			assertTrue( rs.getInt("DATA_TYPE") == java.sql.Types.VARCHAR );
72

73 74 75 76
			assertTrue( rs.next() );
			assertTrue( rs.getString("TABLE_NAME").equals("testmetadata") );
			assertTrue( rs.getString("COLUMN_NAME").equals("updated") );
			assertTrue( rs.getInt("DATA_TYPE") == java.sql.Types.TIMESTAMP );
77

78 79 80 81 82 83 84
		}
		catch (SQLException ex)
		{
			fail(ex.getMessage());
		}
	}

B
Bruce Momjian 已提交
85 86
	public void testCrossReference()
	{
D
Dave Cramer 已提交
87 88
		try
		{
B
Bruce Momjian 已提交
89
			Connection con1 = TestUtil.openDB();
D
Dave Cramer 已提交
90

B
Bruce Momjian 已提交
91
			TestUtil.createTable( con1, "vv", "a int not null, b int not null, primary key ( a, b )" );
D
Dave Cramer 已提交
92

B
Bruce Momjian 已提交
93
			TestUtil.createTable( con1, "ww", "m int not null, n int not null, primary key ( m, n ), foreign key ( m, n ) references vv ( a, b )" );
D
Dave Cramer 已提交
94 95 96 97 98


			DatabaseMetaData dbmd = con.getMetaData();
			assertNotNull(dbmd);

B
Bruce Momjian 已提交
99
			ResultSet rs = dbmd.getCrossReference(null, "", "vv", null, "", "ww" );
D
Dave Cramer 已提交
100

B
Bruce Momjian 已提交
101 102
			for (int j = 1; rs.next(); j++ )
			{
D
Dave Cramer 已提交
103

B
Bruce Momjian 已提交
104 105
				String pkTableName = rs.getString( "PKTABLE_NAME" );
				assertTrue ( pkTableName.equals("vv") );
D
Dave Cramer 已提交
106

B
Bruce Momjian 已提交
107 108
				String pkColumnName = rs.getString( "PKCOLUMN_NAME" );
				assertTrue( pkColumnName.equals("a") || pkColumnName.equals("b"));
D
Dave Cramer 已提交
109

B
Bruce Momjian 已提交
110 111
				String fkTableName = rs.getString( "FKTABLE_NAME" );
				assertTrue( fkTableName.equals( "ww" ) );
D
Dave Cramer 已提交
112

B
Bruce Momjian 已提交
113 114
				String fkColumnName = rs.getString( "FKCOLUMN_NAME" );
				assertTrue( fkColumnName.equals( "m" ) || fkColumnName.equals( "n" ) ) ;
D
Dave Cramer 已提交
115

B
Bruce Momjian 已提交
116
				String fkName = rs.getString( "FK_NAME" );
117
				if (TestUtil.haveMinimumServerVersion(con1,"7.3")) {
118 119 120 121
					assertTrue(fkName.startsWith("$1"));
				} else {
					assertTrue( fkName.startsWith( "<unnamed>") );
				}
D
Dave Cramer 已提交
122

B
Bruce Momjian 已提交
123 124
				String pkName = rs.getString( "PK_NAME" );
				assertTrue( pkName.equals("vv_pkey") );
D
Dave Cramer 已提交
125

B
Bruce Momjian 已提交
126 127 128
				int keySeq = rs.getInt( "KEY_SEQ" );
				assertTrue( keySeq == j );
			}
D
Dave Cramer 已提交
129 130


B
Bruce Momjian 已提交
131 132
			TestUtil.dropTable( con1, "vv" );
			TestUtil.dropTable( con1, "ww" );
D
Dave Cramer 已提交
133 134 135 136 137 138

		}
		catch (SQLException ex)
		{
			fail(ex.getMessage());
		}
B
Bruce Momjian 已提交
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

	public void testForeignKeyActions()
	{
		try {
			Connection conn = TestUtil.openDB();
			TestUtil.createTable(conn, "pkt", "id int primary key");
			TestUtil.createTable(conn, "fkt1", "id int references pkt on update restrict on delete cascade");
			TestUtil.createTable(conn, "fkt2", "id int references pkt on update set null on delete set default");
			DatabaseMetaData dbmd = conn.getMetaData();

			ResultSet rs = dbmd.getImportedKeys(null,"","fkt1");
			assertTrue(rs.next());
			assertTrue(rs.getInt("UPDATE_RULE") == DatabaseMetaData.importedKeyRestrict);
			assertTrue(rs.getInt("DELETE_RULE") == DatabaseMetaData.importedKeyCascade);
			rs.close();

			rs = dbmd.getImportedKeys(null,"","fkt2");
			assertTrue(rs.next());
			assertTrue(rs.getInt("UPDATE_RULE") == DatabaseMetaData.importedKeySetNull);
			assertTrue(rs.getInt("DELETE_RULE") == DatabaseMetaData.importedKeySetDefault);
			rs.close();

			TestUtil.dropTable(conn,"fkt2");
			TestUtil.dropTable(conn,"fkt1");
			TestUtil.dropTable(conn,"pkt");
		}
		catch (SQLException ex)
		{
			fail(ex.getMessage());
		}
	}

B
Bruce Momjian 已提交
172 173
	public void testForeignKeys()
	{
174 175
		try
		{
B
Bruce Momjian 已提交
176 177 178
			Connection con1 = TestUtil.openDB();
			TestUtil.createTable( con1, "people", "id int4 primary key, name text" );
			TestUtil.createTable( con1, "policy", "id int4 primary key, name text" );
179

B
Bruce Momjian 已提交
180 181 182
			TestUtil.createTable( con1, "users", "id int4 primary key, people_id int4, policy_id int4," +
								  "CONSTRAINT people FOREIGN KEY (people_id) references people(id)," +
								  "constraint policy FOREIGN KEY (policy_id) references policy(id)" );
183 184 185 186 187


			DatabaseMetaData dbmd = con.getMetaData();
			assertNotNull(dbmd);

B
Bruce Momjian 已提交
188 189 190 191
			ResultSet rs = dbmd.getImportedKeys(null, "", "users" );
			int j = 0;
			for (; rs.next(); j++ )
			{
192

B
Bruce Momjian 已提交
193 194
				String pkTableName = rs.getString( "PKTABLE_NAME" );
				assertTrue ( pkTableName.equals("people") || pkTableName.equals("policy") );
195

B
Bruce Momjian 已提交
196 197
				String pkColumnName = rs.getString( "PKCOLUMN_NAME" );
				assertTrue( pkColumnName.equals("id") );
198

B
Bruce Momjian 已提交
199 200
				String fkTableName = rs.getString( "FKTABLE_NAME" );
				assertTrue( fkTableName.equals( "users" ) );
201

B
Bruce Momjian 已提交
202 203
				String fkColumnName = rs.getString( "FKCOLUMN_NAME" );
				assertTrue( fkColumnName.equals( "people_id" ) || fkColumnName.equals( "policy_id" ) ) ;
204

B
Bruce Momjian 已提交
205
				String fkName = rs.getString( "FK_NAME" );
206
				assertTrue( fkName.startsWith( "people") || fkName.startsWith( "policy" ) );
207

B
Bruce Momjian 已提交
208 209
				String pkName = rs.getString( "PK_NAME" );
				assertTrue( pkName.equals( "people_pkey") || pkName.equals( "policy_pkey" ) );
210

B
Bruce Momjian 已提交
211
			}
212

B
Bruce Momjian 已提交
213
			assertTrue ( j == 2 );
214

B
Bruce Momjian 已提交
215
			rs = dbmd.getExportedKeys( null, "", "people" );
216

B
Bruce Momjian 已提交
217 218
			// this is hacky, but it will serve the purpose
			assertTrue ( rs.next() );
219

B
Bruce Momjian 已提交
220 221
			assertTrue( rs.getString( "PKTABLE_NAME" ).equals( "people" ) );
			assertTrue( rs.getString( "PKCOLUMN_NAME" ).equals( "id" ) );
222

B
Bruce Momjian 已提交
223 224
			assertTrue( rs.getString( "FKTABLE_NAME" ).equals( "users" ) );
			assertTrue( rs.getString( "FKCOLUMN_NAME" ).equals( "people_id" ) );
225

226
			assertTrue( rs.getString( "FK_NAME" ).startsWith( "people" ) );
227 228


B
Bruce Momjian 已提交
229 230 231
			TestUtil.dropTable( con1, "users" );
			TestUtil.dropTable( con1, "people" );
			TestUtil.dropTable( con1, "policy" );
232 233 234 235 236 237

		}
		catch (SQLException ex)
		{
			fail(ex.getMessage());
		}
B
Bruce Momjian 已提交
238
	}
239 240

	public void testColumns()
241
	{
242
		// At the moment just test that no exceptions are thrown KJ
243 244 245 246
		try
		{
			DatabaseMetaData dbmd = con.getMetaData();
			assertNotNull(dbmd);
247 248 249 250 251
			ResultSet rs = dbmd.getColumns(null,null,"pg_class",null);
			rs.close();
		} catch (SQLException sqle) {
			sqle.printStackTrace();
			fail(sqle.getMessage());
252
		}
253 254 255 256 257 258
	}

	public void testColumnPrivileges()
	{
		// At the moment just test that no exceptions are thrown KJ
		try
259
		{
260 261 262 263 264 265 266
			DatabaseMetaData dbmd = con.getMetaData();
			assertNotNull(dbmd);
			ResultSet rs = dbmd.getColumnPrivileges(null,null,"pg_statistic",null);
			rs.close();
		} catch (SQLException sqle) {
			sqle.printStackTrace();
			fail(sqle.getMessage());
267 268 269
		}
	}

270
	public void testTablePrivileges()
271 272 273 274 275
	{
		try
		{
			DatabaseMetaData dbmd = con.getMetaData();
			assertNotNull(dbmd);
276 277 278 279 280 281
			ResultSet rs = dbmd.getTablePrivileges(null,null,"testmetadata");
			boolean l_foundSelect = false;
			while (rs.next()) {
				if (rs.getString("GRANTEE").equals(TestUtil.getUser()) 
					&& rs.getString("PRIVILEGE").equals("SELECT")) l_foundSelect = true; 
			}
282
			rs.close();
283 284
			//Test that the table owner has select priv
			assertTrue("Couldn't find SELECT priv on table testmetadata for " + TestUtil.getUser(),l_foundSelect);
285 286 287
		} catch (SQLException sqle) {
			sqle.printStackTrace();
			fail(sqle.getMessage());
288
		}
289 290 291 292 293 294
	}

	public void testPrimaryKeys()
	{
		// At the moment just test that no exceptions are thrown KJ
		try
295
		{
296 297 298 299 300 301 302
			DatabaseMetaData dbmd = con.getMetaData();
			assertNotNull(dbmd);
			ResultSet rs = dbmd.getPrimaryKeys(null,null,"pg_class");
			rs.close();
		} catch (SQLException sqle) {
			sqle.printStackTrace();
			fail(sqle.getMessage());
303 304 305
		}
	}

306
	public void testIndexInfo()
307
	{
308
		// At the moment just test that no exceptions are thrown KJ
309 310 311 312
		try
		{
			DatabaseMetaData dbmd = con.getMetaData();
			assertNotNull(dbmd);
313 314 315 316 317
			ResultSet rs = dbmd.getIndexInfo(null,null,"pg_class",false,false);
			rs.close();
		} catch (SQLException sqle) {
			sqle.printStackTrace();
			fail(sqle.getMessage());
318
		}
319 320 321 322 323 324
	}

	public void testTableTypes()
	{
		// At the moment just test that no exceptions are thrown KJ
		try
325
		{
326 327 328 329 330 331 332
			DatabaseMetaData dbmd = con.getMetaData();
			assertNotNull(dbmd);
			ResultSet rs = dbmd.getTableTypes();
			rs.close();
		} catch (SQLException sqle) {
			sqle.printStackTrace();
			fail(sqle.getMessage());
333 334 335
		}
	}

336
	public void testProcedureColumns()
337
	{
338
		// At the moment just test that no exceptions are thrown KJ
339 340 341 342
		try
		{
			DatabaseMetaData dbmd = con.getMetaData();
			assertNotNull(dbmd);
343 344 345 346 347
			ResultSet rs = dbmd.getProcedureColumns(null,null,null,null);
			rs.close();
		} catch (SQLException sqle) {
			sqle.printStackTrace();
			fail(sqle.getMessage());
348
		}
349 350 351 352 353 354
	}

	public void testVersionColumns()
	{
		// At the moment just test that no exceptions are thrown KJ
		try
355
		{
356 357 358 359 360 361
			DatabaseMetaData dbmd = con.getMetaData();
			assertNotNull(dbmd);
			ResultSet rs = dbmd.getVersionColumns(null,null,"pg_class");
			rs.close();
		} catch (SQLException sqle) {
			fail(sqle.getMessage());
362 363 364
		}
	}

365
	public void testBestRowIdentifier()
366
	{
367
		// At the moment just test that no exceptions are thrown KJ
368 369 370 371
		try
		{
			DatabaseMetaData dbmd = con.getMetaData();
			assertNotNull(dbmd);
372
			ResultSet rs = dbmd.getBestRowIdentifier(null,null,"pg_type",DatabaseMetaData.bestRowSession,false);
373 374 375 376 377
			rs.close();
		} catch (SQLException sqle) {
			fail(sqle.getMessage());
		}
	}
378

379 380 381 382 383 384 385 386 387 388 389 390 391
	public void testProcedures()
	{
		// At the moment just test that no exceptions are thrown KJ
		try
		{
			DatabaseMetaData dbmd = con.getMetaData();
			assertNotNull(dbmd);
			ResultSet rs = dbmd.getProcedures(null,null,null);
			rs.close();
		} catch (SQLException sqle) {
			fail(sqle.getMessage());
		}
	}
392

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
	public void testCatalogs()
	{
		try
		{
			DatabaseMetaData dbmd = con.getMetaData();
			assertNotNull(dbmd);
			ResultSet rs = dbmd.getCatalogs();
			boolean foundTemplate0 = false;
			boolean foundTemplate1 = false;
			while(rs.next()) {
				String database = rs.getString("TABLE_CAT");
				if ("template0".equals(database)) {
					foundTemplate0 = true;
				} else if ("template1".equals(database)) {
					foundTemplate1 = true;
				}
			}
			rs.close();
			assertTrue(foundTemplate0);
			assertTrue(foundTemplate1);
		} catch(SQLException sqle) {
			fail(sqle.getMessage());
415
		}
416 417 418 419 420
	}

	public void testSchemas()
	{
		try
421
		{
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
			DatabaseMetaData dbmd = con.getMetaData();
			assertNotNull(dbmd);

			ResultSet rs = dbmd.getSchemas();
			boolean foundPublic = false;
			boolean foundEmpty = false;
			boolean foundPGCatalog = false;
			int count;
		
			for(count=0; rs.next(); count++) {
				String schema = rs.getString("TABLE_SCHEM");
				if ("public".equals(schema)) {
					foundPublic = true;
				} else if ("".equals(schema)) {
					foundEmpty = true;
				} else if ("pg_catalog".equals(schema)) {
					foundPGCatalog = true;
				}
			}
			rs.close();
			if (TestUtil.haveMinimumServerVersion(con,"7.3")) {
				assertTrue(count >= 2);
				assertTrue(foundPublic);
				assertTrue(foundPGCatalog);
				assertTrue(!foundEmpty);
			} else {
				assertEquals(count,1);
				assertTrue(foundEmpty);
				assertTrue(!foundPublic);
				assertTrue(!foundPGCatalog);
			}
		} catch (SQLException sqle) {
			fail(sqle.getMessage());
455 456
		}
	}
457

458
}