--- /dev/null 2012-09-21 10:40:39.978073220 +0800 +++ new/test/javax/sql/rowset/serial/SerialBlob/SerialBlobGetBinaryStreamJJ.java 2012-09-21 10:59:39.430229670 +0800 @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * Portions Copyright (c) 2012 IBM Corporation + */ + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import javax.sql.rowset.serial.SerialBlob; +import javax.sql.rowset.serial.SerialException; + +/** + * @test + * @bug + * @summary tests for getBinaryStream(long, long) on SerialBlob + */ + +public class SerialBlobGetBinaryStreamJJ { + static byte[] byteArray = new byte[] { 1, 2, 3, 4, 5 }; + + static Map errorMsgs = new HashMap(); + + private static SerialBlob prepareSerialBlob() throws Exception { + return new SerialBlob(byteArray); + } + + // Test the normal behavior when use getBinaryStream(long, long) + public static int testGetBinaryStream() throws Exception { + SerialBlob sb = prepareSerialBlob(); + InputStream is = sb.getBinaryStream(1, 3); + + byte[] expected = new byte[] { 1, 2, 3 }; + for (int i = 0; i < 3; i++) { + if (expected[i] != is.read()) { + errorMsgs.put("testGetBinaryStream()", "Unexpected byte"); + return 1; + } + } + + if (is.read() != -1) { + errorMsgs.put("testGetBinaryStream()", "More left in SerialBlob"); + return 1; + } + System.out.println("Test Passed"); + return 0; + } + + // Test getBinaryStream with pos is less than 0 + public static int testGetBinaryStreamPosLT0() throws Exception { + SerialBlob sb = prepareSerialBlob(); + + try { + sb.getBinaryStream(-1, 3); + errorMsgs.put("testGetBinaryStreamPosLT0()", + "Should throw exception when pos < 0"); + return 1; + } catch (SerialException se) { + System.out.println("Test Passed"); + return 0; + } + } + + // Test getBinaryStream with pos is 0 + public static int testGetBinaryStreamPosEquals0() throws Exception { + SerialBlob sb = prepareSerialBlob(); + + try { + sb.getBinaryStream(0, 3); + errorMsgs.put("testGetBinaryStreamPosEquals0()", + "Should throw exception when pos == 0"); + return 1; + } catch (SerialException se) { + System.out.println("Test Passed"); + return 0; + } + } + + // Test getBinaryStream with length is larger than the back end buffer + // length of the SerialBlob object + public static int testGetBinaryStreamLengthOutOfBounds() throws Exception { + SerialBlob sb = prepareSerialBlob(); + + try { + sb.getBinaryStream(1, byteArray.length + 1); + errorMsgs.put("testGetBinaryStreamLengthOutOfBounds()", + "Should throw exception when pos + length >" + + "actual length + 1"); + return 1; + } catch (SerialException se) { + System.out.println("Test Passed"); + return 0; + } + } + + // Test getBinaryStream with pos is larger than the back end buffer length + // of the SerialBlob object + public static int testGetBinaryStreamPosGTLength() throws Exception { + SerialBlob sb = prepareSerialBlob(); + + try { + sb.getBinaryStream(byteArray.length + 1, 2); + errorMsgs.put("testGetBinaryStreamPosGTLength()", + "Should throw exception when pos > Blob's length"); + return 1; + } catch (SerialException se) { + System.out.println("Test Passed"); + return 0; + } + } + + public static void main(String[] args) throws Exception { + int totalNumber = 0; + int failedNumber = 0; + + failedNumber += testGetBinaryStream(); + totalNumber++; + + failedNumber += testGetBinaryStreamPosLT0(); + totalNumber++; + + failedNumber += testGetBinaryStreamPosEquals0(); + totalNumber++; + + failedNumber += testGetBinaryStreamLengthOutOfBounds(); + totalNumber++; + + failedNumber += testGetBinaryStreamPosGTLength(); + totalNumber++; + + System.out.println(); + System.out.println("Total tests " + totalNumber); + System.out.println("Passed: " + (totalNumber - failedNumber)); + System.out.println("Failed: " + failedNumber); + System.out.println(); + + if (failedNumber != 0) { + System.out.println("Failed details:"); + + Iterator> itr = errorMsgs.entrySet() + .iterator(); + + while (itr.hasNext()) { + Entry entry = itr.next(); + String testName = entry.getKey(); + String message = entry.getValue(); + + System.out.println(testName + ": " + message); + } + + throw new Exception( + "SerialBlob.getBinaryStream(long, long) test failed"); + } + } +} \ No newline at end of file