001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.maxfieldsize
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.jdbcapi;
023:
024: import java.sql.Connection;
025: import java.sql.DriverManager;
026: import java.sql.PreparedStatement;
027: import java.sql.ResultSetMetaData;
028: import java.sql.ResultSet;
029: import java.sql.Statement;
030: import java.sql.SQLException;
031: import java.sql.Types;
032: import java.sql.PreparedStatement;
033: import java.io.*;
034:
035: import org.apache.derby.tools.ij;
036: import org.apache.derby.tools.JDBCDisplayUtil;
037: import org.apache.derbyTesting.functionTests.util.TestUtil;
038:
039: /**
040: *This Program Test SetMaxFieldsize()/getMaxFieldsize().
041: *and the getXXX calls that are affected.
042: * @author - suresht
043: */
044:
045: public class maxfieldsize {
046:
047: public static ResultSet rs;
048:
049: static final int START_SECOND_HALF = 5;
050: static final int NUM_EXECUTIONS = 2 * START_SECOND_HALF;
051:
052: static final String c1_value = "C1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
053: static final String c2_value = "C2XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
054: static final String c3_value = "C3XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
055: static final String c4_value = "C4XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
056: static final String c5_value = "C5XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
057: static final String c6_value = "C6XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
058:
059: static private boolean isDerbyNet = false;
060:
061: public static void main(String[] args) {
062: Connection conn;
063: Statement stmt;
064:
065: // start by cleaning up, just in case
066: String[] testObjects = { "table tab1", "table tab2" };
067:
068: System.out.println("Test MaxFieldSize starting");
069:
070: isDerbyNet = TestUtil.isNetFramework();
071: try {
072: // use the ij utility to read the property file and
073: // make the initial connection.
074: ij.getPropertyArg(args);
075: conn = ij.startJBMS();
076: stmt = conn.createStatement();
077: TestUtil.cleanUpTest(stmt, testObjects);
078: //create a table, insert a row, do a select from the table,
079: stmt.execute("create table tab1("
080: + "c1 char(100) for bit data,"
081: + "c2 varchar(100) for bit data,"
082: + "c3 long varchar for bit data," + "c4 char(100),"
083: + "c5 varchar(100)," + "c6 long varchar)");
084: // load some data into this table ..
085: load_data(conn);
086:
087: // read the data of each type with all the possible functions
088: int loop = 0;
089: while (loop < NUM_EXECUTIONS) {
090: if (loop == START_SECOND_HALF) {
091: stmt.setMaxFieldSize(24);
092: }
093:
094: System.out.println("Iteration #: " + loop);
095: System.out.println("Max Field Size = "
096: + stmt.getMaxFieldSize());
097: rs = stmt.executeQuery("select * from tab1");
098: while (rs.next()) {
099: for (int i = 1; i < 7; i++) {
100: System.out.println("Column #: " + i);
101: switch (loop % START_SECOND_HALF) {
102: case 0:
103: connectionJdbc20.get_using_object(rs, i);
104: break;
105:
106: case 1:
107: if (isDerbyNet)
108: System.out
109: .println("beetle 5350 - JCC returns incorrect result for maxfieldsize()");
110: connectionJdbc20.get_using_string(rs, i);
111: break;
112:
113: case 2:
114: connectionJdbc20.get_using_ascii_stream(rs,
115: i);
116: break;
117:
118: case 3:
119: if (i < 4) // only c1 , c2, c3
120: {
121: connectionJdbc20
122: .get_using_binary_stream(rs, i);
123: } else {
124: System.out.println("SKIPPING");
125: }
126: break;
127:
128: case 4:
129: if (i < 4) // only c1 , c2, c3
130: {
131: connectionJdbc20.get_using_bytes(rs, i);
132: } else {
133: System.out.println("SKIPPING");
134: }
135: break;
136: }
137: }
138: }
139: rs.close();
140: loop++;
141: }
142: // make sure that we throw exception for invalid values
143: try {
144: // negative value should throw an exception
145: stmt.setMaxFieldSize(-200);
146: } catch (SQLException e) {
147: if ((e.getMessage() != null && e.getMessage().indexOf(
148: "Invalid maxFieldSize value") >= 0)
149: || (e.getSQLState() != null && (e.getSQLState()
150: .equals("XJ066"))))
151: System.out
152: .println("Expected Exception - Invalid maxFieldsize");
153: else
154: System.out.println("Unexpected FAILURE at " + e);
155: }
156: // zero is valid value -- which means unlimited
157: stmt.setMaxFieldSize(0);
158:
159: // Do an external sort (forced via properties file),
160: // verifying that streams work correctly
161: System.out.println("Doing external sort");
162:
163: testSort(conn, stmt);
164:
165: TestUtil.cleanUpTest(stmt, testObjects);
166:
167: stmt.close();
168: conn.close();
169:
170: } catch (SQLException e) {
171: dumpSQLExceptions(e);
172: e.printStackTrace();
173: } catch (Throwable e) {
174: System.out.println("FAIL -- unexpected exception: " + e);
175: e.printStackTrace();
176: }
177:
178: System.out.println("Test maxfieldsize finished");
179: }
180:
181: static private void load_data(Connection conn) throws Exception {
182: PreparedStatement pstmt = null;
183:
184: try {
185: pstmt = conn
186: .prepareStatement("insert into tab1 values(?,?,?,?,?,?)");
187:
188: pstmt.setBytes(1, c1_value.getBytes("US-ASCII"));
189: pstmt.setBytes(2, c2_value.getBytes("US-ASCII"));
190: pstmt.setBytes(3, c3_value.getBytes("US-ASCII"));
191: pstmt.setString(4, c4_value);
192: pstmt.setString(5, c5_value);
193: pstmt.setString(6, c6_value);
194: pstmt.execute();
195: } catch (SQLException e) {
196: dumpSQLExceptions(e);
197: e.printStackTrace();
198: } catch (Throwable e) {
199: System.out.println("Fail -- unexpected exception ");
200: e.printStackTrace();
201: } finally {
202: pstmt.close();
203: }
204: }
205:
206: private static void testSort(Connection conn, Statement stmt)
207: throws SQLException, java.io.UnsupportedEncodingException {
208: PreparedStatement insertPStmt;
209:
210: // Load up a 2nd table using streams where appropriate
211: stmt.execute("create table tab2(" + "c0 int, "
212: + "c1 char(100) for bit data,"
213: + "c2 varchar(100) for bit data,"
214: + "c3 long varchar for bit data," + "c4 char(100),"
215: + "c5 varchar(100)," + "c6 long varchar)");
216:
217: // Populate the table
218: insertPStmt = conn
219: .prepareStatement("insert into tab2 values (?, ?, ?, ?, ?, ?, ?)");
220: for (int index = 0; index < 5000; index++) {
221: insertPStmt.setInt(1, index);
222: insertPStmt.setBytes(2, c1_value.getBytes("US-ASCII"));
223: insertPStmt.setBytes(3, c2_value.getBytes("US-ASCII"));
224: insertPStmt.setBytes(4, c3_value.getBytes("US-ASCII"));
225: insertPStmt.setString(5, c4_value);
226: insertPStmt.setString(6, c5_value);
227: insertPStmt.setString(7, c6_value);
228: insertPStmt.executeUpdate();
229: }
230:
231: insertPStmt.close();
232:
233: // Do sort with maxFieldSize = 0
234: doSort(stmt);
235:
236: // Set maxFieldSize to 24 and do another sort
237: stmt.setMaxFieldSize(24);
238: doSort(stmt);
239: }
240:
241: private static void doSort(Statement stmt) throws SQLException {
242: System.out
243: .println("Max Field Size = " + stmt.getMaxFieldSize());
244:
245: try {
246: /* Do a descending sort on 1st column, but only select
247: * out 1st and last 5 rows. This should test streaming to/from
248: * a work table.
249: */
250: rs = stmt
251: .executeQuery("select * from tab2 order by c0 desc");
252: for (int index = 0; index < 5000; index++) {
253: rs.next();
254: if (index < 5 || index >= 4995) {
255: System.out.println("Iteration #: " + index);
256: System.out.println("Column #1: " + rs.getInt(1));
257: System.out.println("Column #2:");
258: connectionJdbc20.get_using_binary_stream(rs, 2);
259: System.out.println("Column #3:");
260: connectionJdbc20.get_using_binary_stream(rs, 3);
261: System.out.println("Column #4:");
262: connectionJdbc20.get_using_binary_stream(rs, 4);
263: System.out.println("Column #5:");
264: connectionJdbc20.get_using_ascii_stream(rs, 5);
265: System.out.println("Column #6:");
266: connectionJdbc20.get_using_ascii_stream(rs, 6);
267: System.out.println("Column #7:");
268: connectionJdbc20.get_using_ascii_stream(rs, 7);
269: }
270: }
271: } catch (SQLException e) {
272: throw e;
273: } catch (Throwable e) {
274: System.out.println("FAIL -- unexpected exception: " + e);
275: e.printStackTrace();
276: }
277: }
278:
279: static private void dumpSQLExceptions(SQLException se) {
280: System.out.println("FAIL -- unexpected exception");
281: while (se != null) {
282: System.out.println("SQLSTATE(" + se.getSQLState() + "): "
283: + se);
284: se = se.getNextException();
285: }
286: }
287:
288: static private void dumpExpectedSQLExceptions(SQLException se) {
289: System.out.println("PASS -- expected exception");
290: while (se != null) {
291: System.out.println("SQLSTATE(" + se.getSQLState() + "): "
292: + se);
293: se = se.getNextException();
294: }
295: }
296:
297: static private void cleanUp(Connection conn) throws SQLException {
298:
299: }
300:
301: }
|