1 21 22 package org.apache.derbyTesting.functionTests.tests.lang; 23 24 import java.io.FileReader ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 28 import java.sql.Connection ; 29 import java.sql.PreparedStatement ; 30 import java.sql.ResultSet ; 31 import java.sql.Statement ; 32 import java.sql.SQLException ; 33 import java.sql.Types ; 34 35 import org.apache.derby.tools.ij; 36 import org.apache.derby.tools.JDBCDisplayUtil; 37 38 52 public class xmlBinding 53 { 54 57 public static void main(String [] args) 58 { 59 new xmlBinding().go(args); 60 } 61 62 68 public void go(String [] args) 69 { 70 try { 71 72 ij.getPropertyArg(args); 75 Connection conn = ij.startJBMS(); 76 77 Statement st = conn.createStatement(); 79 st.execute("create table xTable.t1 " + 80 "(i int generated always as identity, x xml)"); 81 82 doBindTests(conn); 84 doXMLParseTests(conn); 85 doXMLSerializeTests(conn); 86 doXMLExistsTests(conn); 87 88 st.close(); 90 conn.close(); 91 92 System.out.println("[ Done. ]\n"); 93 94 } catch (Exception e) { 95 96 System.out.println("Unexpected error: "); 97 e.printStackTrace(System.out); 98 99 } 100 } 101 102 107 private void doBindTests(Connection conn) 108 { 109 System.out.println("\n[ Beginning XML binding tests. ]\n"); 111 112 PreparedStatement pSt = null; 114 try { 115 116 pSt = conn.prepareStatement( 126 "insert into xTable.t1(x) values (?)"); 127 128 System.out.print("XML column -- bind String to XML: "); 129 bindAndExecute(pSt, 1, Types.VARCHAR, "shouldn't work", "42Z70", false); 130 131 System.out.print("XML column -- bind Java null to XML: "); 132 bindAndExecute(pSt, 1, Types.VARCHAR, null, "42Z70", false); 133 134 System.out.print("XML column -- bind SQL NULL to XML: "); 135 bindAndExecute(pSt, 1, Types.VARCHAR, null, "42Z70", true); 136 137 System.out.print("XML column -- bind integer to XML: "); 138 bindAndExecute(pSt, 1, Types.INTEGER, new Integer (8), "42Z70", false); 139 140 } catch (SQLException se) { 141 System.out.print("XML column -- insertion via parameter: "); 143 checkException(se, "42Z70"); 144 } 145 146 System.out.print("Trying to bind to XML in XMLSERIALIZE: "); 150 try { 151 pSt = conn.prepareStatement( 152 "select XMLSERIALIZE(? AS CLOB) FROM XTABLE.T1"); 153 bindAndExecute(pSt, 1, Types.VARCHAR, null, "42Z70", true); 154 } catch (SQLException se) { 155 checkException(se, "42Z70"); 156 } 157 158 System.out.print("Trying to bind to XML in XMLEXISTS: "); 162 try { 163 pSt = conn.prepareStatement( 164 "select i from xTable.t1 where " + 165 "XMLEXISTS('//*' PASSING BY REF ?)"); 166 bindAndExecute(pSt, 1, Types.VARCHAR, null, "42Z70", true); 167 } catch (SQLException se) { 168 checkException(se, "42Z70"); 169 } 170 171 System.out.print("XML value in result set: "); 175 try { 176 pSt = conn.prepareStatement("select x from xTable.t1"); 177 pSt.execute(); 178 } catch (SQLException se) { 179 checkException(se, "42Z71"); 180 } 181 182 System.out.println("\n[ End XML binding tests. ]\n"); 183 } 184 185 192 private void doXMLParseTests(Connection conn) 193 { 194 System.out.println("\n[ Beginning XMLPARSE tests. ]\n"); 195 196 System.out.println("Test insertions from file: "); 197 try { 198 199 insertFiles(conn, "xTable.t1", "xmlTestFiles/wide40k.xml", 1); 201 insertFiles(conn, "xTable.t1", "xmlTestFiles/deep40k.xml", 1); 202 203 208 insertDocWithDTD(conn, "xTable.t1", "xmlTestFiles/dtdDoc.xml", 209 "personal.dtd", 1); 210 insertFiles(conn, "xTable.t1", "xmlTestFiles/xsdDoc.xml", 1); 211 212 insertDocWithDTD(conn, "xTable.t1", 216 "xmlTestFiles/dtdDoc_invalid.xml", "personal.dtd", 1); 217 insertFiles(conn, "xTable.t1", 218 "xmlTestFiles/xsdDoc_invalid.xml", 1); 219 220 System.out.println("--> Insertions all PASS."); 221 222 } catch (SQLException se) { 223 System.out.println("FAIL: Unexpected exception: "); 224 while (se != null) { 225 se.printStackTrace(System.out); 226 se = se.getNextException(); 227 } 228 } catch (Exception e) { 229 System.out.println("FAIL: Unexpected exception: "); 230 e.printStackTrace(System.out); 231 } 232 233 235 try { 236 237 PreparedStatement pSt = conn.prepareStatement( 238 "insert into xTable.t1(x) values " + 239 "(XMLPARSE (DOCUMENT CAST (? as CLOB) PRESERVE WHITESPACE))"); 240 241 System.out.print("Binding string in XMLPARSE: "); 244 bindAndExecute(pSt, 1, Types.CHAR, "<simple> doc </simple>", 245 null, false); 246 247 System.out.print("Binding Java null string in XMLPARSE: "); 249 bindAndExecute(pSt, 1, Types.CHAR, null, null, false); 250 System.out.print("Binding SQL NULL string in XMLPARSE: "); 251 bindAndExecute(pSt, 1, Types.CLOB, null, null, true); 252 253 } catch (Exception e) { 254 System.out.println("Unexpected exception: "); 255 e.printStackTrace(System.out); 256 } 257 258 System.out.println("\n[ End XMLPARSE tests. ]\n"); 259 } 260 261 269 private void doXMLSerializeTests(Connection conn) 270 { 271 System.out.println("\n[ Beginning XMLSERIALIZE tests. ]\n"); 272 273 try { 274 275 PreparedStatement pSt = conn.prepareStatement( 276 "select i, XMLSERIALIZE(X AS CLOB) FROM xTable.t1"); 277 ResultSet rs = pSt.executeQuery(); 278 279 String xResult = null; 280 int rowCount = 0; 281 while (rs.next()) { 282 xResult = rs.getString(2); 283 if (!rs.wasNull()) { 284 System.out.println(rs.getInt(1) + ", " + 285 "[ roughly " + (xResult.length() / 1000) + "k ]"); 286 } 287 else 288 System.out.println(rs.getInt(1) + ", NULL"); 289 rowCount++; 290 } 291 292 } catch (Exception e) { 293 System.out.println("Unexpected exception: "); 294 e.printStackTrace(System.out); 295 } 296 297 302 System.out.println("\n[ End XMLSERIALIZE tests. ]\n"); 303 } 304 305 311 private void doXMLExistsTests(Connection conn) 312 { 313 System.out.println("\n[ Begin XMLEXISTS tests. ]\n"); 314 315 try { 317 318 existsQuery(conn, "xTable.t1", "//abb"); 319 existsQuery(conn, "xTable.t1", "//d50"); 320 existsQuery(conn, "xTable.t1", "//person/email"); 321 existsQuery(conn, "xTable.t1", "/personnel"); 322 existsQuery(conn, "xTable.t1", "//person/@id"); 323 324 int rowCount = existsQuery(conn, "xTable.t1", "//person/@noteTwo"); 330 if (rowCount == 0) { 331 System.out.println("FAILED: Query on DTD default didn't " + 332 "return any matches."); 333 } 334 335 } catch (Exception e) { 336 System.out.println("Unexpected exception: "); 337 e.printStackTrace(System.out); 338 } 339 340 try { 347 348 System.out.print("Parameter as first operand in XMLEXISTS: "); 349 350 PreparedStatement pSt = conn.prepareStatement( 357 "select i from xTable.t1 where " + 358 "XMLEXISTS (? PASSING BY REF x)"); 359 pSt.setString(1, "//*"); 360 pSt.execute(); 361 362 } catch (SQLException se) { 363 checkException(se, "42Z75"); 364 } 365 366 System.out.println("\n[ End XMLEXISTS tests. ]\n"); 367 } 368 369 379 private void insertFiles(Connection conn, 380 String tableName, String fName, int numRows) 381 throws Exception 382 { 383 InputStream iS = this.getClass().getResourceAsStream(fName); 386 InputStreamReader reader = new InputStreamReader (iS); 387 char [] cA = new char[1024]; 388 int charCount = 0; 389 for (int len = reader.read(cA, 0, cA.length); len != -1; 390 charCount += len, len = reader.read(cA, 0, cA.length)); 391 392 reader.close(); 393 394 397 PreparedStatement pSt = conn.prepareStatement( 398 "insert into xTable.t1(x) values (" + 399 "xmlparse(document cast (? as clob) preserve whitespace))"); 400 401 for (int i = 0; i < numRows; i++) { 402 403 iS = this.getClass().getResourceAsStream(fName); 404 reader = new InputStreamReader (iS); 405 pSt.setCharacterStream(1, reader, charCount); 406 pSt.execute(); 407 reader.close(); 408 System.out.println("Inserted roughly " + 409 (charCount / 1000) + "k of data."); 410 411 } 412 } 413 414 433 private void insertDocWithDTD(Connection conn, 434 String tableName, String fName, String dtdName, 435 int numRows) throws Exception 436 { 437 boolean needsUpdate = true; 438 String currPath = System.getProperty("user.dir"); 439 String fileSep = System.getProperty("file.separator"); 440 441 String dtdPath = currPath; 442 boolean foundDTD = false; 443 while (!foundDTD) { 444 445 try { 446 447 FileReader fR = new FileReader (dtdPath + 448 fileSep + dtdName); 449 450 foundDTD = true; 453 dtdPath = "file:///" + dtdPath + fileSep + dtdName; 454 break; 455 456 } catch (java.io.IOException ie) { 457 458 464 int pos = dtdPath.lastIndexOf(fileSep); 465 if (pos == -1) { 466 throw new Exception ("Couldn't find DTD '" + 469 dtdName + "' for insertion of file '" + 470 fName + "'."); 471 } 472 dtdPath = dtdPath.substring(0, pos); 473 474 } 475 } 476 477 InputStream iS = this.getClass().getResourceAsStream(fName); 479 InputStreamReader reader = new InputStreamReader (iS); 480 char [] cA = new char[1024]; 481 StringBuffer sBuf = new StringBuffer (); 482 int charCount = 0; 483 for (int len = reader.read(cA, 0, cA.length); len != -1; 484 charCount += len, len = reader.read(cA, 0, cA.length)) 485 { 486 sBuf.append(cA, 0, len); 487 } 488 489 reader.close(); 490 491 String docAsString = sBuf.toString(); 493 int pos = docAsString.indexOf(dtdName); 494 if (pos != -1) 495 sBuf.replace(pos, pos + dtdName.length(), dtdPath); 496 497 docAsString = sBuf.toString(); 500 PreparedStatement pSt = conn.prepareStatement( 501 "insert into xTable.t1(x) values (" + 502 "xmlparse(document cast (? as clob) preserve whitespace))"); 503 504 charCount = docAsString.length(); 505 for (int i = 0; i < numRows; i++) { 506 507 pSt.setString(1, docAsString); 508 pSt.execute(); 509 System.out.println("Inserted roughly " + 510 (charCount / 1000) + "k of data."); 511 512 } 513 } 514 515 526 private int existsQuery(Connection conn, 527 String tableName, String xPath) throws Exception 528 { 529 PreparedStatement pSt = conn.prepareStatement( 530 "select i from " + tableName + " where " + 531 "xmlexists('" + xPath + "' passing by ref x)"); 532 533 System.out.println("Running XMLEXISTS with: " + xPath); 534 ResultSet rs = pSt.executeQuery(); 535 String xResult = null; 536 int rowCount = 0; 537 while (rs.next()) { 538 rowCount++; 539 } 540 541 System.out.println("--> Matching rows: " + rowCount); 542 return rowCount; 543 } 544 545 560 private void bindAndExecute(PreparedStatement pSt, int paramNum, 561 int paramType, Object bindValue, String sqlState, 562 boolean bindSqlNull) 563 { 564 SQLException actualException = null; 565 try { 566 567 if (bindSqlNull) { 569 pSt.setNull(paramNum, paramType); 570 } 571 else { 572 switch (paramType) 573 { 574 case Types.CHAR: 575 case Types.VARCHAR: 576 577 pSt.setString(paramNum, (String )bindValue); 578 break; 579 580 case Types.INTEGER: 581 582 pSt.setInt(paramNum, ((Integer )bindValue).intValue()); 583 break; 584 585 default: 586 587 System.out.println("ERROR: Unexpected bind type (" + 588 paramType + ") in call to doBind."); 589 break; 590 } 591 } 592 593 pSt.execute(); 595 596 } catch (SQLException e) { 597 actualException = e; 598 } 599 600 checkException(actualException, sqlState); 601 } 602 603 613 private void checkException(SQLException se, 614 String targetState) 615 { 616 if (targetState == null) { 617 if (se == null) { 618 System.out.println("PASS -- Completed without exception, " + 619 "as expected."); 620 } 621 else { 622 System.out.println("FAIL -- Was expected to succeed, but " + 623 "failed with error " + se.getSQLState() + "."); 624 se.printStackTrace(System.out); 625 } 626 return; 627 } 628 629 if (se == null) { 630 System.out.println("FAIL -- Completed without exception when " + 631 "error " + targetState + " was expected."); 632 return; 633 } 634 635 if (!targetState.equals(se.getSQLState())) { 636 System.out.println("FAIL: Caught error " + se.getSQLState() + 637 " when was expecting error " + targetState + "."); 638 se.printStackTrace(System.out); 639 return; 640 } 641 642 System.out.println("PASS -- caught expected error " + 643 targetState + "."); 644 } 645 } 646 | Popular Tags |