File: Jdbc3BlobTest.java

package info (click to toggle)
libpgjava 8.4-701-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,532 kB
  • ctags: 4,162
  • sloc: java: 33,948; xml: 3,158; makefile: 14; sh: 10
file content (342 lines) | stat: -rw-r--r-- 8,519 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*-------------------------------------------------------------------------
*
* Copyright (c) 2005-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc3/Jdbc3BlobTest.java,v 1.6 2008/01/08 06:56:31 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test.jdbc3;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

import junit.framework.TestCase;

import org.postgresql.test.TestUtil;

/**
 * @author Michael Barker <mailto:mike@middlesoft.co.uk>
 *
 */
public class Jdbc3BlobTest extends TestCase
{
    Connection _conn;
    private final static String TABLE = "blobtest";
    private final static String INSERT = "INSERT INTO " + TABLE + " VALUES (1, lo_creat(-1))";
    private final static String SELECT = "SELECT ID, DATA FROM " + TABLE + " WHERE ID = 1";

    public Jdbc3BlobTest(String name)
    {
        super(name);
    }

    protected void setUp() throws Exception
    {
        _conn = TestUtil.openDB();
        TestUtil.createTable(_conn, TABLE, "ID INT PRIMARY KEY, DATA OID");
        _conn.setAutoCommit(false);
    }

    protected void tearDown() throws SQLException
    {
        _conn.setAutoCommit(true);
        TestUtil.dropTable(_conn, TABLE);
        TestUtil.closeDB(_conn);
    }

    /**
     * Test the writing and reading of a single byte.
     * 
     * @throws SQLException
     */
    public void test1Byte() throws SQLException
    {
        byte[] data = { (byte) 'a' };
        readWrite(data);
    }

    /**
     * Test the writing and reading of a few bytes.
     * @throws SQLException
     */
    public void testManyBytes() throws SQLException
    {
        byte[] data = "aaaaaaaaaa".getBytes();
        readWrite(data);
    }

    /**
     * Test writing a single byte with an offset.
     * 
     * @throws SQLException
     */
    public void test1ByteOffset() throws SQLException
    {
        byte[] data = { (byte) 'a' };
        readWrite(10, data);
    }

    /**
     * Test the writing and reading of a few bytes with an offset.
     * 
     * @throws SQLException
     */
    public void testManyBytesOffset() throws SQLException
    {
        byte[] data = "aaaaaaaaaa".getBytes();
        readWrite(10, data);
    }

    /**
     * Tests all of the byte values from 0 - 255.
     * 
     * @throws SQLException
     */
    public void testAllBytes() throws SQLException
    {
        byte[] data = new byte[256];
        for (int i = 0; i < data.length; i++)
        {
           data[i] = (byte) i;
        }
        readWrite(data);
    }

    public void testTruncate() throws SQLException
    {
        if (!TestUtil.haveMinimumServerVersion(_conn, "8.3"))
            return;

        byte data[] = new byte[100];
        for (byte i=0; i<data.length; i++) {
            data[i] = i;
        }
        readWrite(data);

        PreparedStatement ps = _conn.prepareStatement(SELECT);
        ResultSet rs = ps.executeQuery();
        
        assertTrue(rs.next());
        Blob blob = rs.getBlob("DATA");

        assertEquals(100, blob.length());

        blob.truncate(50);
        assertEquals(50, blob.length());

        blob.truncate(150);
        assertEquals(150, blob.length());

        data = blob.getBytes(1, 200);
        assertEquals(150, data.length);
        for (byte i=0; i<50; i++) {
            assertEquals(i, data[i]);
        }

        for (int i=50; i<150; i++) {
            assertEquals(0, data[i]);
        }
    }

    /**
     * 
     * @param data
     * @throws SQLException
     */
    public void readWrite(byte[] data) throws SQLException
    {
        readWrite(1, data);
    }

