Search in sources :

Example 1 with DBColumn

use of com.microsoft.sqlserver.testframework.DBColumn in project mssql-jdbc by Microsoft.

the class lobsTest method testMultipleClose.

/**
 * Tests stream closures
 *
 * @param streamClass
 * @throws Exception
 */
private void testMultipleClose(Class streamClass) throws Exception {
    DBConnection conn = new DBConnection(connectionString);
    String[] types = { "varchar(max)", "nvarchar(max)", "varbinary(max)" };
    try {
        table = this.createTable(table, types, true);
        DBStatement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
        String query = "select * from " + table.getEscapedTableName();
        DBResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            for (int i = 0; i < 3; i++) {
                DBColumn col = table.getColumns().get(i);
                if (!col.getSqlType().canConvert(streamClass, DBCoercion.GET, new DBConnection(connectionString)))
                    continue;
                Object stream = rs.getXXX(i + 1, streamClass);
                if (stream == null) {
                    assertEquals(stream, rs.getObject(i + 1), "Stream is null when data is not");
                } else {
                    // close the stream twice
                    if (streamClass == DBCharacterStream.class) {
                        ((Reader) stream).close();
                        ((Reader) stream).close();
                    } else {
                        ((InputStream) stream).close();
                        ((InputStream) stream).close();
                    }
                }
            }
        }
    } finally {
        if (null != table)
            this.dropTables(table);
        if (null != null)
            conn.close();
    }
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) DBStatement(com.microsoft.sqlserver.testframework.DBStatement) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) DBColumn(com.microsoft.sqlserver.testframework.DBColumn) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet)

Example 2 with DBColumn

use of com.microsoft.sqlserver.testframework.DBColumn in project mssql-jdbc by Microsoft.

the class lobsTest method testInvalidLobs.

/**
 * Tests invalid lobs
 *
 * @param lobClass
 * @param isResultSet
 * @throws SQLException
 */
private void testInvalidLobs(Class lobClass, boolean isResultSet) throws SQLException {
    String[] clobTypes = { "varchar(max)", "nvarchar(max)" };
    String[] blobTypes = { "varbinary(max)" };
    int choose = ThreadLocalRandom.current().nextInt(3);
    switch(choose) {
        case 0:
            datasize = packetSize;
            break;
        case 1:
            datasize = packetSize + ThreadLocalRandom.current().nextInt(packetSize) + 1;
            break;
        default:
            datasize = packetSize - ThreadLocalRandom.current().nextInt(packetSize);
    }
    int coercionType = isResultSet ? DBCoercion.UPDATE : DBCoercion.SET;
    try {
        if (clobType == classType(lobClass) || nClobType == classType(lobClass)) {
            table = this.createTable(table, clobTypes, true);
        } else {
            table = this.createTable(table, blobTypes, true);
        }
        Object updater;
        for (int i = 0; i < table.getColumns().size(); i++) {
            DBColumn col = table.getColumns().get(i);
            if (!col.getSqlType().canConvert(lobClass, coercionType, new DBConnection(connectionString)))
                continue;
            // re-create LOB since it might get closed
            Object lob = this.createLob(lobClass);
            if (isResultSet) {
                Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
                updater = stmt.executeQuery("Select " + table.getEscapedTableName() + ".[" + col.getColumnName() + "]" + " from " + table.getEscapedTableName());
                ((ResultSet) updater).next();
            } else
                updater = conn.prepareStatement("update " + table.getEscapedTableName() + " set " + ".[" + col.getColumnName() + "]" + "=?");
            try {
                this.updateLob(lob, updater, 1);
            } catch (SQLException e) {
                boolean verified = false;
                if (lobClass == Clob.class)
                    streamLength = ((DBInvalidUtil.InvalidClob) lob).length;
                else if (lobClass == Blob.class)
                    streamLength = ((DBInvalidUtil.InvalidBlob) lob).length;
                // Case 1: Invalid length value is passed as LOB length
                if (streamLength < 0 || streamLength == Long.MAX_VALUE) {
                    // Applies to all LOB types ("The length {0} is not valid}
                    assertTrue(e.getMessage().startsWith("The length"), "Unexpected message thrown : " + e.getMessage());
                    assertTrue(e.getMessage().endsWith("is not valid."), "Unexpected message thrown : " + e.getMessage());
                    verified = true;
                }
                // Case 2: CharacterStream or Clob.getCharacterStream threw IOException
                if (lobClass == DBCharacterStream.class || (lobClass == Clob.class && ((DBInvalidUtil.InvalidClob) lob).stream != null)) {
                    DBInvalidUtil.InvalidCharacterStream stream = lobClass == DBCharacterStream.class ? ((DBInvalidUtil.InvalidCharacterStream) lob) : ((DBInvalidUtil.InvalidClob) lob).stream;
                    if (stream.threwException) {
                        // CharacterStream threw IOException
                        String[] args = { "java.io.IOException: " + DBInvalidUtil.InvalidCharacterStream.IOExceptionMsg };
                        assertTrue(e.getMessage().contains(args[0]));
                        verified = true;
                    }
                }
                if (!verified) {
                    // Odd CharacterStream length will throw this exception
                    if (!e.getMessage().contains("The stream value is not the specified length. The specified length was")) {
                        if (lobClass == DBCharacterStream.class || lobClass == DBBinaryStream.class)
                            assertTrue(e.getSQLState() != null, "SQLState should not be null");
                        assertTrue(e.getMessage().contains("An error occurred while reading the value from the stream object. Error:"));
                    }
                }
            }
        }
    } catch (Exception e) {
        this.dropTables(table);
        e.printStackTrace();
    }
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) SQLException(java.sql.SQLException) DBStatement(com.microsoft.sqlserver.testframework.DBStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SQLException(java.sql.SQLException) DBInvalidUtil(com.microsoft.sqlserver.testframework.DBInvalidUtil) ResultSet(java.sql.ResultSet) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet) DBColumn(com.microsoft.sqlserver.testframework.DBColumn) NClob(java.sql.NClob) Clob(java.sql.Clob)

Aggregations

DBColumn (com.microsoft.sqlserver.testframework.DBColumn)2 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)2 DBResultSet (com.microsoft.sqlserver.testframework.DBResultSet)2 DBStatement (com.microsoft.sqlserver.testframework.DBStatement)2 DBInvalidUtil (com.microsoft.sqlserver.testframework.DBInvalidUtil)1 BufferedInputStream (java.io.BufferedInputStream)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 Clob (java.sql.Clob)1 NClob (java.sql.NClob)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1