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);
}
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);
}
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();
}
}
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();
}
}
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);
}
}
Aggregations