1 /*
   2  * Copyright (c) 2012 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  * Portions Copyright (c) 2012 IBM Corporation
  26  */
  27 
  28 import java.io.InputStream;
  29 import java.util.HashMap;
  30 import java.util.Iterator;
  31 import java.util.Map;
  32 import java.util.Map.Entry;
  33 
  34 import javax.sql.rowset.serial.SerialBlob;
  35 import javax.sql.rowset.serial.SerialException;
  36 
  37 /**
  38  * @test
  39  * @bug
  40  * @summary tests for getBinaryStream(long, long) on SerialBlob
  41  */
  42 
  43 public class SerialBlobGetBinaryStreamJJ {
  44     static byte[] byteArray = new byte[] { 1, 2, 3, 4, 5 };
  45 
  46     static Map<String, String> errorMsgs = new HashMap<String, String>();
  47 
  48     private static SerialBlob prepareSerialBlob() throws Exception {
  49         return new SerialBlob(byteArray);
  50     }
  51 
  52     // Test the normal behavior when use getBinaryStream(long, long)
  53     public static int testGetBinaryStream() throws Exception {
  54         SerialBlob sb = prepareSerialBlob();
  55         InputStream is = sb.getBinaryStream(1, 3);
  56 
  57         byte[] expected = new byte[] { 1, 2, 3 };
  58         for (int i = 0; i < 3; i++) {
  59             if (expected[i] != is.read()) {
  60                 errorMsgs.put("testGetBinaryStream()", "Unexpected byte");
  61                 return 1;
  62             }
  63         }
  64 
  65         if (is.read() != -1) {
  66             errorMsgs.put("testGetBinaryStream()", "More left in SerialBlob");
  67             return 1;
  68         }
  69         System.out.println("Test Passed");
  70         return 0;
  71     }
  72 
  73     // Test getBinaryStream with pos is less than 0
  74     public static int testGetBinaryStreamPosLT0() throws Exception {
  75         SerialBlob sb = prepareSerialBlob();
  76 
  77         try {
  78             sb.getBinaryStream(-1, 3);
  79             errorMsgs.put("testGetBinaryStreamPosLT0()",
  80                     "Should throw exception when pos < 0");
  81             return 1;
  82         } catch (SerialException se) {
  83             System.out.println("Test Passed");
  84             return 0;
  85         }
  86     }
  87 
  88     // Test getBinaryStream with pos is 0
  89     public static int testGetBinaryStreamPosEquals0() throws Exception {
  90         SerialBlob sb = prepareSerialBlob();
  91 
  92         try {
  93             sb.getBinaryStream(0, 3);
  94             errorMsgs.put("testGetBinaryStreamPosEquals0()",
  95                     "Should throw exception when pos == 0");
  96             return 1;
  97         } catch (SerialException se) {
  98             System.out.println("Test Passed");
  99             return 0;
 100         }
 101     }
 102 
 103     // Test getBinaryStream with length is larger than the back end buffer
 104     // length of the SerialBlob object
 105     public static int testGetBinaryStreamLengthOutOfBounds() throws Exception {
 106         SerialBlob sb = prepareSerialBlob();
 107 
 108         try {
 109             sb.getBinaryStream(1, byteArray.length + 1);
 110             errorMsgs.put("testGetBinaryStreamLengthOutOfBounds()",
 111                     "Should throw exception when pos + length >"
 112                             + "actual length + 1");
 113             return 1;
 114         } catch (SerialException se) {
 115             System.out.println("Test Passed");
 116             return 0;
 117         }
 118     }
 119 
 120     // Test getBinaryStream with pos is larger than the back end buffer length
 121     // of the SerialBlob object
 122     public static int testGetBinaryStreamPosGTLength() throws Exception {
 123         SerialBlob sb = prepareSerialBlob();
 124 
 125         try {
 126             sb.getBinaryStream(byteArray.length + 1, 2);
 127             errorMsgs.put("testGetBinaryStreamPosGTLength()",
 128                     "Should throw exception when pos > Blob's length");
 129             return 1;
 130         } catch (SerialException se) {
 131             System.out.println("Test Passed");
 132             return 0;
 133         }
 134     }
 135 
 136     public static void main(String[] args) throws Exception {
 137         int totalNumber = 0;
 138         int failedNumber = 0;
 139 
 140         failedNumber += testGetBinaryStream();
 141         totalNumber++;
 142 
 143         failedNumber += testGetBinaryStreamPosLT0();
 144         totalNumber++;
 145 
 146         failedNumber += testGetBinaryStreamPosEquals0();
 147         totalNumber++;
 148 
 149         failedNumber += testGetBinaryStreamLengthOutOfBounds();
 150         totalNumber++;
 151 
 152         failedNumber += testGetBinaryStreamPosGTLength();
 153         totalNumber++;
 154 
 155         System.out.println();
 156         System.out.println("Total tests " + totalNumber);
 157         System.out.println("Passed: " + (totalNumber - failedNumber));
 158         System.out.println("Failed: " + failedNumber);
 159         System.out.println();
 160 
 161         if (failedNumber != 0) {
 162             System.out.println("Failed details:");
 163 
 164             Iterator<Entry<String, String>> itr = errorMsgs.entrySet()
 165                     .iterator();
 166 
 167             while (itr.hasNext()) {
 168                 Entry<String, String> entry = itr.next();
 169                 String testName = entry.getKey();
 170                 String message = entry.getValue();
 171 
 172                 System.out.println(testName + ": " + message);
 173             }
 174 
 175             throw new Exception(
 176                     "SerialBlob.getBinaryStream(long, long) test failed");
 177         }
 178     }
 179 }