use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class BlobClob4BlobTest method insertLoopingAlphabetStreamData.
private void insertLoopingAlphabetStreamData(PreparedStatement ps, CharAlphabet alphabet, int lobLength) throws Exception {
ps.setCharacterStream(1, new LoopingAlphabetReader(lobLength, alphabet), lobLength);
ps.setInt(2, lobLength);
ps.setLong(3, -1);
ps.executeUpdate();
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class LobSortTest method suite.
public static Test suite() {
Properties props = new Properties();
// Adjust sort buffer size to trigger the bug situation with less data.
props.setProperty("derby.storage.sortBufferMax", "4");
BaseTestSuite suite = new BaseTestSuite(LobSortTest.class, "LobSortTestEmbedded");
return new CleanDatabaseTestSetup(new SystemPropertyTestSetup(suite, props, true)) {
/**
* Generates a table with Blob and Clobs of mixed size.
*/
protected void decorateSQL(Statement s) throws SQLException {
Random rnd = new Random(SEED);
Connection con = s.getConnection();
con.setAutoCommit(false);
s.executeUpdate("create table MIXED_LOBS (" + "c clob, clen int, b blob, blen int, rnd int)");
PreparedStatement ps = con.prepareStatement("insert into MIXED_LOBS values (?,?,?,?,?)");
// Make sure we get at least one zero-length CLOB and BLOB.
ps.setString(1, "");
ps.setInt(2, 0);
ps.setBytes(3, new byte[0]);
ps.setInt(4, 0);
ps.setInt(5, rnd.nextInt());
ps.executeUpdate();
for (int i = 0; i < 100; i++) {
CharAlphabet ca = getCharAlphabet(1 + rnd.nextInt(3));
int length = (int) (rnd.nextDouble() * 64.0 * 1024.0);
if (rnd.nextInt(1000) < 500) {
// Specify the length.
ps.setCharacterStream(1, new LoopingAlphabetReader(length, ca), length);
} else {
// Don't specify the length.
ps.setCharacterStream(1, new LoopingAlphabetReader(length, ca));
}
ps.setInt(2, length);
length = (int) (rnd.nextDouble() * 64.0 * 1024.0);
if (rnd.nextInt(1000) < 500) {
// Specify the length.
ps.setBinaryStream(3, new LoopingAlphabetStream(length), length);
} else {
// Don't specify the length.
ps.setBinaryStream(3, new LoopingAlphabetStream(length));
}
ps.setInt(4, length);
ps.setInt(5, rnd.nextInt());
ps.executeUpdate();
}
con.commit();
ps.close();
}
/**
* Returns a character alphabet.
*/
private CharAlphabet getCharAlphabet(int i) {
switch(i) {
case 1:
return CharAlphabet.modernLatinLowercase();
case 2:
return CharAlphabet.tamil();
case 3:
return CharAlphabet.cjkSubset();
default:
fail("Unknown alphabet identifier: " + i);
}
// Will never be reached.
return null;
}
};
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class ClobTest method initializeLongClob.
/**
* Insert a row with a large clob into the test table. Read the row from
* the database and assign the clob value to <code>clob</code>.
* @return The id of the row that was inserted
* @throws java.sql.SQLException
*/
private int initializeLongClob() throws SQLException {
// Clob needs to be larger than one page for locking to occur
final int lobLength = 40000;
// Insert a long Clob
PreparedStatement ps = prepareStatement("insert into BLOBCLOB(ID, CLOBDATA) values(?,?)");
int id = BlobClobTestSetup.getID();
ps.setInt(1, id);
ps.setCharacterStream(2, new LoopingAlphabetReader(lobLength), lobLength);
ps.execute();
ps.close();
commit();
// Fetch the Clob object from the database
Statement st = createStatement();
ResultSet rs = st.executeQuery("select CLOBDATA from BLOBCLOB where ID=" + id);
rs.next();
clob = rs.getClob(1);
rs.close();
st.close();
return id;
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class ClobTest method getCharacterStreamLongLastChar.
/**
* Obtains streams from the Clob and makes sure we can always read the
* last char in the Clob.
* <p>
* See DERBY-4060.
*
* @param id id of the Clob to use
* @param length the length of the Clob
* @param alphabet the alphabet used to create the content
* @throws IOException if reading from a stream fails
* @throws SQLException if something goes wrong
*/
private void getCharacterStreamLongLastChar(int id, int length, CharAlphabet alphabet) throws IOException, SQLException {
// Get last char from the source stream.
Reader cmpReader = new LoopingAlphabetReader(length, alphabet);
cmpReader.skip(length - 1);
char srcLastChar = (char) cmpReader.read();
assertTrue(cmpReader.read() == -1);
PreparedStatement ps = prepareStatement("select CLOBDATA from BLOBCLOB where ID=?");
ps.setInt(1, id);
// Read everything first.
int charsToRead = length;
ResultSet rs = ps.executeQuery();
rs.next();
Reader reader = rs.getClob(1).getCharacterStream(length - charsToRead + 1, charsToRead);
// Drain the stream, and make sure we are able to read the last char.
char lastCharRead = getLastCharInStream(reader, charsToRead);
assertEquals(srcLastChar, lastCharRead);
reader.close();
rs.close();
// Read a portion of the stream.
charsToRead = length / 4;
rs = ps.executeQuery();
rs.next();
reader = rs.getClob(1).getCharacterStream(length - charsToRead + 1, charsToRead);
lastCharRead = getLastCharInStream(reader, charsToRead);
assertEquals(srcLastChar, lastCharRead);
reader.close();
rs.close();
// Read a very small portion of the stream.
charsToRead = 1;
rs = ps.executeQuery();
rs.next();
reader = rs.getClob(1).getCharacterStream(length - charsToRead + 1, charsToRead);
lastCharRead = getLastCharInStream(reader, charsToRead);
assertEquals(srcLastChar, lastCharRead);
reader.close();
rs.close();
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class ClobTest method testGetCharacterStreamLongLastCharCJK.
/**
* 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 Chinese/Japanese/Korean characters.
*/
public void testGetCharacterStreamLongLastCharCJK() throws IOException, SQLException {
CharAlphabet alphabet = CharAlphabet.cjkSubset();
// Insert a Clob
int length = 9001;
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);
}
Aggregations