Search in sources :

Example 1 with FbBlob

use of org.firebirdsql.gds.ng.FbBlob in project jaybird by FirebirdSQL.

the class TestV10OutputBlob method testUsingCancelledBlob.

/**
 * Test whether a cancelled blob cannot be used (indicating it was indeed cancelled).
 */
@Test
public void testUsingCancelledBlob() throws Exception {
    expectedException.expect(SQLException.class);
    expectedException.expect(allOf(errorCodeEquals(ISCConstants.isc_bad_segstr_id), message(startsWith(getFbMessage(ISCConstants.isc_bad_segstr_id)))));
    final int testId = 1;
    final byte[] baseContent = generateBaseContent();
    final int requiredSize = 256;
    final byte[] testBytes = generateBlobContent(baseContent, requiredSize);
    try (FbWireDatabase db = createDatabaseConnection()) {
        final SimpleStatementListener listener = new SimpleStatementListener();
        final FbTransaction transaction = getTransaction(db);
        try {
            final FbStatement statement = db.createStatement(transaction);
            statement.addStatementListener(listener);
            final FbBlob blob = db.createBlobForOutput(transaction, null);
            blob.open();
            int bytesWritten = 0;
            while (bytesWritten < testBytes.length) {
                // TODO the interface for writing blobs should be simpler
                byte[] buffer = new byte[Math.min(blob.getMaximumSegmentSize(), testBytes.length - bytesWritten)];
                System.arraycopy(testBytes, bytesWritten, buffer, 0, buffer.length);
                blob.putSegment(buffer);
                bytesWritten += buffer.length;
            }
            blob.cancel();
            statement.prepare(INSERT_BLOB_TABLE);
            final DatatypeCoder datatypeCoder = db.getDatatypeCoder();
            FieldValue param1 = new FieldValue(datatypeCoder.encodeInt(testId));
            FieldValue param2 = new FieldValue(datatypeCoder.encodeLong(blob.getBlobId()));
            statement.execute(RowValue.of(param1, param2));
            statement.close();
        } finally {
            transaction.commit();
        }
    }
}
Also used : SimpleStatementListener(org.firebirdsql.gds.ng.wire.SimpleStatementListener) DatatypeCoder(org.firebirdsql.gds.ng.DatatypeCoder) FieldValue(org.firebirdsql.gds.ng.fields.FieldValue) FbStatement(org.firebirdsql.gds.ng.FbStatement) FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase) FbTransaction(org.firebirdsql.gds.ng.FbTransaction) FbBlob(org.firebirdsql.gds.ng.FbBlob) Test(org.junit.Test)

Example 2 with FbBlob

use of org.firebirdsql.gds.ng.FbBlob in project jaybird by FirebirdSQL.

the class TestV10OutputBlob method testIsEof_afterOpen.

/**
 * Test if blob is not eof after open.
 */
@Test
public void testIsEof_afterOpen() throws Exception {
    try (FbWireDatabase db = createDatabaseConnection()) {
        final FbTransaction transaction = getTransaction(db);
        try {
            FbBlob blob = db.createBlobForOutput(transaction, null);
            assumeTrue("Output blob before open should be eof", blob.isEof());
            blob.open();
            assertFalse("Output blob after open should not be eof", blob.isEof());
        } finally {
            transaction.commit();
        }
    }
}
Also used : FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase) FbTransaction(org.firebirdsql.gds.ng.FbTransaction) FbBlob(org.firebirdsql.gds.ng.FbBlob) Test(org.junit.Test)

Example 3 with FbBlob

use of org.firebirdsql.gds.ng.FbBlob in project jaybird by FirebirdSQL.

the class TestV10OutputBlob method testIsEof_afterCancel.

/**
 * Test if blob is eof after cancel.
 */
@Test
public void testIsEof_afterCancel() throws Exception {
    try (FbWireDatabase db = createDatabaseConnection()) {
        final FbTransaction transaction = getTransaction(db);
        try {
            FbBlob blob = db.createBlobForOutput(transaction, null);
            assumeTrue("Output blob before open should be eof", blob.isEof());
            blob.open();
            blob.cancel();
            assertTrue("Output blob after cancel should be eof", blob.isEof());
        } finally {
            transaction.commit();
        }
    }
}
Also used : FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase) FbTransaction(org.firebirdsql.gds.ng.FbTransaction) FbBlob(org.firebirdsql.gds.ng.FbBlob) Test(org.junit.Test)

Example 4 with FbBlob

use of org.firebirdsql.gds.ng.FbBlob in project jaybird by FirebirdSQL.

the class TestV10InputBlob method testDoubleOpen.

/**
 * Tests double open not allowed.
 */
@Test
public void testDoubleOpen() throws Exception {
    expectedException.expect(SQLNonTransientException.class);
    expectedException.expect(allOf(errorCodeEquals(ISCConstants.isc_no_segstr_close), fbMessageStartsWith(ISCConstants.isc_no_segstr_close)));
    final int testId = 1;
    final byte[] baseContent = generateBaseContent();
    final int requiredSize = 256;
    populateBlob(testId, baseContent, requiredSize);
    try (FbWireDatabase db = createDatabaseConnection()) {
        try {
            long blobId = getBlobId(testId, db);
            final FbBlob blob = db.createBlobForInput(transaction, null, blobId);
            blob.open();
            // Double open
            blob.open();
        } finally {
            if (transaction != null)
                transaction.commit();
        }
    }
}
Also used : FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase) FbBlob(org.firebirdsql.gds.ng.FbBlob) Test(org.junit.Test)

Example 5 with FbBlob

use of org.firebirdsql.gds.ng.FbBlob in project jaybird by FirebirdSQL.

the class TestV10InputBlob method testBlobSeek_segmented.

/**
 * Tests absolute seek on a segmented blob. Expectation: fails with an exception
 */
@Test
public void testBlobSeek_segmented() throws Exception {
    final int testId = 1;
    final byte[] baseContent = generateBaseContent();
    // Use sufficiently large value so that multiple segments are used
    final int requiredSize = 4 * Short.MAX_VALUE;
    populateBlob(testId, baseContent, requiredSize);
    try (FbWireDatabase db = createDatabaseConnection()) {
        try {
            long blobId = getBlobId(testId, db);
            // NOTE: What matters is if the blob on the server is stream or segment
            final FbBlob blob = db.createBlobForInput(transaction, null, blobId);
            blob.open();
            int offset = baseContent.length / 2;
            expectedException.expect(SQLException.class);
            expectedException.expect(allOf(errorCodeEquals(ISCConstants.isc_bad_segstr_type), message(startsWith(getFbMessage(ISCConstants.isc_bad_segstr_type)))));
            blob.seek(offset, FbBlob.SeekMode.ABSOLUTE);
        } finally {
            if (transaction != null)
                transaction.commit();
        }
    }
}
Also used : FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase) FbBlob(org.firebirdsql.gds.ng.FbBlob) Test(org.junit.Test)

Aggregations

FbBlob (org.firebirdsql.gds.ng.FbBlob)12 FbWireDatabase (org.firebirdsql.gds.ng.wire.FbWireDatabase)11 Test (org.junit.Test)11 FbTransaction (org.firebirdsql.gds.ng.FbTransaction)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DatatypeCoder (org.firebirdsql.gds.ng.DatatypeCoder)1 FbStatement (org.firebirdsql.gds.ng.FbStatement)1 FieldValue (org.firebirdsql.gds.ng.fields.FieldValue)1 SimpleStatementListener (org.firebirdsql.gds.ng.wire.SimpleStatementListener)1 Synchronizable (org.firebirdsql.jdbc.Synchronizable)1