001: /*-------------------------------------------------------------------------
002: *
003: * Copyright (c) 2005, PostgreSQL Global Development Group
004: *
005: * IDENTIFICATION
006: * $PostgreSQL: pgjdbc/org/postgresql/test/jdbc3/Jdbc3BlobTest.java,v 1.5 2007/07/27 10:15:37 jurka Exp $
007: *
008: *-------------------------------------------------------------------------
009: */
010: package org.postgresql.test.jdbc3;
011:
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.io.OutputStream;
015: import java.sql.Blob;
016: import java.sql.Connection;
017: import java.sql.PreparedStatement;
018: import java.sql.ResultSet;
019: import java.sql.SQLException;
020: import java.util.Arrays;
021:
022: import junit.framework.TestCase;
023:
024: import org.postgresql.test.TestUtil;
025:
026: /**
027: * @author Michael Barker <mailto:mike@middlesoft.co.uk>
028: *
029: */
030: public class Jdbc3BlobTest extends TestCase {
031: Connection _conn;
032: private final static String TABLE = "blobtest";
033: private final static String INSERT = "INSERT INTO " + TABLE
034: + " VALUES (1, lo_creat(-1))";
035: private final static String SELECT = "SELECT ID, DATA FROM "
036: + TABLE + " WHERE ID = 1";
037:
038: public Jdbc3BlobTest(String name) {
039: super (name);
040: }
041:
042: protected void setUp() throws Exception {
043: _conn = TestUtil.openDB();
044: TestUtil.createTable(_conn, TABLE,
045: "ID INT PRIMARY KEY, DATA OID");
046: _conn.setAutoCommit(false);
047: }
048:
049: protected void tearDown() throws SQLException {
050: _conn.setAutoCommit(true);
051: TestUtil.dropTable(_conn, TABLE);
052: TestUtil.closeDB(_conn);
053: }
054:
055: /**
056: * Test the writing and reading of a single byte.
057: *
058: * @throws SQLException
059: */
060: public void test1Byte() throws SQLException {
061: byte[] data = { (byte) 'a' };
062: readWrite(data);
063: }
064:
065: /**
066: * Test the writing and reading of a few bytes.
067: * @throws SQLException
068: */
069: public void testManyBytes() throws SQLException {
070: byte[] data = "aaaaaaaaaa".getBytes();
071: readWrite(data);
072: }
073:
074: /**
075: * Test writing a single byte with an offset.
076: *
077: * @throws SQLException
078: */
079: public void test1ByteOffset() throws SQLException {
080: byte[] data = { (byte) 'a' };
081: readWrite(10, data);
082: }
083:
084: /**
085: * Test the writing and reading of a few bytes with an offset.
086: *
087: * @throws SQLException
088: */
089: public void testManyBytesOffset() throws SQLException {
090: byte[] data = "aaaaaaaaaa".getBytes();
091: readWrite(10, data);
092: }
093:
094: /**
095: * Tests all of the byte values from 0 - 255.
096: *
097: * @throws SQLException
098: */
099: public void testAllBytes() throws SQLException {
100: byte[] data = new byte[256];
101: for (int i = 0; i < data.length; i++) {
102: data[i] = (byte) i;
103: }
104: readWrite(data);
105: }
106:
107: public void testTruncate() throws SQLException {
108: if (!TestUtil.haveMinimumServerVersion(_conn, "8.3"))
109: return;
110:
111: byte data[] = new byte[100];
112: for (byte i = 0; i < data.length; i++) {
113: data[i] = i;
114: }
115: readWrite(data);
116:
117: PreparedStatement ps = _conn.prepareStatement(SELECT);
118: ResultSet rs = ps.executeQuery();
119:
120: assertTrue(rs.next());
121: Blob blob = rs.getBlob("DATA");
122:
123: assertEquals(100, blob.length());
124:
125: blob.truncate(50);
126: assertEquals(50, blob.length());
127:
128: blob.truncate(150);
129: assertEquals(150, blob.length());
130:
131: data = blob.getBytes(1, 200);
132: assertEquals(150, data.length);
133: for (byte i = 0; i < 50; i++) {
134: assertEquals(i, data[i]);
135: }
136:
137: for (int i = 50; i < 150; i++) {
138: assertEquals(0, data[i]);
139: }
140: }
141:
142: /**
143: *
144: * @param data
145: * @throws SQLException
146: */
147: public void readWrite(byte[] data) throws SQLException {
148: readWrite(1, data);
149: }
150:
151: /**
152: *
153: * @param offset
154: * @param data
155: * @throws SQLException
156: */
157: public void readWrite(int offset, byte[] data) throws SQLException {
158:
159: PreparedStatement ps = _conn.prepareStatement(INSERT);
160: ps.executeUpdate();
161: ps.close();
162:
163: ps = _conn.prepareStatement(SELECT);
164: ResultSet rs = ps.executeQuery();
165:
166: assertTrue(rs.next());
167: Blob b = rs.getBlob("DATA");
168: b.setBytes(offset, data);
169:
170: rs.close();
171: ps.close();
172:
173: ps = _conn.prepareStatement(SELECT);
174: rs = ps.executeQuery();
175:
176: assertTrue(rs.next());
177: b = rs.getBlob("DATA");
178: byte[] rspData = b.getBytes(offset, data.length);
179: assertTrue("Request should be the same as the response", Arrays
180: .equals(data, rspData));
181:
182: rs.close();
183: ps.close();
184: }
185:
186: /**
187: * Test the writing and reading of a single byte.
188: *
189: * @throws SQLException
190: * @throws IOException
191: */
192: public void test1ByteStream() throws SQLException, IOException {
193: byte[] data = { (byte) 'a' };
194: readWriteStream(data);
195: }
196:
197: /**
198: * Test the writing and reading of a few bytes.
199: * @throws SQLException
200: * @throws IOException
201: */
202: public void testManyBytesStream() throws SQLException, IOException {
203: byte[] data = "aaaaaaaaaa".getBytes();
204: readWriteStream(data);
205: }
206:
207: /**
208: * Test writing a single byte with an offset.
209: *
210: * @throws SQLException
211: * @throws IOException
212: */
213: public void test1ByteOffsetStream() throws SQLException,
214: IOException {
215: byte[] data = { (byte) 'a' };
216: readWriteStream(10, data);
217: }
218:
219: /**
220: * Test the writing and reading of a few bytes with an offset.
221: *
222: * @throws SQLException
223: * @throws IOException
224: */
225: public void testManyBytesOffsetStream() throws SQLException,
226: IOException {
227: byte[] data = "aaaaaaaaaa".getBytes();
228: readWriteStream(10, data);
229: }
230:
231: /**
232: * Tests all of the byte values from 0 - 255.
233: *
234: * @throws SQLException
235: * @throws IOException
236: */
237: public void testAllBytesStream() throws SQLException, IOException {
238: byte[] data = new byte[256];
239: for (int i = 0; i < data.length; i++) {
240: data[i] = (byte) i;
241: }
242: readWriteStream(data);
243: }
244:
245: public void readWriteStream(byte[] data) throws SQLException,
246: IOException {
247: readWriteStream(1, data);
248: }
249:
250: /**
251: * Reads then writes data to the blob via a stream.
252: *
253: * @param offset
254: * @param data
255: * @throws SQLException
256: * @throws IOException
257: */
258: public void readWriteStream(int offset, byte[] data)
259: throws SQLException, IOException {
260:
261: PreparedStatement ps = _conn.prepareStatement(INSERT);
262: ps.executeUpdate();
263: ps.close();
264:
265: ps = _conn.prepareStatement(SELECT);
266: ResultSet rs = ps.executeQuery();
267:
268: assertTrue(rs.next());
269: Blob b = rs.getBlob("DATA");
270: OutputStream out = b.setBinaryStream(offset);
271: out.write(data);
272: out.flush();
273: out.close();
274:
275: rs.close();
276: ps.close();
277:
278: ps = _conn.prepareStatement(SELECT);
279: rs = ps.executeQuery();
280:
281: assertTrue(rs.next());
282: b = rs.getBlob("DATA");
283: InputStream in = b.getBinaryStream();
284: byte[] rspData = new byte[data.length];
285: in.skip(offset - 1);
286: in.read(rspData);
287: in.close();
288:
289: assertTrue("Request should be the same as the response", Arrays
290: .equals(data, rspData));
291:
292: rs.close();
293: ps.close();
294: }
295:
296: public void testPattern() throws SQLException {
297: byte[] data = "abcdefghijklmnopqrstuvwxyx0123456789".getBytes();
298: byte[] pattern = "def".getBytes();
299:
300: PreparedStatement ps = _conn.prepareStatement(INSERT);
301: ps.executeUpdate();
302: ps.close();
303:
304: ps = _conn.prepareStatement(SELECT);
305: ResultSet rs = ps.executeQuery();
306:
307: assertTrue(rs.next());
308: Blob b = rs.getBlob("DATA");
309: b.setBytes(1, data);
310:
311: rs.close();
312: ps.close();
313:
314: ps = _conn.prepareStatement(SELECT);
315: rs = ps.executeQuery();
316:
317: assertTrue(rs.next());
318: b = rs.getBlob("DATA");
319: long position = b.position(pattern, 1);
320: byte[] rspData = b.getBytes(position, pattern.length);
321: assertTrue("Request should be the same as the response", Arrays
322: .equals(pattern, rspData));
323:
324: rs.close();
325: ps.close();
326:
327: }
328: }
|