KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.Connection JavaDoc;
23 import java.sql.DatabaseMetaData JavaDoc;
24 import junit.framework.*;
25 import org.netbeans.modules.db.test.jdbcstub.ConnectionImpl;
26 import org.netbeans.test.stub.api.Stub;
27
28 /**
29  *
30  * @author Andrei Badea
31  */

32 public class CatalogTest extends TestCase {
33     
34     private Connection JavaDoc conn;
35     private DatabaseMetaData JavaDoc metaData;
36     
37     public CatalogTest(String JavaDoc testName) {
38         super(testName);
39     }
40     
41     private void createConnection(String JavaDoc[] catalogNames, String JavaDoc[] schemaNames, String JavaDoc[][] tableNamesBySchema) {
42         metaData = (DatabaseMetaData JavaDoc)Stub.create(DatabaseMetaData JavaDoc.class, new SimpleDatabaseMetaDataImpl(catalogNames, schemaNames, tableNamesBySchema));
43         conn = (Connection JavaDoc)Stub.create(Connection JavaDoc.class, new ConnectionImpl(metaData));
44     }
45     
46     public void testGetSchemasWhenDefaultCatalog() throws Exception JavaDoc {
47         createConnection(new String JavaDoc[0], new String JavaDoc[] { "schema2", "schema1" }, new String JavaDoc[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 JavaDoc {
61         createConnection(new String JavaDoc[] { "cat2", "cat1" }, new String JavaDoc[0], new String JavaDoc[][] { new String JavaDoc[] { "schema2", "s2table2", "s2table1" }, new String JavaDoc[] { "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 JavaDoc {
75         createConnection(new String JavaDoc[] { "cat2", "cat1" }, new String JavaDoc[0], new String JavaDoc[][] { new String JavaDoc[] { "schema2", "s2table2", "s2table1" }, new String JavaDoc[] { "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 JavaDoc[][] { new String JavaDoc[] { "newschema2", "s2table2", "s2table1" }, new String JavaDoc[] { "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