-
Notifications
You must be signed in to change notification settings - Fork 420
/
TestUtils.java
958 lines (877 loc) · 31 KB
/
TestUtils.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
/*
* Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made
* available under the terms of the MIT License. See the LICENSE file in the project root for more information.
*/
package com.microsoft.sqlserver.jdbc;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URI;
import java.security.KeyStore;
import java.security.cert.CertificateFactory;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;
import com.microsoft.sqlserver.testframework.PrepUtil;
import com.microsoft.sqlserver.testframework.sqlType.SqlBigInt;
import com.microsoft.sqlserver.testframework.sqlType.SqlBinary;
import com.microsoft.sqlserver.testframework.sqlType.SqlBit;
import com.microsoft.sqlserver.testframework.sqlType.SqlChar;
import com.microsoft.sqlserver.testframework.sqlType.SqlDate;
import com.microsoft.sqlserver.testframework.sqlType.SqlDateTime;
import com.microsoft.sqlserver.testframework.sqlType.SqlDateTime2;
import com.microsoft.sqlserver.testframework.sqlType.SqlDateTimeOffset;
import com.microsoft.sqlserver.testframework.sqlType.SqlDecimal;
import com.microsoft.sqlserver.testframework.sqlType.SqlFloat;
import com.microsoft.sqlserver.testframework.sqlType.SqlInt;
import com.microsoft.sqlserver.testframework.sqlType.SqlMoney;
import com.microsoft.sqlserver.testframework.sqlType.SqlNChar;
import com.microsoft.sqlserver.testframework.sqlType.SqlNVarChar;
import com.microsoft.sqlserver.testframework.sqlType.SqlNVarCharMax;
import com.microsoft.sqlserver.testframework.sqlType.SqlNumeric;
import com.microsoft.sqlserver.testframework.sqlType.SqlReal;
import com.microsoft.sqlserver.testframework.sqlType.SqlSmallDateTime;
import com.microsoft.sqlserver.testframework.sqlType.SqlSmallInt;
import com.microsoft.sqlserver.testframework.sqlType.SqlSmallMoney;
import com.microsoft.sqlserver.testframework.sqlType.SqlTime;
import com.microsoft.sqlserver.testframework.sqlType.SqlTinyInt;
import com.microsoft.sqlserver.testframework.sqlType.SqlType;
import com.microsoft.sqlserver.testframework.sqlType.SqlVarBinary;
import com.microsoft.sqlserver.testframework.sqlType.SqlVarBinaryMax;
import com.microsoft.sqlserver.testframework.sqlType.SqlVarChar;
import com.microsoft.sqlserver.testframework.sqlType.SqlVarCharMax;
/**
* Generic Utility class which we can access by test classes.
*
* @since 6.1.2
*/
public final class TestUtils {
private static ArrayList<SqlType> types = null;
private static final char[] HEXCHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
'F'};
static final int ENGINE_EDITION_FOR_SQL_AZURE = 5;
static final int ENGINE_EDITION_FOR_SQL_AZURE_DW = 6;
static final int ENGINE_EDITION_FOR_SQL_AZURE_MI = 8;
private TestUtils() {}
/**
* Checks if the connection session recovery object has negotiated reflection.
*
* @param con
* @return
*/
public static boolean isConnectionRecoveryNegotiated(Connection con) {
return ((SQLServerConnection) con).getSessionRecovery().isConnectionRecoveryNegotiated();
}
/**
* Checks if connection is dead.
*
* @param con
* @return
* @throws SQLServerException
*/
public static boolean isConnectionDead(Connection con) throws SQLServerException {
return ((SQLServerConnection) con).isConnectionDead();
}
/**
* Checks if connection is established to Azure server.
*
* @see com.microsoft.sqlserver.jdbc.SQLServerConnection#isAzure()
*/
public static boolean isAzure(Connection con) {
return ((SQLServerConnection) con).isAzure();
}
/**
* Checks if connection is established to Azure DW server.
*
* @see com.microsoft.sqlserver.jdbc.SQLServerConnection#isAzureDW()
*/
public static boolean isAzureDW(Connection con) {
isAzure(con);
return ((SQLServerConnection) con).isAzureDW();
}
/**
* Checks if connection is established to Azure MI server.
*
* @see com.microsoft.sqlserver.jdbc.SQLServerConnection#isAzureMI()
*/
public static boolean isAzureMI(Connection con) {
isAzure(con);
return ((SQLServerConnection) con).isAzureMI();
}
/**
* Checks if connection is established to server that supports AEv2.
*
* @see com.microsoft.sqlserver.jdbc.SQLServerConnection#isAEv2()
*/
public static boolean isAEv2(Connection con) {
return ((SQLServerConnection) con).isAEv2();
}
/**
*
* @param javatype
* @return
*/
public static SqlType find(Class<?> javatype) {
if (null != types) {
types();
for (SqlType type : types) {
if (type.getType() == javatype)
return type;
}
}
return null;
}
/**
*
* @param name
* @return
*/
public static SqlType find(String name) {
if (null == types)
types();
if (null != types) {
for (SqlType type : types) {
if (type.getName().equalsIgnoreCase(name))
return type;
}
}
return null;
}
/**
*
* @return
*/
public static ArrayList<SqlType> types() {
if (null == types) {
types = new ArrayList<>();
types.add(new SqlInt());
types.add(new SqlSmallInt());
types.add(new SqlTinyInt());
types.add(new SqlBit());
types.add(new SqlDateTime());
types.add(new SqlSmallDateTime());
types.add(new SqlDecimal());
types.add(new SqlNumeric());
types.add(new SqlReal());
types.add(new SqlFloat());
types.add(new SqlMoney());
types.add(new SqlSmallMoney());
types.add(new SqlVarChar());
types.add(new SqlChar());
// types.add(new SqlText());
types.add(new SqlBinary());
types.add(new SqlVarBinary());
// types.add(new SqlImage());
// types.add(new SqlTimestamp());
types.add(new SqlNVarChar());
types.add(new SqlNChar());
// types.add(new SqlNText());
// types.add(new SqlGuid());
types.add(new SqlBigInt());
// types.add(new SqlVariant(this));
// 9.0 types
types.add(new SqlVarCharMax());
types.add(new SqlNVarCharMax());
types.add(new SqlVarBinaryMax());
// types.add(new SqlXml());
// 10.0 types
types.add(new SqlDate());
types.add(new SqlDateTime2());
types.add(new SqlTime());
types.add(new SqlDateTimeOffset());
}
return types;
}
/**
* Wrapper Class for BinaryStream
*
*/
public static class DBBinaryStream extends ByteArrayInputStream {
byte[] data;
// Constructor
public DBBinaryStream(byte[] value) {
super(value);
data = value;
}
}
/**
* Wrapper for CharacterStream
*
*/
public static class DBCharacterStream extends CharArrayReader {
String localValue;
/**
* Constructor
*
* @param value
*/
public DBCharacterStream(String value) {
super(value.toCharArray());
localValue = value;
}
}
/**
* Wrapper for NCharacterStream
*/
class DBNCharacterStream extends DBCharacterStream {
// Constructor
public DBNCharacterStream(String value) {
super(value);
}
}
/**
*
* @return location of resource file
*/
public static String getCurrentClassPath() {
try {
String className = new Object() {}.getClass().getEnclosingClass().getName();
String location = Class.forName(className).getProtectionDomain().getCodeSource().getLocation().getPath();
URI uri = new URI(location + "/");
return uri.getPath();
} catch (Exception e) {
fail("Failed to get CSV file path. " + e.getMessage());
}
return null;
}
/**
* mimic "DROP TABLE ..."
*
* @param tableName
* @param stmt
* @throws SQLException
*/
public static void dropTableIfExists(String tableName, java.sql.Statement stmt) throws SQLException {
dropObjectIfExists(tableName, "U", stmt);
}
/**
* Deletes the contents of a table.
*
* @param con
* @param tableName
* @throws SQLException
*/
public static void clearTable(Connection con, String tableName) throws SQLException {
try (Statement stmt = con.createStatement()) {
stmt.executeUpdate("DELETE FROM " + tableName);
}
}
/**
* mimic "DROP View ..."
*
* @param tableName
* @param stmt
* @throws SQLException
*/
public static void dropViewIfExists(String tableName, java.sql.Statement stmt) throws SQLException {
dropObjectIfExists(tableName, "V", stmt);
}
/**
* mimic "DROP PROCEDURE ..."
*
* @param procName
* @param stmt
* @throws SQLException
*/
public static void dropProcedureIfExists(String procName, java.sql.Statement stmt) throws SQLException {
dropObjectIfExists(procName, "P", stmt);
}
/**
* mimic "DROP FUNCTION ..."
*
* @param functionName
* @param stmt
* @throws SQLException
*/
public static void dropFunctionIfExists(String functionName, java.sql.Statement stmt) throws SQLException {
dropObjectIfExists(functionName, "FN", stmt);
}
/**
* mimic "DROP TRIGGER ..."
*
* @param triggerName
* @param stmt
* @throws SQLException
*/
public static void dropTriggerIfExists(String triggerName, java.sql.Statement stmt) throws SQLException {
dropObjectIfExists(triggerName, "TR", stmt);
}
/**
* mimic "DROP TYPE ..."
*
* @param typeName
* @param stmt
* @throws SQLException
*/
public static void dropTypeIfExists(String typeName, java.sql.Statement stmt) throws SQLException {
dropObjectIfExists(typeName, "TT", stmt);
}
/**
* Drops user defined types
*
* @param typeName
* @param stmt
* @throws SQLException
*/
public static void dropUserDefinedTypeIfExists(String typeName, Statement stmt) throws SQLException {
stmt.executeUpdate("IF EXISTS (select * from sys.types where name = '" + escapeSingleQuotes(typeName)
+ "') DROP TYPE " + typeName);
}
/**
* mimic "DROP DATABASE ..."
*
* @param databaseName
* @param connectionString
* @throws SQLException
*/
public static void dropDatabaseIfExists(String databaseName, String connectionString) throws SQLException {
try (Connection connection = PrepUtil.getConnection(connectionString + ";databaseName=master");
Statement stmt = connection.createStatement()) {
stmt.executeUpdate("IF EXISTS(SELECT * from sys.databases WHERE name='" + escapeSingleQuotes(databaseName)
+ "') DROP DATABASE [" + databaseName + "]");
}
}
/**
* mimic "DROP SCHEMA ..."
*
* @param schemaName
* @param stmt
* @throws SQLException
*/
public static void dropSchemaIfExists(String schemaName, Statement stmt) throws SQLException {
stmt.execute("if EXISTS (SELECT * FROM sys.schemas where name = '" + escapeSingleQuotes(schemaName)
+ "') drop schema " + AbstractSQLGenerator.escapeIdentifier(schemaName));
}
/**
* <pre>
* This method drops objects for below types:
*
* TT - TYPE_TABLE
* TR - TRIGGER
* FN - SQL_SCALAR_FUNCTION
* P -- SQL_STORED_PROCEDURE
* U -- USER_TABLE
*
* </pre>
*/
private static void dropObjectIfExists(String objectName, String objectType,
java.sql.Statement stmt) throws SQLException {
String typeName = "";
switch (objectType) {
case "TT":
typeName = "TYPE";
break;
case "TR":
typeName = "TRIGGER";
break;
case "FN":
typeName = "FUNCTION";
break;
case "P":
typeName = "PROCEDURE";
break;
case "U":
typeName = "TABLE";
break;
case "V":
typeName = "VIEW";
break;
default:
break;
}
StringBuilder sb = new StringBuilder();
if (!objectName.startsWith("[")) {
sb.append("[");
}
sb.append(objectName);
if (!objectName.endsWith("]")) {
sb.append("]");
}
String bracketedObjectName = sb.toString();
String whereClause = "";
if (objectType != "TT") {
whereClause = "WHERE object_id = OBJECT_ID(N'" + escapeSingleQuotes(bracketedObjectName) + "')";
} else {
whereClause = "WHERE name LIKE '%" + escapeSingleQuotes(objectName) + "%'";
}
String sql = "IF EXISTS ( SELECT * from sys.objects " + whereClause + " AND type='" + objectType + "') DROP "
+ typeName + " " + bracketedObjectName;
try {
stmt.executeUpdate(sql);
} catch (SQLException e) {
fail(TestResource.getResource("R_createDropTableFailed") + TestResource.getResource("R_errorMessage")
+ e.getMessage());
}
}
public static boolean parseByte(byte[] expectedData, byte[] retrieved) {
assertTrue(Arrays.equals(expectedData, Arrays.copyOf(retrieved, expectedData.length)),
" unexpected BINARY value, expected");
for (int i = expectedData.length; i < retrieved.length; i++) {
assertTrue(0 == retrieved[i], "unexpected data BINARY");
}
return true;
}
public static boolean isJDBC43OrGreater(Connection connection) throws SQLException {
return getJDBCVersion(connection) >= 4.3F;
}
public static float getJDBCVersion(Connection connection) throws SQLException {
return Float.valueOf(
connection.getMetaData().getJDBCMajorVersion() + "." + connection.getMetaData().getJDBCMinorVersion());
}
public static boolean serverSupportsUTF8(Connection connection) throws SQLException {
try (Statement stmt = connection.createStatement(); ResultSet rs = stmt
.executeQuery("SELECT name FROM sys.fn_helpcollations() WHERE name LIKE '%UTF8%'");) {
return rs.isBeforeFirst();
}
}
/**
*
* @param b
* byte value
* @param length
* length of the array
* @return
*/
public static String bytesToHexString(byte[] b, int length) {
StringBuilder sb = new StringBuilder(length * 2);
for (int i = 0; i < length; i++) {
int hexVal = b[i] & 0xFF;
sb.append(HEXCHARS[(hexVal & 0xF0) >> 4]);
sb.append(HEXCHARS[(hexVal & 0x0F)]);
}
return sb.toString();
}
/**
*
* @param b
* byte value
* @return
*/
public static String byteToHexDisplayString(byte[] b) {
if (null == b)
return "(null)";
int hexVal;
StringBuilder sb = new StringBuilder(b.length * 2 + 2);
sb.append("0x");
for (byte aB : b) {
hexVal = aB & 0xFF;
sb.append(HEXCHARS[(hexVal & 0xF0) >> 4]);
sb.append(HEXCHARS[(hexVal & 0x0F)]);
}
return sb.toString();
}
/**
* conversion routine valid values 0-9 a-f A-F throws exception when failed to convert
*
* @param value
* charArray
* @return
* @throws SQLException
*/
static byte CharToHex(char value) throws SQLException {
byte ret = 0;
if (value >= 'A' && value <= 'F') {
ret = (byte) (value - 'A' + 10);
} else if (value >= 'a' && value <= 'f') {
ret = (byte) (value - 'a' + 10);
} else if (value >= '0' && value <= '9') {
ret = (byte) (value - '0');
} else {
throw new IllegalArgumentException("The string is not in a valid hex format. ");
}
return ret;
}
/**
* Utility method for a callable statement
*
* @param connection
* connection object
* @param stmtColEncSetting
* SQLServerStatementColumnEncryptionSetting object
* @param sql
* @return
*/
public static CallableStatement getCallableStmt(Connection connection, String sql,
SQLServerStatementColumnEncryptionSetting stmtColEncSetting) throws SQLException {
if (null == stmtColEncSetting) {
return ((SQLServerConnection) connection).prepareCall(sql);
} else {
return ((SQLServerConnection) connection).prepareCall(sql, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY, connection.getHoldability(), stmtColEncSetting);
}
}
/**
* Utility method for generating a prepared statement
*
* @param connection
* connection object
* @param sql
* SQL string
* @param stmtColEncSetting
* SQLServerStatementColumnEncryptionSetting object
* @return
*/
public static PreparedStatement getPreparedStmt(Connection connection, String sql,
SQLServerStatementColumnEncryptionSetting stmtColEncSetting) throws SQLException {
if (null == stmtColEncSetting) {
return ((SQLServerConnection) connection).prepareStatement(sql);
} else {
return ((SQLServerConnection) connection).prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY, connection.getHoldability(), stmtColEncSetting);
}
}
/**
* Utility method for a scrollable statement
*
* @param connection
* connection object
* @return
*/
public static Statement getScrollableStatement(Connection connection) throws SQLException {
return ((SQLServerConnection) connection).createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
}
/**
* Utility method for a scrollable statement
*
* @param connection
* connection object
* @param stmtColEncSetting
* SQLServerStatementColumnEncryptionSetting object
* @return
*/
public static Statement getScrollableStatement(Connection connection,
SQLServerStatementColumnEncryptionSetting stmtColEncSetting) throws SQLException {
return ((SQLServerConnection) connection).createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE, stmtColEncSetting);
}
/**
* Utility method for a statement
*
* @param connection
* connection object
* @param sql
* SQL string
* @param stmtColEncSetting
* SQLServerStatementColumnEncryptionSetting object
* @return
*/
public static Statement getStatement(Connection connection,
SQLServerStatementColumnEncryptionSetting stmtColEncSetting) throws SQLException {
// default getStatement assumes resultSet is type_forward_only and concur_read_only
if (null == stmtColEncSetting) {
return ((SQLServerConnection) connection).createStatement();
} else {
return ((SQLServerConnection) connection).createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY, connection.getHoldability(), stmtColEncSetting);
}
}
/**
* Utility method for a statement
*
* @param connection
* connection object
* @param stmtColEncSetting
* SQLServerStatementColumnEncryptionSetting object
* @param rsScrollSensitivity
* @param rsConcurrence
* @return
*/
public static Statement getStatement(Connection connection,
SQLServerStatementColumnEncryptionSetting stmtColEncSetting, int rsScrollSensitivity,
int rsConcurrence) throws SQLException {
// overloaded getStatement allows setting resultSet type
if (null == stmtColEncSetting) {
return ((SQLServerConnection) connection).createStatement(rsScrollSensitivity, rsConcurrence,
connection.getHoldability());
} else {
return ((SQLServerConnection) connection).createStatement(rsScrollSensitivity, rsConcurrence,
connection.getHoldability(), stmtColEncSetting);
}
}
/**
* Converts a string to an array of bytes
*
* @param hexV
* a hexized string representation of bytes
* @return
* @throws SQLException
*/
public static byte[] hexStringToByte(String hexV) throws SQLException {
int len = hexV.length();
char orig[] = hexV.toCharArray();
if ((len % 2) != 0) {
throw new IllegalArgumentException("The string is not in a valid hex format: " + hexV);
}
byte[] bin = new byte[len / 2];
for (int i = 0; i < len / 2; i++) {
bin[i] = (byte) ((CharToHex(orig[2 * i]) << 4) + CharToHex(orig[2 * i + 1]));
}
return bin;
}
/**
* Utility method for a datetime value
*
* @param value
* @return
*/
public static Object roundDatetimeValue(Object value) {
if (value == null)
return null;
Timestamp ts = value instanceof Timestamp ? (Timestamp) value
: new Timestamp(((Calendar) value).getTimeInMillis());
int millis = ts.getNanos() / 1000000;
int lastDigit = (int) (millis % 10);
switch (lastDigit) {
// 0, 1 -> 0
case 1:
ts.setNanos((millis - 1) * 1000000);
break;
// 2, 3, 4 -> 3
case 2:
ts.setNanos((millis + 1) * 1000000);
break;
case 4:
ts.setNanos((millis - 1) * 1000000);
break;
// 5, 6, 7, 8 -> 7
case 5:
ts.setNanos((millis + 2) * 1000000);
break;
case 6:
ts.setNanos((millis + 1) * 1000000);
break;
case 8:
ts.setNanos((millis - 1) * 1000000);
break;
// 9 -> 0 with overflow
case 9:
ts.setNanos(0);
ts.setTime(ts.getTime() + millis + 1);
break;
// default, i.e. 0, 3, 7 -> 0, 3, 7
// don't change the millis but make sure that any
// sub-millisecond digits are zeroed out
default:
ts.setNanos((millis) * 1000000);
}
if (value instanceof Calendar) {
((Calendar) value).setTimeInMillis(ts.getTime());
((Calendar) value).getTimeInMillis();
return value;
}
return ts;
}
/**
* Utility method for a datetime value
*
* @param value
* @return
*/
public static Object roundSmallDateTimeValue(Object value) {
if (value == null) {
return null;
}
Calendar cal;
java.sql.Timestamp ts = null;
int nanos = -1;
if (value instanceof Calendar) {
cal = (Calendar) value;
} else {
ts = (java.sql.Timestamp) value;
cal = Calendar.getInstance();
cal.setTimeInMillis(ts.getTime());
nanos = ts.getNanos();
}
// round to the nearest minute
double seconds = cal.get(Calendar.SECOND)
+ (nanos == -1 ? ((double) cal.get(Calendar.MILLISECOND) / 1000) : ((double) nanos / 1000000000));
if (seconds > 29.998) {
cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) + 1);
}
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
nanos = 0;
// required to force computation
cal.getTimeInMillis();
// return appropriate value
if (value instanceof Calendar) {
return cal;
} else {
ts.setTime(cal.getTimeInMillis());
ts.setNanos(nanos);
return ts;
}
}
/**
* Checks if object SYS.SENSITIVITY_CLASSIFICATIONS exists in SQL Server
*
* @param Statement
* @return boolean
*/
public static boolean serverSupportsDataClassification(Statement stmt) {
try {
stmt.execute("SELECT * FROM SYS.SENSITIVITY_CLASSIFICATIONS");
} catch (SQLException e) {
// Check for Error 208: Invalid Object Name
if (e.getErrorCode() == 208) {
return false;
}
}
return true;
}
/**
* Utility function for checking if the system supports JDBC 4.2
*
* @param con
* @return
*/
public static boolean supportJDBC42(Connection con) throws SQLException {
SQLServerDatabaseMetaData meta = (SQLServerDatabaseMetaData) con.getMetaData();
return (meta.getJDBCMajorVersion() >= 4 && meta.getJDBCMinorVersion() >= 2);
}
/**
* Utility function for checking if the system supports JDBC 4.3
*
* @param con
* @return
*/
public static boolean supportJDBC43(Connection con) throws SQLException {
SQLServerDatabaseMetaData meta = (SQLServerDatabaseMetaData) con.getMetaData();
return (meta.getJDBCMajorVersion() >= 4 && meta.getJDBCMinorVersion() >= 3);
}
/**
* Escapes single quotes (') in object name to convert and pass it as String safely.
*
* @param name
* Object name to be passed as String
* @return Converted object name
*/
public static String escapeSingleQuotes(String name) {
return name.replace("'", "''");
}
public static final ResourceBundle R_BUNDLE = getDefaultLocaleBundle();
/**
* Returns the root bundle. This is the bundle from SQLServerResource.java - the English version that gets updated
* in development process.
*
* @return root bundle.
*/
private static ResourceBundle getDefaultLocaleBundle() {
return ResourceBundle.getBundle("com.microsoft.sqlserver.jdbc.SQLServerResource", Locale.getDefault());
}
/**
* Creates a regex where all '{#}' fields will return true for any value when calling match.
*
* @param s
* String to be formatted
* @return regex expression.
*/
public static String formatErrorMsg(String s) {
return (".*\\Q" + TestUtils.R_BUNDLE.getString(s) + "\\E" + ".*").replaceAll("\\{+[0-9]+\\}", "\\\\E.*\\\\Q");
}
/**
* Adds or updates the value of the given connection property in the connection string by overriding property.
*
* @param connectionString
* original connection string
* @param property
* name of the property
* @param value
* value of the property
* @return The updated connection string
*/
public static String addOrOverrideProperty(String connectionString, String property, String value) {
return connectionString + ";" + property + "=" + value + ";";
}
/**
* Remove the given connection property in the connection string
*
* @param connectionString
* original connection string
* @param property
* name of the property
* @return The updated connection string
*/
public static String removeProperty(String connectionString, String property) {
int start = connectionString.indexOf(property);
int end = connectionString.indexOf(";", start);
String propertyStr = connectionString.substring(start, -1 != end ? end + 1 : connectionString.length());
return connectionString.replace(propertyStr, "");
}
/**
* Get the given connection property in the connection string
*
* @param connectionString
* connection string
* @param property
* name of the property
* @return The the value of the connection property or null if not found
*/
public static String getProperty(String connectionString, String property) {
int start = connectionString.indexOf(property);
if (-1 == start) {
return null;
}
start = connectionString.indexOf("=", start) + 1;
int end = connectionString.indexOf(";", start);
return connectionString.substring(start, -1 != end ? end : connectionString.length());
}
/**
* Creates a truststore and returns the path of it.
*
* @param certificates
* String list of certificates
* @param tsName
* name of truststore to create
* @param tsPwd
* password of truststore to set
* @param ksType
* type of Keystore e.g PKCS12 or JKS
* @return Path of truststore that was created
* @throws Exception
*/
public static String createTrustStore(List<String> certificates, String tsName, String tsPwd,
String ksType) throws Exception {
return (new TrustStore(certificates, tsName, tsPwd, ksType)).getFileName();
}
private static class TrustStore {
private File trustStoreFile;
TrustStore(List<String> certificateNames, String tsName, String tsPwd, String ksType) throws Exception {
trustStoreFile = File.createTempFile(tsName, null, new File("."));
trustStoreFile.deleteOnExit();
KeyStore ks = KeyStore.getInstance(ksType);
ks.load(null, null);
for (String certificateName : certificateNames) {
ks.setCertificateEntry(certificateName, getCertificate(certificateName));
}
FileOutputStream os = new FileOutputStream(trustStoreFile);
ks.store(os, tsPwd.toCharArray());
os.flush();
os.close();
}
final String getFileName() throws Exception {
return trustStoreFile.getCanonicalPath();
}
private static java.security.cert.Certificate getCertificate(String certname) throws Exception {
FileInputStream is = new FileInputStream(certname);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
return cf.generateCertificate(is);
}
}
}