001: /*
002: * $Id: TestPersistentDatabase.java,v 1.20 2005/05/02 22:32:03 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2003 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;
042:
043: import java.io.File;
044: import java.io.IOException;
045: import java.io.InputStream;
046: import java.io.OutputStream;
047: import java.sql.Connection;
048: import java.sql.DriverManager;
049: import java.sql.PreparedStatement;
050: import java.sql.ResultSet;
051: import java.sql.SQLException;
052: import java.sql.Statement;
053:
054: import junit.framework.Test;
055: import junit.framework.TestCase;
056: import junit.framework.TestSuite;
057:
058: import org.axiondb.io.AxionFileSystem;
059: import org.axiondb.io.FileUtil;
060:
061: /**
062: * @version $Revision: 1.20 $ $Date: 2005/05/02 22:32:03 $
063: * @author Rodney Waldhoff
064: */
065: public class TestPersistentDatabase extends TestCase {
066: private Connection _conn = null;
067: private ResultSet _rset = null;
068: private Statement _stmt = null;
069:
070: //------------------------------------------------------------ Conventional
071:
072: public TestPersistentDatabase(String testName) {
073: super (testName);
074: }
075:
076: public static Test suite() {
077: return new TestSuite(TestPersistentDatabase.class);
078: }
079:
080: //--------------------------------------------------------------- Lifecycle
081: private File _dbDir = null;
082: private String _connectString = "jdbc:axiondb:diskdb:testdb";
083:
084: public void setUp() throws Exception {
085: Class.forName("org.axiondb.jdbc.AxionDriver");
086: _dbDir = new File(".", "testdb");
087: openJDBC();
088: }
089:
090: public void tearDown() throws Exception {
091: closeJDBC();
092: FileUtil.delete(_dbDir);
093: _dbDir = null;
094: }
095:
096: //------------------------------------------------------------------- Tests
097:
098: public void testConstraintsPersist() throws Exception {
099: _stmt
100: .execute("create table foo ( id integer, str varchar(10) )");
101: _stmt
102: .execute("alter table foo add constraint foo_not_null not null ( str )");
103: assertEquals(1, _stmt
104: .executeUpdate("insert into foo values ( 1, 'one' )"));
105: try {
106: _stmt.executeUpdate("insert into foo values ( 2, null )");
107: fail("Expected SQLException");
108: } catch (SQLException e) {
109: // expected
110: }
111: {
112: _rset = _stmt.executeQuery("select * from FOO");
113: assertTrue(_rset.next());
114: assertEquals(1, _rset.getInt(1));
115: assertEquals("one", _rset.getString(2));
116: assertTrue(!_rset.next());
117: }
118:
119: closeJDBC();
120: openJDBC();
121:
122: assertEquals(1, _stmt
123: .executeUpdate("insert into foo values ( 2, 'two' )"));
124: try {
125: _stmt.executeUpdate("insert into foo values ( 3, null )");
126: fail("Expected SQLException");
127: } catch (SQLException e) {
128: // expected
129: }
130:
131: {
132: _rset = _stmt.executeQuery("select * from FOO order by id");
133: assertTrue(_rset.next());
134: assertEquals(1, _rset.getInt(1));
135: assertEquals("one", _rset.getString(2));
136: assertTrue(_rset.next());
137: assertEquals(2, _rset.getInt(1));
138: assertEquals("two", _rset.getString(2));
139: assertTrue(!_rset.next());
140: }
141:
142: }
143:
144: public void testConnected() throws Exception {
145: assertNotNull(_conn);
146: assertNotNull(_stmt);
147: }
148:
149: public void testCreateTable() throws Exception {
150: createTableFoo();
151: File tabledir = new File(_dbDir, "FOO");
152: assertTrue(tabledir.exists());
153: File metafile = new File(tabledir, "FOO.META");
154: assertTrue(metafile.exists());
155: }
156:
157: public void testCreateBadTableType() throws Exception {
158: try {
159: _stmt
160: .execute("create bogus table FOO ( NUM integer, STR varchar2, NUMTWO integer )");
161: fail("Expected Exception : Bad table type");
162: } catch (Exception e) {
163: // expected
164: }
165: }
166:
167: public void testCreatePopulateTable() throws Exception {
168: createTableFoo();
169: populateTableFoo();
170: File tabledir = new File(_dbDir, "FOO");
171: assertTrue(tabledir.exists());
172: File metafile = new File(tabledir, "FOO.META");
173: assertTrue(metafile.exists());
174: File datafile = new File(tabledir, "FOO.DATA");
175: assertTrue(datafile.toString(), datafile.exists());
176: _stmt.execute("shutdown");
177: assertTrue(datafile.length() > 8);
178: }
179:
180: public void testCreateClosePopulateTable() throws Exception {
181: createTableFoo();
182: closeJDBC();
183: openJDBC();
184: populateTableFoo();
185: File tabledir = new File(_dbDir, "FOO");
186: assertTrue(tabledir.exists());
187: File metafile = new File(tabledir, "FOO.META");
188: assertTrue(metafile.exists());
189: File datafile = new File(tabledir, "FOO.DATA");
190: assertTrue(datafile.exists());
191: _stmt.execute("shutdown");
192: assertTrue(datafile.length() > 8);
193: }
194:
195: public void testCreatePopulateSelectTable() throws Exception {
196: createTableFoo();
197: populateTableFoo();
198: _rset = _stmt.executeQuery("select NUM from FOO");
199: assertNotNull(_rset);
200: int expectedSum = 0;
201: int actualSum = 0;
202: for (int i = 0; i < 10; i++) {
203: expectedSum += i;
204: assertTrue(_rset.next());
205: actualSum += _rset.getInt(1);
206: assertTrue(!_rset.wasNull());
207: }
208: assertTrue(!_rset.next());
209: _rset.close();
210: assertEquals(expectedSum, actualSum);
211: }
212:
213: public void testCreatePopulateCloseSelectTable() throws Exception {
214: createTableFoo();
215: populateTableFoo();
216: closeJDBC();
217: openJDBC();
218: _rset = _stmt.executeQuery("select NUM from FOO");
219: assertNotNull(_rset);
220: int expectedSum = 0;
221: int actualSum = 0;
222: for (int i = 0; i < 10; i++) {
223: expectedSum += i;
224: assertTrue(_rset.next());
225: actualSum += _rset.getInt(1);
226: assertTrue(!_rset.wasNull());
227: }
228: assertTrue(!_rset.next());
229: _rset.close();
230: assertEquals(expectedSum, actualSum);
231: }
232:
233: public void testCreateClosePopulateCloseSelectTable()
234: throws Exception {
235: createTableFoo();
236: closeJDBC();
237: openJDBC();
238: populateTableFoo();
239: closeJDBC();
240: openJDBC();
241: _rset = _stmt.executeQuery("select NUM from FOO");
242: assertNotNull(_rset);
243: int expectedSum = 0;
244: int actualSum = 0;
245: for (int i = 0; i < 10; i++) {
246: expectedSum += i;
247: assertTrue(_rset.next());
248: actualSum += _rset.getInt(1);
249: assertTrue(!_rset.wasNull());
250: }
251: assertTrue(!_rset.next());
252: _rset.close();
253: assertEquals(expectedSum, actualSum);
254: }
255:
256: public void testCreateClosePopulateCloseSelectCloseSelectTable()
257: throws Exception {
258: createTableFoo();
259: closeJDBC();
260: openJDBC();
261: populateTableFoo();
262: closeJDBC();
263: openJDBC();
264: {
265: _rset = _stmt.executeQuery("select NUM from FOO");
266: assertNotNull(_rset);
267: int expectedSum = 0;
268: int actualSum = 0;
269: for (int i = 0; i < 10; i++) {
270: expectedSum += i;
271: assertTrue(_rset.next());
272: actualSum += _rset.getInt(1);
273: assertTrue(!_rset.wasNull());
274: }
275: assertTrue(!_rset.next());
276: _rset.close();
277: assertEquals(expectedSum, actualSum);
278: }
279: closeJDBC();
280: openJDBC();
281: {
282: _rset = _stmt.executeQuery("select NUM from FOO");
283: assertNotNull(_rset);
284: int expectedSum = 0;
285: int actualSum = 0;
286: for (int i = 0; i < 10; i++) {
287: expectedSum += i;
288: assertTrue(_rset.next());
289: actualSum += _rset.getInt(1);
290: assertTrue(!_rset.wasNull());
291: }
292: assertTrue(!_rset.next());
293: _rset.close();
294: assertEquals(expectedSum, actualSum);
295: }
296: }
297:
298: public void testCreatePopulateSelectDeleteCloseSelectTable()
299: throws Exception {
300: createTableFoo();
301: populateTableFoo();
302:
303: // select
304: {
305: _rset = _stmt.executeQuery("select NUM from FOO");
306: assertNotNull(_rset);
307: int expectedSum = 0;
308: int actualSum = 0;
309: for (int i = 0; i < 10; i++) {
310: expectedSum += i;
311: assertTrue(_rset.next());
312: actualSum += _rset.getInt(1);
313: assertTrue(!_rset.wasNull());
314: }
315: assertTrue(!_rset.next());
316: _rset.close();
317: assertEquals(expectedSum, actualSum);
318: }
319:
320: // delete
321: assertEquals(5, _stmt
322: .executeUpdate("delete from FOO where NUM >= 5"));
323: assertEquals(1, _stmt
324: .executeUpdate("delete from FOO where NUM = 3"));
325:
326: // close
327: closeJDBC();
328: openJDBC();
329:
330: // select
331: {
332: _rset = _stmt.executeQuery("select NUM from FOO");
333: assertNotNull(_rset);
334: int expectedSum = 0;
335: int actualSum = 0;
336: for (int i = 0; i < 3; i++) {
337: expectedSum += i;
338: assertTrue(_rset.next());
339: actualSum += _rset.getInt(1);
340: assertTrue(!_rset.wasNull());
341: }
342: for (int i = 4; i < 5; i++) {
343: expectedSum += i;
344: assertTrue(_rset.next());
345: actualSum += _rset.getInt(1);
346: assertTrue(!_rset.wasNull());
347: }
348: assertTrue(!_rset.next());
349: _rset.close();
350: assertEquals(expectedSum, actualSum);
351: }
352: }
353:
354: public void testCreateRemountPopulateRemountSelectRemountDeleteRemountSelectTable()
355: throws Exception {
356: createTableFoo();
357: PreparedStatement pstmt = _conn.prepareStatement("remount ?");
358: pstmt.setString(1, _dbDir.getCanonicalPath());
359: pstmt.execute();
360: populateTableFoo();
361: pstmt.setString(1, _dbDir.getCanonicalPath());
362: pstmt.execute();
363: // select
364: {
365: _rset = _stmt.executeQuery("select NUM from FOO");
366: assertNotNull(_rset);
367: int expectedSum = 0;
368: int actualSum = 0;
369: for (int i = 0; i < 10; i++) {
370: expectedSum += i;
371: assertTrue(_rset.next());
372: actualSum += _rset.getInt(1);
373: assertTrue(!_rset.wasNull());
374: }
375: assertTrue(!_rset.next());
376: _rset.close();
377: assertEquals(expectedSum, actualSum);
378: }
379: pstmt.setString(1, _dbDir.getCanonicalPath());
380: pstmt.execute();
381:
382: // delete
383: assertEquals(5, _stmt
384: .executeUpdate("delete from FOO where NUM >= 5"));
385: assertEquals(1, _stmt
386: .executeUpdate("delete from FOO where NUM = 3"));
387:
388: pstmt.setString(1, _dbDir.getCanonicalPath());
389: pstmt.execute();
390:
391: // select
392: {
393: _rset = _stmt.executeQuery("select NUM from FOO");
394: assertNotNull(_rset);
395: int expectedSum = 0;
396: int actualSum = 0;
397: for (int i = 0; i < 3; i++) {
398: expectedSum += i;
399: assertTrue(_rset.next());
400: actualSum += _rset.getInt(1);
401: assertTrue(!_rset.wasNull());
402: }
403: for (int i = 4; i < 5; i++) {
404: expectedSum += i;
405: assertTrue(_rset.next());
406: actualSum += _rset.getInt(1);
407: assertTrue(!_rset.wasNull());
408: }
409: assertTrue(!_rset.next());
410: _rset.close();
411: assertEquals(expectedSum, actualSum);
412: }
413: pstmt.close();
414: }
415:
416: public void testRemount() throws Exception {
417: // create a table and populate it
418: createTableFoo();
419: populateTableFoo();
420:
421: // select
422: {
423: _rset = _stmt.executeQuery("select NUM from FOO");
424: assertNotNull(_rset);
425: int expectedSum = 0;
426: int actualSum = 0;
427: for (int i = 0; i < 10; i++) {
428: expectedSum += i;
429: assertTrue(_rset.next());
430: actualSum += _rset.getInt(1);
431: assertTrue(!_rset.wasNull());
432: }
433: assertTrue(!_rset.next());
434: _rset.close();
435: assertEquals(expectedSum, actualSum);
436: }
437:
438: // now move those files
439: //File newdbdir = new File(".", "dupdb");
440: File newdbdir = new File(".", "testdb");
441: //copyFiles(_dbDir, newdbdir);
442:
443: // remount
444: PreparedStatement pstmt = _conn.prepareStatement("remount ?");
445: pstmt.setString(1, newdbdir.getCanonicalPath());
446: pstmt.execute();
447:
448: // select again
449: {
450: _rset = _stmt.executeQuery("select NUM from FOO");
451: assertNotNull(_rset);
452: int expectedSum = 0;
453: int actualSum = 0;
454: for (int i = 0; i < 10; i++) {
455: expectedSum += i;
456: assertTrue(_rset.next());
457: actualSum += _rset.getInt(1);
458: assertTrue(!_rset.wasNull());
459: }
460: assertTrue(!_rset.next());
461: _rset.close();
462: assertEquals(expectedSum, actualSum);
463: }
464:
465: // remount again
466: pstmt.setString(1, _dbDir.getCanonicalPath());
467: pstmt.execute();
468:
469: //deleteFile(newdbdir);
470:
471: // select again
472: {
473: _rset = _stmt.executeQuery("select NUM from FOO");
474: assertNotNull(_rset);
475: int expectedSum = 0;
476: int actualSum = 0;
477: for (int i = 0; i < 10; i++) {
478: expectedSum += i;
479: assertTrue(_rset.next());
480: actualSum += _rset.getInt(1);
481: assertTrue(!_rset.wasNull());
482: }
483: assertTrue(!_rset.next());
484: _rset.close();
485: assertEquals(expectedSum, actualSum);
486: }
487:
488: closeJDBC();
489:
490: }
491:
492: public void testCheckFileState() throws Exception {
493: // create a table and populate it
494: createTableFoo();
495: populateTableFoo();
496: _rset = _stmt.executeQuery("checkfilestate");
497: assertNotNull(_rset);
498: assertTrue(_rset.next());
499: _rset.close();
500: closeJDBC();
501: }
502:
503: public void testCheckFileState2() throws Exception {
504: // create a table and populate it
505: createTableFoo();
506: populateTableFoo();
507: assertTrue(_stmt.execute("checkfilestate"));
508: _rset = _stmt.getResultSet();
509: assertNotNull(_rset);
510: assertTrue(_rset.next());
511: _rset.close();
512: closeJDBC();
513: }
514:
515: public void testCreatePopulateSelectUpdateSelectTable()
516: throws Exception {
517: createTableFoo();
518: populateTableFoo();
519: {
520: _rset = _stmt.executeQuery("select NUM from FOO");
521: assertNotNull(_rset);
522: int expectedSum = 0;
523: int actualSum = 0;
524: for (int i = 0; i < 10; i++) {
525: expectedSum += i;
526: assertTrue(_rset.next());
527: actualSum += _rset.getInt(1);
528: assertTrue(!_rset.wasNull());
529: }
530: assertTrue(!_rset.next());
531: _rset.close();
532: assertEquals(expectedSum, actualSum);
533: }
534: assertEquals(1, _stmt
535: .executeUpdate("update FOO set NUM = 10 where NUM = 0"));
536: {
537: _rset = _stmt.executeQuery("select NUM from FOO");
538: assertNotNull(_rset);
539: int expectedSum = 0;
540: int actualSum = 0;
541: for (int i = 1; i <= 10; i++) {
542: expectedSum += i;
543: assertTrue(_rset.next());
544: actualSum += _rset.getInt(1);
545: assertTrue(!_rset.wasNull());
546: }
547: assertTrue(!_rset.next());
548: _rset.close();
549: assertEquals(expectedSum, actualSum);
550: }
551: }
552:
553: public void testCreatePopulateSelectUpdateCloseSelectTable()
554: throws Exception {
555: createTableFoo();
556: populateTableFoo();
557: {
558: _rset = _stmt.executeQuery("select NUM from FOO");
559: assertNotNull(_rset);
560: int expectedSum = 0;
561: int actualSum = 0;
562: for (int i = 0; i < 10; i++) {
563: expectedSum += i;
564: assertTrue(_rset.next());
565: actualSum += _rset.getInt(1);
566: assertTrue(!_rset.wasNull());
567: }
568: assertTrue(!_rset.next());
569: _rset.close();
570: assertEquals(expectedSum, actualSum);
571: }
572: assertEquals(1, _stmt
573: .executeUpdate("update FOO set NUM = 10 where NUM = 0"));
574: closeJDBC();
575: openJDBC();
576: {
577: _rset = _stmt.executeQuery("select NUM from FOO");
578: assertNotNull(_rset);
579: int expectedSum = 0;
580: int actualSum = 0;
581: for (int i = 1; i <= 10; i++) {
582: expectedSum += i;
583: assertTrue(_rset.next());
584: actualSum += _rset.getInt(1);
585: assertTrue(!_rset.wasNull());
586: }
587: assertTrue(!_rset.next());
588: _rset.close();
589: assertEquals(expectedSum, actualSum);
590: }
591: }
592:
593: public void testCreatePopulateSelectUpdateInsertInto()
594: throws Exception {
595: createTableFoo();
596: populateTableFoo();
597: createExceptionTableFoo();
598: {
599: _rset = _stmt.executeQuery("select NUM from FOO");
600: assertNotNull(_rset);
601: int expectedSum = 0;
602: int actualSum = 0;
603: for (int i = 0; i < 10; i++) {
604: expectedSum += i;
605: assertTrue(_rset.next());
606: actualSum += _rset.getInt(1);
607: assertTrue(!_rset.wasNull());
608: }
609: assertTrue(!_rset.next());
610: _rset.close();
611: assertEquals(expectedSum, actualSum);
612: }
613:
614: assertEquals(
615: 5,
616: _stmt
617: .executeUpdate(" UPDATE FOO SET FOO.NUM = S1.NUM+1 "
618: + " FROM FOO S1 WHERE S1.NUM = FOO.NUM EXCEPTION WHEN S1.NUM < 5 THEN "
619: + " INSERT INTO EXPFOO"));
620: ResultSet rset = _stmt
621: .executeQuery("select count(*) from expfoo");
622: rset.next();
623: assertEquals(5, rset.getInt(1));
624:
625: assertEquals(
626: 5,
627: _stmt
628: .executeUpdate(" UPDATE FOO SET FOO.NUM = S1.NUM+1 "
629: + " FROM FOO S1 WHERE S1.NUM = FOO.NUM EXCEPTION WHEN S1.NUM < 5 THEN "
630: + " INSERT INTO EXPFOO VALUES(S1.NUM, S1.STR, S1.NUMTWO)"));
631: rset = _stmt.executeQuery("select count(*) from expfoo");
632: rset.next();
633: assertEquals(10, rset.getInt(1));
634:
635: assertEquals(
636: 5,
637: _stmt
638: .executeUpdate(" UPDATE FOO SET FOO.NUM = S1.NUM+1 "
639: + " FROM FOO S1 WHERE S1.NUM = FOO.NUM EXCEPTION WHEN S1.NUM < 5 THEN "
640: + " INSERT INTO EXPFOO(NUM,STR,NUMTWO) VALUES(S1.NUM, S1.STR, S1.NUMTWO)"));
641: rset = _stmt.executeQuery("select count(*) from expfoo");
642: rset.next();
643: assertEquals(15, rset.getInt(1));
644:
645: assertEquals(
646: 5,
647: _stmt
648: .executeUpdate(" UPDATE FOO SET FOO.NUM = S1.NUM+1 "
649: + " FROM FOO S1 WHERE S1.NUM = FOO.NUM EXCEPTION WHEN S1.NUM < 5 THEN "
650: + " INSERT INTO EXPFOO T (T.NUM, T.STR, T.NUMTWO) "
651: + " VALUES(S1.NUM, S1.STR, S1.NUMTWO)"));
652: rset = _stmt.executeQuery("select count(*) from expfoo");
653: rset.next();
654: assertEquals(20, rset.getInt(1));
655:
656: }
657:
658: public void testCreatePopulateSelectUpdateInsertInto2()
659: throws Exception {
660: createTableFoo();
661: populateTableFoo();
662: createExceptionTableFoo();
663:
664: assertEquals(
665: 5,
666: _stmt
667: .executeUpdate(" UPDATE FOO SET FOO.NUM = FOO.NUM+1 "
668: + " EXCEPTION WHEN FOO.NUM < 5 THEN INSERT INTO EXPFOO"));
669: ResultSet rset = _stmt
670: .executeQuery("select count(*) from expfoo");
671: rset.next();
672: assertEquals(5, rset.getInt(1));
673:
674: assertEquals(
675: 5,
676: _stmt
677: .executeUpdate(" UPDATE FOO SET FOO.NUM = FOO.NUM+1 "
678: + " EXCEPTION WHEN FOO.NUM < 5 THEN "
679: + " INSERT INTO EXPFOO VALUES(FOO.NUM, FOO.STR, FOO.NUMTWO)"));
680: rset = _stmt.executeQuery("select count(*) from expfoo");
681: rset.next();
682: assertEquals(10, rset.getInt(1));
683:
684: assertEquals(
685: 5,
686: _stmt
687: .executeUpdate(" UPDATE FOO SET FOO.NUM = FOO.NUM+1 "
688: + " EXCEPTION WHEN FOO.NUM < 5 THEN "
689: + " INSERT INTO EXPFOO(NUM,STR,NUMTWO) VALUES(FOO.NUM, FOO.STR, FOO.NUMTWO)"));
690: rset = _stmt.executeQuery("select count(*) from expfoo");
691: rset.next();
692: assertEquals(15, rset.getInt(1));
693:
694: assertEquals(
695: 5,
696: _stmt
697: .executeUpdate(" UPDATE FOO SET FOO.NUM = FOO.NUM+1 "
698: + " EXCEPTION WHEN FOO.NUM < 5 THEN "
699: + " INSERT INTO EXPFOO T (T.NUM, T.STR, T.NUMTWO) "
700: + " VALUES(FOO.NUM, FOO.STR, FOO.NUMTWO)"));
701: rset = _stmt.executeQuery("select count(*) from expfoo");
702: rset.next();
703: assertEquals(20, rset.getInt(1));
704: }
705:
706: public void testUpdateExceptionWhenClause() throws Exception {
707: createTableFoo();
708: populateTableFoo();
709: createExceptionTableFoo();
710:
711: PreparedStatement pstmt = _conn
712: .prepareStatement(" UPDATE FOO SET FOO.NUM = FOO.NUM+1 "
713: + " EXCEPTION WHEN FOO.NUM < ? THEN "
714: + " INSERT INTO EXPFOO T (T.NUM, T.STR, T.NUMTWO) "
715: + " VALUES(FOO.NUM, FOO.STR, FOO.NUMTWO)");
716: pstmt.setInt(1, 5);
717:
718: assertEquals(5, pstmt.executeUpdate());
719: pstmt.close();
720: }
721:
722: public void testMultipleConnectionsBetweenShutdowns()
723: throws Exception {
724: closeJDBC(); // close out the instance scope connection
725:
726: // create a table
727: {
728: Connection conn = null;
729: Statement stmt = null;
730: try {
731: conn = DriverManager.getConnection(_connectString);
732: stmt = conn.createStatement();
733: stmt
734: .execute("create table BAR ( \"VALUE\" varchar(10) )");
735: } finally {
736: stmt.close();
737: conn.close();
738: }
739: }
740:
741: // insert one row at a time
742: for (int i = 0; i < 10; i++) {
743: Connection conn = null;
744: Statement stmt = null;
745: try {
746: conn = DriverManager.getConnection(_connectString);
747: stmt = conn.createStatement();
748: stmt.executeUpdate("insert into BAR values ( '" + i
749: + "')");
750: } finally {
751: stmt.close();
752: conn.close();
753: }
754: // and select it back
755: ResultSet rset = null;
756: try {
757: conn = DriverManager.getConnection(_connectString);
758: stmt = conn.createStatement();
759: rset = stmt.executeQuery("select count(*) from BAR");
760: assertTrue(rset.next());
761: assertEquals(i + 1, rset.getInt(1));
762: assertTrue(!rset.next());
763: } finally {
764: rset.close();
765: stmt.close();
766: conn.close();
767: }
768: }
769:
770: // now shutdown
771: {
772: Connection conn = null;
773: Statement stmt = null;
774: try {
775: conn = DriverManager.getConnection(_connectString);
776: stmt = conn.createStatement();
777: stmt.execute("shutdown");
778: } finally {
779: stmt.close();
780: conn.close();
781: }
782: }
783:
784: // reopen and select again
785: {
786: Connection conn = null;
787: Statement stmt = null;
788: ResultSet rset = null;
789: try {
790: conn = DriverManager.getConnection(_connectString);
791: stmt = conn.createStatement();
792: rset = stmt.executeQuery("select count(*) from BAR");
793: assertTrue(rset.next());
794: assertEquals(10, rset.getInt(1));
795: assertTrue(!rset.next());
796: } finally {
797: rset.close();
798: stmt.close();
799: conn.close();
800: }
801: }
802:
803: }
804:
805: //-------------------------------------------------------------------- Util
806:
807: private void openJDBC() throws Exception {
808: _conn = DriverManager.getConnection(_connectString);
809: _stmt = _conn.createStatement();
810: }
811:
812: private void closeJDBC() throws Exception {
813: try {
814: if (_rset != null)
815: _rset.close();
816: } catch (Exception t) {
817: }
818: try {
819: if (_stmt != null)
820: _stmt.close();
821: } catch (Exception t) {
822: }
823: try {
824: if (_conn != null)
825: _conn.close();
826: } catch (Exception t) {
827: }
828: _rset = null;
829: _stmt = null;
830: _conn = null;
831: {
832: Connection conn = DriverManager
833: .getConnection(_connectString);
834: Statement stmt = conn.createStatement();
835: stmt.execute("shutdown");
836: stmt.close();
837: conn.close();
838: }
839: }
840:
841: private void createTableFoo() throws Exception {
842: _stmt
843: .execute("create table FOO ( NUM integer, STR varchar2, NUMTWO integer )");
844: _stmt.execute("create index FOONDX on FOO ( NUM )");
845: }
846:
847: private void createExceptionTableFoo() throws Exception {
848: _stmt
849: .execute("create table EXPFOO ( NUM integer, STR varchar2, NUMTWO integer )");
850: }
851:
852: private void populateTableFoo() throws Exception {
853: for (int i = 0; i < 10; i++) {
854: _stmt
855: .execute("insert into FOO ( NUM, STR, NUMTWO ) values ( "
856: + i + ", '" + i + "', " + (i / 2) + ")");
857: }
858: }
859:
860: private void copyFiles(File from, File to) throws IOException {
861: if (from.isDirectory()) {
862: copyDirectory(from, to);
863: } else {
864: copyFile(from, to);
865: }
866: }
867:
868: private void copyFile(File from, File to) throws IOException {
869: AxionFileSystem fs = new AxionFileSystem();
870: InputStream in = fs.openDataInputSteam(from);
871: OutputStream out = fs.createDataOutputSteam(to);
872: for (int b = in.read(); b != -1; b = in.read()) {
873: out.write(b);
874: }
875: in.close();
876: out.close();
877: }
878:
879: private void copyDirectory(File from, File to) throws IOException {
880: to.mkdirs();
881: String[] files = from.list();
882: for (int i = 0; i < files.length; i++) {
883: File secondfrom = new File(from, files[i]);
884: File secondto = new File(to, files[i]);
885: copyFiles(secondfrom, secondto);
886: }
887: }
888:
889: }
|