Search in sources :

Example 11 with LoopingAlphabetStream

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

the class PositionedStoreStreamTest method testPositionAfterEOFRead.

/**
 * Verifies that reading after EOF doesn't change the position.
 */
public void testPositionAfterEOFRead() throws IOException, StandardException {
    InputStream in = new LoopingAlphabetStream(10);
    PositionedStoreStream pss = new PositionedStoreStream(in);
    assertEquals(0, pss.getPosition());
    for (int i = 0; i < 10; i++) {
        pss.read();
        assertEquals(i + 1, pss.getPosition());
    }
    assertEquals(10, pss.getPosition());
    assertEquals(-1, pss.read());
    assertEquals(10, pss.getPosition());
    assertEquals(-1, pss.read());
    assertEquals(0, pss.skip(199));
    assertEquals(10, pss.getPosition());
    pss.resetStream();
    assertEquals(0, pss.getPosition());
}
Also used : PositionedStoreStream(org.apache.derby.impl.jdbc.PositionedStoreStream) InputStream(java.io.InputStream) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 12 with LoopingAlphabetStream

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

the class PositionedStoreStreamTest method testDerby3735.

/**
 * A regression test for DERBY-3735.
 * <p>
 * If the bug is present, the repositioning will cause an EOF-exception to
 * be thrown in {@code skipFully}.
 */
public void testDerby3735() throws IOException, StandardException {
    InputStream in = new LoopingAlphabetStream(2);
    PositionedStoreStream pss = new PositionedStoreStream(in);
    byte[] b = new byte[2];
    for (int i = 0; i < 10; i++) {
        // After the first iteration, the position will be decreased and
        // eventually become negative.
        pss.read(b);
        println("Position at iteration " + i + ": " + pss.getPosition());
    }
    // This failed, because we tried skipping more bytes than there are in
    // the stream to get to position 0.
    pss.reposition(0);
}
Also used : PositionedStoreStream(org.apache.derby.impl.jdbc.PositionedStoreStream) InputStream(java.io.InputStream) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 13 with LoopingAlphabetStream

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

the class PositionedStoreStreamTest method testDerby3781.

/**
 * Tests that trying to move past the end of the stream leaves the stream
 * object in a consistent state, and can be repositioned again after the
 * failed reposition attempt.
 * <p>
 * Issue logged in Jira as DERBY-3781
 *
 * @throws IOException if reading the stream fails unexpectedly
 * @throws StandardException will never happen
 */
public void testDerby3781() throws IOException, StandardException {
    final long size = 10;
    InputStream in = new LoopingAlphabetStream(size);
    PositionedStoreStream pss = new PositionedStoreStream(in);
    assertEquals("Invalid initial position", 0L, pss.getPosition());
    // Goto end.
    pss.reposition(size - 1);
    assertEquals(size - 1, pss.getPosition());
    assertEquals('j', pss.read());
    assertEquals(size, pss.getPosition());
    assertEquals(-1, pss.read());
    // This step is crucial, position must be different than zero when the
    // first exception below is thrown.
    // Goto middle.
    pss.reposition(size / 2);
    assertEquals(size / 2, pss.getPosition());
    try {
        // Try to go past end.
        pss.reposition(size * 2);
        fail("Should have failed with EOFException");
    } catch (EOFException eofe) {
    // Ignore this exception
    }
    // Failed here before, because internal state was inconsistent.
    // Assumed: pos = 5, underlying stream at pos 5, skipped (size -1 - pos)
    // Actual: pos = 5, underlying stream at pos (size -1)
    // Goto end.
    pss.reposition(size - 1);
    assertEquals(size - 1, pss.getPosition());
    assertEquals('j', pss.read());
}
Also used : PositionedStoreStream(org.apache.derby.impl.jdbc.PositionedStoreStream) InputStream(java.io.InputStream) EOFException(java.io.EOFException) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 14 with LoopingAlphabetStream

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

the class PositionedStoreStreamTest method testReadEverythingInOneGo.

/**
 * Reads the whole stream repeatedly in one go, and resets it after each
 * read.
 */
public void testReadEverythingInOneGo() throws IOException, StandardException {
    InputStream in = new LoopingAlphabetStream(127);
    PositionedStoreStream pss = new PositionedStoreStream(in);
    byte[] b = new byte[256];
    for (int i = 0; i < 3; i++) {
        Arrays.fill(b, (byte) -1);
        assertEquals(127, pss.read(b, 0, 256));
        assertEquals(-1, pss.read(b, 127, 10));
        assertEquals(-1, b[127]);
        assertTrue(-1 != b[126]);
        assertEquals('a', b[0]);
        pss.reposition(0);
    }
}
Also used : PositionedStoreStream(org.apache.derby.impl.jdbc.PositionedStoreStream) InputStream(java.io.InputStream) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 15 with LoopingAlphabetStream

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

the class PositionedStoreStreamTest method testRepositionBackwards.

public void testRepositionBackwards() throws IOException, StandardException {
    final long length = 20L;
    InputStream in = new LoopingAlphabetStream(length);
    PositionedStoreStream pss = new PositionedStoreStream(in);
    assertEquals(0, pss.getPosition());
    // Position backwards one by one.
    for (int i = (int) length; i >= 0; i--) {
        InputStream inComp = new LoopingAlphabetStream(length);
        pss.reposition(i);
        inComp.skip(i);
        assertEquals(inComp.read(), pss.read());
    }
    // Position backwards two by two.
    for (int i = (int) length - 1; i >= 0; i -= 2) {
        InputStream inComp = new LoopingAlphabetStream(length);
        pss.reposition(i);
        inComp.skip(i);
        assertEquals(inComp.read(), pss.read());
    }
}
Also used : PositionedStoreStream(org.apache.derby.impl.jdbc.PositionedStoreStream) InputStream(java.io.InputStream) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

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