001: /*
002: * $Id: AbstractBlobTest.java,v 1.4 2004/04/29 22:41:39 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.ByteArrayOutputStream;
045: import java.io.File;
046: import java.io.InputStream;
047: import java.io.OutputStream;
048: import java.sql.Blob;
049: import java.sql.Connection;
050: import java.sql.DriverManager;
051: import java.sql.PreparedStatement;
052: import java.sql.ResultSet;
053: import java.sql.Statement;
054:
055: import junit.framework.TestCase;
056:
057: import org.apache.commons.codec.binary.Base64;
058: import org.axiondb.jdbc.AxionBlob;
059:
060: /**
061: * @version $Revision: 1.4 $ $Date: 2004/04/29 22:41:39 $
062: * @author Rodney Waldhoff
063: */
064: public abstract class AbstractBlobTest extends TestCase {
065: private Connection _conn = null;
066: private ResultSet _rset = null;
067: private Statement _stmt = null;
068: private String _axiondbLobdir = null;
069:
070: public AbstractBlobTest(String s) {
071: super (s);
072: }
073:
074: public abstract String getConnectString();
075:
076: public void setUp() throws Exception {
077: _axiondbLobdir = System.getProperty("axiondb.lobdir");
078: System.setProperty("axiondb.lobdir", "testdb");
079:
080: Class.forName("org.axiondb.jdbc.AxionDriver");
081:
082: _conn = DriverManager.getConnection(getConnectString());
083: _stmt = _conn.createStatement();
084: }
085:
086: public void tearDown() throws Exception {
087: try {
088: _rset.close();
089: } catch (Exception t) {
090: }
091: try {
092: _stmt.close();
093: } catch (Exception t) {
094: }
095: try {
096: _conn.close();
097: } catch (Exception t) {
098: }
099: _rset = null;
100: _stmt = null;
101: _conn = null;
102: {
103: Connection conn = DriverManager
104: .getConnection(getConnectString());
105: Statement stmt = conn.createStatement();
106: stmt.execute("shutdown");
107: stmt.close();
108: conn.close();
109: }
110: deleteFile(new File("testdb"));
111: if (null == _axiondbLobdir) {
112: System.getProperties().remove("axiondb.lobdir");
113: } else {
114: System.setProperty("axiondb.lobdir", _axiondbLobdir);
115: }
116: }
117:
118: protected boolean deleteFile(File file) throws Exception {
119: if (file.exists()) {
120: if (file.isDirectory()) {
121: File[] files = file.listFiles();
122: for (int i = 0; i < files.length; i++) {
123: deleteFile(files[i]);
124: }
125: }
126: return file.delete();
127: }
128: return true;
129: }
130:
131: private void createBlobTable(boolean compressed) throws Exception {
132: if (compressed) {
133: _stmt
134: .execute("create table FOO ( ID integer, BODY compressedblob )");
135: } else {
136: _stmt.execute("create table FOO ( ID integer, BODY blob )");
137: }
138: }
139:
140: public void testSetBlobAsBinaryStream() throws Exception {
141: updateAndSelectBlobAsBinaryStream(false);
142: }
143:
144: public void testSetCompressedBlobAsBinaryStream() throws Exception {
145: updateAndSelectBlobAsBinaryStream(true);
146: }
147:
148: public void testSetBlobAsByteArray() throws Exception {
149: updateAndSelectBlobAsByteArray(false);
150: }
151:
152: public void testSetCompressedBlobAsByteArray() throws Exception {
153: updateAndSelectBlobAsByteArray(true);
154: }
155:
156: public void testSetBlobViaBase64() throws Exception {
157: updateAndSelectBlobViaBase64Encoding(false);
158: }
159:
160: public void testSetCompressedBlobViaBase64() throws Exception {
161: updateAndSelectBlobViaBase64Encoding(true);
162: }
163:
164: public void testInsertEmptyBlob() throws Exception {
165: createBlobTable(false);
166: insertEmptyBlob();
167: }
168:
169: public void testInsertEmptyCompressedBlob() throws Exception {
170: createBlobTable(true);
171: insertEmptyBlob();
172: }
173:
174: public void testGetBinaryStream() throws Exception {
175: createBlobTable(false);
176: _stmt
177: .execute("insert into FOO ( ID, BODY ) values ( 0, BASE64DECODE('VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQ=') )");
178: _rset = _stmt.executeQuery("select ID, BODY from FOO");
179: assertNotNull(_rset);
180: assertTrue(_rset.next());
181: assertEquals(0, _rset.getInt(1));
182: assertNotNull(_rset.getBinaryStream(2));
183: assertNotNull(_rset.getBinaryStream("body"));
184: }
185:
186: public void testGetBinaryStreamWhenNull() throws Exception {
187: createBlobTable(false);
188: _stmt
189: .execute("insert into FOO ( ID, BODY ) values ( 0, NULL )");
190: _rset = _stmt.executeQuery("select ID, BODY from FOO");
191: assertNotNull(_rset);
192: assertTrue(_rset.next());
193: assertEquals(0, _rset.getInt(1));
194: assertNull(_rset.getBinaryStream(2));
195: assertNull(_rset.getBinaryStream("body"));
196: }
197:
198: private void insertEmptyBlob() throws Exception {
199: _stmt
200: .execute("insert into FOO ( ID, BODY ) values ( 0, 'newlob()' )");
201: _rset = _stmt.executeQuery("select ID, BODY from FOO");
202: assertNotNull(_rset);
203: assertTrue(_rset.next());
204: assertEquals(0, _rset.getInt(1));
205: assertNotNull(_rset.getBlob(2));
206: assertNotNull(_rset.getBlob("body"));
207: assertTrue(_rset.getBlob(2) instanceof AxionBlob);
208: }
209:
210: public void testWriteToEmptyBlob() throws Exception {
211: createBlobTable(false);
212: writeToEmptyBlob();
213: }
214:
215: public void testWriteToEmptyCompressedBlob() throws Exception {
216: createBlobTable(true);
217: writeToEmptyBlob();
218: }
219:
220: private void writeToEmptyBlob() throws Exception {
221: {
222: _stmt
223: .execute("insert into FOO ( ID, BODY ) values ( 0, 'newlob()' )");
224: }
225: {
226: _rset = _stmt.executeQuery("select ID, BODY from FOO");
227: assertNotNull(_rset);
228: assertTrue(_rset.next());
229: assertEquals(0, _rset.getInt(1));
230: assertNotNull(_rset.getBlob(2));
231: assertTrue(_rset.getBlob(2) instanceof AxionBlob);
232: AxionBlob blob = (AxionBlob) (_rset.getBlob(2));
233: OutputStream out = blob.setBinaryStream(0L);
234: for (int i = 0; i < 10; i++) {
235: out
236: .write("The quick brown fox jumped over the lazy dogs. "
237: .getBytes());
238: }
239: out.close();
240: _rset.close();
241: }
242: {
243: _rset = _stmt.executeQuery("select ID, BODY from FOO");
244: assertNotNull(_rset);
245: assertTrue(_rset.next());
246: assertEquals(0, _rset.getInt(1));
247: assertNotNull(_rset.getBlob(2));
248: assertTrue(_rset.getBlob(2) instanceof AxionBlob);
249: AxionBlob blob = (AxionBlob) (_rset.getBlob(2));
250: InputStream in = blob.getBinaryStream();
251: ByteArrayOutputStream read = new ByteArrayOutputStream();
252: for (int c = in.read(); c != -1; c = in.read()) {
253: read.write(c);
254: }
255: in.close();
256: _rset.close();
257: StringBuffer expected = new StringBuffer();
258: for (int i = 0; i < 10; i++) {
259: expected
260: .append("The quick brown fox jumped over the lazy dogs. ");
261: }
262: String actual = new String(read.toByteArray());
263: assertEquals(expected.toString(), actual);
264: }
265: }
266:
267: public void testWriteToNonEmptyBlob() throws Exception {
268: createBlobTable(false);
269: writeToNonEmptyBlob();
270: }
271:
272: public void testWriteToNonEmptyCompressedBlob() throws Exception {
273: createBlobTable(true);
274: writeToNonEmptyBlob();
275: }
276:
277: private void writeToNonEmptyBlob() throws Exception {
278: writeToEmptyBlob();
279: {
280: _rset = _stmt.executeQuery("select ID, BODY from FOO");
281: assertNotNull(_rset);
282: assertTrue(_rset.next());
283: assertEquals(0, _rset.getInt(1));
284: assertNotNull(_rset.getClob(2));
285: assertTrue(_rset.getBlob(2) instanceof AxionBlob);
286: AxionBlob blob = (AxionBlob) (_rset.getBlob(2));
287: blob.truncate(0);
288: OutputStream out = blob.setBinaryStream(0L);
289: out
290: .write("It was the best of times, it was the worst of times."
291: .getBytes());
292: out.close();
293: _rset.close();
294: }
295: {
296: _rset = _stmt.executeQuery("select ID, BODY from FOO");
297: assertNotNull(_rset);
298: assertTrue(_rset.next());
299: assertEquals(0, _rset.getInt(1));
300: assertNotNull(_rset.getBlob(2));
301: assertTrue(_rset.getBlob(2) instanceof AxionBlob);
302: AxionBlob blob = (AxionBlob) (_rset.getBlob(2));
303: InputStream in = blob.getBinaryStream();
304: ByteArrayOutputStream read = new ByteArrayOutputStream();
305: for (int c = in.read(); c != -1; c = in.read()) {
306: read.write(c);
307: }
308: in.close();
309: _rset.close();
310: String expected = "It was the best of times, it was the worst of times.";
311: String actual = new String(read.toByteArray());
312: assertEquals(expected, actual);
313: }
314: }
315:
316: private void insertAndSelectBlobAsBinaryStream(boolean compressed)
317: throws Exception {
318: byte[] body = "This is a blob, inserted as if it were a byte array field"
319: .getBytes();
320: createBlobTable(compressed);
321: PreparedStatement pstmt = _conn
322: .prepareStatement("insert into FOO values ( 0, ? )");
323: pstmt.setBinaryStream(1, new ByteArrayInputStream(body),
324: body.length);
325: assertEquals(1, pstmt.executeUpdate());
326: pstmt.close();
327: compareByteArrays(body, readBlobAsStream(0));
328: assertEquals(new String(Base64.encodeBase64(body)),
329: readBlobAsBase64String(0));
330: }
331:
332: private void updateAndSelectBlobAsBinaryStream(boolean compressed)
333: throws Exception {
334: insertAndSelectBlobAsBinaryStream(compressed);
335: byte[] body = "This is the updated data".getBytes();
336: PreparedStatement pstmt = _conn
337: .prepareStatement("update FOO set BODY = ? where ID = 0");
338: pstmt.setBinaryStream(1, new ByteArrayInputStream(body),
339: body.length);
340: assertEquals(1, pstmt.executeUpdate());
341: pstmt.close();
342: compareByteArrays(body, readBlobAsStream(0));
343: assertEquals(new String(Base64.encodeBase64(body)),
344: readBlobAsBase64String(0));
345: }
346:
347: private void insertAndSelectBlobAsByteArray(boolean compressed)
348: throws Exception {
349: byte[] body = "This is a blob, inserted as if it were a byte array field"
350: .getBytes();
351: createBlobTable(compressed);
352: PreparedStatement pstmt = _conn
353: .prepareStatement("insert into FOO values ( 0, ? )");
354: pstmt.setObject(1, body);
355: assertEquals(1, pstmt.executeUpdate());
356: pstmt.close();
357: compareByteArrays(body, readBlobAsStream(0));
358: assertEquals(new String(Base64.encodeBase64(body)),
359: readBlobAsBase64String(0));
360: }
361:
362: private void updateAndSelectBlobAsByteArray(boolean compressed)
363: throws Exception {
364: insertAndSelectBlobAsByteArray(compressed);
365: byte[] body = "This is the updated data".getBytes();
366: PreparedStatement pstmt = _conn
367: .prepareStatement("update FOO set BODY = ? where ID = 0");
368: pstmt.setObject(1, body);
369: assertEquals(1, pstmt.executeUpdate());
370: pstmt.close();
371: compareByteArrays(body, readBlobAsStream(0));
372: assertEquals(new String(Base64.encodeBase64(body)),
373: readBlobAsBase64String(0));
374: }
375:
376: private void insertAndSelectBlobViaBase64Encoding(boolean compressed)
377: throws Exception {
378: byte[] body = "This is a blob, inserted as if it were a byte array field"
379: .getBytes();
380: String encoded = new String(Base64.encodeBase64(body));
381: createBlobTable(compressed);
382: PreparedStatement pstmt = _conn
383: .prepareStatement("insert into FOO values ( 0, base64decode(?) )");
384: pstmt.setString(1, encoded);
385: assertEquals(1, pstmt.executeUpdate());
386: pstmt.close();
387: compareByteArrays(body, readBlobAsStream(0));
388: assertEquals(encoded, readBlobAsBase64String(0));
389: }
390:
391: private void updateAndSelectBlobViaBase64Encoding(boolean compressed)
392: throws Exception {
393: insertAndSelectBlobViaBase64Encoding(compressed);
394: byte[] body = "This is the updated data".getBytes();
395: String encoded = new String(Base64.encodeBase64(body));
396: PreparedStatement pstmt = _conn
397: .prepareStatement("update FOO set BODY = BASE64DECODE(?) where ID = 0");
398: pstmt.setString(1, encoded);
399: assertEquals(1, pstmt.executeUpdate());
400: pstmt.close();
401: compareByteArrays(body, readBlobAsStream(0));
402: assertEquals(encoded, readBlobAsBase64String(0));
403: }
404:
405: private byte[] readBlobAsStream(int id) throws Exception {
406: _rset = _stmt.executeQuery("select BODY from FOO where ID = "
407: + id);
408: assertTrue(_rset.next());
409: ByteArrayOutputStream read = new ByteArrayOutputStream();
410: Blob blob = _rset.getBlob(1);
411: InputStream in = blob.getBinaryStream();
412: for (int b = in.read(); b != -1; b = in.read()) {
413: read.write((byte) b);
414: }
415: in.close();
416: _rset.close();
417: return read.toByteArray();
418: }
419:
420: private String readBlobAsBase64String(int id) throws Exception {
421: _rset = _stmt
422: .executeQuery("select BASE64ENCODE(BODY) from FOO where ID = "
423: + id);
424: assertTrue(_rset.next());
425: String value = _rset.getString(1);
426: _rset.close();
427: return value;
428: }
429:
430: private void compareByteArrays(byte[] expected, byte[] actual) {
431: assertEquals("array length", expected.length, actual.length);
432: for (int i = 0; i < expected.length; i++) {
433: assertEquals("[" + i + "]", expected[i], actual[i]);
434: }
435: }
436: }
|