forked from microsoft/mssql-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CallableStatementTest.java
258 lines (214 loc) · 10.7 KB
/
CallableStatementTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package com.microsoft.sqlserver.jdbc.callablestatement;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import java.sql.*;
import java.text.MessageFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.TimeZone;
import java.util.UUID;
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;
import com.microsoft.sqlserver.jdbc.RandomUtil;
import com.microsoft.sqlserver.jdbc.SQLServerCallableStatement;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.microsoft.sqlserver.jdbc.TestResource;
import com.microsoft.sqlserver.jdbc.TestUtils;
import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;
import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.Constants;
/**
* Test CallableStatement
*/
@RunWith(JUnitPlatform.class)
@Tag(Constants.xAzureSQLDW)
public class CallableStatementTest extends AbstractTest {
private static String tableNameGUID = RandomUtil.getIdentifier("uniqueidentifier_Table");
private static String outputProcedureNameGUID = RandomUtil.getIdentifier("uniqueidentifier_SP");
private static String setNullProcedureName = RandomUtil.getIdentifier("CallableStatementTest_setNull_SP");
private static String inputParamsProcedureName = RandomUtil.getIdentifier("CallableStatementTest_inputParams_SP");
private static String getObjectLocalDateTimeProcedureName = RandomUtil.getIdentifier("CallableStatementTest_getObjectLocalDateTime_SP");
/**
* Setup before test
*
* @throws SQLException
*/
@BeforeAll
public static void setupTest() throws SQLException {
try (Statement stmt = connection.createStatement()) {
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableNameGUID), stmt);
TestUtils.dropProcedureIfExists(AbstractSQLGenerator.escapeIdentifier(outputProcedureNameGUID), stmt);
TestUtils.dropProcedureIfExists(AbstractSQLGenerator.escapeIdentifier(setNullProcedureName), stmt);
TestUtils.dropProcedureIfExists(AbstractSQLGenerator.escapeIdentifier(inputParamsProcedureName), stmt);
TestUtils.dropProcedureIfExists(AbstractSQLGenerator.escapeIdentifier(getObjectLocalDateTimeProcedureName), stmt);
createGUIDTable(stmt);
createGUIDStoredProcedure(stmt);
createSetNullProcedure(stmt);
createInputParamsProcedure(stmt);
createGetObjectLocalDateTimeProcedure(stmt);
}
}
/**
* Tests CallableStatement.getString() with uniqueidentifier parameter
*
* @throws SQLException
*/
@Test
public void getStringGUIDTest() throws SQLException {
String sql = "{call " + AbstractSQLGenerator.escapeIdentifier(outputProcedureNameGUID) + "(?)}";
try (SQLServerCallableStatement callableStatement = (SQLServerCallableStatement) connection.prepareCall(sql)) {
UUID originalValue = UUID.randomUUID();
callableStatement.registerOutParameter(1, microsoft.sql.Types.GUID);
callableStatement.setObject(1, originalValue.toString(), microsoft.sql.Types.GUID);
callableStatement.execute();
String retrievedValue = callableStatement.getString(1);
assertEquals(originalValue.toString().toLowerCase(), retrievedValue.toLowerCase());
}
}
/**
* test for setNull(index, varchar) to behave as setNull(index, nvarchar) when SendStringParametersAsUnicode is true
*
* @throws SQLException
*/
@Test
public void getSetNullWithTypeVarchar() throws SQLException {
String polishchar = "\u0143";
SQLServerDataSource ds = new SQLServerDataSource();
ds.setURL(connectionString);
ds.setSendStringParametersAsUnicode(true);
String sql = "{? = call " + AbstractSQLGenerator.escapeIdentifier(setNullProcedureName) + " (?,?)}";
try (Connection connection = ds.getConnection();
SQLServerCallableStatement cs = (SQLServerCallableStatement) connection.prepareCall(sql);
SQLServerCallableStatement cs2 = (SQLServerCallableStatement) connection.prepareCall(sql)) {
cs.registerOutParameter(1, Types.INTEGER);
cs.setString(2, polishchar);
cs.setString(3, null);
cs.registerOutParameter(3, Types.VARCHAR);
cs.execute();
String expected = cs.getString(3);
cs2.registerOutParameter(1, Types.INTEGER);
cs2.setString(2, polishchar);
cs2.setNull(3, Types.VARCHAR);
cs2.registerOutParameter(3, Types.NVARCHAR);
cs2.execute();
String actual = cs2.getString(3);
assertEquals(expected, actual);
}
}
/**
* Tests getObject(n, java.time.LocalDateTime.class).
*
* @throws SQLException
*/
@Test
public void getObjectAsLocalDateTime() throws SQLException {
String sql = "{CALL " + AbstractSQLGenerator.escapeIdentifier(getObjectLocalDateTimeProcedureName) + " (?)}";
try (Connection con = DriverManager.getConnection(connectionString); CallableStatement cs = con.prepareCall(sql)) {
cs.registerOutParameter(1, Types.TIMESTAMP);
TimeZone prevTimeZone = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("America/Edmonton"));
// a local date/time that does not actually exist because of Daylight Saving Time
final String testValueDate = "2018-03-11";
final String testValueTime = "02:00:00.1234567";
final String testValueDateTime = testValueDate + "T" + testValueTime;
try {
cs.execute();
LocalDateTime expectedLocalDateTime = LocalDateTime.parse(testValueDateTime);
LocalDateTime actualLocalDateTime = cs.getObject(1, LocalDateTime.class);
assertEquals(expectedLocalDateTime, actualLocalDateTime);
LocalDate expectedLocalDate = LocalDate.parse(testValueDate);
LocalDate actualLocalDate = cs.getObject(1, LocalDate.class);
assertEquals(expectedLocalDate, actualLocalDate);
LocalTime expectedLocalTime = LocalTime.parse(testValueTime);
LocalTime actualLocalTime = cs.getObject(1, LocalTime.class);
assertEquals(expectedLocalTime, actualLocalTime);
} finally {
TimeZone.setDefault(prevTimeZone);
}
}
}
/**
* recognize parameter names with and without leading '@'
*
* @throws SQLException
*/
@Test
public void inputParamsTest() throws SQLException {
String call = "{CALL " + AbstractSQLGenerator.escapeIdentifier(inputParamsProcedureName) + " (?,?)}";
// the historical way: no leading '@', parameter names respected (not positional)
try (CallableStatement cs = connection.prepareCall(call)) {
cs.setString("p2", "world");
cs.setString("p1", "hello");
try (ResultSet rs = cs.executeQuery()) {
rs.next();
assertEquals("helloworld", rs.getString(1));
}
}
// the "new" way: leading '@', parameter names still respected (not positional)
try (CallableStatement cs = connection.prepareCall(call)) {
cs.setString("@p2", "world!");
cs.setString("@p1", "Hello ");
try (ResultSet rs = cs.executeQuery()) {
rs.next();
assertEquals("Hello world!", rs.getString(1));
}
}
// sanity check: unrecognized parameter name
try (CallableStatement cs = connection.prepareCall(call)) {
cs.setString("@whatever", "test");
fail(TestResource.getResource("R_shouldThrowException"));
} catch (SQLException sse) {
MessageFormat form = new MessageFormat(TestResource.getResource("R_parameterNotDefined"));
Object[] msgArgs = {"@whatever"};
if (!sse.getMessage().startsWith(form.format(msgArgs))) {
fail(TestResource.getResource("R_unexpectedExceptionContent"));
}
}
}
/**
* Cleanup after test
*
* @throws SQLException
*/
@AfterAll
public static void cleanup() throws SQLException {
try (Statement stmt = connection.createStatement()) {
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableNameGUID), stmt);
TestUtils.dropProcedureIfExists(AbstractSQLGenerator.escapeIdentifier(outputProcedureNameGUID), stmt);
TestUtils.dropProcedureIfExists(AbstractSQLGenerator.escapeIdentifier(setNullProcedureName), stmt);
TestUtils.dropProcedureIfExists(AbstractSQLGenerator.escapeIdentifier(inputParamsProcedureName), stmt);
}
}
private static void createGUIDStoredProcedure(Statement stmt) throws SQLException {
String sql = "CREATE PROCEDURE " + AbstractSQLGenerator.escapeIdentifier(outputProcedureNameGUID)
+ "(@p1 uniqueidentifier OUTPUT) AS SELECT @p1 = c1 FROM "
+ AbstractSQLGenerator.escapeIdentifier(tableNameGUID) + Constants.SEMI_COLON;
stmt.execute(sql);
}
private static void createGUIDTable(Statement stmt) throws SQLException {
String sql = "CREATE TABLE " + AbstractSQLGenerator.escapeIdentifier(tableNameGUID)
+ " (c1 uniqueidentifier null)";
stmt.execute(sql);
}
private static void createSetNullProcedure(Statement stmt) throws SQLException {
stmt.execute("create procedure " + AbstractSQLGenerator.escapeIdentifier(setNullProcedureName)
+ " (@p1 nvarchar(255), @p2 nvarchar(255) output) as select @p2=@p1 return 0");
}
private static void createInputParamsProcedure(Statement stmt) throws SQLException {
String sql = "CREATE PROCEDURE " + AbstractSQLGenerator.escapeIdentifier(inputParamsProcedureName)
+ " @p1 nvarchar(max) = N'parameter1', " + " @p2 nvarchar(max) = N'parameter2' " + "AS "
+ "BEGIN " + " SET NOCOUNT ON; " + " SELECT @p1 + @p2 AS result; " + "END ";
stmt.execute(sql);
}
private static void createGetObjectLocalDateTimeProcedure(Statement stmt) throws SQLException {
String sql = "CREATE PROCEDURE " + AbstractSQLGenerator.escapeIdentifier(getObjectLocalDateTimeProcedureName)
+ "(@p1 datetime2(7) OUTPUT) AS "
+ "SELECT @p1 = '2018-03-11T02:00:00.1234567'";
stmt.execute(sql);
}
}