001: /*
002: * $Id: TestTransactionalLobs.java,v 1.4 2003/11/07 08:11:12 jstrachan Exp $
003: * =======================================================================
004: * Copyright (c) 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.functional;
042:
043: import java.io.ByteArrayOutputStream;
044: import java.io.InputStream;
045: import java.sql.Blob;
046: import java.sql.Connection;
047: import java.sql.DriverManager;
048: import java.sql.PreparedStatement;
049: import java.sql.ResultSet;
050: import java.sql.Statement;
051:
052: import junit.framework.Test;
053: import junit.framework.TestSuite;
054:
055: import org.axiondb.types.ByteArrayBlob;
056:
057: /**
058: * @version $Revision: 1.4 $ $Date: 2003/11/07 08:11:12 $
059: * @author Rodney Waldhoff
060: */
061: public class TestTransactionalLobs extends AbstractFunctionalTest {
062:
063: //------------------------------------------------------------ Conventional
064:
065: public TestTransactionalLobs(String testName) {
066: super (testName);
067: }
068:
069: public static Test suite() {
070: return new TestSuite(TestTransactionalLobs.class);
071: }
072:
073: //--------------------------------------------------------------- Lifecycle
074:
075: public void setUp() throws Exception {
076: super .setUp();
077: }
078:
079: public void tearDown() throws Exception {
080: super .tearDown();
081: }
082:
083: //------------------------------------------------------------------- Tests
084:
085: public void testSimpleClobInsert() throws Exception {
086: String body = "This is a simple test.";
087: createClobTable(_conn);
088:
089: Connection conn = DriverManager
090: .getConnection(getConnectString());
091: conn.setAutoCommit(false);
092:
093: assertEquals(0, selectClobCount(conn));
094: insertClob(conn, 0, body);
095: assertEquals(1, selectClobCount(conn));
096: assertEquals(body, selectClob(conn, 0));
097: conn.commit();
098: assertEquals(1, selectClobCount(conn));
099: assertEquals(body, selectClob(conn, 0));
100:
101: conn.close();
102: }
103:
104: public void testSimpleBlobInsert() throws Exception {
105: String body = "This is a simple test.";
106: createBlobTable(_conn);
107:
108: Connection conn = DriverManager
109: .getConnection(getConnectString());
110: conn.setAutoCommit(false);
111:
112: assertEquals(0, selectBlobCount(conn));
113: insertBlob(conn, 0, body.getBytes());
114: assertEquals(1, selectBlobCount(conn));
115: assertEquals(body, new String(selectBlob(conn, 0)));
116: conn.commit();
117: assertEquals(1, selectBlobCount(conn));
118: assertEquals(body, new String(selectBlob(conn, 0)));
119:
120: conn.close();
121: }
122:
123: public void testClobInsertRollback() throws Exception {
124: String body = "This is a simple test.";
125: createClobTable(_conn);
126:
127: Connection conn = DriverManager
128: .getConnection(getConnectString());
129: conn.setAutoCommit(false);
130:
131: assertEquals(0, selectClobCount(conn));
132: assertNull(selectClob(conn, 0));
133:
134: insertClob(conn, 0, body);
135:
136: assertEquals(1, selectClobCount(conn));
137: assertEquals(body, selectClob(conn, 0));
138:
139: conn.rollback();
140:
141: assertEquals(0, selectClobCount(conn));
142: assertNull(selectClob(conn, 0));
143:
144: conn.close();
145: }
146:
147: public void testBlobInsertRollback() throws Exception {
148: String body = "This is a simple test.";
149: createBlobTable(_conn);
150:
151: Connection conn = DriverManager
152: .getConnection(getConnectString());
153: conn.setAutoCommit(false);
154:
155: assertEquals(0, selectBlobCount(conn));
156: assertNull(selectBlob(conn, 0));
157:
158: insertBlob(conn, 0, body.getBytes());
159:
160: assertEquals(1, selectBlobCount(conn));
161: assertEquals(body, new String(selectBlob(conn, 0)));
162:
163: conn.rollback();
164:
165: assertEquals(0, selectBlobCount(conn));
166: assertNull(selectBlob(conn, 0));
167:
168: conn.close();
169: }
170:
171: public void testClobInsertIsolation() throws Exception {
172: String body = "This is a simple test.";
173: createClobTable(_conn);
174:
175: Connection conn = DriverManager
176: .getConnection(getConnectString());
177: conn.setAutoCommit(false);
178:
179: Connection conn2 = DriverManager
180: .getConnection(getConnectString());
181: conn2.setAutoCommit(false);
182:
183: assertEquals(0, selectClobCount(conn));
184: assertNull(selectClob(conn, 0));
185:
186: assertEquals(0, selectClobCount(conn2));
187: assertNull(selectClob(conn2, 0));
188:
189: insertClob(conn, 0, body);
190:
191: assertEquals(1, selectClobCount(conn));
192: assertEquals(body, selectClob(conn, 0));
193:
194: assertEquals(0, selectClobCount(conn2));
195: assertNull(selectClob(conn2, 0));
196:
197: conn2.commit();
198:
199: assertEquals(1, selectClobCount(conn));
200: assertEquals(body, selectClob(conn, 0));
201:
202: assertEquals(0, selectClobCount(conn2));
203: assertNull(selectClob(conn2, 0));
204:
205: conn.commit();
206:
207: assertEquals(1, selectClobCount(conn));
208: assertEquals(body, selectClob(conn, 0));
209:
210: assertEquals(0, selectClobCount(conn2));
211: assertNull(selectClob(conn2, 0));
212:
213: conn.close();
214:
215: assertEquals(0, selectClobCount(conn2));
216: assertNull(selectClob(conn2, 0));
217:
218: conn2.commit();
219:
220: assertEquals(1, selectClobCount(conn2));
221: assertEquals(body, selectClob(conn2, 0));
222:
223: conn2.close();
224: }
225:
226: public void testClobUpdateIsolation() throws Exception {
227: String inserted = "This is a simple test.";
228: createClobTable(_conn);
229: insertClob(_conn, 0, inserted);
230: assertEquals(1, selectClobCount(_conn));
231: assertEquals(inserted, selectClob(_conn, 0));
232:
233: assertEquals(1, selectClobCount(_conn));
234: assertEquals(inserted, selectClob(_conn, 0));
235:
236: Connection conn = DriverManager
237: .getConnection(getConnectString());
238: conn.setAutoCommit(false);
239:
240: Connection conn2 = DriverManager
241: .getConnection(getConnectString());
242: conn2.setAutoCommit(false);
243:
244: assertEquals(1, selectClobCount(conn));
245: assertEquals(inserted, selectClob(conn, 0));
246:
247: assertEquals(1, selectClobCount(conn2));
248: assertEquals(inserted, selectClob(conn2, 0));
249:
250: String updated = "This is the updated contents.";
251:
252: updateClob(conn, 0, updated);
253:
254: assertEquals(1, selectClobCount(conn));
255: assertEquals(updated, selectClob(conn, 0));
256:
257: assertEquals(1, selectClobCount(conn2));
258: assertEquals(inserted, selectClob(conn2, 0));
259:
260: conn2.commit();
261:
262: assertEquals(1, selectClobCount(conn));
263: assertEquals(updated, selectClob(conn, 0));
264:
265: assertEquals(1, selectClobCount(conn2));
266: assertEquals(inserted, selectClob(conn2, 0));
267:
268: conn.commit();
269:
270: assertEquals(1, selectClobCount(conn));
271: assertEquals(updated, selectClob(conn, 0));
272:
273: assertEquals(1, selectClobCount(conn2));
274: assertEquals(inserted, selectClob(conn2, 0));
275:
276: conn.close();
277:
278: assertEquals(1, selectClobCount(conn2));
279: assertEquals(inserted, selectClob(conn2, 0));
280:
281: conn2.commit();
282:
283: assertEquals(1, selectClobCount(conn2));
284: assertEquals(updated, selectClob(conn2, 0));
285:
286: conn2.close();
287: }
288:
289: public void testClobDeleteIsolation() throws Exception {
290: String inserted = "This is a simple test.";
291: createClobTable(_conn);
292: insertClob(_conn, 0, inserted);
293: assertEquals(1, selectClobCount(_conn));
294: assertEquals(inserted, selectClob(_conn, 0));
295:
296: assertEquals(1, selectClobCount(_conn));
297: assertEquals(inserted, selectClob(_conn, 0));
298:
299: Connection conn = DriverManager
300: .getConnection(getConnectString());
301: conn.setAutoCommit(false);
302:
303: Connection conn2 = DriverManager
304: .getConnection(getConnectString());
305: conn2.setAutoCommit(false);
306:
307: assertEquals(1, selectClobCount(conn));
308: assertEquals(inserted, selectClob(conn, 0));
309:
310: assertEquals(1, selectClobCount(conn2));
311: assertEquals(inserted, selectClob(conn2, 0));
312:
313: deleteClob(conn, 0);
314:
315: assertEquals(0, selectClobCount(conn));
316: assertNull(selectClob(conn, 0));
317:
318: assertEquals(1, selectClobCount(conn2));
319: assertEquals(inserted, selectClob(conn2, 0));
320:
321: conn2.commit();
322:
323: assertEquals(0, selectClobCount(conn));
324: assertNull(selectClob(conn, 0));
325:
326: assertEquals(1, selectClobCount(conn2));
327: assertEquals(inserted, selectClob(conn2, 0));
328:
329: conn.commit();
330:
331: assertEquals(0, selectClobCount(conn));
332: assertNull(selectClob(conn, 0));
333:
334: assertEquals(1, selectClobCount(conn2));
335: assertEquals(inserted, selectClob(conn2, 0));
336:
337: conn.close();
338:
339: assertEquals(1, selectClobCount(conn2));
340: assertEquals(inserted, selectClob(conn2, 0));
341:
342: conn2.commit();
343:
344: assertEquals(0, selectClobCount(conn2));
345: assertNull(selectClob(conn2, 0));
346:
347: conn2.close();
348: }
349:
350: public void testBlobInsertIsolation() throws Exception {
351: String body = "This is a simple test.";
352: createBlobTable(_conn);
353:
354: Connection conn = DriverManager
355: .getConnection(getConnectString());
356: conn.setAutoCommit(false);
357:
358: Connection conn2 = DriverManager
359: .getConnection(getConnectString());
360: conn2.setAutoCommit(false);
361:
362: assertEquals(0, selectBlobCount(conn));
363: assertNull(selectBlob(conn, 0));
364:
365: assertEquals(0, selectBlobCount(conn2));
366: assertNull(selectBlob(conn2, 0));
367:
368: insertBlob(conn, 0, body.getBytes());
369:
370: assertEquals(1, selectBlobCount(conn));
371: assertEquals(body, new String(selectBlob(conn, 0)));
372:
373: assertEquals(0, selectBlobCount(conn2));
374: assertNull(selectBlob(conn2, 0));
375:
376: conn2.commit();
377:
378: assertEquals(1, selectBlobCount(conn));
379: assertEquals(body, new String(selectBlob(conn, 0)));
380:
381: assertEquals(0, selectBlobCount(conn2));
382: assertNull(selectBlob(conn2, 0));
383:
384: conn.commit();
385:
386: assertEquals(1, selectBlobCount(conn));
387: assertEquals(body, new String(selectBlob(conn, 0)));
388:
389: assertEquals(0, selectBlobCount(conn2));
390: assertNull(selectBlob(conn2, 0));
391:
392: conn.close();
393:
394: assertEquals(0, selectBlobCount(conn2));
395: assertNull(selectBlob(conn2, 0));
396:
397: conn2.commit();
398:
399: assertEquals(1, selectBlobCount(conn2));
400: assertEquals(body, new String(selectBlob(conn2, 0)));
401:
402: conn2.close();
403: }
404:
405: public void testBlobUpdateIsolation() throws Exception {
406: String inserted = "This is a simple test.";
407: createBlobTable(_conn);
408: insertBlob(_conn, 0, inserted.getBytes());
409: assertEquals(1, selectBlobCount(_conn));
410: assertEquals(inserted, new String(selectBlob(_conn, 0)));
411:
412: assertEquals(1, selectBlobCount(_conn));
413: assertEquals(inserted, new String(selectBlob(_conn, 0)));
414:
415: Connection conn = DriverManager
416: .getConnection(getConnectString());
417: conn.setAutoCommit(false);
418:
419: Connection conn2 = DriverManager
420: .getConnection(getConnectString());
421: conn2.setAutoCommit(false);
422:
423: assertEquals(1, selectBlobCount(conn));
424: assertEquals(inserted, new String(selectBlob(conn, 0)));
425:
426: assertEquals(1, selectBlobCount(conn2));
427: assertEquals(inserted, new String(selectBlob(conn2, 0)));
428:
429: String updated = "This is the updated contents.";
430:
431: updateBlob(conn, 0, updated.getBytes());
432:
433: assertEquals(1, selectBlobCount(conn));
434: assertEquals(updated, new String(selectBlob(conn, 0)));
435:
436: assertEquals(1, selectBlobCount(conn2));
437: assertEquals(inserted, new String(selectBlob(conn2, 0)));
438:
439: conn2.commit();
440:
441: assertEquals(1, selectBlobCount(conn));
442: assertEquals(updated, new String(selectBlob(conn, 0)));
443:
444: assertEquals(1, selectBlobCount(conn2));
445: assertEquals(inserted, new String(selectBlob(conn2, 0)));
446:
447: conn.commit();
448:
449: assertEquals(1, selectBlobCount(conn));
450: assertEquals(updated, new String(selectBlob(conn, 0)));
451:
452: assertEquals(1, selectBlobCount(conn2));
453: assertEquals(inserted, new String(selectBlob(conn2, 0)));
454:
455: conn.close();
456:
457: assertEquals(1, selectBlobCount(conn2));
458: assertEquals(inserted, new String(selectBlob(conn2, 0)));
459:
460: conn2.commit();
461:
462: assertEquals(1, selectBlobCount(conn2));
463: assertEquals(updated, new String(selectBlob(conn2, 0)));
464:
465: conn2.close();
466: }
467:
468: public void testBlobDeleteIsolation() throws Exception {
469: String inserted = "This is a simple test.";
470: createBlobTable(_conn);
471: insertBlob(_conn, 0, inserted.getBytes());
472: assertEquals(1, selectBlobCount(_conn));
473: assertEquals(inserted, new String(selectBlob(_conn, 0)));
474:
475: assertEquals(1, selectBlobCount(_conn));
476: assertEquals(inserted, new String(selectBlob(_conn, 0)));
477:
478: Connection conn = DriverManager
479: .getConnection(getConnectString());
480: conn.setAutoCommit(false);
481:
482: Connection conn2 = DriverManager
483: .getConnection(getConnectString());
484: conn2.setAutoCommit(false);
485:
486: assertEquals(1, selectBlobCount(conn));
487: assertEquals(inserted, new String(selectBlob(conn, 0)));
488:
489: assertEquals(1, selectBlobCount(conn2));
490: assertEquals(inserted, new String(selectBlob(conn2, 0)));
491:
492: deleteBlob(conn, 0);
493:
494: assertEquals(0, selectBlobCount(conn));
495: assertNull(selectBlob(conn, 0));
496:
497: assertEquals(1, selectBlobCount(conn2));
498: assertEquals(inserted, new String(selectBlob(conn2, 0)));
499:
500: conn2.commit();
501:
502: assertEquals(0, selectBlobCount(conn));
503: assertNull(selectBlob(conn, 0));
504:
505: assertEquals(1, selectBlobCount(conn2));
506: assertEquals(inserted, new String(selectBlob(conn2, 0)));
507:
508: conn.commit();
509:
510: assertEquals(0, selectBlobCount(conn));
511: assertNull(selectBlob(conn, 0));
512:
513: assertEquals(1, selectBlobCount(conn2));
514: assertEquals(inserted, new String(selectBlob(conn2, 0)));
515:
516: conn.close();
517:
518: assertEquals(1, selectBlobCount(conn2));
519: assertEquals(inserted, new String(selectBlob(conn2, 0)));
520:
521: conn2.commit();
522:
523: assertEquals(0, selectBlobCount(conn2));
524: assertNull(selectBlob(conn2, 0));
525:
526: conn2.close();
527: }
528:
529: //------------------------------------------------------------------- Utils
530:
531: private void createClobTable(Connection conn) throws Exception {
532: Statement stmt = conn.createStatement();
533: stmt
534: .execute("create table CLOB_TBL ( ID integer, BODY compressedclob )");
535: stmt.close();
536: }
537:
538: private void createBlobTable(Connection conn) throws Exception {
539: Statement stmt = conn.createStatement();
540: stmt
541: .execute("create table BLOB_TBL ( ID integer, BODY compressedblob )");
542: stmt.close();
543: }
544:
545: private void insertClob(Connection conn, int id, String body)
546: throws Exception {
547: Statement stmt = conn.createStatement();
548: assertEquals(1, stmt
549: .executeUpdate("insert into CLOB_TBL values ( " + id
550: + ", '" + body + "' )"));
551: /*
552: // TODO: this style of clob writing doesn't work transactionally right now, but the inline style does
553: assertEquals(1,stmt.executeUpdate("insert into CLOB_TBL values ( " + id + ", 'newlob()' )"));
554: ResultSet rset = stmt.executeQuery("select BODY from CLOB_TBL where ID = " + id);
555: assertTrue(rset.next());
556: Clob clob = rset.getClob(1);
557: Writer out = clob.setCharacterStream(0);
558: out.write(body);
559: out.close();
560: rset.close();
561: */
562: stmt.close();
563: }
564:
565: private void insertBlob(Connection conn, int id, byte[] body)
566: throws Exception {
567: PreparedStatement stmt = conn
568: .prepareStatement("insert into BLOB_TBL values ( ?, ? )");
569: stmt.setInt(1, id);
570: stmt.setBlob(2, new ByteArrayBlob(body));
571: assertEquals(1, stmt.executeUpdate());
572: stmt.close();
573: }
574:
575: private void updateClob(Connection conn, int id, String body)
576: throws Exception {
577: Statement stmt = conn.createStatement();
578: assertEquals(1, stmt
579: .executeUpdate("update CLOB_TBL set BODY = '" + body
580: + "' where ID = " + id));
581: /*
582: // TODO: this style of clob writing doesn't work transactionally right now, but the inline style does
583: ResultSet rset = stmt.executeQuery("select BODY from CLOB_TBL where ID = " + id);
584: assertTrue(rset.next());
585: Clob clob = rset.getClob(1);
586: Writer out = clob.setCharacterStream(0);
587: out.write(body);
588: out.close();
589: rset.close();
590: */
591: stmt.close();
592: }
593:
594: private void updateBlob(Connection conn, int id, byte[] body)
595: throws Exception {
596: PreparedStatement stmt = conn
597: .prepareStatement("update BLOB_TBL set BODY = ? where ID = ?");
598: stmt.setBlob(1, new ByteArrayBlob(body));
599: stmt.setInt(2, id);
600: assertEquals(1, stmt.executeUpdate());
601: stmt.close();
602: }
603:
604: private void deleteClob(Connection conn, int id) throws Exception {
605: Statement stmt = conn.createStatement();
606: assertEquals(1, stmt
607: .executeUpdate("delete from CLOB_TBL where ID = " + id));
608: stmt.close();
609: }
610:
611: private void deleteBlob(Connection conn, int id) throws Exception {
612: Statement stmt = conn.createStatement();
613: assertEquals(1, stmt
614: .executeUpdate("delete from BLOB_TBL where ID = " + id));
615: stmt.close();
616: }
617:
618: private String selectClob(Connection conn, int id) throws Exception {
619: Statement stmt = conn.createStatement();
620: String result = null;
621: ResultSet rset = stmt
622: .executeQuery("select BODY from CLOB_TBL where ID = "
623: + id);
624: if (rset.next()) {
625: result = rset.getString(1);
626: }
627: rset.close();
628: stmt.close();
629: return result;
630: }
631:
632: private byte[] selectBlob(Connection conn, int id) throws Exception {
633: Statement stmt = conn.createStatement();
634: byte[] result = null;
635: ResultSet rset = stmt
636: .executeQuery("select BODY from BLOB_TBL where ID = "
637: + id);
638: if (rset.next()) {
639: ByteArrayOutputStream read = new ByteArrayOutputStream();
640: Blob blob = rset.getBlob(1);
641: if (null != blob) {
642: InputStream in = blob.getBinaryStream();
643: for (int b = in.read(); b != -1; b = in.read()) {
644: read.write((byte) b);
645: }
646: in.close();
647: result = read.toByteArray();
648: }
649: }
650: rset.close();
651: stmt.close();
652: return result;
653: }
654:
655: private int selectClobCount(Connection conn) throws Exception {
656: Statement stmt = conn.createStatement();
657: int result = -1;
658: ResultSet rset = stmt
659: .executeQuery("select count(*) from CLOB_TBL");
660: assertTrue(rset.next());
661: result = rset.getInt(1);
662: rset.close();
663: stmt.close();
664: return result;
665: }
666:
667: private int selectBlobCount(Connection conn) throws Exception {
668: Statement stmt = conn.createStatement();
669: int result = -1;
670: ResultSet rset = stmt
671: .executeQuery("select count(*) from BLOB_TBL");
672: assertTrue(rset.next());
673: result = rset.getInt(1);
674: rset.close();
675: stmt.close();
676: return result;
677: }
678:
679: }
|