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

Tests | Added more datatype tests to increase code coverage #878

Merged
merged 39 commits into from
Dec 25, 2018
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
eb3df3a
ported tests from VSO and added tests to increase code coverage
lilgreenbird Nov 13, 2018
c31b1f3
moved resultset tests to resultset
lilgreenbird Nov 14, 2018
145ee72
removed unused import
lilgreenbird Nov 14, 2018
fd123b2
added tests to increase code coverage
lilgreenbird Nov 16, 2018
3275750
fixed issue with timezone
lilgreenbird Nov 17, 2018
45eeef5
fixed issue with timezone
lilgreenbird Nov 17, 2018
c5f4a9d
modified testWithThaiLocale to use current time
lilgreenbird Nov 19, 2018
d93e0fe
fixed issue with timezone
lilgreenbird Nov 19, 2018
27702d3
fixed issue with timezone
lilgreenbird Nov 19, 2018
a48c810
fixed issue with timezone
lilgreenbird Nov 20, 2018
9c697cf
added SparseTest and BigIntegerTest and cleaned up with try-with-reso…
lilgreenbird Nov 20, 2018
83b6ddc
review changes
lilgreenbird Nov 23, 2018
b3c5ac5
review comments
lilgreenbird Nov 23, 2018
8f71ce6
more review updates
lilgreenbird Nov 23, 2018
c46db9f
review updates
lilgreenbird Nov 26, 2018
e9556dd
Merge remote-tracking branch 'upstream/dev' into cov
lilgreenbird Nov 26, 2018
3a0703d
removed dummy file
lilgreenbird Nov 26, 2018
d507114
review udpates
lilgreenbird Nov 27, 2018
2994200
more cleanup
lilgreenbird Nov 27, 2018
e3512f9
updated isSqlAzure
lilgreenbird Nov 28, 2018
e4cb7f2
Merge branch 'cov' of https://github.com/lilgreenbird/mssql-jdbc into…
lilgreenbird Dec 13, 2018
5217270
review updates
lilgreenbird Dec 13, 2018
79ac177
more review updates
lilgreenbird Dec 13, 2018
ada91ae
fixed imports
lilgreenbird Dec 13, 2018
36838a8
updated failed error strings with more detail
lilgreenbird Dec 13, 2018
a196be9
added locale to time format
lilgreenbird Dec 13, 2018
c7df1ed
format date for comparision
lilgreenbird Dec 13, 2018
dbcc0a3
1 more timezone fix
lilgreenbird Dec 14, 2018
f36c132
Merge remote-tracking branch 'upstream/dev' into cov
lilgreenbird Dec 14, 2018
bcc8732
cleanup leaded stream resources
lilgreenbird Dec 17, 2018
a5ee49f
Merge remote-tracking branch 'upstream/dev' into cov
lilgreenbird Dec 17, 2018
62f615b
Merge remote-tracking branch 'upstream/dev' into cov
lilgreenbird Dec 18, 2018
fe01b41
manually merged with #903 changes for now
lilgreenbird Dec 18, 2018
d72eccc
merged
lilgreenbird Dec 18, 2018
238b66f
more cleanup on assertEqual checks
lilgreenbird Dec 18, 2018
dc1a721
more cleanup on assertEqual checks
lilgreenbird Dec 18, 2018
6eac22c
cosmetic changes from review
lilgreenbird Dec 19, 2018
72956ed
updated all assertEquals params to proper expected, actual order
lilgreenbird Dec 19, 2018
db38827
fixed comments and leaked resultsets
lilgreenbird Dec 20, 2018
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 @@ -153,6 +153,6 @@ protected Object[][] getContents() {
{"R_expectedValue", "Expected value: "}, {"R_expectedValueAtIndex", "Expected value at index: "},
{"R_switchFailed", "Switch case is not matched with data"},
{"R_resultsetNotInstance", "Result set is not instance of SQLServerResultSet"},

{"R_noJRESupport", "No JRE support for {0}"},
};
}
32 changes: 32 additions & 0 deletions src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class TestUtils {
public static final String SERVER_TYPE_SQL_AZURE = "SQLAzure";
// private static SqlType types = null;
private static ArrayList<SqlType> types = null;
private final static int ENGINE_EDITION_FOR_SQL_AZURE = 5;
private final static int ENGINE_EDITION_FOR_SQL_AZURE_DW = 6;

/**
* Returns serverType
Expand Down Expand Up @@ -666,4 +668,34 @@ public static boolean supportJDBC43(Connection con) throws SQLException {
public static String escapeSingleQuotes(String name) {
return name.replace("'", "''");
}

/**
* Returns if connected to SQL Azure
* @param con
* connection to server
* @return boolean
* @throws SQLException
*/
public static boolean isSqlAzure(Connection con) throws SQLException {
try (ResultSet rs = con.createStatement().executeQuery("SELECT CAST(SERVERPROPERTY('EngineEdition') as INT)")) {
rs.next();
int engineEdition = rs.getInt(1);
return (engineEdition == ENGINE_EDITION_FOR_SQL_AZURE || engineEdition == ENGINE_EDITION_FOR_SQL_AZURE_DW);
}
}

/**
* Returns if connected to SQL Azure DW
* @param con
* connection to server
* @return boolean
* @throws SQLException
*/
public static boolean isSqlAzureDW(Connection con) throws SQLException {
try (ResultSet rs = con.createStatement().executeQuery("SELECT CAST(SERVERPROPERTY('EngineEdition') as INT)")) {
rs.next();
int engineEdition = rs.getInt(1);
return (engineEdition == ENGINE_EDITION_FOR_SQL_AZURE_DW);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.microsoft.sqlserver.jdbc.callablestatement;

import java.sql.*;

import com.microsoft.sqlserver.jdbc.RandomUtil;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
import com.microsoft.sqlserver.jdbc.TestUtils;
import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;

import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import static org.junit.jupiter.api.Assertions.fail;


@RunWith(JUnitPlatform.class)
public class CallableMixedTest {

@Test
public void datatypestest() throws Exception {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionString = TestUtils.getConfiguredProperty("mssql_jdbc_test_connection_properties");
String tableName = RandomUtil.getIdentifier("TFOO3");
String procName = RandomUtil.getIdentifier("SPFOO3");

try (Connection conn = DriverManager.getConnection(connectionString)) {
try (Statement stmt = conn.createStatement()) {
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt);

String createSQL = "create table " + AbstractSQLGenerator.escapeIdentifier(tableName)
+ "(c1_int int primary key, col2 int)";
stmt.executeUpdate(createSQL);

stmt.executeUpdate("Insert into " + AbstractSQLGenerator.escapeIdentifier(tableName) + " values(0, 1)");

stmt.executeUpdate("CREATE PROCEDURE " + AbstractSQLGenerator.escapeIdentifier(procName)
+ " (@p2_int int, @p2_int_out int OUTPUT, @p4_smallint smallint, @p4_smallint_out smallint OUTPUT) AS begin transaction SELECT * FROM "
+ AbstractSQLGenerator.escapeIdentifier(tableName)
+ " ; SELECT @p2_int_out=@p2_int, @p4_smallint_out=@p4_smallint commit transaction RETURN -2147483648");
}

try (CallableStatement cstmt = conn.prepareCall(
"{ ? = CALL " + AbstractSQLGenerator.escapeIdentifier(procName) + " (?, ?, ?, ?) }")) {
cstmt.registerOutParameter((int) 1, (int) 4);
cstmt.setObject((int) 2, Integer.valueOf("31"), (int) 4);
cstmt.registerOutParameter((int) 3, (int) 4);
cstmt.registerOutParameter((int) 5, java.sql.Types.BINARY); // Test OUT param
// re-registration
// (Defect 60921)
cstmt.registerOutParameter((int) 5, (int) 5);
cstmt.setObject((int) 4, Short.valueOf("-5372"), (int) 5);

// get results and a value
ResultSet rs = cstmt.executeQuery();
rs.next();

if (rs.getInt(1) != 0) {
fail("Received data not equal to setdata");
}

if (cstmt.getInt((int) 5) != -5372) {
fail("Received data not equal to setdata");
}

// do nothing and reexecute
rs = cstmt.executeQuery();
// get the param without getting the resultset
rs = cstmt.executeQuery();
if (cstmt.getInt((int) 1) != -2147483648) {
fail("Received data not equal to setdata");
}

if (cstmt.getInt((int) 1) != -2147483648) {
fail("Received data not equal to setdata");
}

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

if (rs.getInt(1) != 0) {
fail("Received data not equal to setdata");
}

if (cstmt.getInt((int) 1) != -2147483648) {
fail("Received data not equal to setdata");
}

if (cstmt.getInt((int) 5) != -5372) {
fail("Received data not equal to setdata");
}

rs = cstmt.executeQuery();
}
} finally {
try (Connection conn = DriverManager.getConnection(connectionString);
Statement stmt = conn.createStatement()) {
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt);
} catch (SQLException e) {
fail(e.toString());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
package com.microsoft.sqlserver.jdbc.datatypes;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.math.BigInteger;

import com.microsoft.sqlserver.jdbc.RandomUtil;
import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;
import com.microsoft.sqlserver.testframework.AbstractTest;

/*
* This test is for testing the setObject methods for the new data type mappings in JDBC 4.1 for java.math.BigInteger
*/
@RunWith(JUnitPlatform.class)
public class BigIntegerTest extends AbstractTest {

enum TestType {
SETOBJECT_WITHTYPE, // This is to test conversions in Table B-5
SETOBJECT_WITHOUTTYPE, // This is to test conversions in Table B-4
SETNULL // This is to test setNull method
};

final static String tableName = RandomUtil.getIdentifier("BigIntegerTestTable");

@Test
public void testJDBC41BigInteger() throws Exception {
try (Connection conn = DriverManager.getConnection(connectionString); Statement stmt = conn.createStatement()) {

// Create the test table
try {
stmt.executeUpdate("drop table " + AbstractSQLGenerator.escapeIdentifier(tableName));
} catch (Exception e) {}

String query = "create table " + AbstractSQLGenerator.escapeIdentifier(tableName)
+ " (col1 varchar(100), col2 bigint, col3 real, col4 float, "
+ "col5 numeric(38,0), col6 int, col7 smallint, col8 char(100), col9 varchar(max), "
+ "id int IDENTITY primary key)";
stmt.executeUpdate(query);

try (PreparedStatement pstmt = conn
.prepareStatement("INSERT INTO " + AbstractSQLGenerator.escapeIdentifier(tableName)
+ " VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?) SELECT * FROM "
+ AbstractSQLGenerator.escapeIdentifier(tableName) + " where id = ?")) {

// test that the driver converts the BigInteger values greater than LONG.MAX_VALUE and lesser than
// LONG.MIN_VALUE correctly
// A random value that is bigger than LONG.MAX_VALUE
BigInteger bigIntPos = new BigInteger("922337203685477580776767676");
// A random value that is smaller than LONG.MIN_VALUE
BigInteger bigIntNeg = new BigInteger("-922337203685477580776767676");

// Test the setObject method for different types of BigInteger values. Since BigInteger is mapped to
// JDBC
// BIGINT, the max and min limits for
int row = 1;
testSetObject(AbstractSQLGenerator.escapeIdentifier(tableName), BigInteger.valueOf(Long.MAX_VALUE),
row++, pstmt, TestType.SETOBJECT_WITHTYPE);

testSetObject(AbstractSQLGenerator.escapeIdentifier(tableName), BigInteger.valueOf(Long.MIN_VALUE),
row++, pstmt, TestType.SETOBJECT_WITHTYPE);
testSetObject(AbstractSQLGenerator.escapeIdentifier(tableName), BigInteger.valueOf(10), row++, pstmt,
TestType.SETOBJECT_WITHTYPE);
testSetObject(AbstractSQLGenerator.escapeIdentifier(tableName), BigInteger.valueOf(-10), row++, pstmt,
TestType.SETOBJECT_WITHTYPE);
testSetObject(AbstractSQLGenerator.escapeIdentifier(tableName), BigInteger.ZERO, row++, pstmt,
TestType.SETOBJECT_WITHTYPE);
testSetObject(AbstractSQLGenerator.escapeIdentifier(tableName), bigIntPos, row++, pstmt,
TestType.SETOBJECT_WITHTYPE);
testSetObject(AbstractSQLGenerator.escapeIdentifier(tableName), bigIntNeg, row++, pstmt,
TestType.SETOBJECT_WITHTYPE);

// Test setObject method with SQL TYPE parameter
testSetObject(AbstractSQLGenerator.escapeIdentifier(tableName), BigInteger.valueOf(Long.MAX_VALUE),
row++, pstmt, TestType.SETOBJECT_WITHOUTTYPE);
testSetObject(AbstractSQLGenerator.escapeIdentifier(tableName), BigInteger.valueOf(Long.MIN_VALUE),
row++, pstmt, TestType.SETOBJECT_WITHOUTTYPE);
testSetObject(AbstractSQLGenerator.escapeIdentifier(tableName), BigInteger.valueOf(1000), row++, pstmt,
TestType.SETOBJECT_WITHOUTTYPE);
testSetObject(AbstractSQLGenerator.escapeIdentifier(tableName), BigInteger.valueOf(-1000), row++, pstmt,
TestType.SETOBJECT_WITHOUTTYPE);
testSetObject(AbstractSQLGenerator.escapeIdentifier(tableName), BigInteger.ZERO, row++, pstmt,
TestType.SETOBJECT_WITHOUTTYPE);
testSetObject(AbstractSQLGenerator.escapeIdentifier(tableName), bigIntPos, row++, pstmt,
TestType.SETOBJECT_WITHOUTTYPE);
testSetObject(AbstractSQLGenerator.escapeIdentifier(tableName), bigIntNeg, row++, pstmt,
TestType.SETOBJECT_WITHOUTTYPE);

// Test setNull
testSetObject(AbstractSQLGenerator.escapeIdentifier(tableName), bigIntNeg, row++, pstmt,
TestType.SETNULL);

try {
stmt.executeUpdate("drop table " + AbstractSQLGenerator.escapeIdentifier(tableName));
} catch (Exception e) {}
}
}
}

static void testSetObject(String tableName, BigInteger obj, int id, PreparedStatement pstmt,
TestType testType) throws SQLException {
if (TestType.SETOBJECT_WITHTYPE == testType) {
callSetObjectWithType(obj, pstmt);
} else if (TestType.SETOBJECT_WITHOUTTYPE == testType) {
callSetObjectWithoutType(obj, pstmt);
} else if (TestType.SETNULL == testType) {
callSetNull(obj, pstmt);
} else
assertEquals(true, false, "Invalid test type");

// The id column
pstmt.setObject(10, id);

pstmt.execute();
pstmt.getMoreResults();
try (ResultSet rs = pstmt.getResultSet()) {
rs.next();

if (TestType.SETNULL == testType) {
for (int i = 1; 9 >= i; ++i) {
// Get the data first before calling rs.wasNull()
rs.getString(i);
assertEquals(rs.wasNull(), true, "setNull mismatch");
}
return;
}

if ((0 > obj.compareTo(BigInteger.valueOf(Long.MIN_VALUE)))
|| (0 < obj.compareTo(BigInteger.valueOf(Long.MAX_VALUE)))) {
// For the BigInteger values greater/less than Long limits test only the long data type.
// This test is here just to make sure the driver does not do anything wired when the value is
// bigger/smaller than JDBC BIGINT
assertEquals(rs.getString(1), Long.valueOf(obj.longValue()).toString(),
"getString(greater/less than Long limits) mismatch");
assertEquals(rs.getLong(2), obj.longValue(), "getLong(greater/less than Long limits) mismatch");
// As CHAR is fixed length, rs.getString() returns a string of the size allocated in the database.
// Need to trim it for comparison.
assertEquals(rs.getString(8).trim(), Long.valueOf(obj.longValue()).toString(),
"getString(greater/less than Long limits (char)) mismatch");

assertEquals(rs.getString(9), Long.valueOf(obj.longValue()).toString(),
"getString(greater/less than Long limits (varchar(max)))) mismatch");
} else {
assertEquals(rs.getString(1), obj.toString(), "getString mismatch");
assertEquals(rs.getLong(2), obj.longValue(), "getLong mismatch");
assertEquals(rs.getFloat(3), obj.floatValue(), "getFloat mismatch");
assertEquals(rs.getDouble(4), obj.doubleValue(), "getDouble(float) mismatch");
assertEquals(rs.getDouble(5), obj.doubleValue(), "getDouble(numeric) mismatch");
if (obj.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) >= 0) {
assertEquals(rs.getInt(6), Integer.MAX_VALUE, "getInt(numeric) mismatch");
} else if (obj.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) <= 0) {
assertEquals(rs.getInt(6), Integer.MIN_VALUE, "getInt(numeric) mismatch");
} else {
assertEquals(rs.getInt(6), obj.intValue(), "getInt(numeric) mismatch");
}
if (obj.compareTo(BigInteger.valueOf(Short.MAX_VALUE)) >= 0) {
assertEquals(rs.getShort(7), Short.MAX_VALUE, "getShort(numeric) mismatch");
} else if (obj.compareTo(BigInteger.valueOf(Short.MIN_VALUE)) <= 0) {
assertEquals(rs.getShort(7), Short.MIN_VALUE, "getShort(numeric) mismatch");
} else {
assertEquals(rs.getShort(7), obj.shortValue(), "getShort(numeric) mismatch");
}

assertEquals(rs.getString(8).trim(), obj.toString(), "getString(char) mismatch");
assertEquals(rs.getString(9), obj.toString(), "getString(varchar(max)) mismatch");
}
}
}

static void callSetObjectWithType(BigInteger obj, PreparedStatement pstmt) throws SQLException {
pstmt.setObject(1, obj, java.sql.Types.VARCHAR);
pstmt.setObject(2, obj, java.sql.Types.BIGINT);
pstmt.setObject(3, obj, java.sql.Types.FLOAT);
pstmt.setObject(4, obj, java.sql.Types.DOUBLE);
pstmt.setObject(5, obj, java.sql.Types.NUMERIC);
// Use Integer/Short limits instead of Long limits for the int/smallint column
if (obj.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) >= 0) {
pstmt.setObject(6, BigInteger.valueOf(Integer.MAX_VALUE), java.sql.Types.INTEGER);
} else if (obj.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) <= 0) {
pstmt.setObject(6, BigInteger.valueOf(Integer.MIN_VALUE), java.sql.Types.INTEGER);
} else {
pstmt.setObject(6, obj, java.sql.Types.INTEGER);
}
if (obj.compareTo(BigInteger.valueOf(Short.MAX_VALUE)) >= 0) {
pstmt.setObject(7, BigInteger.valueOf(Short.MAX_VALUE), java.sql.Types.SMALLINT);
} else if (obj.compareTo(BigInteger.valueOf(Short.MIN_VALUE)) <= 0) {
pstmt.setObject(7, BigInteger.valueOf(Short.MIN_VALUE), java.sql.Types.SMALLINT);
} else {
pstmt.setObject(7, obj, java.sql.Types.SMALLINT);
}
pstmt.setObject(8, obj, java.sql.Types.CHAR);
pstmt.setObject(9, obj, java.sql.Types.LONGVARCHAR);
}

static void callSetObjectWithoutType(BigInteger obj, PreparedStatement pstmt) throws SQLException {
// Cannot send a long value to a column of type int/smallint (even if the long value is small enough to fit in
// those types)
pstmt.setObject(1, obj);
pstmt.setObject(2, obj);
pstmt.setObject(3, obj);
pstmt.setObject(4, obj);
pstmt.setObject(5, obj);
// Use Integer/Short limits instead of Long limits for the int/smallint column
if (obj.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) >= 0) {
pstmt.setObject(6, BigInteger.valueOf(Integer.MAX_VALUE));
} else if (obj.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) <= 0) {
pstmt.setObject(6, BigInteger.valueOf(Integer.MIN_VALUE));
} else {
pstmt.setObject(6, obj);
}
if (obj.compareTo(BigInteger.valueOf(Short.MAX_VALUE)) >= 0) {
pstmt.setObject(7, BigInteger.valueOf(Short.MAX_VALUE));
} else if (obj.compareTo(BigInteger.valueOf(Short.MIN_VALUE)) <= 0) {
pstmt.setObject(7, BigInteger.valueOf(Short.MIN_VALUE));
} else {
pstmt.setObject(7, obj);
}

pstmt.setObject(8, obj);
pstmt.setObject(9, obj);
}

static void callSetNull(BigInteger obj, PreparedStatement pstmt) throws SQLException {
pstmt.setNull(1, java.sql.Types.VARCHAR);
pstmt.setNull(2, java.sql.Types.BIGINT);
pstmt.setNull(3, java.sql.Types.FLOAT);
pstmt.setNull(4, java.sql.Types.DOUBLE);
pstmt.setNull(5, java.sql.Types.NUMERIC);
pstmt.setNull(6, java.sql.Types.INTEGER);
pstmt.setNull(7, java.sql.Types.SMALLINT);
pstmt.setNull(8, java.sql.Types.CHAR);
pstmt.setNull(9, java.sql.Types.LONGVARCHAR);
}
}
Loading