Search in sources :

Example 31 with LoopingAlphabetStream

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.

the class BlobClob4BlobTest method testDerby5113.

/**
 * Test that Blob.truncate() resets the position before copying the first
 * N bytes into a new holder object. The bug is only triggered if the
 * BLOB returned as a stream from store. That is, it must be larger than
 * one page so that it's not materialized. Regression test for DERBY-5113.
 */
public void testDerby5113() throws Exception {
    setAutoCommit(false);
    // Insert a BLOB larger than one page. Normally, this means larger
    // than 32K, but the test tables use smaller pages, see comment in
    // setUp().
    PreparedStatement insert = prepareStatement("insert into testblob(a) values ?");
    insert.setBinaryStream(1, new LoopingAlphabetStream(5000), 5000);
    insert.executeUpdate();
    // Retrieve the BLOB.
    Statement s = createStatement();
    ResultSet rs = s.executeQuery("select a from testblob");
    rs.next();
    Blob blob = rs.getBlob(1);
    // Now call getBytes() so that the position in the underlying stream
    // is changed.
    byte[] bytes = blob.getBytes(1, 3000);
    assertEquals(new LoopingAlphabetStream(3000), new ByteArrayInputStream(bytes));
    // Truncate the BLOB. This used to fail with "Reached EOF prematurely"
    // because truncate() didn't move the position in the underlying stream
    // back to the beginning.
    blob.truncate(4000);
    // Verify that the BLOB was truncated correctly.
    assertEquals(4000, blob.length());
    bytes = blob.getBytes(1, 4000);
    assertEquals(new LoopingAlphabetStream(4000), new ByteArrayInputStream(bytes));
    rs.close();
}
Also used : Blob(java.sql.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 32 with LoopingAlphabetStream

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.

the class BlobClob4BlobTest method testPositionBlobDeterministic.

/**
 * Tests the {@code Blob.position} using a deterministic sequence of
 * actions and arguments.
 */
public void testPositionBlobDeterministic() throws IOException, SQLException {
    getConnection().setAutoCommit(false);
    final int size = 100000;
    PreparedStatement ps = prepareStatement("INSERT INTO testBlob (a, b) VALUES (?, ?)");
    ps.setBinaryStream(1, new LoopingAlphabetStream(size), size);
    ps.setInt(2, size);
    ps.executeUpdate();
    ps.close();
    ps = prepareStatement("select a from testBlob where b = ?");
    ps.setInt(1, size);
    ResultSet rs = ps.executeQuery();
    assertTrue("No data found", rs.next());
    Blob blob = rs.getBlob(1);
    // Try with a one-byte pattern.
    // k number 11 in the alphabet
    byte[] pattern = new byte[] { (byte) 'k' };
    assertEquals(11, blob.position(pattern, 1));
    // Try with a non-existing pattern.
    pattern = new byte[] { (byte) 'p', (byte) 'o' };
    assertEquals(-1, blob.position(pattern, size / 3));
    // Loop through all matches
    pattern = new byte[] { (byte) 'd', (byte) 'e' };
    long foundAtPos = 1;
    int index = 0;
    int stepSize = ByteAlphabet.modernLatinLowercase().byteCount();
    while ((foundAtPos = blob.position(pattern, foundAtPos + 1)) != -1) {
        assertEquals((stepSize * index++) + 4, foundAtPos);
        byte[] fetchedPattern = blob.getBytes(foundAtPos, pattern.length);
        assertTrue(Arrays.equals(pattern, fetchedPattern));
    }
    // Try a longer pattern.
    // 65 KB
    int pSize = 65 * 1024;
    pattern = new byte[pSize];
    assertEquals(pSize, new LoopingAlphabetStream(pSize).read(pattern));
    assertEquals(1, blob.position(pattern, 1));
    assertEquals(stepSize * 100 + 1, blob.position(pattern, stepSize * 99 + 7));
    // Try again after getting the length.
    assertEquals(size, blob.length());
    assertEquals(stepSize * 100 + 1, blob.position(pattern, stepSize * 99 + 7));
    // Try specifing a starting position that's too big.
    try {
        blob.position(pattern, size * 2);
        fail("Accepted position after end of Blob");
    } catch (SQLException sqle) {
        assertSQLState("XJ076", sqle);
    }
    // Fetch the last 5 bytes, try with a partial match at the end.
    byte[] blobEnd = blob.getBytes(size - 4, 5);
    pattern = new byte[6];
    System.arraycopy(blobEnd, 0, pattern, 0, blobEnd.length);
    // Only lowercase in the looping alphabet stream.
    pattern[5] = 'X';
    assertEquals(-1, blob.position(pattern, size - 10));
    // Get the very last byte, try with a partial match at the end.
    blobEnd = blob.getBytes(size, 1);
    pattern = new byte[] { blobEnd[0], 'X' };
    assertEquals(-1, blob.position(pattern, size - 5));
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 33 with LoopingAlphabetStream

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.

the class BlobClob4BlobTest method testNegativeTestDerby265Blob.

/**
 * Test fix for derby-265.
 * Test that if getBlob is called after the transaction
 * in which it was created is committed, a proper user error
 * is thrown instead of an NPE.
 * Basically per the spec, getBlob is valid only for the duration of
 * the transaction it was created in
 * Updated for DERBY-1511: The test case wasn't supposed to fail in the
 * first place (neither with NPE nor with "proper user error") since none
 * of the BLOBs are accessed after the transaction that created them was
 * completed.
 * @throws Exception
 * @throws FileNotFoundException
 * @throws IOException
 */
public void testNegativeTestDerby265Blob() throws Exception {
    getConnection().setAutoCommit(false);
    PreparedStatement ps = prepareStatement("insert into testBlob(b, a) values(?,?)");
    for (int i = 0; i < 3; i++) {
        InputStream fis = new LoopingAlphabetStream(300000);
        ps.setInt(1, i);
        ps.setBinaryStream(2, fis, 300000);
        ps.executeUpdate();
        fis.close();
    }
    commit();
    getConnection().setAutoCommit(true);
    Statement s = createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    s.execute("SELECT b, a FROM testBlob");
    ResultSet rs1 = s.getResultSet();
    Statement s2 = createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    s2.executeQuery("SELECT b, a FROM testBlob");
    ResultSet rs2 = s2.getResultSet();
    rs2.next();
    Blob b2 = rs2.getBlob(2);
    rs1.next();
    Blob b1 = rs1.getBlob(2);
    rs1.close();
    // DERBY-1511: Fetching the next BLOB here used to fail because it
    // had been pre-fetched before the commit and was closed in the commit.
    // Now, we don't pre-fetch BLOBs anymore, so expect to get a working
    // object here.
    assertTrue(rs2.next());
    assertNotNull(rs2.getBlob(2));
    rs2.close();
}
Also used : Blob(java.sql.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 34 with LoopingAlphabetStream

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.

the class BlobClob4BlobTest method testLockingWithLongRowClob.

/**
 * test locking with a long row + long column
 */
public void testLockingWithLongRowClob() throws Exception {
    ResultSet rs;
    Statement stmt, stmt2;
    stmt = createStatement();
    stmt.execute("alter table testClob add column al varchar(2000)");
    stmt.execute("alter table testClob add column bl varchar(3000)");
    stmt.execute("alter table testClob add column cl varchar(2000)");
    stmt.execute("alter table testClob add column dl varchar(3000)");
    stmt.execute("alter table testClob add column el CLOB(400k)");
    PreparedStatement ps = prepareStatement("insert into testClob (al, bl, cl, dl, el, b) values(?,?,?,?,?,?)");
    ps.setString(1, Formatters.padString("blaaa", 2000));
    ps.setString(2, Formatters.padString("tralaaaa", 3000));
    ps.setString(3, Formatters.padString("foodar", 2000));
    ps.setString(4, Formatters.padString("moped", 3000));
    InputStream streamIn = new LoopingAlphabetStream(10000);
    ps.setAsciiStream(5, streamIn, 10000);
    ps.setInt(6, 1);
    // DERBY-4312 make sure commit() doesn't interfere here.
    commit();
    ps.executeUpdate();
    streamIn.close();
    ps.close();
    commit();
    stmt = createStatement();
    rs = stmt.executeQuery("select el from testClob");
    // fetch row back, get the column as a clob.
    Clob clob = null;
    assertTrue("FAIL - row not found", rs.next());
    clob = rs.getClob(1);
    assertTrue("FAIL - clob is null", clob != null);
    rs.close();
    stmt.close();
    Connection conn2 = openDefaultConnection();
    // turn off autocommit, otherwise blobs/clobs cannot hang around
    // until end of transaction
    conn2.setAutoCommit(false);
    // the following should timeout
    stmt2 = conn2.createStatement();
    try {
        stmt2.executeUpdate("update testClob set el = 'smurfball' where b = 1");
        // Cleanup on fail
        stmt2.close();
        conn2.rollback();
        conn2.close();
        fail("FAIL - statement should timeout");
    } catch (SQLException se) {
        checkException(LOCK_TIMEOUT, se);
    }
    // Test that update goes through after the transaction is committed
    commit();
    stmt2.executeUpdate("update testClob set el = 'smurfball' where b = 1");
    stmt2.close();
    conn2.commit();
    conn2.close();
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream) Clob(java.sql.Clob)

Example 35 with LoopingAlphabetStream

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream 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;
        }
    };
}
Also used : Random(java.util.Random) CleanDatabaseTestSetup(org.apache.derbyTesting.junit.CleanDatabaseTestSetup) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SystemPropertyTestSetup(org.apache.derbyTesting.junit.SystemPropertyTestSetup) Connection(java.sql.Connection) BaseTestSuite(org.apache.derbyTesting.junit.BaseTestSuite) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream) Properties(java.util.Properties) CharAlphabet(org.apache.derbyTesting.functionTests.util.streams.CharAlphabet) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Aggregations

LoopingAlphabetStream (org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)61 PreparedStatement (java.sql.PreparedStatement)33 InputStream (java.io.InputStream)31 ResultSet (java.sql.ResultSet)31 Statement (java.sql.Statement)20 Blob (java.sql.Blob)10 SQLException (java.sql.SQLException)9 LoopingAlphabetReader (org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)9 PositionedStoreStream (org.apache.derby.impl.jdbc.PositionedStoreStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Connection (java.sql.Connection)7 OutputStream (java.io.OutputStream)5 Reader (java.io.Reader)5 Clob (java.sql.Clob)3 Timestamp (java.sql.Timestamp)3 DataInputStream (java.io.DataInputStream)2 EOFException (java.io.EOFException)2 CallableStatement (java.sql.CallableStatement)2 Date (java.sql.Date)2 Time (java.sql.Time)2