001: /*
002:
003: Derby - Class BlobClobTestSetup
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.jdbc4;
023:
024: import junit.extensions.TestSetup;
025: import junit.framework.Test;
026:
027: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
028: import org.apache.derbyTesting.junit.BaseJDBCTestSetup;
029:
030: import java.io.ByteArrayInputStream;
031: import java.io.InputStream;
032: import java.io.IOException;
033: import java.io.Reader;
034: import java.io.StringReader;
035: import java.sql.*;
036:
037: /**
038: * Create a table with one column for a blob and one column for a clob.
039: * This is shared between tests that need a blob or a clob, and is required
040: * because the createBlob/-Clob methods are not yet implemented.
041: */
042: public class BlobClobTestSetup extends BaseJDBCTestSetup {
043:
044: /** Constant for accessing the row with null values. */
045: public static final int ID_NULLVALUES = 1;
046: /** Constant for accessing the row with sample values. */
047: public static final int ID_SAMPLEVALUES = 2;
048:
049: /** Blob data. */
050: private static final byte[] blobData = new byte[] { 0x65, 0x66,
051: 0x67, 0x68, 0x69, 0x69, 0x68, 0x67, 0x66, 0x65 };
052: /** Clob data. */
053: private static final String clobData = "This is a string, inserted into a CLOB";
054:
055: /**
056: * Create a test setup for the specified blob or clob test.
057: *
058: * @param test the test to provide setup for.
059: */
060: public BlobClobTestSetup(Test test) {
061: super (test);
062: }
063:
064: /**
065: * Create a table with BLOB and CLOB, so that such objects can be
066: * accessed/used from JDBC.
067: */
068: protected void setUp() throws IOException, SQLException {
069: Connection con = getConnection();
070: Statement stmt = con.createStatement();
071: stmt.execute("create table BLOBCLOB (ID int primary key, "
072: + "BLOBDATA blob(1k)," + "CLOBDATA clob(1k))");
073: stmt.execute("insert into BLOBCLOB VALUES " + "("
074: + ID_NULLVALUES + ", null, null)");
075: // Actual data is inserted in the getSample* methods.
076: stmt.execute("insert into BLOBCLOB VALUES " + "("
077: + ID_SAMPLEVALUES + ", null, null)");
078: stmt.close();
079: }
080:
081: /**
082: * Drop the table we created during setup.
083: * @throws Exception
084: */
085: protected void tearDown() throws Exception {
086: Connection con = getConnection();
087: Statement stmt = con.createStatement();
088: stmt.execute("drop table BLOBCLOB");
089: stmt.close();
090: super .tearDown();
091: }
092:
093: /**
094: * Fetch a sample Blob.
095: * If this method fails, the test fails.
096: *
097: * @param con database connection to fetch data from.
098: * @return a sample <code>Blob</code> object.
099: */
100: public static Blob getSampleBlob(Connection con)
101: throws SQLException {
102: InputStream blobInput = new ByteArrayInputStream(blobData, 0,
103: blobData.length);
104: PreparedStatement pStmt = con
105: .prepareStatement("update BLOBCLOB set BLOBDATA = ? where ID = ?");
106: try {
107: blobInput.reset();
108: } catch (IOException ioe) {
109: fail("Failed to reset blob input stream: "
110: + ioe.getMessage());
111: }
112: pStmt.setBlob(1, blobInput, blobData.length);
113: pStmt.setInt(2, ID_SAMPLEVALUES);
114: assertEquals("Invalid update count", 1, pStmt.executeUpdate());
115: Statement stmt = con.createStatement();
116: ResultSet rs = stmt
117: .executeQuery("select BLOBDATA from BLOBCLOB where ID = "
118: + ID_SAMPLEVALUES);
119: rs.next();
120: Blob blob = rs.getBlob(1);
121: rs.close();
122: stmt.close();
123: return blob;
124: }
125:
126: /**
127: * Fetch a sample Clob.
128: * If this method fails, the test fails.
129: *
130: * @param con database connection to fetch data from.
131: * @return a sample <code>Clob</code> object.
132: */
133: public static Clob getSampleClob(Connection con)
134: throws SQLException {
135: Reader clobInput = new StringReader(clobData);
136: PreparedStatement pStmt = con
137: .prepareStatement("update BLOBCLOB set CLOBDATA = ? where ID = ?");
138: try {
139: clobInput.reset();
140: } catch (IOException ioe) {
141: fail("Failed to reset clob input stream: "
142: + ioe.getMessage());
143: }
144: pStmt.setClob(1, clobInput, clobData.length());
145: pStmt.setInt(2, ID_SAMPLEVALUES);
146: assertEquals("Invalid update count", 1, pStmt.executeUpdate());
147: Statement stmt = con.createStatement();
148: ResultSet rs = stmt
149: .executeQuery("select CLOBDATA from BLOBCLOB where ID = "
150: + ID_SAMPLEVALUES);
151: rs.next();
152: Clob clob = rs.getClob(1);
153: rs.close();
154: stmt.close();
155: return clob;
156: }
157:
158: } // End class BlobClobTestSetup
|