📄 lobtest.java
字号:
// Test ResultSet.getBlob() Blob blob = rs.getBlob(1); assertNotNull(blob); byte[] tmpData = new byte[data.length / 2]; // Offset data copy by 1 System.arraycopy(data, 1, tmpData, 0, tmpData.length); // Test Blob.getBytes() assertTrue(Arrays.equals(tmpData, blob.getBytes(2L, tmpData.length))); assertFalse(rs.next()); stmt2.close(); rs.close(); } public void testBlobLength1() throws Exception { byte[] data = getBlobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #bloblength1 (data IMAGE)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #bloblength1 (data) VALUES (?)"); // Test PreparedStatement.setBytes() pstmt.setBytes(1, data); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #bloblength1"); assertTrue(rs.next()); // Test ResultSet.getBlob() Blob blob = rs.getBlob(1); assertNotNull(blob); // Test Blob.length() assertEquals(data.length, blob.length()); assertFalse(rs.next()); stmt2.close(); rs.close(); } public void testBlobTruncate1() throws Exception { byte[] data = getBlobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #blobtruncate1 (data IMAGE)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #blobtruncate1 (data) VALUES (?)"); // Test PreparedStatement.setBytes() pstmt.setBytes(1, data); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobtruncate1"); assertTrue(rs.next()); // Test ResultSet.getBlob() Blob blob = rs.getBlob(1); assertNotNull(blob); byte[] tmpData = new byte[data.length / 2]; System.arraycopy(data, 0, tmpData, 0, tmpData.length); // Test Blob.truncate() blob.truncate(tmpData.length); assertEquals(tmpData.length, blob.length()); // Test Blob.getBytes() assertTrue(Arrays.equals(tmpData, blob.getBytes(1L, (int) blob.length()))); assertFalse(rs.next()); stmt2.close(); rs.close(); } /** * Test for bug [1062395] Empty (but not null) blobs should return byte[0]. */ public void testBlobEmpty() throws Exception { Statement stmt = con.createStatement(); assertEquals(0, stmt.executeUpdate("CREATE TABLE #blobEmpty (data IMAGE)")); assertEquals(1, stmt.executeUpdate("INSERT INTO #blobEmpty (data) values ('')")); ResultSet rs = stmt.executeQuery("SELECT * FROM #blobEmpty"); assertTrue(rs.next()); Blob blob = rs.getBlob(1); assertEquals(0, blob.length()); assertEquals(0, blob.getBytes(1, 0).length); rs.close(); stmt.close(); } /************************************************************************* ************************************************************************* ** CLOB TESTS ** ************************************************************************* *************************************************************************/ public void testClobGet1() throws Exception { String data = getClobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #clobget1 (data TEXT)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #clobget1 (data) VALUES (?)"); pstmt.setString(1, data); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobget1"); assertTrue(rs.next()); // Test ResultSet.getString() assertTrue(data.equals(rs.getString(1))); // Test ResultSet.getAsciiStream() InputStream is = rs.getAsciiStream(1); compareInputStreams(new ByteArrayInputStream(data.getBytes("ASCII")), is); // Test ResultSet.getUnicodeStream(() InputStream is2 = rs.getUnicodeStream(1); compareInputStreams(new ByteArrayInputStream(data.getBytes("UTF-16BE")), is2); // Test ResultSet.getCharacterStream() Reader rdr = rs.getCharacterStream(1); compareReaders(new StringReader(data), rdr); // Test ResultSet.getClob() Clob clob = rs.getClob(1); assertNotNull(clob); // Test Clob.length() assertEquals(clob.length(), data.length()); // Test Clob.getSubString(0, length); should fail try { clob.getSubString(0L, (int) clob.length()); fail("Clob.getSubString(0, length) should fail."); } catch (SQLException ex) { assertEquals("HY090", ex.getSQLState()); } // Test Clob.getSubString() assertTrue(data.equals(clob.getSubString(1L, (int) clob.length()))); // Test Clob.getAsciiStream() InputStream is3 = clob.getAsciiStream(); compareInputStreams(new ByteArrayInputStream(data.getBytes("ASCII")), is3); // Test Clob.getCharacterStream() Reader rdr2 = rs.getCharacterStream(1); compareReaders(new StringReader(data), rdr2); assertFalse(rs.next()); stmt2.close(); rs.close(); } public void testClobGet2() throws Exception { String data = getClobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #clobget2 (data TEXT)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #clobget2 (data) VALUES (?)"); // Test PreparedStatement.setCharacterStream() pstmt.setCharacterStream(1, new StringReader(data), data.length()); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobget2"); assertTrue(rs.next()); // Test ResultSet.getObject() - Clob Object result = rs.getObject(1); assertTrue(result instanceof Clob); Clob clob = (Clob) result; assertEquals(data.length(), clob.length()); // Test Clob.getSubString() assertTrue(data.equals(clob.getSubString(1L, (int) clob.length()))); assertFalse(rs.next()); stmt2.close(); rs.close(); } public void testClobSet1() throws Exception { String data = getClobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #clobset1 (data TEXT)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #clobset1 (data) VALUES (?)"); // Test PreparedStatement.setAsciiStream() pstmt.setAsciiStream(1, new ByteArrayInputStream(data.getBytes("ASCII")), data.length()); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobset1"); assertTrue(rs.next()); // Test ResultSet.getString() assertTrue(data.equals(rs.getString(1))); assertFalse(rs.next()); stmt2.close(); rs.close(); } public void testClobSet2() throws Exception { String data = getClobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #clobset2 (data TEXT)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #clobset2 (data) VALUES (?)"); // Test PreparedStatement.setCharacterStream() pstmt.setCharacterStream(1, new StringReader(data), data.length()); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobset2"); assertTrue(rs.next()); // Test ResultSet.getString() assertTrue(data.equals(rs.getString(1))); assertFalse(rs.next()); stmt2.close(); rs.close(); } public void testClobSet3() throws Exception { String data = getClobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #clobset3 (data TEXT)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #clobset3 (data) VALUES (?)"); // Test PreparedStatement.setString() pstmt.setString(1, data); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobset3"); assertTrue(rs.next()); Clob clob = rs.getClob(1); data = getNewClobTestData(); // Test Clob.setBytes() clob.setString(1, data); assertTrue(data.equals(clob.getSubString(1L, (int) clob.length()))); assertFalse(rs.next()); PreparedStatement pstmt2 = con.prepareStatement("UPDATE #clobset3 SET data = ?"); // Test PreparedStatement.setClob() pstmt2.setClob(1, clob); assertEquals(1, pstmt2.executeUpdate()); pstmt2.close(); stmt2.close(); rs.close(); Statement stmt3 = con.createStatement(); ResultSet rs2 = stmt3.executeQuery("SELECT data FROM #clobset3"); assertTrue(rs2.next()); // Test ResultSet.getString() assertTrue(data.equals(rs2.getString(1))); assertFalse(rs2.next()); stmt3.close(); rs2.close(); } public void testClobSet4() throws Exception { String data = getClobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #clobset4 (data TEXT)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #clobset4 (data) VALUES (?)"); // Test PreparedStatement.setUnicodeStream() pstmt.setUnicodeStream(1, new ByteArrayInputStream(data.getBytes("UTF-16BE")), data.length() * 2); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobset4"); assertTrue(rs.next()); // Test ResultSet.getString() assertTrue(data.equals(rs.getString(1))); assertFalse(rs.next()); stmt2.close(); rs.close(); } public void testClobSet5() throws Exception { String data = getClobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #clobset5 (data TEXT)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #clobset5 (data) VALUES (?)"); // Test PreparedStatement.setObject(int,String) pstmt.setObject(1, data); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobset5"); assertTrue(rs.next()); // Test ResultSet.getString() assertTrue(data.equals(rs.getString(1))); assertFalse(rs.next()); stmt2.close(); rs.close(); } public void testClobSet6() throws Exception { String data = getClobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #clobset6 (data TEXT)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #clobset6 (data) VALUES (?)"); // Test PreparedStatement.setString() pstmt.setString(1, data); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #clobset6"); assertTrue(rs.next()); Clob clob = rs.getClob(1); data = getNewClobTestData(); // Test Clob.setBytes() clob.setString(1, data); assertTrue(data.equals(clob.getSubString(1L, (int) clob.length()))); assertFalse(rs.next()); PreparedStatement pstmt2 = con.prepareStatement("UPDATE #clobset6 SET data = ?"); // Test PreparedStatement.setObject(int,Clob) pstmt2.setObject(1, clob); assertEquals(1, pstmt2.executeUpdate()); pstmt2.close(); stmt2.close(); rs.close();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -