1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbc4; 23 24 import junit.extensions.TestSetup; 25 import junit.framework.Test; 26 27 import org.apache.derbyTesting.junit.BaseJDBCTestCase; 28 import org.apache.derbyTesting.junit.BaseJDBCTestSetup; 29 30 import java.io.ByteArrayInputStream ; 31 import java.io.InputStream ; 32 import java.io.IOException ; 33 import java.io.Reader ; 34 import java.io.StringReader ; 35 import java.sql.*; 36 37 42 public class BlobClobTestSetup 43 extends BaseJDBCTestSetup { 44 45 46 public static final int ID_NULLVALUES = 1; 47 48 public static final int ID_SAMPLEVALUES = 2; 49 50 51 private static final byte[] blobData = new byte[] { 52 0x65, 0x66, 0x67, 0x68, 0x69, 53 0x69, 0x68, 0x67, 0x66, 0x65 54 }; 55 56 private static final String clobData = 57 "This is a string, inserted into a CLOB"; 58 59 64 public BlobClobTestSetup(Test test) { 65 super(test); 66 } 67 68 72 protected void setUp() 73 throws IOException , SQLException { 74 Connection con = getConnection(); 75 Statement stmt = con.createStatement(); 76 stmt.execute("create table BLOBCLOB (ID int primary key, " + 77 "BLOBDATA blob(1k)," + 78 "CLOBDATA clob(1k))"); 79 stmt.execute("insert into BLOBCLOB VALUES " + 80 "(" + ID_NULLVALUES + ", null, null)"); 81 stmt.execute("insert into BLOBCLOB VALUES " + 83 "(" + ID_SAMPLEVALUES + ", null, null)"); 84 stmt.close(); 85 } 86 87 91 protected void tearDown() 92 throws Exception { 93 Connection con = getConnection(); 94 Statement stmt = con.createStatement(); 95 stmt.execute("drop table BLOBCLOB"); 96 stmt.close(); 97 super.tearDown(); 98 } 99 100 107 public static Blob getSampleBlob(Connection con) 108 throws SQLException { 109 InputStream blobInput = new ByteArrayInputStream (blobData, 0, blobData.length); 110 PreparedStatement pStmt = 111 con.prepareStatement("update BLOBCLOB set BLOBDATA = ? where ID = ?"); 112 try { 113 blobInput.reset(); 114 } catch (IOException ioe) { 115 fail("Failed to reset blob input stream: " + ioe.getMessage()); 116 } 117 pStmt.setBlob(1, blobInput, blobData.length); 118 pStmt.setInt(2, ID_SAMPLEVALUES); 119 assertEquals("Invalid update count", 1, pStmt.executeUpdate()); 120 Statement stmt = con.createStatement(); 121 ResultSet rs = stmt.executeQuery("select BLOBDATA from BLOBCLOB where ID = " + 122 ID_SAMPLEVALUES); 123 rs.next(); 124 Blob blob = rs.getBlob(1); 125 rs.close(); 126 stmt.close(); 127 return blob; 128 } 129 130 137 public static Clob getSampleClob(Connection con) 138 throws SQLException { 139 Reader clobInput = new StringReader (clobData); 140 PreparedStatement pStmt = 141 con.prepareStatement("update BLOBCLOB set CLOBDATA = ? where ID = ?"); 142 try { 143 clobInput.reset(); 144 } catch (IOException ioe) { 145 fail("Failed to reset clob input stream: " + ioe.getMessage()); 146 } 147 pStmt.setClob(1, clobInput, clobData.length()); 148 pStmt.setInt(2, ID_SAMPLEVALUES); 149 assertEquals("Invalid update count", 1, pStmt.executeUpdate()); 150 Statement stmt = con.createStatement(); 151 ResultSet rs = stmt.executeQuery("select CLOBDATA from BLOBCLOB where ID = " + 152 ID_SAMPLEVALUES); 153 rs.next(); 154 Clob clob = rs.getClob(1); 155 rs.close(); 156 stmt.close(); 157 return clob; 158 } 159 160 } | Popular Tags |