1 19 20 package org.netbeans.modules.j2ee.persistence.editor.completion.db; 21 22 import java.sql.Connection ; 23 import java.sql.DatabaseMetaData ; 24 import java.sql.ResultSet ; 25 import java.sql.SQLException ; 26 import java.sql.Types ; 27 import java.util.Arrays ; 28 import java.util.HashSet ; 29 import junit.framework.*; 30 import org.netbeans.modules.db.test.jdbcstub.ConnectionImpl; 31 import org.netbeans.modules.db.test.jdbcstub.DatabaseMetaDataImpl; 32 import org.netbeans.modules.db.test.jdbcstub.JDBCStubUtil; 33 import org.netbeans.modules.dbschema.ColumnPairElement; 34 import org.netbeans.test.stub.api.Stub; 35 import org.netbeans.modules.dbschema.TableElement; 36 37 41 public class SchemaTest extends TestCase { 42 43 private Connection conn; 44 private DatabaseMetaData metaData; 45 46 public SchemaTest(String testName) { 47 super(testName); 48 } 49 50 private void createConnection(String [] catalogNames, String [] schemaNames, String [][] tableNamesBySchema) { 51 metaData = (DatabaseMetaData )Stub.create(DatabaseMetaData .class, new MyDatabaseMetaDataImpl()); 52 conn = (Connection )Stub.create(Connection .class, new ConnectionImpl(metaData)); 53 } 54 55 public void testGetTableNames() throws SQLException { 56 createConnection(new String [0], new String [] { "schema1" }, new String [][] { new String [] { "schema1", "s1table2", "s1table1" } }); 57 DBMetaDataProvider provider = DBMetaDataProvider.get(conn, ""); 58 59 String [] tableNames = provider.getCatalog(null).getSchema("schema1").getTableNames(); 60 assertEquals("s1table1", tableNames[0]); 61 assertEquals("s1table2", tableNames[1]); 62 } 63 64 public void testGetTableByName() throws SQLException { 65 System.setProperty("netbeans.debug.exceptions", "true"); 67 assertTrue(Boolean.getBoolean("netbeans.debug.exceptions")); 68 69 createConnection(new String [0], new String [] { "schema1" }, new String [][] { new String [] { "schema1", "s1table2", "s1table1" } }); 70 DBMetaDataProvider provider = DBMetaDataProvider.get(conn, ""); 71 72 TableElement table = provider.getCatalog(null).getSchema("schema1").getTable("s1table1"); 73 assertEquals(3, table.getColumns().length); 74 assertEquals("S1TABLE1_INTEGER_COL", table.getPrimaryKey().getColumns()[0].getName().getName()); 75 ColumnPairElement[] pairs = table.getForeignKeys()[0].getColumnPairs(); 76 assertEquals("S1TABLE1_FK_COL", pairs[0].getLocalColumn().getName().getName()); 77 assertEquals("S1TABLE2_INTEGER_COL", pairs[0].getReferencedColumn().getName().getName()); 78 } 79 80 public static final class MyDatabaseMetaDataImpl extends DatabaseMetaDataImpl { 81 82 public ResultSet getCatalogs() { 83 return JDBCStubUtil.catalogsResultSet(new String [0]); 84 } 85 86 public ResultSet getSchemas() { 87 return JDBCStubUtil.schemasResultSet(new String [] { "schema1" }); 88 } 89 90 public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String [] types) { 91 if (catalog == null && new HashSet (Arrays.asList(types)).contains("TABLE")) { 92 return JDBCStubUtil.tablesResultSet(new String [][] { { "schema1", "s1table2", "s1table1" } }); 93 } 94 return JDBCStubUtil.emptyResultSet(); 95 } 96 97 public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) { 98 if (catalog == null) { 99 if ("s1table1".equals(tableNamePattern)) { 100 return JDBCStubUtil.columnsResultSet( 101 new String [] { "S1TABLE1_INTEGER_COL", "S1TABLE1_VARCHAR_COL", "S1TABLE1_FK_COL" }, 102 new String [] { "INTEGER", "VARCAHR", "INTEGER" }, 103 new int[] { Types.INTEGER, Types.VARCHAR, Types.INTEGER }, 104 new int[] { 0, 20, 0 }, 105 new int[] { 0, 0, 0 }, 106 new int[] { DatabaseMetaData.columnNoNulls, DatabaseMetaData.columnNullable, DatabaseMetaData.columnNullable } 107 ); 108 109 } else if ("s1table2".equals(tableNamePattern)) { 110 return JDBCStubUtil.columnsResultSet( 111 new String [] { "S1TABLE2_INTEGER_COL" }, 112 new String [] { "INTEGER" }, 113 new int[] { Types.INTEGER }, 114 new int[] { 0 }, 115 new int[] { 0 }, 116 new int[] { DatabaseMetaData.columnNoNulls } 117 ); 118 } 119 } 120 return JDBCStubUtil.emptyResultSet(); 121 } 122 123 public ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) { 124 if (catalog == null) { 125 if ("s1table1".equals(table)) { 126 return JDBCStubUtil.indexesResultSet( 127 new String [] { "PK_S1TABLE1_INTEGER_COL" }, 128 new String [] { "S1TABLE1_INTEGER_COL" }, 129 new boolean[] { false } 130 ); 131 132 } else if ("s1table2".equals(table)) { 133 return JDBCStubUtil.indexesResultSet( 134 new String [] { "PK_S1TABLE2_INTEGER_COL" }, 135 new String [] { "S1TABLE2_INTEGER_COL" }, 136 new boolean[] { false } 137 ); 138 139 } 140 } 141 return JDBCStubUtil.emptyResultSet(); 142 } 143 144 public ResultSet getPrimaryKeys(String catalog, String schema, String table) { 145 if (catalog == null) { 146 if ("s1table1".equals(table)) { 147 return JDBCStubUtil.primaryKeysResultSet( 148 new String [] { "PK_S1TABLE1_INTEGER_COL" }, 149 new String [] { "S1TABLE1_INTEGER_COL" }, 150 new short[] { 0 } 151 ); 152 153 } else if ("s1table2".equals(table)) { 154 return JDBCStubUtil.primaryKeysResultSet( 155 new String [] { "PK_S1TABLE2_INTEGER_COL" }, 156 new String [] { "S1TABLE2_INTEGER_COL" }, 157 new short[] { 0 } 158 ); 159 } 160 } 161 return JDBCStubUtil.emptyResultSet(); 162 } 163 164 public ResultSet getImportedKeys(String catalog, String schema, String table) { 165 if (catalog == null) { 167 if ("s1table1".equals(table)) { 168 return JDBCStubUtil.importedKeysResultSet( 169 new String [] { "FK_S1TABLE1_FK_COL" }, 170 new String [] { null }, 171 new String [] { null }, 172 new String [] { "s1table2" }, 173 new String [] { "S1TABLE2_INTEGER_COL" }, 174 new String [] { null }, 175 new String [] { null }, 176 new String [] { "s1table1" }, 177 new String [] { "S1TABLE1_FK_COL" } 178 ); 179 } 180 } 181 182 return JDBCStubUtil.emptyResultSet(); 183 } 184 } 185 } 186 | Popular Tags |