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

Fix closeable resource leaks in Junit tests #797

Merged
merged 16 commits into from
Sep 21, 2018

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import org.junit.runner.RunWith;
import org.opentest4j.TestAbortedException;

import com.microsoft.sqlserver.jdbc.RandomData;
import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement;
import com.microsoft.sqlserver.jdbc.SQLServerResultSet;
import com.microsoft.sqlserver.jdbc.SQLServerStatement;
import com.microsoft.sqlserver.jdbc.TestResource;
import com.microsoft.sqlserver.testframework.util.RandomData;
import com.microsoft.sqlserver.testframework.util.Util;
import com.microsoft.sqlserver.jdbc.TestUtils;


/**
Expand Down Expand Up @@ -516,7 +516,7 @@ public void testNumericNormalization() throws SQLException {

private void testChar(SQLServerStatement stmt, String[] values) throws SQLException {
String sql = "select * from " + charTable;
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) Util.getPreparedStmt(con, sql,
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) TestUtils.getPreparedStmt(con, sql,
stmtColEncSetting)) {
try (ResultSet rs = (stmt == null) ? pstmt.executeQuery() : stmt.executeQuery(sql)) {
int numberOfColumns = rs.getMetaData().getColumnCount();
Expand All @@ -530,7 +530,7 @@ private void testChar(SQLServerStatement stmt, String[] values) throws SQLExcept

private void testBinary(SQLServerStatement stmt, LinkedList<byte[]> values) throws SQLException {
String sql = "select * from " + binaryTable;
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) Util.getPreparedStmt(con, sql,
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) TestUtils.getPreparedStmt(con, sql,
stmtColEncSetting)) {
try (ResultSet rs = (stmt == null) ? pstmt.executeQuery() : stmt.executeQuery(sql)) {
int numberOfColumns = rs.getMetaData().getColumnCount();
Expand All @@ -545,7 +545,7 @@ private void testBinary(SQLServerStatement stmt, LinkedList<byte[]> values) thro

private void testDate(SQLServerStatement stmt, LinkedList<Object> values1) throws SQLException {
String sql = "select * from " + dateTable;
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) Util.getPreparedStmt(con, sql,
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) TestUtils.getPreparedStmt(con, sql,
stmtColEncSetting)) {
try (ResultSet rs = (stmt == null) ? pstmt.executeQuery() : stmt.executeQuery(sql)) {
int numberOfColumns = rs.getMetaData().getColumnCount();
Expand Down Expand Up @@ -599,9 +599,9 @@ private void testGetObjectForTemporal(ResultSet rs, int numberOfColumns,

Object expected = null;
if (rs.getMetaData().getColumnTypeName(i).equalsIgnoreCase("smalldatetime")) {
expected = Util.roundSmallDateTimeValue(values.get(index));
expected = TestUtils.roundSmallDateTimeValue(values.get(index));
} else if (rs.getMetaData().getColumnTypeName(i).equalsIgnoreCase("datetime")) {
expected = Util.roundDatetimeValue(values.get(index));
expected = TestUtils.roundDatetimeValue(values.get(index));
} else {
expected = values.get(index);
}
Expand Down Expand Up @@ -758,7 +758,7 @@ private void testGetStringForDate(ResultSet rs, int numberOfColumns,
+ TestResource.getResource("R_expectedValue") + values.get(index));
} else if (index == 4) // round value for datetime
{
Object datetimeValue = "" + Util.roundDatetimeValue(values.get(index));
Object datetimeValue = "" + TestUtils.roundDatetimeValue(values.get(index));
assertTrue(
stringValue1.equalsIgnoreCase("" + datetimeValue)
&& stringValue2.equalsIgnoreCase("" + datetimeValue)
Expand All @@ -768,7 +768,7 @@ private void testGetStringForDate(ResultSet rs, int numberOfColumns,
+ TestResource.getResource("R_expectedValue") + datetimeValue);
} else if (index == 5) // round value for smalldatetime
{
Object smalldatetimeValue = "" + Util.roundSmallDateTimeValue(values.get(index));
Object smalldatetimeValue = "" + TestUtils.roundSmallDateTimeValue(values.get(index));
assertTrue(
stringValue1.equalsIgnoreCase("" + smalldatetimeValue)
&& stringValue2.equalsIgnoreCase("" + smalldatetimeValue)
Expand Down Expand Up @@ -896,14 +896,14 @@ private void testGetDate(ResultSet rs, int numberOfColumns, LinkedList<Object> v
stringValue1 = "" + ((SQLServerResultSet) rs).getDateTime(i);
stringValue2 = "" + ((SQLServerResultSet) rs).getDateTime(i + 1);
stringValue3 = "" + ((SQLServerResultSet) rs).getDateTime(i + 2);
expected = "" + Util.roundDatetimeValue(values.get(4));
expected = "" + TestUtils.roundDatetimeValue(values.get(4));
break;

case 16:
stringValue1 = "" + ((SQLServerResultSet) rs).getSmallDateTime(i);
stringValue2 = "" + ((SQLServerResultSet) rs).getSmallDateTime(i + 1);
stringValue3 = "" + ((SQLServerResultSet) rs).getSmallDateTime(i + 2);
expected = "" + Util.roundSmallDateTimeValue(values.get(5));
expected = "" + TestUtils.roundSmallDateTimeValue(values.get(5));
break;

default:
Expand All @@ -926,7 +926,7 @@ private void testGetDate(ResultSet rs, int numberOfColumns, LinkedList<Object> v

private void testNumeric(Statement stmt, String[] numericValues, boolean isNull) throws SQLException {
String sql = "select * from " + numericTable;
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) Util.getPreparedStmt(con, sql,
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) TestUtils.getPreparedStmt(con, sql,
stmtColEncSetting)) {
try (SQLServerResultSet rs = (stmt == null) ? (SQLServerResultSet) pstmt.executeQuery()
: (SQLServerResultSet) stmt.executeQuery(sql)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement;
import com.microsoft.sqlserver.jdbc.SQLServerResultSet;
import com.microsoft.sqlserver.jdbc.TestResource;
import com.microsoft.sqlserver.testframework.util.Util;
import com.microsoft.sqlserver.jdbc.TestUtils;


/**
Expand All @@ -32,7 +32,6 @@
*/
@RunWith(JUnitPlatform.class)
public class PrecisionScaleTest extends AESetup {
private static SQLServerPreparedStatement pstmt = null;

private static java.util.Date date = null;
private static int offsetFromGMT = 0;
Expand Down Expand Up @@ -339,7 +338,7 @@ private void populateDateNormalCase(int scale) throws SQLException {
String sql = "insert into " + dateTable + " values( " + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?,"
+ "?,?,?" + ")";

try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) Util.getPreparedStmt(con, sql,
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) TestUtils.getPreparedStmt(con, sql,
stmtColEncSetting)) {

// datetime2(5)
Expand Down Expand Up @@ -381,7 +380,7 @@ private void populateDateNormalCaseNull(int scale) throws SQLException {
String sql = "insert into " + dateTable + " values( " + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?,"
+ "?,?,?" + ")";

try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) Util.getPreparedStmt(con, sql,
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) TestUtils.getPreparedStmt(con, sql,
stmtColEncSetting)) {

// datetime2(5)
Expand Down Expand Up @@ -421,7 +420,7 @@ private void populateDateNormalCaseNull(int scale) throws SQLException {
private void populateNumericNormalCase(String[] numeric, int precision, int scale) throws SQLException {
String sql = "insert into " + numericTable + " values( " + "?,?,?," + "?,?,?," + "?,?,?" + ")";

try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) Util.getPreparedStmt(con, sql,
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) TestUtils.getPreparedStmt(con, sql,
stmtColEncSetting)) {

// float(30)
Expand All @@ -446,7 +445,7 @@ private void populateNumericNormalCase(String[] numeric, int precision, int scal
private void populateNumericSetObject(String[] numeric, int precision, int scale) throws SQLException {
String sql = "insert into " + numericTable + " values( " + "?,?,?," + "?,?,?," + "?,?,?" + ")";

try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) Util.getPreparedStmt(con, sql,
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) TestUtils.getPreparedStmt(con, sql,
stmtColEncSetting)) {

// float(30)
Expand All @@ -472,7 +471,7 @@ private void populateNumericSetObject(String[] numeric, int precision, int scale
private void populateNumericSetObjectNull(int precision, int scale) throws SQLException {
String sql = "insert into " + numericTable + " values( " + "?,?,?," + "?,?,?," + "?,?,?" + ")";

try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) Util.getPreparedStmt(con, sql,
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) TestUtils.getPreparedStmt(con, sql,
stmtColEncSetting)) {

// float(30)
Expand All @@ -499,7 +498,7 @@ private void populateDateSetObject(int scale) throws SQLException {
String sql = "insert into " + dateTable + " values( " + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?,"
+ "?,?,?" + ")";

try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) Util.getPreparedStmt(con, sql,
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) TestUtils.getPreparedStmt(con, sql,
stmtColEncSetting)) {

// datetime2(5)
Expand Down Expand Up @@ -542,7 +541,7 @@ private void populateDateSetObjectNull(int scale) throws SQLException {
String sql = "insert into " + dateTable + " values( " + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?,"
+ "?,?,?" + ")";

try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) Util.getPreparedStmt(con, sql,
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) TestUtils.getPreparedStmt(con, sql,
stmtColEncSetting)) {

// datetime2(5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* available under the terms of the MIT License. See the LICENSE file in the project root for more information.
*/

package com.microsoft.sqlserver.testframework.util;
package com.microsoft.sqlserver.jdbc;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -20,9 +20,9 @@

import com.microsoft.sqlserver.jdbc.SQLServerResultSetMetaData;
import com.microsoft.sqlserver.testframework.DBConnection;
import com.microsoft.sqlserver.testframework.DBStatement;
import com.microsoft.sqlserver.testframework.DBResultSet;
import com.microsoft.sqlserver.testframework.DBTable;
import com.microsoft.sqlserver.testframework.Utils;


public class ComparisonUtil {
Expand All @@ -37,49 +37,52 @@ public class ComparisonUtil {
*/
public static void compareSrcTableAndDestTableIgnoreRowOrder(DBConnection con, DBTable srcTable,
DBTable destTable) throws SQLException {
DBResultSet srcResultSetCount = con.createStatement()
.executeQuery("SELECT COUNT(*) FROM " + srcTable.getEscapedTableName() + ";");
DBResultSet dstResultSetCount = con.createStatement()
.executeQuery("SELECT COUNT(*) FROM " + destTable.getEscapedTableName() + ";");
srcResultSetCount.next();
dstResultSetCount.next();
int srcRows = srcResultSetCount.getInt(1);
int destRows = dstResultSetCount.getInt(1);

if (srcRows != destRows) {
fail("Souce table and Destination table have different number of rows.");
}
try (DBStatement stmt = con.createStatement();
DBResultSet srcResultSetCount = stmt
.executeQuery("SELECT COUNT(*) FROM " + srcTable.getEscapedTableName() + ";");
DBResultSet dstResultSetCount = stmt
.executeQuery("SELECT COUNT(*) FROM " + destTable.getEscapedTableName() + ";")) {
srcResultSetCount.next();
dstResultSetCount.next();
int srcRows = srcResultSetCount.getInt(1);
int destRows = dstResultSetCount.getInt(1);

if (srcRows != destRows) {
fail("Souce table and Destination table have different number of rows.");
}

if (srcTable.getColumns().size() != destTable.getColumns().size()) {
fail("Souce table and Destination table have different number of columns.");
}
if (srcTable.getColumns().size() != destTable.getColumns().size()) {
fail("Souce table and Destination table have different number of columns.");
}

DBResultSet srcResultSet = con.createStatement().executeQuery(
"SELECT * FROM " + srcTable.getEscapedTableName() + " ORDER BY [" + srcTable.getColumnName(1) + "], ["
+ srcTable.getColumnName(2) + "],[" + srcTable.getColumnName(3) + "];");
DBResultSet dstResultSet = con.createStatement().executeQuery(
"SELECT * FROM " + destTable.getEscapedTableName() + " ORDER BY [" + destTable.getColumnName(1) + "], ["
+ destTable.getColumnName(2) + "],[" + destTable.getColumnName(3) + "];");

while (srcResultSet.next() && dstResultSet.next()) {
for (int i = 0; i < destTable.getColumns().size(); i++) {
SQLServerResultSetMetaData srcMeta = (SQLServerResultSetMetaData) ((ResultSet) srcResultSet.product())
.getMetaData();
SQLServerResultSetMetaData destMeta = (SQLServerResultSetMetaData) ((ResultSet) dstResultSet.product())
.getMetaData();

int srcJDBCTypeInt = srcMeta.getColumnType(i + 1);
int destJDBCTypeInt = destMeta.getColumnType(i + 1);

// verify column types
if (srcJDBCTypeInt != destJDBCTypeInt) {
fail("Souce table and Destination table have different number of columns.");
try (DBResultSet srcResultSet = stmt.executeQuery(
"SELECT * FROM " + srcTable.getEscapedTableName() + " ORDER BY [" + srcTable.getColumnName(1)
+ "], [" + srcTable.getColumnName(2) + "],[" + srcTable.getColumnName(3) + "];");
DBResultSet dstResultSet = stmt.executeQuery("SELECT * FROM " + destTable.getEscapedTableName()
+ " ORDER BY [" + destTable.getColumnName(1) + "], [" + destTable.getColumnName(2) + "],["
+ destTable.getColumnName(3) + "];")) {

while (srcResultSet.next() && dstResultSet.next()) {
for (int i = 0; i < destTable.getColumns().size(); i++) {
SQLServerResultSetMetaData srcMeta = (SQLServerResultSetMetaData) ((ResultSet) srcResultSet
.product()).getMetaData();
SQLServerResultSetMetaData destMeta = (SQLServerResultSetMetaData) ((ResultSet) dstResultSet
.product()).getMetaData();

int srcJDBCTypeInt = srcMeta.getColumnType(i + 1);
int destJDBCTypeInt = destMeta.getColumnType(i + 1);

// verify column types
if (srcJDBCTypeInt != destJDBCTypeInt) {
fail("Souce table and Destination table have different number of columns.");
}

Object expectedValue = srcResultSet.getObject(i + 1);
Object actualValue = dstResultSet.getObject(i + 1);

compareExpectedAndActual(destJDBCTypeInt, expectedValue, actualValue);
}
}

Object expectedValue = srcResultSet.getObject(i + 1);
Object actualValue = dstResultSet.getObject(i + 1);

compareExpectedAndActual(destJDBCTypeInt, expectedValue, actualValue);
}
}
}
Expand Down Expand Up @@ -156,7 +159,7 @@ public static void compareExpectedAndActual(int dataType, Object expectedValue,

case java.sql.Types.BINARY:
case java.sql.Types.VARBINARY:
assertTrue(Utils.parseByte((byte[]) expectedValue, (byte[]) actualValue),
assertTrue(TestUtils.parseByte((byte[]) expectedValue, (byte[]) actualValue),
"Unexpected bianry/varbinary value ");
break;

Expand Down
Loading