1 /* 2 * Copyright (c) 2014, 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 package test.rowset.serial; 24 25 import java.io.InputStream; 26 import java.io.OutputStream; 27 import java.io.Reader; 28 import java.io.Writer; 29 import javax.sql.rowset.serial.SerialClob; 30 import javax.sql.rowset.serial.SerialException; 31 import static org.testng.Assert.*; 32 import org.testng.annotations.Test; 33 import util.BaseTest; 34 import util.StubClob; 35 36 public class SerialClobTests extends BaseTest { 37 38 // char[] used to populate SerialClob 39 private final char[] chars; 40 41 public SerialClobTests() { 42 this.chars = new char[]{'h', 'e', 'l', 'l', 'o', ' ', 'w', 43 'o', 'r', 'l', 'd'}; 44 } 45 46 /* 47 * Validate calling free() does not throw an Exception 48 */ 49 @Test 50 public void test() throws Exception { 51 SerialClob sc = new SerialClob(new StubClob()); 52 sc.free(); 53 } 54 55 /* 56 * Validate calling getCharacterStream() after calling free() throws an 57 * SerialException 58 */ 59 @Test(expectedExceptions = SerialException.class) 60 public void test01() throws Exception { 61 SerialClob sc = new SerialClob(new StubClob()); 62 sc.free(); 63 sc.getCharacterStream(); 64 } 65 66 /* 67 * Validate calling getCharacterStream() after calling free() throws an 68 * SerialException 69 */ 70 @Test(expectedExceptions = SerialException.class) 71 public void test02() throws Exception { 72 SerialClob sc = new SerialClob(chars); 73 sc.free(); 74 sc.getCharacterStream(); 75 } 76 77 /* 78 * Validate calling getCharacterStream() after calling free() throws an 79 * SerialException 80 */ 81 @Test(expectedExceptions = SerialException.class) 82 public void test03() throws Exception { 83 SerialClob sc = new SerialClob(new StubClob()); 84 sc.free(); 85 sc.getCharacterStream(1, 5); 86 } 87 88 /* 89 * Validate calling getSubString() after calling free() throws an 90 * SerialException 91 */ 92 @Test(expectedExceptions = SerialException.class) 93 public void test04() throws Exception { 94 SerialClob sc = new SerialClob(new StubClob()); 95 sc.free(); 96 sc.getSubString(1, 1); 97 } 98 99 /* 100 * Validate calling truncate() after calling free() throws an 101 * SerialException 102 */ 103 @Test(expectedExceptions = SerialException.class) 104 public void test05() throws Exception { 105 SerialClob sc = new SerialClob(new StubClob()); 106 sc.free(); 107 sc.truncate(1); 108 } 109 110 /* 111 * Validate calling getAsciiStream() after calling free() throws an 112 * SerialException 113 */ 114 @Test(expectedExceptions = SerialException.class) 115 public void test06() throws Exception { 116 SerialClob sc = new SerialClob(new StubClob()); 117 sc.free(); 118 sc.getAsciiStream(); 119 } 120 121 /* 122 * Validate calling length() after calling free() throws an SerialException 123 */ 124 @Test(expectedExceptions = SerialException.class) 125 public void test07() throws Exception { 126 SerialClob sc = new SerialClob(new StubClob()); 127 sc.free(); 128 sc.length(); 129 } 130 131 /* 132 * Validate calling position() after calling free() throws an 133 * SerialException 134 */ 135 @Test(expectedExceptions = SerialException.class) 136 public void test08() throws Exception { 137 SerialClob sc = new SerialClob(new StubClob()); 138 sc.free(); 139 sc.position("hello", 1); 140 } 141 142 /* 143 * Validate calling position() after calling free() throws an 144 * SerialException 145 */ 146 @Test(expectedExceptions = SerialException.class) 147 public void test09() throws Exception { 148 SerialClob sc = new SerialClob(new StubClob()); 149 sc.free(); 150 sc.position(new StubClob(), 1); 151 } 152 153 /* 154 * Validate calling setAsciiStream() after calling free() throws an 155 * SerialException 156 */ 157 @Test(expectedExceptions = SerialException.class) 158 public void test10() throws Exception { 159 SerialClob sc = new SerialClob(new StubClob()); 160 sc.free(); 161 sc.setAsciiStream(5); 162 } 163 164 /* 165 * Validate calling setCharacterStream() after calling free() throws an 166 * SerialException 167 */ 168 @Test(expectedExceptions = SerialException.class) 169 public void test11() throws Exception { 170 SerialClob sc = new SerialClob(new StubClob()); 171 sc.free(); 172 sc.setCharacterStream(5); 173 } 174 175 /* 176 * Validate calling setString() after calling free() throws an 177 * SerialException 178 */ 179 @Test(expectedExceptions = SerialException.class) 180 public void test12() throws Exception { 181 SerialClob sc = new SerialClob(new StubClob()); 182 sc.free(); 183 sc.setString(1, "hello"); 184 } 185 186 /* 187 * Validate calling setString() after calling free() throws an 188 * SerialException 189 */ 190 @Test(expectedExceptions = SerialException.class) 191 public void test13() throws Exception { 192 SerialClob sc = new SerialClob(new StubClob()); 193 sc.free(); 194 sc.setString(1, "hello", 0, 5); 195 } 196 197 /* 198 * Test that SerialException is thrown if pos < 0 on a call to 199 * getCharacterStream 200 */ 201 @Test(expectedExceptions = SerialException.class) 202 public void test14() throws Exception { 203 SerialClob sc = new SerialClob(chars); 204 sc.getCharacterStream(-1, 5); 205 } 206 207 /* 208 * Test that SerialException is thrown if pos = 0 on a call to 209 * getCharacterStream 210 */ 211 @Test(expectedExceptions = SerialException.class) 212 public void test15() throws Exception { 213 SerialClob sc = new SerialClob(chars); 214 sc.getCharacterStream(0, 5); 215 } 216 217 /* 218 * Test that SerialException is thrown if pos = 0 on a call to 219 * getCharacterStream 220 */ 221 @Test(expectedExceptions = SerialException.class) 222 public void test16() throws Exception { 223 SerialClob sc = new SerialClob(chars); 224 sc.getCharacterStream(1, 100); 225 } 226 227 /* 228 * Test that SerialException is thrown if length = 0 on a call to 229 * getCharacterStream 230 */ 231 @Test(expectedExceptions = SerialException.class) 232 public void test17() throws Exception { 233 SerialClob sc = new SerialClob(chars); 234 sc.getCharacterStream(1, 0); 235 } 236 237 /* 238 * Test that SerialException is thrown if pos > length on a call to 239 * getCharacterStream 240 */ 241 @Test(expectedExceptions = SerialException.class) 242 public void test18() throws Exception { 243 SerialClob sc = new SerialClob(chars); 244 sc.getCharacterStream(100, 5); 245 } 246 247 /* 248 * Clone a SerialClob and check that it is equal to itself 249 */ 250 @Test 251 public void test19() throws Exception { 252 SerialClob sc = new SerialClob(chars); 253 SerialClob sc1 = (SerialClob) sc.clone(); 254 assertTrue(sc.equals(sc1), "SerialClobs not equal"); 255 } 256 257 /* 258 * Validate that a getAsciiStream() returns an InputStream when a Clob is 259 * used to create the SerialClob 260 */ 261 @Test 262 public void test20() throws Exception { 263 SerialClob sc = new SerialClob(new StubClob()); 264 InputStream is = sc.getAsciiStream(); 265 assertTrue(is != null); 266 } 267 268 /* 269 * Validate that a getCharacterStream() returns an Reader when a Clob is 270 * used to create the SerialClob 271 */ 272 @Test 273 public void test21() throws Exception { 274 SerialClob sc = new SerialClob(new StubClob()); 275 Reader is = sc.getCharacterStream(); 276 assertTrue(is != null); 277 } 278 279 /* 280 * Validate that a getCharacterStream() returns an Reader when a char[] is 281 * used to create the SerialClob 282 */ 283 @Test 284 public void test22() throws Exception { 285 SerialClob sc = new SerialClob(chars); 286 Reader is = sc.getCharacterStream(); 287 assertTrue(is != null); 288 } 289 290 /* 291 * Validate that a getSubString() returns the correct value when a char[] is 292 * used to create the SerialClob 293 */ 294 @Test 295 public void test23() throws Exception { 296 SerialClob sc = new SerialClob(chars); 297 String expected = "world"; 298 assertEquals(expected, sc.getSubString(7, 5)); 299 } 300 301 /* 302 * Validate that a getSubString() returns the correct value when a Clob is 303 * used to create the SerialClob 304 */ 305 @Test 306 public void test24() throws Exception { 307 SerialClob sc = new SerialClob(new StubClob()); 308 String expected = "test string"; 309 assertEquals(expected, sc.getSubString(5, 11)); 310 } 311 312 /* 313 * Validate that position() returns the correct value when a Clob is used to 314 * create the SerialClob 315 */ 316 @Test 317 public void test25() throws Exception { 318 long expectedPos = 5; 319 SerialClob sc = new SerialClob(new StubClob()); 320 String expected = "test string"; 321 long pos = sc.position(expected, 1); 322 assertEquals(expectedPos, pos); 323 } 324 325 /* 326 * Validate that position returned is -1 when an the search string is not 327 * part of the SerialClob 328 */ 329 @Test 330 public void test26() throws Exception { 331 long expectedPos = -1; 332 SerialClob sc = new SerialClob(chars); 333 String expected = "test string"; 334 long pos = sc.position(expected, 1); 335 assertEquals(expectedPos, pos); 336 } 337 338 /* 339 * Validate that position() returned is -1 when an the search string is not 340 * part of the SerialClob 341 */ 342 @Test 343 public void test27() throws Exception { 344 long expectedPos = -1; 345 SerialClob sc = new SerialClob(new StubClob()); 346 String expected = "I am Batman"; 347 long pos = sc.position(expected, 2); 348 assertEquals(expectedPos, pos); 349 } 350 351 /* 352 * Validate that position() returns the correct value when a char[] is used 353 * to create the SerialClob 354 */ 355 @Test 356 public void test28() throws Exception { 357 long expectedPos = 2; 358 SerialClob sc = new SerialClob(chars); 359 String expected = "ello"; 360 long pos = sc.position(expected, 1); 361 assertEquals(expectedPos, pos); 362 } 363 364 /* 365 * Validate that position() returns the correct value when a SerialClob is 366 * used for the search argument 367 */ 368 @Test 369 public void test29() throws Exception { 370 long expectedPos = 21; 371 String expected = "Batman"; 372 String buf = "I am Joker, not the Batman, hahaha"; 373 SerialClob sc = new SerialClob(expected.toCharArray()); 374 SerialClob sc1 = new SerialClob(buf.toCharArray()); 375 long pos = sc1.position(sc, 1); 376 assertEquals(expectedPos, pos); 377 } 378 379 /* 380 * Validate that position() returns the correct value when a SerialClob is 381 * used for the search argument 382 */ 383 @Test 384 public void test30() throws Exception { 385 long expectedPos = 17; 386 String expected = "012"; 387 SerialClob sc = new SerialClob(expected.toCharArray()); 388 SerialClob sc1 = new SerialClob(new StubClob()); 389 long pos = sc1.position(sc, 1); 390 assertEquals(expectedPos, pos); 391 } 392 393 /* 394 * Check that setString() updates the appropriate characters in the 395 * SerialClob 396 */ 397 @Test 398 public void test31() throws Exception { 399 String val = "Hello, I am Bruce Wayne"; 400 String val1 = "the Batman!"; 401 String expected = "Hello, I am the Batman!"; 402 SerialClob sc = new SerialClob(val.toCharArray()); 403 int written = sc.setString(13, val1); 404 assertEquals(val1.length(), written); 405 assertTrue(expected.equals(sc.getSubString(1, (int) sc.length()))); 406 } 407 408 /* 409 * Check that setString() updates the appropriate characters in the 410 * SerialClob 411 */ 412 @Test(enabled = false) 413 public void test32() throws Exception { 414 int expectedWritten = 9; 415 String val = "Hi, I am Catwoman!!!!!!"; 416 String val1 = "Hahaha the Joker, who are you?!"; 417 String expected = "Hi, I am the Joker!"; 418 SerialClob sc = new SerialClob(val.toCharArray()); 419 int written = sc.setString(10, val1, 8, expectedWritten+1); 420 assertEquals(written, expectedWritten); 421 422 } 423 424 /* 425 * Check that setCharacterStream() returns a non-null Writer for an 426 * SerialClob created from a Clob 427 */ 428 @Test 429 public void test33() throws Exception { 430 SerialClob sc = new SerialClob(new StubClob()); 431 Writer w = sc.setCharacterStream(1); 432 assertTrue(w != null); 433 } 434 435 /* 436 * Check that setAsciiStream() returns a non-null OutputStream for an SerialClob 437 * created from a Clob 438 */ 439 @Test 440 public void test34() throws Exception { 441 SerialClob sc = new SerialClob(new StubClob()); 442 OutputStream os = sc.setAsciiStream(1); 443 assertTrue(os != null); 444 } 445 446 /* 447 * Check that truncate() truncates the length of the SerialClob to the 448 * specified size 449 */ 450 @Test 451 public void test35() throws Exception { 452 SerialClob sc = new SerialClob(new StubClob()); 453 sc.truncate(0); 454 assertTrue(sc.length() == 0); 455 sc = new SerialClob(chars); 456 sc.truncate(5); 457 assertTrue(sc.length() == 5); 458 } 459 460 /* 461 * Check that getCharacterStream() returns a Reader and that the char[] that 462 * was specified to create the SerialClob can be returned via the Reader 463 */ 464 @Test 465 public void test36() throws Exception { 466 SerialClob sc = new SerialClob(chars); 467 Reader r = sc.getCharacterStream(); 468 for (char c : chars) { 469 char val = (char) r.read(); 470 assertTrue(c == val, val + " does not match " + c); 471 } 472 } 473 474 /* 475 * Check that getCharacterStream() returns a Reader and that the char[] that 476 * was specified to create the SerialClob can be returned via the Reader 477 */ 478 @Test(enabled = false) 479 public void test37() throws Exception { 480 SerialClob sc = new SerialClob(chars); 481 String expected = "ello w"; 482 Reader r = sc.getCharacterStream(2, 6); 483 for (char c : expected.toCharArray()) { 484 char val = (char) r.read(); 485 assertTrue(c == val, val + " does not match " + c); 486 } 487 } 488 489 /* 490 * Validate that a SerialClob that is serialized & deserialized is equal to 491 * itself 492 */ 493 @Test 494 public void test38() throws Exception { 495 SerialClob sc = new SerialClob(chars); 496 SerialClob sc2 = serializeDeserializeObject(sc); 497 assertTrue(sc.equals(sc2), "SerialClobs not equal"); 498 } 499 }