    /**
     * 
     * @param offset
     * @param data
     * @throws SQLException
     */
    public void readWrite(int offset, byte[] data) throws SQLException
    {

        PreparedStatement ps = _conn.prepareStatement(INSERT);
        ps.executeUpdate();
        ps.close();

        ps = _conn.prepareStatement(SELECT);
        ResultSet rs = ps.executeQuery();

        assertTrue(rs.next());
        Blob b = rs.getBlob("DATA");
        b.setBytes(offset, data);

        rs.close();
        ps.close();

        ps = _conn.prepareStatement(SELECT);
        rs = ps.executeQuery();

        assertTrue(rs.next());
        b = rs.getBlob("DATA");
        byte[] rspData = b.getBytes(offset, data.length);
        assertTrue("Request should be the same as the response", Arrays.equals(data, rspData));
 
        rs.close();
        ps.close();
    }


    /**
     * Test the writing and reading of a single byte.
     * 
     * @throws SQLException
     * @throws IOException 
     */
    public void test1ByteStream() throws SQLException, IOException
    {
        byte[] data = { (byte) 'a' };
        readWriteStream(data);
    }

    /**
     * Test the writing and reading of a few bytes.
     * @throws SQLException
     * @throws IOException 
     */
    public void testManyBytesStream() throws SQLException, IOException
    {
        byte[] data = "aaaaaaaaaa".getBytes();
        readWriteStream(data);
    }

    /**
     * Test writing a single byte with an offset.
     * 
     * @throws SQLException
     * @throws IOException 
     */
    public void test1ByteOffsetStream() throws SQLException, IOException
    {
        byte[] data = { (byte) 'a' };
        readWriteStream(10, data);
    }

    /**
     * Test the writing and reading of a few bytes with an offset.
     * 
     * @throws SQLException
     * @throws IOException 
     */
    public void testManyBytesOffsetStream() throws SQLException, IOException
    {
        byte[] data = "aaaaaaaaaa".getBytes();
        readWriteStream(10, data);
    }

    /**
     * Tests all of the byte values from 0 - 255.
     * 
     * @throws SQLException
     * @throws IOException 
     */
    public void testAllBytesStream() throws SQLException, IOException
    {
        byte[] data = new byte[256];
        for (int i = 0; i < data.length; i++)
        {
           data[i] = (byte) i;
        }
        readWriteStream(data);
    }

    public void readWriteStream(byte[] data) throws SQLException, IOException
    {
        readWriteStream(1, data);
    }


    /**
     * Reads then writes data to the blob via a stream.
     * 
     * @param offset
     * @param data
     * @throws SQLException
     * @throws IOException 
     */
    public void readWriteStream(int offset, byte[] data) throws SQLException, IOException
    {

        PreparedStatement ps = _conn.prepareStatement(INSERT);
        ps.executeUpdate();
        ps.close();
       
        ps = _conn.prepareStatement(SELECT);
        ResultSet rs = ps.executeQuery();

        assertTrue(rs.next());
        Blob b = rs.getBlob("DATA");
        OutputStream out = b.setBinaryStream(offset);
        out.write(data);
        out.flush();
        out.close();

        rs.close();
        ps.close();

        ps = _conn.prepareStatement(SELECT);
        rs = ps.executeQuery();

        assertTrue(rs.next());
        b = rs.getBlob("DATA");
        InputStream in = b.getBinaryStream();
        byte[] rspData = new byte[data.length];
        in.skip(offset-1);
        in.read(rspData);
        in.close();

        assertTrue("Request should be the same as the response", Arrays.equals(data, rspData));
 
        rs.close();
        ps.close();
    }

    public void testPattern() throws SQLException
    {
        byte[] data = "abcdefghijklmnopqrstuvwxyx0123456789".getBytes();
        byte[] pattern = "def".getBytes();

        PreparedStatement ps = _conn.prepareStatement(INSERT);
        ps.executeUpdate();
        ps.close();

        ps = _conn.prepareStatement(SELECT);
        ResultSet rs = ps.executeQuery();

        assertTrue(rs.next());
        Blob b = rs.getBlob("DATA");
        b.setBytes(1, data);

        rs.close();
        ps.close();

        ps = _conn.prepareStatement(SELECT);
        rs = ps.executeQuery();

        assertTrue(rs.next());
        b = rs.getBlob("DATA");
        long position = b.position(pattern, 1);
        byte[] rspData = b.getBytes(position, pattern.length);
        assertTrue("Request should be the same as the response", Arrays.equals(pattern, rspData));

        rs.close();
        ps.close();

    }
}