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