|
| 1 | +package org.postgresql.test.jdbc2; |
| 2 | + |
| 3 | +import org.postgresql.test.JDBC2Tests; |
| 4 | +import junit.framework.TestCase; |
| 5 | +import java.io.*; |
| 6 | +import java.sql.*; |
| 7 | + |
| 8 | +import org.postgresql.largeobject.*; |
| 9 | + |
| 10 | +/** |
| 11 | + * $Id: BlobTest.java,v 1.1 2001/02/14 17:45:17 peter Exp $ |
| 12 | + * |
| 13 | + * Some simple tests based on problems reported by users. Hopefully these will |
| 14 | + * help prevent previous problems from re-occuring ;-) |
| 15 | + * |
| 16 | + */ |
| 17 | +public class BlobTest extends TestCase { |
| 18 | + |
| 19 | + public BlobTest(String name) { |
| 20 | + super(name); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * The table format used by this TestCase |
| 25 | + */ |
| 26 | + private static final String BLOB_TABLE_FMT = "id name,lo oid"; |
| 27 | + |
| 28 | + /** |
| 29 | + * Tests one method of uploading a blob to the database |
| 30 | + */ |
| 31 | + public void testUploadBlob_LOOP() { |
| 32 | + try { |
| 33 | + Connection con = JDBC2Tests.openDB(); |
| 34 | + |
| 35 | + JDBC2Tests.createTable(con,BLOB_TABLE_FMT); |
| 36 | + |
| 37 | + con.setAutoCommit(false); |
| 38 | + assert(!con.getAutoCommit()); |
| 39 | + |
| 40 | + assert(uploadFile(con,"build.xml",LOOP)>0); |
| 41 | + |
| 42 | + // Now compare the blob & the file. Note this actually tests the |
| 43 | + // InputStream implementation! |
| 44 | + assert(compareBlobs(con)); |
| 45 | + |
| 46 | + JDBC2Tests.closeDB(con); |
| 47 | + } catch(Exception ex) { |
| 48 | + assert(ex.getMessage(),false); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Tests one method of uploading a blob to the database |
| 54 | + */ |
| 55 | + public void testUploadBlob_NATIVE() { |
| 56 | + try { |
| 57 | + Connection con = JDBC2Tests.openDB(); |
| 58 | + |
| 59 | + JDBC2Tests.createTable(con,BLOB_TABLE_FMT); |
| 60 | + |
| 61 | + con.setAutoCommit(false); |
| 62 | + assert(!con.getAutoCommit()); |
| 63 | + |
| 64 | + assert(uploadFile(con,"build.xml",NATIVE_STREAM)>0); |
| 65 | + |
| 66 | + // Now compare the blob & the file. Note this actually tests the |
| 67 | + // InputStream implementation! |
| 68 | + assert(compareBlobs(con)); |
| 69 | + |
| 70 | + JDBC2Tests.closeDB(con); |
| 71 | + } catch(Exception ex) { |
| 72 | + assert(ex.getMessage(),false); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static final int LOOP = 0; // LargeObject API using loop |
| 77 | + private static final int NATIVE_STREAM = 1; // LargeObject API using OutputStream |
| 78 | + private static final int JDBC_STREAM = 2; // JDBC API using OutputStream |
| 79 | + |
| 80 | + /** |
| 81 | + * Helper - uploads a file into a blob using old style methods. We use this |
| 82 | + * because it always works, and we can use it as a base to test the new |
| 83 | + * methods. |
| 84 | + */ |
| 85 | + private int uploadFile(Connection con,String file,int method) throws Exception { |
| 86 | + LargeObjectManager lom = ((org.postgresql.Connection)con).getLargeObjectAPI(); |
| 87 | + |
| 88 | + FileInputStream fis = new FileInputStream(file); |
| 89 | + |
| 90 | + int oid = lom.create(LargeObjectManager.READWRITE); |
| 91 | + LargeObject blob = lom.open(oid); |
| 92 | + |
| 93 | + int s,t; |
| 94 | + byte buf[]; |
| 95 | + OutputStream os; |
| 96 | + |
| 97 | + switch(method) |
| 98 | + { |
| 99 | + case LOOP: |
| 100 | + buf = new byte[2048]; |
| 101 | + t=0; |
| 102 | + while((s=fis.read(buf,0,buf.length))>0) { |
| 103 | + t+=s; |
| 104 | + blob.write(buf,0,s); |
| 105 | + } |
| 106 | + break; |
| 107 | + |
| 108 | + case NATIVE_STREAM: |
| 109 | + os = blob.getOutputStream(); |
| 110 | + s= fis.read(); |
| 111 | + while(s>-1) { |
| 112 | + os.write(s); |
| 113 | + s=fis.read(); |
| 114 | + } |
| 115 | + os.close(); |
| 116 | + break; |
| 117 | + |
| 118 | + case JDBC_STREAM: |
| 119 | + File f = new File(file); |
| 120 | + PreparedStatement ps = con.prepareStatement(JDBC2Tests.insert("?")); |
| 121 | + ps.setBinaryStream(1,fis,(int) f.length()); |
| 122 | + ps.execute(); |
| 123 | + break; |
| 124 | + |
| 125 | + default: |
| 126 | + assert("Unknown method in uploadFile",false); |
| 127 | + } |
| 128 | + |
| 129 | + blob.close(); |
| 130 | + fis.close(); |
| 131 | + |
| 132 | + // Insert into the table |
| 133 | + Statement st = con.createStatement(); |
| 134 | + st.executeUpdate(JDBC2Tests.insert("id,lo","'"+file+"',"+oid)); |
| 135 | + con.commit(); |
| 136 | + st.close(); |
| 137 | + |
| 138 | + return oid; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Helper - compares the blobs in a table with a local file. Note this alone |
| 143 | + * tests the InputStream methods! |
| 144 | + */ |
| 145 | + private boolean compareBlobs(Connection con) throws Exception { |
| 146 | + boolean result=true; |
| 147 | + |
| 148 | + LargeObjectManager lom = ((org.postgresql.Connection)con).getLargeObjectAPI(); |
| 149 | + |
| 150 | + Statement st = con.createStatement(); |
| 151 | + ResultSet rs = st.executeQuery(JDBC2Tests.select("id,lo")); |
| 152 | + assert(rs!=null); |
| 153 | + |
| 154 | + while(rs.next()) { |
| 155 | + String file = rs.getString(1); |
| 156 | + int oid = rs.getInt(2); |
| 157 | + |
| 158 | + FileInputStream fis = new FileInputStream(file); |
| 159 | + LargeObject blob = lom.open(oid); |
| 160 | + InputStream bis = blob.getInputStream(); |
| 161 | + |
| 162 | + int f=fis.read(); |
| 163 | + int b=bis.read(); |
| 164 | + int c=0; |
| 165 | + while(f>=0 && b>=0 & result) { |
| 166 | + result=(f==b); |
| 167 | + f=fis.read(); |
| 168 | + b=bis.read(); |
| 169 | + c++; |
| 170 | + } |
| 171 | + result=result && f==-1 && b==-1; |
| 172 | + |
| 173 | + if(!result) |
| 174 | + System.out.println("\nBlob compare failed at "+c+" of "+blob.size()); |
| 175 | + |
| 176 | + blob.close(); |
| 177 | + fis.close(); |
| 178 | + } |
| 179 | + rs.close(); |
| 180 | + st.close(); |
| 181 | + |
| 182 | + return result; |
| 183 | + } |
| 184 | +} |
0 commit comments