KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > BLOBDataModelSetup


1 /**
2  *
3  * Derby - Class BLOBDataModelSetup
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,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */

20 package org.apache.derbyTesting.functionTests.tests.jdbcapi;
21 import org.apache.derbyTesting.functionTests.util.TestInputStream;
22 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
23 import org.apache.derbyTesting.junit.BaseJDBCTestSetup;
24
25 import junit.extensions.TestSetup;
26 import junit.framework.Test;
27 import java.sql.Connection JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Statement JavaDoc;
31 import java.io.InputStream JavaDoc;
32
33 /**
34  * Sets up a data model with very large BLOBs.
35  * The table created will have three fields:
36  * 1. a value field (val), which is the value for every byte in the BLOB.
37  * 2. a length (length) field which is the actual size of the BLOB
38  * 3. the data field (data), which is the actual BLOB data.
39  *
40  * @author Andreas Korneliussen
41  */

42 final public class BLOBDataModelSetup extends BaseJDBCTestSetup
43 {
44     
45     /**
46      * Constructor
47      * @param test test object being decorated by this TestSetup
48      */

49     public BLOBDataModelSetup(Test test)
50     {
51         super(test);
52     }
53
54     /**
55      * The setup creates a Connection to the database, and creates a table
56      * with blob columns.
57      * @exception Exception any exception will cause test to fail with error.
58      */

59     protected final void setUp()
60         throws Exception JavaDoc
61     {
62         Connection JavaDoc con = getConnection();
63         con.setAutoCommit(false);
64         
65         // Create table:
66
final Statement JavaDoc statement = con.createStatement();
67         statement.executeUpdate("CREATE TABLE " + tableName + " ("+
68                                 " val INTEGER," +
69                                 " length INTEGER, " +
70                                 " data BLOB(2G) NOT NULL)");
71         statement.close();
72         // Insert some data:
73
final PreparedStatement JavaDoc preparedStatement =
74             con.prepareStatement
75             ("INSERT INTO " + tableName + "(val, length, data) VALUES (?,?, ?)");
76         
77         // Insert 10 records with size of 1MB
78
for (int i = 0; i < regularBlobs; i++) {
79             final int val = i;
80             final InputStream stream = new TestInputStream(size, val);
81             preparedStatement.setInt(1, val);
82             preparedStatement.setInt(2, size);
83             preparedStatement.setBinaryStream(3, stream, size);
84             preparedStatement.executeUpdate();
85         }
86         
87         // Insert 1 record with size of 64 MB
88
BaseJDBCTestCase.println("Insert BLOB with size = " + bigSize);
89         preparedStatement.setInt(1, bigVal);
90         preparedStatement.setInt(2, bigSize);
91         final InputStream stream = new TestInputStream(bigSize, bigVal);
92         preparedStatement.setBinaryStream(3, stream, bigSize);
93         
94         BaseJDBCTestCase.println("Execute update");
95         preparedStatement.executeUpdate();
96         preparedStatement.close();
97         
98         BaseJDBCTestCase.println("Commit");
99         con.commit();
100     }
101     
102     /**
103      * Teardown test.
104      * Rollback connection and close it.
105      * @exception Exceptions causes the test to fail with error
106      */

107     protected final void tearDown()
108         throws Exception JavaDoc
109     {
110         try {
111             Connection JavaDoc con = getConnection();
112             Statement JavaDoc statement = con.createStatement();
113             statement.execute("DROP TABLE " + tableName);
114             statement.close();
115             con.commit();
116         } catch (SQLException JavaDoc e) {
117             BaseJDBCTestCase.printStackTrace(e);
118         }
119         
120         super.tearDown();
121     }
122
123     /**
124      * Return table name
125      * @return table name
126      */

127     public static final String JavaDoc getBlobTableName()
128     {
129         return tableName;
130     }
131     
132     /** Size of regular Blobs (currently 1MB) */
133     final static int size = 1024 * 1024;
134     
135     /** Number of regular Blobs */
136     final static int regularBlobs = 10;
137
138     /** Size of big record (currently 64 MB) */
139     final static int bigSize = 64 * 1024 * 1024;
140     
141     /** Val for big record */
142     final static int bigVal = regularBlobs + 1;
143     
144     /** Name of table */
145     private static final String JavaDoc tableName = "TESTBLOBTABLE";
146 }
147
Popular Tags