--- /dev/null 2012-06-18 09:42:28.035981046 +0800 +++ new/test/javax/sql/rowset/serial/SerialBlob/FreeAndGetBinaryStream.java 2012-06-25 15:45:04.316365927 +0800 @@ -0,0 +1,132 @@ +/* + * 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 javax.sql.rowset.serial.SerialBlob; +import javax.sql.rowset.serial.SerialException; + +/** + * @test + * @bug + * @summary tests for free() and getBinaryStream on SerialBlob + */ + +public class FreeAndGetBinaryStream { + static byte[] byteArray = new byte[] {1, 2, 3, 4, 5}; + + private static SerialBlob prepareSerialBlob() throws Exception { + return new SerialBlob(byteArray); + } + + public static void testFree() throws Exception { + SerialBlob sb = prepareSerialBlob(); + sb.free(); + // test a API, should throw exception after free() + Exception exception = null; + try { + sb.getBinaryStream(); + } catch (SerialException se) { + exception = se; + } + if (exception == null) { + throw new Exception("operation on a freed SerialBlob should throw exception"); + } + // multiple calls are treated as no-op + sb.free(); + } + + public static void testGetBinaryStreamLLContent() throws Exception { + SerialBlob sb = prepareSerialBlob(); + InputStream is = sb.getBinaryStream(1, 3); + + byte[] expected = new byte[] {1, 2, 3}; + for (int idx=0; idx<3; idx++) { + if (expected[idx] != is.read()) { + throw new Exception("Unexpected byte"); + } + } + + if (is.read()!=-1) { + throw new Exception("More left in SerialBlob"); + } + } + + public static void testGetBinaryStreamLLEdgyCase() throws Exception { + SerialBlob sb = prepareSerialBlob(); + Exception exception = null; + + try { + sb.getBinaryStream(-1, 3); + } catch (SerialException se) { + exception = se; + } + if (exception==null) { + throw new Exception("Should throw exception when pos < 0"); + } + + sb = prepareSerialBlob(); + exception = null; + try { + sb.getBinaryStream(0, 3); + } catch (SerialException se) { + exception = se; + } + if (exception==null) { + throw new Exception("Should throw exception when pos == 0"); + } + + sb = prepareSerialBlob(); + exception = null; + try { + sb.getBinaryStream(1, byteArray.length+1); + } catch (SerialException se) { + exception = se; + } + if (exception==null) { + throw new Exception("Should throw exception when pos + length > actual length + 1"); + } + + sb = prepareSerialBlob(); + exception = null; + try { + sb.getBinaryStream(byteArray.length+1, 2); + } catch (SerialException se) { + exception = se; + } + if (exception==null) { + throw new Exception("Should throw exception when pos > length"); + } + } + + public static void main(String[] args) throws Exception { + testGetBinaryStreamLLEdgyCase(); + testGetBinaryStreamLLContent(); + testFree(); + } + +}