1/*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package test.rowset.serial;
24
25import java.io.InputStream;
26import java.io.OutputStream;
27import java.util.Arrays;
28import javax.sql.rowset.serial.SerialBlob;
29import javax.sql.rowset.serial.SerialException;
30import static org.testng.Assert.*;
31import org.testng.annotations.Test;
32import util.BaseTest;
33import util.StubBlob;
34
35public class SerialBlobTests extends BaseTest {
36
37    // byte[] used to populate SerialBlob
38    private byte[] bytes = new byte[]{1, 2, 3, 4, 5};
39
40    /*
41     * Validate calling free() does not throw an Exception
42     */
43    @Test
44    public void test() throws Exception {
45        SerialBlob sb = new SerialBlob(new StubBlob());
46        sb.free();
47    }
48
49    /*
50     * Validate calling getBinaryStream() after calling free() throws an
51     * SerialException
52     */
53    @Test(expectedExceptions = SerialException.class)
54    public void test01() throws Exception {
55        SerialBlob sb = new SerialBlob(new StubBlob());
56        sb.free();
57        sb.getBinaryStream();
58    }
59
60    /*
61     * Validate calling getBinaryStream() after calling free() throws an
62     * SerialException
63     */
64    @Test(expectedExceptions = SerialException.class)
65    public void test02() throws Exception {
66        SerialBlob sb = new SerialBlob(new StubBlob());
67        sb.free();
68        sb.getBinaryStream(1, 5);
69    }
70
71    /*
72     * Validate calling getBytes() after calling free() throws an
73     * SerialException
74     */
75    @Test(expectedExceptions = SerialException.class)
76    public void test03() throws Exception {
77        SerialBlob sb = new SerialBlob(new StubBlob());
78        sb.free();
79        sb.getBytes(1, 1);
80    }
81
82    /*
83     * Validate calling getLength() after calling free() throws an
84     * SerialException
85     */
86    @Test(expectedExceptions = SerialException.class)
87    public void test04() throws Exception {
88        SerialBlob sb = new SerialBlob(new StubBlob());
89        sb.free();
90        sb.length();
91    }
92
93    /*
94     * Validate calling position() after calling free() throws an
95     * SerialException
96     */
97    @Test(expectedExceptions = SerialException.class)
98    public void test05() throws Exception {
99        SerialBlob sb = new SerialBlob(new StubBlob());
100        sb.free();
101        sb.position(new byte[5], 1);
102    }
103
104    /*
105     * Validate calling position() after calling free() throws an
106     * SerialException
107     */
108    @Test(expectedExceptions = SerialException.class)
109    public void test06() throws Exception {
110        SerialBlob sb = new SerialBlob(new StubBlob());
111        sb.free();
112        sb.position(new StubBlob(), 1);
113    }
114
115    /*
116     * Validate calling free() after calling setBinaryStream() throws an
117     * SerialException
118     */
119    @Test(expectedExceptions = SerialException.class)
120    public void test07() throws Exception {
121        SerialBlob sb = new SerialBlob(new StubBlob());
122        sb.free();
123        sb.setBinaryStream(5);
124    }
125
126    /*
127     * Validate calling free() after calling setBytes() throws an
128     * SerialException
129     */
130    @Test(expectedExceptions = SerialException.class)
131    public void test08() throws Exception {
132        SerialBlob sb = new SerialBlob(new StubBlob());
133        sb.free();
134        sb.setBytes(1, new byte[5]);
135    }
136
137    /*
138     * Validate calling setBytes() after calling free() throws an
139     * SerialException
140     */
141    @Test(expectedExceptions = SerialException.class)
142    public void test09() throws Exception {
143        SerialBlob sb = new SerialBlob(new StubBlob());
144        sb.free();
145        sb.setBytes(1, new byte[10], 0, 5);
146    }
147
148    /*
149     * Validate calling truncate() after calling free() throws an
150     * SerialException
151     */
152    @Test(expectedExceptions = SerialException.class)
153    public void test10() throws Exception {
154        SerialBlob sb = new SerialBlob(new StubBlob());
155        sb.free();
156        sb.truncate(1);
157    }
158
159    /*
160     * Validate getBinaryStream returns the correct bytes
161     */
162    @Test
163    public void test11() throws Exception {
164        byte[] expected = new byte[]{1, 2, 3};
165        SerialBlob sb = new SerialBlob(bytes);
166        InputStream is = sb.getBinaryStream(1, 3);
167        for (byte b : expected) {
168            byte val = (byte) is.read();
169            assertTrue(b == val, val + " does not match " + b);
170        }
171    }
172
173    /*
174     * Validate a SerialException is thrown if pos < 0 for getBinaryStream
175     */
176    @Test(expectedExceptions = SerialException.class)
177    public void test12() throws Exception {
178        SerialBlob sb = new SerialBlob(bytes);
179        InputStream is = sb.getBinaryStream(-1, 3);
180    }
181
182    /*
183     * Validate a SerialException is thrown if pos = 0 for getBinaryStream
184     */
185    @Test(expectedExceptions = SerialException.class)
186    public void test13() throws Exception {
187        SerialBlob sb = new SerialBlob(bytes);
188        InputStream is = sb.getBinaryStream(0, 3);
189    }
190
191    /*
192     * Validate a SerialException is thrown if len > the length of the stream
193     * for getBinaryStream
194     */
195    @Test(expectedExceptions = SerialException.class)
196    public void test14() throws Exception {
197        SerialBlob sb = new SerialBlob(bytes);
198        InputStream is = sb.getBinaryStream(0, 3);
199    }
200
201    /*
202     * Validate a SerialException is thrown if length < 1
203     */
204    @Test(expectedExceptions = SerialException.class)
205    public void test15() throws Exception {
206        SerialBlob sb = new SerialBlob(bytes);
207        InputStream is = sb.getBinaryStream(1, 0);
208    }
209
210    /*
211     * Validate a SerialException is thrown if length > byte array length
212     */
213    @Test(expectedExceptions = SerialException.class)
214    public void test16() throws Exception {
215        SerialBlob sb = new SerialBlob(bytes);
216        InputStream is = sb.getBinaryStream(1, 6);
217    }
218
219    /*
220     * Validate a SerialException is thrown if pos > byte array length
221     */
222    @Test(expectedExceptions = SerialException.class)
223    public void test17() throws Exception {
224        SerialBlob sb = new SerialBlob(bytes);
225        InputStream is = sb.getBinaryStream(bytes.length + 2, 6);
226    }
227
228    /*
229     * Validate that a cloned SerializedBlob bytes match the original
230     */
231    @Test
232    public void test18() throws Exception {
233        SerialBlob sb = new SerialBlob(bytes);
234        SerialBlob sb2 = (SerialBlob) sb.clone();
235        assertTrue(
236                Arrays.equals(sb.getBytes(1, (int) sb.length()),
237                        sb2.getBytes(1, (int) sb2.length())),
238                "arrays do not match ");
239    }
240
241    /*
242     * Test clone after free has been called that the clone is not accessible
243     */
244    @Test(expectedExceptions = SerialException.class)
245    public void test19() throws Exception {
246        SerialBlob sb = new SerialBlob(bytes);
247        sb.free();
248        SerialBlob sb2 = (SerialBlob) sb.clone();
249        InputStream is = sb2.getBinaryStream(1, 3);
250    }
251
252    /*
253     * Validate that a SerialBlob that is serialized & deserialized is equal to
254     * itself
255     */
256    @Test
257    public void test20() throws Exception {
258        SerialBlob sb = new SerialBlob(bytes);
259        SerialBlob sb2 = serializeDeserializeObject(sb);
260        assertTrue(sb.equals(sb2), "SerialBlob not equal");
261    }
262
263    /*
264     * Validate a SerialException is thrown if byte[] is used to
265     * create the SeriablBlob and setBinaryStream is called
266     */
267    @Test(expectedExceptions = SerialException.class)
268    public void test21() throws Exception {
269        SerialBlob sb = new SerialBlob(bytes);
270        sb.setBinaryStream(3);
271    }
272
273    /*
274     * Validate that setBytes will properly write a set of bytes to the
275     * specified location in the SerialBlob and the correct count is returned
276     * for bytes written
277     */
278    @Test
279    public void test22() throws Exception {
280        byte[] diff = new byte[]{7, 8, 9};
281        byte[] expected = new byte[]{1, 7, 8, 9, 5};
282        SerialBlob sb = new SerialBlob(bytes);
283        int written = sb.setBytes(2, diff);
284        assertEquals(written, diff.length);
285        assertTrue(
286                Arrays.equals(sb.getBytes(1, (int) sb.length()),
287                        expected),
288                "arrays do not match ");
289    }
290
291    /*
292     * Validate that setBytes will properly write a set of bytes to the
293     * specified location in the SerialBlob and the correct count is returned
294     * for bytes written
295     */
296    @Test
297    public void test23() throws Exception {
298        int bytesToWrite = 3;
299        byte[] diff = new byte[]{7, 8, 9, 0};
300        byte[] expected = new byte[]{1, 8, 9, 0, 5};
301        SerialBlob sb = new SerialBlob(bytes);
302        int written = sb.setBytes(2, diff, 1, bytesToWrite);
303        assertEquals(written, bytesToWrite);
304        assertTrue(
305                Arrays.equals(sb.getBytes(1, (int) sb.length()),
306                        expected),
307                "arrays do not match ");
308    }
309
310    /*
311     * Validate that truncate reduces the length of the SerlizedBlob to the
312     * specified value
313     */
314    @Test
315    public void test24() throws Exception {
316        SerialBlob sb = new SerialBlob(bytes);
317        sb.truncate(0);
318        assertTrue(sb.length() == 0);
319        sb = new SerialBlob(bytes);
320        sb.truncate(3);
321        assertTrue(sb.length() == 3);
322    }
323
324    /*
325     * Validate getBinaryStream returns the correct bytes
326     */
327    @Test
328    public void test25() throws Exception {
329        byte[] expected = bytes;
330        SerialBlob sb = new SerialBlob(bytes);
331        InputStream is = sb.getBinaryStream();
332        for (byte b : expected) {
333            byte val = (byte) is.read();
334            assertTrue(b == val, val + " does not match " + b);
335        }
336    }
337
338    /*
339     * Validate setBinaryStream returns an OutputStream when passed a Blob
340     */
341    @Test
342    public void test26() throws Exception {
343        SerialBlob sb = new SerialBlob(new StubBlob());
344        OutputStream os = sb.setBinaryStream(0);
345        assertTrue(os != null);
346    }
347
348    /*
349     * Validate that position returns the correct starting location for a
350     * pattern in the SerialBlob
351     */
352    @Test
353    public void test27() throws Exception {
354        long expectedPos = 3; // starting offset is 1 vs 0
355        byte[] pattern = new byte[]{3, 4};
356        SerialBlob sb = new SerialBlob(bytes);
357        long pos = sb.position(pattern, 1);
358        assertEquals(pos, expectedPos);
359    }
360
361    /*
362     * Validate that position returns the correct starting location for a
363     * pattern in the SerialBlob
364     */
365    @Test
366    public void test28() throws Exception {
367        long expectedPos = 3; // starting offset is 1 vs 0
368        byte[] pattern = new byte[]{3, 4, 5};
369        SerialBlob sb = new SerialBlob(bytes);
370        long pos = sb.position(pattern, 2);
371        assertEquals(pos, expectedPos);
372    }
373
374    /*
375     * Validate that position returns the correct starting location for a
376     * pattern in the SerialBlob
377     */
378    @Test
379    public void test29() throws Exception {
380        long expectedPos = 2; // starting offset is 1 vs 0
381        byte[] pattern = new byte[]{4, 6};
382        SerialBlob sb = new SerialBlob(new StubBlob());
383        long pos = sb.position(pattern, 1);
384        assertEquals(pos, expectedPos);
385    }
386
387    /*
388     * Validate that position returns the correct starting location for a
389     * pattern in the SerialBlob
390     */
391    @Test
392    public void test30() throws Exception {
393        long expectedPos = 3; // starting offset is 1 vs 0
394        byte[] pattern = new byte[]{6, 8};
395        SerialBlob sb = new SerialBlob(new StubBlob());
396        long pos = sb.position(pattern, 2);
397        assertEquals(pos, expectedPos);
398    }
399}
400