88import org .postgresql .largeobject .*;
99
1010/*
11- * $Id: BlobTest.java,v 1.8 2003/05/29 03:21:32 barry Exp $
11+ * $Id: BlobTest.java,v 1.9 2003/08/15 18:45:11 barry Exp $
1212 *
1313 * Some simple tests based on problems reported by users. Hopefully these will
1414 * help prevent previous problems from re-occuring ;-)
@@ -54,7 +54,9 @@ public void testUploadBlob_LOOP()
5454
5555// Now compare the blob & the file. Note this actually tests the
5656// InputStream implementation!
57+ assertTrue (compareBlobsLOAPI ());
5758assertTrue (compareBlobs ());
59+ assertTrue (compareClobs ());
5860
5961con .setAutoCommit (true );
6062}
@@ -153,10 +155,10 @@ private int uploadFile(String file, int method) throws Exception
153155}
154156
155157/*
156- * Helper - compares the blobs in a table with a local file. Note thisalone
157- *tests theInputStream methods!
158+ * Helper - compares the blobs in a table with a local file. Note thisuses
159+ * thepostgresql specific Large Object API
158160 */
159- private boolean compareBlobs ()throws Exception
161+ private boolean compareBlobsLOAPI ()throws Exception
160162{
161163boolean result =true ;
162164
@@ -188,7 +190,7 @@ private boolean compareBlobs() throws Exception
188190result =result &&f == -1 &&b == -1 ;
189191
190192if (!result )
191- assertTrue ("Blob compare failed at " +c +" of " +blob .size (),false );
193+ assertTrue ("Large Object API Blob compare failed at " +c +" of " +blob .size (),false );
192194
193195blob .close ();
194196fis .close ();
@@ -198,4 +200,91 @@ private boolean compareBlobs() throws Exception
198200
199201return result ;
200202}
203+
204+ /*
205+ * Helper - compares the blobs in a table with a local file. This uses the
206+ * jdbc java.sql.Blob api
207+ */
208+ private boolean compareBlobs ()throws Exception
209+ {
210+ boolean result =true ;
211+
212+ Statement st =con .createStatement ();
213+ ResultSet rs =st .executeQuery (TestUtil .selectSQL ("testblob" ,"id,lo" ));
214+ assertNotNull (rs );
215+
216+ while (rs .next ())
217+ {
218+ String file =rs .getString (1 );
219+ Blob blob =rs .getBlob (2 );
220+
221+ FileInputStream fis =new FileInputStream (file );
222+ InputStream bis =blob .getBinaryStream ();
223+
224+ int f =fis .read ();
225+ int b =bis .read ();
226+ int c =0 ;
227+ while (f >=0 &&b >=0 &result )
228+ {
229+ result = (f ==b );
230+ f =fis .read ();
231+ b =bis .read ();
232+ c ++;
233+ }
234+ result =result &&f == -1 &&b == -1 ;
235+
236+ if (!result )
237+ assertTrue ("JDBC API Blob compare failed at " +c +" of " +blob .length (),false );
238+
239+ bis .close ();
240+ fis .close ();
241+ }
242+ rs .close ();
243+ st .close ();
244+
245+ return result ;
246+ }
247+
248+ /*
249+ * Helper - compares the clobs in a table with a local file.
250+ */
251+ private boolean compareClobs ()throws Exception
252+ {
253+ boolean result =true ;
254+
255+ Statement st =con .createStatement ();
256+ ResultSet rs =st .executeQuery (TestUtil .selectSQL ("testblob" ,"id,lo" ));
257+ assertNotNull (rs );
258+
259+ while (rs .next ())
260+ {
261+ String file =rs .getString (1 );
262+ Clob clob =rs .getClob (2 );
263+
264+ FileInputStream fis =new FileInputStream (file );
265+ InputStream bis =clob .getAsciiStream ();
266+
267+ int f =fis .read ();
268+ int b =bis .read ();
269+ int c =0 ;
270+ while (f >=0 &&b >=0 &result )
271+ {
272+ result = (f ==b );
273+ f =fis .read ();
274+ b =bis .read ();
275+ c ++;
276+ }
277+ result =result &&f == -1 &&b == -1 ;
278+
279+ if (!result )
280+ assertTrue ("Clob compare failed at " +c +" of " +clob .length (),false );
281+
282+ bis .close ();
283+ fis .close ();
284+ }
285+ rs .close ();
286+ st .close ();
287+
288+ return result ;
289+ }
201290}