/* * 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.io.OutputStream; import java.io.Reader; import java.io.Writer; import java.sql.Clob; import java.sql.SQLException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import javax.sql.rowset.serial.SerialClob; import javax.sql.rowset.serial.SerialException; /** * @test * @bug * @summary tests for free() on SerialBlob */ public class SerialClobFree { static char[] charArray = "char array test".toCharArray(); static Map errorMsgs = new HashMap(); private static SerialClob prepareSerialClob() throws Exception { return new SerialClob(charArray); } public static int testGetCharacterStreamAfterFree() throws Exception { SerialClob sc = prepareSerialClob(); sc.free(); // should throw SerialException after free() has been called. try { sc.getCharacterStream(); errorMsgs.put("testGetCharacterStreamAfterFree()", "operation on a freed SerialClob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testGetAsciiStreamAfterFree() throws Exception { SerialClob sc = prepareSerialClob(); sc.free(); // should throw SerialException after free() has been called. try { sc.getAsciiStream(); errorMsgs.put("testGetAsciiStreamAfterFree()", "operation on a freed SerialClob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testGetSubStringJIAfterFree() throws Exception { SerialClob sc = prepareSerialClob(); sc.free(); // should throw SerialException after free() has been called. try { sc.getSubString(1, 3); errorMsgs.put("testGetSubStringJIAfterFree()", "operation on a freed SerialClob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testLengthAfterFree() throws Exception { SerialClob sc = prepareSerialClob(); sc.free(); // should throw SerialException after free() has been called. try { sc.length(); errorMsgs.put("testLengthAfterFree()", "operation on a freed SerialClob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testPositionStringJAfterFree() throws Exception { SerialClob sc = prepareSerialClob(); sc.free(); // should throw SerialException after free() has been called. try { sc.position("cha", 1); errorMsgs.put("testPositionStringJAfterFree()", "operation on a freed SerialClob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testPositionClobJAfterFree() throws Exception { SerialClob sc = prepareSerialClob(); sc.free(); // should throw SerialException after free() has been called. try { MockClob mClob = new MockClob(); sc.position(mClob, 1); errorMsgs.put("testPositionClobJAfterFree()", "operation on a freed SerialClob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testSetStringJStringAfterFree() throws Exception { SerialClob sc = prepareSerialClob(); sc.free(); // should throw SerialException after free() has been called. try { sc.setString(1, "cha"); errorMsgs.put("testSetStringJStringAfterFree()", "operation on a freed SerialClob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testSetStringJStringIIAfterFree() throws Exception { SerialClob sc = prepareSerialClob(); sc.free(); // should throw SerialException after free() has been called. try { sc.setString(1, "char", 0, 3); errorMsgs.put("testSetStringJStringIIAfterFree()", "operation on a freed SerialClob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testSetAsciiStreamJAfterFree() throws Exception { SerialClob sc = prepareSerialClob(); sc.free(); // should throw SerialException after free() has been called. try { sc.setAsciiStream(1); errorMsgs.put("testSetAsciiStreamJAfterFree()", "operation on a freed SerialClob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testSetCharacterStreamJAfterFree() throws Exception { SerialClob sc = prepareSerialClob(); sc.free(); // should throw SerialException after free() has been called. try { sc.setCharacterStream(1); errorMsgs.put("testSetCharacterStreamJAfterFree()", "operation on a freed SerialClob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testTruncateAfterFree() throws Exception { SerialClob sc = prepareSerialClob(); sc.free(); // should throw SerialException after free() has been called. try { sc.truncate(3); errorMsgs.put("testTruncateAfterFree()", "operation on a freed SerialClob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testGetCharacterStreamJJAfterFree() throws Exception { SerialClob sc = prepareSerialClob(); sc.free(); // should throw SerialException after free() has been called. try { sc.getCharacterStream(1, 3); errorMsgs.put("testGetCharacterStreamJJAfterFree()", "operation on a freed SerialClob should throw exception"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testMultipleFree() throws Exception { SerialClob sc = prepareSerialClob(); try { sc.free(); // multiple calls are treated as no-op sc.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 += testGetCharacterStreamAfterFree(); totalNumber++; failedNumber += testGetAsciiStreamAfterFree(); totalNumber++; failedNumber += testGetSubStringJIAfterFree(); totalNumber++; failedNumber += testLengthAfterFree(); totalNumber++; failedNumber += testPositionStringJAfterFree(); totalNumber++; failedNumber += testPositionClobJAfterFree(); totalNumber++; failedNumber += testSetStringJStringAfterFree(); totalNumber++; failedNumber += testSetStringJStringIIAfterFree(); totalNumber++; failedNumber += testSetAsciiStreamJAfterFree(); totalNumber++; failedNumber += testSetCharacterStreamJAfterFree(); totalNumber++; failedNumber += testTruncateAfterFree(); totalNumber++; failedNumber += testGetCharacterStreamJJAfterFree(); 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("SerialClob.free() test failed"); } } static class MockClob implements Clob { @Override public long length() throws SQLException { // TODO Auto-generated method stub return 0; } @Override public String getSubString(long pos, int length) throws SQLException { // TODO Auto-generated method stub return null; } @Override public Reader getCharacterStream() throws SQLException { // TODO Auto-generated method stub return null; } @Override public InputStream getAsciiStream() throws SQLException { // TODO Auto-generated method stub return null; } @Override public long position(String searchstr, long start) throws SQLException { // TODO Auto-generated method stub return 0; } @Override public long position(Clob searchstr, long start) throws SQLException { // TODO Auto-generated method stub return 0; } @Override public int setString(long pos, String str) throws SQLException { // TODO Auto-generated method stub return 0; } @Override public int setString(long pos, String str, int offset, int len) throws SQLException { // TODO Auto-generated method stub return 0; } @Override public OutputStream setAsciiStream(long pos) throws SQLException { // TODO Auto-generated method stub return null; } @Override public Writer setCharacterStream(long pos) throws SQLException { // TODO Auto-generated method stub return null; } @Override public void truncate(long len) throws SQLException { // TODO Auto-generated method stub } @Override public void free() throws SQLException { // TODO Auto-generated method stub } @Override public Reader getCharacterStream(long pos, long length) throws SQLException { // TODO Auto-generated method stub return null; } } }