001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.lang.longStringColumn
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.lang;
023:
024: import java.sql.Connection;
025: import java.sql.DriverManager;
026: import java.sql.ResultSetMetaData;
027: import java.sql.ResultSet;
028: import java.sql.Statement;
029: import java.sql.SQLException;
030: import java.sql.Types;
031:
032: import org.apache.derby.tools.ij;
033: import org.apache.derby.tools.JDBCDisplayUtil;
034: import java.io.*;
035: import java.sql.PreparedStatement;
036:
037: /**
038: * Test of strings longer than 64K. Need to test it using Clob because long varchars don't accept data longer than 32700.
039: */
040:
041: public class longStringColumn {
042:
043: public static PreparedStatement psSet;
044: public static PreparedStatement psGet;
045:
046: public static void main(String[] args) {
047:
048: System.out.println("Test longStringColumn starting");
049:
050: String longText;
051: StringBuffer buff = new StringBuffer("... ");
052:
053: try {
054: // use the ij utility to read the property file and
055: // make the initial connection.
056: ij.getPropertyArg(args);
057: Connection conn = ij.startJBMS();
058:
059: Statement st2 = conn.createStatement();
060: st2.execute("CREATE TABLE TEST(id bigint, body clob(65K))");
061: psSet = conn
062: .prepareStatement("insert into test values(?,?)");
063: psGet = conn
064: .prepareStatement("select body from test where id=?");
065:
066: for (long i = 0; i < 65560; i++) {
067:
068: if (i % 10 == 0)
069: buff.append(" ");
070: else
071: buff.append("x");
072:
073: // Show something is happening
074: if (i % 10000 == 0)
075: System.out.println("... " + i);
076:
077: // only test after buffer length reaches 65500
078: if (buff.length() > 65525) {
079:
080: System.out.println("i = " + i
081: + ", testing length: " + buff.length());
082:
083: longText = buff.toString();
084: // set the text
085: setBody(i, longText);
086:
087: // now read the text
088: String res = getBody(i);
089: if (!res.equals(longText)) {
090: System.out
091: .println("FAIL -- string fetched is incorrect, length is "
092: + buff.length()
093: + ", expecting string: "
094: + longText
095: + ", instead got the following: "
096: + res);
097: break;
098: }
099: }
100: }
101:
102: conn.close();
103:
104: } catch (SQLException e) {
105: dumpSQLExceptions(e);
106: } catch (Throwable e) {
107: System.out.println("FAIL -- unexpected exception:"
108: + e.toString());
109: }
110:
111: System.out.println("Test longStringColumn finished");
112:
113: }
114:
115: private static void setBody(long key, String body) {
116:
117: try {
118: psSet.setLong(1, key);
119: psSet.setString(2, body);
120: psSet.executeUpdate();
121:
122: } catch (SQLException ex) {
123: ex.printStackTrace();
124:
125: System.out.println("FAIL -- unexpected exception");
126: System.exit(-1);
127: }
128: }
129:
130: private static String getBody(long key) {
131:
132: String result = "NO RESULT";
133:
134: try {
135: psGet.setLong(1, key);
136: ResultSet rs = psGet.executeQuery();
137:
138: if (rs.next())
139: result = rs.getString(1);
140:
141: } catch (SQLException ex) {
142: ex.printStackTrace();
143: }
144:
145: return result;
146: }
147:
148: static private void dumpSQLExceptions(SQLException se) {
149: System.out.println("FAIL -- unexpected exception: "
150: + se.toString());
151: while (se != null) {
152: System.out.print("SQLSTATE(" + se.getSQLState() + "):");
153: se = se.getNextException();
154: }
155: }
156:
157: }
|