KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > testlet > BlobTestLet


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.tools.testlet;
26
27 import java.io.File JavaDoc;
28 import java.sql.Blob JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32
33 import org.objectweb.cjdbc.scenario.tools.ScenarioUtility;
34
35 /**
36  * Make tests on blobs, using bytes or directly the blob object
37  *
38  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
39  * @version 1.0
40  */

41 public class BlobTestLet extends AbstractConnectionTestLet
42 {
43
44   /**
45    * Creates a new <code>BlobTestLet</code> object
46    *
47    * @param con connection to use for testing
48    */

49   public BlobTestLet(Connection JavaDoc con)
50   {
51     super(con);
52     config.put(TABLE_NAME, "BLOB");
53     config.put(COLUMN_NAME, "blob");
54   }
55
56   /**
57    * @see org.objectweb.cjdbc.scenario.tools.testlet.AbstractTestLet#execute()
58    */

59   public void execute() throws Exception JavaDoc
60   {
61     String JavaDoc storeFile = (String JavaDoc) config.get(FILE_NAME);
62     String JavaDoc tableName = (String JavaDoc) config.get(TABLE_NAME);
63     String JavaDoc columnName = (String JavaDoc) config.get(COLUMN_NAME);
64     
65     File JavaDoc fis = new File JavaDoc(storeFile);
66     if(!fis.exists())
67       fis = new File JavaDoc(getClass().getResource(storeFile).getFile());
68
69     if (storeFile == null || tableName == null || !fis.exists())
70       throw new Exception JavaDoc("Cannot run BlobTestLet with given arguments");
71
72     boolean ok = true;
73     // Delete previous entry
74
String JavaDoc query = "Delete from " + tableName + " where id='1'";
75     PreparedStatement JavaDoc ps1, ps2;
76     jdbcConnection.createStatement().executeUpdate(query);
77
78     // Store file in database
79

80     query = "Insert into " + tableName + " values(1,?)";
81     ps1 = jdbcConnection.prepareStatement(query);
82     if (useCJDBCClass())
83     {
84       Blob JavaDoc 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     // Read File from database
95
query = "select * from " + tableName + " where id=1";
96     ps2 = jdbcConnection.prepareStatement(query);
97     ResultSet JavaDoc 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 JavaDoc blisette = rs.getBlob(columnName);
105       lisette = blisette.getBytes(1, (int) blisette.length());
106     }
107     else
108       lisette = rs.getBytes(columnName);
109
110     // Write Retrieved blob
111
String JavaDoc filename = (useCJDBCClass()) ? fis.getAbsolutePath() + ".blobed" : fis.getAbsolutePath()
112         + ".byted";
113     File JavaDoc fos = new File JavaDoc(filename);
114     ScenarioUtility.writeBinary(lisette, fos);
115
116     // Test retrieved Blob
117
if (fis.length() != fos.length())
118     {
119       System.out.println("Lenght are different:" + fis.length() + ";"
120           + fos.length());
121       ok = false;
122     }
123     // Close statements
124
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