use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class BiggerTemporaryClobTest method setUp.
/**
* Creates a bigger read-write Clob that is being kept on disk due to its
* size.
*/
public void setUp() throws Exception {
super.initialCharLength = CLOBLENGTH;
super.headerLength = 2 + 3;
// All tamil letters. Also add the header bytes.
super.initialByteLength = CLOBLENGTH * 3 + headerLength;
super.bytesPerChar = BYTES_PER_CHAR;
EmbedStatement embStmt = (EmbedStatement) createStatement();
iClob = new TemporaryClob(embStmt);
transferData(new LoopingAlphabetReader(CLOBLENGTH, CharAlphabet.cjkSubset()), iClob.getWriter(1L), CLOBLENGTH);
assertEquals(CLOBLENGTH, iClob.getCharLength());
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class SmallTemporaryClobTest method setUp.
/**
* Creates a small read-write Clob that is kept in memory.
*/
public void setUp() throws Exception {
super.initialCharLength = CLOBLENGTH;
super.headerLength = 2 + 3;
// All tamil letters. Also add the header bytes.
super.initialByteLength = CLOBLENGTH * 3 + headerLength;
super.bytesPerChar = BYTES_PER_CHAR;
EmbedStatement embStmt = (EmbedStatement) createStatement();
iClob = new TemporaryClob(embStmt);
transferData(new LoopingAlphabetReader(CLOBLENGTH, CharAlphabet.tamil()), iClob.getWriter(1L), CLOBLENGTH);
assertEquals(CLOBLENGTH, iClob.getCharLength());
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class UTF8ReaderTest method checkInternalStream.
/**
* Makes sure the data returned from the internal Clob matches the data
* returned by a fresh looping alphabet stream.
*
* @param pos 1-based Clob position
* @param clob internal store stream Clob representation
*/
private static void checkInternalStream(long pos, StoreStreamClob clob) throws IOException, SQLException {
Reader canonStream = new LoopingAlphabetReader(pos + 100);
// Convert to 0-based index.
long toSkip = pos - 1;
while (toSkip > 0) {
long skipped = canonStream.skip(toSkip);
if (skipped > 0) {
toSkip -= skipped;
}
}
Reader clobStream = clob.getInternalReader(pos);
assertEquals("Data mismatch", canonStream.read(), clobStream.read());
clobStream.close();
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class UTF8ReaderTest method insertTestData.
/**
* Inserts data used by the tests.
* <p>
* Use the id to select a Clob with specific contents.
*/
private static void insertTestData(Statement stmt) throws SQLException {
int[][] sizes = new int[][] { // 1M chars
{ 100, 1 * 1024 * 1024 }, // 32K chars
{ 101, 32 * 1024 } };
stmt.executeUpdate("create table Utf8ReaderTest" + "(id int primary key, size int, dClob clob)");
PreparedStatement ps = stmt.getConnection().prepareStatement("insert into Utf8ReaderTest values (?,?,?)");
for (int i = 0; i < sizes.length; i++) {
ps.setInt(1, sizes[i][0]);
int size = sizes[i][1];
ps.setInt(2, size);
ps.setCharacterStream(3, new LoopingAlphabetReader(size), size);
ps.executeUpdate();
}
// Insert some special pieces of text, repeat to get it represented as
// a stream.
ps.setInt(1, 1);
int size = aintWeGotFun.length();
ps.setInt(2, size);
StringBuffer str = new StringBuffer(32 * 1024 + aintWeGotFun.length());
while (str.length() < 32 * 1024) {
str.append(aintWeGotFun);
}
ps.setString(3, str.toString());
ps.executeUpdate();
}
use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader in project derby by apache.
the class Derby2017LayerATest method doInsertTest.
/**
* Performs the base test cycle; insert 3 valid rows, try to insert 2
* invalid rows, insert 2 valid rows.
* <p>
* The outcome depends on whether auto-commit is on, and whether a rollback
* is issued when an insert fails.
*
* @param INSERT the data to insert
* @param autoCommit the auto-commit state to use
* @param rollbackOnError whether or not to issue a rollback if an insert
* fails
*
* @throws IOException if something goes wrong
* @throws SQLException if something goes wrong
*/
private void doInsertTest(int[] INSERT, boolean autoCommit, boolean rollbackOnError) throws IOException, SQLException {
// A few sanity checks.
assertEquals("Expects 7 rows", 7, INSERT.length);
rollback();
Statement stmt = createStatement();
try {
stmt.executeUpdate("create table t2017_id (id int unique, c clob)");
} catch (SQLException sqle) {
assertSQLState("X0Y32", sqle);
stmt.executeUpdate("delete from t2017_id");
}
commit();
setAutoCommit(autoCommit);
PreparedStatement ps = prepareStatement("insert into t2017_id values (?, ?)");
// Insert the 3 first rows (id is 1-based).
for (int i = 0; i < 3; i++) {
ps.setInt(1, i + 1);
int length = INSERT[i];
ps.setCharacterStream(2, new LoopingAlphabetReader(length), length);
assertEquals(1, ps.executeUpdate());
}
// Insert the 4th row with a stream that's longer than the specified
// length, then the 5th row that's shorter. Both should fail, and the
// data shouldn't be inserted into the database.
Reader r4 = new LoopingAlphabetReader(INSERT[3]);
ps.setInt(1, 4);
ps.setCharacterStream(2, r4, INSERT[3] - 5);
try {
ps.executeUpdate();
fail("Insert should have failed, stream too long");
} catch (SQLException sqle) {
// The states are different between client and embedded.
assertSQLState(usingEmbedded() ? "XSDA4" : "XN015", sqle);
if (rollbackOnError) {
rollback();
}
}
Reader r5 = new LoopingAlphabetReader(INSERT[4]);
ps.setInt(1, 5);
ps.setCharacterStream(2, r5, INSERT[4] + 5);
try {
ps.executeUpdate();
fail("Insert should have failed, stream too short");
} catch (SQLException sqle) {
// The states are different between client and embedded.
assertSQLState(usingEmbedded() ? "XSDA4" : "XN017", sqle);
if (rollbackOnError) {
rollback();
}
}
// two rows and make sure the transaction commits.
for (int i = 5; i < INSERT.length; i++) {
ps.setInt(1, i + 1);
int length = INSERT[i];
ps.setCharacterStream(2, new LoopingAlphabetReader(length), length);
assertEquals(1, ps.executeUpdate());
}
if (!autoCommit) {
commit();
}
// Make sure we have the expected number of rows.
ResultSet rs = stmt.executeQuery("select count(*) from t2017_id");
rs.next();
assertEquals((rollbackOnError && !autoCommit ? 2 : 5), rs.getInt(1));
// Select data in the table, compare to what we expect.
rs = stmt.executeQuery("select * from t2017_id order by id asc");
// Check rows 1-4 if rollback on error is false.
if (autoCommit || !rollbackOnError) {
for (int i = 0; i < 3; i++) {
rs.next();
int id = rs.getInt(1);
assertTrue(id - 1 == i);
assertEquals(new LoopingAlphabetReader(INSERT[i]), rs.getCharacterStream(2));
}
}
// Check rows 6 and 7.
for (int i = 5; i < 7; i++) {
rs.next();
int id = rs.getInt(1);
assertTrue(id - 1 == i);
assertEquals(new LoopingAlphabetReader(INSERT[i]), rs.getCharacterStream(2));
}
assertFalse(rs.next());
rs.close();
}
Aggregations