Skip to content

Commit

Permalink
Reduce connections and save resources
Browse files Browse the repository at this point in the history
  • Loading branch information
cheenamalhotra committed Apr 3, 2019
1 parent b021297 commit e14177e
Show file tree
Hide file tree
Showing 54 changed files with 557 additions and 670 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public class AESetup extends AbstractTest {
public static void setUpConnection() throws TestAbortedException, Exception {
AETestConnectionString = connectionString + ";sendTimeAsDateTime=false";
readFromFile(Constants.JAVA_KEY_STORE_FILENAME, "Alias name");
try (SQLServerConnection con = (SQLServerConnection) PrepUtil.getConnection(AETestConnectionString);
SQLServerStatement stmt = (SQLServerStatement) con.createStatement()) {
try (Connection con = PrepUtil.getConnection(AETestConnectionString);
Statement stmt = con.createStatement()) {
dropCEK(stmt);
dropCMK(stmt);
}
Expand All @@ -101,7 +101,7 @@ public static void setUpConnection() throws TestAbortedException, Exception {
*/
@AfterAll
public static void dropAll() throws Exception {
try (Connection con = getConnection(); Statement stmt = con.createStatement()) {
try (Statement stmt = connection.createStatement()) {
dropTables(stmt);
dropCEK(stmt);
dropCMK(stmt);
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package com.microsoft.sqlserver.jdbc.AlwaysEncrypted;

import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.math.BigDecimal;
Expand Down Expand Up @@ -363,7 +364,7 @@ private void testGetDate(ResultSet rs, int numberOfColumns, String[] dates) thro
}

else {
throw new Exception(TestResource.getResource("R_resultsetNotInstance"));
fail(TestResource.getResource("R_resultsetNotInstance"));
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/microsoft/sqlserver/jdbc/JDBC43Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.sql.JDBCType;
import java.sql.SQLException;
import java.sql.ShardingKey;

import javax.sql.ConnectionPoolDataSource;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.logging.Logger;
import java.util.stream.Stream;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
Expand Down
1 change: 0 additions & 1 deletion src/test/java/com/microsoft/sqlserver/jdbc/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.sql.SQLException;
import java.util.UUID;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class BulkCopyAllTypesTest extends AbstractTest {
@Tag("xAzureSQLDW")
public void testTVPResultSet() throws SQLException {
if (isSqlAzureDW()) {
//TODO : Fix this test to run with Azure DW
// TODO : Fix this test to run with Azure DW
testBulkCopyResultSet(false, null, null);
testBulkCopyResultSet(false, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
} else {
Expand Down Expand Up @@ -88,8 +88,7 @@ private void setupVariation() throws SQLException {
}

private void terminateVariation() throws SQLException {
try (Connection conn = getConnection(); Statement stmt = conn.createStatement()) {

try (Statement stmt = connection.createStatement()) {
TestUtils.dropTableIfExists(tableSrc.getEscapedTableName(), stmt);
TestUtils.dropTableIfExists(tableDest.getEscapedTableName(), stmt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private static void createTables(Statement stmt) throws SQLException {
*/
@AfterAll
public static void terminate() throws SQLException {
try (Connection conn = getConnection(); Statement stmt = conn.createStatement()) {
try (Statement stmt = connection.createStatement()) {
dropTables(stmt);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.junit.Assert.assertEquals;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Expand All @@ -29,16 +28,17 @@
import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;
import com.microsoft.sqlserver.testframework.AbstractTest;


@RunWith(JUnitPlatform.class)
@Tag("xAzureSQLDW")
public class BulkCopyRowSetTest extends AbstractTest {

private static String tableName = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("BulkCopyFloatTest"));


private static String tableName = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("BulkCopyFloatTest"));

@Test
public void testBulkCopyFloatRowSet() throws SQLException {
try (Connection con = DriverManager.getConnection(connectionString);
Statement stmt = connection.createStatement()) {
try (Connection con = getConnection(); Statement stmt = connection.createStatement()) {
RowSetFactory rsf = RowSetProvider.newFactory();
CachedRowSet crs = rsf.createCachedRowSet();
RowSetMetaData rsmd = new RowSetMetaDataImpl();
Expand All @@ -47,41 +47,40 @@ public void testBulkCopyFloatRowSet() throws SQLException {
rsmd.setColumnName(2, "c2");
rsmd.setColumnType(1, java.sql.Types.FLOAT);
rsmd.setColumnType(2, java.sql.Types.FLOAT);

Float floatData = RandomData.generateReal(false);

crs.setMetaData(rsmd);
crs.moveToInsertRow();
crs.updateFloat(1, floatData);
crs.updateFloat(2, floatData);
crs.insertRow();
crs.moveToCurrentRow();

try (SQLServerBulkCopy bcOperation = new SQLServerBulkCopy(con)) {
bcOperation.setDestinationTableName(tableName);
bcOperation.writeToServer(crs);
}

ResultSet rs = stmt.executeQuery("select * from " + tableName);
rs.next();
assertEquals(floatData, (Float) rs.getFloat(1));
assertEquals(floatData, (Float) rs.getFloat(2));

try (ResultSet rs = stmt.executeQuery("select * from " + tableName)) {
rs.next();
assertEquals(floatData, (Float) rs.getFloat(1));
assertEquals(floatData, (Float) rs.getFloat(2));
}
}
}

@BeforeAll
public static void testSetup() throws TestAbortedException, Exception {
try (Connection connection = DriverManager.getConnection(connectionString);
Statement stmt = connection.createStatement()) {
try (Statement stmt = connection.createStatement()) {
String sql1 = "create table " + tableName + " (c1 float, c2 real)";
stmt.execute(sql1);
}
}

@AfterAll
public static void terminateVariation() throws SQLException {
try (Connection connection = DriverManager.getConnection(connectionString);
Statement stmt = connection.createStatement()) {
try (Statement stmt = connection.createStatement()) {
TestUtils.dropTableIfExists(tableName, stmt);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public void testSendValidValueforBinaryColumnAsString() throws Exception {
*/
@BeforeAll
public static void setupHere() throws SQLException, SecurityException, IOException {
try (Connection con = getConnection(); Statement stmt = con.createStatement()) {
try (Statement stmt = connection.createStatement()) {
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(destTable), stmt);
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(srcTable), stmt);
}
Expand All @@ -245,7 +245,7 @@ public static void setupHere() throws SQLException, SecurityException, IOExcepti
*/
@AfterEach
public void afterEachTests() throws SQLException {
try (Connection con = getConnection(); Statement stmt = con.createStatement()) {
try (Statement stmt = connection.createStatement()) {
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(destTable), stmt);
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(srcTable), stmt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ public void datatypestest() throws Exception {
}
}
} finally {
try (Connection conn = getConnection(); Statement stmt = conn.createStatement()) {
try (Statement stmt = connection.createStatement()) {
TestUtils.dropTableIfExists(escapedTableName, stmt);
} catch (SQLException e) {
fail(e.getMessage());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class CallableStatementTest extends AbstractTest {
@BeforeAll
public static void setupTest() throws SQLException {

try (Connection connection = getConnection(); Statement stmt = connection.createStatement()) {
try (Statement stmt = connection.createStatement()) {
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableNameGUID), stmt);
TestUtils.dropProcedureIfExists(AbstractSQLGenerator.escapeIdentifier(outputProcedureNameGUID), stmt);
TestUtils.dropProcedureIfExists(AbstractSQLGenerator.escapeIdentifier(setNullProcedureName), stmt);
Expand Down Expand Up @@ -174,7 +174,7 @@ public void inputParamsTest() throws SQLException {
*/
@AfterAll
public static void cleanup() throws SQLException {
try (Connection connection = getConnection(); Statement stmt = connection.createStatement()) {
try (Statement stmt = connection.createStatement()) {
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableNameGUID), stmt);
TestUtils.dropProcedureIfExists(AbstractSQLGenerator.escapeIdentifier(outputProcedureNameGUID), stmt);
TestUtils.dropProcedureIfExists(AbstractSQLGenerator.escapeIdentifier(setNullProcedureName), stmt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public void testNegativeTimeout() throws Exception {
try (Connection conn = getConnection()) {
try {
conn.isValid(-42);
throw new Exception(TestResource.getResource("R_noExceptionNegativeTimeout"));
fail(TestResource.getResource("R_noExceptionNegativeTimeout"));
} catch (SQLException e) {
MessageFormat form = new MessageFormat(TestResource.getResource("R_invalidQueryTimeout"));
Object[] msgArgs = {"-42"};
Expand Down Expand Up @@ -284,8 +284,7 @@ public void testDeadConnection() throws SQLException {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
} finally {
if (null != tableName) {
try (SQLServerConnection conn = (SQLServerConnection) PrepUtil
.getConnection(connectionString + ";responseBuffering=adaptive");
try (Connection conn = PrepUtil.getConnection(connectionString + ";responseBuffering=adaptive");
Statement stmt = conn.createStatement()) {
stmt.execute("drop table " + AbstractSQLGenerator.escapeIdentifier(tableName));
}
Expand All @@ -301,15 +300,15 @@ public void testClientConnectionId() throws Exception {
try {
// Call getClientConnectionId on a closed connection, should raise exception
conn.getClientConnectionId();
throw new Exception(TestResource.getResource("R_noExceptionClosedConnection"));
fail(TestResource.getResource("R_noExceptionClosedConnection"));
} catch (SQLException e) {
assertEquals(e.getMessage(), TestResource.getResource("R_connectionIsClosed"),
TestResource.getResource("R_wrongExceptionMessage"));
}
}

// Wrong database, ClientConnectionId should be available in error message
try (SQLServerConnection conn = (SQLServerConnection) PrepUtil.getConnection(connectionString + ";databaseName="
try (Connection conn = PrepUtil.getConnection(connectionString + ";databaseName="
+ RandomUtil.getIdentifierForDB("DataBase") + Constants.SEMI_COLON)) {
conn.close();

Expand All @@ -319,7 +318,7 @@ public void testClientConnectionId() throws Exception {
}

// Nonexist host, ClientConnectionId should not be available in error message
try (SQLServerConnection conn = (SQLServerConnection) PrepUtil.getConnection(
try (Connection conn = PrepUtil.getConnection(
connectionString + ";instanceName=" + RandomUtil.getIdentifier("Instance") + ";logintimeout=5;")) {
conn.close();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Arrays;
import java.util.Random;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private static void connect(DataSource ds) throws SQLException {
*/
@AfterAll
public static void afterAll() throws SQLException {
try (Connection con = getConnection(); Statement stmt = con.createStatement()) {
try (Statement stmt = connection.createStatement()) {
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tempTableName), stmt);
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand Down Expand Up @@ -136,7 +138,7 @@ public void testModifiableConnectionProperties() throws SQLException {
con.setCatalog("master");
}
} finally {
try (SQLServerConnection con = getConnection(); Statement stmt = con.createStatement()) {
try (Connection con = getConnection(); Statement stmt = con.createStatement()) {
TestUtils.dropDatabaseIfExists(sCatalog2, stmt);
}
}
Expand All @@ -149,7 +151,7 @@ public void testModifiableConnectionProperties() throws SQLException {
*/
@Test
public void testWarnings() throws SQLException {
try (SQLServerConnection con = getConnection()) {
try (Connection con = getConnection()) {
if (TestUtils.isJDBC43OrGreater(con)) {
con.beginRequest();
generateWarning(con);
Expand Down Expand Up @@ -178,7 +180,7 @@ public void testWarnings() throws SQLException {
*/
@Test
public void testOpenTransactions() throws SQLException {
try (SQLServerConnection con = getConnection(); Statement stmt = con.createStatement()) {
try (Connection con = getConnection(); Statement stmt = con.createStatement()) {
if (TestUtils.isJDBC43OrGreater(con)) {
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt);
stmt.executeUpdate("CREATE TABLE " + AbstractSQLGenerator.escapeIdentifier(tableName) + " (col int)");
Expand All @@ -199,7 +201,7 @@ public void testOpenTransactions() throws SQLException {
}
}
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}

Expand All @@ -210,7 +212,7 @@ public void testOpenTransactions() throws SQLException {
*/
@Test
public void testStatements() throws SQLException {
try (SQLServerConnection con = getConnection();) {
try (Connection con = getConnection();) {
if (TestUtils.isJDBC43OrGreater(con)) {
try (Statement stmt1 = con.createStatement()) {
con.beginRequest();
Expand Down Expand Up @@ -278,7 +280,7 @@ public void testStatements() throws SQLException {
@Test
public void testThreads() throws SQLException {
class Variables {
volatile SQLServerConnection con = null;
volatile Connection con = null;
volatile Statement stmt = null;
volatile PreparedStatement pstmt = null;
}
Expand Down Expand Up @@ -349,8 +351,8 @@ public void run() {
assertTrue(sharedVariables.pstmt.isClosed());
}
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
fail(e.getMessage());
} finally {
if (null != sharedVariables.stmt) {
sharedVariables.stmt.close();
Expand Down Expand Up @@ -427,7 +429,7 @@ private void compareValuesAgainstConnection(SQLServerConnection con, boolean aut
"useBulkCopyForBatchInsert" + description);
}

private void generateWarning(SQLServerConnection con) throws SQLException {
private void generateWarning(Connection con) throws SQLException {
con.setClientInfo("name", "value");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.sql.DatabaseMetaData;
import java.text.MessageFormat;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
Expand Down
Loading

0 comments on commit e14177e

Please sign in to comment.