/* * 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.Reader; 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 getBinaryStream(long, long) on SerialBlob */ public class SerialClobGetCharacterStreamJJ { static char[] charArray = "char array test".toCharArray(); static Map errorMsgs = new HashMap(); private static SerialClob prepareSerialClob() throws Exception { return new SerialClob(charArray); } // Test the normal behavior of getCharacterStream(long, long) public static int testGetCharacterStream() throws Exception { SerialClob sc = prepareSerialClob(); Reader is = sc.getCharacterStream(1, 3); char[] expected = "cha".toCharArray(); for (int i = 0; i < 3; i++) { if (expected[i] != is.read()) { errorMsgs.put("testGetCharacterStream()", "Unexpected byte"); return 1; } } if (is.read() != -1) { errorMsgs .put("testGetCharacterStream()", "More left in SerialClob"); return 1; } System.out.println("Test Passed"); return 0; } // Test getCharacterStream with pos is less than 0 public static int testGetCharacterStreamPosLT0() throws Exception { SerialClob sc = prepareSerialClob(); try { sc.getCharacterStream(-1, 3); errorMsgs.put("testGetCharacterStreamPosLT0()", "Should throw exception when pos < 0"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } // Test getCharacterStream with pos is 0 public static int testGetCharacterStreamPosEquals0() throws Exception { SerialClob sc = prepareSerialClob(); try { sc.getCharacterStream(0, 3); errorMsgs.put("testGetCharacterStreamPosEquals0()", "Should throw exception when pos == 0"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } // Test getCharacterStream with length is larger than the back end buffer // length of the SerialClob object public static int testGetCharacterStreamLengthOutOfBounds() throws Exception { SerialClob sc = prepareSerialClob(); try { sc.getCharacterStream(1, charArray.length + 1); errorMsgs .put("testGetCharacterStreamLengthOutOfBounds()", "Should throw exception when pos + length > actual length + 1"); return 1; } catch (SerialException se) { System.out.println("Test Passed"); return 0; } } public static int testGetCharacterStreamPosGTLength() throws Exception { SerialClob sc = prepareSerialClob(); try { sc.getCharacterStream(charArray.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 += testGetCharacterStream(); totalNumber++; failedNumber += testGetCharacterStreamPosLT0(); totalNumber++; failedNumber += testGetCharacterStreamPosEquals0(); totalNumber++; failedNumber += testGetCharacterStreamLengthOutOfBounds(); totalNumber++; failedNumber += testGetCharacterStreamPosGTLength(); 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.getCharacterStream(long, long) test failed"); } } }