KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > test > TestDatabaseMetaData


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.test;
33
34 import java.sql.Connection JavaDoc;
35 import java.sql.DatabaseMetaData JavaDoc;
36 import java.sql.PreparedStatement JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38 import java.sql.ResultSetMetaData JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42
43 import junit.framework.TestCase;
44 import junit.framework.TestResult;
45
46 public class TestDatabaseMetaData extends TestBase {
47
48     public TestDatabaseMetaData(String JavaDoc name) {
49         super(name);
50     }
51
52     public void test() throws Exception JavaDoc {
53
54         Connection JavaDoc conn = newConnection();
55         PreparedStatement JavaDoc pstmt;
56         int updateCount;
57
58         try {
59             pstmt = conn.prepareStatement(
60                 "SET PROPERTY \"sql.enforce_strict_size\" true");
61
62             pstmt.executeUpdate();
63             pstmt.close();
64
65             pstmt = conn.prepareStatement("DROP TABLE t1 IF EXISTS");
66
67             pstmt.executeUpdate();
68             pstmt.close();
69
70             pstmt = conn.prepareStatement(
71                 "CREATE TABLE t1 (cha CHARACTER, dec DECIMAL, doub DOUBLE, lon BIGINT, \"IN\" INTEGER, sma SMALLINT, tin TINYINT, "
72                 + "dat DATE DEFAULT CURRENT_DATE, tim TIME DEFAULT CURRENT_TIME, timest TIMESTAMP DEFAULT CURRENT_TIMESTAMP );");
73             updateCount = pstmt.executeUpdate();
74
75             assertTrue("expected update count of zero", updateCount == 0);
76
77             pstmt = conn.prepareStatement("CREATE INDEX t1 ON t1 (cha );");
78             updateCount = pstmt.executeUpdate();
79             pstmt = conn.prepareStatement("DROP TABLE t2 IF EXISTS");
80             updateCount = pstmt.executeUpdate();
81             pstmt = conn.prepareStatement(
82                 "CREATE TABLE t2 (cha CHARACTER, dec DECIMAL, doub DOUBLE, lon BIGINT, \"IN\" INTEGER, sma SMALLINT, tin TINYINT, "
83                 + "dat DATE DEFAULT CURRENT_DATE, tim TIME DEFAULT CURRENT_TIME, timest TIMESTAMP DEFAULT CURRENT_TIMESTAMP );");
84             updateCount = pstmt.executeUpdate();
85             pstmt = conn.prepareStatement("CREATE INDEX t2 ON t2 (cha );");
86             updateCount = pstmt.executeUpdate();
87
88             DatabaseMetaData JavaDoc dbmd = conn.getMetaData();
89             ResultSet JavaDoc rsp = dbmd.getTablePrivileges(null, null, "T1");
90
91             while (rsp.next()) {
92                 System.out.println("Table: " + rsp.getString(3) + " priv: "
93                                    + rsp.getString(6));
94             }
95
96             rsp = dbmd.getIndexInfo(null, null, "T1", false, false);
97
98             while (rsp.next()) {
99                 System.out.println("Table: " + rsp.getString(3)
100                                    + " IndexName: " + rsp.getString(6));
101             }
102
103             rsp = dbmd.getIndexInfo(null, null, "T2", false, false);
104
105             while (rsp.next()) {
106                 System.out.println("Table: " + rsp.getString(3)
107                                    + " IndexName: " + rsp.getString(6));
108             }
109
110             pstmt = conn.prepareStatement("DROP INDEX t2 ON t2;");
111             updateCount = pstmt.executeUpdate();
112             rsp = dbmd.getIndexInfo(null, null, "T2", false, false);
113
114             assertTrue("expected getIndexInfo returns empty resultset",
115                        rsp.next() == false);
116
117             ResultSet JavaDoc rs = dbmd.getTables(null, null, "T1",
118                                           new String JavaDoc[]{ "TABLE" });
119             ArrayList JavaDoc tablesarr = new ArrayList JavaDoc();
120             int i;
121
122             for (i = 0; rs.next(); i++) {
123                 String JavaDoc tempstr =
124                     rs.getString("TABLE_NAME").trim().toLowerCase();
125
126                 tablesarr.add(tempstr);
127             }
128
129             rs.close();
130             assertTrue("expected table t1 count of 1", i == 1);
131
132             Iterator JavaDoc it = tablesarr.iterator();
133
134             for (; it.hasNext(); ) {
135
136                 // create new ArrayList and HashMap for the table
137
String JavaDoc tablename = ((String JavaDoc) it.next()).trim();
138                 List JavaDoc collist = new ArrayList JavaDoc(30);
139
140                 rs = dbmd.getColumns(null, null, tablename.toUpperCase(),
141                                      null);
142
143                 for (i = 0; rs.next(); i++) {
144                     collist.add(
145                         rs.getString("COLUMN_NAME").trim().toLowerCase());
146                 }
147
148                 rs.close();
149             }
150
151             pstmt = conn.prepareStatement("DROP TABLE t_1 IF EXISTS");
152
153             pstmt.executeUpdate();
154             pstmt.close();
155
156             pstmt = conn.prepareStatement(
157                 "CREATE TABLE t_1 (cha CHARACTER(10), dec DECIMAL(10,2), doub DOUBLE, lon BIGINT, \"IN\" INTEGER, sma SMALLINT, tin TINYINT, "
158                 + "dat DATE DEFAULT CURRENT_DATE, tim TIME DEFAULT CURRENT_TIME, timest TIMESTAMP DEFAULT CURRENT_TIMESTAMP, bool BOOLEAN );");
159             updateCount = pstmt.executeUpdate();
160
161             assertTrue("expected update count of zero", updateCount == 0);
162
163             rs = dbmd.getTables(null, null, "T\\_1", new String JavaDoc[]{ "TABLE" });
164
165             for (i = 0; rs.next(); i++) {
166                 String JavaDoc tempstr =
167                     rs.getString("TABLE_NAME").trim().toLowerCase();
168
169                 tablesarr.add(tempstr);
170             }
171
172             rs.close();
173             assertTrue("expected table t_1 count of 1", i == 1);
174
175             // test various methods
176
dbmd.getPrimaryKeys(null, null, "T_1");
177             dbmd.getImportedKeys(null, null, "T_1");
178             dbmd.getCrossReference(null, null, "T_1", null, null, "T_1");
179
180             // test ResultSetMetaData
181
pstmt = conn.prepareStatement(
182                 "INSERT INTO T_1 (cha, dec, doub) VALUES ('name', 10.23, 0)");
183
184             pstmt.executeUpdate();
185             pstmt.close();
186
187             pstmt = conn.prepareStatement("SELECT * FROM T_1");
188             rs = pstmt.executeQuery();
189
190             ResultSetMetaData JavaDoc md = rs.getMetaData();
191             int x = md.getColumnDisplaySize(1);
192             int y = md.getColumnDisplaySize(2);
193             int b = md.getPrecision(2);
194             int c = md.getScale(1);
195             int d = md.getScale(2);
196             String JavaDoc e = md.getColumnClassName(10);
197             boolean testresult = (x == 10) && (y == 13) && (b == 10)
198                                  && (c == 0) && (d == 2)
199                                  && e.equals("java.sql.Timestamp");
200
201             assertTrue("wrong result metadata", testresult);
202
203             e = md.getColumnClassName(11);
204             testresult = e.equals("java.lang.Boolean");
205
206             assertTrue("wrong result metadata", testresult);
207             pstmt.close();
208             conn.close();
209         } catch (Exception JavaDoc e) {
210             assertTrue("unable to prepare or execute DDL", false);
211         } finally {
212             conn.close();
213         }
214     }
215
216     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
217
218         TestResult result;
219         TestCase test;
220         java.util.Enumeration JavaDoc failures;
221         int count;
222
223         result = new TestResult();
224         test = new TestDatabaseMetaData("test");
225
226         test.run(result);
227
228         count = result.failureCount();
229
230         System.out.println("TestDatabaseMetaData failure count: " + count);
231
232         failures = result.failures();
233
234         while (failures.hasMoreElements()) {
235             System.out.println(failures.nextElement());
236         }
237     }
238 }
239
Popular Tags