KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > editor > completion > db > DBMetaDataProviderTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.editor.completion.db;
21
22 import java.lang.ref.Reference JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import java.sql.Connection JavaDoc;
25 import java.sql.DatabaseMetaData JavaDoc;
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 /**
32  *
33  * @author Andrei Badea
34  */

35 public class DBMetaDataProviderTest extends NbTestCase {
36     
37     private Connection JavaDoc conn;
38     private DatabaseMetaData JavaDoc metaData;
39     
40     public DBMetaDataProviderTest(String JavaDoc testName) {
41         super(testName);
42     }
43     
44     private void createConnection(String JavaDoc[] catalogNames) {
45         metaData = (DatabaseMetaData JavaDoc)Stub.create(DatabaseMetaData JavaDoc.class, new SimpleDatabaseMetaDataImpl(catalogNames));
46         conn = (Connection JavaDoc)Stub.create(Connection JavaDoc.class, new ConnectionImpl(metaData));
47     }
48     
49     public void testSameProviderForSameConnection() {
50         createConnection(new String JavaDoc[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 JavaDoc[0]);
59         DBMetaDataProvider provider = DBMetaDataProvider.get(conn, "");
60         
61         Reference JavaDoc ref = new WeakReference JavaDoc(conn);
62         conn = null;
63         assertGC("The connection can be GCd", ref);
64         
65         // causes the stale entries (those, whose keys have been GCd) to be removed from the weak map
66
DBMetaDataProvider.get((Connection JavaDoc)Stub.create(Connection JavaDoc.class), "");
67         
68         ref = new WeakReference JavaDoc(provider);
69         provider = null;
70         assertGC("The provider can be GCd", ref);
71     }
72     
73     public void testGetCatalogs() throws Exception JavaDoc {
74         createConnection(new String JavaDoc[] { "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 JavaDoc {
86         createConnection(new String JavaDoc[] { "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 JavaDoc[] { "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 JavaDoc {
101         createConnection(new String JavaDoc[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