KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.sql.ResultSet JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.sql.Types JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.HashSet JavaDoc;
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 /**
38  *
39  * @author Andrei Badea
40  */

41 public class SchemaTest extends TestCase {
42     
43     private Connection JavaDoc conn;
44     private DatabaseMetaData JavaDoc metaData;
45     
46     public SchemaTest(String JavaDoc testName) {
47         super(testName);
48     }
49     
50     private void createConnection(String JavaDoc[] catalogNames, String JavaDoc[] schemaNames, String JavaDoc[][] tableNamesBySchema) {
51         metaData = (DatabaseMetaData JavaDoc)Stub.create(DatabaseMetaData JavaDoc.class, new MyDatabaseMetaDataImpl());
52         conn = (Connection JavaDoc)Stub.create(Connection JavaDoc.class, new ConnectionImpl(metaData));
53     }
54     
55     public void testGetTableNames() throws SQLException JavaDoc {
56         createConnection(new String JavaDoc[0], new String JavaDoc[] { "schema1" }, new String JavaDoc[][] { new String JavaDoc[] { "schema1", "s1table2", "s1table1" } });
57         DBMetaDataProvider provider = DBMetaDataProvider.get(conn, "");
58         
59         String JavaDoc[] 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 JavaDoc {
65         // to display the exceptions in dbschema
66
System.setProperty("netbeans.debug.exceptions", "true");
67         assertTrue(Boolean.getBoolean("netbeans.debug.exceptions"));
68         
69         createConnection(new String JavaDoc[0], new String JavaDoc[] { "schema1" }, new String JavaDoc[][] { new String JavaDoc[] { "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 JavaDoc getCatalogs() {
83             return JDBCStubUtil.catalogsResultSet(new String JavaDoc[0]);
84         }
85         
86         public ResultSet JavaDoc getSchemas() {
87             return JDBCStubUtil.schemasResultSet(new String JavaDoc[] { "schema1" });
88         }
89         
90         public ResultSet JavaDoc getTables(String JavaDoc catalog, String JavaDoc schemaPattern, String JavaDoc tableNamePattern, String JavaDoc[] types) {
91             if (catalog == null && new HashSet JavaDoc(Arrays.asList(types)).contains("TABLE")) {
92                 return JDBCStubUtil.tablesResultSet(new String JavaDoc[][] { { "schema1", "s1table2", "s1table1" } });
93             }
94             return JDBCStubUtil.emptyResultSet();
95         }
96         
97         public ResultSet JavaDoc getColumns(String JavaDoc catalog, String JavaDoc schemaPattern, String JavaDoc tableNamePattern, String JavaDoc columnNamePattern) {
98             if (catalog == null) {
99                 if ("s1table1".equals(tableNamePattern)) {
100                     return JDBCStubUtil.columnsResultSet(
101                             new String JavaDoc[] { "S1TABLE1_INTEGER_COL", "S1TABLE1_VARCHAR_COL", "S1TABLE1_FK_COL" },
102                             new String JavaDoc[] { "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 JavaDoc[] { "S1TABLE2_INTEGER_COL" },
112                             new String JavaDoc[] { "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 JavaDoc getIndexInfo(String JavaDoc catalog, String JavaDoc schema, String JavaDoc table, boolean unique, boolean approximate) {
124             if (catalog == null) {
125                 if ("s1table1".equals(table)) {
126                     return JDBCStubUtil.indexesResultSet(
127                             new String JavaDoc[] { "PK_S1TABLE1_INTEGER_COL" },
128                             new String JavaDoc[] { "S1TABLE1_INTEGER_COL" },
129                             new boolean[] { false }
130                     );
131                     
132                 } else if ("s1table2".equals(table)) {
133                     return JDBCStubUtil.indexesResultSet(
134                             new String JavaDoc[] { "PK_S1TABLE2_INTEGER_COL" },
135                             new String JavaDoc[] { "S1TABLE2_INTEGER_COL" },
136                             new boolean[] { false }
137                     );
138                     
139                 }
140             }
141             return JDBCStubUtil.emptyResultSet();
142         }
143         
144         public ResultSet JavaDoc getPrimaryKeys(String JavaDoc catalog, String JavaDoc schema, String JavaDoc table) {
145             if (catalog == null) {
146                 if ("s1table1".equals(table)) {
147                     return JDBCStubUtil.primaryKeysResultSet(
148                             new String JavaDoc[] { "PK_S1TABLE1_INTEGER_COL" },
149                             new String JavaDoc[] { "S1TABLE1_INTEGER_COL" },
150                             new short[] { 0 }
151                     );
152                     
153                 } else if ("s1table2".equals(table)) {
154                     return JDBCStubUtil.primaryKeysResultSet(
155                             new String JavaDoc[] { "PK_S1TABLE2_INTEGER_COL" },
156                             new String JavaDoc[] { "S1TABLE2_INTEGER_COL" },
157                             new short[] { 0 }
158                     );
159                 }
160             }
161             return JDBCStubUtil.emptyResultSet();
162         }
163         
164         public ResultSet JavaDoc getImportedKeys(String JavaDoc catalog, String JavaDoc schema, String JavaDoc table) {
165             //return JDBCStubUtil.emptyResultSet();
166
if (catalog == null) {
167                 if ("s1table1".equals(table)) {
168                     return JDBCStubUtil.importedKeysResultSet(
169                             new String JavaDoc[] { "FK_S1TABLE1_FK_COL" },
170                             new String JavaDoc[] { null },
171                             new String JavaDoc[] { null },
172                             new String JavaDoc[] { "s1table2" },
173                             new String JavaDoc[] { "S1TABLE2_INTEGER_COL" },
174                             new String JavaDoc[] { null },
175                             new String JavaDoc[] { null },
176                             new String JavaDoc[] { "s1table1" },
177                             new String JavaDoc[] { "S1TABLE1_FK_COL" }
178                     );
179                 }
180             }
181             
182             return JDBCStubUtil.emptyResultSet();
183         }
184     }
185 }
186
Popular Tags