Search in sources :

Example 16 with DBConnection

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

the class TVPAllTypes method setupVariation.

private void setupVariation() throws SQLException {
    conn = DriverManager.getConnection(connectionString);
    stmt = conn.createStatement();
    Utils.dropProcedureIfExists(procedureName, stmt);
    dropTVPS(tvpName);
    DBConnection dbConnection = new DBConnection(connectionString);
    DBStatement dbStmt = dbConnection.createStatement();
    tableSrc = new DBTable(true);
    tableDest = tableSrc.cloneSchema();
    dbStmt.createTable(tableSrc);
    dbStmt.createTable(tableDest);
    createTVPS(tvpName, tableSrc.getDefinitionOfColumns());
    createPreocedure(procedureName, tableDest.getEscapedTableName());
    dbStmt.populateTable(tableSrc);
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) DBTable(com.microsoft.sqlserver.testframework.DBTable) DBStatement(com.microsoft.sqlserver.testframework.DBStatement)

Example 17 with DBConnection

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

the class TVPSchemaTest method testSetup.

@BeforeEach
private void testSetup() throws SQLException {
    conn = new DBConnection(connectionString);
    stmt = conn.createStatement();
    dropProcedure();
    dropTables();
    dropTVPS();
    dropAndCreateSchema();
    createTVPS();
    createTables();
    createPreocedure();
    tvp = new SQLServerDataTable();
    tvp.addColumnMetadata("PlainChar", java.sql.Types.CHAR);
    tvp.addColumnMetadata("PlainVarchar", java.sql.Types.VARCHAR);
    tvp.addColumnMetadata("PlainVarcharMax", java.sql.Types.VARCHAR);
    tvp.addRow(expectecValue1, expectecValue2, expectecValue3);
    tvp.addRow(expectecValue1, expectecValue2, expectecValue3);
    tvp.addRow(expectecValue1, expectecValue2, expectecValue3);
    tvp.addRow(expectecValue1, expectecValue2, expectecValue3);
    tvp.addRow(expectecValue1, expectecValue2, expectecValue3);
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) SQLServerDataTable(com.microsoft.sqlserver.jdbc.SQLServerDataTable) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 18 with DBConnection

use of com.microsoft.sqlserver.testframework.DBConnection 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 19 with DBConnection

use of com.microsoft.sqlserver.testframework.DBConnection 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)

Example 20 with DBConnection

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

the class bvtTest method testStmtSserverCursorForwardOnly.

/**
 * Create a statement TYPE_SS_SEVER_CURSOR_FORWARD_ONLY, CONCUR_READ_ONLY, executeQuery verify cursor by using next and verify data
 *
 * @throws SQLException
 */
@Test
public void testStmtSserverCursorForwardOnly() throws SQLException {
    DBResultSetTypes rsType = DBResultSetTypes.TYPE_FORWARD_ONLY_CONCUR_READ_ONLY;
    String query = "SELECT * FROM " + table1.getEscapedTableName();
    try (DBConnection conn = new DBConnection(connectionString);
        DBStatement stmt = conn.createStatement(rsType.resultsetCursor, rsType.resultSetConcurrency);
        DBResultSet rs = stmt.executeQuery(query)) {
        // Verify resultset behavior
        rs.next();
        rs.verify(table1);
    }
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) DBStatement(com.microsoft.sqlserver.testframework.DBStatement) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet) DBResultSetTypes(com.microsoft.sqlserver.testframework.DBResultSetTypes) Test(org.junit.jupiter.api.Test)

Aggregations

DBConnection (com.microsoft.sqlserver.testframework.DBConnection)38 DBStatement (com.microsoft.sqlserver.testframework.DBStatement)25 Test (org.junit.jupiter.api.Test)16 DBResultSet (com.microsoft.sqlserver.testframework.DBResultSet)14 SQLException (java.sql.SQLException)8 DBTable (com.microsoft.sqlserver.testframework.DBTable)6 BeforeAll (org.junit.jupiter.api.BeforeAll)6 Connection (java.sql.Connection)5 Statement (java.sql.Statement)5 ResultSet (java.sql.ResultSet)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 SQLServerBulkCopy (com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)3 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)3 SQLServerCallableStatement (com.microsoft.sqlserver.jdbc.SQLServerCallableStatement)2 SQLServerPreparedStatement (com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)2 SQLServerStatement (com.microsoft.sqlserver.jdbc.SQLServerStatement)2 DBColumn (com.microsoft.sqlserver.testframework.DBColumn)2 DBPreparedStatement (com.microsoft.sqlserver.testframework.DBPreparedStatement)2 DisplayName (org.junit.jupiter.api.DisplayName)2 SQLServerColumnEncryptionJavaKeyStoreProvider (com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionJavaKeyStoreProvider)1