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.util.HashMap; 29 import java.util.Iterator; 30 import java.util.Map; 31 import java.util.Map.Entry; 32 33 import javax.sql.rowset.serial.SerialBlob; 34 import javax.sql.rowset.serial.SerialException; 35 36 import java.io.ByteArrayInputStream; 37 import java.io.InputStream; 38 import java.io.OutputStream; 39 import java.sql.Blob; 40 import java.sql.SQLException; 41 42 /** 43 * @test 44 * @bug 45 * @summary tests for free() on SerialBlob 46 */ 47 48 public class SerialBlobFree { 49 static byte[] byteArray = new byte[] { 1, 2, 3, 4, 5 }; 50 51 static Map<String, String> errorMsgs = new HashMap<String, String>(); 52 53 private static SerialBlob prepareSerialBlob() throws Exception { 54 return new SerialBlob(byteArray); 55 } 56 57 public static int testGetBinaryStreamAfterFree() throws Exception { 58 SerialBlob sb = prepareSerialBlob(); 59 sb.free(); 60 61 // should throw SerialException after free() has been called. 62 try { 63 sb.getBinaryStream(); 64 errorMsgs.put("testGetBinaryStreamAfterFree()", 65 "operation on a freed SerialBlob should throw exception"); 66 return 1; 67 } catch (SerialException se) { 68 System.out.println("Test Passed"); 69 return 0; 70 } 71 } 72 73 public static int testGetBytesAfterFree() throws Exception { 74 SerialBlob sb = prepareSerialBlob(); 75 sb.free(); 76 77 // should throw SerialException after free() has been called. 78 try { 79 sb.getBytes(1, 3); 80 errorMsgs.put("testGetBytesAfterFree()", 81 "operation on a freed SerialBlob should throw exception"); 82 return 1; 83 } catch (SerialException se) { 84 System.out.println("Test Passed"); 85 return 0; 86 } 87 } 88 89 public static int testLengthAfterFree() throws Exception { 90 SerialBlob sb = prepareSerialBlob(); 91 sb.free(); 92 93 // should throw SerialException after free() has been called. 94 try { 95 sb.length(); 96 errorMsgs.put("testLengthAfterFree()", 97 "operation on a freed SerialBlob should throw exception"); 98 return 1; 99 } catch (SerialException se) { 100 System.out.println("Test Passed"); 101 return 0; 102 } 103 } 104 105 public static int testPositionBAJAfterFree() throws Exception { 106 SerialBlob sb = prepareSerialBlob(); 107 sb.free(); 108 109 // should throw SerialException after free() has been called. 110 try { 111 sb.position(new byte[] { 2, 3 }, 1); 112 errorMsgs.put("testPositionBAJAfterFree()", 113 "operation on a freed SerialBlob should throw exception"); 114 return 1; 115 } catch (SerialException se) { 116 System.out.println("Test Passed"); 117 return 0; 118 } 119 } 120 121 public static int testPositionBlobJAfterFree() throws Exception { 122 SerialBlob sb = prepareSerialBlob(); 123 sb.free(); 124 125 // should throw SerialException after free() has been called. 126 try { 127 MockBlob mBlob = new MockBlob(); 128 sb.position(mBlob, 1); 129 errorMsgs.put("testPositionBlobJAfterFree()", 130 "operation on a freed SerialBlob should throw exception"); 131 return 1; 132 } catch (SerialException se) { 133 System.out.println("Test Passed"); 134 return 0; 135 } 136 } 137 138 public static int testSetBytesJBAAfterFree() throws Exception { 139 SerialBlob sb = prepareSerialBlob(); 140 sb.free(); 141 142 // should throw SerialException after free() has been called. 143 try { 144 sb.setBytes(1, new byte[] { 2, 3 }); 145 errorMsgs.put("testSetBytesJBAAfterFree()", 146 "operation on a freed SerialBlob should throw exception"); 147 return 1; 148 } catch (SerialException se) { 149 System.out.println("Test Passed"); 150 return 0; 151 } 152 } 153 154 public static int testSetBytesJBAIIAfterFree() throws Exception { 155 SerialBlob sb = prepareSerialBlob(); 156 sb.free(); 157 158 // should throw SerialException after free() has been called. 159 try { 160 sb.setBytes(1, new byte[] { 2, 3 }, 0, 2); 161 errorMsgs.put("testSetBytesJBAIIAfterFree()", 162 "operation on a freed SerialBlob should throw exception"); 163 return 1; 164 } catch (SerialException se) { 165 System.out.println("Test Passed"); 166 return 0; 167 } 168 } 169 170 public static int testSetBinaryStreamJAfterFree() throws Exception { 171 SerialBlob sb = prepareSerialBlob(); 172 sb.free(); 173 174 // should throw SerialException after free() has been called. 175 try { 176 sb.setBinaryStream(1); 177 errorMsgs.put("testSetBinaryStreamJAfterFree()", 178 "operation on a freed SerialBlob should throw exception"); 179 return 1; 180 } catch (SerialException se) { 181 System.out.println("Test Passed"); 182 return 0; 183 } 184 } 185 186 public static int testTruncateAfterFree() throws Exception { 187 SerialBlob sb = prepareSerialBlob(); 188 sb.free(); 189 190 // should throw SerialException after free() has been called. 191 try { 192 sb.truncate(3); 193 errorMsgs.put("testTruncateAfterFree()", 194 "operation on a freed SerialBlob should throw exception"); 195 return 1; 196 } catch (SerialException se) { 197 System.out.println("Test Passed"); 198 return 0; 199 } 200 } 201 202 public static int testGetBinaryStreamJJAfterFree() throws Exception { 203 SerialBlob sb = prepareSerialBlob(); 204 sb.free(); 205 206 // should throw SerialException after free() has been called. 207 try { 208 sb.getBinaryStream(1, 3); 209 errorMsgs.put("testGetBinaryStreamJJAfterFree()", 210 "operation on a freed SerialBlob should throw exception"); 211 return 1; 212 } catch (SerialException se) { 213 System.out.println("Test Passed"); 214 return 0; 215 } 216 } 217 218 public static int testMultipleFree() throws Exception { 219 SerialBlob sb = prepareSerialBlob(); 220 try { 221 sb.free(); 222 223 // multiple calls are treated as no-op 224 sb.free(); 225 } catch (Exception e) { 226 errorMsgs.put("testMultipleFree()", 227 "multiple calls should be treated as no-op"); 228 return 1; 229 } 230 231 System.out.println("Test Passed"); 232 return 0; 233 234 } 235 236 public static void main(String[] args) throws Exception { 237 int totalNumber = 0; 238 int failedNumber = 0; 239 240 failedNumber += testGetBinaryStreamAfterFree(); 241 totalNumber++; 242 243 failedNumber += testGetBytesAfterFree(); 244 totalNumber++; 245 246 failedNumber += testLengthAfterFree(); 247 totalNumber++; 248 249 failedNumber += testPositionBAJAfterFree(); 250 totalNumber++; 251 252 failedNumber += testPositionBlobJAfterFree(); 253 totalNumber++; 254 255 failedNumber += testSetBytesJBAAfterFree(); 256 totalNumber++; 257 258 failedNumber += testSetBytesJBAIIAfterFree(); 259 totalNumber++; 260 261 failedNumber += testSetBinaryStreamJAfterFree(); 262 totalNumber++; 263 264 failedNumber += testTruncateAfterFree(); 265 totalNumber++; 266 267 failedNumber += testGetBinaryStreamJJAfterFree(); 268 totalNumber++; 269 270 failedNumber += testMultipleFree(); 271 totalNumber++; 272 273 System.out.println(); 274 System.out.println("Total tests " + totalNumber); 275 System.out.println("Passed: " + (totalNumber - failedNumber)); 276 System.out.println("Failed: " + failedNumber); 277 System.out.println(); 278 279 if (failedNumber != 0) { 280 System.out.println("Failed details:"); 281 282 Iterator<Entry<String, String>> itr = errorMsgs.entrySet() 283 .iterator(); 284 285 while (itr.hasNext()) { 286 Entry<String, String> entry = itr.next(); 287 String testName = entry.getKey(); 288 String message = entry.getValue(); 289 290 System.out.println(testName + ": " + message); 291 } 292 293 throw new Exception("SerialBlob.free() test failed"); 294 } 295 } 296 297 static class MockBlob implements Blob { 298 byte buff[] = { 3, 4, 5 }; 299 300 @Override 301 public long length() throws SQLException { 302 return buff.length; 303 } 304 305 @Override 306 public byte[] getBytes(long pos, int length) throws SQLException { 307 return buff; 308 } 309 310 @Override 311 public InputStream getBinaryStream() throws SQLException { 312 return new ByteArrayInputStream(buff); 313 } 314 315 @Override 316 public long position(byte[] pattern, long start) throws SQLException { 317 return 0; 318 } 319 320 @Override 321 public long position(Blob pattern, long start) throws SQLException { 322 return 0; 323 } 324 325 @Override 326 public int setBytes(long pos, byte[] bytes) throws SQLException { 327 return 0; 328 } 329 330 @Override 331 public int setBytes(long pos, byte[] bytes, int offset, int len) 332 throws SQLException { 333 return 0; 334 } 335 336 @Override 337 public OutputStream setBinaryStream(long pos) throws SQLException { 338 return null; 339 } 340 341 @Override 342 public void truncate(long len) throws SQLException { 343 } 344 345 @Override 346 public void free() throws SQLException { 347 } 348 349 @Override 350 public InputStream getBinaryStream(long pos, long length) 351 throws SQLException { 352 return null; 353 } 354 355 } 356 357 }