1 24 25 package org.objectweb.cjdbc.scenario.tools.testlet; 26 27 import java.io.File ; 28 import java.sql.Blob ; 29 import java.sql.Connection ; 30 import java.sql.PreparedStatement ; 31 import java.sql.ResultSet ; 32 33 import org.objectweb.cjdbc.scenario.tools.ScenarioUtility; 34 35 41 public class BlobTestLet extends AbstractConnectionTestLet 42 { 43 44 49 public BlobTestLet(Connection con) 50 { 51 super(con); 52 config.put(TABLE_NAME, "BLOB"); 53 config.put(COLUMN_NAME, "blob"); 54 } 55 56 59 public void execute() throws Exception 60 { 61 String storeFile = (String ) config.get(FILE_NAME); 62 String tableName = (String ) config.get(TABLE_NAME); 63 String columnName = (String ) config.get(COLUMN_NAME); 64 65 File fis = new File (storeFile); 66 if(!fis.exists()) 67 fis = new File (getClass().getResource(storeFile).getFile()); 68 69 if (storeFile == null || tableName == null || !fis.exists()) 70 throw new Exception ("Cannot run BlobTestLet with given arguments"); 71 72 boolean ok = true; 73 String query = "Delete from " + tableName + " where id='1'"; 75 PreparedStatement ps1, ps2; 76 jdbcConnection.createStatement().executeUpdate(query); 77 78 80 query = "Insert into " + tableName + " values(1,?)"; 81 ps1 = jdbcConnection.prepareStatement(query); 82 if (useCJDBCClass()) 83 { 84 Blob bob = new org.objectweb.cjdbc.driver.Blob(ScenarioUtility 85 .readBinary(fis)); 86 ps1.setBlob(1, bob); 87 } 88 else 89 { 90 ps1.setBytes(1, ScenarioUtility.readBinary(fis)); 91 } 92 ps1.executeUpdate(); 93 94 query = "select * from " + tableName + " where id=1"; 96 ps2 = jdbcConnection.prepareStatement(query); 97 ResultSet rs = ps2.executeQuery(); 98 assertFalse("Unexpected null result", rs.wasNull()); 99 assertTrue("Unexpected result", rs.first()); 100 101 byte[] lisette; 102 if (useCJDBCClass()) 103 { 104 Blob blisette = rs.getBlob(columnName); 105 lisette = blisette.getBytes(1, (int) blisette.length()); 106 } 107 else 108 lisette = rs.getBytes(columnName); 109 110 String filename = (useCJDBCClass()) ? fis.getAbsolutePath() + ".blobed" : fis.getAbsolutePath() 112 + ".byted"; 113 File fos = new File (filename); 114 ScenarioUtility.writeBinary(lisette, fos); 115 116 if (fis.length() != fos.length()) 118 { 119 System.out.println("Lenght are different:" + fis.length() + ";" 120 + fos.length()); 121 ok = false; 122 } 123 ps1.close(); 125 ps2.close(); 126 fos.delete(); 127 128 assertTrue("BlobTestLet failed with:" + storeFile 129 + ". CalledBlob was used:" + useCJDBCClass(), ok); 130 131 } 132 133 } | Popular Tags |