/* * 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.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; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.OutputStream; import java.sql.Blob; import java.sql.SQLException; /** * @test * @bug * @summary tests for free() on SerialBlob */ public class SerialBlobFree { 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); } public static int testGetBinaryStreamAfterFree() throws Exception { SerialBlob sb = prepareSerialBlob(); sb.free(); // should throw SerialException after free() has been called. try { sb.getBinaryStream(); errorMsgs.put("testGetBinaryStreamAfterFree()", "operation on a freed SerialBlob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testGetBytesAfterFree() throws Exception { SerialBlob sb = prepareSerialBlob(); sb.free(); // should throw SerialException after free() has been called. try { sb.getBytes(1, 3); errorMsgs.put("testGetBytesAfterFree()", "operation on a freed SerialBlob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testLengthAfterFree() throws Exception { SerialBlob sb = prepareSerialBlob(); sb.free(); // should throw SerialException after free() has been called. try { sb.length(); errorMsgs.put("testLengthAfterFree()", "operation on a freed SerialBlob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testPositionBAJAfterFree() throws Exception { SerialBlob sb = prepareSerialBlob(); sb.free(); // should throw SerialException after free() has been called. try { sb.position(new byte[] { 2, 3 }, 1); errorMsgs.put("testPositionBAJAfterFree()", "operation on a freed SerialBlob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testPositionBlobJAfterFree() throws Exception { SerialBlob sb = prepareSerialBlob(); sb.free(); // should throw SerialException after free() has been called. try { MockBlob mBlob = new MockBlob(); sb.position(mBlob, 1); errorMsgs.put("testPositionBlobJAfterFree()", "operation on a freed SerialBlob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testSetBytesJBAAfterFree() throws Exception { SerialBlob sb = prepareSerialBlob(); sb.free(); // should throw SerialException after free() has been called. try { sb.setBytes(1, new byte[] { 2, 3 }); errorMsgs.put("testSetBytesJBAAfterFree()", "operation on a freed SerialBlob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testSetBytesJBAIIAfterFree() throws Exception { SerialBlob sb = prepareSerialBlob(); sb.free(); // should throw SerialException after free() has been called. try { sb.setBytes(1, new byte[] { 2, 3 }, 0, 2); errorMsgs.put("testSetBytesJBAIIAfterFree()", "operation on a freed SerialBlob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testSetBinaryStreamJAfterFree() throws Exception { SerialBlob sb = prepareSerialBlob(); sb.free(); // should throw SerialException after free() has been called. try { sb.setBinaryStream(1); errorMsgs.put("testSetBinaryStreamJAfterFree()", "operation on a freed SerialBlob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testTruncateAfterFree() throws Exception { SerialBlob sb = prepareSerialBlob(); sb.free(); // should throw SerialException after free() has been called. try { sb.truncate(3); errorMsgs.put("testTruncateAfterFree()", "operation on a freed SerialBlob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testGetBinaryStreamJJAfterFree() throws Exception { SerialBlob sb = prepareSerialBlob(); sb.free(); // should throw SerialException after free() has been called. try { sb.getBinaryStream(1, 3); errorMsgs.put("testGetBinaryStreamJJAfterFree()", "operation on a freed SerialBlob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testMultipleFree() throws Exception { SerialBlob sb = prepareSerialBlob(); try { sb.free(); // multiple calls are treated as no-op sb.free(); } catch (Exception e) { errorMsgs.put("testMultipleFree()", "multiple calls should be treated as no-op"); return 1; } System.out.println("Test Passed"); return 0; } public static void main(String[] args) throws Exception { int totalNumber = 0; int failedNumber = 0; failedNumber += testGetBinaryStreamAfterFree(); totalNumber++; failedNumber += testGetBytesAfterFree(); totalNumber++; failedNumber += testLengthAfterFree(); totalNumber++; failedNumber += testPositionBAJAfterFree(); totalNumber++; failedNumber += testPositionBlobJAfterFree(); totalNumber++; failedNumber += testSetBytesJBAAfterFree(); totalNumber++; failedNumber += testSetBytesJBAIIAfterFree(); totalNumber++; failedNumber += testSetBinaryStreamJAfterFree(); totalNumber++; failedNumber += testTruncateAfterFree(); totalNumber++; failedNumber += testGetBinaryStreamJJAfterFree(); totalNumber++; failedNumber += testMultipleFree(); 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.free() test failed"); } } static class MockBlob implements Blob { byte buff[] = { 3, 4, 5 }; @Override public long length() throws SQLException { return buff.length; } @Override public byte[] getBytes(long pos, int length) throws SQLException { return buff; } @Override public InputStream getBinaryStream() throws SQLException { return new ByteArrayInputStream(buff); } @Override public long position(byte[] pattern, long start) throws SQLException { return 0; } @Override public long position(Blob pattern, long start) throws SQLException { return 0; } @Override public int setBytes(long pos, byte[] bytes) throws SQLException { return 0; } @Override public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException { return 0; } @Override public OutputStream setBinaryStream(long pos) throws SQLException { return null; } @Override public void truncate(long len) throws SQLException { } @Override public void free() throws SQLException { } @Override public InputStream getBinaryStream(long pos, long length) throws SQLException { return null; } } }