001: /**
002: *
003: * Derby - Class BLOBDataModelSetup
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,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
017: * either express or implied. See the License for the specific
018: * language governing permissions and limitations under the License.
019: */package org.apache.derbyTesting.functionTests.tests.jdbcapi;
020:
021: import org.apache.derbyTesting.functionTests.util.TestInputStream;
022: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
023: import org.apache.derbyTesting.junit.BaseJDBCTestSetup;
024:
025: import junit.extensions.TestSetup;
026: import junit.framework.Test;
027: import java.sql.Connection;
028: import java.sql.PreparedStatement;
029: import java.sql.SQLException;
030: import java.sql.Statement;
031: import java.io.InputStream;
032:
033: /**
034: * Sets up a data model with very large BLOBs.
035: * The table created will have three fields:
036: * 1. a value field (val), which is the value for every byte in the BLOB.
037: * 2. a length (length) field which is the actual size of the BLOB
038: * 3. the data field (data), which is the actual BLOB data.
039: *
040: * @author Andreas Korneliussen
041: */
042: final public class BLOBDataModelSetup extends BaseJDBCTestSetup {
043:
044: /**
045: * Constructor
046: * @param test test object being decorated by this TestSetup
047: */
048: public BLOBDataModelSetup(Test test) {
049: super (test);
050: }
051:
052: /**
053: * The setup creates a Connection to the database, and creates a table
054: * with blob columns.
055: * @exception Exception any exception will cause test to fail with error.
056: */
057: protected final void setUp() throws Exception {
058: Connection con = getConnection();
059: con.setAutoCommit(false);
060:
061: // Create table:
062: final Statement statement = con.createStatement();
063: statement.executeUpdate("CREATE TABLE " + tableName + " ("
064: + " val INTEGER," + " length INTEGER, "
065: + " data BLOB(2G) NOT NULL)");
066: statement.close();
067: // Insert some data:
068: final PreparedStatement preparedStatement = con
069: .prepareStatement("INSERT INTO " + tableName
070: + "(val, length, data) VALUES (?,?, ?)");
071:
072: // Insert 10 records with size of 1MB
073: for (int i = 0; i < regularBlobs; i++) {
074: final int val = i;
075: final InputStream stream = new TestInputStream(size, val);
076: preparedStatement.setInt(1, val);
077: preparedStatement.setInt(2, size);
078: preparedStatement.setBinaryStream(3, stream, size);
079: preparedStatement.executeUpdate();
080: }
081:
082: // Insert 1 record with size of 64 MB
083: BaseJDBCTestCase.println("Insert BLOB with size = " + bigSize);
084: preparedStatement.setInt(1, bigVal);
085: preparedStatement.setInt(2, bigSize);
086: final InputStream stream = new TestInputStream(bigSize, bigVal);
087: preparedStatement.setBinaryStream(3, stream, bigSize);
088:
089: BaseJDBCTestCase.println("Execute update");
090: preparedStatement.executeUpdate();
091: preparedStatement.close();
092:
093: BaseJDBCTestCase.println("Commit");
094: con.commit();
095: }
096:
097: /**
098: * Teardown test.
099: * Rollback connection and close it.
100: * @exception Exceptions causes the test to fail with error
101: */
102: protected final void tearDown() throws Exception {
103: try {
104: Connection con = getConnection();
105: Statement statement = con.createStatement();
106: statement.execute("DROP TABLE " + tableName);
107: statement.close();
108: con.commit();
109: } catch (SQLException e) {
110: BaseJDBCTestCase.printStackTrace(e);
111: }
112:
113: super .tearDown();
114: }
115:
116: /**
117: * Return table name
118: * @return table name
119: */
120: public static final String getBlobTableName() {
121: return tableName;
122: }
123:
124: /** Size of regular Blobs (currently 1MB) */
125: final static int size = 1024 * 1024;
126:
127: /** Number of regular Blobs */
128: final static int regularBlobs = 10;
129:
130: /** Size of big record (currently 64 MB) */
131: final static int bigSize = 64 * 1024 * 1024;
132:
133: /** Val for big record */
134: final static int bigVal = regularBlobs + 1;
135:
136: /** Name of table */
137: private static final String tableName = "TESTBLOBTABLE";
138: }
|