KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbc4 > BlobClobTestSetup


1 /*
2  
3    Derby - Class BlobClobTestSetup
4  
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11  
12       http://www.apache.org/licenses/LICENSE-2.0
13  
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19  
20  */

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 JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.Reader JavaDoc;
34 import java.io.StringReader JavaDoc;
35 import java.sql.*;
36
37 /**
38  * Create a table with one column for a blob and one column for a clob.
39  * This is shared between tests that need a blob or a clob, and is required
40  * because the createBlob/-Clob methods are not yet implemented.
41  */

42 public class BlobClobTestSetup
43     extends BaseJDBCTestSetup {
44
45     /** Constant for accessing the row with null values. */
46     public static final int ID_NULLVALUES = 1;
47     /** Constant for accessing the row with sample values. */
48     public static final int ID_SAMPLEVALUES = 2;
49
50     /** Blob data. */
51     private static final byte[] blobData = new byte[] {
52         0x65, 0x66, 0x67, 0x68, 0x69,
53         0x69, 0x68, 0x67, 0x66, 0x65
54     };
55     /** Clob data. */
56     private static final String JavaDoc clobData =
57         "This is a string, inserted into a CLOB";
58    
59     /**
60      * Create a test setup for the specified blob or clob test.
61      *
62      * @param test the test to provide setup for.
63      */

64     public BlobClobTestSetup(Test test) {
65         super(test);
66     }
67
68     /**
69      * Create a table with BLOB and CLOB, so that such objects can be
70      * accessed/used from JDBC.
71      */

72     protected void setUp()
73         throws IOException JavaDoc, 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         // Actual data is inserted in the getSample* methods.
82
stmt.execute("insert into BLOBCLOB VALUES " +
83                 "(" + ID_SAMPLEVALUES + ", null, null)");
84         stmt.close();
85     }
86
87     /**
88      * Drop the table we created during setup.
89      * @throws Exception
90      */

91     protected void tearDown()
92         throws Exception JavaDoc {
93         Connection con = getConnection();
94         Statement stmt = con.createStatement();
95         stmt.execute("drop table BLOBCLOB");
96         stmt.close();
97         super.tearDown();
98     }
99     
100     /**
101      * Fetch a sample Blob.
102      * If this method fails, the test fails.
103      *
104      * @param con database connection to fetch data from.
105      * @return a sample <code>Blob</code> object.
106      */

107     public static Blob getSampleBlob(Connection con)
108         throws SQLException {
109         InputStream JavaDoc blobInput = new ByteArrayInputStream JavaDoc(blobData, 0, blobData.length);
110         PreparedStatement pStmt =
111             con.prepareStatement("update BLOBCLOB set BLOBDATA = ? where ID = ?");
112         try {
113             blobInput.reset();
114         } catch (IOException JavaDoc 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     /**
131      * Fetch a sample Clob.
132      * If this method fails, the test fails.
133      *
134      * @param con database connection to fetch data from.
135      * @return a sample <code>Clob</code> object.
136      */

137     public static Clob getSampleClob(Connection con)
138         throws SQLException {
139         Reader JavaDoc clobInput = new StringReader JavaDoc(clobData);
140         PreparedStatement pStmt =
141             con.prepareStatement("update BLOBCLOB set CLOBDATA = ? where ID = ?");
142         try {
143             clobInput.reset();
144         } catch (IOException JavaDoc 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 } // End class BlobClobTestSetup
161
Popular Tags