use of com.microsoft.sqlserver.jdbc.ISQLServerPreparedStatement in project mssql-jdbc by Microsoft.
the class WrapperTest method wrapTest.
/**
* Wrapper tests
* @throws Exception
*/
@Test
public void wrapTest() throws Exception {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(connectionString);
Statement stmt = con.createStatement();
try {
// First make sure that a statement can be unwrapped
boolean isWrapper = ((SQLServerStatement) stmt).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerStatement"));
assertEquals(isWrapper, true, "SQLServerStatement should be a wrapper for self");
isWrapper = ((SQLServerStatement) stmt).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerStatement"));
assertEquals(isWrapper, true, "SQLServerStatement should be a wrapper for ISQLServerStatement");
isWrapper = ((SQLServerStatement) stmt).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerConnection"));
assertEquals(isWrapper, false, "SQLServerStatement should not be a wrapper for SQLServerConnection");
// Now make sure that we can unwrap a SQLServerCallableStatement to a SQLServerStatement
CallableStatement cs = con.prepareCall("{ ? = CALL " + "ProcName" + " (?, ?, ?, ?) }");
// Test the class first
isWrapper = ((SQLServerCallableStatement) cs).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerStatement"));
assertEquals(isWrapper, true, "SQLServerCallableStatement should be a wrapper for SQLServerStatement");
// Now unwrap the Callable to a statement and call a SQLServerStatement specific function and make sure it succeeds.
SQLServerStatement stmt2 = (SQLServerStatement) ((SQLServerCallableStatement) cs).unwrap(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerStatement"));
stmt2.setResponseBuffering("adaptive");
// now test the interface
isWrapper = ((SQLServerCallableStatement) cs).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerCallableStatement"));
assertEquals(isWrapper, true, "SQLServerCallableStatement should be a wrapper for ISQLServerCallableStatement");
// Now unwrap the Callable to a statement and call a SQLServerStatement specific function and make sure it succeeds.
ISQLServerPreparedStatement stmt4 = (ISQLServerPreparedStatement) ((SQLServerCallableStatement) cs).unwrap(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerPreparedStatement"));
stmt4.setResponseBuffering("adaptive");
if (isKatmaiServer())
stmt4.setDateTimeOffset(1, null);
// Try Unwrapping CallableStatement to a callableStatement
isWrapper = ((SQLServerCallableStatement) cs).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerCallableStatement"));
assertEquals(isWrapper, true, "SQLServerCallableStatement should be a wrapper for SQLServerCallableStatement");
// Now unwrap the Callable to a SQLServerCallableStatement and call a SQLServerStatement specific function and make sure it succeeds.
SQLServerCallableStatement stmt3 = (SQLServerCallableStatement) ((SQLServerCallableStatement) cs).unwrap(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerCallableStatement"));
stmt3.setResponseBuffering("adaptive");
if (isKatmaiServer()) {
stmt3.setDateTimeOffset(1, null);
}
if (null != stmt4) {
stmt4.close();
}
if (null != cs) {
cs.close();
}
} catch (UnsupportedOperationException e) {
assertEquals(System.getProperty("java.specification.version"), "1.5", "isWrapperFor should be supported in anything other than 1.5");
assertTrue(e.getMessage().equalsIgnoreCase("This operation is not supported."), "Wrong exception message");
} finally {
if (null != stmt) {
stmt.close();
}
if (null != con) {
con.close();
}
}
}
Aggregations