0001: //jTDS JDBC Driver for Microsoft SQL Server and Sybase
0002: //Copyright (C) 2004 The jTDS Project
0003: //
0004: //This library is free software; you can redistribute it and/or
0005: //modify it under the terms of the GNU Lesser General Public
0006: //License as published by the Free Software Foundation; either
0007: //version 2.1 of the License, or (at your option) any later version.
0008: //
0009: //This library is distributed in the hope that it will be useful,
0010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
0011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0012: //Lesser General Public License for more details.
0013: //
0014: //You should have received a copy of the GNU Lesser General Public
0015: //License along with this library; if not, write to the Free Software
0016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0017: //
0018: package net.sourceforge.jtds.test;
0019:
0020: import java.io.*;
0021: import java.sql.*;
0022: import java.util.*;
0023:
0024: //
0025: // MJH - Changes for new jTDS version
0026: // Amended many lines such as those in testBlobSetNull6
0027: // where ResultSet variable rs was used when rs2 is actually required.
0028: // Amazed old version did not fail also as rs was closed!
0029: // Changed get / set UnicodeStream tests to align with standard.
0030: //
0031: /**
0032: * @version $Id: LOBTest.java,v 1.27 2005/04/17 18:41:29 alin_sinpalean Exp $
0033: */
0034: public class LOBTest extends TestBase {
0035: private static final int LOB_LENGTH = 8000;
0036: private static final byte[] blobData = new byte[LOB_LENGTH];
0037: private static final byte[] newBlobData = new byte[LOB_LENGTH];
0038: private static final String clobData;
0039: private static final String newClobData;
0040:
0041: static {
0042: for (int i = 0; i < blobData.length; i++) {
0043: blobData[i] = (byte) (Math.random() * 255);
0044: newBlobData[i] = (byte) (Math.random() * 255);
0045: }
0046:
0047: StringBuffer data = new StringBuffer();
0048: StringBuffer newData = new StringBuffer();
0049:
0050: for (int i = 0; i < LOB_LENGTH; i++) {
0051: data.append((char) (Math.random() * 58) + 32);
0052: newData.append((char) (Math.random() * 58) + 32);
0053: }
0054:
0055: clobData = data.toString();
0056: newClobData = newData.toString();
0057: }
0058:
0059: public LOBTest(String name) {
0060: super (name);
0061: }
0062:
0063: /*************************************************************************
0064: *************************************************************************
0065: ** BLOB TESTS **
0066: *************************************************************************
0067: *************************************************************************/
0068:
0069: public void testBlobGet1() throws Exception {
0070: byte[] data = getBlobTestData();
0071:
0072: Statement stmt = con.createStatement();
0073: stmt.execute("CREATE TABLE #blobget1 (data IMAGE)");
0074: stmt.close();
0075:
0076: PreparedStatement pstmt = con
0077: .prepareStatement("INSERT INTO #blobget1 (data) VALUES (?)");
0078:
0079: // Test PreparedStatement.setBytes()
0080: pstmt.setBytes(1, data);
0081: assertEquals(pstmt.executeUpdate(), 1);
0082:
0083: pstmt.close();
0084:
0085: Statement stmt2 = con.createStatement();
0086: ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobget1");
0087:
0088: assertTrue(rs.next());
0089:
0090: // Test ResultSet.getBytes()
0091: assertTrue(Arrays.equals(data, rs.getBytes(1)));
0092:
0093: // Test ResultSet.getBinaryStream()
0094: InputStream is = rs.getBinaryStream(1);
0095: byte[] isTmpData = new byte[data.length];
0096:
0097: assertEquals(data.length, is.read(isTmpData));
0098: assertEquals(-1, is.read());
0099: assertTrue(Arrays.equals(data, isTmpData));
0100:
0101: // Test ResultSet.getBlob()
0102: Blob blob = rs.getBlob(1);
0103:
0104: assertNotNull(blob);
0105:
0106: // Test Blob.length()
0107: assertEquals(blob.length(), data.length);
0108:
0109: // Test Blob.getBytes(0, length); should fail
0110: try {
0111: blob.getBytes(0L, (int) blob.length());
0112: fail("Blob.getBytes(0, length) should fail.");
0113: } catch (SQLException ex) {
0114: assertEquals("HY090", ex.getSQLState());
0115: }
0116:
0117: // Test Blob.getBytes()
0118: byte[] tmpData2 = blob.getBytes(1L, (int) blob.length());
0119:
0120: assertTrue(Arrays.equals(data, tmpData2));
0121:
0122: // Test Blob.getBinaryStream()
0123: InputStream is2 = blob.getBinaryStream();
0124: compareInputStreams(new ByteArrayInputStream(data), is2);
0125:
0126: assertFalse(rs.next());
0127: stmt2.close();
0128: rs.close();
0129: }
0130:
0131: public void testBlobGet2() throws Exception {
0132: byte[] data = getBlobTestData();
0133:
0134: Statement stmt = con.createStatement();
0135: stmt.execute("CREATE TABLE #blobget2 (data IMAGE)");
0136: stmt.close();
0137:
0138: PreparedStatement pstmt = con
0139: .prepareStatement("INSERT INTO #blobget2 (data) VALUES (?)");
0140:
0141: // Test PreparedStatement.setBinaryStream()
0142: pstmt.setBinaryStream(1, new ByteArrayInputStream(data),
0143: data.length);
0144: assertEquals(pstmt.executeUpdate(), 1);
0145:
0146: pstmt.close();
0147:
0148: Statement stmt2 = con.createStatement();
0149: ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobget2");
0150:
0151: assertTrue(rs.next());
0152:
0153: // Test ResultSet.getObject() - Blob
0154: Object result = rs.getObject(1);
0155:
0156: assertTrue(result instanceof Blob);
0157:
0158: Blob blob = (Blob) result;
0159:
0160: assertEquals(data.length, blob.length());
0161:
0162: // Test Blob.getBytes()
0163: assertTrue(Arrays.equals(data, blob.getBytes(1L, (int) blob
0164: .length())));
0165:
0166: assertFalse(rs.next());
0167: stmt2.close();
0168: rs.close();
0169: }
0170:
0171: public void testBlobSet1() throws Exception {
0172: byte[] data = getBlobTestData();
0173:
0174: Statement stmt = con.createStatement();
0175: stmt.execute("CREATE TABLE #blobset1 (data IMAGE)");
0176: stmt.close();
0177:
0178: PreparedStatement pstmt = con
0179: .prepareStatement("INSERT INTO #blobset1 (data) VALUES (?)");
0180:
0181: // Test PreparedStatement.setBinaryStream()
0182: pstmt.setBinaryStream(1, new ByteArrayInputStream(data),
0183: data.length);
0184: assertEquals(pstmt.executeUpdate(), 1);
0185:
0186: pstmt.close();
0187:
0188: Statement stmt2 = con.createStatement();
0189: ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobset1");
0190:
0191: assertTrue(rs.next());
0192:
0193: // Test ResultSet.getBytes()
0194: assertTrue(Arrays.equals(data, rs.getBytes(1)));
0195:
0196: assertFalse(rs.next());
0197: stmt2.close();
0198: rs.close();
0199: }
0200:
0201: public void testBlobSet2() throws Exception {
0202: byte[] data = getBlobTestData();
0203:
0204: Statement stmt = con.createStatement();
0205: stmt.execute("CREATE TABLE #blobset2 (data IMAGE)");
0206: stmt.close();
0207:
0208: PreparedStatement pstmt = con
0209: .prepareStatement("INSERT INTO #blobset2 (data) VALUES (?)");
0210:
0211: // Test PreparedStatement.setBytes()
0212: pstmt.setBytes(1, data);
0213: assertEquals(pstmt.executeUpdate(), 1);
0214:
0215: pstmt.close();
0216:
0217: Statement stmt2 = con.createStatement();
0218: ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobset2");
0219:
0220: assertTrue(rs.next());
0221:
0222: Blob blob = rs.getBlob(1);
0223:
0224: data = getNewBlobTestData();
0225:
0226: // Test Blob.setBytes()
0227: blob.setBytes(1, data);
0228:
0229: assertTrue(Arrays.equals(data, blob.getBytes(1L, (int) blob
0230: .length())));
0231:
0232: assertFalse(rs.next());
0233:
0234: PreparedStatement pstmt2 = con
0235: .prepareStatement("UPDATE #blobset2 SET data = ?");
0236:
0237: // Test PreparedStatement.setBlob()
0238: pstmt2.setBlob(1, blob);
0239: assertEquals(1, pstmt2.executeUpdate());
0240:
0241: pstmt2.close();
0242:
0243: stmt2.close();
0244: rs.close();
0245:
0246: Statement stmt3 = con.createStatement();
0247: ResultSet rs2 = stmt3
0248: .executeQuery("SELECT data FROM #blobset2");
0249:
0250: assertTrue(rs2.next());
0251:
0252: // Test ResultSet.getBytes()
0253: assertTrue(Arrays.equals(data, rs2.getBytes(1)));
0254:
0255: assertFalse(rs2.next());
0256: stmt3.close();
0257: rs2.close();
0258: }
0259:
0260: public void testBlobSet3() throws Exception {
0261: byte[] data = getBlobTestData();
0262:
0263: Statement stmt = con.createStatement();
0264: stmt.execute("CREATE TABLE #blobset3 (data IMAGE)");
0265: stmt.close();
0266:
0267: PreparedStatement pstmt = con
0268: .prepareStatement("INSERT INTO #blobset3 (data) VALUES (?)");
0269:
0270: // Test PreparedStatement.setObject(int,byte[])
0271: pstmt.setObject(1, data);
0272: assertEquals(pstmt.executeUpdate(), 1);
0273:
0274: pstmt.close();
0275:
0276: Statement stmt2 = con.createStatement();
0277: ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobset3");
0278:
0279: assertTrue(rs.next());
0280:
0281: // Test ResultSet.getBytes()
0282: assertTrue(Arrays.equals(data, rs.getBytes(1)));
0283:
0284: assertFalse(rs.next());
0285: stmt2.close();
0286: rs.close();
0287: }
0288:
0289: public void testBlobSet4() throws Exception {
0290: byte[] data = getBlobTestData();
0291:
0292: Statement stmt = con.createStatement();
0293: stmt.execute("CREATE TABLE #blobset4 (data IMAGE)");
0294: stmt.close();
0295:
0296: PreparedStatement pstmt = con
0297: .prepareStatement("INSERT INTO #blobset4 (data) VALUES (?)");
0298:
0299: // Test PreparedStatement.setBytes()
0300: pstmt.setBytes(1, data);
0301: assertEquals(pstmt.executeUpdate(), 1);
0302:
0303: pstmt.close();
0304:
0305: Statement stmt2 = con.createStatement();
0306: ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobset4");
0307:
0308: assertTrue(rs.next());
0309:
0310: Blob blob = rs.getBlob(1);
0311:
0312: data = getNewBlobTestData();
0313:
0314: // Test Blob.setBytes()
0315: blob.setBytes(1, data);
0316:
0317: assertTrue(Arrays.equals(data, blob.getBytes(1L, (int) blob
0318: .length())));
0319:
0320: assertFalse(rs.next());
0321:
0322: PreparedStatement pstmt2 = con
0323: .prepareStatement("UPDATE #blobset4 SET data = ?");
0324:
0325: // Test PreparedStatement.setObject(int,Blob)
0326: pstmt2.setObject(1, blob);
0327: assertEquals(1, pstmt2.executeUpdate());
0328:
0329: pstmt2.close();
0330:
0331: stmt2.close();
0332: rs.close();
0333:
0334: Statement stmt3 = con.createStatement();
0335: ResultSet rs2 = stmt3
0336: .executeQuery("SELECT data FROM #blobset4");
0337:
0338: assertTrue(rs2.next());
0339:
0340: // Test ResultSet.getBytes()
0341: assertTrue(Arrays.equals(data, rs2.getBytes(1)));
0342:
0343: assertFalse(rs2.next());
0344: stmt3.close();
0345: rs2.close();
0346: }
0347:
0348: public void testBlobSet5() throws Exception {
0349: byte[] data = getBlobTestData();
0350:
0351: Statement stmt = con.createStatement();
0352: stmt.execute("CREATE TABLE #blobset5 (data IMAGE)");
0353: stmt.close();
0354:
0355: PreparedStatement pstmt = con
0356: .prepareStatement("INSERT INTO #blobset5 (data) VALUES (?)");
0357:
0358: // Test PreparedStatement.setObject(int,byte[],int)
0359: pstmt.setObject(1, data, Types.BINARY);
0360: assertEquals(pstmt.executeUpdate(), 1);
0361:
0362: pstmt.close();
0363:
0364: Statement stmt2 = con.createStatement();
0365: ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobset5");
0366:
0367: assertTrue(rs.next());
0368:
0369: // Test ResultSet.getBytes()
0370: assertTrue(Arrays.equals(data, rs.getBytes(1)));
0371:
0372: assertFalse(rs.next());
0373: stmt2.close();
0374: rs.close();
0375: }
0376:
0377: public void testBlobSet6() throws Exception {
0378: byte[] data = getBlobTestData();
0379:
0380: Statement stmt = con.createStatement();
0381: stmt.execute("CREATE TABLE #blobset6 (data IMAGE)");
0382: stmt.close();
0383:
0384: PreparedStatement pstmt = con
0385: .prepareStatement("INSERT INTO #blobset6 (data) VALUES (?)");
0386:
0387: // Test PreparedStatement.setObject(int,byte[],int)
0388: pstmt.setObject(1, data, Types.VARBINARY);
0389: assertEquals(pstmt.executeUpdate(), 1);
0390:
0391: pstmt.close();
0392:
0393: Statement stmt2 = con.createStatement();
0394: ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobset6");
0395:
0396: assertTrue(rs.next());
0397:
0398: // Test ResultSet.getBytes()
0399: assertTrue(Arrays.equals(data, rs.getBytes(1)));
0400:
0401: assertFalse(rs.next());
0402: stmt2.close();
0403: rs.close();
0404: }
0405:
0406: public void testBlobSet7() throws Exception {
0407: byte[] data = getBlobTestData();
0408:
0409: Statement stmt = con.createStatement();
0410: stmt.execute("CREATE TABLE #blobset7 (data IMAGE)");
0411: stmt.close();
0412:
0413: PreparedStatement pstmt = con
0414: .prepareStatement("INSERT INTO #blobset7 (data) VALUES (?)");
0415:
0416: // Test PreparedStatement.setBytes()
0417: pstmt.setBytes(1, data);
0418: assertEquals(pstmt.executeUpdate(), 1);
0419:
0420: pstmt.close();
0421:
0422: Statement stmt2 = con.createStatement();
0423: ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobset7");
0424:
0425: assertTrue(rs.next());
0426:
0427: Blob blob = rs.getBlob(1);
0428:
0429: data = getNewBlobTestData();
0430:
0431: // Test Blob.setBytes()
0432: blob.setBytes(1, data);
0433:
0434: assertTrue(Arrays.equals(data, blob.getBytes(1L, (int) blob
0435: .length())));
0436:
0437: assertFalse(rs.next());
0438:
0439: PreparedStatement pstmt2 = con
0440: .prepareStatement("UPDATE #blobset7 SET data = ?");
0441:
0442: // Test PreparedStatement.setObject(int,Blob,int)
0443: pstmt2.setObject(1, blob, Types.BLOB);
0444: assertEquals(1, pstmt2.executeUpdate());
0445:
0446: pstmt2.close();
0447:
0448: stmt2.close();
0449: rs.close();
0450:
0451: Statement stmt3 = con.createStatement();
0452: ResultSet rs2 = stmt3
0453: .executeQuery("SELECT data FROM #blobset7");
0454:
0455: assertTrue(rs2.next());
0456:
0457: // Test ResultSet.getBytes()
0458: assertTrue(Arrays.equals(data, rs2.getBytes(1)));
0459:
0460: assertFalse(rs2.next());
0461: stmt3.close();
0462: rs2.close();
0463: }
0464:
0465: /**
0466: * Test inserting from an <code>InputStream</code> that doesn't fill the
0467: * buffer on <code>read()</code>.
0468: * <p>
0469: * For bug #1008816 - "More data in stream ..." error when inserting an image.
0470: *
0471: * @throws Exception if an error condition occurs
0472: */
0473: public void testBlobSet8() throws Exception {
0474: Statement stmt = con.createStatement();
0475: stmt.execute("CREATE TABLE #blobset8 (data IMAGE)");
0476: stmt.close();
0477:
0478: PreparedStatement pstmt = con
0479: .prepareStatement("INSERT INTO #blobset8 (data) VALUES (?)");
0480:
0481: // Test PreparedStatement.setBinaryStream()
0482: pstmt.setBinaryStream(1, new RealInputStream(),
0483: RealInputStream.LENGTH);
0484: assertEquals(pstmt.executeUpdate(), 1);
0485:
0486: pstmt.close();
0487:
0488: Statement stmt2 = con.createStatement();
0489: ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobset8");
0490:
0491: assertTrue(rs.next());
0492:
0493: // Test ResultSet.getBinaryStream()
0494: compareInputStreams(new RealInputStream(), rs
0495: .getBinaryStream(1));
0496:
0497: assertFalse(rs.next());
0498: stmt2.close();
0499: rs.close();
0500: }
0501:
0502: public void testBlobUpdate1() throws Exception {
0503: byte[] data = getBlobTestData();
0504:
0505: Statement stmt = con.createStatement();
0506: stmt
0507: .execute("CREATE TABLE #blobupdate1 (id NUMERIC IDENTITY, data IMAGE, "
0508: + "CONSTRAINT pk_blobupdate1 PRIMARY KEY CLUSTERED (id))");
0509: stmt.close();
0510:
0511: stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
0512: ResultSet.CONCUR_UPDATABLE);
0513: ResultSet rs = stmt
0514: .executeQuery("SELECT id, data FROM #blobupdate1");
0515:
0516: rs.moveToInsertRow();
0517:
0518: // Test ResultSet.updateBytes()
0519: rs.updateBytes(2, data);
0520:
0521: rs.insertRow();
0522:
0523: stmt.close();
0524: rs.close();
0525:
0526: Statement stmt2 = con.createStatement();
0527: ResultSet rs2 = stmt2
0528: .executeQuery("SELECT data FROM #blobupdate1");
0529:
0530: assertTrue(rs2.next());
0531:
0532: // Test ResultSet.getBytes()
0533: assertTrue(Arrays.equals(data, rs2.getBytes(1)));
0534:
0535: assertFalse(rs2.next());
0536: stmt2.close();
0537: rs2.close();
0538: }
0539:
0540: public void testBlobUpdate2() throws Exception {
0541: byte[] data = getBlobTestData();
0542:
0543: Statement stmt = con.createStatement();
0544: stmt
0545: .execute("CREATE TABLE #blobupdate2 (id NUMERIC IDENTITY, data IMAGE, "
0546: + "CONSTRAINT pk_blobupdate2 PRIMARY KEY CLUSTERED (id))");
0547: stmt.close();
0548:
0549: stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
0550: ResultSet.CONCUR_UPDATABLE);
0551: ResultSet rs = stmt
0552: .executeQuery("SELECT id, data FROM #blobupdate2");
0553:
0554: rs.moveToInsertRow();
0555:
0556: // Test ResultSet.updateBinaryStream()
0557: rs.updateBinaryStream(2, new ByteArrayInputStream(data),
0558: data.length);
0559:
0560: rs.insertRow();
0561:
0562: stmt.close();
0563: rs.close();
0564:
0565: Statement stmt2 = con.createStatement();
0566: ResultSet rs2 = stmt2
0567: .executeQuery("SELECT data FROM #blobupdate2");
0568:
0569: assertTrue(rs2.next());
0570:
0571: // Test ResultSet.getBytes()
0572: assertTrue(Arrays.equals(data, rs2.getBytes(1)));
0573:
0574: assertFalse(rs2.next());
0575: stmt2.close();
0576: rs2.close();
0577: }
0578:
0579: public void testBlobUpdate3() throws Exception {
0580: byte[] data = getBlobTestData();
0581:
0582: Statement stmt = con.createStatement();
0583: stmt
0584: .execute("CREATE TABLE #blobupdate3 (id NUMERIC IDENTITY, data IMAGE, "
0585: + "CONSTRAINT pk_blobupdate3 PRIMARY KEY CLUSTERED (id))");
0586: stmt.close();
0587:
0588: PreparedStatement pstmt = con
0589: .prepareStatement("INSERT INTO #blobupdate3 (data) VALUES (?)");
0590:
0591: // Test PreparedStatement.setBytes()
0592: pstmt.setBytes(1, data);
0593: assertEquals(pstmt.executeUpdate(), 1);
0594:
0595: pstmt.close();
0596:
0597: Statement stmt2 = con.createStatement();
0598: ResultSet rs = stmt2
0599: .executeQuery("SELECT data FROM #blobupdate3");
0600:
0601: assertTrue(rs.next());
0602:
0603: Blob blob = rs.getBlob(1);
0604:
0605: data = getNewBlobTestData();
0606:
0607: // Test Blob.setBytes()
0608: blob.setBytes(1, data);
0609:
0610: assertTrue(Arrays.equals(data, blob.getBytes(1L, (int) blob
0611: .length())));
0612:
0613: assertFalse(rs.next());
0614:
0615: Statement stmt3 = con.createStatement(
0616: ResultSet.TYPE_SCROLL_SENSITIVE,
0617: ResultSet.CONCUR_UPDATABLE);
0618: ResultSet rs2 = stmt3
0619: .executeQuery("SELECT id, data FROM #blobupdate3");
0620:
0621: assertTrue(rs2.next());
0622:
0623: // Test ResultSet.updateBlob()
0624: rs2.updateBlob(2, blob);
0625:
0626: rs2.updateRow();
0627:
0628: assertFalse(rs2.next());
0629:
0630: stmt2.close();
0631: rs.close();
0632:
0633: stmt3.close();
0634: rs2.close();
0635:
0636: Statement stmt4 = con.createStatement();
0637: ResultSet rs3 = stmt4
0638: .executeQuery("SELECT data FROM #blobupdate3");
0639:
0640: assertTrue(rs3.next());
0641:
0642: // Test ResultSet.getBytes()
0643: assertTrue(Arrays.equals(data, rs3.getBytes(1)));
0644:
0645: assertFalse(rs3.next());
0646: stmt4.close();
0647: rs3.close();
0648: }
0649:
0650: public void testBlobUpdate4() throws Exception {
0651: byte[] data = getBlobTestData();
0652:
0653: Statement stmt = con.createStatement();
0654: stmt
0655: .execute("CREATE TABLE #blobupdate4 (id NUMERIC IDENTITY, data IMAGE, "
0656: + "CONSTRAINT pk_blobupdate4 PRIMARY KEY CLUSTERED (id))");
0657: stmt.close();
0658:
0659: PreparedStatement pstmt = con
0660: .prepareStatement("INSERT INTO #blobupdate4 (data) VALUES (?)");
0661:
0662: // Test PreparedStatement.setBytes()
0663: pstmt.setBytes(1, data);
0664: assertEquals(pstmt.executeUpdate(), 1);
0665:
0666: pstmt.close();
0667:
0668: Statement stmt2 = con.createStatement();
0669: ResultSet rs = stmt2
0670: .executeQuery("SELECT data FROM #blobupdate4");
0671:
0672: assertTrue(rs.next());
0673:
0674: Blob blob = rs.getBlob(1);
0675:
0676: data = getNewBlobTestData();
0677:
0678: // Test Blob.setBytes()
0679: blob.setBytes(1, data);
0680:
0681: assertTrue(Arrays.equals(data, blob.getBytes(1L, (int) blob
0682: .length())));
0683:
0684: assertFalse(rs.next());
0685:
0686: Statement stmt3 = con.createStatement(
0687: ResultSet.TYPE_SCROLL_SENSITIVE,
0688: ResultSet.CONCUR_UPDATABLE);
0689: ResultSet rs2 = stmt3
0690: .executeQuery("SELECT id, data FROM #blobupdate4");
0691:
0692: assertTrue(rs2.next());
0693:
0694: // Test ResultSet.updateBlob()
0695: rs2.updateObject(2, blob);
0696:
0697: rs2.updateRow();
0698:
0699: assertFalse(rs2.next());
0700:
0701: stmt2.close();
0702: rs.close();
0703:
0704: stmt3.close();
0705: rs2.close();
0706:
0707: Statement stmt4 = con.createStatement();
0708: ResultSet rs3 = stmt4
0709: .executeQuery("SELECT data FROM #blobupdate4");
0710:
0711: assertTrue(rs3.next());
0712:
0713: // Test ResultSet.getBytes()
0714: assertTrue(Arrays.equals(data, rs3.getBytes(1)));
0715:
0716: assertFalse(rs3.next());
0717: stmt4.close();
0718: rs3.close();
0719: }
0720:
0721: /**
0722: * Test Long blob manipulation including updates to the middle of the
0723: * <code>Blob</code>.
0724: */
0725: public void testBlobUpdate5() throws Exception {
0726: byte[] data = new byte[100000];
0727: for (int i = 0; i < data.length; i++) {
0728: data[i] = (byte) ('A' + i % 10);
0729: }
0730: //
0731: // Construct a blob
0732: //
0733: Statement stmt = con.createStatement();
0734: ResultSet rs = stmt.executeQuery("SELECT 0x00");
0735: assertNotNull(rs);
0736: assertTrue(rs.next());
0737: Blob blob = rs.getBlob(1);
0738: blob.setBytes(1, data);
0739: byte[] tmp = blob.getBytes(1, (int) blob.length());
0740: assertTrue(compare(data, tmp));
0741: blob.setBytes(1, data);
0742: tmp = blob.getBytes(1, (int) blob.length());
0743: assertTrue(compare(data, tmp));
0744: data[100] = 'a';
0745: data[101] = 'b';
0746: blob.setBytes(101, data, 100, 2);
0747: tmp = blob.getBytes(1, (int) blob.length());
0748: assertTrue(compare(data, tmp));
0749: InputStream is = blob.getBinaryStream();
0750: tmp = new byte[data.length];
0751: int b;
0752: int p = 0;
0753: while ((b = is.read()) >= 0) {
0754: tmp[p++] = (byte) b;
0755: }
0756: is.close();
0757: assertTrue(compare(data, tmp));
0758: tmp = blob.getBytes(101, 2);
0759: assertTrue(compare(new byte[] { 'a', 'b' }, tmp));
0760: blob = rs.getBlob(1);
0761: OutputStream os = blob.setBinaryStream(1);
0762: for (int i = 0; i < data.length; i++) {
0763: os.write(('A' + i % 10));
0764: }
0765: os.close();
0766: os = blob.setBinaryStream(101);
0767: os.write('a');
0768: os.write('b');
0769: os.close();
0770: tmp = blob.getBytes(1, (int) blob.length());
0771: assertTrue(compare(data, tmp));
0772: tmp = new byte[5000];
0773: for (int i = 0; i < 5000; i++) {
0774: tmp[i] = (byte) (0x80 + (i % 10));
0775: }
0776: blob.setBytes(100000 - 5000, tmp);
0777: assertTrue(compare(tmp, blob.getBytes(100000 - 5000, 5000)));
0778: assertEquals(100000L, blob.length());
0779: assertEquals(100000 - 5000, blob.position(tmp, 100000 - 5000));
0780: Blob blob2 = rs.getBlob(1);
0781: blob2.setBytes(1, tmp);
0782: assertEquals(100000 - 5000, blob.position(blob2, 1));
0783: assertEquals(101, blob.position(new byte[] { 'a', 'b' }, 1));
0784: blob.truncate(10);
0785: assertEquals(10L, blob.length());
0786: tmp = new byte[10];
0787: System.arraycopy(data, 0, tmp, 0, 10);
0788: assertTrue(compare(tmp, blob.getBytes(1, (int) blob.length())));
0789: }
0790:
0791: public void testBlobSetNull1() throws Exception {
0792: Statement stmt = con.createStatement();
0793: stmt.execute("CREATE TABLE #blobsetnull1 (data IMAGE NULL)");
0794: stmt.close();
0795:
0796: PreparedStatement pstmt = con
0797: .prepareStatement("INSERT INTO #blobsetnull1 (data) VALUES (?)");
0798:
0799: // Test PreparedStatement.setBinaryStream()
0800: pstmt.setBinaryStream(1, null, 0);
0801: assertEquals(pstmt.executeUpdate(), 1);
0802:
0803: pstmt.close();
0804:
0805: Statement stmt2 = con.createStatement();
0806: ResultSet rs = stmt2
0807: .executeQuery("SELECT data FROM #blobsetnull1");
0808:
0809: assertTrue(rs.next());
0810:
0811: // Test ResultSet.getBinaryStream()
0812: assertNull(rs.getBinaryStream(1));
0813: assertTrue(rs.wasNull());
0814:
0815: // Test ResultSet.getBlob()
0816: assertNull(rs.getBlob(1));
0817: assertTrue(rs.wasNull());
0818:
0819: // Test ResultSet.getBytes()
0820: assertNull(rs.getBytes(1));
0821: assertTrue(rs.wasNull());
0822:
0823: // Test ResultSet.getObject()
0824: assertNull(rs.getObject(1));
0825: assertTrue(rs.wasNull());
0826:
0827: assertFalse(rs.next());
0828: stmt2.close();
0829: rs.close();
0830: }
0831:
0832: public void testBlobSetNull2() throws Exception {
0833: Statement stmt = con.createStatement();
0834: stmt.execute("CREATE TABLE #blobsetnull2 (data IMAGE NULL)");
0835: stmt.close();
0836:
0837: PreparedStatement pstmt = con
0838: .prepareStatement("INSERT INTO #blobsetnull2 (data) VALUES (?)");
0839:
0840: // Test PreparedStatement.setBlob()
0841: pstmt.setBlob(1, null);
0842: assertEquals(pstmt.executeUpdate(), 1);
0843:
0844: pstmt.close();
0845:
0846: Statement stmt2 = con.createStatement();
0847: ResultSet rs = stmt2
0848: .executeQuery("SELECT data FROM #blobsetnull2");
0849:
0850: assertTrue(rs.next());
0851:
0852: // Test ResultSet.getBinaryStream()
0853: assertNull(rs.getBinaryStream(1));
0854: assertTrue(rs.wasNull());
0855:
0856: // Test ResultSet.getBlob()
0857: assertNull(rs.getBlob(1));
0858: assertTrue(rs.wasNull());
0859:
0860: // Test ResultSet.getBytes()
0861: assertNull(rs.getBytes(1));
0862: assertTrue(rs.wasNull());
0863:
0864: // Test ResultSet.getObject()
0865: assertNull(rs.getObject(1));
0866: assertTrue(rs.wasNull());
0867:
0868: assertFalse(rs.next());
0869: stmt2.close();
0870: rs.close();
0871: }
0872:
0873: public void testBlobSetNull3() throws Exception {
0874: Statement stmt = con.createStatement();
0875: stmt.execute("CREATE TABLE #blobsetnull3 (data IMAGE NULL)");
0876: stmt.close();
0877:
0878: PreparedStatement pstmt = con
0879: .prepareStatement("INSERT INTO #blobsetnull3 (data) VALUES (?)");
0880:
0881: // Test PreparedStatement.setBytes()
0882: pstmt.setBytes(1, null);
0883: assertEquals(pstmt.executeUpdate(), 1);
0884:
0885: pstmt.close();
0886:
0887: Statement stmt2 = con.createStatement();
0888: ResultSet rs = stmt2
0889: .executeQuery("SELECT data FROM #blobsetnull3");
0890:
0891: assertTrue(rs.next());
0892:
0893: // Test ResultSet.getBinaryStream()
0894: assertNull(rs.getBinaryStream(1));
0895: assertTrue(rs.wasNull());
0896:
0897: // Test ResultSet.getBlob()
0898: assertNull(rs.getBlob(1));
0899: assertTrue(rs.wasNull());
0900:
0901: // Test ResultSet.getBytes()
0902: assertNull(rs.getBytes(1));
0903: assertTrue(rs.wasNull());
0904:
0905: // Test ResultSet.getObject()
0906: assertNull(rs.getObject(1));
0907: assertTrue(rs.wasNull());
0908:
0909: assertFalse(rs.next());
0910: stmt2.close();
0911: rs.close();
0912: }
0913:
0914: /**
0915: * Test for bug [985956] Cannot setObject(null) on image.
0916: */
0917: public void testBlobSetNull4() throws Exception {
0918: Statement stmt = con.createStatement();
0919: stmt.execute("CREATE TABLE #blobsetnull4 (data IMAGE NULL)");
0920: stmt.close();
0921:
0922: PreparedStatement pstmt = con
0923: .prepareStatement("INSERT INTO #blobsetnull4 (data) VALUES (?)");
0924:
0925: // Test PreparedStatement.setObject(int,Object)
0926: pstmt.setObject(1, null);
0927: assertEquals(pstmt.executeUpdate(), 1);
0928:
0929: pstmt.close();
0930:
0931: Statement stmt2 = con.createStatement();
0932: ResultSet rs = stmt2
0933: .executeQuery("SELECT data FROM #blobsetnull4");
0934:
0935: assertTrue(rs.next());
0936:
0937: // Test ResultSet.getBinaryStream()
0938: assertNull(rs.getBinaryStream(1));
0939: assertTrue(rs.wasNull());
0940:
0941: // Test ResultSet.getBlob()
0942: assertNull(rs.getBlob(1));
0943: assertTrue(rs.wasNull());
0944:
0945: // Test ResultSet.getBytes()
0946: assertNull(rs.getBytes(1));
0947: assertTrue(rs.wasNull());
0948:
0949: // Test ResultSet.getObject()
0950: assertNull(rs.getObject(1));
0951: assertTrue(rs.wasNull());
0952:
0953: assertFalse(rs.next());
0954: stmt2.close();
0955: rs.close();
0956: }
0957:
0958: public void testBlobSetNull5() throws Exception {
0959: Statement stmt = con.createStatement();
0960: stmt.execute("CREATE TABLE #blobsetnull5 (data IMAGE NULL)");
0961: stmt.close();
0962:
0963: PreparedStatement pstmt = con
0964: .prepareStatement("INSERT INTO #blobsetnull5 (data) VALUES (?)");
0965:
0966: // Test PreparedStatement.setObject(int,Object,int)
0967: pstmt.setObject(1, null, Types.BLOB);
0968: assertEquals(pstmt.executeUpdate(), 1);
0969:
0970: pstmt.close();
0971:
0972: Statement stmt2 = con.createStatement();
0973: ResultSet rs = stmt2
0974: .executeQuery("SELECT data FROM #blobsetnull5");
0975:
0976: assertTrue(rs.next());
0977:
0978: // Test ResultSet.getBinaryStream()
0979: assertNull(rs.getBinaryStream(1));
0980: assertTrue(rs.wasNull());
0981:
0982: // Test ResultSet.getBlob()
0983: assertNull(rs.getBlob(1));
0984: assertTrue(rs.wasNull());
0985:
0986: // Test ResultSet.getBytes()
0987: assertNull(rs.getBytes(1));
0988: assertTrue(rs.wasNull());
0989:
0990: // Test ResultSet.getObject()
0991: assertNull(rs.getObject(1));
0992: assertTrue(rs.wasNull());
0993:
0994: assertFalse(rs.next());
0995: stmt2.close();
0996: rs.close();
0997: }
0998:
0999: public void testBlobSetNull6() throws Exception {
1000: Statement stmt = con.createStatement();
1001: stmt.execute("CREATE TABLE #blobsetnull6 (data IMAGE NULL)");
1002: stmt.close();
1003:
1004: PreparedStatement pstmt = con
1005: .prepareStatement("INSERT INTO #blobsetnull6 (data) VALUES (?)");
1006:
1007: // Test PreparedStatement.setNull()
1008: pstmt.setNull(1, Types.BLOB);
1009: assertEquals(pstmt.executeUpdate(), 1);
1010:
1011: pstmt.close();
1012:
1013: Statement stmt2 = con.createStatement();
1014: ResultSet rs = stmt2
1015: .executeQuery("SELECT data FROM #blobsetnull6");
1016:
1017: assertTrue(rs.next());
1018:
1019: // Test ResultSet.getBinaryStream()
1020: assertNull(rs.getBinaryStream(1));
1021: assertTrue(rs.wasNull());
1022:
1023: // Test ResultSet.getBlob()
1024: assertNull(rs.getBlob(1));
1025: assertTrue(rs.wasNull());
1026:
1027: // Test ResultSet.getBytes()
1028: assertNull(rs.getBytes(1));
1029: assertTrue(rs.wasNull());
1030:
1031: // Test ResultSet.getObject()
1032: assertNull(rs.getObject(1));
1033: assertTrue(rs.wasNull());
1034:
1035: assertFalse(rs.next());
1036: stmt2.close();
1037: rs.close();
1038: }
1039:
1040: public void testBlobSetNull7() throws Exception {
1041: Statement stmt = con.createStatement();
1042: stmt
1043: .execute("CREATE TABLE #blobsetnull7 (id NUMERIC IDENTITY, data IMAGE NULL, "
1044: + "CONSTRAINT pk_blobsetnull7 PRIMARY KEY CLUSTERED (id))");
1045: stmt.close();
1046:
1047: stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
1048: ResultSet.CONCUR_UPDATABLE);
1049: ResultSet rs = stmt
1050: .executeQuery("SELECT id, data FROM #blobsetnull7");
1051:
1052: rs.moveToInsertRow();
1053:
1054: // Test ResultSet.updateBinaryStream()
1055: rs.updateBinaryStream(2, null, 0);
1056:
1057: rs.insertRow();
1058:
1059: stmt.close();
1060: rs.close();
1061:
1062: Statement stmt2 = con.createStatement();
1063: ResultSet rs2 = stmt2
1064: .executeQuery("SELECT data FROM #blobsetnull7");
1065:
1066: assertTrue(rs2.next());
1067:
1068: // Test ResultSet.getBinaryStream()
1069: assertNull(rs2.getBinaryStream(1));
1070: assertTrue(rs2.wasNull());
1071:
1072: // Test ResultSet.getBlob()
1073: assertNull(rs2.getBlob(1));
1074: assertTrue(rs2.wasNull());
1075:
1076: // Test ResultSet.getBytes()
1077: assertNull(rs2.getBytes(1));
1078: assertTrue(rs2.wasNull());
1079:
1080: // Test ResultSet.getObject()
1081: assertNull(rs2.getObject(1));
1082: assertTrue(rs2.wasNull());
1083:
1084: assertFalse(rs2.next());
1085: stmt2.close();
1086: rs2.close();
1087: }
1088:
1089: public void testBlobSetNull8() throws Exception {
1090: Statement stmt = con.createStatement();
1091: stmt
1092: .execute("CREATE TABLE #blobsetnull8 (id NUMERIC IDENTITY, data IMAGE NULL, "
1093: + "CONSTRAINT pk_blobsetnull8 PRIMARY KEY CLUSTERED (id))");
1094: stmt.close();
1095:
1096: stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
1097: ResultSet.CONCUR_UPDATABLE);
1098: ResultSet rs = stmt
1099: .executeQuery("SELECT id, data FROM #blobsetnull8");
1100:
1101: rs.moveToInsertRow();
1102:
1103: // Test ResultSet.updateBlob()
1104: rs.updateBlob(2, null);
1105:
1106: rs.insertRow();
1107:
1108: stmt.close();
1109: rs.close();
1110:
1111: Statement stmt2 = con.createStatement();
1112: ResultSet rs2 = stmt2
1113: .executeQuery("SELECT data FROM #blobsetnull8");
1114:
1115: assertTrue(rs2.next());
1116:
1117: // Test ResultSet.getBinaryStream()
1118: assertNull(rs2.getBinaryStream(1));
1119: assertTrue(rs2.wasNull());
1120:
1121: // Test ResultSet.getBlob()
1122: assertNull(rs2.getBlob(1));
1123: assertTrue(rs2.wasNull());
1124:
1125: // Test ResultSet.getBytes()
1126: assertNull(rs2.getBytes(1));
1127: assertTrue(rs2.wasNull());
1128:
1129: // Test ResultSet.getObject()
1130: assertNull(rs2.getObject(1));
1131: assertTrue(rs2.wasNull());
1132:
1133: assertFalse(rs2.next());
1134: stmt2.close();
1135: rs2.close();
1136: }
1137:
1138: public void testBlobSetNull9() throws Exception {
1139: Statement stmt = con.createStatement();
1140: stmt
1141: .execute("CREATE TABLE #blobsetnull9 (id NUMERIC IDENTITY, data IMAGE NULL, "
1142: + "CONSTRAINT pk_blobsetnull9 PRIMARY KEY CLUSTERED (id))");
1143: stmt.close();
1144:
1145: stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
1146: ResultSet.CONCUR_UPDATABLE);
1147: ResultSet rs = stmt
1148: .executeQuery("SELECT id, data FROM #blobsetnull9");
1149:
1150: rs.moveToInsertRow();
1151:
1152: // Test ResultSet.updateBytes()
1153: rs.updateBytes(2, null);
1154:
1155: rs.insertRow();
1156:
1157: stmt.close();
1158: rs.close();
1159:
1160: Statement stmt2 = con.createStatement();
1161: ResultSet rs2 = stmt2
1162: .executeQuery("SELECT data FROM #blobsetnull9");
1163:
1164: assertTrue(rs2.next());
1165:
1166: // Test ResultSet.getBinaryStream()
1167: assertNull(rs2.getBinaryStream(1));
1168: assertTrue(rs2.wasNull());
1169:
1170: // Test ResultSet.getBlob()
1171: assertNull(rs2.getBlob(1));
1172: assertTrue(rs2.wasNull());
1173:
1174: // Test ResultSet.getBytes()
1175: assertNull(rs2.getBytes(1));
1176: assertTrue(rs2.wasNull());
1177:
1178: // Test ResultSet.getObject()
1179: assertNull(rs2.getObject(1));
1180: assertTrue(rs2.wasNull());
1181:
1182: assertFalse(rs2.next());
1183: stmt2.close();
1184: rs2.close();
1185: }
1186:
1187: public void testBlobSetNull10() throws Exception {
1188: Statement stmt = con.createStatement();
1189: stmt
1190: .execute("CREATE TABLE #blobsetnull10 (id NUMERIC IDENTITY, data IMAGE NULL, "
1191: + "CONSTRAINT pk_blobsetnull10 PRIMARY KEY CLUSTERED (id))");
1192: stmt.close();
1193:
1194: stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
1195: ResultSet.CONCUR_UPDATABLE);
1196: ResultSet rs = stmt
1197: .executeQuery("SELECT id, data FROM #blobsetnull10");
1198:
1199: rs.moveToInsertRow();
1200:
1201: // Test ResultSet.updateObject()
1202: rs.updateObject(2, null);
1203:
1204: rs.insertRow();
1205:
1206: stmt.close();
1207: rs.close();
1208:
1209: Statement stmt2 = con.createStatement();
1210: ResultSet rs2 = stmt2
1211: .executeQuery("SELECT data FROM #blobsetnull10");
1212:
1213: assertTrue(rs2.next());
1214:
1215: // Test ResultSet.getBinaryStream()
1216: assertNull(rs2.getBinaryStream(1));
1217: assertTrue(rs2.wasNull());
1218:
1219: // Test ResultSet.getBlob()
1220: assertNull(rs2.getBlob(1));
1221: assertTrue(rs2.wasNull());
1222:
1223: // Test ResultSet.getBytes()
1224: assertNull(rs2.getBytes(1));
1225: assertTrue(rs2.wasNull());
1226:
1227: // Test ResultSet.getObject()
1228: assertNull(rs2.getObject(1));
1229: assertTrue(rs2.wasNull());
1230:
1231: assertFalse(rs2.next());
1232: stmt2.close();
1233: rs2.close();
1234: }
1235:
1236: public void testBlobSetNull11() throws Exception {
1237: Statement stmt = con.createStatement();
1238: stmt
1239: .execute("CREATE TABLE #blobsetnull11 (id NUMERIC IDENTITY, data IMAGE NULL, "
1240: + "CONSTRAINT pk_blobsetnull11 PRIMARY KEY CLUSTERED (id))");
1241: stmt.close();
1242:
1243: stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
1244: ResultSet.CONCUR_UPDATABLE);
1245: ResultSet rs = stmt
1246: .executeQuery("SELECT id, data FROM #blobsetnull11");
1247:
1248: rs.moveToInsertRow();
1249:
1250: // Test ResultSet.updateNull()
1251: rs.updateNull(2);
1252:
1253: rs.insertRow();
1254:
1255: stmt.close();
1256: rs.close();
1257:
1258: Statement stmt2 = con.createStatement();
1259: ResultSet rs2 = stmt2
1260: .executeQuery("SELECT data FROM #blobsetnull11");
1261:
1262: assertTrue(rs2.next());
1263:
1264: // Test ResultSet.getBinaryStream()
1265: assertNull(rs2.getBinaryStream(1));
1266: assertTrue(rs2.wasNull());
1267:
1268: // Test ResultSet.getBlob()
1269: assertNull(rs2.getBlob(1));
1270: assertTrue(rs2.wasNull());
1271:
1272: // Test ResultSet.getBytes()
1273: assertNull(rs2.getBytes(1));
1274: assertTrue(rs2.wasNull());
1275:
1276: // Test ResultSet.getObject()
1277: assertNull(rs2.getObject(1));
1278: assertTrue(rs2.wasNull());
1279:
1280: assertFalse(rs2.next());
1281: stmt2.close();
1282: rs2.close();
1283: }
1284:
1285: /**
1286: * Test for bug [989399] blob.getBytes() from 0.
1287: */
1288: public void testBlobGetBytes1() throws Exception {
1289: byte[] data = getBlobTestData();
1290:
1291: Statement stmt = con.createStatement();
1292: stmt.execute("CREATE TABLE #blobgetbytes1 (data IMAGE)");
1293: stmt.close();
1294:
1295: PreparedStatement pstmt = con
1296: .prepareStatement("INSERT INTO #blobgetbytes1 (data) VALUES (?)");
1297:
1298: // Test PreparedStatement.setBytes()
1299: pstmt.setBytes(1, data);
1300: assertEquals(pstmt.executeUpdate(), 1);
1301:
1302: pstmt.close();
1303:
1304: Statement stmt2 = con.createStatement();
1305: ResultSet rs = stmt2
1306: .executeQuery("SELECT data FROM #blobgetbytes1");
1307:
1308: assertTrue(rs.next());
1309:
1310: // Test ResultSet.getBlob()
1311: Blob blob = rs.getBlob(1);
1312:
1313: assertNotNull(blob);
1314:
1315: // Test Blob.getBytes()
1316: assertTrue(Arrays.equals(data, blob.getBytes(1L, (int) blob
1317: .length())));
1318:
1319: assertFalse(rs.next());
1320: stmt2.close();
1321: rs.close();
1322: }
1323:
1324: public void testBlobGetBytes2() throws Exception {
1325: byte[] data = getBlobTestData();
1326:
1327: Statement stmt = con.createStatement();
1328: stmt.execute("CREATE TABLE #blobgetbytes2 (data IMAGE)");
1329: stmt.close();
1330:
1331: PreparedStatement pstmt = con
1332: .prepareStatement("INSERT INTO #blobgetbytes2 (data) VALUES (?)");
1333:
1334: // Test PreparedStatement.setBytes()
1335: pstmt.setBytes(1, data);
1336: assertEquals(pstmt.executeUpdate(), 1);
1337:
1338: pstmt.close();
1339:
1340: Statement stmt2 = con.createStatement();
1341: ResultSet rs = stmt2
1342: .executeQuery("SELECT data FROM #blobgetbytes2");
1343:
1344: assertTrue(rs.next());
1345:
1346: // Test ResultSet.getBlob()
1347: Blob blob = rs.getBlob(1);
1348:
1349: assertNotNull(blob);
1350:
1351: byte[] tmpData = new byte[data.length / 2];
1352:
1353: System.arraycopy(data, 0, tmpData, 0, tmpData.length);
1354:
1355: // Test Blob.getBytes()
1356: assertTrue(Arrays.equals(tmpData, blob.getBytes(1L,
1357: tmpData.length)));
1358:
1359: assertFalse(rs.next());
1360: stmt2.close();
1361: rs.close();
1362: }
1363:
1364: public void testBlobGetBytes3() throws Exception {
1365: byte[] data = getBlobTestData();
1366:
1367: Statement stmt = con.createStatement();
1368: stmt.execute("CREATE TABLE #blobgetbytes3 (data IMAGE)");
1369: stmt.close();
1370:
1371: PreparedStatement pstmt = con
1372: .prepareStatement("INSERT INTO #blobgetbytes3 (data) VALUES (?)");
1373:
1374: // Test PreparedStatement.setBytes()
1375: pstmt.setBytes(1, data);
1376: assertEquals(pstmt.executeUpdate(), 1);
1377:
1378: pstmt.close();
1379:
1380: Statement stmt2 = con.createStatement();
1381: ResultSet rs = stmt2
1382: .executeQuery("SELECT data FROM #blobgetbytes3");
1383:
1384: assertTrue(rs.next());
1385:
1386: // Test ResultSet.getBlob()
1387: Blob blob = rs.getBlob(1);
1388:
1389: assertNotNull(blob);
1390:
1391: byte[] tmpData = new byte[data.length / 2];
1392:
1393: // Offset data copy by 1
1394: System.arraycopy(data, 1, tmpData, 0, tmpData.length);
1395:
1396: // Test Blob.getBytes()
1397: assertTrue(Arrays.equals(tmpData, blob.getBytes(2L,
1398: tmpData.length)));
1399:
1400: assertFalse(rs.next());
1401: stmt2.close();
1402: rs.close();
1403: }
1404:
1405: public void testBlobLength1() throws Exception {
1406: byte[] data = getBlobTestData();
1407:
1408: Statement stmt = con.createStatement();
1409: stmt.execute("CREATE TABLE #bloblength1 (data IMAGE)");
1410: stmt.close();
1411:
1412: PreparedStatement pstmt = con
1413: .prepareStatement("INSERT INTO #bloblength1 (data) VALUES (?)");
1414:
1415: // Test PreparedStatement.setBytes()
1416: pstmt.setBytes(1, data);
1417: assertEquals(pstmt.executeUpdate(), 1);
1418:
1419: pstmt.close();
1420:
1421: Statement stmt2 = con.createStatement();
1422: ResultSet rs = stmt2
1423: .executeQuery("SELECT data FROM #bloblength1");
1424:
1425: assertTrue(rs.next());
1426:
1427: // Test ResultSet.getBlob()
1428: Blob blob = rs.getBlob(1);
1429:
1430: assertNotNull(blob);
1431:
1432: // Test Blob.length()
1433: assertEquals(data.length, blob.length());
1434:
1435: assertFalse(rs.next());
1436: stmt2.close();
1437: rs.close();
1438: }
1439:
1440: public void testBlobTruncate1() throws Exception {
1441: byte[] data = getBlobTestData();
1442:
1443: Statement stmt = con.createStatement();
1444: stmt.execute("CREATE TABLE #blobtruncate1 (data IMAGE)");
1445: stmt.close();
1446:
1447: PreparedStatement pstmt = con
1448: .prepareStatement("INSERT INTO #blobtruncate1 (data) VALUES (?)");
1449:
1450: // Test PreparedStatement.setBytes()
1451: pstmt.setBytes(1, data);
1452: assertEquals(pstmt.executeUpdate(), 1);
1453:
1454: pstmt.close();
1455:
1456: Statement stmt2 = con.createStatement();
1457: ResultSet rs = stmt2
1458: .executeQuery("SELECT data FROM #blobtruncate1");
1459:
1460: assertTrue(rs.next());
1461:
1462: // Test ResultSet.getBlob()
1463: Blob blob = rs.getBlob(1);
1464:
1465: assertNotNull(blob);
1466:
1467: byte[] tmpData = new byte[data.length / 2];
1468:
1469: System.arraycopy(data, 0, tmpData, 0, tmpData.length);
1470:
1471: // Test Blob.truncate()
1472: blob.truncate(tmpData.length);
1473: assertEquals(tmpData.length, blob.length());
1474:
1475: // Test Blob.getBytes()
1476: assertTrue(Arrays.equals(tmpData, blob.getBytes(1L, (int) blob
1477: .length())));
1478:
1479: assertFalse(rs.next());
1480: stmt2.close();
1481: rs.close();
1482: }
1483:
1484: /**
1485: * Test for bug [1062395] Empty (but not null) blobs should return byte[0].
1486: */
1487: public void testBlobEmpty() throws Exception {
1488: Statement stmt = con.createStatement();
1489:
1490: assertEquals(0, stmt
1491: .executeUpdate("CREATE TABLE #blobEmpty (data IMAGE)"));
1492: assertEquals(
1493: 1,
1494: stmt
1495: .executeUpdate("INSERT INTO #blobEmpty (data) values ('')"));
1496:
1497: ResultSet rs = stmt.executeQuery("SELECT * FROM #blobEmpty");
1498: assertTrue(rs.next());
1499: Blob blob = rs.getBlob(1);
1500: assertEquals(0, blob.length());
1501: assertEquals(0, blob.getBytes(1, 0).length);
1502:
1503: rs.close();
1504: stmt.close();
1505: }
1506:
1507: /*************************************************************************
1508: *************************************************************************
1509: ** CLOB TESTS **
1510: *************************************************************************
1511: *************************************************************************/
1512:
1513: public void testClobGet1() throws Exception {
1514: String data = getClobTestData();
1515:
1516: Statement stmt = con.createStatement();
1517: stmt.execute("CREATE TABLE #clobget1 (data TEXT)");
1518: stmt.close();
1519:
1520: PreparedStatement pstmt = con
1521: .prepareStatement("INSERT INTO #clobget1 (data) VALUES (?)");
1522:
1523: pstmt.setString(1, data);
1524: assertEquals(pstmt.executeUpdate(), 1);
1525:
1526: pstmt.close();
1527:
1528: Statement stmt2 = con.createStatement();
1529: ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobget1");
1530:
1531: assertTrue(rs.next());
1532:
1533: // Test ResultSet.getString()
1534: assertTrue(data.equals(rs.getString(1)));
1535:
1536: // Test ResultSet.getAsciiStream()
1537: InputStream is = rs.getAsciiStream(1);
1538: compareInputStreams(new ByteArrayInputStream(data
1539: .getBytes("ASCII")), is);
1540:
1541: // Test ResultSet.getUnicodeStream(()
1542: InputStream is2 = rs.getUnicodeStream(1);
1543: compareInputStreams(new ByteArrayInputStream(data
1544: .getBytes("UTF-16BE")), is2);
1545:
1546: // Test ResultSet.getCharacterStream()
1547: Reader rdr = rs.getCharacterStream(1);
1548: compareReaders(new StringReader(data), rdr);
1549:
1550: // Test ResultSet.getClob()
1551: Clob clob = rs.getClob(1);
1552:
1553: assertNotNull(clob);
1554:
1555: // Test Clob.length()
1556: assertEquals(clob.length(), data.length());
1557:
1558: // Test Clob.getSubString(0, length); should fail
1559: try {
1560: clob.getSubString(0L, (int) clob.length());
1561: fail("Clob.getSubString(0, length) should fail.");
1562: } catch (SQLException ex) {
1563: assertEquals("HY090", ex.getSQLState());
1564: }
1565:
1566: // Test Clob.getSubString()
1567: assertTrue(data.equals(clob.getSubString(1L, (int) clob
1568: .length())));
1569:
1570: // Test Clob.getAsciiStream()
1571: InputStream is3 = clob.getAsciiStream();
1572: compareInputStreams(new ByteArrayInputStream(data
1573: .getBytes("ASCII")), is3);
1574:
1575: // Test Clob.getCharacterStream()
1576: Reader rdr2 = rs.getCharacterStream(1);
1577: compareReaders(new StringReader(data), rdr2);
1578:
1579: assertFalse(rs.next());
1580: stmt2.close();
1581: rs.close();
1582: }
1583:
1584: public void testClobGet2() throws Exception {
1585: String data = getClobTestData();
1586:
1587: Statement stmt = con.createStatement();
1588: stmt.execute("CREATE TABLE #clobget2 (data TEXT)");
1589: stmt.close();
1590:
1591: PreparedStatement pstmt = con
1592: .prepareStatement("INSERT INTO #clobget2 (data) VALUES (?)");
1593:
1594: // Test PreparedStatement.setCharacterStream()
1595: pstmt.setCharacterStream(1, new StringReader(data), data
1596: .length());
1597: assertEquals(pstmt.executeUpdate(), 1);
1598:
1599: pstmt.close();
1600:
1601: Statement stmt2 = con.createStatement();
1602: ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobget2");
1603:
1604: assertTrue(rs.next());
1605:
1606: // Test ResultSet.getObject() - Clob
1607: Object result = rs.getObject(1);
1608:
1609: assertTrue(result instanceof Clob);
1610:
1611: Clob clob = (Clob) result;
1612:
1613: assertEquals(data.length(), clob.length());
1614:
1615: // Test Clob.getSubString()
1616: assertTrue(data.equals(clob.getSubString(1L, (int) clob
1617: .length())));
1618:
1619: assertFalse(rs.next());
1620: stmt2.close();
1621: rs.close();
1622: }
1623:
1624: public void testClobSet1() throws Exception {
1625: String data = getClobTestData();
1626:
1627: Statement stmt = con.createStatement();
1628: stmt.execute("CREATE TABLE #clobset1 (data TEXT)");
1629: stmt.close();
1630:
1631: PreparedStatement pstmt = con
1632: .prepareStatement("INSERT INTO #clobset1 (data) VALUES (?)");
1633:
1634: // Test PreparedStatement.setAsciiStream()
1635: pstmt.setAsciiStream(1, new ByteArrayInputStream(data
1636: .getBytes("ASCII")), data.length());
1637: assertEquals(pstmt.executeUpdate(), 1);
1638:
1639: pstmt.close();
1640:
1641: Statement stmt2 = con.createStatement();
1642: ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobset1");
1643:
1644: assertTrue(rs.next());
1645:
1646: // Test ResultSet.getString()
1647: assertTrue(data.equals(rs.getString(1)));
1648:
1649: assertFalse(rs.next());
1650: stmt2.close();
1651: rs.close();
1652: }
1653:
1654: public void testClobSet2() throws Exception {
1655: String data = getClobTestData();
1656:
1657: Statement stmt = con.createStatement();
1658: stmt.execute("CREATE TABLE #clobset2 (data TEXT)");
1659: stmt.close();
1660:
1661: PreparedStatement pstmt = con
1662: .prepareStatement("INSERT INTO #clobset2 (data) VALUES (?)");
1663:
1664: // Test PreparedStatement.setCharacterStream()
1665: pstmt.setCharacterStream(1, new StringReader(data), data
1666: .length());
1667: assertEquals(pstmt.executeUpdate(), 1);
1668:
1669: pstmt.close();
1670:
1671: Statement stmt2 = con.createStatement();
1672: ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobset2");
1673:
1674: assertTrue(rs.next());
1675:
1676: // Test ResultSet.getString()
1677: assertTrue(data.equals(rs.getString(1)));
1678:
1679: assertFalse(rs.next());
1680: stmt2.close();
1681: rs.close();
1682: }
1683:
1684: public void testClobSet3() throws Exception {
1685: String data = getClobTestData();
1686:
1687: Statement stmt = con.createStatement();
1688: stmt.execute("CREATE TABLE #clobset3 (data TEXT)");
1689: stmt.close();
1690:
1691: PreparedStatement pstmt = con
1692: .prepareStatement("INSERT INTO #clobset3 (data) VALUES (?)");
1693:
1694: // Test PreparedStatement.setString()
1695: pstmt.setString(1, data);
1696: assertEquals(pstmt.executeUpdate(), 1);
1697:
1698: pstmt.close();
1699:
1700: Statement stmt2 = con.createStatement();
1701: ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobset3");
1702:
1703: assertTrue(rs.next());
1704:
1705: Clob clob = rs.getClob(1);
1706:
1707: data = getNewClobTestData();
1708:
1709: // Test Clob.setBytes()
1710: clob.setString(1, data);
1711:
1712: assertTrue(data.equals(clob.getSubString(1L, (int) clob
1713: .length())));
1714:
1715: assertFalse(rs.next());
1716:
1717: PreparedStatement pstmt2 = con
1718: .prepareStatement("UPDATE #clobset3 SET data = ?");
1719:
1720: // Test PreparedStatement.setClob()
1721: pstmt2.setClob(1, clob);
1722: assertEquals(1, pstmt2.executeUpdate());
1723:
1724: pstmt2.close();
1725:
1726: stmt2.close();
1727: rs.close();
1728:
1729: Statement stmt3 = con.createStatement();
1730: ResultSet rs2 = stmt3
1731: .executeQuery("SELECT data FROM #clobset3");
1732:
1733: assertTrue(rs2.next());
1734:
1735: // Test ResultSet.getString()
1736: assertTrue(data.equals(rs2.getString(1)));
1737:
1738: assertFalse(rs2.next());
1739: stmt3.close();
1740: rs2.close();
1741: }
1742:
1743: public void testClobSet4() throws Exception {
1744: String data = getClobTestData();
1745:
1746: Statement stmt = con.createStatement();
1747: stmt.execute("CREATE TABLE #clobset4 (data TEXT)");
1748: stmt.close();
1749:
1750: PreparedStatement pstmt = con
1751: .prepareStatement("INSERT INTO #clobset4 (data) VALUES (?)");
1752:
1753: // Test PreparedStatement.setUnicodeStream()
1754: pstmt.setUnicodeStream(1, new ByteArrayInputStream(data
1755: .getBytes("UTF-16BE")), data.length() * 2);
1756: assertEquals(pstmt.executeUpdate(), 1);
1757:
1758: pstmt.close();
1759:
1760: Statement stmt2 = con.createStatement();
1761: ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobset4");
1762:
1763: assertTrue(rs.next());
1764:
1765: // Test ResultSet.getString()
1766: assertTrue(data.equals(rs.getString(1)));
1767:
1768: assertFalse(rs.next());
1769: stmt2.close();
1770: rs.close();
1771: }
1772:
1773: public void testClobSet5() throws Exception {
1774: String data = getClobTestData();
1775:
1776: Statement stmt = con.createStatement();
1777: stmt.execute("CREATE TABLE #clobset5 (data TEXT)");
1778: stmt.close();
1779:
1780: PreparedStatement pstmt = con
1781: .prepareStatement("INSERT INTO #clobset5 (data) VALUES (?)");
1782:
1783: // Test PreparedStatement.setObject(int,String)
1784: pstmt.setObject(1, data);
1785: assertEquals(pstmt.executeUpdate(), 1);
1786:
1787: pstmt.close();
1788:
1789: Statement stmt2 = con.createStatement();
1790: ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobset5");
1791:
1792: assertTrue(rs.next());
1793:
1794: // Test ResultSet.getString()
1795: assertTrue(data.equals(rs.getString(1)));
1796:
1797: assertFalse(rs.next());
1798: stmt2.close();
1799: rs.close();
1800: }
1801:
1802: public void testClobSet6() throws Exception {
1803: String data = getClobTestData();
1804:
1805: Statement stmt = con.createStatement();
1806: stmt.execute("CREATE TABLE #clobset6 (data TEXT)");
1807: stmt.close();
1808:
1809: PreparedStatement pstmt = con
1810: .prepareStatement("INSERT INTO #clobset6 (data) VALUES (?)");
1811:
1812: // Test PreparedStatement.setString()
1813: pstmt.setString(1, data);
1814: assertEquals(pstmt.executeUpdate(), 1);
1815:
1816: pstmt.close();
1817:
1818: Statement stmt2 = con.createStatement();
1819: ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobset6");
1820:
1821: assertTrue(rs.next());
1822:
1823: Clob clob = rs.getClob(1);
1824:
1825: data = getNewClobTestData();
1826:
1827: // Test Clob.setBytes()
1828: clob.setString(1, data);
1829:
1830: assertTrue(data.equals(clob.getSubString(1L, (int) clob
1831: .length())));
1832:
1833: assertFalse(rs.next());
1834:
1835: PreparedStatement pstmt2 = con
1836: .prepareStatement("UPDATE #clobset6 SET data = ?");
1837:
1838: // Test PreparedStatement.setObject(int,Clob)
1839: pstmt2.setObject(1, clob);
1840: assertEquals(1, pstmt2.executeUpdate());
1841:
1842: pstmt2.close();
1843:
1844: stmt2.close();
1845: rs.close();
1846:
1847: Statement stmt3 = con.createStatement();
1848: ResultSet rs2 = stmt3
1849: .executeQuery("SELECT data FROM #clobset6");
1850:
1851: assertTrue(rs2.next());
1852:
1853: // Test ResultSet.getString()
1854: assertTrue(data.equals(rs2.getString(1)));
1855:
1856: assertFalse(rs2.next());
1857: stmt3.close();
1858: rs2.close();
1859: }
1860:
1861: public void testClobSet7() throws Exception {
1862: String data = getClobTestData();
1863:
1864: Statement stmt = con.createStatement();
1865: stmt.execute("CREATE TABLE #clobset7 (data TEXT)");
1866: stmt.close();
1867:
1868: PreparedStatement pstmt = con
1869: .prepareStatement("INSERT INTO #clobset7 (data) VALUES (?)");
1870:
1871: // Test PreparedStatement.setObject(int,String,int)
1872: pstmt.setObject(1, data, Types.LONGVARCHAR);
1873: assertEquals(pstmt.executeUpdate(), 1);
1874:
1875: pstmt.close();
1876:
1877: Statement stmt2 = con.createStatement();
1878: ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobset7");
1879:
1880: assertTrue(rs.next());
1881:
1882: // Test ResultSet.getString()
1883: assertTrue(data.equals(rs.getString(1)));
1884:
1885: assertFalse(rs.next());
1886: stmt2.close();
1887: rs.close();
1888: }
1889:
1890: public void testClobSet8() throws Exception {
1891: String data = getClobTestData();
1892:
1893: Statement stmt = con.createStatement();
1894: stmt.execute("CREATE TABLE #clobset8 (data TEXT)");
1895: stmt.close();
1896:
1897: PreparedStatement pstmt = con
1898: .prepareStatement("INSERT INTO #clobset8 (data) VALUES (?)");
1899:
1900: // Test PreparedStatement.setString()
1901: pstmt.setString(1, data);
1902: assertEquals(1, pstmt.executeUpdate());
1903:
1904: pstmt.close();
1905:
1906: Statement stmt2 = con.createStatement();
1907: ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobset8");
1908:
1909: assertTrue(rs.next());
1910:
1911: Clob clob = rs.getClob(1);
1912:
1913: data = getNewClobTestData();
1914:
1915: // Test Clob.setBytes()
1916: clob.setString(1, data);
1917:
1918: assertTrue(data.equals(clob.getSubString(1L, (int) clob
1919: .length())));
1920:
1921: assertFalse(rs.next());
1922:
1923: PreparedStatement pstmt2 = con
1924: .prepareStatement("UPDATE #clobset8 SET data = ?");
1925:
1926: // Test PreparedStatement.setObject(int,Clob,int)
1927: pstmt2.setObject(1, clob, Types.CLOB);
1928: assertEquals(1, pstmt2.executeUpdate());
1929:
1930: pstmt2.close();
1931:
1932: stmt2.close();
1933: rs.close();
1934:
1935: Statement stmt3 = con.createStatement();
1936: ResultSet rs2 = stmt3
1937: .executeQuery("SELECT data FROM #clobset8");
1938:
1939: assertTrue(rs2.next());
1940:
1941: // Test ResultSet.getString()
1942: assertTrue(data.equals(rs2.getString(1)));
1943:
1944: assertFalse(rs2.next());
1945: stmt3.close();
1946: rs2.close();
1947: }
1948:
1949: public void testClobUpdate1() throws Exception {
1950: String data = getClobTestData();
1951:
1952: Statement stmt = con.createStatement();
1953: stmt
1954: .execute("CREATE TABLE #clobupdate1 (id NUMERIC IDENTITY, data TEXT, "
1955: + "CONSTRAINT pk_clobupdate1 PRIMARY KEY CLUSTERED (id))");
1956: stmt.close();
1957:
1958: stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
1959: ResultSet.CONCUR_UPDATABLE);
1960: ResultSet rs = stmt
1961: .executeQuery("SELECT id, data FROM #clobupdate1");
1962:
1963: rs.moveToInsertRow();
1964:
1965: // Test ResultSet.updateString()
1966: rs.updateString(2, data);
1967:
1968: rs.insertRow();
1969:
1970: stmt.close();
1971: rs.close();
1972:
1973: Statement stmt2 = con.createStatement();
1974: ResultSet rs2 = stmt2
1975: .executeQuery("SELECT data FROM #clobupdate1");
1976:
1977: assertTrue(rs2.next());
1978:
1979: // Test ResultSet.getString()
1980: assertTrue(data.equals(rs2.getString(1)));
1981:
1982: assertFalse(rs2.next());
1983: stmt2.close();
1984: rs2.close();
1985: }
1986:
1987: public void testClobUpdate2() throws Exception {
1988: String data = getClobTestData();
1989:
1990: Statement stmt = con.createStatement();
1991: stmt
1992: .execute("CREATE TABLE #clobupdate2 (id NUMERIC IDENTITY, data TEXT, "
1993: + "CONSTRAINT pk_clobupdate2 PRIMARY KEY CLUSTERED (id))");
1994: stmt.close();
1995:
1996: stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
1997: ResultSet.CONCUR_UPDATABLE);
1998: ResultSet rs = stmt
1999: .executeQuery("SELECT id, data FROM #clobupdate2");
2000:
2001: rs.moveToInsertRow();
2002:
2003: // Test ResultSet.updateAsciiStream()
2004: rs.updateAsciiStream(2, new ByteArrayInputStream(data
2005: .getBytes("ASCII")), data.length());
2006:
2007: rs.insertRow();
2008:
2009: stmt.close();
2010: rs.close();
2011:
2012: Statement stmt2 = con.createStatement();
2013: ResultSet rs2 = stmt2
2014: .executeQuery("SELECT data FROM #clobupdate2");
2015:
2016: assertTrue(rs2.next());
2017:
2018: // Test ResultSet.getString()
2019: assertTrue(data.equals(rs2.getString(1)));
2020:
2021: assertFalse(rs2.next());
2022: stmt2.close();
2023: rs2.close();
2024: }
2025:
2026: public void testClobUpdate3() throws Exception {
2027: String data = getClobTestData();
2028:
2029: Statement stmt = con.createStatement();
2030: stmt
2031: .execute("CREATE TABLE #clobupdate3 (id NUMERIC IDENTITY, data TEXT, "
2032: + "CONSTRAINT pk_clobupdate3 PRIMARY KEY CLUSTERED (id))");
2033: stmt.close();
2034:
2035: stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
2036: ResultSet.CONCUR_UPDATABLE);
2037: ResultSet rs = stmt
2038: .executeQuery("SELECT id, data FROM #clobupdate3");
2039:
2040: rs.moveToInsertRow();
2041:
2042: // Test ResultSet.updateCharacterStream()
2043: rs.updateCharacterStream(2, new StringReader(data), data
2044: .length());
2045:
2046: rs.insertRow();
2047:
2048: stmt.close();
2049: rs.close();
2050:
2051: Statement stmt2 = con.createStatement();
2052: ResultSet rs2 = stmt2
2053: .executeQuery("SELECT data FROM #clobupdate3");
2054:
2055: assertTrue(rs2.next());
2056:
2057: // Test ResultSet.getString()
2058: assertTrue(data.equals(rs2.getString(1)));
2059:
2060: assertFalse(rs2.next());
2061: stmt2.close();
2062: rs2.close();
2063: }
2064:
2065: public void testClobUpdate4() throws Exception {
2066: String data = getClobTestData();
2067:
2068: Statement stmt = con.createStatement();
2069: stmt
2070: .execute("CREATE TABLE #clobupdate4 (id NUMERIC IDENTITY, data TEXT, "
2071: + "CONSTRAINT pk_clobupdate4 PRIMARY KEY CLUSTERED (id))");
2072: stmt.close();
2073:
2074: PreparedStatement pstmt = con
2075: .prepareStatement("INSERT INTO #clobupdate4 (data) VALUES (?)");
2076:
2077: // Test PreparedStatement.setString()
2078: pstmt.setString(1, data);
2079: assertEquals(1, pstmt.executeUpdate());
2080:
2081: pstmt.close();
2082:
2083: Statement stmt2 = con.createStatement();
2084: ResultSet rs = stmt2
2085: .executeQuery("SELECT data FROM #clobupdate4");
2086:
2087: assertTrue(rs.next());
2088:
2089: Clob clob = rs.getClob(1);
2090:
2091: data = getNewClobTestData();
2092:
2093: // Test Clob.setBytes()
2094: clob.setString(1, data);
2095:
2096: assertTrue(data.equals(clob
2097: .getSubString(1, (int) clob.length())));
2098:
2099: assertFalse(rs.next());
2100:
2101: Statement stmt3 = con.createStatement(
2102: ResultSet.TYPE_SCROLL_SENSITIVE,
2103: ResultSet.CONCUR_UPDATABLE);
2104: ResultSet rs2 = stmt3
2105: .executeQuery("SELECT id, data FROM #clobupdate4");
2106:
2107: assertTrue(rs2.next());
2108:
2109: // Test ResultSet.updateClob()
2110: rs2.updateClob(2, clob);
2111:
2112: rs2.updateRow();
2113:
2114: assertFalse(rs2.next());
2115:
2116: stmt2.close();
2117: rs.close();
2118:
2119: stmt3.close();
2120: rs2.close();
2121:
2122: Statement stmt4 = con.createStatement();
2123: ResultSet rs3 = stmt4
2124: .executeQuery("SELECT data FROM #clobupdate4");
2125:
2126: assertTrue(rs3.next());
2127:
2128: // Test ResultSet.getString()
2129: assertTrue(data.equals(rs3.getString(1)));
2130:
2131: assertFalse(rs3.next());
2132: stmt4.close();
2133: rs3.close();
2134: }
2135:
2136: public void testClobUpdate5() throws Exception {
2137: String data = getClobTestData();
2138:
2139: Statement stmt = con.createStatement();
2140: stmt
2141: .execute("CREATE TABLE #clobupdate5 (id NUMERIC IDENTITY, data TEXT, "
2142: + "CONSTRAINT pk_clobupdate5 PRIMARY KEY CLUSTERED (id))");
2143: stmt.close();
2144:
2145: PreparedStatement pstmt = con
2146: .prepareStatement("INSERT INTO #clobupdate5 (data) VALUES (?)");
2147:
2148: // Test PreparedStatement.setString()
2149: pstmt.setString(1, data);
2150: assertEquals(1, pstmt.executeUpdate());
2151:
2152: pstmt.close();
2153:
2154: Statement stmt2 = con.createStatement();
2155: ResultSet rs = stmt2
2156: .executeQuery("SELECT data FROM #clobupdate5");
2157:
2158: assertTrue(rs.next());
2159:
2160: Clob clob = rs.getClob(1);
2161:
2162: data = getNewClobTestData();
2163:
2164: // Test Clob.setBytes()
2165: clob.setString(1, data);
2166:
2167: assertTrue(data.equals(clob
2168: .getSubString(1, (int) clob.length())));
2169:
2170: assertFalse(rs.next());
2171:
2172: Statement stmt3 = con.createStatement(
2173: ResultSet.TYPE_SCROLL_SENSITIVE,
2174: ResultSet.CONCUR_UPDATABLE);
2175: ResultSet rs2 = stmt3
2176: .executeQuery("SELECT id, data FROM #clobupdate5");
2177:
2178: assertTrue(rs2.next());
2179:
2180: // Test ResultSet.updateClob()
2181: rs2.updateClob(2, clob);
2182:
2183: rs2.updateRow();
2184:
2185: assertFalse(rs2.next());
2186:
2187: stmt2.close();
2188: rs.close();
2189:
2190: stmt3.close();
2191: rs2.close();
2192:
2193: Statement stmt4 = con.createStatement();
2194: ResultSet rs3 = stmt4
2195: .executeQuery("SELECT data FROM #clobupdate5");
2196:
2197: assertTrue(rs3.next());
2198:
2199: // Test ResultSet.getString()
2200: assertTrue(data.equals(rs3.getString(1)));
2201:
2202: assertFalse(rs3.next());
2203: stmt4.close();
2204: rs3.close();
2205: }
2206:
2207: /**
2208: * Test long <code>Clob</code> manipulation including indexed writes.
2209: */
2210: public void testClobUpdate6() throws Exception {
2211: int size = 100000;
2212: StringBuffer data = new StringBuffer(size);
2213: for (int i = 0; i < size; i++) {
2214: data.append((char) ('A' + i % 10));
2215: }
2216: //
2217: // Construct a clob
2218: //
2219: Statement stmt = con.createStatement();
2220: ResultSet rs = stmt.executeQuery("SELECT ''");
2221: assertNotNull(rs);
2222: assertTrue(rs.next());
2223: Clob clob = rs.getClob(1);
2224: clob.setString(1, data.toString());
2225: assertEquals((long) size, clob.length());
2226: assertTrue(data.toString().equals(
2227: clob.getSubString(1, (int) clob.length())));
2228: clob.setString(10, "THIS IS A TEST");
2229: data.replace(9, 23, "THIS IS A TEST");
2230: assertEquals("THIS IS A TEST", clob.getSubString(10, 14));
2231: assertTrue(compare(data.toString(), clob.getSubString(1,
2232: (int) clob.length())));
2233: clob.truncate(23);
2234: assertEquals("ABCDEFGHITHIS IS A TEST", clob
2235: .getSubString(1, 23));
2236: OutputStream os = clob.setAsciiStream(1);
2237: for (int i = 0; i < size; i++) {
2238: os.write(data.charAt(i));
2239: }
2240: os.close();
2241: assertEquals((long) size, clob.length());
2242: assertTrue(data.toString().equals(
2243: clob.getSubString(1, (int) clob.length())));
2244: InputStream is = clob.getAsciiStream();
2245: int b;
2246: int p = 0;
2247: while ((b = is.read()) >= 0) {
2248: if ((char) b != data.charAt(p++)) {
2249: fail("Mismatch at " + p);
2250: }
2251: }
2252: is.close();
2253: assertTrue(p == size);
2254: Reader rdr = clob.getCharacterStream();
2255: p = 0;
2256: while ((b = rdr.read()) >= 0) {
2257: if ((char) b != data.charAt(p++)) {
2258: fail("Mismatch at " + p);
2259: }
2260: }
2261: rdr.close();
2262: assertTrue(p == size);
2263: clob.truncate(0);
2264: Writer wtr = clob.setCharacterStream(1);
2265: for (int i = 0; i < size; i++) {
2266: wtr.write(data.charAt(i));
2267: }
2268: wtr.close();
2269: assertTrue(p == size);
2270: assertTrue(data.toString().equals(
2271: clob.getSubString(1, (int) clob.length())));
2272: wtr = clob.setCharacterStream(10000);
2273: for (int i = 0; i < 8; i++) {
2274: wtr.write('X');
2275: }
2276: wtr.close();
2277: data.replace(10000 - 1, 10000 - 1 + 8, "XXXXXXXX");
2278: assertTrue(data.toString().equals(
2279: clob.getSubString(1, (int) clob.length())));
2280: clob.setString(100001, "XTESTX", 1, 4);
2281: assertEquals((long) 100000 + 4, clob.length());
2282: assertEquals("JTEST", clob.getSubString(100000, 8));
2283: assertEquals(100000, clob.position("JTEST", 100000));
2284: Clob clob2 = rs.getClob(1);
2285: clob.setString(1, "XXXXXXXX");
2286: assertEquals(10000, clob.position("XXXXXXXX", 10000));
2287: assertFalse(10000 == clob.position("XXXXXXXX", 10001));
2288: }
2289:
2290: public void testClobSetNull1() throws Exception {
2291: Statement stmt = con.createStatement();
2292: stmt.execute("CREATE TABLE #clobsetnull1 (data TEXT NULL)");
2293: stmt.close();
2294:
2295: PreparedStatement pstmt = con
2296: .prepareStatement("INSERT INTO #clobsetnull1 (data) VALUES (?)");
2297:
2298: // Test PreparedStatement.setAsciiStream()
2299: pstmt.setAsciiStream(1, null, 0);
2300: assertEquals(1, pstmt.executeUpdate());
2301:
2302: pstmt.close();
2303:
2304: Statement stmt2 = con.createStatement();
2305: ResultSet rs = stmt2
2306: .executeQuery("SELECT data FROM #clobsetnull1");
2307:
2308: assertTrue(rs.next());
2309:
2310: // Test ResultSet.getAsciiStream()
2311: assertNull(rs.getAsciiStream(1));
2312: assertTrue(rs.wasNull());
2313:
2314: // Test ResultSet.getCharacterStream()
2315: assertNull(rs.getCharacterStream(1));
2316: assertTrue(rs.wasNull());
2317:
2318: // Test ResultSet.getClob()
2319: assertNull(rs.getClob(1));
2320: assertTrue(rs.wasNull());
2321:
2322: // Test ResultSet.getObject()
2323: assertNull(rs.getObject(1));
2324: assertTrue(rs.wasNull());
2325:
2326: // Test ResultSet.getString()
2327: assertNull(rs.getString(1));
2328: assertTrue(rs.wasNull());
2329:
2330: // Test ResultSet.getUnicodeStream()
2331: assertNull(rs.getUnicodeStream(1));
2332: assertTrue(rs.wasNull());
2333:
2334: assertFalse(rs.next());
2335: stmt2.close();
2336: rs.close();
2337: }
2338:
2339: public void testClobSetNull2() throws Exception {
2340: Statement stmt = con.createStatement();
2341: stmt.execute("CREATE TABLE #clobsetnull2 (data TEXT NULL)");
2342: stmt.close();
2343:
2344: PreparedStatement pstmt = con
2345: .prepareStatement("INSERT INTO #clobsetnull2 (data) VALUES (?)");
2346:
2347: // Test PreparedStatement.setCharacterStream()
2348: pstmt.setCharacterStream(1, null, 0);
2349: assertEquals(1, pstmt.executeUpdate());
2350:
2351: pstmt.close();
2352:
2353: Statement stmt2 = con.createStatement();
2354: ResultSet rs = stmt2
2355: .executeQuery("SELECT data FROM #clobsetnull2");
2356:
2357: assertTrue(rs.next());
2358:
2359: // Test ResultSet.getAsciiStream()
2360: assertNull(rs.getAsciiStream(1));
2361: assertTrue(rs.wasNull());
2362:
2363: // Test ResultSet.getCharacterStream()
2364: assertNull(rs.getCharacterStream(1));
2365: assertTrue(rs.wasNull());
2366:
2367: // Test ResultSet.getClob()
2368: assertNull(rs.getClob(1));
2369: assertTrue(rs.wasNull());
2370:
2371: // Test ResultSet.getObject()
2372: assertNull(rs.getObject(1));
2373: assertTrue(rs.wasNull());
2374:
2375: // Test ResultSet.getString()
2376: assertNull(rs.getString(1));
2377: assertTrue(rs.wasNull());
2378:
2379: // Test ResultSet.getUnicodeStream()
2380: assertNull(rs.getUnicodeStream(1));
2381: assertTrue(rs.wasNull());
2382:
2383: assertFalse(rs.next());
2384: stmt2.close();
2385: rs.close();
2386: }
2387:
2388: public void testClobSetNull3() throws Exception {
2389: Statement stmt = con.createStatement();
2390: stmt.execute("CREATE TABLE #clobsetnull3 (data TEXT NULL)");
2391: stmt.close();
2392:
2393: PreparedStatement pstmt = con
2394: .prepareStatement("INSERT INTO #clobsetnull3 (data) VALUES (?)");
2395:
2396: // Test PreparedStatement.setClob()
2397: pstmt.setClob(1, null);
2398: assertEquals(1, pstmt.executeUpdate());
2399:
2400: pstmt.close();
2401:
2402: Statement stmt2 = con.createStatement();
2403: ResultSet rs = stmt2
2404: .executeQuery("SELECT data FROM #clobsetnull3");
2405:
2406: assertTrue(rs.next());
2407:
2408: // Test ResultSet.getAsciiStream()
2409: assertNull(rs.getAsciiStream(1));
2410: assertTrue(rs.wasNull());
2411:
2412: // Test ResultSet.getCharacterStream()
2413: assertNull(rs.getCharacterStream(1));
2414: assertTrue(rs.wasNull());
2415:
2416: // Test ResultSet.getClob()
2417: assertNull(rs.getClob(1));
2418: assertTrue(rs.wasNull());
2419:
2420: // Test ResultSet.getObject()
2421: assertNull(rs.getObject(1));
2422: assertTrue(rs.wasNull());
2423:
2424: // Test ResultSet.getString()
2425: assertNull(rs.getString(1));
2426: assertTrue(rs.wasNull());
2427:
2428: // Test ResultSet.getUnicodeStream()
2429: assertNull(rs.getUnicodeStream(1));
2430: assertTrue(rs.wasNull());
2431:
2432: assertFalse(rs.next());
2433: stmt2.close();
2434: rs.close();
2435: }
2436:
2437: public void testClobSetNull4() throws Exception {
2438: Statement stmt = con.createStatement();
2439: stmt.execute("CREATE TABLE #clobsetnull4 (data TEXT NULL)");
2440: stmt.close();
2441:
2442: PreparedStatement pstmt = con
2443: .prepareStatement("INSERT INTO #clobsetnull4 (data) VALUES (?)");
2444:
2445: // Test PreparedStatement.setObject(int,Object)
2446: pstmt.setObject(1, null);
2447: assertEquals(1, pstmt.executeUpdate());
2448:
2449: pstmt.close();
2450:
2451: Statement stmt2 = con.createStatement();
2452: ResultSet rs = stmt2
2453: .executeQuery("SELECT data FROM #clobsetnull4");
2454:
2455: assertTrue(rs.next());
2456:
2457: // Test ResultSet.getAsciiStream()
2458: assertNull(rs.getAsciiStream(1));
2459: assertTrue(rs.wasNull());
2460:
2461: // Test ResultSet.getCharacterStream()
2462: assertNull(rs.getCharacterStream(1));
2463: assertTrue(rs.wasNull());
2464:
2465: // Test ResultSet.getClob()
2466: assertNull(rs.getClob(1));
2467: assertTrue(rs.wasNull());
2468:
2469: // Test ResultSet.getObject()
2470: assertNull(rs.getObject(1));
2471: assertTrue(rs.wasNull());
2472:
2473: // Test ResultSet.getString()
2474: assertNull(rs.getString(1));
2475: assertTrue(rs.wasNull());
2476:
2477: // Test ResultSet.getUnicodeStream()
2478: assertNull(rs.getUnicodeStream(1));
2479: assertTrue(rs.wasNull());
2480:
2481: assertFalse(rs.next());
2482: stmt2.close();
2483: rs.close();
2484: }
2485:
2486: public void testClobSetNull5() throws Exception {
2487: Statement stmt = con.createStatement();
2488: stmt.execute("CREATE TABLE #clobsetnull5 (data TEXT NULL)");
2489: stmt.close();
2490:
2491: PreparedStatement pstmt = con
2492: .prepareStatement("INSERT INTO #clobsetnull5 (data) VALUES (?)");
2493:
2494: // Test PreparedStatement.setObject(int,Object,int)
2495: pstmt.setObject(1, null, Types.CLOB);
2496: assertEquals(1, pstmt.executeUpdate());
2497:
2498: pstmt.close();
2499:
2500: Statement stmt2 = con.createStatement();
2501: ResultSet rs = stmt2
2502: .executeQuery("SELECT data FROM #clobsetnull5");
2503:
2504: assertTrue(rs.next());
2505:
2506: // Test ResultSet.getAsciiStream()
2507: assertNull(rs.getAsciiStream(1));
2508: assertTrue(rs.wasNull());
2509:
2510: // Test ResultSet.getCharacterStream()
2511: assertNull(rs.getCharacterStream(1));
2512: assertTrue(rs.wasNull());
2513:
2514: // Test ResultSet.getClob()
2515: assertNull(rs.getClob(1));
2516: assertTrue(rs.wasNull());
2517:
2518: // Test ResultSet.getObject()
2519: assertNull(rs.getObject(1));
2520: assertTrue(rs.wasNull());
2521:
2522: // Test ResultSet.getString()
2523: assertNull(rs.getString(1));
2524: assertTrue(rs.wasNull());
2525:
2526: // Test ResultSet.getUnicodeStream()
2527: assertNull(rs.getUnicodeStream(1));
2528: assertTrue(rs.wasNull());
2529:
2530: assertFalse(rs.next());
2531: stmt2.close();
2532: rs.close();
2533: }
2534:
2535: public void testClobSetNull6() throws Exception {
2536: Statement stmt = con.createStatement();
2537: stmt.execute("CREATE TABLE #clobsetnull6 (data TEXT NULL)");
2538: stmt.close();
2539:
2540: PreparedStatement pstmt = con
2541: .prepareStatement("INSERT INTO #clobsetnull6 (data) VALUES (?)");
2542:
2543: // Test PreparedStatement.setString()
2544: pstmt.setString(1, null);
2545: assertEquals(1, pstmt.executeUpdate());
2546:
2547: pstmt.close();
2548:
2549: Statement stmt2 = con.createStatement();
2550: ResultSet rs = stmt2
2551: .executeQuery("SELECT data FROM #clobsetnull6");
2552:
2553: assertTrue(rs.next());
2554:
2555: // Test ResultSet.getAsciiStream()
2556: assertNull(rs.getAsciiStream(1));
2557: assertTrue(rs.wasNull());
2558:
2559: // Test ResultSet.getCharacterStream()
2560: assertNull(rs.getCharacterStream(1));
2561: assertTrue(rs.wasNull());
2562:
2563: // Test ResultSet.getClob()
2564: assertNull(rs.getClob(1));
2565: assertTrue(rs.wasNull());
2566:
2567: // Test ResultSet.getObject()
2568: assertNull(rs.getObject(1));
2569: assertTrue(rs.wasNull());
2570:
2571: // Test ResultSet.getString()
2572: assertNull(rs.getString(1));
2573: assertTrue(rs.wasNull());
2574:
2575: // Test ResultSet.getUnicodeStream()
2576: assertNull(rs.getUnicodeStream(1));
2577: assertTrue(rs.wasNull());
2578:
2579: assertFalse(rs.next());
2580: stmt2.close();
2581: rs.close();
2582: }
2583:
2584: public void testClobSetNull7() throws Exception {
2585: Statement stmt = con.createStatement();
2586: stmt.execute("CREATE TABLE #clobsetnull7 (data TEXT NULL)");
2587: stmt.close();
2588:
2589: PreparedStatement pstmt = con
2590: .prepareStatement("INSERT INTO #clobsetnull7 (data) VALUES (?)");
2591:
2592: // Test PreparedStatement.setUnicodeStream()
2593: pstmt.setUnicodeStream(1, null, 0);
2594: assertEquals(1, pstmt.executeUpdate());
2595:
2596: pstmt.close();
2597:
2598: Statement stmt2 = con.createStatement();
2599: ResultSet rs = stmt2
2600: .executeQuery("SELECT data FROM #clobsetnull7");
2601:
2602: assertTrue(rs.next());
2603:
2604: // Test ResultSet.getAsciiStream()
2605: assertNull(rs.getAsciiStream(1));
2606: assertTrue(rs.wasNull());
2607:
2608: // Test ResultSet.getCharacterStream()
2609: assertNull(rs.getCharacterStream(1));
2610: assertTrue(rs.wasNull());
2611:
2612: // Test ResultSet.getClob()
2613: assertNull(rs.getClob(1));
2614: assertTrue(rs.wasNull());
2615:
2616: // Test ResultSet.getObject()
2617: assertNull(rs.getObject(1));
2618: assertTrue(rs.wasNull());
2619:
2620: // Test ResultSet.getString()
2621: assertNull(rs.getString(1));
2622: assertTrue(rs.wasNull());
2623:
2624: // Test ResultSet.getUnicodeStream()
2625: assertNull(rs.getUnicodeStream(1));
2626: assertTrue(rs.wasNull());
2627:
2628: assertFalse(rs.next());
2629: stmt2.close();
2630: rs.close();
2631: }
2632:
2633: public void testClobSetNull8() throws Exception {
2634: Statement stmt = con.createStatement();
2635: stmt.execute("CREATE TABLE #clobsetnull8 (data TEXT NULL)");
2636: stmt.close();
2637:
2638: PreparedStatement pstmt = con
2639: .prepareStatement("INSERT INTO #clobsetnull8 (data) VALUES (?)");
2640:
2641: // Test PreparedStatement.setNull()
2642: pstmt.setNull(1, Types.CLOB);
2643: assertEquals(1, pstmt.executeUpdate());
2644:
2645: pstmt.close();
2646:
2647: Statement stmt2 = con.createStatement();
2648: ResultSet rs = stmt2
2649: .executeQuery("SELECT data FROM #clobsetnull8");
2650:
2651: assertTrue(rs.next());
2652:
2653: // Test ResultSet.getAsciiStream()
2654: assertNull(rs.getAsciiStream(1));
2655: assertTrue(rs.wasNull());
2656:
2657: // Test ResultSet.getCharacterStream()
2658: assertNull(rs.getCharacterStream(1));
2659: assertTrue(rs.wasNull());
2660:
2661: // Test ResultSet.getClob()
2662: assertNull(rs.getClob(1));
2663: assertTrue(rs.wasNull());
2664:
2665: // Test ResultSet.getObject()
2666: assertNull(rs.getObject(1));
2667: assertTrue(rs.wasNull());
2668:
2669: // Test ResultSet.getString()
2670: assertNull(rs.getString(1));
2671: assertTrue(rs.wasNull());
2672:
2673: // Test ResultSet.getUnicodeStream()
2674: assertNull(rs.getUnicodeStream(1));
2675: assertTrue(rs.wasNull());
2676:
2677: assertFalse(rs.next());
2678: stmt2.close();
2679: rs.close();
2680: }
2681:
2682: public void testClobSetNull9() throws Exception {
2683: Statement stmt = con.createStatement();
2684: stmt
2685: .execute("CREATE TABLE #clobsetnull9 (id NUMERIC IDENTITY, data TEXT NULL, "
2686: + "CONSTRAINT pk_clobsetnull9 PRIMARY KEY CLUSTERED (id))");
2687: stmt.close();
2688:
2689: stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
2690: ResultSet.CONCUR_UPDATABLE);
2691: ResultSet rs = stmt
2692: .executeQuery("SELECT id, data FROM #clobsetnull9");
2693:
2694: rs.moveToInsertRow();
2695:
2696: // Test ResultSet.updateAsciiStream()
2697: rs.updateAsciiStream(2, null, 0);
2698:
2699: rs.insertRow();
2700:
2701: stmt.close();
2702: rs.close();
2703:
2704: Statement stmt2 = con.createStatement();
2705: ResultSet rs2 = stmt2
2706: .executeQuery("SELECT data FROM #clobsetnull9");
2707:
2708: assertTrue(rs2.next());
2709:
2710: // Test ResultSet.getAsciiStream()
2711: assertNull(rs2.getAsciiStream(1));
2712: assertTrue(rs2.wasNull());
2713:
2714: // Test ResultSet.getCharacterStream()
2715: assertNull(rs2.getCharacterStream(1));
2716: assertTrue(rs2.wasNull());
2717:
2718: // Test ResultSet.getClob()
2719: assertNull(rs2.getClob(1));
2720: assertTrue(rs2.wasNull());
2721:
2722: // Test ResultSet.getObject()
2723: assertNull(rs2.getObject(1));
2724: assertTrue(rs2.wasNull());
2725:
2726: // Test ResultSet.getString()
2727: assertNull(rs2.getString(1));
2728: assertTrue(rs2.wasNull());
2729:
2730: // Test ResultSet.getUnicodeStream()
2731: assertNull(rs2.getUnicodeStream(1));
2732: assertTrue(rs2.wasNull());
2733:
2734: assertFalse(rs2.next());
2735: stmt2.close();
2736: rs2.close();
2737: }
2738:
2739: public void testClobSetNull10() throws Exception {
2740: Statement stmt = con.createStatement();
2741: stmt
2742: .execute("CREATE TABLE #clobsetnull10 (id NUMERIC IDENTITY, data TEXT NULL, "
2743: + "CONSTRAINT pk_clobsetnull10 PRIMARY KEY CLUSTERED (id))");
2744: stmt.close();
2745:
2746: stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
2747: ResultSet.CONCUR_UPDATABLE);
2748: ResultSet rs = stmt
2749: .executeQuery("SELECT id, data FROM #clobsetnull10");
2750:
2751: rs.moveToInsertRow();
2752:
2753: // Test ResultSet.updateCharacterStream()
2754: rs.updateCharacterStream(2, null, 0);
2755:
2756: rs.insertRow();
2757:
2758: stmt.close();
2759: rs.close();
2760:
2761: Statement stmt2 = con.createStatement();
2762: ResultSet rs2 = stmt2
2763: .executeQuery("SELECT data FROM #clobsetnull10");
2764:
2765: assertTrue(rs2.next());
2766:
2767: // Test ResultSet.getAsciiStream()
2768: assertNull(rs2.getAsciiStream(1));
2769: assertTrue(rs2.wasNull());
2770:
2771: // Test ResultSet.getCharacterStream()
2772: assertNull(rs2.getCharacterStream(1));
2773: assertTrue(rs2.wasNull());
2774:
2775: // Test ResultSet.getClob()
2776: assertNull(rs2.getClob(1));
2777: assertTrue(rs2.wasNull());
2778:
2779: // Test ResultSet.getObject()
2780: assertNull(rs2.getObject(1));
2781: assertTrue(rs2.wasNull());
2782:
2783: // Test ResultSet.getString()
2784: assertNull(rs2.getString(1));
2785: assertTrue(rs2.wasNull());
2786:
2787: // Test ResultSet.getUnicodeStream()
2788: assertNull(rs2.getUnicodeStream(1));
2789: assertTrue(rs2.wasNull());
2790:
2791: assertFalse(rs2.next());
2792: stmt2.close();
2793: rs2.close();
2794: }
2795:
2796: public void testClobSetNull11() throws Exception {
2797: Statement stmt = con.createStatement();
2798: stmt
2799: .execute("CREATE TABLE #clobsetnull11 (id NUMERIC IDENTITY, data TEXT NULL, "
2800: + "CONSTRAINT pk_clobsetnull11 PRIMARY KEY CLUSTERED (id))");
2801: stmt.close();
2802:
2803: stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
2804: ResultSet.CONCUR_UPDATABLE);
2805: ResultSet rs = stmt
2806: .executeQuery("SELECT id, data FROM #clobsetnull11");
2807:
2808: rs.moveToInsertRow();
2809:
2810: // Test ResultSet.updateClob()
2811: rs.updateClob(2, null);
2812:
2813: rs.insertRow();
2814:
2815: stmt.close();
2816: rs.close();
2817:
2818: Statement stmt2 = con.createStatement();
2819: ResultSet rs2 = stmt2
2820: .executeQuery("SELECT data FROM #clobsetnull11");
2821:
2822: assertTrue(rs2.next());
2823:
2824: // Test ResultSet.getAsciiStream()
2825: assertNull(rs2.getAsciiStream(1));
2826: assertTrue(rs2.wasNull());
2827:
2828: // Test ResultSet.getCharacterStream()
2829: assertNull(rs2.getCharacterStream(1));
2830: assertTrue(rs2.wasNull());
2831:
2832: // Test ResultSet.getClob()
2833: assertNull(rs2.getClob(1));
2834: assertTrue(rs2.wasNull());
2835:
2836: // Test ResultSet.getObject()
2837: assertNull(rs2.getObject(1));
2838: assertTrue(rs2.wasNull());
2839:
2840: // Test ResultSet.getString()
2841: assertNull(rs2.getString(1));
2842: assertTrue(rs2.wasNull());
2843:
2844: // Test ResultSet.getUnicodeStream()
2845: assertNull(rs2.getUnicodeStream(1));
2846: assertTrue(rs2.wasNull());
2847:
2848: assertFalse(rs2.next());
2849: stmt2.close();
2850: rs2.close();
2851: }
2852:
2853: public void testClobSetNull12() throws Exception {
2854: Statement stmt = con.createStatement();
2855: stmt
2856: .execute("CREATE TABLE #clobsetnull12 (id NUMERIC IDENTITY, data TEXT NULL, "
2857: + "CONSTRAINT pk_clobsetnull12 PRIMARY KEY CLUSTERED (id))");
2858: stmt.close();
2859:
2860: stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
2861: ResultSet.CONCUR_UPDATABLE);
2862: ResultSet rs = stmt
2863: .executeQuery("SELECT id, data FROM #clobsetnull12");
2864:
2865: rs.moveToInsertRow();
2866:
2867: // Test ResultSet.updateObject()
2868: rs.updateObject(2, null);
2869:
2870: rs.insertRow();
2871:
2872: stmt.close();
2873: rs.close();
2874:
2875: Statement stmt2 = con.createStatement();
2876: ResultSet rs2 = stmt2
2877: .executeQuery("SELECT data FROM #clobsetnull12");
2878:
2879: assertTrue(rs2.next());
2880:
2881: // Test ResultSet.getAsciiStream()
2882: assertNull(rs2.getAsciiStream(1));
2883: assertTrue(rs2.wasNull());
2884:
2885: // Test ResultSet.getCharacterStream()
2886: assertNull(rs2.getCharacterStream(1));
2887: assertTrue(rs2.wasNull());
2888:
2889: // Test ResultSet.getClob()
2890: assertNull(rs2.getClob(1));
2891: assertTrue(rs2.wasNull());
2892:
2893: // Test ResultSet.getObject()
2894: assertNull(rs2.getObject(1));
2895: assertTrue(rs2.wasNull());
2896:
2897: // Test ResultSet.getString()
2898: assertNull(rs2.getString(1));
2899: assertTrue(rs2.wasNull());
2900:
2901: // Test ResultSet.getUnicodeStream()
2902: assertNull(rs2.getUnicodeStream(1));
2903: assertTrue(rs2.wasNull());
2904:
2905: assertFalse(rs2.next());
2906: stmt2.close();
2907: rs2.close();
2908: }
2909:
2910: public void testClobSetNull13() throws Exception {
2911: Statement stmt = con.createStatement();
2912: stmt
2913: .execute("CREATE TABLE #clobsetnull13 (id NUMERIC IDENTITY, data TEXT NULL, "
2914: + "CONSTRAINT pk_clobsetnull13 PRIMARY KEY CLUSTERED (id))");
2915: stmt.close();
2916:
2917: stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
2918: ResultSet.CONCUR_UPDATABLE);
2919: ResultSet rs = stmt
2920: .executeQuery("SELECT id, data FROM #clobsetnull13");
2921:
2922: rs.moveToInsertRow();
2923:
2924: // Test ResultSet.updateString()
2925: rs.updateString(2, null);
2926:
2927: rs.insertRow();
2928:
2929: stmt.close();
2930: rs.close();
2931:
2932: Statement stmt2 = con.createStatement();
2933: ResultSet rs2 = stmt2
2934: .executeQuery("SELECT data FROM #clobsetnull13");
2935:
2936: assertTrue(rs2.next());
2937:
2938: // Test ResultSet.getAsciiStream()
2939: assertNull(rs2.getAsciiStream(1));
2940: assertTrue(rs2.wasNull());
2941:
2942: // Test ResultSet.getCharacterStream()
2943: assertNull(rs2.getCharacterStream(1));
2944: assertTrue(rs2.wasNull());
2945:
2946: // Test ResultSet.getClob()
2947: assertNull(rs2.getClob(1));
2948: assertTrue(rs2.wasNull());
2949:
2950: // Test ResultSet.getObject()
2951: assertNull(rs2.getObject(1));
2952: assertTrue(rs2.wasNull());
2953:
2954: // Test ResultSet.getString()
2955: assertNull(rs2.getString(1));
2956: assertTrue(rs2.wasNull());
2957:
2958: // Test ResultSet.getUnicodeStream()
2959: assertNull(rs2.getUnicodeStream(1));
2960: assertTrue(rs2.wasNull());
2961:
2962: assertFalse(rs2.next());
2963: stmt2.close();
2964: rs2.close();
2965: }
2966:
2967: public void testClobSetNull14() throws Exception {
2968: Statement stmt = con.createStatement();
2969: stmt
2970: .execute("CREATE TABLE #clobsetnull14 (id NUMERIC IDENTITY, data TEXT NULL, "
2971: + "CONSTRAINT pk_clobsetnull14 PRIMARY KEY CLUSTERED (id))");
2972: stmt.close();
2973:
2974: stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
2975: ResultSet.CONCUR_UPDATABLE);
2976: ResultSet rs = stmt
2977: .executeQuery("SELECT id, data FROM #clobsetnull14");
2978:
2979: rs.moveToInsertRow();
2980:
2981: // Test ResultSet.updateNull()
2982: rs.updateNull(2);
2983:
2984: rs.insertRow();
2985:
2986: stmt.close();
2987: rs.close();
2988:
2989: Statement stmt2 = con.createStatement();
2990: ResultSet rs2 = stmt2
2991: .executeQuery("SELECT data FROM #clobsetnull14");
2992:
2993: assertTrue(rs2.next());
2994:
2995: // Test ResultSet.getAsciiStream()
2996: assertNull(rs2.getAsciiStream(1));
2997: assertTrue(rs2.wasNull());
2998:
2999: // Test ResultSet.getCharacterStream()
3000: assertNull(rs2.getCharacterStream(1));
3001: assertTrue(rs2.wasNull());
3002:
3003: // Test ResultSet.getClob()
3004: assertNull(rs2.getClob(1));
3005: assertTrue(rs2.wasNull());
3006:
3007: // Test ResultSet.getObject()
3008: assertNull(rs2.getObject(1));
3009: assertTrue(rs2.wasNull());
3010:
3011: // Test ResultSet.getString()
3012: assertNull(rs2.getString(1));
3013: assertTrue(rs2.wasNull());
3014:
3015: // Test ResultSet.getUnicodeStream()
3016: assertNull(rs2.getUnicodeStream(1));
3017: assertTrue(rs2.wasNull());
3018:
3019: assertFalse(rs2.next());
3020: stmt2.close();
3021: rs2.close();
3022: }
3023:
3024: public void testClobGetSubString1() throws Exception {
3025: String data = getClobTestData();
3026:
3027: Statement stmt = con.createStatement();
3028: stmt.execute("CREATE TABLE #clobgetsubstring1 (data TEXT)");
3029: stmt.close();
3030:
3031: PreparedStatement pstmt = con
3032: .prepareStatement("INSERT INTO #clobgetsubstring1 (data) VALUES (?)");
3033:
3034: pstmt.setString(1, data);
3035: assertEquals(1, pstmt.executeUpdate());
3036:
3037: pstmt.close();
3038:
3039: Statement stmt2 = con.createStatement();
3040: ResultSet rs = stmt2
3041: .executeQuery("SELECT data FROM #clobgetsubstring1");
3042:
3043: assertTrue(rs.next());
3044:
3045: // Test ResultSet.getClob()
3046: Clob clob = rs.getClob(1);
3047:
3048: assertNotNull(clob);
3049:
3050: // Test Clob.getSubString()
3051: assertTrue(data.equals(clob.getSubString(1L, (int) clob
3052: .length())));
3053:
3054: assertFalse(rs.next());
3055: stmt2.close();
3056: rs.close();
3057: }
3058:
3059: public void testClobGetSubString2() throws Exception {
3060: String data = getClobTestData();
3061:
3062: Statement stmt = con.createStatement();
3063: stmt.execute("CREATE TABLE #clobgetsubstring2 (data TEXT)");
3064: stmt.close();
3065:
3066: PreparedStatement pstmt = con
3067: .prepareStatement("INSERT INTO #clobgetsubstring2 (data) VALUES (?)");
3068:
3069: pstmt.setString(1, data);
3070: assertEquals(1, pstmt.executeUpdate());
3071:
3072: pstmt.close();
3073:
3074: Statement stmt2 = con.createStatement();
3075: ResultSet rs = stmt2
3076: .executeQuery("SELECT data FROM #clobgetsubstring2");
3077:
3078: assertTrue(rs.next());
3079:
3080: // Test ResultSet.getClob()
3081: Clob clob = rs.getClob(1);
3082:
3083: assertNotNull(clob);
3084:
3085: String tmpData = data.substring(0, data.length() / 2);
3086:
3087: // Test Clob.getSubString()
3088: assertTrue(tmpData.equals(clob.getSubString(1L, tmpData
3089: .length())));
3090:
3091: assertFalse(rs.next());
3092: stmt2.close();
3093: rs.close();
3094: }
3095:
3096: public void testClobGetSubString3() throws Exception {
3097: String data = getClobTestData();
3098:
3099: Statement stmt = con.createStatement();
3100: stmt.execute("CREATE TABLE #clobgetsubstring3 (data TEXT)");
3101: stmt.close();
3102:
3103: PreparedStatement pstmt = con
3104: .prepareStatement("INSERT INTO #clobgetsubstring3 (data) VALUES (?)");
3105:
3106: pstmt.setString(1, data);
3107: assertEquals(1, pstmt.executeUpdate());
3108:
3109: pstmt.close();
3110:
3111: Statement stmt2 = con.createStatement();
3112: ResultSet rs = stmt2
3113: .executeQuery("SELECT data FROM #clobgetsubstring3");
3114:
3115: assertTrue(rs.next());
3116:
3117: // Test ResultSet.getClob()
3118: Clob clob = rs.getClob(1);
3119:
3120: assertNotNull(clob);
3121:
3122: // Offset data by 1
3123: String tmpData = data.substring(1, data.length() / 2);
3124:
3125: // Test Clob.getSubString()
3126: assertTrue(tmpData.equals(clob.getSubString(2L, tmpData
3127: .length())));
3128:
3129: assertFalse(rs.next());
3130: stmt2.close();
3131: rs.close();
3132: }
3133:
3134: public void testClobLength1() throws Exception {
3135: String data = getClobTestData();
3136:
3137: Statement stmt = con.createStatement();
3138: stmt.execute("CREATE TABLE #cloblength1 (data TEXT)");
3139: stmt.close();
3140:
3141: PreparedStatement pstmt = con
3142: .prepareStatement("INSERT INTO #cloblength1 (data) VALUES (?)");
3143:
3144: pstmt.setString(1, data);
3145: assertEquals(1, pstmt.executeUpdate());
3146:
3147: pstmt.close();
3148:
3149: Statement stmt2 = con.createStatement();
3150: ResultSet rs = stmt2
3151: .executeQuery("SELECT data FROM #cloblength1");
3152:
3153: assertTrue(rs.next());
3154:
3155: // Test ResultSet.getClob()
3156: Clob clob = rs.getClob(1);
3157:
3158: assertNotNull(clob);
3159:
3160: // Test Clob.length()
3161: assertEquals(data.length(), clob.length());
3162:
3163: assertFalse(rs.next());
3164: stmt2.close();
3165: rs.close();
3166: }
3167:
3168: public void testClobTruncate1() throws Exception {
3169: String data = getClobTestData();
3170:
3171: Statement stmt = con.createStatement();
3172: stmt.execute("CREATE TABLE #clobtruncate1 (data TEXT)");
3173: stmt.close();
3174:
3175: PreparedStatement pstmt = con
3176: .prepareStatement("INSERT INTO #clobtruncate1 (data) VALUES (?)");
3177:
3178: pstmt.setString(1, data);
3179: assertEquals(1, pstmt.executeUpdate());
3180:
3181: pstmt.close();
3182:
3183: Statement stmt2 = con.createStatement();
3184: ResultSet rs = stmt2
3185: .executeQuery("SELECT data FROM #clobtruncate1");
3186:
3187: assertTrue(rs.next());
3188:
3189: // Test ResultSet.getClob()
3190: Clob clob = rs.getClob(1);
3191:
3192: assertNotNull(clob);
3193:
3194: String tmpData = data.substring(0, data.length() / 2);
3195:
3196: // Test Clob.truncate()
3197: clob.truncate(tmpData.length());
3198: assertEquals(tmpData.length(), clob.length());
3199:
3200: // Test Clob.getSubString()
3201: assertTrue(tmpData.equals(clob.getSubString(1L, (int) clob
3202: .length())));
3203:
3204: assertFalse(rs.next());
3205: stmt2.close();
3206: rs.close();
3207: }
3208:
3209: /**
3210: * Test for bug [1062395] Empty (but not null) blobs should return byte[0].
3211: */
3212: public void testClobEmpty() throws Exception {
3213: Statement stmt = con.createStatement();
3214:
3215: assertEquals(0, stmt
3216: .executeUpdate("CREATE TABLE #clobEmpty (data TEXT)"));
3217: assertEquals(
3218: 1,
3219: stmt
3220: .executeUpdate("INSERT INTO #clobEmpty (data) values ('')"));
3221:
3222: ResultSet rs = stmt.executeQuery("SELECT * FROM #clobEmpty");
3223: assertTrue(rs.next());
3224: Clob clob = rs.getClob(1);
3225: assertEquals(0, clob.length());
3226: assertEquals("", clob.getSubString(1, 0));
3227:
3228: rs.close();
3229: stmt.close();
3230: }
3231:
3232: public void testClobCaching() throws Exception {
3233: // Create a Clob large enough to need caching to disk
3234: char[] in = new char[100000];
3235: for (int i = 0; i < in.length; i++) {
3236: // Store non-Cp1252 characters into it
3237: in[i] = 0x2032;
3238: }
3239:
3240: Statement stmt = con.createStatement();
3241: stmt.executeUpdate("create table #testClobCaching (val ntext)");
3242:
3243: PreparedStatement pstmt = con
3244: .prepareStatement("insert into #testClobCaching values (?)");
3245: pstmt.setCharacterStream(1, new CharArrayReader(in), in.length);
3246: pstmt.executeUpdate();
3247: pstmt.close();
3248:
3249: ResultSet rs = stmt
3250: .executeQuery("select * from #testClobCaching");
3251: assertTrue(rs.next());
3252: String out = rs.getString(1);
3253: assertEquals(in.length, out.length());
3254: for (int i = 0; i < in.length; i++) {
3255: if (in[i] != out.charAt(i)) {
3256: fail("Result differs at position " + i);
3257: }
3258: }
3259: assertFalse(rs.next());
3260: rs.close();
3261: stmt.close();
3262: }
3263:
3264: /**
3265: * Test for incorrect handling of zero length streams (bug [1096086] Zero
3266: * length streams generate null values).
3267: */
3268: public void testZeroLengthStreams() throws Exception {
3269: Statement stmt = con
3270: .createStatement(ResultSet.TYPE_FORWARD_ONLY,
3271: ResultSet.CONCUR_UPDATABLE);
3272: stmt
3273: .execute("create table #test (id int primary key, s_ascii text,"
3274: + "s_char text, s_unicode text, s_bytes image)");
3275: PreparedStatement pstmt = con
3276: .prepareStatement("insert into #test values(?,?,?,?,?)");
3277:
3278: // Write zero length fields
3279: pstmt.setInt(1, 1);
3280: pstmt.setAsciiStream(2, new ByteArrayInputStream(new byte[0]),
3281: 0);
3282: pstmt.setCharacterStream(3, new StringReader(""), 0);
3283: pstmt.setUnicodeStream(4,
3284: new ByteArrayInputStream(new byte[0]), 0);
3285: pstmt.setBinaryStream(5, new ByteArrayInputStream(new byte[0]),
3286: 0);
3287: assertEquals(1, pstmt.executeUpdate());
3288:
3289: // Write non zero fields
3290: pstmt.setInt(1, 2);
3291: pstmt.setAsciiStream(2, new ByteArrayInputStream(new byte[1]),
3292: 1);
3293: pstmt.setCharacterStream(3, new StringReader("TEST"), 4);
3294: pstmt.setCharacterStream(4, new StringReader(""), 0);
3295: pstmt.setUnicodeStream(4,
3296: new ByteArrayInputStream(new byte[2]), 2);
3297: pstmt.setBinaryStream(5, new ByteArrayInputStream(new byte[1]),
3298: 1);
3299: assertEquals(1, pstmt.executeUpdate());
3300: pstmt.close();
3301:
3302: ResultSet rs = stmt
3303: .executeQuery("select * from #test order by id");
3304: assertTrue(rs.next());
3305: assertTrue(rs.next());
3306:
3307: // Update non zero length fields to zero
3308: rs.updateAsciiStream(2, new ByteArrayInputStream(new byte[0]),
3309: 0);
3310: rs.updateCharacterStream(3, new StringReader(""), 0);
3311: Clob clob = rs.getClob(4);
3312: clob.truncate(0);
3313: rs.updateClob(4, clob);
3314: rs.updateBinaryStream(5, new ByteArrayInputStream(new byte[0]),
3315: 0);
3316: rs.updateRow();
3317: rs.close();
3318:
3319: // Test all fields now zero length
3320: rs = stmt.executeQuery("select * from #test order by id");
3321: while (rs.next()) {
3322: assertEquals("AsciiStream", "", rs.getString(2));
3323: assertEquals("Reader", "", rs.getString(3));
3324: assertEquals("UnicodeStream", "", rs.getString(4));
3325: assertEquals("byteStream", 0, rs.getBytes(5).length);
3326: }
3327: rs.close();
3328: stmt.close();
3329: }
3330:
3331: /**
3332: * Test for bug [1172405] BLOB/CLOB position methods fail.
3333: */
3334: public void testBlobMethods() throws Exception {
3335: Statement stmt = con.createStatement();
3336: stmt
3337: .execute("CREATE TABLE #TESTBLOB (id int, blob image null)");
3338: assertEquals(1, stmt
3339: .executeUpdate("INSERT INTO #TESTBLOB (id) VALUES (1)"));
3340: assertEquals(
3341: 1,
3342: stmt
3343: .executeUpdate("INSERT INTO #TESTBLOB (id, blob) VALUES (2, 0x4445)"));
3344: assertEquals(
3345: 1,
3346: stmt
3347: .executeUpdate("INSERT INTO #TESTBLOB (id, blob) VALUES (3, 0x4142434445464748)"));
3348: ResultSet rs = stmt.executeQuery("SELECT * FROM #TESTBLOB");
3349: assertNotNull(rs);
3350: assertTrue(rs.next());
3351: assertEquals(1, rs.getInt(1));
3352: Blob blob = rs.getBlob(2);
3353: assertNull(blob);
3354: assertTrue(rs.next());
3355: assertEquals(2, rs.getInt(1));
3356: Blob pattern = rs.getBlob(2);
3357: assertNotNull(pattern);
3358: assertTrue(rs.next());
3359: assertEquals(3, rs.getInt(1));
3360: blob = rs.getBlob(2);
3361: assertEquals(8, blob.length());
3362: assertEquals("ABCDEFGH", new String(blob.getBytes(1, 8)));
3363: assertEquals(4, blob.position(pattern, 1));
3364: assertEquals(-1, blob.position(pattern, 8));
3365: assertEquals(3, blob.position(new byte[] { 0x43, 0x44 }, 1));
3366: assertEquals(-1, blob.position(new byte[] { 0x43, 0x44 }, 8));
3367: byte buf[] = new byte[(int) blob.length()];
3368: InputStream is = blob.getBinaryStream();
3369: assertEquals((int) blob.length(), is.read(buf));
3370: assertEquals(-1, is.read());
3371: assertEquals("ABCDEFGH", new String(buf));
3372: }
3373:
3374: /**
3375: * Test for bug [1172405] BLOB/CLOB position methods fail.
3376: */
3377: public void testClobMethods() throws Exception {
3378: Statement stmt = con.createStatement();
3379: stmt.execute("CREATE TABLE #TESTCLOB (id int, clob text null)");
3380: assertEquals(1, stmt
3381: .executeUpdate("INSERT INTO #TESTCLOB (id) VALUES (1)"));
3382: assertEquals(
3383: 1,
3384: stmt
3385: .executeUpdate("INSERT INTO #TESTCLOB (id, clob) VALUES (2, 'CD')"));
3386: assertEquals(
3387: 1,
3388: stmt
3389: .executeUpdate("INSERT INTO #TESTCLOB (id, clob) VALUES (3, 'ABCDEFGH')"));
3390: ResultSet rs = stmt.executeQuery("SELECT * FROM #TESTCLOB");
3391: assertNotNull(rs);
3392: assertTrue(rs.next());
3393: assertEquals(1, rs.getInt(1));
3394: Clob clob = rs.getClob(2);
3395: assertNull(clob);
3396: assertTrue(rs.next());
3397: assertEquals(2, rs.getInt(1));
3398: Clob pattern = rs.getClob(2);
3399: assertNotNull(pattern);
3400: assertTrue(rs.next());
3401: assertEquals(3, rs.getInt(1));
3402: clob = rs.getClob(2);
3403: assertEquals(8, clob.length());
3404: assertEquals("ABCDEFGH", clob.getSubString(1, 8));
3405: assertEquals(3, clob.position(pattern, 1));
3406: assertEquals(-1, clob.position(pattern, 8));
3407: assertEquals(3, clob.position("CD", 1));
3408: assertEquals(-1, clob.position("CD", 8));
3409: Reader rdr = clob.getCharacterStream();
3410: char buf[] = new char[(int) clob.length()];
3411: assertEquals((int) clob.length(), rdr.read(buf));
3412: assertEquals(-1, rdr.read());
3413: assertEquals("ABCDEFGH", new String(buf));
3414: byte bbuf[] = new byte[(int) clob.length()];
3415: InputStream is = clob.getAsciiStream();
3416: assertEquals((int) clob.length(), is.read(bbuf));
3417: assertEquals(-1, is.read());
3418: assertEquals("ABCDEFGH", new String(bbuf));
3419: }
3420:
3421: private byte[] getBlobTestData() {
3422: return blobData;
3423: }
3424:
3425: private byte[] getNewBlobTestData() {
3426: return newBlobData;
3427: }
3428:
3429: private String getClobTestData() {
3430: return clobData;
3431: }
3432:
3433: private String getNewClobTestData() {
3434: return newClobData;
3435: }
3436:
3437: /**
3438: * Implements an <code>InputStream</code> that only returns a limited
3439: * number of bytes on read (less than the requested number of bytes).
3440: * <p>
3441: * Used for testing <code>Blob</code> insert behavior.
3442: */
3443: static class RealInputStream extends InputStream {
3444: /**
3445: * Length of the stream.
3446: */
3447: static final int LENGTH = 10000;
3448:
3449: /**
3450: * Current position in the stream.
3451: */
3452: private int pos = 0;
3453:
3454: public int read() {
3455: if (++pos > LENGTH) {
3456: return -1;
3457: }
3458: return pos % 256;
3459: }
3460:
3461: public int read(byte[] b) {
3462: return read(b, 0, b.length);
3463: }
3464:
3465: public int read(byte[] b, int off, int len) {
3466: int res = read();
3467: if (res == -1) {
3468: return -1;
3469: } else {
3470: b[off] = (byte) res;
3471: return 1;
3472: }
3473: }
3474: }
3475:
3476: /**
3477: * Compares long byte arrays.
3478: */
3479: private boolean compare(byte[] b1, byte[] b2) {
3480: if (b1.length != b2.length) {
3481: System.out.println("Compare failed: lengths differ");
3482: return false;
3483: }
3484: for (int i = 0; i < b1.length; i++) {
3485: if (b1[i] != b2[i]) {
3486: System.out.println("Compare failed: bytes at " + i
3487: + " differ [" + b1[i] + "] [" + b2[i] + "]");
3488: return false;
3489: }
3490: }
3491: return true;
3492: }
3493:
3494: /**
3495: * Compare long <code>String</code>s.
3496: */
3497: public boolean compare(String s1, String s2) {
3498: if (s1.length() != s2.length()) {
3499: System.out.println("Compare failed: lengths differ");
3500: return false;
3501: }
3502: for (int i = 0; i < s1.length(); i++) {
3503: if (s1.charAt(i) != s2.charAt(i)) {
3504: System.out.println("Compare failed: bytes at " + i
3505: + " differ [" + s1.charAt(i) + "] ["
3506: + s2.charAt(i) + "]");
3507: return false;
3508: }
3509: }
3510: return true;
3511: }
3512:
3513: public static void main(String[] args) {
3514: junit.textui.TestRunner.run(LOBTest.class);
3515: }
3516: }
|