1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.jdbc4.StreamTruncationTest
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to you under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derbyTesting.functionTests.tests.jdbc4;
import java.io.IOException;
import java.io.Reader;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.concurrent.atomic.AtomicInteger;
import junit.framework.Test;
import org.apache.derbyTesting.functionTests.util.streams.CharAlphabet;
import org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Tests string data value truncation when the data is inserted with a stream.
* <p>
* Tests that truncation does indeed happen for CHAR, VARCHAR and CLOB.
* Truncation is not allowed for LONG VARCHAR columns.
* <p>
* There are two aspects to consider; specified length vs unspecified length
* (lengthless override), and small vs large. In this regard, small is when the
* stream content fits into the internal buffer of the conversion reader
* ({@code ReaderToUTF8Stream}). In this test, the buffer is assumed to be
* approximately 32KB.
*/
public class StreamTruncationTest
extends BaseJDBCTestCase {
/**
* Assumed conversion buffer size.
* @see org.apache.derby.iapi.types.ReaderToUTF8Stream
*/
public static final int CONV_BUFFER_SIZE = 32*1024;
// Column index constants
public static final int CLOB = 2;
public static final int VARCHAR = 3;
public static final int LONGVARCHAR = 4;
public static final int CHAR = 5;
/** Name of table with the "small" columns. */
public static final String TABLE_SMALL = "TRUNCATE_SMALL";
/** Name of table with the "large" columns. */
public static final String TABLE_LARGE = "TRUNCATE_LARGE";
/** Small size (smaller than the conversion buffer). */
public static final int SMALL_SIZE = CONV_BUFFER_SIZE / 2;
/** Large size (larger than the conversion buffer). */
public static final int LARGE_SIZE = CONV_BUFFER_SIZE * 2;
/* Size used for the large VARCHAR column. */
public static final int LARGE_VARCHAR_SIZE = 32672;
/* Size used for the CHAR column. */
public static final int CHAR_SIZE = 138;
/** Id/counter to use for the inserted rows. */
private static AtomicInteger ID = new AtomicInteger(1);
public StreamTruncationTest(String name) {
super(name);
}
public void setUp()
throws SQLException {
setAutoCommit(false);
}
public void testCharWithLength()
throws IOException, SQLException {
charSmall(false);
}
public void testCharWithoutLength()
throws IOException, SQLException {
charSmall(true);
}
public void testSmallVarcharWithLength()
throws IOException, SQLException {
generalTypeSmall(VARCHAR, false);
}
public void testSmallVarcharWithoutLength()
throws IOException, SQLException {
generalTypeSmall(VARCHAR, true);
}
public void testLargeVarcharWithLength()
throws IOException, SQLException {
generalTypeLarge(VARCHAR, false);
}
public void testLargeVarcharWithoutLength()
throws IOException, SQLException {
generalTypeLarge(VARCHAR, true);
}
public void testLongVarcharWithLength()
throws IOException, SQLException {
generalTypeSmall(LONGVARCHAR, false);
}
public void testLongVarcharWithoutLength()
throws IOException, SQLException {
generalTypeSmall(LONGVARCHAR, true);
}
public void testSmallClobWithLength()
throws IOException, SQLException {
generalTypeSmall(CLOB, false);
}
public void testSmallClobWithoutLength()
throws IOException, SQLException {
generalTypeSmall(CLOB, true);
}
public void testLargeClobWithLength()
throws IOException, SQLException {
generalTypeLarge(CLOB, false);
}
public void testLargeClobWithoutLength()
throws IOException, SQLException {
generalTypeLarge(CLOB, true);
}
/**
* Executes a set of insertions into the larger of the columns.
*
* @param colIndex column index to insert into, which also determines the
* type to test
* @param lengthless {@code true} if a lengthless override should be used,
* {@code false} if the length of the stream shall be specified when
* inserted
* @throws IOException if reading from the source stream fails
* @throws SQLException if something goes wrong
*/
private void generalTypeLarge(int colIndex, boolean lengthless)
throws IOException, SQLException {
insertLarge(colIndex, lengthless, LARGE_SIZE, 0); // Fits
insertLarge(colIndex, lengthless, LARGE_SIZE -99, 15); // Fits
insertLarge(colIndex, lengthless, LARGE_SIZE + 189, 189); // Truncate
insertLarge(colIndex, lengthless, LARGE_SIZE, 250); // Fits
insertLarge(colIndex, lengthless, LARGE_SIZE + 180, 0); // Should fail
insertLarge(colIndex, lengthless, LARGE_SIZE + 180, 17); // Should fail
}
/**
* Executes a set of insertions into the smaller of the columns.
*
* @param colIndex column index to insert into, which also determines the
* type to test
* @param lengthless {@code true} if a lengthless override should be used,
* {@code false} if the length of the stream shall be specified when
* inserted
* @throws IOException if reading from the source stream fails
* @throws SQLException if something goes wrong
*/
private void generalTypeSmall(int colIndex, boolean lengthless)
throws IOException, SQLException {
insertSmall(colIndex, lengthless, SMALL_SIZE, 0); // Fits
insertSmall(colIndex, lengthless, SMALL_SIZE -99, 15); // Fits
insertSmall(colIndex, lengthless, SMALL_SIZE + 189, 189); // Truncate
insertSmall(colIndex, lengthless, SMALL_SIZE, 250); // Fits
insertSmall(colIndex, lengthless, SMALL_SIZE + 180, 0); // Should fail
insertSmall(colIndex, lengthless, SMALL_SIZE + 180, 17); // Should fail
}
/**
* Executes a set of insertions into the CHAR column.
*
* @param lengthless {@code true} if a lengthless override should be used,
* {@code false} if the length of the stream shall be specified when
* inserted
* @throws IOException if reading from the source stream fails
* @throws SQLException if something goes wrong
*/
private void charSmall(boolean lengthless)
throws IOException, SQLException {
insertSmall(CHAR, lengthless, CHAR_SIZE, 0); // Fits
insertSmall(CHAR, lengthless, CHAR_SIZE -10, 4); // Fits
insertSmall(CHAR, lengthless, CHAR_SIZE + 189, 189); // Should truncate
insertSmall(CHAR, lengthless, CHAR_SIZE, 20); // Fits
insertSmall(CHAR, lengthless, CHAR_SIZE + 180, 0); // Should fail
insertSmall(CHAR, lengthless, CHAR_SIZE + 180, 17); // Should fail
}
/**
* Inserts a small (smaller than internal conversion buffer) string value.
*
* @param colIndex column to insert into (see constants)
* @param lengthless whether the length of the stream should be specified
* or not on insertion
* @param totalLength the total character length of the stream to insert
* @param blanks number of trailing blanks in the stream
* @return The id of the row inserted.
*
* @throws IOException if reading from the source stream fails
* @throws SQLException if something goes wrong, or the test fails
*/
private int insertSmall(int colIndex, boolean lengthless,
int totalLength, int blanks)
throws IOException, SQLException {
int id = ID.getAndAdd(1);
PreparedStatement ps = prepareStatement(
"insert into " + TABLE_SMALL + " values (?,?,?,?,?)");
ps.setInt(1, id);
ps.setNull(2, Types.CLOB);
ps.setNull(3, Types.VARCHAR);
ps.setNull(4, Types.LONGVARCHAR);
ps.setNull(5, Types.CHAR);
int colWidth = SMALL_SIZE;
if (colIndex == LONGVARCHAR) {
colWidth = 32700;
}
int expectedLength = Math.min(totalLength, colWidth);
// Length of CHAR is always the defined length due to padding.
if (colIndex == CHAR) {
colWidth = expectedLength = CHAR_SIZE;
}
println("totalLength=" + totalLength + ", blanks=" + blanks +
", colWidth=" + colWidth + ", expectedLength=" +
expectedLength);
Reader source = new LoopingAlphabetReader(totalLength,
CharAlphabet.modernLatinLowercase(), blanks);
// Now set what we are going to test.
if (lengthless) {
ps.setCharacterStream(colIndex, source);
} else {
ps.setCharacterStream(colIndex, source, totalLength);
}
try {
// Exceute the insert.
assertEquals(1, ps.executeUpdate());
if (totalLength > expectedLength) {
assertTrue(totalLength - blanks <= expectedLength);
}
// Fetch the value.
assertEquals(expectedLength,
getStreamLength(TABLE_SMALL, colIndex, id));
} catch (SQLException sqle) {
// Sanity check of the length.
if (colIndex == LONGVARCHAR) {
// Truncation is not allowed.
assertTrue(totalLength > expectedLength);
} else {
// Total length minus blanks must still be larger then the
// expected length.
assertTrue(totalLength - blanks > expectedLength);
}
// The error handling here is very fuzzy...
// This will hopefully be fixed, such that the exception thrown
// will always be 22001. Today this is currently wrapped by several
// other exceptions.
String expectedState = "XSDA4";
if (colIndex == CHAR || colIndex == VARCHAR) {
if (lengthless) {
expectedState = "XJ001";
} else {
if (!usingEmbedded()) {
expectedState = "XJ001";
} else {
expectedState = "22001";
}
}
}
assertSQLState(expectedState, sqle);
}
return id;
}
/**
* Inserts a large (largerer than internal conversion buffer) string value.
*
* @param colIndex column to insert into (see constants)
* @param lengthless whether the length of the stream should be specified
* or not on insertion
* @param totalLength the total character length of the stream to insert
* @param blanks number of trailing blanks in the stream
* @return The id of the row inserted.
*
* @throws IOException if reading from the source stream fails
* @throws SQLException if something goes wrong, or the test fails
*/
private int insertLarge(int colIndex, boolean lengthless,
int totalLength, int blanks)
throws IOException, SQLException {
// Not used here, see insertSmall.
assertTrue(colIndex != CHAR && colIndex != LONGVARCHAR);
int id = ID.getAndAdd(1);
PreparedStatement ps = prepareStatement(
"insert into " + TABLE_LARGE + " values (?,?,?)");
ps.setInt(1, id);
ps.setNull(2, Types.CLOB);
ps.setNull(3, Types.VARCHAR);
int colWidth = (colIndex == VARCHAR ? LARGE_VARCHAR_SIZE : LARGE_SIZE);
int expectedLength = Math.min(totalLength, colWidth);
println("totalLength=" + totalLength + ", blanks=" + blanks +
", colWidth=" + colWidth + ", expectedLength=" +
expectedLength);
Reader source = new LoopingAlphabetReader(totalLength,
CharAlphabet.modernLatinLowercase(), blanks);
// Now set what we are going to test.
if (lengthless) {
ps.setCharacterStream(colIndex, source);
} else {
ps.setCharacterStream(colIndex, source, totalLength);
}
try {
// Exceute the insert.
assertEquals(1, ps.executeUpdate());
if (totalLength > expectedLength) {
assertTrue(totalLength - blanks <= expectedLength);
}
// Fetch the value.
assertEquals(expectedLength,
getStreamLength(TABLE_LARGE, colIndex, id));
} catch (SQLException sqle) {
// Sanity check of the length.
// Total length minus blanks must still be larger then the
// expected length.
assertTrue(totalLength - blanks > expectedLength);
// The error handling here is very fuzzy...
// This will hopefully be fixed, such that the exception thrown
// will always be 22001. Today this is currently wrapped by several
// other exceptions.
String expectedState = "XSDA4";
if (colIndex == VARCHAR) {
if (lengthless) {
expectedState = "XJ001";
} else {
if (!usingEmbedded()) {
expectedState = "XJ001";
} else {
expectedState = "22001";
}
}
}
assertSQLState(expectedState, sqle);
}
return id;
}
/**
* Obtains the length of the data value stored in the specified table,
* column index and id (primary key).
*
* @param table table name
* @param colIndex column index
* @param id id of the row to fetch
* @return The length in characters of the string data value fetched.
* @throws IOException if reading the stream fails
* @throws SQLException if something goes wrong
*/
private int getStreamLength(String table, int colIndex, int id)
throws IOException, SQLException {
Statement sFetch = createStatement();
ResultSet rs = sFetch.executeQuery("select * from " + table +
" where id = " + id);
assertTrue(rs.next());
Reader dbSource = rs.getCharacterStream(colIndex);
int observedLen = 0;
char[] buf = new char[1024];
while (true) {
int read = dbSource.read(buf);
if (read == -1) {
break;
}
observedLen += read;
}
rs.close();
return observedLen;
}
/**
* Returns the suite of tests.
* <p>
* Two tables are created for the test.
*
* @return A suite of tests.
*/
public static Test suite() {
return new CleanDatabaseTestSetup(TestConfiguration.defaultSuite(
StreamTruncationTest.class, false)) {
protected void decorateSQL(Statement stmt)
throws SQLException {
stmt.executeUpdate(
"create table " + TABLE_SMALL + " (" +
"ID int primary key, " +
"CLOBDATA clob(" + SMALL_SIZE + ")," +
"VCHARDATA varchar(" + SMALL_SIZE + ")," +
"LVCHARDATA long varchar," +
"CHARDATA char(" + CHAR_SIZE + "))");
stmt.executeUpdate(
"create table " + TABLE_LARGE + " (" +
"ID int primary key, " +
"CLOBDATA clob(" + LARGE_SIZE + ")," +
"VCHARDATA varchar(" + LARGE_VARCHAR_SIZE +
"))");
stmt.close();
}
};
}
}
|