001: /*
002: * $Id: AbstractClobTest.java,v 1.3 2004/02/27 22:23:01 rwald Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2004 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.ByteArrayInputStream;
044: import java.io.File;
045: import java.io.Reader;
046: import java.io.StringReader;
047: import java.io.Writer;
048: import java.sql.Connection;
049: import java.sql.DriverManager;
050: import java.sql.PreparedStatement;
051: import java.sql.ResultSet;
052: import java.sql.Statement;
053:
054: import junit.framework.TestCase;
055:
056: import org.axiondb.jdbc.AxionClob;
057:
058: /**
059: * @version $Revision: 1.3 $ $Date: 2004/02/27 22:23:01 $
060: * @author Rodney Waldhoff
061: */
062: public abstract class AbstractClobTest extends TestCase {
063: private Connection _conn = null;
064: private ResultSet _rset = null;
065: private Statement _stmt = null;
066: private String _axiondbLobdir = null;
067:
068: public AbstractClobTest(String s) {
069: super (s);
070: }
071:
072: public abstract String getConnectString();
073:
074: public void setUp() throws Exception {
075: _axiondbLobdir = System.getProperty("axiondb.lobdir");
076: System.setProperty("axiondb.lobdir", "testdb");
077:
078: Class.forName("org.axiondb.jdbc.AxionDriver");
079:
080: _conn = DriverManager.getConnection(getConnectString());
081: _stmt = _conn.createStatement();
082: }
083:
084: public void tearDown() throws Exception {
085: try {
086: _rset.close();
087: } catch (Exception t) {
088: }
089: try {
090: _stmt.close();
091: } catch (Exception t) {
092: }
093: try {
094: _conn.close();
095: } catch (Exception t) {
096: }
097: _rset = null;
098: _stmt = null;
099: _conn = null;
100: {
101: Connection conn = DriverManager
102: .getConnection(getConnectString());
103: Statement stmt = conn.createStatement();
104: stmt.execute("shutdown");
105: stmt.close();
106: conn.close();
107: }
108: deleteFile(new File("testdb"));
109: if (null == _axiondbLobdir) {
110: System.getProperties().remove("axiondb.lobdir");
111: } else {
112: System.setProperty("axiondb.lobdir", _axiondbLobdir);
113: }
114: }
115:
116: protected boolean deleteFile(File file) throws Exception {
117: if (file.exists()) {
118: if (file.isDirectory()) {
119: File[] files = file.listFiles();
120: for (int i = 0; i < files.length; i++) {
121: deleteFile(files[i]);
122: }
123: }
124: return file.delete();
125: }
126: return true;
127: }
128:
129: private void createClobTable(boolean compressed) throws Exception {
130: if (compressed) {
131: _stmt
132: .execute("create table FOO ( ID integer, BODY compressedclob, BODY2 compressedclob )");
133: } else {
134: _stmt
135: .execute("create table FOO ( ID integer, BODY clob, BODY2 clob )");
136: }
137: }
138:
139: public void testInsertEmptyClob() throws Exception {
140: createClobTable(false);
141: insertEmptyClob();
142: }
143:
144: public void testInsertEmptyCompressedClob() throws Exception {
145: createClobTable(true);
146: insertEmptyClob();
147: }
148:
149: private void insertEmptyClob() throws Exception {
150: _stmt
151: .execute("insert into FOO ( ID, BODY, BODY2 ) values ( 0, 'newlob()', 'newlob()' )");
152: _rset = _stmt.executeQuery("select ID, BODY, BODY2 from FOO");
153: assertNotNull(_rset);
154: assertTrue(_rset.next());
155: assertEquals(0, _rset.getInt(1));
156: assertNotNull(_rset.getClob(2));
157: assertNotNull(_rset.getClob("body"));
158: assertNotNull(_rset.getClob(3));
159: assertTrue(_rset.getClob(2) instanceof AxionClob);
160: assertTrue(_rset.getClob(3) instanceof AxionClob);
161: }
162:
163: public void testWriteToEmptyClob() throws Exception {
164: createClobTable(false);
165: writeToEmptyClob();
166: }
167:
168: public void testInsertAndSelectClobAsString() throws Exception {
169: insertAndSelectClobAsString(false);
170: }
171:
172: public void testUpdateAndSelectClobAsString() throws Exception {
173: updateAndSelectClobAsString(false);
174: }
175:
176: public void testInsertAndSelectCompressedClobAsString()
177: throws Exception {
178: insertAndSelectClobAsString(true);
179: }
180:
181: public void testUpdateAndSelectCompressedClobAsString()
182: throws Exception {
183: updateAndSelectClobAsString(true);
184: }
185:
186: public void testInsertAndSelectClobAsStringViaPstmt()
187: throws Exception {
188: insertAndSelectClobAsStringViaPstmt(false);
189: }
190:
191: public void testUpdateAndSelectClobAsStringViaPstmt()
192: throws Exception {
193: updateAndSelectClobAsStringViaPstmt(false);
194: }
195:
196: public void testInsertAndSelectCompressedClobAsStringViaPstmt()
197: throws Exception {
198: insertAndSelectClobAsStringViaPstmt(true);
199: }
200:
201: public void testUpdateAndSelectCompressedClobAsStringViaPstmt()
202: throws Exception {
203: updateAndSelectClobAsStringViaPstmt(true);
204: }
205:
206: public void testSetClobAsAsciiStream() throws Exception {
207: updateAndSelectClobAsAsciiStream(false);
208: }
209:
210: public void testSetCompressedClobAsAsciiStream() throws Exception {
211: updateAndSelectClobAsAsciiStream(true);
212: }
213:
214: public void testSetClobAsCharacterStream() throws Exception {
215: updateAndSelectClobAsCharacterStream(false);
216: }
217:
218: public void testSetCompressedClobAsCharacterStream()
219: throws Exception {
220: updateAndSelectClobAsCharacterStream(true);
221: }
222:
223: public void testSetClobAsUnicodeStream() throws Exception {
224: updateAndSelectClobAsUnicodeStream(false);
225: }
226:
227: public void testSetCompressedClobAsUnicodeStream() throws Exception {
228: updateAndSelectClobAsUnicodeStream(true);
229: }
230:
231: public void testWriteToEmptyCompressedClob() throws Exception {
232: createClobTable(true);
233: writeToEmptyClob();
234: }
235:
236: public void testGetAsciiStream() throws Exception {
237: createClobTable(false);
238: _stmt
239: .execute("insert into FOO ( ID, BODY, BODY2 ) values ( 0, 'The quick brown fox jumped over the lazy dogs.', NULL )");
240: _rset = _stmt.executeQuery("select ID, BODY, BODY2 from FOO");
241: assertNotNull(_rset);
242: assertTrue(_rset.next());
243: assertEquals(0, _rset.getInt(1));
244: assertNotNull(_rset.getAsciiStream(2));
245: assertNotNull(_rset.getCharacterStream(2));
246: assertNotNull(_rset.getAsciiStream("body"));
247: assertNotNull(_rset.getCharacterStream("body"));
248:
249: assertNull(_rset.getAsciiStream(3));
250: assertNull(_rset.getAsciiStream("body2"));
251: assertNull(_rset.getCharacterStream(3));
252: assertNull(_rset.getCharacterStream("body2"));
253: }
254:
255: private void writeToEmptyClob() throws Exception {
256: String bodyline = "The quick brown fox jumped over the lazy dogs. ";
257: String bodyline2 = "And then the dogs ran away. ";
258:
259: _stmt
260: .execute("insert into FOO ( ID, BODY, BODY2 ) values ( 0, 'newlob()', 'newlob()' )");
261:
262: _rset = _stmt.executeQuery("select ID, BODY, BODY2 from FOO");
263: assertNotNull(_rset);
264: assertTrue(_rset.next());
265: assertEquals(0, _rset.getInt(1));
266: assertNotNull(_rset.getClob(2));
267: assertNotNull(_rset.getClob(3));
268: assertTrue(_rset.getClob(2) instanceof AxionClob);
269: assertTrue(_rset.getClob(3) instanceof AxionClob);
270: AxionClob clob = (AxionClob) (_rset.getClob(2));
271: Writer out = clob.setCharacterStream(0L);
272: for (int i = 0; i < 10; i++) {
273: out.write(bodyline);
274: }
275: out.close();
276:
277: clob = (AxionClob) (_rset.getClob(3));
278: out = clob.setCharacterStream(0L);
279: for (int i = 0; i < 10; i++) {
280: out.write(bodyline2);
281: }
282: out.close();
283: _rset.close();
284:
285: StringBuffer expected = new StringBuffer();
286: for (int i = 0; i < 10; i++) {
287: expected.append(bodyline);
288: }
289: assertEquals(expected.toString(), readClobAsStream(0, 1));
290: assertEquals(expected.toString(), readClobAsVarchar(0, 1));
291:
292: expected = new StringBuffer();
293: for (int i = 0; i < 10; i++) {
294: expected.append(bodyline2);
295: }
296: assertEquals(expected.toString(), readClobAsStream(0, 2));
297: assertEquals(expected.toString(), readClobAsVarchar(0, 2));
298: }
299:
300: public void testWriteToNonEmptyClob() throws Exception {
301: createClobTable(false);
302: writeToNonEmptyClob();
303: }
304:
305: public void testWriteToNonEmptyCompressedClob() throws Exception {
306: createClobTable(true);
307: writeToNonEmptyClob();
308: }
309:
310: private void writeToNonEmptyClob() throws Exception {
311: String body = "It was the best of times, it was the worst of times.";
312: String body2 = "Or was it?";
313: writeToEmptyClob();
314: {
315: _rset = _stmt
316: .executeQuery("select ID, BODY, BODY2 from FOO");
317: assertNotNull(_rset);
318: assertTrue(_rset.next());
319: assertEquals(0, _rset.getInt(1));
320: assertNotNull(_rset.getClob(2));
321: assertTrue(_rset.getClob(2) instanceof AxionClob);
322: AxionClob clob = (AxionClob) (_rset.getClob(2));
323: clob.truncate(0);
324: Writer out = clob.setCharacterStream(0L);
325: out.write(body);
326: out.close();
327:
328: assertNotNull(_rset.getClob(3));
329: assertTrue(_rset.getClob(3) instanceof AxionClob);
330: clob = (AxionClob) (_rset.getClob(3));
331: clob.truncate(0);
332: out = clob.setCharacterStream(0L);
333: out.write(body2);
334: out.close();
335:
336: _rset.close();
337: }
338: assertEquals(body, readClobAsStream(0, 1));
339: assertEquals(body, readClobAsVarchar(0, 1));
340:
341: assertEquals(body2, readClobAsStream(0, 2));
342: assertEquals(body2, readClobAsVarchar(0, 2));
343: }
344:
345: private void insertAndSelectClobAsString(boolean compressed)
346: throws Exception {
347: String body = "This is a clob, inserted as if it were a VARCHAR field";
348: String body2 = "And this is as well";
349: createClobTable(compressed);
350: assertEquals(1, _stmt
351: .executeUpdate("insert into FOO values ( 0, '" + body
352: + "', '" + body2 + "')"));
353:
354: assertEquals(body, readClobAsVarchar(0, 1));
355: assertEquals(body, readClobAsStream(0, 1));
356:
357: assertEquals(body2, readClobAsVarchar(0, 2));
358: assertEquals(body2, readClobAsStream(0, 2));
359: }
360:
361: private void updateAndSelectClobAsString(boolean compressed)
362: throws Exception {
363: insertAndSelectClobAsString(compressed);
364: String body = "This is the updated clob.";
365: String body2 = "Me too.";
366: assertEquals(1, _stmt.executeUpdate("update FOO set BODY = '"
367: + body + "', BODY2 = '" + body2 + "' where ID = 0"));
368:
369: assertEquals(body, readClobAsVarchar(0, 1));
370: assertEquals(body, readClobAsStream(0, 1));
371:
372: assertEquals(body2, readClobAsVarchar(0, 2));
373: assertEquals(body2, readClobAsStream(0, 2));
374: }
375:
376: private void insertAndSelectClobAsStringViaPstmt(boolean compressed)
377: throws Exception {
378: String body = "This is a clob, inserted as if it were a VARCHAR field";
379: String body2 = "Yadda, yadda, yadda";
380: createClobTable(compressed);
381: PreparedStatement pstmt = _conn
382: .prepareStatement("insert into FOO values ( 0, ?, ? )");
383: pstmt.setString(1, body);
384: pstmt.setString(2, body2);
385: assertEquals(1, pstmt.executeUpdate());
386: pstmt.close();
387:
388: assertEquals(body, readClobAsVarchar(0, 1));
389: assertEquals(body, readClobAsStream(0, 1));
390:
391: assertEquals(body2, readClobAsVarchar(0, 2));
392: assertEquals(body2, readClobAsStream(0, 2));
393: }
394:
395: private void updateAndSelectClobAsStringViaPstmt(boolean compressed)
396: throws Exception {
397: insertAndSelectClobAsStringViaPstmt(compressed);
398: String body = "This is the updated clob.";
399: String body2 = "yup, updated clob here too.";
400: PreparedStatement pstmt = _conn
401: .prepareStatement("update FOO set BODY = ?, BODY2 = ? where ID = 0");
402: pstmt.setString(1, body);
403: pstmt.setString(2, body2);
404: assertEquals(1, pstmt.executeUpdate());
405: pstmt.close();
406:
407: assertEquals(body, readClobAsVarchar(0, 1));
408: assertEquals(body, readClobAsStream(0, 1));
409:
410: assertEquals(body2, readClobAsVarchar(0, 2));
411: assertEquals(body2, readClobAsStream(0, 2));
412: }
413:
414: private void insertAndSelectClobAsAsciiStream(boolean compressed)
415: throws Exception {
416: String body = "This is a clob, inserted as if it were a VARCHAR field";
417: String body2 = "clob, clob, clob";
418: createClobTable(compressed);
419: PreparedStatement pstmt = _conn
420: .prepareStatement("insert into FOO values ( 0, ?, ? )");
421: pstmt.setAsciiStream(1, new ByteArrayInputStream(body
422: .getBytes("ASCII")), body.length());
423: pstmt.setAsciiStream(2, new ByteArrayInputStream(body2
424: .getBytes("ASCII")), body2.length());
425: assertEquals(1, pstmt.executeUpdate());
426: pstmt.close();
427:
428: assertEquals(body, readClobAsVarchar(0, 1));
429: assertEquals(body, readClobAsStream(0, 1));
430:
431: assertEquals(body2, readClobAsVarchar(0, 2));
432: assertEquals(body2, readClobAsStream(0, 2));
433: }
434:
435: private void updateAndSelectClobAsAsciiStream(boolean compressed)
436: throws Exception {
437: insertAndSelectClobAsAsciiStream(compressed);
438: String body = "This is the updated clob.";
439: String body2 = "Ditto";
440: PreparedStatement pstmt = _conn
441: .prepareStatement("update FOO set BODY = ?, BODY2 = ? where ID = 0");
442: pstmt.setAsciiStream(1, new ByteArrayInputStream(body
443: .getBytes("ASCII")), body.length());
444: pstmt.setAsciiStream(2, new ByteArrayInputStream(body2
445: .getBytes("ASCII")), body2.length());
446: assertEquals(1, pstmt.executeUpdate());
447: pstmt.close();
448:
449: assertEquals(body, readClobAsVarchar(0, 1));
450: assertEquals(body, readClobAsStream(0, 1));
451:
452: assertEquals(body2, readClobAsVarchar(0, 2));
453: assertEquals(body2, readClobAsStream(0, 2));
454: }
455:
456: private void insertAndSelectClobAsUnicodeStream(boolean compressed)
457: throws Exception {
458: String body = "This is a clob, inserted as if it were a VARCHAR field";
459: String body2 = "blah, blah";
460: createClobTable(compressed);
461: PreparedStatement pstmt = _conn
462: .prepareStatement("insert into FOO values ( 0, ?, ? )");
463: pstmt.setUnicodeStream(1, new ByteArrayInputStream(body
464: .getBytes("UnicodeBig")), body.length() * 2);
465: pstmt.setUnicodeStream(2, new ByteArrayInputStream(body2
466: .getBytes("UnicodeBig")), body2.length() * 2);
467: assertEquals(1, pstmt.executeUpdate());
468: pstmt.close();
469:
470: assertEquals(body, readClobAsVarchar(0, 1));
471: assertEquals(body, readClobAsStream(0, 1));
472:
473: assertEquals(body2, readClobAsVarchar(0, 2));
474: assertEquals(body2, readClobAsStream(0, 2));
475: }
476:
477: private void updateAndSelectClobAsUnicodeStream(boolean compressed)
478: throws Exception {
479: insertAndSelectClobAsUnicodeStream(compressed);
480: String body = "This is the updated clob.";
481: String body2 = "Ditto.";
482: PreparedStatement pstmt = _conn
483: .prepareStatement("update FOO set BODY = ?, BODY2 = ? where ID = 0");
484: pstmt.setUnicodeStream(1, new ByteArrayInputStream(body
485: .getBytes("UnicodeBig")), body.length() * 2);
486: pstmt.setUnicodeStream(2, new ByteArrayInputStream(body2
487: .getBytes("UnicodeBig")), body2.length() * 2);
488: assertEquals(1, pstmt.executeUpdate());
489: pstmt.close();
490:
491: assertEquals(body, readClobAsVarchar(0, 1));
492: assertEquals(body, readClobAsStream(0, 1));
493:
494: assertEquals(body2, readClobAsVarchar(0, 2));
495: assertEquals(body2, readClobAsStream(0, 2));
496: }
497:
498: private void insertAndSelectClobAsCharacterStream(boolean compressed)
499: throws Exception {
500: String body = "This is a clob, inserted as if it were a VARCHAR field";
501: String body2 = "Ditto.";
502: createClobTable(compressed);
503: PreparedStatement pstmt = _conn
504: .prepareStatement("insert into FOO values ( 0, ?, ? )");
505: pstmt.setCharacterStream(1, new StringReader(body), body
506: .length());
507: pstmt.setCharacterStream(2, new StringReader(body2), body2
508: .length());
509: assertEquals(1, pstmt.executeUpdate());
510: pstmt.close();
511:
512: assertEquals(body, readClobAsVarchar(0, 1));
513: assertEquals(body, readClobAsStream(0, 1));
514:
515: assertEquals(body2, readClobAsVarchar(0, 2));
516: assertEquals(body2, readClobAsStream(0, 2));
517: }
518:
519: private void updateAndSelectClobAsCharacterStream(boolean compressed)
520: throws Exception {
521: insertAndSelectClobAsCharacterStream(compressed);
522: String body = "This is the updated clob.";
523: String body2 = "This is the updated clob as well.";
524: PreparedStatement pstmt = _conn
525: .prepareStatement("update FOO set BODY = ?, BODY2 = ? where ID = 0");
526: pstmt.setCharacterStream(1, new StringReader(body), body
527: .length());
528: pstmt.setCharacterStream(2, new StringReader(body2), body2
529: .length());
530: assertEquals(1, pstmt.executeUpdate());
531: pstmt.close();
532:
533: assertEquals(body, readClobAsVarchar(0, 1));
534: assertEquals(body, readClobAsStream(0, 1));
535:
536: assertEquals(body2, readClobAsVarchar(0, 2));
537: assertEquals(body2, readClobAsStream(0, 2));
538: }
539:
540: private String readClobAsVarchar(int id, int index)
541: throws Exception {
542: _rset = _stmt
543: .executeQuery("select BODY, BODY2 from FOO where ID = "
544: + id);
545: assertTrue(_rset.next());
546: String value = _rset.getString(index);
547: _rset.close();
548: return value;
549: }
550:
551: private String readClobAsStream(int id, int index) throws Exception {
552: _rset = _stmt
553: .executeQuery("select BODY, BODY2 from FOO where ID = "
554: + id);
555: assertTrue(_rset.next());
556: StringBuffer buf = new StringBuffer();
557: AxionClob clob = (AxionClob) (_rset.getClob(index));
558: Reader in = clob.getCharacterStream();
559: for (int c = in.read(); c != -1; c = in.read()) {
560: buf.append((char) c);
561: }
562: in.close();
563: _rset.close();
564: return buf.toString();
565: }
566: }
|