Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statement test cases #147

Merged
merged 6 commits into from
Feb 15, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@
import com.microsoft.sqlserver.testframework.Utils;
import com.microsoft.sqlserver.testframework.util.RandomUtil;

/**
* Tests batch execution with errors
*
*/
@RunWith(JUnitPlatform.class)
public class BatchExecuteWithErrorsTest extends AbstractTest {

public static final Logger log = Logger.getLogger("BatchExecuteWithErrors");
DBConnection con = null;
Connection con = null;
String tableN = RandomUtil.getIdentifier("t_Repro47239");
final String tableName = AbstractSQLGenerator.escapeIdentifier(tableN);
final String insertStmt = "INSERT INTO " + tableName + " VALUES (999, 'HELLO', '4/12/1994')";
Expand All @@ -48,6 +52,7 @@ public class BatchExecuteWithErrorsTest extends AbstractTest {

/**
* Batch test
*
* @throws SQLException
*/
@Test
Expand All @@ -63,8 +68,8 @@ public void Repro47239() throws SQLException {
String warning;
String error;
String severe;
con = new DBConnection(connectionString);
if (con.isSqlAzure()) {
con = DriverManager.getConnection(connectionString);
if (DBConnection.isSqlAzure(con)) {
// SQL Azure will throw exception for "raiserror WITH LOG", so the following RAISERROR statements have not "with log" option
warning = "RAISERROR ('raiserror level 4',4,1)";
error = "RAISERROR ('raiserror level 11',11,1)";
Expand Down Expand Up @@ -219,7 +224,7 @@ public void Repro47239() throws SQLException {
// and thus it cannot be put into a TSQL batch and it is useless here.
// So we have to skip the last scenario of this test case, i.e. "Test Severe (connection-closing) errors"
// It is worthwhile to still execute the first 5 test scenarios of this test case, in order to have best test coverage.
if (!con.isSqlAzure()) {
if (!DBConnection.isSqlAzure(conn)) {
// Test Severe (connection-closing) errors
stmt.addBatch(error);
stmt.addBatch(insertStmt);
Expand Down Expand Up @@ -269,12 +274,11 @@ public void Repro47239_large() throws Exception {

assumeTrue("JDBC42".equals(Utils.getConfiguredProperty("JDBC_Version")), "Aborting test case as JDBC version is not compatible. ");
// the DBConnection for detecting whether the server is SQL Azure or SQL Server.
con = new DBConnection(connectionString);

con = DriverManager.getConnection(connectionString);
final String warning;
final String error;
final String severe;
if (con.isSqlAzure()) {
if (DBConnection.isSqlAzure(con)) {
// SQL Azure will throw exception for "raiserror WITH LOG", so the following RAISERROR statements have not "with log" option
warning = "RAISERROR ('raiserror level 4',4,1)";
error = "RAISERROR ('raiserror level 11',11,1)";
Expand Down Expand Up @@ -409,7 +413,7 @@ public void Repro47239_large() throws Exception {
assertThat(bue.getMessage(), containsString("Syntax error converting date"));
}
catch (SQLException e) {
assertThat(e.getMessage(), containsString("Conversion failed when converting date"));
assertThat(e.getMessage(), containsString("Conversion failed when converting date"));
}

conn.setAutoCommit(true);
Expand All @@ -421,7 +425,7 @@ public void Repro47239_large() throws Exception {
// and thus it cannot be put into a TSQL batch and it is useless here.
// So we have to skip the last scenario of this test case, i.e. "Test Severe (connection-closing) errors"
// It is worthwhile to still execute the first 5 test scenarios of this test case, in order to have best test coverage.
if (!con.isSqlAzure()) {
if (!DBConnection.isSqlAzure(DriverManager.getConnection(connectionString))) {
// Test Severe (connection-closing) errors
stmt.addBatch(error);
stmt.addBatch(insertStmt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.util.RandomUtil;

/**
* Callable Mix tests using stored procedure with input and output
*
*/
@RunWith(JUnitPlatform.class)
public class CallableMixedTest extends AbstractTest {
Connection connection1 = null;
Statement Statement151 = null;
Connection connection = null;
Statement statement = null;
String tableN = RandomUtil.getIdentifier("TFOO3");
String procN = RandomUtil.getIdentifier("SPFOO3");
String tableName = AbstractSQLGenerator.escapeIdentifier(tableN);
Expand All @@ -39,65 +43,67 @@ public class CallableMixedTest extends AbstractTest {
*/
@Test
@DisplayName("Test CallableMix")
public void datatypestest() throws SQLException {
connection1 = DriverManager.getConnection(connectionString);
Statement151 = connection1.createStatement();
public void datatypesTest() throws SQLException {
connection = DriverManager.getConnection(connectionString);
statement = connection.createStatement();

try {
Statement151.executeUpdate("DROP TABLE " + tableName);
Statement151.executeUpdate(" DROP PROCEDURE " + procName);
statement.executeUpdate("DROP TABLE " + tableName);
statement.executeUpdate(" DROP PROCEDURE " + procName);
}
catch (Exception e) {
}

Statement151.executeUpdate("create table " + tableName + " (c1_int int primary key, col2 int)");
Statement151.executeUpdate("Insert into " + tableName + " values(0, 1)");
Statement151.close();
Statement Statement153 = connection1.createStatement();
Statement153.executeUpdate("CREATE PROCEDURE " + procName
statement.executeUpdate("create table " + tableName + " (c1_int int primary key, col2 int)");
statement.executeUpdate("Insert into " + tableName + " values(0, 1)");
statement.close();
Statement stmt = connection.createStatement();
stmt.executeUpdate("CREATE PROCEDURE " + procName
+ " (@p2_int int, @p2_int_out int OUTPUT, @p4_smallint smallint, @p4_smallint_out smallint OUTPUT) AS begin transaction SELECT * FROM "
+ tableName + " ; SELECT @p2_int_out=@p2_int, @p4_smallint_out=@p4_smallint commit transaction RETURN -2147483648");
Statement153.close();
stmt.close();

CallableStatement CallableStatement1 = connection1.prepareCall("{ ? = CALL " + procName + " (?, ?, ?, ?) }");
CallableStatement1.registerOutParameter((int) 1, (int) 4);
CallableStatement1.setObject((int) 2, Integer.valueOf("31"), (int) 4);
CallableStatement1.registerOutParameter((int) 3, (int) 4);
CallableStatement1.registerOutParameter((int) 5, java.sql.Types.BINARY);
CallableStatement1.registerOutParameter((int) 5, (int) 5);
CallableStatement1.setObject((int) 4, Short.valueOf("-5372"), (int) 5);
CallableStatement callableStatement = connection.prepareCall("{ ? = CALL " + procName + " (?, ?, ?, ?) }");
callableStatement.registerOutParameter((int) 1, (int) 4);
callableStatement.setObject((int) 2, Integer.valueOf("31"), (int) 4);
callableStatement.registerOutParameter((int) 3, (int) 4);
callableStatement.registerOutParameter((int) 5, java.sql.Types.BINARY);
callableStatement.registerOutParameter((int) 5, (int) 5);
callableStatement.setObject((int) 4, Short.valueOf("-5372"), (int) 5);

// get results and a value
ResultSet ResultSet1 = CallableStatement1.executeQuery();
ResultSet1.next();
ResultSet rs = callableStatement.executeQuery();
rs.next();

assertEquals(ResultSet1.getInt(1), 0, "Received data not equal to setdata");
assertEquals(CallableStatement1.getInt((int) 5), -5372, "Received data not equal to setdata");
assertEquals(rs.getInt(1), 0, "Received data not equal to setdata");
assertEquals(callableStatement.getInt((int) 5), -5372, "Received data not equal to setdata");

// do nothing and reexecute
ResultSet1 = CallableStatement1.executeQuery();
rs = callableStatement.executeQuery();
// get the param without getting the resultset
ResultSet1 = CallableStatement1.executeQuery();
assertEquals(CallableStatement1.getInt((int) 1), -2147483648, "Received data not equal to setdata");

ResultSet1 = CallableStatement1.executeQuery();
ResultSet1.next();

assertEquals(ResultSet1.getInt(1), 0, "Received data not equal to setdata");
assertEquals(CallableStatement1.getInt((int) 1), -2147483648, "Received data not equal to setdata");
assertEquals(CallableStatement1.getInt((int) 5), -5372, "Received data not equal to setdata");
ResultSet1 = CallableStatement1.executeQuery();
CallableStatement1.close();
ResultSet1.close();

rs = callableStatement.executeQuery();
assertEquals(callableStatement.getInt((int) 1), -2147483648, "Received data not equal to setdata");

rs = callableStatement.executeQuery();
rs.next();

assertEquals(rs.getInt(1), 0, "Received data not equal to setdata");
assertEquals(callableStatement.getInt((int) 1), -2147483648, "Received data not equal to setdata");
assertEquals(callableStatement.getInt((int) 5), -5372, "Received data not equal to setdata");
rs = callableStatement.executeQuery();
callableStatement.close();
rs.close();
stmt.close();
terminateVariation();
}


private void terminateVariation() throws SQLException {
Statement151 = connection1.createStatement();
Statement151.executeUpdate("DROP TABLE " + tableName);
Statement151.executeUpdate(" DROP PROCEDURE " + procName);
statement = connection.createStatement();
statement.executeUpdate("DROP TABLE " + tableName);
statement.executeUpdate(" DROP PROCEDURE " + procName);
statement.close();
connection.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@

import com.microsoft.sqlserver.testframework.AbstractTest;

/**
* Testing with LimitEscape queries
*
*/
@RunWith(JUnitPlatform.class)
public class LimitEscapeTest extends AbstractTest {
public static final Logger log = Logger.getLogger("LimitEscape");
Expand Down Expand Up @@ -755,6 +759,9 @@ public void verifyOffsetException() throws Exception {
}
}

/**
* clean up
*/
@BeforeAll
public static void beforeAll() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@
import static org.junit.jupiter.api.Assertions.fail;

import java.sql.ResultSet;
import java.sql.Statement;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.DBConnection;
import com.microsoft.sqlserver.testframework.DBStatement;

/**
* Testing merge queries
*/
@RunWith(JUnitPlatform.class)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to have class & method level javadocs for all classes in this PR

public class MergeTest extends AbstractTest {
private static final String setupTables = "IF OBJECT_ID (N'dbo.CricketTeams', N'U') IS NOT NULL DROP TABLE dbo.CricketTeams;"
Expand Down Expand Up @@ -50,7 +53,7 @@ public class MergeTest extends AbstractTest {
@DisplayName("Merge Test")
public void runTest() throws Exception {
DBConnection conn = new DBConnection(connectionString);
if (conn.serverversion() >= 10) {
if (conn.getServerversion() >= 10) {
DBStatement stmt = conn.createStatement();
stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(setupTables);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@

import com.microsoft.sqlserver.testframework.AbstractTest;

/**
* Multipart parameters
*
*/
@RunWith(JUnitPlatform.class)
public class NamedParamMultiPartTest extends AbstractTest {
private static final String dataPut = "eminem ";
private static Connection Connection2 = null;
private static Driver Driver1 = null;
private static Connection connection2 = null;
private static Driver driver1 = null;
private static CallableStatement cs = null;

/**
Expand All @@ -41,8 +45,8 @@ public class NamedParamMultiPartTest extends AbstractTest {
*/
@BeforeAll
public static void beforeAll() throws SQLException {
Driver1 = DriverManager.getDriver(connectionString);
Connection2 = DriverManager.getConnection(connectionString);
driver1 = DriverManager.getDriver(connectionString);
connection2 = DriverManager.getConnection(connectionString);
}

/**
Expand All @@ -52,20 +56,14 @@ public static void beforeAll() throws SQLException {
@Test
@DisplayName("Named Param Multi Part Test")
public void datatypestest() throws Exception {
Connection Connection1 = DriverManager.getConnection(connectionString);
Statement Statement1 = Connection1.createStatement();
Statement1.close();
Connection1.close();

DatabaseMetaData DatabaseMetaData1 = Connection2.getMetaData();
String String1 = DatabaseMetaData1.getDriverVersion();
// 1.0.505.2
Connection Connection3 = Driver1.connect(connectionString, null);
Statement Statement6 = Connection2.createStatement();
Statement6.executeUpdate(
Connection connection = driver1.connect(connectionString, null);
Statement statement = connection2.createStatement();
statement.executeUpdate(
"if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[mystoredproc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) DROP PROCEDURE [mystoredproc]");
Statement6.executeUpdate("CREATE PROCEDURE [mystoredproc] (@p_out varchar(255) OUTPUT) AS set @p_out = '" + dataPut + "'");
Statement6.close();
statement.executeUpdate("CREATE PROCEDURE [mystoredproc] (@p_out varchar(255) OUTPUT) AS set @p_out = '" + dataPut + "'");
statement.close();
connection.close();
}

/**
Expand All @@ -74,7 +72,7 @@ public void datatypestest() throws Exception {
*/
@Test
public void update1() throws Exception {
cs = Connection2.prepareCall("{ CALL [mystoredproc] (?) }");
cs = connection2.prepareCall("{ CALL [mystoredproc] (?) }");
cs.registerOutParameter("p_out", Types.VARCHAR);
cs.executeUpdate();
String data = cs.getString("p_out");
Expand All @@ -87,7 +85,7 @@ public void update1() throws Exception {
*/
@Test
public void update2() throws Exception {
cs = Connection2.prepareCall("{ CALL [dbo].[mystoredproc] (?) }");
cs = connection2.prepareCall("{ CALL [dbo].[mystoredproc] (?) }");
cs.registerOutParameter("p_out", Types.VARCHAR);
cs.executeUpdate();
Object data = cs.getObject("p_out");
Expand All @@ -100,9 +98,9 @@ public void update2() throws Exception {
*/
@Test
public void update3() throws Exception {
String catalog = Connection2.getCatalog();
String catalog = connection2.getCatalog();
String storedproc = "[" + catalog + "]" + ".[dbo].[mystoredproc]";
cs = Connection2.prepareCall("{ CALL " + storedproc + " (?) }");
cs = connection2.prepareCall("{ CALL " + storedproc + " (?) }");
cs.registerOutParameter("p_out", Types.VARCHAR);
cs.executeUpdate();
Object data = cs.getObject("p_out");
Expand All @@ -115,7 +113,7 @@ public void update3() throws Exception {
*/
@Test
public void update4() throws Exception {
cs = Connection2.prepareCall("{ CALL mystoredproc (?) }");
cs = connection2.prepareCall("{ CALL mystoredproc (?) }");
cs.registerOutParameter("p_out", Types.VARCHAR);
cs.executeUpdate();
Object data = cs.getObject("p_out");
Expand All @@ -128,7 +126,7 @@ public void update4() throws Exception {
*/
@Test
public void update5() throws Exception {
cs = Connection2.prepareCall("{ CALL dbo.mystoredproc (?) }");
cs = connection2.prepareCall("{ CALL dbo.mystoredproc (?) }");
cs.registerOutParameter("p_out", Types.VARCHAR);
cs.executeUpdate();
Object data = cs.getObject("p_out");
Expand All @@ -141,9 +139,9 @@ public void update5() throws Exception {
*/
@Test
public void update6() throws Exception {
String catalog = Connection2.getCatalog();
String catalog = connection2.getCatalog();
String storedproc = catalog + ".dbo.mystoredproc";
cs = Connection2.prepareCall("{ CALL " + storedproc + " (?) }");
cs = connection2.prepareCall("{ CALL " + storedproc + " (?) }");
cs.registerOutParameter("p_out", Types.VARCHAR);
cs.executeUpdate();
Object data = cs.getObject("p_out");
Expand All @@ -156,8 +154,8 @@ public void update6() throws Exception {
@AfterAll
public static void afterAll() {
try {
if (null != Connection2) {
Connection2.close();
if (null != connection2) {
connection2.close();
}
if (null != cs) {
cs.close();
Expand Down
Loading