1 19 20 package org.netbeans.modules.j2ee.persistence.editor.completion.db; 21 22 import java.lang.ref.Reference ; 23 import java.lang.ref.WeakReference ; 24 import java.sql.Connection ; 25 import java.sql.DatabaseMetaData ; 26 import org.netbeans.junit.NbTestCase; 27 import org.netbeans.modules.db.test.jdbcstub.ConnectionImpl; 28 import org.netbeans.test.stub.api.Stub; 29 import org.netbeans.test.stub.api.StubDelegate; 30 31 35 public class DBMetaDataProviderTest extends NbTestCase { 36 37 private Connection conn; 38 private DatabaseMetaData metaData; 39 40 public DBMetaDataProviderTest(String testName) { 41 super(testName); 42 } 43 44 private void createConnection(String [] catalogNames) { 45 metaData = (DatabaseMetaData )Stub.create(DatabaseMetaData .class, new SimpleDatabaseMetaDataImpl(catalogNames)); 46 conn = (Connection )Stub.create(Connection .class, new ConnectionImpl(metaData)); 47 } 48 49 public void testSameProviderForSameConnection() { 50 createConnection(new String [0]); 51 52 DBMetaDataProvider provider1 = DBMetaDataProvider.get(conn, ""); 53 DBMetaDataProvider provider2 = DBMetaDataProvider.get(conn, ""); 54 assertSame("Same provider for the same connection", provider1, provider2); 55 } 56 57 public void testConnectionAndProviderLeak() { 58 createConnection(new String [0]); 59 DBMetaDataProvider provider = DBMetaDataProvider.get(conn, ""); 60 61 Reference ref = new WeakReference (conn); 62 conn = null; 63 assertGC("The connection can be GCd", ref); 64 65 DBMetaDataProvider.get((Connection )Stub.create(Connection .class), ""); 67 68 ref = new WeakReference (provider); 69 provider = null; 70 assertGC("The provider can be GCd", ref); 71 } 72 73 public void testGetCatalogs() throws Exception { 74 createConnection(new String [] { "cat2", "cat1" }); 75 DBMetaDataProvider provider = DBMetaDataProvider.get(conn, ""); 76 77 Catalog[] catalogs = provider.getCatalogs(); 78 assertEquals("cat1", catalogs[0].getName()); 79 assertEquals("cat2", catalogs[1].getName()); 80 81 assertSame(catalogs[0], provider.getCatalog("cat1")); 82 assertSame(catalogs[1], provider.getCatalog("cat2")); 83 } 84 85 public void testGetCatalogsCache() throws Exception { 86 createConnection(new String [] { "cat1", "cat2" }); 87 DBMetaDataProvider provider = DBMetaDataProvider.get(conn, ""); 88 89 Catalog[] catalogs1 = provider.getCatalogs(); 90 assertEquals("cat1", catalogs1[0].getName()); 91 assertEquals("cat2", catalogs1[1].getName()); 92 93 ((SimpleDatabaseMetaDataImpl)Stub.getDelegate(metaData)).setCatalogs(new String [] { "newcat1", "newcat2" }); 94 95 Catalog[] catalogs2 = provider.getCatalogs(); 96 assertEquals("cat1", catalogs2[0].getName()); 97 assertEquals("cat2", catalogs2[1].getName()); 98 } 99 100 public void testGetCatalogsWhenNoCatalogs() throws Exception { 101 createConnection(new String [0]); 102 DBMetaDataProvider provider = DBMetaDataProvider.get(conn, ""); 103 104 Catalog[] catalogs = provider.getCatalogs(); 105 assertNull(catalogs[0].getName()); 106 assertSame(catalogs[0], provider.getCatalog(null)); 107 } 108 } 109 | Popular Tags |