1 /* 2 * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * Portions Copyright (c) 2012 IBM Corporation 26 */ 27 28 import java.io.Reader; 29 import java.util.HashMap; 30 import java.util.Iterator; 31 import java.util.Map; 32 import java.util.Map.Entry; 33 34 import javax.sql.rowset.serial.SerialClob; 35 import javax.sql.rowset.serial.SerialException; 36 37 /** 38 * @test 39 * @bug 40 * @summary tests for getBinaryStream(long, long) on SerialBlob 41 */ 42 43 public class SerialClobGetCharacterStreamJJ { 44 static char[] charArray = "char array test".toCharArray(); 45 46 static Map<String, String> errorMsgs = new HashMap<String, String>(); 47 48 private static SerialClob prepareSerialClob() throws Exception { 49 return new SerialClob(charArray); 50 } 51 52 // Test the normal behavior of getCharacterStream(long, long) 53 public static int testGetCharacterStream() throws Exception { 54 SerialClob sc = prepareSerialClob(); 55 Reader is = sc.getCharacterStream(1, 3); 56 57 char[] expected = "cha".toCharArray(); 58 for (int i = 0; i < 3; i++) { 59 if (expected[i] != is.read()) { 60 errorMsgs.put("testGetCharacterStream()", "Unexpected byte"); 61 return 1; 62 } 63 } 64 65 if (is.read() != -1) { 66 errorMsgs 67 .put("testGetCharacterStream()", "More left in SerialClob"); 68 return 1; 69 } 70 System.out.println("Test Passed"); 71 return 0; 72 } 73 74 // Test getCharacterStream with pos is less than 0 75 public static int testGetCharacterStreamPosLT0() throws Exception { 76 SerialClob sc = prepareSerialClob(); 77 78 try { 79 sc.getCharacterStream(-1, 3); 80 errorMsgs.put("testGetCharacterStreamPosLT0()", 81 "Should throw exception when pos < 0"); 82 return 1; 83 } catch (SerialException se) { 84 System.out.println("Test Passed"); 85 return 0; 86 } 87 } 88 89 // Test getCharacterStream with pos is 0 90 public static int testGetCharacterStreamPosEquals0() throws Exception { 91 SerialClob sc = prepareSerialClob(); 92 93 try { 94 sc.getCharacterStream(0, 3); 95 errorMsgs.put("testGetCharacterStreamPosEquals0()", 96 "Should throw exception when pos == 0"); 97 return 1; 98 } catch (SerialException se) { 99 System.out.println("Test Passed"); 100 return 0; 101 } 102 } 103 104 // Test getCharacterStream with length is larger than the back end buffer 105 // length of the SerialClob object 106 public static int testGetCharacterStreamLengthOutOfBounds() 107 throws Exception { 108 SerialClob sc = prepareSerialClob(); 109 110 try { 111 sc.getCharacterStream(1, charArray.length + 1); 112 errorMsgs 113 .put("testGetCharacterStreamLengthOutOfBounds()", 114 "Should throw exception when pos + length > actual length + 1"); 115 return 1; 116 } catch (SerialException se) { 117 System.out.println("Test Passed"); 118 return 0; 119 } 120 } 121 122 public static int testGetCharacterStreamPosGTLength() throws Exception { 123 SerialClob sc = prepareSerialClob(); 124 125 try { 126 sc.getCharacterStream(charArray.length + 1, 2); 127 errorMsgs.put("testGetBinaryStreamPosGTLength()", 128 "Should throw exception when pos > Blob's length"); 129 return 1; 130 } catch (SerialException se) { 131 System.out.println("Test Passed"); 132 return 0; 133 } 134 } 135 136 public static void main(String[] args) throws Exception { 137 int totalNumber = 0; 138 int failedNumber = 0; 139 140 failedNumber += testGetCharacterStream(); 141 totalNumber++; 142 143 failedNumber += testGetCharacterStreamPosLT0(); 144 totalNumber++; 145 146 failedNumber += testGetCharacterStreamPosEquals0(); 147 totalNumber++; 148 149 failedNumber += testGetCharacterStreamLengthOutOfBounds(); 150 totalNumber++; 151 152 failedNumber += testGetCharacterStreamPosGTLength(); 153 totalNumber++; 154 155 System.out.println(); 156 System.out.println("Total tests " + totalNumber); 157 System.out.println("Passed: " + (totalNumber - failedNumber)); 158 System.out.println("Failed: " + failedNumber); 159 System.out.println(); 160 161 if (failedNumber != 0) { 162 System.out.println("Failed details:"); 163 164 Iterator<Entry<String, String>> itr = errorMsgs.entrySet() 165 .iterator(); 166 167 while (itr.hasNext()) { 168 Entry<String, String> entry = itr.next(); 169 String testName = entry.getKey(); 170 String message = entry.getValue(); 171 172 System.out.println(testName + ": " + message); 173 } 174 175 throw new Exception( 176 "SerialClob.getCharacterStream(long, long) test failed"); 177 } 178 } 179 }