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.InputStream; 29 import java.io.OutputStream; 30 import java.io.Reader; 31 import java.io.Writer; 32 import java.sql.Clob; 33 import java.sql.SQLException; 34 import java.util.HashMap; 35 import java.util.Iterator; 36 import java.util.Map; 37 import java.util.Map.Entry; 38 39 import javax.sql.rowset.serial.SerialClob; 40 import javax.sql.rowset.serial.SerialException; 41 42 /** 43 * @test 44 * @bug 45 * @summary tests for free() on SerialBlob 46 */ 47 48 public class SerialClobFree { 49 static char[] charArray = "char array test".toCharArray(); 50 51 static Map<String, String> errorMsgs = new HashMap<String, String>(); 52 53 private static SerialClob prepareSerialClob() throws Exception { 54 return new SerialClob(charArray); 55 } 56 57 public static int testGetCharacterStreamAfterFree() throws Exception { 58 SerialClob sc = prepareSerialClob(); 59 sc.free(); 60 61 // should throw SerialException after free() has been called. 62 try { 63 sc.getCharacterStream(); 64 errorMsgs.put("testGetCharacterStreamAfterFree()", 65 "operation on a freed SerialClob 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 testGetAsciiStreamAfterFree() throws Exception { 74 SerialClob sc = prepareSerialClob(); 75 sc.free(); 76 77 // should throw SerialException after free() has been called. 78 try { 79 sc.getAsciiStream(); 80 errorMsgs.put("testGetAsciiStreamAfterFree()", 81 "operation on a freed SerialClob 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 testGetSubStringJIAfterFree() throws Exception { 90 SerialClob sc = prepareSerialClob(); 91 sc.free(); 92 93 // should throw SerialException after free() has been called. 94 try { 95 sc.getSubString(1, 3); 96 errorMsgs.put("testGetSubStringJIAfterFree()", 97 "operation on a freed SerialClob 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 testLengthAfterFree() throws Exception { 106 SerialClob sc = prepareSerialClob(); 107 sc.free(); 108 109 // should throw SerialException after free() has been called. 110 try { 111 sc.length(); 112 errorMsgs.put("testLengthAfterFree()", 113 "operation on a freed SerialClob 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 testPositionStringJAfterFree() throws Exception { 122 SerialClob sc = prepareSerialClob(); 123 sc.free(); 124 125 // should throw SerialException after free() has been called. 126 try { 127 sc.position("cha", 1); 128 errorMsgs.put("testPositionStringJAfterFree()", 129 "operation on a freed SerialClob should throw exception"); 130 return 1; 131 } catch (SerialException se) { 132 System.out.println("Test Passed"); 133 return 0; 134 } 135 } 136 137 public static int testPositionClobJAfterFree() throws Exception { 138 SerialClob sc = prepareSerialClob(); 139 sc.free(); 140 141 // should throw SerialException after free() has been called. 142 try { 143 MockClob mClob = new MockClob(); 144 sc.position(mClob, 1); 145 errorMsgs.put("testPositionClobJAfterFree()", 146 "operation on a freed SerialClob 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 testSetStringJStringAfterFree() throws Exception { 155 SerialClob sc = prepareSerialClob(); 156 sc.free(); 157 158 // should throw SerialException after free() has been called. 159 try { 160 sc.setString(1, "cha"); 161 errorMsgs.put("testSetStringJStringAfterFree()", 162 "operation on a freed SerialClob 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 testSetStringJStringIIAfterFree() throws Exception { 171 SerialClob sc = prepareSerialClob(); 172 sc.free(); 173 174 // should throw SerialException after free() has been called. 175 try { 176 sc.setString(1, "char", 0, 3); 177 errorMsgs.put("testSetStringJStringIIAfterFree()", 178 "operation on a freed SerialClob 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 testSetAsciiStreamJAfterFree() throws Exception { 187 SerialClob sc = prepareSerialClob(); 188 sc.free(); 189 190 // should throw SerialException after free() has been called. 191 try { 192 sc.setAsciiStream(1); 193 errorMsgs.put("testSetAsciiStreamJAfterFree()", 194 "operation on a freed SerialClob 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 testSetCharacterStreamJAfterFree() throws Exception { 203 SerialClob sc = prepareSerialClob(); 204 sc.free(); 205 206 // should throw SerialException after free() has been called. 207 try { 208 sc.setCharacterStream(1); 209 errorMsgs.put("testSetCharacterStreamJAfterFree()", 210 "operation on a freed SerialClob 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 testTruncateAfterFree() throws Exception { 219 SerialClob sc = prepareSerialClob(); 220 sc.free(); 221 222 // should throw SerialException after free() has been called. 223 try { 224 sc.truncate(3); 225 errorMsgs.put("testTruncateAfterFree()", 226 "operation on a freed SerialClob should throw exception"); 227 return 1; 228 } catch (SerialException se) { 229 System.out.println("Test Passed"); 230 return 0; 231 } 232 } 233 234 public static int testGetCharacterStreamJJAfterFree() throws Exception { 235 SerialClob sc = prepareSerialClob(); 236 sc.free(); 237 238 // should throw SerialException after free() has been called. 239 try { 240 sc.getCharacterStream(1, 3); 241 errorMsgs.put("testGetCharacterStreamJJAfterFree()", 242 "operation on a freed SerialClob should throw exception"); 243 return 1; 244 } catch (SerialException se) { 245 System.out.println("Test Passed"); 246 return 0; 247 } 248 } 249 250 public static int testMultipleFree() throws Exception { 251 SerialClob sc = prepareSerialClob(); 252 try { 253 sc.free(); 254 255 // multiple calls are treated as no-op 256 sc.free(); 257 } catch (Exception e) { 258 errorMsgs.put("testMultipleFree()", 259 "multiple calls should be treated as no-op"); 260 return 1; 261 } 262 263 System.out.println("Test Passed"); 264 return 0; 265 266 } 267 268 public static void main(String[] args) throws Exception { 269 int totalNumber = 0; 270 int failedNumber = 0; 271 272 failedNumber += testGetCharacterStreamAfterFree(); 273 totalNumber++; 274 275 failedNumber += testGetAsciiStreamAfterFree(); 276 totalNumber++; 277 278 failedNumber += testGetSubStringJIAfterFree(); 279 totalNumber++; 280 281 failedNumber += testLengthAfterFree(); 282 totalNumber++; 283 284 failedNumber += testPositionStringJAfterFree(); 285 totalNumber++; 286 287 failedNumber += testPositionClobJAfterFree(); 288 totalNumber++; 289 290 failedNumber += testSetStringJStringAfterFree(); 291 totalNumber++; 292 293 failedNumber += testSetStringJStringIIAfterFree(); 294 totalNumber++; 295 296 failedNumber += testSetAsciiStreamJAfterFree(); 297 totalNumber++; 298 299 failedNumber += testSetCharacterStreamJAfterFree(); 300 totalNumber++; 301 302 failedNumber += testTruncateAfterFree(); 303 totalNumber++; 304 305 failedNumber += testGetCharacterStreamJJAfterFree(); 306 totalNumber++; 307 308 failedNumber += testMultipleFree(); 309 totalNumber++; 310 311 System.out.println(); 312 System.out.println("Total tests " + totalNumber); 313 System.out.println("Passed: " + (totalNumber - failedNumber)); 314 System.out.println("Failed: " + failedNumber); 315 System.out.println(); 316 317 if (failedNumber != 0) { 318 System.out.println("Failed details:"); 319 320 Iterator<Entry<String, String>> itr = errorMsgs.entrySet() 321 .iterator(); 322 323 while (itr.hasNext()) { 324 Entry<String, String> entry = itr.next(); 325 String testName = entry.getKey(); 326 String message = entry.getValue(); 327 328 System.out.println(testName + ": " + message); 329 } 330 331 throw new Exception("SerialClob.free() test failed"); 332 } 333 } 334 335 static class MockClob implements Clob { 336 337 @Override 338 public long length() throws SQLException { 339 // TODO Auto-generated method stub 340 return 0; 341 } 342 343 @Override 344 public String getSubString(long pos, int length) throws SQLException { 345 // TODO Auto-generated method stub 346 return null; 347 } 348 349 @Override 350 public Reader getCharacterStream() throws SQLException { 351 // TODO Auto-generated method stub 352 return null; 353 } 354 355 @Override 356 public InputStream getAsciiStream() throws SQLException { 357 // TODO Auto-generated method stub 358 return null; 359 } 360 361 @Override 362 public long position(String searchstr, long start) throws SQLException { 363 // TODO Auto-generated method stub 364 return 0; 365 } 366 367 @Override 368 public long position(Clob searchstr, long start) throws SQLException { 369 // TODO Auto-generated method stub 370 return 0; 371 } 372 373 @Override 374 public int setString(long pos, String str) throws SQLException { 375 // TODO Auto-generated method stub 376 return 0; 377 } 378 379 @Override 380 public int setString(long pos, String str, int offset, int len) 381 throws SQLException { 382 // TODO Auto-generated method stub 383 return 0; 384 } 385 386 @Override 387 public OutputStream setAsciiStream(long pos) throws SQLException { 388 // TODO Auto-generated method stub 389 return null; 390 } 391 392 @Override 393 public Writer setCharacterStream(long pos) throws SQLException { 394 // TODO Auto-generated method stub 395 return null; 396 } 397 398 @Override 399 public void truncate(long len) throws SQLException { 400 // TODO Auto-generated method stub 401 402 } 403 404 @Override 405 public void free() throws SQLException { 406 // TODO Auto-generated method stub 407 408 } 409 410 @Override 411 public Reader getCharacterStream(long pos, long length) 412 throws SQLException { 413 // TODO Auto-generated method stub 414 return null; 415 } 416 417 } 418 }