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 junit.framework.*; 25 import org.netbeans.modules.db.test.jdbcstub.ConnectionImpl; 26 import org.netbeans.test.stub.api.Stub; 27 28 32 public class CatalogTest extends TestCase { 33 34 private Connection conn; 35 private DatabaseMetaData metaData; 36 37 public CatalogTest(String testName) { 38 super(testName); 39 } 40 41 private void createConnection(String [] catalogNames, String [] schemaNames, String [][] tableNamesBySchema) { 42 metaData = (DatabaseMetaData )Stub.create(DatabaseMetaData .class, new SimpleDatabaseMetaDataImpl(catalogNames, schemaNames, tableNamesBySchema)); 43 conn = (Connection )Stub.create(Connection .class, new ConnectionImpl(metaData)); 44 } 45 46 public void testGetSchemasWhenDefaultCatalog() throws Exception { 47 createConnection(new String [0], new String [] { "schema2", "schema1" }, new String [0][0]); 48 49 DBMetaDataProvider provider = DBMetaDataProvider.get(conn, ""); 50 Catalog catalog = provider.getCatalog(null); 51 52 Schema[] schemas = catalog.getSchemas(); 53 assertEquals("schema1", schemas[0].getName()); 54 assertEquals("schema2", schemas[1].getName()); 55 56 assertSame(schemas[0], catalog.getSchema("schema1")); 57 assertSame(schemas[1], catalog.getSchema("schema2")); 58 } 59 60 public void testGetSchemasWhenMultipleCatalogs() throws Exception { 61 createConnection(new String [] { "cat2", "cat1" }, new String [0], new String [][] { new String [] { "schema2", "s2table2", "s2table1" }, new String [] { "schema1", "s1table1" } }); 62 63 DBMetaDataProvider provider = DBMetaDataProvider.get(conn, ""); 64 Catalog catalog = provider.getCatalog("cat1"); 65 66 Schema[] schemas = catalog.getSchemas(); 67 assertEquals("schema1", schemas[0].getName()); 68 assertEquals("schema2", schemas[1].getName()); 69 70 assertSame(schemas[0], catalog.getSchema("schema1")); 71 assertSame(schemas[1], catalog.getSchema("schema2")); 72 } 73 74 public void testGetSchemasCache() throws Exception { 75 createConnection(new String [] { "cat2", "cat1" }, new String [0], new String [][] { new String [] { "schema2", "s2table2", "s2table1" }, new String [] { "schema1", "s1table1" } }); 76 77 DBMetaDataProvider provider = DBMetaDataProvider.get(conn, ""); 78 Catalog catalog = provider.getCatalog("cat1"); 79 80 Schema[] schemas1 = catalog.getSchemas(); 81 assertEquals("schema1", schemas1[0].getName()); 82 assertEquals("schema2", schemas1[1].getName()); 83 84 ((SimpleDatabaseMetaDataImpl)Stub.getDelegate(metaData)).setTables(new String [][] { new String [] { "newschema2", "s2table2", "s2table1" }, new String [] { "newschema1", "s1table1" } }); 85 86 Schema[] schemas2 = catalog.getSchemas(); 87 assertEquals("schema1", schemas2[0].getName()); 88 assertEquals("schema2", schemas2[1].getName()); 89 } 90 } 91 | Popular Tags |