use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class ClobTest method insertDataWithToken.
/**
* Inserts data into the test Clob, referenced by {@code this.clob}.
*
* @param token a token to insert into the Clob, cannot be {@code null} but
* the empty string is accepted
* @param pre number of characters to insert before the token, using the
* repeating alphabet stream (latin lower-case)
* @param post number of characters to insert after the token, using the
* repeating alphabet stream (latin lower-case)
* @param mode insertion mode; SET_STRING, SET_ASCII_STREAM or
* SET_CHARACTER_STREAM
* @throws IOException if inserting data fails for some reason
* @throws SQLException if inserting data fails for some reason
*/
private void insertDataWithToken(String token, long pre, long post, int mode) throws IOException, SQLException {
long total = 0;
switch(mode) {
case SET_STRING:
{
Reader charIn = new LoopingAlphabetReader(pre);
total += transferData(charIn, TRANSFER_BUFFER_SIZE);
this.clob.setString(pre + 1, token);
total += token.length();
charIn = new LoopingAlphabetReader(post);
total += transferData(charIn, TRANSFER_BUFFER_SIZE);
break;
}
case SET_ASCII_STREAM:
{
OutputStream asciiOut = this.clob.setAsciiStream(1L);
InputStream asciiIn = new LoopingAlphabetStream(pre);
total += transferData(asciiIn, asciiOut, TRANSFER_BUFFER_SIZE);
byte[] tokenBytes = token.getBytes("ISO-8859-1");
asciiOut.write(tokenBytes, 0, tokenBytes.length);
total += tokenBytes.length;
asciiIn = new LoopingAlphabetStream(post);
total += transferData(asciiIn, asciiOut, TRANSFER_BUFFER_SIZE);
break;
}
case SET_CHARACTER_STREAM:
{
Writer charOut = this.clob.setCharacterStream(1L);
Reader charIn = new LoopingAlphabetReader(pre);
total += transferData(charIn, charOut, TRANSFER_BUFFER_SIZE);
charOut.write(token);
total += token.length();
charIn = new LoopingAlphabetReader(post);
total += transferData(charIn, charOut, TRANSFER_BUFFER_SIZE);
break;
}
default:
throw new IllegalArgumentException("Unknown insertion mode: " + mode);
}
assertEquals("Invalid length after insertion", pre + post + token.length(), this.clob.length());
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class ClobUpdatableReaderTest method testMultiplexedOperationProblem.
/**
* Tests that the Clob can handle multiple streams and the length call
* multiplexed.
* <p>
* This test was written after bug DERBY-2806 was reported, where getting
* the length of the Clob after fetching a stream from it would exhaust
* the stream and cause the next read to return -1.
* <p>
* The test is written to work on a Clob that operates on streams from
* the store, which currently means that it must be over a certain size
* and that no modifying methods can be called on it.
*/
public void testMultiplexedOperationProblem() throws IOException, SQLException {
getConnection().setAutoCommit(false);
int length = 266000;
PreparedStatement ps = prepareStatement("insert into updateClob (id, data) values (?,?)");
ps.setInt(1, length);
ps.setCharacterStream(2, new LoopingAlphabetReader(length), length);
assertEquals(1, ps.executeUpdate());
ps.close();
PreparedStatement psFetchClob = prepareStatement("select data from updateClob where id = ?");
psFetchClob.setInt(1, length);
ResultSet rs = psFetchClob.executeQuery();
assertTrue("No Clob of length " + length + " in database", rs.next());
Clob clob = rs.getClob(1);
assertEquals(length, clob.length());
Reader r = clob.getCharacterStream();
int lastReadChar = r.read();
lastReadChar = assertCorrectChar(lastReadChar, r.read());
lastReadChar = assertCorrectChar(lastReadChar, r.read());
assertEquals(length, clob.length());
// Must be bigger than internal buffers might be.
int nextChar;
for (int i = 2; i < 160000; i++) {
nextChar = r.read();
// Check manually to report position where it fails.
if (nextChar == -1) {
fail("Failed at position " + i + ", stream should not be" + " exhausted now");
}
lastReadChar = assertCorrectChar(lastReadChar, nextChar);
}
lastReadChar = assertCorrectChar(lastReadChar, r.read());
lastReadChar = assertCorrectChar(lastReadChar, r.read());
InputStream ra = clob.getAsciiStream();
assertEquals(length, clob.length());
int lastReadAscii = ra.read();
lastReadAscii = assertCorrectChar(lastReadAscii, ra.read());
lastReadAscii = assertCorrectChar(lastReadAscii, ra.read());
assertEquals(length, clob.length());
lastReadAscii = assertCorrectChar(lastReadAscii, ra.read());
lastReadChar = assertCorrectChar(lastReadChar, r.read());
// Close resources.
r.close();
ra.close();
rs.close();
psFetchClob.close();
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class ClobTest method insertAndFetchTest.
/**
* Inserts a Clob with the specified length, using a stream source, then
* fetches it from the database and checks the length.
*
* @param length number of characters in the Clob
* @throws IOException if reading from the source fails
* @throws SQLException if something goes wrong
*/
private void insertAndFetchTest(long length) throws IOException, SQLException {
PreparedStatement ps = prepareStatement("insert into BLOBCLOB(ID, CLOBDATA) values(?,?)");
int id = BlobClobTestSetup.getID();
ps.setInt(1, id);
ps.setCharacterStream(2, new LoopingAlphabetReader(length), length);
long tsStart = System.currentTimeMillis();
ps.execute();
println("Inserted " + length + " chars (length specified) in " + (System.currentTimeMillis() - tsStart) + " ms");
Statement stmt = createStatement();
tsStart = System.currentTimeMillis();
ResultSet rs = stmt.executeQuery("select CLOBDATA from BLOBCLOB where id = " + id);
assertTrue("Clob not inserted", rs.next());
Clob aClob = rs.getClob(1);
assertEquals("Invalid length", length, aClob.length());
println("Fetched length (" + length + ") in " + (System.currentTimeMillis() - tsStart) + " ms");
rs.close();
// Insert same Clob again, using the lengthless override.
id = BlobClobTestSetup.getID();
ps.setInt(1, id);
ps.setCharacterStream(2, new LoopingAlphabetReader(length));
tsStart = System.currentTimeMillis();
ps.executeUpdate();
println("Inserted " + length + " chars (length unspecified) in " + (System.currentTimeMillis() - tsStart) + " ms");
rs = stmt.executeQuery("select CLOBDATA from BLOBCLOB where id = " + id);
assertTrue("Clob not inserted", rs.next());
aClob = rs.getClob(1);
assertEquals("Invalid length", length, aClob.length());
println("Fetched length (" + length + ") in " + (System.currentTimeMillis() - tsStart) + " ms");
rs.close();
rollback();
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class ClobTest method testGetCharacterStreamLongLastCharLatin.
/**
* Obtains streams from the Clob reading portions of the content, always
* including the last character in the Clob.
* <p>
* This case fills the Clob with latin lowercase characters.
*/
public void testGetCharacterStreamLongLastCharLatin() throws IOException, SQLException {
CharAlphabet alphabet = CharAlphabet.modernLatinLowercase();
// Insert a Clob
int length = 5000;
PreparedStatement ps = prepareStatement("insert into BLOBCLOB(ID, CLOBDATA) values(?,?)");
int id = BlobClobTestSetup.getID();
ps.setInt(1, id);
ps.setCharacterStream(2, new LoopingAlphabetReader(length, alphabet), length);
ps.execute();
ps.close();
// Perform the actual test.
getCharacterStreamLongLastChar(id, length, alphabet);
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class Derby2017LayerBTest method testStreamInsertCharBufferBoundary.
public void testStreamInsertCharBufferBoundary() throws IOException, SQLException {
// NOTE: Many of these lengths are implementation dependent, and the
// code paths in LayerBStreamedEXTDTAReaderInputStream may change
// if the implementation of certain points of the DRDA protocol
// changes.
int[] lengths = new int[] { 1, 16383, 0, 32756, 36383, 16384, // Just a longer stream
192 * 1024 };
rollback();
Statement stmt = createStatement();
try {
stmt.executeUpdate("create table t2017_len (len int, c clob)");
} catch (SQLException sqle) {
assertSQLState("X0Y32", sqle);
stmt.executeUpdate("delete from t2017_len");
}
commit();
setAutoCommit(false);
PreparedStatement ps = prepareStatement("insert into t2017_len values (?,?)");
for (int length : lengths) {
ps.setInt(1, length);
ps.setCharacterStream(2, new LoopingAlphabetReader(length));
ps.executeUpdate();
}
// Verify the data, basically making sure the status flag isn't
// included as part of the user data.
ResultSet rs = stmt.executeQuery("select len, c from t2017_len");
int rows = 0;
while (rs.next()) {
rows++;
int length = rs.getInt(1);
assertEquals(new LoopingAlphabetReader(length), rs.getCharacterStream(2));
}
assertEquals(lengths.length, rows);
}
Aggregations