|
10 | 10 | * Executes all known tests for JDBC2 and includes some utility methods.
|
11 | 11 | */
|
12 | 12 | publicclassJDBC2TestsextendsTestSuite {
|
13 |
| -/** |
14 |
| - * Returns the Test database JDBC URL |
15 |
| - */ |
16 |
| -publicstaticStringgetURL() { |
17 |
| -returnSystem.getProperty("database"); |
18 |
| -} |
19 |
| - |
20 |
| -/** |
21 |
| - * Returns the Postgresql username |
22 |
| - */ |
23 |
| -publicstaticStringgetUser() { |
24 |
| -returnSystem.getProperty("username"); |
25 |
| -} |
26 |
| - |
27 |
| -/** |
28 |
| - * Returns the user's password |
29 |
| - */ |
30 |
| -publicstaticStringgetPassword() { |
31 |
| -returnSystem.getProperty("password"); |
32 |
| -} |
33 |
| - |
34 |
| -/** |
35 |
| - * helper - opens a connection. Static so other classes can call it. |
36 |
| - */ |
37 |
| -publicstaticjava.sql.ConnectionopenDB() { |
38 |
| -try { |
39 |
| -Class.forName("org.postgresql.Driver"); |
40 |
| -returnjava.sql.DriverManager.getConnection(JDBC2Tests.getURL(),JDBC2Tests.getUser(),JDBC2Tests.getPassword()); |
41 |
| -}catch(ClassNotFoundExceptionex) { |
42 |
| -TestCase.assert(ex.getMessage(),false); |
43 |
| -}catch(SQLExceptionex) { |
44 |
| -TestCase.assert(ex.getMessage(),false); |
45 |
| -} |
46 |
| -returnnull; |
47 |
| -} |
48 |
| - |
49 |
| -/** |
50 |
| - * Helper - closes an open connection. This rewrites SQLException to a failed |
51 |
| - * assertion. It's static so other classes can use it. |
52 |
| - */ |
53 |
| -publicstaticvoidcloseDB(Connectionconn) { |
54 |
| -try { |
55 |
| -if(conn!=null) |
56 |
| -conn.close(); |
57 |
| -}catch(SQLExceptionex) { |
58 |
| -TestCase.assert(ex.getMessage(),false); |
59 |
| -} |
60 |
| -} |
| 13 | +/** |
| 14 | + * Returns the Test database JDBC URL |
| 15 | + */ |
| 16 | +publicstaticStringgetURL() { |
| 17 | +returnSystem.getProperty("database"); |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Returns the Postgresql username |
| 22 | + */ |
| 23 | +publicstaticStringgetUser() { |
| 24 | +returnSystem.getProperty("username"); |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Returns the user's password |
| 29 | + */ |
| 30 | +publicstaticStringgetPassword() { |
| 31 | +returnSystem.getProperty("password"); |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Helper - opens a connection. |
| 36 | + */ |
| 37 | +publicstaticjava.sql.ConnectionopenDB() { |
| 38 | +try { |
| 39 | +Class.forName("org.postgresql.Driver"); |
| 40 | +returnjava.sql.DriverManager.getConnection(JDBC2Tests.getURL(),JDBC2Tests.getUser(),JDBC2Tests.getPassword()); |
| 41 | +}catch(ClassNotFoundExceptionex) { |
| 42 | +TestCase.fail(ex.getMessage()); |
| 43 | +}catch(SQLExceptionex) { |
| 44 | +TestCase.fail(ex.getMessage()); |
| 45 | +} |
| 46 | +returnnull; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Helper - closes an open connection. This rewrites SQLException to a failed |
| 51 | + * assertion. It's static so other classes can use it. |
| 52 | + */ |
| 53 | +publicstaticvoidcloseDB(Connectioncon) { |
| 54 | +try { |
| 55 | +if (con !=null) |
| 56 | +con.close(); |
| 57 | +}catch(SQLExceptionex) { |
| 58 | +TestCase.fail(ex.getMessage()); |
| 59 | +} |
| 60 | +} |
61 | 61 |
|
62 | 62 | /**
|
63 | 63 | * Helper - creates a test table for use by a test
|
64 |
| -*/ |
65 |
| -publicstaticvoidcreateTable( |
66 |
| -Connectionconn,Stringtable,Stringcolumns) { |
| 64 | + */ |
| 65 | +publicstaticvoidcreateTable(Connectioncon, |
| 66 | +Stringtable, |
| 67 | +Stringcolumns) { |
67 | 68 | try {
|
68 |
| -Statementst =conn.createStatement(); |
| 69 | +Statementst =con.createStatement(); |
69 | 70 | try {
|
70 |
| -try { |
71 |
| -st.executeUpdate("drop table " +table); |
72 |
| -}catch(SQLExceptionse) { |
73 |
| -// Intentionally ignore exception |
74 |
| -} |
| 71 | +// Drop the table |
| 72 | +dropTable(con,table); |
75 | 73 |
|
76 | 74 | // Now create the table
|
77 |
| -st.executeUpdate("create table " +table +" (" +columns + |
78 |
| -")" ); |
| 75 | +st.executeUpdate("create table " +table +" (" +columns +")"); |
79 | 76 | }finally {
|
80 | 77 | st.close();
|
81 | 78 | }
|
82 | 79 | }catch(SQLExceptionex) {
|
83 |
| -TestCase.assert(ex.getMessage(),false); |
| 80 | +TestCase.fail(ex.getMessage()); |
| 81 | +} |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Helper - drops a table |
| 86 | + */ |
| 87 | +publicstaticvoiddropTable(Connectioncon,Stringtable) { |
| 88 | +try { |
| 89 | +Statementstmt =con.createStatement(); |
| 90 | +try { |
| 91 | +stmt.executeUpdate("DROP TABLE " +table); |
| 92 | +}catch (SQLExceptionex) { |
| 93 | +// ignore |
| 94 | +} |
| 95 | +}catch (SQLExceptionex) { |
| 96 | +TestCase.fail(ex.getMessage()); |
84 | 97 | }
|
85 | 98 | }
|
86 | 99 |
|
87 |
| -// Create the test table whose name is passed via the properties |
88 |
| -// (see ../../../build.xml). It appears that the original author of |
89 |
| -// this test suite intended to specify all test table names via the |
90 |
| -// properties, but this was never fully implemented. |
91 |
| -publicstaticvoidcreateTable(Connectionconn,Stringcolumns) { |
92 |
| -createTable(conn,getTableName(),columns); |
| 100 | +/** |
| 101 | + * Helper - generates INSERT SQL - very simple |
| 102 | + */ |
| 103 | +publicstaticStringinsertSQL(Stringtable,Stringvalues) { |
| 104 | +returninsertSQL(table,null,values); |
93 | 105 | }
|
| 106 | + |
| 107 | +publicstaticStringinsertSQL(Stringtable,Stringcolumns,Stringvalues) { |
| 108 | +Strings ="INSERT INTO " +table; |
| 109 | + |
| 110 | +if (columns !=null) |
| 111 | +s =s +" (" +columns +")"; |
| 112 | + |
| 113 | +returns +" VALUES (" +values +")"; |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Helper - generates SELECT SQL - very simple |
| 118 | + */ |
| 119 | +publicstaticStringselectSQL(Stringtable,Stringcolumns) { |
| 120 | +returnselectSQL(table,columns,null,null); |
| 121 | +} |
| 122 | + |
| 123 | +publicstaticStringselectSQL(Stringtable,Stringcolumns,Stringwhere) { |
| 124 | +returnselectSQL(table,columns,where,null); |
| 125 | +} |
| 126 | + |
| 127 | +publicstaticStringselectSQL(Stringtable,Stringcolumns,Stringwhere,Stringother) { |
| 128 | +Strings ="SELECT " +columns +" FROM " +table; |
| 129 | + |
| 130 | +if (where !=null) |
| 131 | +s =s +" WHERE " +where; |
| 132 | +if (other !=null) |
| 133 | +s =s +" " +other; |
| 134 | + |
| 135 | +returns; |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * Helper to prefix a number with leading zeros - ugly but it works... |
| 140 | + * @param v value to prefix |
| 141 | + * @param l number of digits (0-10) |
| 142 | + */ |
| 143 | +publicstaticStringfix(intv,intl) { |
| 144 | +Strings ="0000000000".substring(0,l) +Integer.toString(v); |
| 145 | +returns.substring(s.length() -l); |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * The main entry point for JUnit |
| 150 | + */ |
| 151 | +publicstaticTestSuitesuite() { |
| 152 | +TestSuitesuite=newTestSuite(); |
94 | 153 |
|
95 |
| -/** |
96 |
| - * Helper - generates INSERT SQL - very simple |
97 |
| - */ |
98 |
| -publicstaticStringinsert(Stringvalues) { |
99 |
| -returninsert(null,values); |
100 |
| - } |
101 |
| -publicstaticStringinsert(Stringcolumns,Stringvalues) { |
102 |
| -Strings ="INSERT INTO "+getTableName(); |
103 |
| -if(columns!=null) |
104 |
| -s=s+" ("+columns+")"; |
105 |
| -returns+" VALUES ("+values+")"; |
106 |
| - } |
107 |
| - |
108 |
| -/** |
109 |
| - * Helper - generates SELECT SQL - very simple |
110 |
| - */ |
111 |
| -publicstaticStringselect(Stringcolumns) { |
112 |
| -returnselect(columns,null,null); |
113 |
| - } |
114 |
| -publicstaticStringselect(Stringcolumns,Stringwhere) { |
115 |
| -returnselect(columns,where,null); |
116 |
| - } |
117 |
| -publicstaticStringselect(Stringcolumns,Stringwhere,Stringother) { |
118 |
| -Strings ="SELECT "+columns+" FROM "+getTableName(); |
119 |
| -if(where!=null) |
120 |
| -s=s+" WHERE "+where; |
121 |
| -if(other!=null) |
122 |
| -s=s+" "+other; |
123 |
| -returns; |
124 |
| - } |
125 |
| - |
126 |
| -/** |
127 |
| - * Helper - returns the test table's name |
128 |
| - * This is defined by the tablename property. If not defined it defaults to |
129 |
| - * jdbctest |
130 |
| - */ |
131 |
| -publicstaticStringgetTableName() { |
132 |
| -if(tablename==null) |
133 |
| -tablename=System.getProperty("tablename","jdbctest"); |
134 |
| -returntablename; |
135 |
| - } |
136 |
| - |
137 |
| -/** |
138 |
| - * As getTableName() but the id is a suffix. Used when more than one table is |
139 |
| - * required in a test. |
140 |
| - */ |
141 |
| -publicstaticStringgetTableName(Stringid) { |
142 |
| -if(tablename==null) |
143 |
| -tablename=System.getProperty("tablename","jdbctest"); |
144 |
| -returntablename+"_"+id; |
145 |
| - } |
146 |
| - |
147 |
| -/** |
148 |
| - * Cache used by getTableName() [its used a lot!] |
149 |
| - */ |
150 |
| -privatestaticStringtablename; |
151 |
| - |
152 |
| -/** |
153 |
| - * Helper to prefix a number with leading zeros - ugly but it works... |
154 |
| - * @param v value to prefix |
155 |
| - * @param l number of digits (0-10) |
156 |
| - */ |
157 |
| -publicstaticStringfix(intv,intl) { |
158 |
| -Strings ="0000000000".substring(0,l)+Integer.toString(v); |
159 |
| -returns.substring(s.length()-l); |
160 |
| - } |
161 |
| - |
162 |
| -/** |
163 |
| - * Number of milliseconds in a day |
164 |
| - */ |
165 |
| -publicstaticfinallongDAYMILLIS =24*3600*1000; |
166 |
| - |
167 |
| -/** |
168 |
| - * The main entry point for JUnit |
169 |
| - */ |
170 |
| -publicstaticTestSuitesuite() { |
171 |
| -TestSuitesuite=newTestSuite(); |
172 |
| - |
173 |
| -// |
174 |
| -// Add one line per class in our test cases. These should be in order of |
175 |
| -// complexity. |
176 |
| - |
177 |
| -// ANTTest should be first as it ensures that test parameters are |
178 |
| -// being sent to the suite. It also initialises the database (if required) |
179 |
| -// with some simple global tables (will make each testcase use its own later). |
180 |
| -// |
181 |
| -suite.addTestSuite(ANTTest.class); |
182 |
| - |
183 |
| -// Basic Driver internals |
184 |
| -suite.addTestSuite(DriverTest.class); |
185 |
| -suite.addTestSuite(ConnectionTest.class); |
186 |
| -suite.addTestSuite(DatabaseMetaDataTest.class); |
187 |
| -suite.addTestSuite(EncodingTest.class); |
188 |
| - |
189 |
| -// Connectivity/Protocols |
190 |
| - |
191 |
| -// ResultSet |
192 |
| -suite.addTestSuite(DateTest.class); |
193 |
| -suite.addTestSuite(TimeTest.class); |
194 |
| -suite.addTestSuite(TimestampTest.class); |
195 |
| - |
196 |
| -// PreparedStatement |
197 |
| -suite.addTestSuite(BatchExecuteTest.class); |
198 |
| - |
199 |
| -// BatchExecute |
200 |
| - |
201 |
| - |
202 |
| -// MetaData |
203 |
| - |
204 |
| -// Other misc tests, based on previous problems users have had or specific |
205 |
| -// features some applications require. |
206 |
| -suite.addTestSuite(JBuilderTest.class); |
207 |
| -suite.addTestSuite(MiscTest.class); |
208 |
| - |
209 |
| -// Fastpath/LargeObject |
210 |
| -suite.addTestSuite(BlobTest.class); |
211 |
| - |
212 |
| -// That's all folks |
213 |
| -returnsuite; |
214 |
| - } |
| 154 | +// |
| 155 | +// Add one line per class in our test cases. These should be in order of |
| 156 | +// complexity. |
| 157 | + |
| 158 | +// ANTTest should be first as it ensures that test parameters are |
| 159 | +// being sent to the suite. It also initialises the database (if required) |
| 160 | +// with some simple global tables (will make each testcase use its own later). |
| 161 | +// |
| 162 | +suite.addTestSuite(ANTTest.class); |
| 163 | + |
| 164 | +// Basic Driver internals |
| 165 | +suite.addTestSuite(DriverTest.class); |
| 166 | +suite.addTestSuite(ConnectionTest.class); |
| 167 | +suite.addTestSuite(DatabaseMetaDataTest.class); |
| 168 | +suite.addTestSuite(EncodingTest.class); |
| 169 | + |
| 170 | +// Connectivity/Protocols |
| 171 | + |
| 172 | +// ResultSet |
| 173 | + |
| 174 | +// Time, Date, Timestamp |
| 175 | +suite.addTestSuite(DateTest.class); |
| 176 | +suite.addTestSuite(TimeTest.class); |
| 177 | +suite.addTestSuite(TimestampTest.class); |
| 178 | + |
| 179 | +// PreparedStatement |
| 180 | + |
| 181 | +// BatchExecute |
| 182 | +suite.addTestSuite(BatchExecuteTest.class); |
| 183 | + |
| 184 | +// MetaData |
| 185 | + |
| 186 | +// Other misc tests, based on previous problems users have had or specific |
| 187 | +// features some applications require. |
| 188 | +suite.addTestSuite(JBuilderTest.class); |
| 189 | +suite.addTestSuite(MiscTest.class); |
| 190 | + |
| 191 | +// Fastpath/LargeObject |
| 192 | +suite.addTestSuite(BlobTest.class); |
| 193 | + |
| 194 | +// That's all folks |
| 195 | +returnsuite; |
| 196 | +} |
215 | 197 | }
|