001: /*
002: * $Id: AbstractFunctionalTest.java,v 1.11 2005/05/02 22:32:02 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.functional;
042:
043: import java.io.File;
044: import java.sql.Connection;
045: import java.sql.DriverManager;
046: import java.sql.PreparedStatement;
047: import java.sql.ResultSet;
048: import java.sql.SQLException;
049: import java.sql.Statement;
050:
051: import junit.framework.TestCase;
052:
053: import org.axiondb.io.FileUtil;
054: import org.axiondb.jdbc.AxionConnection;
055:
056: /**
057: * @version $Revision: 1.11 $ $Date: 2005/05/02 22:32:02 $
058: * @author Chuck Burdick
059: * @author Rodney Waldhoff
060: * @author Amrish Lal
061: */
062: public abstract class AbstractFunctionalTest extends TestCase {
063:
064: //------------------------------------------------------------ Conventional
065:
066: public AbstractFunctionalTest(String testName) {
067: super (testName);
068: try {
069: Class.forName("org.axiondb.jdbc.AxionDriver");
070: } catch (Exception e) {
071: throw new RuntimeException(e.toString());
072: }
073: }
074:
075: //--------------------------------------------------------------- Lifecycle
076:
077: public void setUp() throws Exception {
078: _conn = (AxionConnection) (DriverManager
079: .getConnection(getConnectString()));
080: _stmt = _conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
081: ResultSet.CONCUR_READ_ONLY);
082: }
083:
084: public void tearDown() throws Exception {
085: try {
086: if (_rset != null)
087: _rset.close();
088: } catch (Exception t) {
089: }
090: try {
091: if (_stmt != null)
092: _stmt.close();
093: } catch (Exception t) {
094: }
095: try {
096: if (_conn != null)
097: _conn.close();
098: } catch (Exception t) {
099: }
100: _rset = null;
101: _stmt = null;
102: _conn = null;
103: {
104: Connection conn = DriverManager
105: .getConnection(getConnectString());
106: Statement stmt = conn.createStatement();
107: stmt.execute("shutdown");
108: stmt.close();
109: conn.close();
110: }
111: deleteFile(getDatabaseDirectory());
112: }
113:
114: protected boolean deleteFile(File file) throws Exception {
115: return FileUtil.delete(file);
116: }
117:
118: //-------------------------------------------------------------------- Util
119:
120: protected void assertNRows(int n, String query) throws SQLException {
121: ResultSet rset = null;
122: try {
123: rset = _stmt.executeQuery(query);
124: assertNRows(n, rset);
125: } finally {
126: if (null != rset) {
127: rset.close();
128: }
129: }
130: }
131:
132: protected void assertNRows(int n, ResultSet rset)
133: throws SQLException {
134: for (int i = 0; i < n; i++) {
135: assertTrue("Expected " + (i + 1) + "th row.", rset.next());
136: }
137: assertTrue("Did not expect " + (n + 1) + "th row.", !rset
138: .next());
139: }
140:
141: protected void assertOneRow(String query) throws SQLException {
142: assertNRows(1, query);
143: }
144:
145: protected void assertNoRows(String query) throws SQLException {
146: assertNRows(0, query);
147: }
148:
149: protected void assertNoRows(ResultSet rset) throws SQLException {
150: assertNRows(0, rset);
151: }
152:
153: protected void assertException(String query) {
154: try {
155: _stmt.execute(query);
156: fail("Expected Exception");
157: } catch (Exception e) {
158: // expected
159: }
160: }
161:
162: protected void assertExceptionOnRead(String query)
163: throws SQLException {
164: ResultSet rset = null;
165: try {
166: rset = _stmt.executeQuery(query);
167: rset.next();
168: Object obj = rset.getObject(1);
169: fail("Expected Exception, found " + obj);
170: } catch (Exception e) {
171: // expected
172: } finally {
173: if (null != rset) {
174: rset.close();
175: }
176: }
177: }
178:
179: protected void assertObjectResult(Object expected, ResultSet rset)
180: throws SQLException {
181: try {
182: assertTrue("Expected a row here.", rset.next());
183: assertEquals(expected, rset.getObject(1));
184: assertTrue("Expected no more rows here.", !rset.next());
185: } finally {
186: rset.close();
187: }
188: }
189:
190: protected void assertObjectResult(Object expected, String query)
191: throws SQLException {
192: assertObjectResult(expected, _stmt.executeQuery(query));
193: }
194:
195: protected void assertResult(Object[] expected, ResultSet rset)
196: throws SQLException {
197: try {
198: assertTrue("Expected a row here.", rset.next());
199: for (int i = 0; i < expected.length; i++) {
200: if (expected[i] == null) {
201: assertNull(rset.getString(i + 1));
202: assertTrue(rset.wasNull());
203: } else if (expected[i] instanceof Integer) {
204: assertEquals(((Number) expected[i]).intValue(),
205: rset.getInt(i + 1));
206: assertFalse(rset.wasNull());
207: } else if (expected[i] instanceof String) {
208: assertEquals(expected[i], rset.getString(i + 1));
209: assertFalse(rset.wasNull());
210: } else {
211: fail("Unexpected type "
212: + expected.getClass().getName()
213: + " in expected array. Please add a new case to the assertResult method.");
214: }
215: }
216: assertTrue("Expected no more rows here.", !rset.next());
217: } finally {
218: rset.close();
219: }
220: }
221:
222: protected void assertResult(Object[] expected, String query)
223: throws SQLException {
224: assertResult(expected, _stmt.executeQuery(query));
225: }
226:
227: protected void assertResult(String expected, ResultSet rset)
228: throws SQLException {
229: try {
230: assertTrue("Expected a row here.", rset.next());
231: assertEquals(expected, rset.getString(1));
232: assertTrue("Expected no more rows here.", !rset.next());
233: } finally {
234: rset.close();
235: }
236: }
237:
238: protected void assertResult(String expected, String query)
239: throws SQLException {
240: assertResult(expected, _stmt.executeQuery(query));
241: }
242:
243: protected void assertResult(int expected, ResultSet rset)
244: throws SQLException {
245: try {
246: assertTrue("Expected a row here.", rset.next());
247: assertEquals(expected, rset.getInt(1));
248: assertTrue("Expected no more rows here.", !rset.next());
249: } finally {
250: if (null != rset) {
251: rset.close();
252: }
253: }
254: }
255:
256: protected void assertResult(int expected, String query)
257: throws SQLException {
258: assertResult(expected, _stmt.executeQuery(query));
259: }
260:
261: protected void assertResult(float expected, String query)
262: throws SQLException {
263: ResultSet rset = null;
264: try {
265: rset = _stmt.executeQuery(query);
266: assertTrue("Expected a row here.", rset.next());
267: assertEquals(expected, rset.getFloat(1), 0.0001f);
268: assertTrue("Expected no more rows here.", !rset.next());
269: } finally {
270: if (null != rset) {
271: rset.close();
272: }
273: }
274: }
275:
276: protected void assertNullResult(String query) throws SQLException {
277: ResultSet rset = null;
278: try {
279: rset = _stmt.executeQuery(query);
280: assertTrue("Expected a row here.", rset.next());
281: assertNull(rset.getObject(1));
282: assertTrue(rset.wasNull());
283: assertTrue("Expected no more rows here.", !rset.next());
284: } finally {
285: if (null != rset) {
286: rset.close();
287: }
288: }
289: }
290:
291: protected void assertResult(boolean expected, String query)
292: throws SQLException {
293: ResultSet rset = null;
294: try {
295: rset = _stmt.executeQuery(query);
296: assertTrue("Expected a row here.", rset.next());
297: assertEquals(expected, rset.getBoolean(1));
298: assertTrue("Expected no more rows here.", !rset.next());
299: } finally {
300: if (null != rset) {
301: rset.close();
302: }
303: }
304: }
305:
306: protected String getConnectString() {
307: return "jdbc:axiondb:memdb";
308: }
309:
310: protected File getDatabaseDirectory() {
311: return null;
312: }
313:
314: protected void createTableFoo(boolean includeIndex)
315: throws Exception {
316: _stmt
317: .execute("create table FOO ( NUM integer, STR varchar2(255), NUMTWO integer )");
318: if (includeIndex) {
319: createIndexOnFoo();
320: }
321: }
322:
323: protected void createTableFoo() throws Exception {
324: createTableFoo(true);
325: }
326:
327: protected void createIndexOnFoo() throws Exception {
328: }
329:
330: protected void createAndPopulateDual() throws Exception {
331: Statement stmt = _conn.createStatement();
332: stmt.execute("create table DUAL ( DUMMY varchar(10) )");
333: stmt.executeUpdate("insert into DUAL values ( 'X' )");
334: stmt.close();
335: }
336:
337: protected void populateTableFoo() throws Exception {
338: PreparedStatement pstmt = _conn
339: .prepareStatement("insert into FOO ( NUM, STR, NUMTWO ) values ( ?, ?, ?)");
340: for (int i = 0; i < NUM_ROWS_IN_FOO; i++) {
341: pstmt.setInt(1, i);
342: pstmt.setString(2, String.valueOf(i));
343: pstmt.setInt(3, (i / 2));
344: pstmt.executeUpdate();
345: }
346: pstmt.close();
347: }
348:
349: protected void createTableBar() throws Exception {
350: _stmt
351: .execute("create table BAR ( ID integer, DESCR varchar(10), DESCR2 varchar(10) )");
352: }
353:
354: protected void populateTableBar() throws Exception {
355: PreparedStatement pstmt = _conn
356: .prepareStatement("insert into BAR ( ID, DESCR, DESCR2 ) values ( ?, ?, ?)");
357: for (int i = 0; i < NUM_ROWS_IN_BAR; i++) {
358: pstmt.setInt(1, i);
359: pstmt.setString(2, String.valueOf(i));
360: pstmt.setString(3, "Descr" + String.valueOf(i));
361: pstmt.executeUpdate();
362: }
363: pstmt.close();
364: }
365:
366: protected void createTableX() throws Exception {
367: _stmt.execute("CREATE TABLE x(a INTEGER, b INTEGER)");
368: }
369:
370: protected void dropTableX() throws Exception {
371: _stmt.execute("DROP TABLE x");
372: }
373:
374: protected void populateTableX() throws Exception {
375: PreparedStatement pstmt = _conn
376: .prepareStatement("INSERT INTO x VALUES(?,?)");
377: int count = 0;
378: int strt = 2;
379: while (count < NUM_ROWS_IN_X) {
380: pstmt.setInt(1, strt);
381: pstmt.setInt(2, strt);
382: strt++;
383: count++;
384: pstmt.executeUpdate();
385: }
386: pstmt.close();
387: }
388:
389: protected void createTableY() throws Exception {
390: _stmt.execute("CREATE TABLE Y(a INTEGER, b INTEGER)");
391: }
392:
393: protected void dropTableY() throws Exception {
394: _stmt.execute("DROP TABLE y");
395: }
396:
397: protected void populateTableY() throws Exception {
398: PreparedStatement pstmt = _conn
399: .prepareStatement("INSERT INTO y VALUES(?,?)");
400: int count = 0;
401: int strt = 1;
402: while (count < NUM_ROWS_IN_Y) {
403: pstmt.setInt(1, strt);
404: pstmt.setInt(2, (strt * 10));
405: strt++;
406: count++;
407: pstmt.executeUpdate();
408: }
409: pstmt.close();
410: }
411:
412: protected void createTableZ() throws Exception {
413: _stmt.execute("CREATE TABLE Z(a INTEGER, b INTEGER)");
414: }
415:
416: protected void dropTableZ() throws Exception {
417: _stmt.execute("DROP TABLE z");
418: }
419:
420: protected void populateTableZ() throws Exception {
421: PreparedStatement pstmt = _conn
422: .prepareStatement("INSERT INTO z VALUES(?,?)");
423: int count = 0;
424: int strt = 3;
425: while (count < NUM_ROWS_IN_Z) {
426: pstmt.setInt(1, strt);
427: pstmt.setInt(2, (strt * 100));
428: strt++;
429: count++;
430: pstmt.executeUpdate();
431: }
432: pstmt.close();
433: }
434:
435: protected void createTableStates() throws Exception {
436: _stmt.execute("CREATE TABLE states ( state varchar(20) )");
437: }
438:
439: protected void populateTableStates() throws Exception {
440: PreparedStatement pstmt = _conn
441: .prepareStatement("INSERT INTO states VALUES(?)");
442: String[] states = new String[] { "alabama", "alaska",
443: "arizona", "arkansas", "california", "colorado",
444: "connecticut", "delaware", "florida", "georgia",
445: "hawaii", "idaho", "illinois", "indiana", "iowa",
446: "kansas", "kentucky", "louisiana", "maine", "maryland",
447: "massachusetts", "michigan", "minnesota",
448: "mississippi", "missouri", "montana", "nebraska",
449: "nevada", "new hampshire", "new jersey", "new mexico",
450: "new york", "north carolina", "north dakota", "ohio",
451: "oklahoma", "oregon", "pennsylvania", "rhode island",
452: "south carolina", "south dakota", "tennessee", "texas",
453: "utah", "vermont", "viginia", "washington",
454: "west virginia", "wisconsin", "wyoming" };
455:
456: for (int i = 0; i < 50; i++) {
457: pstmt.setString(1, states[i]);
458: pstmt.executeUpdate();
459: }
460: pstmt.close();
461: }
462:
463: protected void createTableWords() throws Exception {
464: _stmt.execute("CREATE TABLE words ( word varchar(10) )");
465: }
466:
467: protected void populateTableWords() throws Exception {
468: PreparedStatement pstmt = _conn
469: .prepareStatement("INSERT INTO words VALUES(?)");
470: String[] words = new String[] { "bat", "bait", "bet", "bent",
471: "bit", "bot", "bolt", "but", "bunt", "big", "bing",
472: "bog", "borg", "cat", "cot", "cut", "dang", "dog",
473: "dig", "drag", "fat", "falt", "fig", "fist", "fit",
474: "fog", "frog" };
475:
476: for (int i = 0; i < words.length; i++) {
477: pstmt.setString(1, words[i]);
478: pstmt.executeUpdate();
479: }
480: pstmt.close();
481: }
482:
483: protected void createSequenceFooSeq() throws Exception {
484: _stmt.execute("create sequence foo_seq");
485: }
486:
487: protected void dropSequenceFooSeq() throws Exception {
488: _stmt.execute("drop sequence foo_seq");
489: }
490:
491: protected static final int NUM_ROWS_IN_FOO = 6; // assumed to be even in some tests
492: protected static final int NUM_ROWS_IN_BAR = 6;
493: protected static final int NUM_ROWS_IN_X = 3;
494: protected static final int NUM_ROWS_IN_Y = 3;
495: protected static final int NUM_ROWS_IN_Z = 3;
496: protected AxionConnection _conn = null;
497: protected ResultSet _rset = null;
498: protected Statement _stmt = null;
499: }
|