diff options
author | bvosburgh | 2009-01-06 05:49:38 +0000 |
---|---|---|
committer | bvosburgh | 2009-01-06 05:49:38 +0000 |
commit | 75c0ec98f4393ba113c0ef76889cb613cb869f54 (patch) | |
tree | 4064945ae844c1d2c369b4e24aedf8e40598868e /jpa/tests/org.eclipse.jpt.db.tests/src | |
parent | 6fd836f6f64f7008fb1c99de5631fa27b0b0edca (diff) | |
download | webtools.dali-75c0ec98f4393ba113c0ef76889cb613cb869f54.tar.gz webtools.dali-75c0ec98f4393ba113c0ef76889cb613cb869f54.tar.xz webtools.dali-75c0ec98f4393ba113c0ef76889cb613cb869f54.zip |
[259534] reworked catalog and schema code
Diffstat (limited to 'jpa/tests/org.eclipse.jpt.db.tests/src')
8 files changed, 556 insertions, 120 deletions
diff --git a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/JDBCTests.java b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/JDBCTests.java new file mode 100644 index 0000000000..ce05d768f8 --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/JDBCTests.java @@ -0,0 +1,135 @@ +/******************************************************************************* + * Copyright (c) 2009 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.db.tests.internal; + +import java.sql.Connection; +import java.sql.DriverManager; + +import junit.framework.TestCase; + +/** + * These aren't tests. They are just an easy way to dump JDBC metadata to the + * console. + */ +@SuppressWarnings("nls") +public class JDBCTests extends TestCase { + + public JDBCTests(String name) { + super(name); + } + + public void testDerby() throws Exception { + this.dumpMetaData(DERBY); + } + + public void testMySQL() throws Exception { + this.dumpMetaData(MYSQL); + } + + public void testOracle() throws Exception { + this.dumpMetaData(ORACLE); + } + + public void testPostgreSQL() throws Exception { + this.dumpMetaData(POSTGRESQL); + } + + public void testSybase() throws Exception { + this.dumpMetaData(SYBASE); + } + + protected void dumpMetaData(ConnectionConfig config) throws Exception { + System.out.println("***** PLATFORM: " + config.platformName + " *****"); + System.out.println(); + Class.forName(config.driverClassName); + Connection connection = DriverManager.getConnection(this.buildURL(config), config.user, config.password); + System.out.println("CATALOGS:"); + JDBCTools.dump(connection.getMetaData().getCatalogs()); + System.out.println(); + System.out.println("SCHEMATA:"); + JDBCTools.dump(connection.getMetaData().getSchemas()); + connection.close(); + System.out.println(); + } + + protected String buildURL(ConnectionConfig config) { + return "jdbc:" + config.databaseURL; + } + + protected static final ConnectionConfig DERBY = + new ConnectionConfig( + "Derby", + "org.apache.derby.jdbc.EmbeddedDriver", + "derby:C:/derby/data/test", + null, + null + ); + + protected static final ConnectionConfig MYSQL = + new ConnectionConfig( + "MySQL", + "com.mysql.jdbc.Driver", + "mysql://localhost:3306", + "root", + "oracle" + ); + + protected static final ConnectionConfig ORACLE = + new ConnectionConfig( + "Oracle", + "oracle.jdbc.OracleDriver", + "oracle:thin:@localhost:1521:orcl", + "scott", + "tiger" + ); + + protected static final ConnectionConfig POSTGRESQL = + new ConnectionConfig( + "PostgreSQL", + "org.postgresql.Driver", + "postgresql:postgres", + "postgres", + "oracle" + ); + + // the Sybase server must be configured explicitly to "localhost" + // in the config file [SYBASE]/ini/sql.ini + protected static final ConnectionConfig SYBASE = + new ConnectionConfig( + "Sybase", + "com.sybase.jdbc3.jdbc.SybDriver", + "sybase:Tds:localhost:5000", + "sa", + "oracle" + ); + + protected static class ConnectionConfig { + protected final String platformName; + protected final String driverClassName; + protected final String databaseURL; + protected final String user; + protected final String password; + protected ConnectionConfig( + String platformName, + String driverClassName, + String databaseURL, + String user, + String password + ) { + super(); + this.platformName = platformName; + this.driverClassName = driverClassName; + this.databaseURL = databaseURL; + this.user = user; + this.password = password; + } + } + +} diff --git a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/JDBCTools.java b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/JDBCTools.java new file mode 100644 index 0000000000..57c98feafb --- /dev/null +++ b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/JDBCTools.java @@ -0,0 +1,105 @@ +/******************************************************************************* + * Copyright (c) 2009 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.db.tests.internal; + +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.eclipse.jpt.utility.internal.iterators.ResultSetIterator; + +@SuppressWarnings("nls") +public class JDBCTools { + + public static void dump(Connection connection, String sql) throws SQLException { + dump(execute(connection, sql)); + } + + public static void dump(ResultSet resultSet) throws SQLException { + PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); + // synchronize the console so everything is contiguous + synchronized (System.out) { + dumpOn(resultSet, pw); + } + pw.flush(); + } + + public static void dumpOn(ResultSet resultSet, PrintWriter pw) throws SQLException { + ArrayList<HashMap<String, Object>> maps = convertToMaps(resultSet); + for (Iterator<HashMap<String, Object>> mapStream = maps.iterator(); mapStream.hasNext(); ) { + for (Iterator<Map.Entry<String, Object>> entryStream = mapStream.next().entrySet().iterator(); entryStream.hasNext(); ) { + Map.Entry<String, Object> entry = entryStream.next(); + pw.print(entry.getKey()); + pw.print(" = "); + pw.print(entry.getValue()); + pw.println(); + } + if (mapStream.hasNext()) { + pw.println(); + } + } + pw.println("total rows: " + maps.size()); + } + + public static ArrayList<HashMap<String, Object>> convertToMaps(Connection connection, String sql) throws SQLException { + return convertToMaps(execute(connection, sql)); + } + + public static ResultSet execute(Connection connection, String sql) throws SQLException { + Statement statement = connection.createStatement(); + statement.execute(sql); + ResultSet resultSet = statement.getResultSet(); + statement.close(); + return resultSet; + } + + public static ArrayList<HashMap<String, Object>> convertToMaps(ResultSet resultSet) throws SQLException { + ArrayList<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>(); + for (Iterator<HashMap<String, Object>> stream = buildMapIterator(resultSet); stream.hasNext(); ) { + rows.add(stream.next()); + } + return rows; + } + + public static Iterator<HashMap<String, Object>> buildMapIterator(ResultSet resultSet) throws SQLException { + return new ResultSetIterator<HashMap<String, Object>>(resultSet, new MapResultSetIteratorAdapter(buildColumnNames(resultSet))); + } + + public static String[] buildColumnNames(ResultSet resultSet) throws SQLException { + String[] names = new String[resultSet.getMetaData().getColumnCount()]; + for (int i = 0; i < names.length; i++) { + names[i] = resultSet.getMetaData().getColumnName(i + 1); // NB: ResultSet index/subscript is 1-based + } + return names; + } + + public static class MapResultSetIteratorAdapter implements ResultSetIterator.Adapter<HashMap<String, Object>> { + private final String[] columnNames; + public MapResultSetIteratorAdapter(String[] columnNames) { + super(); + this.columnNames = columnNames; + } + public HashMap<String, Object> buildNext(ResultSet rs) throws SQLException { + HashMap<String, Object> map = new HashMap<String, Object>(this.columnNames.length); + for (int i = 0; i < this.columnNames.length; i++) { + map.put(this.columnNames[i], rs.getObject(i + 1)); // NB: ResultSet index/subscript is 1-based + } + return map; + } + } + +} diff --git a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/DTPPlatformTests.java b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/DTPPlatformTests.java index 0388d34e2a..7080bdb8b8 100644 --- a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/DTPPlatformTests.java +++ b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/DTPPlatformTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2008 Oracle. All rights reserved. + * Copyright (c) 2006, 2009 Oracle. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0, which accompanies this distribution * and is available at http://www.eclipse.org/legal/epl-v10.html. @@ -19,12 +19,12 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; -import java.util.HashMap; import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.Properties; + import junit.framework.TestCase; + import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IStatus; @@ -524,7 +524,15 @@ public abstract class DTPPlatformTests extends TestCase { TestConnectionListener listener = new TestConnectionListener(); this.connectionProfile.addConnectionListener(listener); - assertEquals(this.supportsCatalogs(), this.connectionProfile.getDatabase().supportsCatalogs()); + boolean supportsCatalogs = this.supportsCatalogs(); + assertEquals(supportsCatalogs, this.connectionProfile.getDatabase().supportsCatalogs()); + if (supportsCatalogs) { + assertTrue(this.connectionProfile.getDatabase().catalogsSize() > 0); + assertEquals(0, this.connectionProfile.getDatabase().schemataSize()); + } else { + assertEquals(0, this.connectionProfile.getDatabase().catalogsSize()); + assertTrue(this.connectionProfile.getDatabase().schemataSize() > 0); + } this.connectionProfile.removeConnectionListener(listener); this.connectionProfile.disconnect(); @@ -555,12 +563,12 @@ public abstract class DTPPlatformTests extends TestCase { return this.connectionProfile.getDatabase(); } - protected Catalog getCatalogNamed(String catalogName) { - return this.connectionProfile.getDatabase().getCatalogNamed(catalogName); + protected Catalog getDefaultCatalog() { + return this.getDatabase().getDefaultCatalog(); } - protected Schema getSchemaForIdentifier(String schemaName) { - return this.connectionProfile.getDatabase().getSchemaForIdentifier(schemaName); + protected Catalog getCatalogNamed(String catalogName) { + return this.connectionProfile.getDatabase().getCatalogNamed(catalogName); } protected String getRequiredPlatformProperty(String propertyKey) { @@ -709,7 +717,7 @@ public abstract class DTPPlatformTests extends TestCase { return new ResultSetIterator<ArrayList<Object>>(resultSet, new ListResultSetIteratorAdapter(resultSet.getMetaData().getColumnCount())); } - public class ListResultSetIteratorAdapter implements ResultSetIterator.Adapter<ArrayList<Object>> { + public static class ListResultSetIteratorAdapter implements ResultSetIterator.Adapter<ArrayList<Object>> { private final int columnCount; public ListResultSetIteratorAdapter(int columnCount) { super(); @@ -724,49 +732,6 @@ public abstract class DTPPlatformTests extends TestCase { } } - protected ArrayList<HashMap<String, Object>> execute2(String sql) throws SQLException { - Statement jdbcStatement = this.createJDBCStatement(); - jdbcStatement.execute(sql); - ArrayList<HashMap<String, Object>> rows = this.buildMaps(jdbcStatement.getResultSet()); - jdbcStatement.close(); - return rows; - } - - protected ArrayList<HashMap<String, Object>> buildMaps(ResultSet resultSet) throws SQLException { - ArrayList<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>(); - for (Iterator<HashMap<String, Object>> stream = this.buildMapIterator(resultSet); stream.hasNext(); ) { - rows.add(stream.next()); - } - return rows; - } - - protected Iterator<HashMap<String, Object>> buildMapIterator(ResultSet resultSet) throws SQLException { - return new ResultSetIterator<HashMap<String, Object>>(resultSet, new MapResultSetIteratorAdapter(this.buildColumnNames(resultSet))); - } - - protected String[] buildColumnNames(ResultSet resultSet) throws SQLException { - String[] names = new String[resultSet.getMetaData().getColumnCount()]; - for (int i = 0; i < names.length; i++) { - names[i] = resultSet.getMetaData().getColumnName(i + 1); // NB: ResultSet index/subscript is 1-based - } - return names; - } - - public class MapResultSetIteratorAdapter implements ResultSetIterator.Adapter<HashMap<String, Object>> { - private final String[] columnNames; - public MapResultSetIteratorAdapter(String[] columnNames) { - super(); - this.columnNames = columnNames; - } - public HashMap<String, Object> buildNext(ResultSet rs) throws SQLException { - HashMap<String, Object> map = new HashMap<String, Object>(this.columnNames.length); - for (int i = 0; i < this.columnNames.length; i++) { - map.put(this.columnNames[i], rs.getObject(i + 1)); // NB: ResultSet index/subscript is 1-based - } - return map; - } - } - protected Statement createJDBCStatement() throws SQLException { return this.getJDBCConnection().createStatement(); } @@ -923,7 +888,7 @@ public abstract class DTPPlatformTests extends TestCase { protected void dumpJDBCCatalogsOn(IndentingPrintWriter pw) throws SQLException { pw.println("JDBC catalogs: "); pw.indent(); - ArrayList<ArrayList<Object>> rows = this.buildRows(this.getJDBCConnection().getMetaData().getCatalogs()); + ArrayList<ArrayList<Object>> rows = this.buildRows(this.getDatabaseMetaData().getCatalogs()); for (Iterator<ArrayList<Object>> stream = rows.iterator(); stream.hasNext(); ) { pw.println(stream.next().get(0)); } @@ -942,7 +907,7 @@ public abstract class DTPPlatformTests extends TestCase { protected void dumpJDBCSchemataOn(IndentingPrintWriter pw) throws SQLException { pw.println("JDBC schemata: "); pw.indent(); - ArrayList<ArrayList<Object>> rows = this.buildRows(this.getJDBCConnection().getMetaData().getSchemas()); + ArrayList<ArrayList<Object>> rows = this.buildRows(this.getDatabaseMetaData().getSchemas()); for (ArrayList<Object> row : rows) { if (row.size() == 2) { // catalogs were added in jdk 1.4 Object catalog = row.get(1); @@ -955,35 +920,10 @@ public abstract class DTPPlatformTests extends TestCase { pw.undent(); } - protected void dump(ResultSet resultSet) throws SQLException { - IndentingPrintWriter pw = new IndentingPrintWriter(new OutputStreamWriter(System.out)); - // synchronize the console so everything is contiguous - synchronized (System.out) { - this.dumpOn(resultSet, pw); - } - pw.flush(); - } - - protected void dumpOn(ResultSet resultSet, IndentingPrintWriter pw) throws SQLException { - ArrayList<HashMap<String, Object>> maps = this.buildMaps(resultSet); - for (Iterator<HashMap<String, Object>> mapStream = maps.iterator(); mapStream.hasNext(); ) { - for (Iterator<Map.Entry<String, Object>> entryStream = mapStream.next().entrySet().iterator(); entryStream.hasNext(); ) { - Map.Entry<String, Object> entry = entryStream.next(); - pw.print(entry.getKey()); - pw.print(" = "); - pw.print(entry.getValue()); - pw.println(); - } - if (mapStream.hasNext()) { - pw.println(); - } - } - } - // ********** connection profile listener ********** - protected class TestConnectionProfileListener implements ConnectionProfileListener { + protected static class TestConnectionProfileListener implements ConnectionProfileListener { public String addedName; public String removedName; public String renamedOldName; @@ -1010,7 +950,7 @@ public abstract class DTPPlatformTests extends TestCase { // ********** connection listener ********** - protected class TestConnectionListener implements ConnectionListener { + protected static class TestConnectionListener implements ConnectionListener { public ConnectionProfile openedProfile; public ConnectionProfile modifiedProfile; public ConnectionProfile okToCloseProfile; diff --git a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/DerbyTests.java b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/DerbyTests.java index 28ea244d11..e2aacd8656 100644 --- a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/DerbyTests.java +++ b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/DerbyTests.java @@ -412,8 +412,57 @@ public class DerbyTests extends DTPPlatformTests { this.connectionProfile.disconnect(); } + public void testCrossSchemaReference() throws Exception { + this.connectionProfile.connect(); + TestConnectionListener listener = new TestConnectionListener(); + this.connectionProfile.addConnectionListener(listener); + + this.dropTable("XREF_TEST2", "EMP"); + this.dropSchema("XREF_TEST2"); + this.dropTable("XREF_TEST1", "ORG"); + this.dropSchema("XREF_TEST1"); + + this.executeUpdate("CREATE SCHEMA XREF_TEST1"); + this.executeUpdate("SET SCHEMA = XREF_TEST1"); + this.executeUpdate("CREATE TABLE ORG (ID INTEGER PRIMARY KEY, NAME VARCHAR(20))"); + + this.executeUpdate("CREATE SCHEMA XREF_TEST2"); + this.executeUpdate("SET SCHEMA = XREF_TEST2"); + this.executeUpdate("CREATE TABLE EMP (ID INTEGER PRIMARY KEY, NAME VARCHAR(20), " + + "ORG_ID INTEGER REFERENCES XREF_TEST1.ORG(ID))"); + + ((ICatalogObject) this.getDTPDatabase()).refresh(); + Schema schema1 = this.getDatabase().getSchemaNamed("XREF_TEST1"); + assertNotNull(schema1); + Table orgTable = schema1.getTableNamed("ORG"); + assertNotNull(orgTable); + + Schema schema2 = this.getDatabase().getSchemaNamed("XREF_TEST2"); + assertNotNull(schema2); + Table empTable = schema2.getTableNamed("EMP"); + assertNotNull(empTable); + assertEquals(1, empTable.foreignKeysSize()); + ForeignKey fk = empTable.foreignKeys().next(); + Table refTable = fk.getReferencedTable(); + assertNotNull(refTable); + assertEquals("ORG", refTable.getName()); + assertEquals(1, fk.columnPairsSize()); + ForeignKey.ColumnPair cp = fk.columnPairs().next(); + Column baseColumn = cp.getBaseColumn(); + assertEquals("ORG_ID", baseColumn.getName()); + Column refColumn = cp.getReferencedColumn(); + assertEquals("ID", refColumn.getName()); + + this.dropTable("XREF_TEST2", "EMP"); + this.dropSchema("XREF_TEST2"); + this.dropTable("XREF_TEST1", "ORG"); + this.dropSchema("XREF_TEST1"); + this.connectionProfile.removeConnectionListener(listener); + this.connectionProfile.disconnect(); + } + private void dropTable(String schemaName, String tableName) throws Exception { - Schema schema= this.getSchemaForIdentifier(schemaName); + Schema schema= this.getDatabase().getSchemaForIdentifier(schemaName); if (schema != null) { if (schema.getTableForIdentifier(tableName) != null) { this.executeUpdate("DROP TABLE " + schemaName + '.' + tableName); @@ -425,7 +474,7 @@ public class DerbyTests extends DTPPlatformTests { * NB: A Derby schema must be empty before it can be dropped. */ private void dropSchema(String name) throws Exception { - if (this.getSchemaForIdentifier(name) != null) { + if (this.getDatabase().getSchemaForIdentifier(name) != null) { this.executeUpdate("DROP SCHEMA " + name + " RESTRICT"); } } diff --git a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/MySQLTests.java b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/MySQLTests.java index d08d290b7e..4b65142baf 100644 --- a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/MySQLTests.java +++ b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/MySQLTests.java @@ -10,6 +10,7 @@ package org.eclipse.jpt.db.tests.internal.platforms; import java.util.Properties; + import org.eclipse.datatools.connectivity.drivers.jdbc.IJDBCDriverDefinitionConstants; import org.eclipse.datatools.connectivity.sqm.core.rte.ICatalogObject; import org.eclipse.jpt.db.Column; @@ -355,6 +356,43 @@ public class MySQLTests extends DTPPlatformTests { this.connectionProfile.disconnect(); } + /** + * We can only get a single "schema" per connection via DTP, + * so cross-schema references are not visible. + */ + public void testCrossSchemaReference() throws Exception { + this.connectionProfile.connect(); + TestConnectionListener listener = new TestConnectionListener(); + this.connectionProfile.addConnectionListener(listener); + + this.dropDatabase("xref_test2"); + this.dropDatabase("xref_test1"); + + this.executeUpdate("CREATE DATABASE xref_test1"); + this.getJDBCConnection().setCatalog("xref_test1"); + this.executeUpdate("CREATE TABLE org (id INTEGER PRIMARY KEY, name VARCHAR(20))"); + + this.executeUpdate("CREATE DATABASE xref_test2"); + this.getJDBCConnection().setCatalog("xref_test2"); + this.executeUpdate("CREATE TABLE emp (id INTEGER PRIMARY KEY, name VARCHAR(20), " + + "org_id INTEGER, FOREIGN KEY (org_id) REFERENCES xref_test1.org(id))"); + + this.getJDBCConnection().setCatalog("xref_test2"); + ((ICatalogObject) this.getDTPDatabase()).refresh(); + Schema schema2 = this.getDatabase().getDefaultSchema(); + assertNotNull(schema2); + Table empTable = schema2.getTableNamed("emp"); + assertNotNull(empTable); + // no foreign keys + assertEquals(0, empTable.foreignKeysSize()); + + this.dropDatabase("xref_test2"); + this.dropDatabase("xref_test1"); + + this.connectionProfile.removeConnectionListener(listener); + this.connectionProfile.disconnect(); + } + private void dropTable(String dbName, String tableName) throws Exception { this.executeUpdate("DROP TABLE IF EXISTS " + dbName + '.' + tableName); } diff --git a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/Oracle10gTests.java b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/Oracle10gTests.java index 8639cfceb6..bd70825d3f 100644 --- a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/Oracle10gTests.java +++ b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/Oracle10gTests.java @@ -10,6 +10,7 @@ package org.eclipse.jpt.db.tests.internal.platforms; import java.sql.SQLException; + import org.eclipse.datatools.connectivity.sqm.core.rte.ICatalogObject; import org.eclipse.jpt.db.Column; import org.eclipse.jpt.db.ForeignKey; @@ -80,6 +81,10 @@ public class Oracle10gTests extends DTPPlatformTests { } public void testDatabase() throws Exception { + if (this.connectionProfile.getUserName().toUpperCase().equals("SYS")) { + System.out.println("skipped test: " + this.getClass() + '.' + this.getName()); + return; // SYS does not have a schema + } this.connectionProfile.connect(); TestConnectionListener listener = new TestConnectionListener(); this.connectionProfile.addConnectionListener(listener); @@ -306,6 +311,56 @@ public class Oracle10gTests extends DTPPlatformTests { this.connectionProfile.disconnect(); } + public void testCrossSchemaReference() throws Exception { + if ( ! this.connectionProfile.getUserName().toUpperCase().equals("SYS")) { + System.out.println("skipped test: " + this.getClass() + '.' + this.getName()); + return; // SYS does not have a schema + } + this.connectionProfile.connect(); + TestConnectionListener listener = new TestConnectionListener(); + this.connectionProfile.addConnectionListener(listener); + + this.executeUpdateIgnoreErrors("DROP USER XREF_TEST2 CASCADE"); + this.executeUpdateIgnoreErrors("DROP USER XREF_TEST1 CASCADE"); + + this.executeUpdate("CREATE USER XREF_TEST1 IDENTIFIED BY foo"); + this.executeUpdate("ALTER USER XREF_TEST1 QUOTA UNLIMITED ON USERS"); + this.executeUpdate("CREATE TABLE XREF_TEST1.ORG (ID NUMBER(10) PRIMARY KEY, NAME VARCHAR2(20))"); + + this.executeUpdate("CREATE USER XREF_TEST2 IDENTIFIED BY foo"); + this.executeUpdate("ALTER USER XREF_TEST2 QUOTA UNLIMITED ON USERS"); + this.executeUpdate("GRANT ALL ON XREF_TEST1.ORG TO XREF_TEST2"); + this.executeUpdate("CREATE TABLE XREF_TEST2.EMP (ID NUMBER(10) PRIMARY KEY, NAME VARCHAR2(20), " + + "ORG_ID NUMBER(10) REFERENCES XREF_TEST1.ORG(ID))"); + + ((ICatalogObject) this.getDTPDatabase()).refresh(); + Schema schema1 = this.getDatabase().getSchemaNamed("XREF_TEST1"); + assertNotNull(schema1); + Table orgTable = schema1.getTableNamed("ORG"); + assertNotNull(orgTable); + + Schema schema2 = this.getDatabase().getSchemaNamed("XREF_TEST2"); + assertNotNull(schema2); + Table empTable = schema2.getTableNamed("EMP"); + assertNotNull(empTable); + assertEquals(1, empTable.foreignKeysSize()); + ForeignKey fk = empTable.foreignKeys().next(); + Table refTable = fk.getReferencedTable(); + assertNotNull(refTable); + assertEquals("ORG", refTable.getName()); + assertEquals(1, fk.columnPairsSize()); + ForeignKey.ColumnPair cp = fk.columnPairs().next(); + Column baseColumn = cp.getBaseColumn(); + assertEquals("ORG_ID", baseColumn.getName()); + Column refColumn = cp.getReferencedColumn(); + assertEquals("ID", refColumn.getName()); + + this.executeUpdate("DROP USER XREF_TEST2 CASCADE"); + this.executeUpdate("DROP USER XREF_TEST1 CASCADE"); + this.connectionProfile.removeConnectionListener(listener); + this.connectionProfile.disconnect(); + } + private void dropTable(String tableName) throws Exception { this.executeUpdateIgnoreErrors("DROP TABLE " + tableName + " CASCADE CONSTRAINTS"); } diff --git a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/PostgreSQLTests.java b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/PostgreSQLTests.java index 723b7faa15..7066141a5c 100644 --- a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/PostgreSQLTests.java +++ b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/PostgreSQLTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2008 Oracle. All rights reserved. + * Copyright (c) 2007, 2009 Oracle. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0, which accompanies this distribution * and is available at http://www.eclipse.org/legal/epl-v10.html. @@ -59,6 +59,10 @@ public class PostgreSQLTests extends DTPPlatformTests { @Override protected String getDefaultJDBCURL() { + // using this URL will result in the DTP database containing a single + // catalog named "" - which, unfortunately, resembles the pseudo-catalog + // generated by DTP for databases that do not return any catalogs via + // JDBC metadata calls... return "jdbc:postgresql"; } @@ -74,7 +78,7 @@ public class PostgreSQLTests extends DTPPlatformTests { @Override protected boolean supportsCatalogs() { - return false; + return true; } @Override @@ -94,19 +98,19 @@ public class PostgreSQLTests extends DTPPlatformTests { this.executeUpdate("CREATE SCHEMA TEST1"); ((ICatalogObject) this.getDTPDatabase()).refresh(); - Schema schema1 = this.getDatabase().getSchemaForIdentifier("TEST1"); + Schema schema1 = this.getDefaultCatalog().getSchemaForIdentifier("TEST1"); assertNotNull(schema1); this.executeUpdate("CREATE SCHEMA TEST2"); - Schema schema2 = this.getDatabase().getSchemaForIdentifier("TEST2"); + Schema schema2 = this.getDefaultCatalog().getSchemaForIdentifier("TEST2"); assertNull(schema2); // should be null until refresh ((ICatalogObject) this.getDTPDatabase()).refresh(); assertSame(this.getDatabase(), listener.changedDatabase); - schema2 = this.getDatabase().getSchemaForIdentifier("TEST2"); + schema2 = this.getDefaultCatalog().getSchemaForIdentifier("TEST2"); assertNotNull(schema2); - assertNotSame(schema1, this.getDatabase().getSchemaForIdentifier("TEST1")); // we should have a new schema after the refresh + assertNotSame(schema1, this.getDefaultCatalog().getSchemaForIdentifier("TEST1")); // we should have a new schema after the refresh this.dropSchema("TEST2"); this.dropSchema("TEST1"); @@ -125,35 +129,35 @@ public class PostgreSQLTests extends DTPPlatformTests { this.executeUpdate("CREATE SCHEMA LOOKUP_TEST"); ((ICatalogObject) this.getDTPDatabase()).refresh(); - assertNull(this.getDatabase().getSchemaNamed("LOOKUP_TEST")); - assertNotNull(this.getDatabase().getSchemaForIdentifier("LOOKUP_TEST")); + assertNull(this.getDefaultCatalog().getSchemaNamed("LOOKUP_TEST")); + assertNotNull(this.getDefaultCatalog().getSchemaForIdentifier("LOOKUP_TEST")); - assertNotNull(this.getDatabase().getSchemaNamed("lookup_test")); - assertNotNull(this.getDatabase().getSchemaForIdentifier("lookup_test")); + assertNotNull(this.getDefaultCatalog().getSchemaNamed("lookup_test")); + assertNotNull(this.getDefaultCatalog().getSchemaForIdentifier("lookup_test")); - assertNull(this.getDatabase().getSchemaNamed("lookup_TEST")); - assertNotNull(this.getDatabase().getSchemaForIdentifier("lookup_TEST")); + assertNull(this.getDefaultCatalog().getSchemaNamed("lookup_TEST")); + assertNotNull(this.getDefaultCatalog().getSchemaForIdentifier("lookup_TEST")); - assertNotNull(this.getDatabase().getSchemaForIdentifier("\"lookup_test\"")); - assertNull(this.getDatabase().getSchemaForIdentifier("\"lookup_TEST\"")); - assertNull(this.getDatabase().getSchemaForIdentifier("\"LOOKUP_TEST\"")); + assertNotNull(this.getDefaultCatalog().getSchemaForIdentifier("\"lookup_test\"")); + assertNull(this.getDefaultCatalog().getSchemaForIdentifier("\"lookup_TEST\"")); + assertNull(this.getDefaultCatalog().getSchemaForIdentifier("\"LOOKUP_TEST\"")); this.dropSchema("LOOKUP_TEST"); this.executeUpdate("CREATE SCHEMA \"lookup_TEST\""); ((ICatalogObject) this.getDTPDatabase()).refresh(); - assertNull(this.getDatabase().getSchemaNamed("LOOKUP_TEST")); - assertNull(this.getDatabase().getSchemaForIdentifier("LOOKUP_TEST")); + assertNull(this.getDefaultCatalog().getSchemaNamed("LOOKUP_TEST")); + assertNull(this.getDefaultCatalog().getSchemaForIdentifier("LOOKUP_TEST")); - assertNull(this.getDatabase().getSchemaNamed("lookup_test")); - assertNull(this.getDatabase().getSchemaForIdentifier("lookup_test")); + assertNull(this.getDefaultCatalog().getSchemaNamed("lookup_test")); + assertNull(this.getDefaultCatalog().getSchemaForIdentifier("lookup_test")); - assertNotNull(this.getDatabase().getSchemaNamed("lookup_TEST")); - assertNull(this.getDatabase().getSchemaForIdentifier("lookup_TEST")); + assertNotNull(this.getDefaultCatalog().getSchemaNamed("lookup_TEST")); + assertNull(this.getDefaultCatalog().getSchemaForIdentifier("lookup_TEST")); - assertNull(this.getDatabase().getSchemaForIdentifier("\"LOOKUP_TEST\"")); - assertNotNull(this.getDatabase().getSchemaForIdentifier("\"lookup_TEST\"")); + assertNull(this.getDefaultCatalog().getSchemaForIdentifier("\"LOOKUP_TEST\"")); + assertNotNull(this.getDefaultCatalog().getSchemaForIdentifier("\"lookup_TEST\"")); this.dropSchema("\"lookup_TEST\""); @@ -173,15 +177,15 @@ public class PostgreSQLTests extends DTPPlatformTests { this.executeUpdate("CREATE SCHEMA \"lookup_TEST\""); ((ICatalogObject) this.getDTPDatabase()).refresh(); - Schema schema = this.getDatabase().getSchemaForIdentifier("LOOKUP_TEST"); + Schema schema = this.getDefaultCatalog().getSchemaForIdentifier("LOOKUP_TEST"); assertEquals("lookup_test", schema.getIdentifier()); assertEquals("lookup_test", schema.getIdentifier("LookupTest")); assertNull(schema.getIdentifier("Lookup_Test")); - schema = this.getDatabase().getSchemaNamed("lookup_test"); + schema = this.getDefaultCatalog().getSchemaNamed("lookup_test"); assertEquals("lookup_test", schema.getIdentifier()); - schema = this.getDatabase().getSchemaForIdentifier("\"lookup_TEST\""); + schema = this.getDefaultCatalog().getSchemaForIdentifier("\"lookup_TEST\""); assertEquals("\"lookup_TEST\"", schema.getIdentifier()); assertEquals("\"lookup_TEST\"", schema.getIdentifier("lookup_TEST")); @@ -212,7 +216,7 @@ public class PostgreSQLTests extends DTPPlatformTests { this.executeUpdate(this.buildFooBazDDL()); ((ICatalogObject) this.getDTPDatabase()).refresh(); - Schema schema = this.getDatabase().getSchemaForIdentifier("TABLE_TEST"); + Schema schema = this.getDefaultCatalog().getSchemaForIdentifier("TABLE_TEST"); // FOO Table fooTable = schema.getTableForIdentifier("FOO"); @@ -335,7 +339,7 @@ public class PostgreSQLTests extends DTPPlatformTests { this.executeUpdate("CREATE TABLE test (id int, name varchar(20))"); ((ICatalogObject) this.getDTPDatabase()).refresh(); - Table table = this.getDatabase().getSchemaForIdentifier("TABLE_TEST").getTableForIdentifier("test"); + Table table = this.getDefaultCatalog().getSchemaForIdentifier("TABLE_TEST").getTableForIdentifier("test"); assertNotNull(table.getColumnForIdentifier("id")); assertNotNull(table.getColumnForIdentifier("name")); @@ -345,7 +349,7 @@ public class PostgreSQLTests extends DTPPlatformTests { this.executeUpdate("CREATE TABLE test (ID int, NAME varchar(20))"); ((ICatalogObject) this.getDTPDatabase()).refresh(); - table = this.getDatabase().getSchemaForIdentifier("TABLE_TEST").getTableForIdentifier("test"); + table = this.getDefaultCatalog().getSchemaForIdentifier("TABLE_TEST").getTableForIdentifier("test"); assertNotNull(table.getColumnForIdentifier("ID")); assertNotNull(table.getColumnForIdentifier("NAME")); @@ -355,7 +359,7 @@ public class PostgreSQLTests extends DTPPlatformTests { this.executeUpdate("CREATE TABLE test (Id int, Name varchar(20))"); ((ICatalogObject) this.getDTPDatabase()).refresh(); - table = this.getDatabase().getSchemaForIdentifier("TABLE_TEST").getTableForIdentifier("test"); + table = this.getDefaultCatalog().getSchemaForIdentifier("TABLE_TEST").getTableForIdentifier("test"); assertNotNull(table.getColumnForIdentifier("Id")); assertNotNull(table.getColumnForIdentifier("Name")); @@ -365,7 +369,7 @@ public class PostgreSQLTests extends DTPPlatformTests { this.executeUpdate("CREATE TABLE test (\"Id\" int, \"Name\" varchar(20))"); ((ICatalogObject) this.getDTPDatabase()).refresh(); - table = this.getDatabase().getSchemaForIdentifier("TABLE_TEST").getTableForIdentifier("test"); + table = this.getDefaultCatalog().getSchemaForIdentifier("TABLE_TEST").getTableForIdentifier("test"); assertNotNull(table.getColumnForIdentifier("\"Id\"")); assertNotNull(table.getColumnForIdentifier("\"Name\"")); @@ -376,8 +380,57 @@ public class PostgreSQLTests extends DTPPlatformTests { this.connectionProfile.disconnect(); } + public void testCrossSchemaReference() throws Exception { + this.connectionProfile.connect(); + TestConnectionListener listener = new TestConnectionListener(); + this.connectionProfile.addConnectionListener(listener); + + this.dropTable("XREF_TEST2", "EMP"); + this.dropSchema("XREF_TEST2"); + this.dropTable("XREF_TEST1", "ORG"); + this.dropSchema("XREF_TEST1"); + + this.executeUpdate("CREATE SCHEMA XREF_TEST1"); + this.executeUpdate("SET search_path TO XREF_TEST1"); + this.executeUpdate("CREATE TABLE ORG (ID integer PRIMARY KEY, NAME varchar(20))"); + + this.executeUpdate("CREATE SCHEMA XREF_TEST2"); + this.executeUpdate("SET search_path TO XREF_TEST2"); + this.executeUpdate("CREATE TABLE EMP (ID integer PRIMARY KEY, NAME varchar(20), " + + "ORG_ID integer REFERENCES XREF_TEST1.ORG(ID))"); + + ((ICatalogObject) this.getDTPDatabase()).refresh(); + Schema schema1 = this.getDatabase().getDefaultCatalog().getSchemaNamed("xref_test1"); + assertNotNull(schema1); + Table orgTable = schema1.getTableNamed("org"); + assertNotNull(orgTable); + + Schema schema2 = this.getDatabase().getDefaultCatalog().getSchemaNamed("xref_test2"); + assertNotNull(schema2); + Table empTable = schema2.getTableNamed("emp"); + assertNotNull(empTable); + assertEquals(1, empTable.foreignKeysSize()); + ForeignKey fk = empTable.foreignKeys().next(); + Table refTable = fk.getReferencedTable(); + assertNotNull(refTable); + assertEquals("org", refTable.getName()); + assertEquals(1, fk.columnPairsSize()); + ForeignKey.ColumnPair cp = fk.columnPairs().next(); + Column baseColumn = cp.getBaseColumn(); + assertEquals("org_id", baseColumn.getName()); + Column refColumn = cp.getReferencedColumn(); + assertEquals("id", refColumn.getName()); + + this.dropTable("XREF_TEST2", "EMP"); + this.dropSchema("XREF_TEST2"); + this.dropTable("XREF_TEST1", "ORG"); + this.dropSchema("XREF_TEST1"); + this.connectionProfile.removeConnectionListener(listener); + this.connectionProfile.disconnect(); + } + private void dropTable(String schemaName, String tableName) throws Exception { - Schema schema= this.getSchemaForIdentifier(schemaName); + Schema schema= this.getDefaultCatalog().getSchemaForIdentifier(schemaName); if (schema != null) { if (schema.getTableForIdentifier(tableName) != null) { this.executeUpdate("DROP TABLE " + schemaName + '.' + tableName); @@ -386,7 +439,7 @@ public class PostgreSQLTests extends DTPPlatformTests { } private void dropSchema(String name) throws Exception { - if (this.getSchemaForIdentifier(name) != null) { + if (this.getDefaultCatalog().getSchemaForIdentifier(name) != null) { this.executeUpdate("DROP SCHEMA " + name + " CASCADE"); } } @@ -409,7 +462,7 @@ public class PostgreSQLTests extends DTPPlatformTests { //// System.out.println(list); // ((ICatalogObject) this.getDTPDatabase()).refresh(); // -// Schema schema = this.getDatabase().getSchemaNamed("SEQUENCE_TEST"); +// Schema schema = this.getDefaultCatalog().getSchemaNamed("SEQUENCE_TEST"); // Sequence sequence = schema.getSequenceNamed("FOO"); // assertNotNull(sequence); // assertEquals("foo_seq", sequence.getName()); @@ -422,7 +475,7 @@ public class PostgreSQLTests extends DTPPlatformTests { // } // // private void dropSequence(String schemaName, String sequenceName) throws Exception { -// Schema schema= this.getSchemaNamed(schemaName); +// Schema schema= this.getDefaultCatalog().getSchemaNamed(schemaName); // if (schema != null) { // if (schema.getSequenceNamed(sequenceName) != null) { // this.executeUpdate("DROP SEQUENCE " + schemaName + '.' + sequenceName); diff --git a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/SybaseTests.java b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/SybaseTests.java index 07917b2374..d822e514ed 100644 --- a/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/SybaseTests.java +++ b/jpa/tests/org.eclipse.jpt.db.tests/src/org/eclipse/jpt/db/tests/internal/platforms/SybaseTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2008 Oracle. All rights reserved. + * Copyright (c) 2007, 2009 Oracle. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0, which accompanies this distribution * and is available at http://www.eclipse.org/legal/epl-v10.html. @@ -15,6 +15,7 @@ import org.eclipse.jpt.db.Column; import org.eclipse.jpt.db.ForeignKey; import org.eclipse.jpt.db.Schema; import org.eclipse.jpt.db.Table; +import org.eclipse.jpt.db.tests.internal.platforms.DTPPlatformTests.TestConnectionListener; @SuppressWarnings("nls") public class SybaseTests extends DTPPlatformTests { @@ -111,7 +112,9 @@ public class SybaseTests extends DTPPlatformTests { assertNotNull(catalog2); Schema schema2 = catalog2.getDefaultSchema(); assertNotNull(schema2); - assertNotSame(catalog1, this.getDatabase().getSchemaNamed("test1")); // we should have a new schema after the refresh + + assertNotSame(catalog1, this.getDatabase().getCatalogNamed("test1")); // we should have a new catalog after the refresh + assertNotSame(schema1, this.getDatabase().getCatalogNamed("test1").getDefaultSchema()); // we should have a new schema after the refresh this.executeUpdate("drop database test2"); this.executeUpdate("drop database test1"); @@ -380,4 +383,62 @@ public class SybaseTests extends DTPPlatformTests { this.connectionProfile.disconnect(); } + public void testCrossSchemaReference() throws Exception { + this.connectionProfile.connect(); + TestConnectionListener listener = new TestConnectionListener(); + this.connectionProfile.addConnectionListener(listener); + + this.getJDBCConnection().setCatalog("master"); + this.executeUpdateIgnoreErrors("drop database xref_test2"); + this.executeUpdateIgnoreErrors("drop database xref_test1"); + + this.getJDBCConnection().setCatalog("master"); + this.executeUpdate("create database xref_test1"); + this.getJDBCConnection().setCatalog("xref_test1"); + this.executeUpdate("create table org (id integer primary key, name varchar(20))"); + + this.getJDBCConnection().setCatalog("master"); + this.executeUpdate("create database xref_test2"); + this.getJDBCConnection().setCatalog("xref_test2"); + this.executeUpdate("create table emp (id integer primary key, name varchar(20), " + + "org_id integer references xref_test1..org(id))"); + + ((ICatalogObject) this.getDTPDatabase()).refresh(); + Catalog catalog1 = this.getDatabase().getCatalogNamed("xref_test1"); + assertNotNull(catalog1); + Schema schema1 = catalog1.getSchemaNamed("dbo"); + assertNotNull(schema1); + Table orgTable = schema1.getTableNamed("org"); + assertNotNull(orgTable); + + Catalog catalog2 = this.getDatabase().getCatalogNamed("xref_test2"); + assertNotNull(catalog2); + Schema schema2 = catalog2.getSchemaNamed("dbo"); + assertNotNull(schema2); + Table empTable = schema2.getTableNamed("emp"); + assertNotNull(empTable); + assertEquals(1, empTable.foreignKeysSize()); + ForeignKey fk = empTable.foreignKeys().next(); + Table refTable = fk.getReferencedTable(); + assertNotNull(refTable); + assertEquals("org", refTable.getName()); + assertEquals(1, fk.columnPairsSize()); + ForeignKey.ColumnPair cp = fk.columnPairs().next(); + Column baseColumn = cp.getBaseColumn(); + assertEquals("org_id", baseColumn.getName()); + Column refColumn = cp.getReferencedColumn(); + assertEquals("id", refColumn.getName()); + + this.getJDBCConnection().setCatalog("xref_test2"); + this.executeUpdate("drop table emp"); + this.getJDBCConnection().setCatalog("xref_test1"); + this.executeUpdate("drop table org"); + this.getJDBCConnection().setCatalog("master"); + this.executeUpdate("drop database xref_test2"); + this.executeUpdate("drop database xref_test1"); + + this.connectionProfile.removeConnectionListener(listener); + this.connectionProfile.disconnect(); + } + } |