~ubuntu-branches/ubuntu/karmic/mysql-connector-java/karmic

« back to all changes in this revision

Viewing changes to testsuite/regression/StatementRegressionTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Takashi Okamoto
  • Date: 2003-12-30 22:37:53 UTC
  • Revision ID: james.westby@ubuntu.com-20031230223753-5d9n2b0w2ms6puqa
Tags: upstream-3.0.9
ImportĀ upstreamĀ versionĀ 3.0.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2002 MySQL AB
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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
 
17
 
 
18
 */
 
19
package testsuite.regression;
 
20
 
 
21
import testsuite.BaseTestCase;
 
22
 
 
23
import java.io.ByteArrayInputStream;
 
24
import java.io.CharArrayReader;
 
25
import java.io.File;
 
26
import java.io.FileWriter;
 
27
import java.io.Writer;
 
28
 
 
29
import java.sql.ResultSet;
 
30
import java.sql.Statement;
 
31
 
 
32
 
 
33
/**
 
34
 * Regression tests for the Statement class
 
35
 *
 
36
 * @author Mark Matthews
 
37
 */
 
38
public class StatementRegressionTest extends BaseTestCase {
 
39
    /**
 
40
     * Constructor for StatementRegressionTest.
 
41
     *
 
42
     * @param name the name of the test to run
 
43
     */
 
44
    public StatementRegressionTest(String name) {
 
45
        super(name);
 
46
    }
 
47
 
 
48
    /**
 
49
     * Tests that you can close a statement twice without an NPE.
 
50
     *
 
51
     * @throws Exception if an error occurs.
 
52
     */
 
53
    public void testCloseTwice() throws Exception {
 
54
        Statement closeMe = this.conn.createStatement();
 
55
        closeMe.close();
 
56
        closeMe.close();
 
57
    }
 
58
 
 
59
    /**
 
60
     * Tests that 'LOAD DATA LOCAL INFILE' works
 
61
     *
 
62
     * @throws Exception if any errors occur
 
63
     */
 
64
    public void testLoadData() throws Exception {
 
65
        try {
 
66
            int maxAllowedPacket = 1048576;
 
67
 
 
68
            stmt.executeUpdate("DROP TABLE IF EXISTS loadDataRegress");
 
69
            stmt.executeUpdate(
 
70
                "CREATE TABLE loadDataRegress (field1 int, field2 int)");
 
71
 
 
72
            File tempFile = File.createTempFile("mysql", ".txt");
 
73
            //tempFile.deleteOnExit();
 
74
            
 
75
            System.out.println(tempFile);
 
76
 
 
77
            Writer out = new FileWriter(tempFile);
 
78
 
 
79
            int count = 0;
 
80
            int rowCount = 128; //maxAllowedPacket * 4;
 
81
 
 
82
            for (int i = 0; i < rowCount; i++) {
 
83
                out.write((count++) + "\t" + (count++) + "\n");
 
84
            }
 
85
 
 
86
            out.close();
 
87
 
 
88
            StringBuffer fileNameBuf = null;
 
89
 
 
90
            if (File.separatorChar == '\\') {
 
91
                fileNameBuf = new StringBuffer();
 
92
 
 
93
                String fileName = tempFile.getAbsolutePath();
 
94
                int fileNameLength = fileName.length();
 
95
 
 
96
                for (int i = 0; i < fileNameLength; i++) {
 
97
                    char c = fileName.charAt(i);
 
98
 
 
99
                    if (c == '\\') {
 
100
                        fileNameBuf.append("/");
 
101
                    } else {
 
102
                        fileNameBuf.append(c);
 
103
                    }
 
104
                }
 
105
            } else {
 
106
                fileNameBuf = new StringBuffer(tempFile.getAbsolutePath());
 
107
            }
 
108
 
 
109
            int updateCount = stmt.executeUpdate("LOAD DATA LOCAL INFILE '"
 
110
                    + fileNameBuf.toString() + "' INTO TABLE loadDataRegress");
 
111
            assertTrue(updateCount == rowCount);
 
112
        } finally {
 
113
            stmt.executeUpdate("DROP TABLE IF EXISTS loadDataRegress");
 
114
        }
 
115
    }
 
116
 
 
117
    /**
 
118
     * Tests PreparedStatement.setCharacterStream() to ensure it accepts > 4K
 
119
     * streams
 
120
     *
 
121
     * @throws Exception if an error occurs.
 
122
     */
 
123
    public void testSetCharacterStream() throws Exception {
 
124
        try {
 
125
            stmt.executeUpdate("DROP TABLE IF EXISTS charStreamRegressTest");
 
126
            stmt.executeUpdate(
 
127
                "CREATE TABLE charStreamRegressTest(field1 text)");
 
128
 
 
129
            pstmt = conn.prepareStatement(
 
130
                    "INSERT INTO charStreamRegressTest VALUES (?)");
 
131
 
 
132
            char[] charBuf = new char[16384];
 
133
 
 
134
            for (int i = 0; i < charBuf.length; i++) {
 
135
                charBuf[i] = 'A';
 
136
            }
 
137
 
 
138
            CharArrayReader reader = new CharArrayReader(charBuf);
 
139
 
 
140
            pstmt.setCharacterStream(1, reader, charBuf.length);
 
141
            pstmt.executeUpdate();
 
142
 
 
143
            rs = stmt.executeQuery("SELECT field1 FROM charStreamRegressTest");
 
144
 
 
145
            rs.next();
 
146
 
 
147
            String result = rs.getString(1);
 
148
 
 
149
            assertTrue(result.length() == charBuf.length);
 
150
 
 
151
            stmt.execute("TRUNCATE TABLE charStreamRegressTest");
 
152
 
 
153
            // Test that EOF is not thrown
 
154
            reader = new CharArrayReader(charBuf);
 
155
            pstmt.setCharacterStream(1, reader, (charBuf.length * 2));
 
156
            pstmt.executeUpdate();
 
157
 
 
158
            rs = stmt.executeQuery("SELECT field1 FROM charStreamRegressTest");
 
159
 
 
160
            rs.next();
 
161
 
 
162
            result = rs.getString(1);
 
163
 
 
164
            assertTrue("Retrieved value of length " + result.length()
 
165
                + " != length of inserted value " + charBuf.length,
 
166
                result.length() == charBuf.length);
 
167
 
 
168
            // Test single quotes inside identifers
 
169
            stmt.executeUpdate("DROP TABLE IF EXISTS `charStream'RegressTest`");
 
170
            stmt.executeUpdate(
 
171
                "CREATE TABLE `charStream'RegressTest`(field1 text)");
 
172
 
 
173
            pstmt = conn.prepareStatement(
 
174
                    "INSERT INTO `charStream'RegressTest` VALUES (?)");
 
175
 
 
176
            reader = new CharArrayReader(charBuf);
 
177
            pstmt.setCharacterStream(1, reader, (charBuf.length * 2));
 
178
            pstmt.executeUpdate();
 
179
 
 
180
            rs = stmt.executeQuery(
 
181
                    "SELECT field1 FROM `charStream'RegressTest`");
 
182
 
 
183
            rs.next();
 
184
 
 
185
            result = rs.getString(1);
 
186
 
 
187
            assertTrue("Retrieved value of length " + result.length()
 
188
                + " != length of inserted value " + charBuf.length,
 
189
                result.length() == charBuf.length);
 
190
        } finally {
 
191
            if (rs != null) {
 
192
                try {
 
193
                    rs.close();
 
194
                } catch (Exception ex) {
 
195
                    // ignore
 
196
                }
 
197
 
 
198
                rs = null;
 
199
            }
 
200
 
 
201
            stmt.executeUpdate("DROP TABLE IF EXISTS `charStream'RegressTest`");
 
202
            stmt.executeUpdate("DROP TABLE IF EXISTS charStreamRegressTest");
 
203
        }
 
204
    }
 
205
 
 
206
    /**
 
207
     * Tests a bug where Statement.setFetchSize() does not work for values
 
208
     * other than 0 or Integer.MIN_VALUE
 
209
     *
 
210
     * @throws Exception if any errors occur
 
211
     */
 
212
    public void testSetFetchSize() throws Exception {
 
213
        int oldFetchSize = stmt.getFetchSize();
 
214
 
 
215
        try {
 
216
            stmt.setFetchSize(10);
 
217
        } finally {
 
218
            stmt.setFetchSize(oldFetchSize);
 
219
        }
 
220
    }
 
221
 
 
222
    /**
 
223
     * DOCUMENT ME!
 
224
     *
 
225
     * @throws Exception DOCUMENT ME!
 
226
     */
 
227
    public void testUpdatableStream() throws Exception {
 
228
        try {
 
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)");
 
232
 
 
233
            int streamLength = 16385;
 
234
            byte[] streamData = new byte[streamLength];
 
235
 
 
236
            /* create an updatable statement */
 
237
            Statement updStmt = this.conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
 
238
                    ResultSet.CONCUR_UPDATABLE);
 
239
 
 
240
            /* fill the resultset with some values */
 
241
            ResultSet updRs = updStmt.executeQuery("SELECT * FROM updateStreamTest");
 
242
 
 
243
            /* move to insertRow */
 
244
            updRs.moveToInsertRow();
 
245
 
 
246
            /* update the table */
 
247
            updRs.updateBinaryStream("field1",
 
248
                new ByteArrayInputStream(streamData), streamLength);
 
249
 
 
250
            updRs.insertRow();
 
251
        } finally {
 
252
            this.stmt.executeUpdate("DROP TABLE IF EXISTS updateStreamTest");
 
253
        }
 
254
    }
 
255
    
 
256
    /**
 
257
     * Tests fix for BUG#907
 
258
     * 
 
259
     * @throws Exception if an error occurs
 
260
     */
 
261
    public void testSetMaxRows() throws Exception {
 
262
        Statement maxRowsStmt = null;
 
263
        
 
264
        try {
 
265
                maxRowsStmt = this.conn.createStatement();
 
266
                maxRowsStmt.setMaxRows(1);
 
267
                maxRowsStmt.executeQuery("SELECT 1");
 
268
        }
 
269
        finally {
 
270
                if (maxRowsStmt != null) {
 
271
                        maxRowsStmt.close();
 
272
                }
 
273
        }
 
274
    }
 
275
}