Skip to content

Commit ec052ca

Browse files
authored
Tests | Added more datatype tests to increase code coverage (#878)
* ported tests from VSO and added tests to increase code coverage * moved resultset tests to resultset * removed unused import * added tests to increase code coverage * fixed issue with timezone * fixed issue with timezone * modified testWithThaiLocale to use current time * fixed issue with timezone * fixed issue with timezone * fixed issue with timezone * added SparseTest and BigIntegerTest and cleaned up with try-with-resources * review changes * review comments * more review updates * review updates * removed dummy file * review udpates * more cleanup * updated isSqlAzure * review updates * more review updates * fixed imports * updated failed error strings with more detail * added locale to time format * format date for comparision * 1 more timezone fix * cleanup leaded stream resources * manually merged with #903 changes for now * more cleanup on assertEqual checks * more cleanup on assertEqual checks * cosmetic changes from review * updated all assertEquals params to proper expected, actual order * fixed comments and leaked resultsets
2 parents 3f9635a + db38827 commit ec052ca

File tree

10 files changed

+2413
-55
lines changed

10 files changed

+2413
-55
lines changed

src/test/java/com/microsoft/sqlserver/jdbc/TestResource.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ protected Object[][] getContents() {
157157
{"R_expectedValue", "Expected value: "}, {"R_expectedValueAtIndex", "Expected value at index: "},
158158
{"R_switchFailed", "Switch case is not matched with data"},
159159
{"R_resultsetNotInstance", "Result set is not instance of SQLServerResultSet"},
160+
{"R_noJRESupport", "No JRE support for {0}"},
160161
{"R_incorrectColumnNum", "Column name or number of supplied values does not match table definition."},
161162
{"R_incorrectColumnNumInsert",
162163
"There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement."},

src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyColumnMappingTest.java

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.sql.ResultSet;
1111
import java.sql.ResultSetMetaData;
1212
import java.sql.SQLException;
13+
import java.sql.Statement;
1314
import java.text.MessageFormat;
1415
import java.util.concurrent.ThreadLocalRandom;
1516

@@ -22,6 +23,7 @@
2223

2324
import com.microsoft.sqlserver.jdbc.ComparisonUtil;
2425
import com.microsoft.sqlserver.jdbc.TestResource;
26+
import com.microsoft.sqlserver.jdbc.TestUtils;
2527
import com.microsoft.sqlserver.testframework.DBConnection;
2628
import com.microsoft.sqlserver.testframework.DBResultSet;
2729
import com.microsoft.sqlserver.testframework.DBStatement;
@@ -56,7 +58,7 @@ public static void closeConnection() throws SQLException {
5658

5759
@Test
5860
@DisplayName("BulkCopy:test no explicit column mapping")
59-
public void testNoExplicitCM() {
61+
public void testNoExplicitCM() throws SQLException {
6062
DBTable destTable = null;
6163
try {
6264
// create dest table
@@ -68,16 +70,13 @@ public void testNoExplicitCM() {
6870
bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false);
6971
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable);
7072
} finally {
71-
if (null != destTable) {
72-
// drop dest table
73-
stmt.dropTable(destTable);
74-
}
73+
TestUtils.dropTableIfExists(destTable.getEscapedTableName(), (Statement) stmt.product());
7574
}
7675
}
7776

7877
@Test
7978
@DisplayName("BulkCopy:test explicit column mapping")
80-
public void testExplicitCM() {
79+
public void testExplicitCM() throws SQLException {
8180
DBTable destTable = null;
8281
try {
8382
// create dest table
@@ -110,16 +109,13 @@ public void testExplicitCM() {
110109
}
111110
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable);
112111
} finally {
113-
// drop dest table
114-
if (null != destTable) {
115-
stmt.dropTable(destTable);
116-
}
112+
TestUtils.dropTableIfExists(destTable.getEscapedTableName(), (Statement) stmt.product());
117113
}
118114
}
119115

120116
@Test
121117
@DisplayName("BulkCopy:test unicode column mapping")
122-
public void testUnicodeCM() {
118+
public void testUnicodeCM() throws SQLException {
123119
DBTable sourceTableUnicode = null;
124120
DBTable destTableUnicode = null;
125121
try {
@@ -158,18 +154,14 @@ public void testUnicodeCM() {
158154
}
159155
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTableUnicode, destTableUnicode);
160156
} finally {
161-
if (null != sourceTableUnicode) {
162-
dropTable(sourceTableUnicode.getEscapedTableName());
163-
}
164-
if (null != destTableUnicode) {
165-
dropTable(destTableUnicode.getEscapedTableName());
166-
}
157+
TestUtils.dropTableIfExists(sourceTableUnicode.getEscapedTableName(), (Statement) stmt.product());
158+
TestUtils.dropTableIfExists(destTableUnicode.getEscapedTableName(), (Statement) stmt.product());
167159
}
168160
}
169161

170162
@Test
171163
@DisplayName("BulkCopy:test repetitive column mapping")
172-
public void testRepetitiveCM() {
164+
public void testRepetitiveCM() throws SQLException {
173165
DBTable sourceTable1 = null;
174166
DBTable destTable = null;
175167
try {
@@ -225,18 +217,14 @@ public void testRepetitiveCM() {
225217
fail(form.format(msgArgs) + "\n" + destTable.getTableName() + "\n" + e.getMessage());
226218
}
227219
} finally {
228-
if (null != sourceTable1) {
229-
dropTable(sourceTable1.getEscapedTableName());
230-
}
231-
if (null != destTable) {
232-
dropTable(destTable.getEscapedTableName());
233-
}
220+
TestUtils.dropTableIfExists(sourceTable1.getEscapedTableName(), (Statement) stmt.product());
221+
TestUtils.dropTableIfExists(destTable.getEscapedTableName(), (Statement) stmt.product());
234222
}
235223
}
236224

237225
@Test
238226
@DisplayName("BulkCopy:test implicit mismatched column mapping")
239-
public void testImplicitMismatchCM() {
227+
public void testImplicitMismatchCM() throws SQLException {
240228
DBTable destTable = null;
241229
try {
242230
// create non unicode dest table with different schema from source table
@@ -269,15 +257,13 @@ public void testImplicitMismatchCM() {
269257
}
270258
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
271259
} finally {
272-
if (null != destTable) {
273-
stmt.dropTable(destTable);
274-
}
260+
TestUtils.dropTableIfExists(destTable.getEscapedTableName(), (Statement) stmt.product());
275261
}
276262
}
277263

278264
@Test
279265
@DisplayName("BulkCopy:test invalid column mapping")
280-
public void testInvalidCM() {
266+
public void testInvalidCM() throws SQLException {
281267
DBTable destTable = null;
282268
try {
283269
// create dest table
@@ -360,9 +346,7 @@ public void testInvalidCM() {
360346
bulkWrapper.setColumnMapping(Integer.MIN_VALUE, Integer.MAX_VALUE);
361347
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
362348
} finally {
363-
if (null != destTable) {
364-
stmt.dropTable(destTable);
365-
}
349+
TestUtils.dropTableIfExists(destTable.getEscapedTableName(), (Statement) stmt.product());
366350
}
367351
}
368352

@@ -411,15 +395,4 @@ private void validateValuesRepetitiveCM(DBConnection con, DBTable sourceTable,
411395
assertTrue(destinationTable.getTotalRows() == numRows);
412396
}
413397
}
414-
415-
private void dropTable(String tableName) {
416-
417-
String dropSQL = "DROP TABLE [dbo]." + tableName;
418-
try {
419-
stmt.execute(dropSQL);
420-
} catch (SQLException e) {
421-
fail(tableName + " " + TestResource.getResource("R_tableNotDropped") + "\n" + e.getMessage());
422-
}
423-
}
424-
425398
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.microsoft.sqlserver.jdbc.callablestatement;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.fail;
5+
6+
import java.sql.CallableStatement;
7+
import java.sql.Connection;
8+
import java.sql.DriverManager;
9+
import java.sql.ResultSet;
10+
import java.sql.SQLException;
11+
import java.sql.Statement;
12+
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.platform.runner.JUnitPlatform;
15+
import org.junit.runner.RunWith;
16+
17+
import com.microsoft.sqlserver.jdbc.RandomUtil;
18+
import com.microsoft.sqlserver.jdbc.TestUtils;
19+
import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;
20+
import com.microsoft.sqlserver.testframework.AbstractTest;
21+
22+
23+
@RunWith(JUnitPlatform.class)
24+
public class CallableMixedTest extends AbstractTest {
25+
26+
@Test
27+
public void datatypestest() throws Exception {
28+
String tableName = RandomUtil.getIdentifier("TFOO3");
29+
String escapedTableName = AbstractSQLGenerator.escapeIdentifier(tableName);
30+
String procName = RandomUtil.getIdentifier("SPFOO3");
31+
String escapedProcName = AbstractSQLGenerator.escapeIdentifier(procName);
32+
33+
try (Connection conn = DriverManager.getConnection(connectionString)) {
34+
try (Statement stmt = conn.createStatement()) {
35+
TestUtils.dropTableIfExists(escapedTableName, stmt);
36+
37+
String createSQL = "create table " + escapedTableName + "(c1_int int primary key, col2 int)";
38+
stmt.executeUpdate(createSQL);
39+
40+
stmt.executeUpdate("Insert into " + escapedTableName + " values(0, 1)");
41+
42+
stmt.executeUpdate("CREATE PROCEDURE " + escapedProcName
43+
+ " (@p2_int int, @p2_int_out int OUTPUT, @p4_smallint smallint, @p4_smallint_out smallint OUTPUT) AS begin transaction SELECT * FROM "
44+
+ escapedTableName
45+
+ " ; SELECT @p2_int_out=@p2_int, @p4_smallint_out=@p4_smallint commit transaction RETURN -2147483648");
46+
}
47+
48+
try (CallableStatement cstmt = conn.prepareCall("{ ? = CALL " + escapedProcName + " (?, ?, ?, ?) }")) {
49+
cstmt.registerOutParameter((int) 1, (int) 4);
50+
cstmt.setObject((int) 2, Integer.valueOf("31"), (int) 4);
51+
cstmt.registerOutParameter((int) 3, (int) 4);
52+
53+
// Test OUT param re-registration
54+
cstmt.registerOutParameter((int) 5, java.sql.Types.BINARY);
55+
cstmt.registerOutParameter((int) 5, (int) 5);
56+
cstmt.setObject((int) 4, Short.valueOf("-5372"), (int) 5);
57+
58+
// get results and a value
59+
try (ResultSet rs = cstmt.executeQuery()) {
60+
rs.next();
61+
assertEquals(0, rs.getInt(1));
62+
assertEquals(-5372, cstmt.getInt((int) 5));
63+
}
64+
65+
// get the param without getting the resultset
66+
try (ResultSet rs = cstmt.executeQuery()) {
67+
assertEquals(-2147483648, cstmt.getInt((int) 1));
68+
}
69+
70+
try (ResultSet rs = cstmt.executeQuery()) {
71+
rs.next();
72+
assertEquals(0, rs.getInt(1));
73+
assertEquals(-2147483648, cstmt.getInt((int) 1));
74+
assertEquals(-5372, cstmt.getInt((int) 5));
75+
}
76+
}
77+
} finally {
78+
try (Connection conn = DriverManager.getConnection(connectionString);
79+
Statement stmt = conn.createStatement()) {
80+
TestUtils.dropTableIfExists(escapedTableName, stmt);
81+
} catch (SQLException e) {
82+
fail(e.toString());
83+
}
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)