2
Copyright (C) 2002 MySQL AB
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
package testsuite.regression;
21
import testsuite.BaseTestCase;
23
import java.io.ByteArrayInputStream;
24
import java.io.CharArrayReader;
26
import java.io.FileWriter;
27
import java.io.Writer;
29
import java.sql.ResultSet;
30
import java.sql.Statement;
34
* Regression tests for the Statement class
36
* @author Mark Matthews
38
public class StatementRegressionTest extends BaseTestCase {
40
* Constructor for StatementRegressionTest.
42
* @param name the name of the test to run
44
public StatementRegressionTest(String name) {
49
* Tests that you can close a statement twice without an NPE.
51
* @throws Exception if an error occurs.
53
public void testCloseTwice() throws Exception {
54
Statement closeMe = this.conn.createStatement();
60
* Tests that 'LOAD DATA LOCAL INFILE' works
62
* @throws Exception if any errors occur
64
public void testLoadData() throws Exception {
66
int maxAllowedPacket = 1048576;
68
stmt.executeUpdate("DROP TABLE IF EXISTS loadDataRegress");
70
"CREATE TABLE loadDataRegress (field1 int, field2 int)");
72
File tempFile = File.createTempFile("mysql", ".txt");
73
//tempFile.deleteOnExit();
75
System.out.println(tempFile);
77
Writer out = new FileWriter(tempFile);
80
int rowCount = 128; //maxAllowedPacket * 4;
82
for (int i = 0; i < rowCount; i++) {
83
out.write((count++) + "\t" + (count++) + "\n");
88
StringBuffer fileNameBuf = null;
90
if (File.separatorChar == '\\') {
91
fileNameBuf = new StringBuffer();
93
String fileName = tempFile.getAbsolutePath();
94
int fileNameLength = fileName.length();
96
for (int i = 0; i < fileNameLength; i++) {
97
char c = fileName.charAt(i);
100
fileNameBuf.append("/");
102
fileNameBuf.append(c);
106
fileNameBuf = new StringBuffer(tempFile.getAbsolutePath());
109
int updateCount = stmt.executeUpdate("LOAD DATA LOCAL INFILE '"
110
+ fileNameBuf.toString() + "' INTO TABLE loadDataRegress");
111
assertTrue(updateCount == rowCount);
113
stmt.executeUpdate("DROP TABLE IF EXISTS loadDataRegress");
118
* Tests PreparedStatement.setCharacterStream() to ensure it accepts > 4K
121
* @throws Exception if an error occurs.
123
public void testSetCharacterStream() throws Exception {
125
stmt.executeUpdate("DROP TABLE IF EXISTS charStreamRegressTest");
127
"CREATE TABLE charStreamRegressTest(field1 text)");
129
pstmt = conn.prepareStatement(
130
"INSERT INTO charStreamRegressTest VALUES (?)");
132
char[] charBuf = new char[16384];
134
for (int i = 0; i < charBuf.length; i++) {
138
CharArrayReader reader = new CharArrayReader(charBuf);
140
pstmt.setCharacterStream(1, reader, charBuf.length);
141
pstmt.executeUpdate();
143
rs = stmt.executeQuery("SELECT field1 FROM charStreamRegressTest");
147
String result = rs.getString(1);
149
assertTrue(result.length() == charBuf.length);
151
stmt.execute("TRUNCATE TABLE charStreamRegressTest");
153
// Test that EOF is not thrown
154
reader = new CharArrayReader(charBuf);
155
pstmt.setCharacterStream(1, reader, (charBuf.length * 2));
156
pstmt.executeUpdate();
158
rs = stmt.executeQuery("SELECT field1 FROM charStreamRegressTest");
162
result = rs.getString(1);
164
assertTrue("Retrieved value of length " + result.length()
165
+ " != length of inserted value " + charBuf.length,
166
result.length() == charBuf.length);
168
// Test single quotes inside identifers
169
stmt.executeUpdate("DROP TABLE IF EXISTS `charStream'RegressTest`");
171
"CREATE TABLE `charStream'RegressTest`(field1 text)");
173
pstmt = conn.prepareStatement(
174
"INSERT INTO `charStream'RegressTest` VALUES (?)");
176
reader = new CharArrayReader(charBuf);
177
pstmt.setCharacterStream(1, reader, (charBuf.length * 2));
178
pstmt.executeUpdate();
180
rs = stmt.executeQuery(
181
"SELECT field1 FROM `charStream'RegressTest`");
185
result = rs.getString(1);
187
assertTrue("Retrieved value of length " + result.length()
188
+ " != length of inserted value " + charBuf.length,
189
result.length() == charBuf.length);
194
} catch (Exception ex) {
201
stmt.executeUpdate("DROP TABLE IF EXISTS `charStream'RegressTest`");
202
stmt.executeUpdate("DROP TABLE IF EXISTS charStreamRegressTest");
207
* Tests a bug where Statement.setFetchSize() does not work for values
208
* other than 0 or Integer.MIN_VALUE
210
* @throws Exception if any errors occur
212
public void testSetFetchSize() throws Exception {
213
int oldFetchSize = stmt.getFetchSize();
216
stmt.setFetchSize(10);
218
stmt.setFetchSize(oldFetchSize);
225
* @throws Exception DOCUMENT ME!
227
public void testUpdatableStream() throws Exception {
229
this.stmt.executeUpdate("DROP TABLE IF EXISTS updateStreamTest");
230
this.stmt.executeUpdate(
231
"CREATE TABLE updateStreamTest (keyField INT NOT NULL AUTO_INCREMENT PRIMARY KEY, field1 BLOB)");
233
int streamLength = 16385;
234
byte[] streamData = new byte[streamLength];
236
/* create an updatable statement */
237
Statement updStmt = this.conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
238
ResultSet.CONCUR_UPDATABLE);
240
/* fill the resultset with some values */
241
ResultSet updRs = updStmt.executeQuery("SELECT * FROM updateStreamTest");
243
/* move to insertRow */
244
updRs.moveToInsertRow();
246
/* update the table */
247
updRs.updateBinaryStream("field1",
248
new ByteArrayInputStream(streamData), streamLength);
252
this.stmt.executeUpdate("DROP TABLE IF EXISTS updateStreamTest");
257
* Tests fix for BUG#907
259
* @throws Exception if an error occurs
261
public void testSetMaxRows() throws Exception {
262
Statement maxRowsStmt = null;
265
maxRowsStmt = this.conn.createStatement();
266
maxRowsStmt.setMaxRows(1);
267
maxRowsStmt.executeQuery("SELECT 1");
270
if (maxRowsStmt != null) {