forked from assertj/assertj-db
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValues.java
More file actions
1223 lines (1141 loc) · 45.9 KB
/
Values.java
File metadata and controls
1223 lines (1141 loc) · 45.9 KB
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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2015-2024 the original author or authors.
*/
package org.assertj.db.util;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Array;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.UUID;
import org.assertj.db.exception.AssertJDBException;
import org.assertj.db.type.DateTimeValue;
import org.assertj.db.type.DateValue;
import org.assertj.db.type.TimeValue;
import org.assertj.db.type.Value;
import org.assertj.db.type.ValueType;
/**
* Utility methods related to values.
*
* @author Régis Pouiller
* @author Otoniel Isidoro
*/
public class Values {
/**
* Private constructor.
*/
private Values() {
// Empty
}
/**
* Returns if the value is equal to another value in parameter.
*
* @param value The value.
* @param expected The other value to compare.
* @return {@code true} if the value is equal to the value in parameter, {@code false} otherwise.
*/
public static boolean areEqual(Value value, Object expected) {
ValueType valueType = value.getValueType();
if (valueType == ValueType.BOOLEAN) {
if (expected instanceof Boolean) {
return areEqual(value, (Boolean) expected);
}
} else if (valueType == ValueType.NUMBER) {
if (expected instanceof Number) {
return areEqual(value, (Number) expected);
} else if (expected instanceof String) {
return areEqual(value, (String) expected);
}
} else if (valueType == ValueType.BYTES) {
if (expected instanceof byte[]) {
return areEqual(value, (byte[]) expected);
}
} else if (valueType == ValueType.TEXT) {
if (expected instanceof String) {
return areEqual(value, (String) expected);
} else if (expected instanceof Character) {
return areEqual(value, (Character) expected);
}
} else if (valueType == ValueType.UUID) {
if (expected instanceof UUID) {
return areEqual(value, (UUID) expected);
} else if (expected instanceof String) {
return areEqual(value, (String) expected);
}
} else if (valueType == ValueType.DATE) {
if (expected instanceof DateValue) {
return areEqual(value, (DateValue) expected);
} else if (expected instanceof String) {
return areEqual(value, (String) expected);
} else if (expected instanceof Date) {
return areEqual(value, DateValue.from((Date) expected));
} else if (expected instanceof LocalDate) {
return areEqual(value, DateValue.from((LocalDate) expected));
}
} else if (valueType == ValueType.TIME) {
if (expected instanceof TimeValue) {
return areEqual(value, (TimeValue) expected);
} else if (expected instanceof String) {
return areEqual(value, (String) expected);
} else if (expected instanceof Time) {
return areEqual(value, TimeValue.from((Time) expected));
} else if (expected instanceof LocalTime) {
return areEqual(value, TimeValue.from((LocalTime) expected));
}
} else if (valueType == ValueType.DATE_TIME) {
if (expected instanceof DateTimeValue) {
return areEqual(value, (DateTimeValue) expected);
} else if (expected instanceof DateValue) {
return areEqual(value, (DateValue) expected);
} else if (expected instanceof String) {
return areEqual(value, (String) expected);
} else if (expected instanceof Timestamp) {
return areEqual(value, DateTimeValue.from((Timestamp) expected));
} else if (expected instanceof LocalDateTime) {
return areEqual(value, DateTimeValue.from((LocalDateTime) expected));
} else if (expected instanceof LocalDate) {
return areEqual(value, DateValue.from((LocalDate) expected));
}
} else if (valueType == ValueType.ARRAY) {
if (expected instanceof Array) {
return areEqual(value, (Array) expected);
}
} else {
Object object = value.getValue();
if (expected == null && object == null) {
return true;
}
if (object != null) {
return object.equals(expected);
}
}
return false;
}
/**
* Returns if the value is equal to the {@code Boolean} in parameter.
*
* @param value The value.
* @param expected The {@code Boolean} to compare.
* @return {@code true} if the value is equal to the {@code Boolean} parameter, {@code false} otherwise.
*/
public static boolean areEqual(Value value, Boolean expected) {
Object object = value.getValue();
if (expected == null) {
return object == null;
}
return expected.equals(object);
}
/**
* Returns if the value is equal to the {@code Number} in parameter.
*
* @param value The value.
* @param expected The {@code Number} to compare.
* @return {@code true} if the value is equal to the {@code Number} parameter, {@code false} otherwise.
*/
public static boolean areEqual(Value value, Number expected) {
Object object = value.getValue();
if (expected == null) {
return object == null;
}
// If parameter is a BigInteger,
// change the actual in BigInteger to compare
if (expected instanceof BigInteger) {
BigInteger bi;
if (object instanceof BigInteger) {
bi = (BigInteger) object;
} else {
try {
bi = new BigInteger("" + object);
} catch (NumberFormatException e) {
throw new AssertJDBException("Expected <%s> can not be compared to a BigInteger (<%s>)", expected, object);
}
}
return bi.compareTo((BigInteger) expected) == 0;
}
// If parameter is a BigDecimal,
// change the value in BigDecimal to compare
else if (expected instanceof BigDecimal) {
BigDecimal bd;
if (object instanceof BigDecimal) {
bd = (BigDecimal) object;
} else {
try {
bd = new BigDecimal("" + object);
} catch (NumberFormatException e) {
throw new AssertJDBException("Expected <%s> can not be compared to a BigDecimal (<%s>)", expected, object);
}
}
return bd.compareTo((BigDecimal) expected) == 0;
}
// Otherwise
// If the value is Float, Double, BigInteger or BigDecimal
// change the value to compare to make the comparison possible
// else
// get the value value in Long to compare
else {
Long actualValue = null;
if (object instanceof Float) {
if (((Float) object) == expected.floatValue()) {
return true;
}
} else if (object instanceof Double) {
if (((Double) object) == expected.doubleValue()) {
return true;
}
} else if (object instanceof BigInteger) {
BigInteger bi = new BigInteger("" + expected);
if (((BigInteger) object).compareTo(bi) == 0) {
return true;
}
} else if (object instanceof BigDecimal) {
BigDecimal bd = new BigDecimal("" + expected);
if (((BigDecimal) object).compareTo(bd) == 0) {
return true;
}
} else if (object instanceof Byte) {
actualValue = ((Byte) object).longValue();
} else if (object instanceof Short) {
actualValue = ((Short) object).longValue();
} else if (object instanceof Integer) {
actualValue = ((Integer) object).longValue();
} else if (object instanceof Long) {
actualValue = (Long) object;
}
if (actualValue != null) {
if (expected instanceof Float) {
return actualValue == expected.floatValue();
} else if (expected instanceof Double) {
return actualValue == expected.doubleValue();
} else {
return actualValue == expected.longValue();
}
}
}
return false;
}
/**
* Returns if the value is equal to the array of {@code byte} in parameter.
*
* @param value The value.
* @param expected The array of {@code byte} to compare.
* @return {@code true} if the value is equal to the array of {@code byte} parameter, {@code false} otherwise.
*/
public static boolean areEqual(Value value, byte[] expected) {
Object object = value.getValue();
if (expected == null) {
return object == null;
}
if (object instanceof byte[]) {
byte[] bytes = (byte[]) object;
if (bytes.length != expected.length) {
return false;
}
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] != expected[i]) {
return false;
}
}
return true;
}
return false;
}
/**
* Returns if the date is equal to the {@code String} representation in parameter.
*
* @param date The date.
* @param expected The {@code String} representation to compare.
* @return {@code true} if the date is equal to the {@code String} representation parameter, {@code false} otherwise.
* @throws AssertJDBException If it is not possible to compare {@code date} to {@code expected}.
*/
private static boolean areEqual(Date date, String expected) {
try {
DateTimeValue dateTimeValue = DateTimeValue.of(DateValue.from(date));
DateTimeValue expectedDateTimeValue = DateTimeValue.parse(expected);
if (dateTimeValue.equals(expectedDateTimeValue)) {
return true;
}
} catch (ParseException e) {
throw new AssertJDBException("Expected <%s> is not correct to compare to <%s>", expected, date);
}
return false;
}
/**
* Returns if the time is equal to the {@code String} representation in parameter.
*
* @param time The time.
* @param expected The {@code String} representation to compare.
* @return {@code true} if the time is equal to the {@code String} representation parameter, {@code false} otherwise.
* @throws AssertJDBException If it is not possible to compare {@code time} to {@code expected}.
*/
private static boolean areEqual(Time time, String expected) {
try {
TimeValue timeValue = TimeValue.from(time);
TimeValue expectedTimeValue = TimeValue.parse(expected);
if (timeValue.equals(expectedTimeValue)) {
return true;
}
} catch (ParseException e) {
throw new AssertJDBException("Expected <%s> is not correct to compare to <%s>", expected, time);
}
return false;
}
/**
* Returns if the timestamp is equal to the {@code String} representation in parameter.
*
* @param timestamp The timestamp.
* @param expected The {@code String} representation to compare.
* @return {@code true} if the timestamp is equal to the {@code String} representation parameter, {@code false}
* otherwise.
* @throws AssertJDBException If it is not possible to compare {@code timestamp} to {@code expected}.
*/
private static boolean areEqual(Timestamp timestamp, String expected) {
try {
DateTimeValue dateTimeValue = DateTimeValue.from(timestamp);
DateTimeValue expectedDateTimeValue = DateTimeValue.parse(expected);
if (dateTimeValue.equals(expectedDateTimeValue)) {
return true;
}
} catch (ParseException e) {
throw new AssertJDBException("Expected <%s> is not correct to compare to <%s>", expected, timestamp);
}
return false;
}
/**
* Returns if the number is equal to the {@code String} representation in parameter.
*
* @param number The number.
* @param expected The {@code String} representation to compare.
* @return {@code true} if the number is equal to the {@code String} representation parameter, {@code false}
* otherwise.
* @throws NullPointerException if {@code expected} is {@code null}.
* @throws AssertJDBException If it is not possible to compare {@code number} to {@code expected}.
*/
private static boolean areEqual(Number number, String expected) {
try {
if (number instanceof Float) {
if (number.floatValue() == Float.parseFloat(expected)) {
return true;
}
} else if (number instanceof Double) {
if (number.doubleValue() == Double.parseDouble(expected)) {
return true;
}
} else if (number instanceof BigInteger) {
BigInteger bi = new BigInteger("" + expected);
if (((BigInteger) number).compareTo(bi) == 0) {
return true;
}
} else if (number instanceof BigDecimal) {
BigDecimal bd = new BigDecimal("" + expected);
if (((BigDecimal) number).compareTo(bd) == 0) {
return true;
}
} else {
Long actual = null;
if (number instanceof Byte || number instanceof Short || number instanceof Integer) {
actual = number.longValue();
} else if (number instanceof Long) {
actual = (Long) number;
}
if (actual != null && actual == Long.parseLong(expected)) {
return true;
}
}
} catch (NumberFormatException e) {
throw new AssertJDBException("Expected <%s> is not correct to compare to <%s>", expected, number);
}
return false;
}
/**
* Returns if the value is equal to the {@code String} in parameter.
*
* @param value The value.
* @param expected The {@code String} to compare.
* @return {@code true} if the value is equal to the {@code String} parameter, {@code false} otherwise.
* @throws NullPointerException if {@code expected} is {@code null}.
* @throws AssertJDBException If {@code value} is a {@code Number} and it is not possible to compare to
* {@code expected}.
*/
public static boolean areEqual(Value value, String expected) {
Object object = value.getValue();
if (expected == null) {
return object == null;
}
if (object instanceof Number) {
return areEqual((Number) object, expected);
} else if (object instanceof Date) {
return areEqual((Date) object, expected);
} else if (object instanceof Time) {
return areEqual((Time) object, expected);
} else if (object instanceof Timestamp) {
return areEqual((Timestamp) object, expected);
} else if (object instanceof UUID) {
return areEqual((UUID) object, expected);
}
return expected.equals(object);
}
/**
* Returns if the value is equal to the {@code String} in parameter.
*
* @param value The value.
* @param expected The {@code Character} to compare.
* @return {@code true} if the value is equal to the {@code Character} parameter, {@code false} otherwise.
* @throws NullPointerException if {@code expected} is {@code null}.
* @since 1.2.0
*/
public static boolean areEqual(Value value, Character expected) {
Object object = value.getValue();
if (expected == null) {
return object == null;
}
if (object instanceof String) {
return ((String) object).charAt(0) == expected;
}
return expected.equals(object);
}
/**
* Returns if the value is equal to the {@code UUID} in parameter.
*
* @param value The value.
* @param expected The {@code UUID} to compare.
* @return {@code true} if the value is equal to the {@code UUID} parameter, {@code false} otherwise.
* @since 1.1.0
*/
public static boolean areEqual(Value value, UUID expected) {
Object object = value.getValue();
if (expected == null) {
return object == null;
}
return expected.equals(object);
}
/**
* Returns if the {@code UUID} value is equal to the {@code String} in parameter.
*
* @param value The {@code UUID} value.
* @param expected The {@code String} to compare.
* @return {@code true} if the {@code UUID} value is equal to the {@code String} parameter, {@code false} otherwise.
* @throws AssertJDBException If it is not possible to compare {@code UUID} to {@code expected}.
* @since 1.1.0
*/
public static boolean areEqual(UUID value, String expected) {
if (expected == null) {
return value == null;
}
try {
return UUID.fromString(expected).equals(value);
} catch (IllegalArgumentException e) {
throw new AssertJDBException("Expected <%s> is not correct to compare to <%s>", expected, value);
}
}
/**
* Returns if the value is equal to the {@link DateValue} in parameter.
*
* @param value The value.
* @param expected The {@link DateValue} to compare.
* @return {@code true} if the value is equal to the {@link DateValue} parameter, {@code false} otherwise.
*/
public static boolean areEqual(Value value, DateValue expected) {
Object object = value.getValue();
if (expected == null) {
return object == null;
}
if (object instanceof Date) {
Date date = (Date) object;
DateValue dateValue = DateValue.from(date);
return dateValue.equals(expected);
} else if (object instanceof Timestamp) {
Timestamp timestamp = (Timestamp) object;
DateTimeValue dateTimeValue = DateTimeValue.from(timestamp);
return dateTimeValue.equals(DateTimeValue.of(expected));
}
return false;
}
/**
* Returns if the value is equal to the {@link TimeValue} in parameter.
*
* @param value The value.
* @param expected The {@link TimeValue} to compare.
* @return {@code true} if the value is equal to the {@link TimeValue} parameter, {@code false} otherwise.
*/
public static boolean areEqual(Value value, TimeValue expected) {
Object object = value.getValue();
if (expected == null) {
return object == null;
}
if (object instanceof Time) {
Time time = (Time) object;
TimeValue timeValue = TimeValue.from(time);
return timeValue.equals(expected);
}
return false;
}
/**
* Returns if the value is equal to the {@link DateTimeValue} in parameter.
*
* @param value The value.
* @param expected The {@link DateTimeValue} to compare.
* @return {@code true} if the value is equal to the {@link DateTimeValue} parameter, {@code false} otherwise.
*/
public static boolean areEqual(Value value, DateTimeValue expected) {
Object object = value.getValue();
if (expected == null) {
return object == null;
}
if (object instanceof Date) {
Date date = (Date) object;
DateTimeValue dateTimeValue = DateTimeValue.of(DateValue.from(date));
return dateTimeValue.equals(expected);
}
if (object instanceof Timestamp) {
Timestamp timestamp = (Timestamp) object;
DateTimeValue dateTimeValue = DateTimeValue.from(timestamp);
return dateTimeValue.equals(expected);
}
return false;
}
/**
* Returns the result of the comparison between the value and the {@code Number} in parameter.
*
* @param value The value.
* @param expected The {@code Number} to compare.
* @return {@code 0} if the value is equal to the {@code Number} parameter, {@code -1} if value is less than the
* {@code Number} parameter and {@code 1} if value is greater than the {@code Number} parameter.
*/
public static int compare(Value value, Number expected) {
Object object = value.getValue();
// If parameter is a BigInteger,
// change the actual in BigInteger to compare
if (expected instanceof BigInteger) {
BigInteger bi;
if (object instanceof BigInteger) {
bi = (BigInteger) object;
} else {
try {
bi = new BigInteger("" + object);
} catch (NumberFormatException e) {
throw new AssertJDBException("Expected <%s> can not be compared to a BigInteger (<%s>)", expected, object);
}
}
return bi.compareTo((BigInteger) expected);
}
// If parameter is a BigDecimal,
// change the value in BigDecimal to compare
else if (expected instanceof BigDecimal) {
BigDecimal bd;
if (object instanceof BigDecimal) {
bd = (BigDecimal) object;
} else {
try {
bd = new BigDecimal("" + object);
} catch (NumberFormatException e) {
throw new AssertJDBException("Expected <%s> can not be compared to a BigDecimal (<%s>)", expected, object);
}
}
return bd.compareTo((BigDecimal) expected);
}
// Otherwise
// If the value is Float, Double, BigInteger or BigDecimal
// change the value to compare to make the comparison possible
// else
// get the value value in Long to compare
else {
Long actualValue = null;
if (object instanceof Float) {
float f = (Float) object;
float expectedF = expected.floatValue();
return Float.compare(f, expectedF);
} else if (object instanceof Double) {
double d = (Double) object;
double expectedD = expected.doubleValue();
return Double.compare(d, expectedD);
} else if (object instanceof BigInteger) {
BigInteger bi = new BigInteger("" + expected);
return ((BigInteger) object).compareTo(bi);
} else if (object instanceof BigDecimal) {
BigDecimal bd = new BigDecimal("" + expected);
return ((BigDecimal) object).compareTo(bd);
} else if (object instanceof Byte) {
actualValue = ((Byte) object).longValue();
} else if (object instanceof Short) {
actualValue = ((Short) object).longValue();
} else if (object instanceof Integer) {
actualValue = ((Integer) object).longValue();
} else if (object instanceof Long) {
actualValue = (Long) object;
}
if (actualValue != null) {
if (expected instanceof Float) {
float expectedF = expected.floatValue();
if (actualValue > expectedF) {
return 1;
} else if (actualValue < expectedF) {
return -1;
} else {
return 0;
}
} else if (expected instanceof Double) {
double expectedD = expected.doubleValue();
if (actualValue > expectedD) {
return 1;
} else if (actualValue < expectedD) {
return -1;
} else {
return 0;
}
} else {
double expectedL = expected.longValue();
if (actualValue > expectedL) {
return 1;
} else if (actualValue < expectedL) {
return -1;
} else {
return 0;
}
}
}
}
throw new AssertJDBException("Expected <%s> can not be compared to a Number (<%s>)", expected, object);
}
/**
* Returns a representation of the values (this representation is used for error message).
*
* @param values Values in the database.
* @param expected Expected values for comparison.
* @return A representation of the values.
* @throws org.assertj.db.exception.AssertJDBException If the length of the two arrays are different.
*/
public static Object[] getRepresentationsFromValuesInFrontOfExpected(Value[] values, Object[] expected) {
Object[] representationsValues = new Object[values.length];
int i = 0;
for (Value obj : values) {
if (i >= expected.length) {
representationsValues[i] = obj.getValue();
} else {
representationsValues[i] = Values.getRepresentationFromValueInFrontOfExpected(obj, expected[i]);
}
i++;
}
return representationsValues;
}
/**
* Returns a representation of the value (this representation is used for error message).
*
* @param value Value in the database.
* @param expected Expected value for comparison.
* @return A representation of the value.
*/
public static Object getRepresentationFromValueInFrontOfExpected(Value value, Object expected) {
Object object = value.getValue();
ValueType valueType = value.getValueType();
if (valueType == ValueType.DATE) {
if (expected instanceof String) {
if (((String) expected).contains("T")) {
return DateTimeValue.of(DateValue.from((Date) object)).toString();
} else {
return DateValue.from((Date) object).toString();
}
}
}
return getRepresentationFromValueInFrontOfClass(value, expected == null ? null : expected.getClass());
}
/**
* Returns a representation of the value (this representation is used for error message).
*
* @param value Value in the database.
* @param clazz Class for comparison.
* @return A representation of the value.
*/
public static Object getRepresentationFromValueInFrontOfClass(Value value, Class<?> clazz) {
Object object = value.getValue();
ValueType valueType = value.getValueType();
if (valueType == ValueType.DATE) {
if (clazz == DateValue.class) {
return DateValue.from((Date) object);
} else if (clazz == DateTimeValue.class) {
return DateTimeValue.of(DateValue.from((Date) object));
} else if (clazz == String.class) {
return DateTimeValue.of(DateValue.from((Date) object)).toString();
}
return object;
} else if (valueType == ValueType.TIME) {
if (clazz == String.class) {
return TimeValue.from((Time) object).toString();
} else {
return TimeValue.from((Time) object);
}
} else if (valueType == ValueType.DATE_TIME) {
if (clazz == String.class) {
return DateTimeValue.from((Timestamp) object).toString();
} else {
return DateTimeValue.from((Timestamp) object);
}
} else if (valueType == ValueType.NUMBER || valueType == ValueType.UUID) {
if (clazz == String.class) {
return object.toString();
} else {
return object;
}
}
return object;
}
/**
* Returns if object is close to the {@code BigInteger} in parameter with the tolerance in parameter.
*
* @param object The object.
* @param expected The {@code BigInteger} to compare.
* @param tolerance The tolerance of the closeness.
* @return {@code true} if the value is close to the {@code BigInteger} parameter, {@code false} otherwise.
*/
private static boolean isObjectCloseToBigInteger(Object object, BigInteger expected, Number tolerance) {
BigInteger bi;
if (object instanceof BigInteger) {
bi = (BigInteger) object;
} else {
try {
bi = new BigInteger("" + object);
} catch (NumberFormatException e) {
throw new AssertJDBException("Expected <%s> can not be compared to a BigInteger (<%s>)", expected, object);
}
}
BigInteger bigTolerance = new BigInteger("" + tolerance);
BigInteger bigMin = expected.subtract(bigTolerance);
BigInteger bigMax = expected.add(bigTolerance);
return bi.compareTo(bigMin) >= 0 && bi.compareTo(bigMax) <= 0;
}
/**
* Returns if object is close to the {@code BigDecimal} in parameter with the tolerance in parameter.
*
* @param object The object.
* @param expected The {@code BigDecimal} to compare.
* @param tolerance The tolerance of the closeness.
* @return {@code true} if the value is close to the {@code BigDecimal} parameter, {@code false} otherwise.
*/
private static boolean isObjectCloseToBigDecimal(Object object, BigDecimal expected, Number tolerance) {
BigDecimal bd;
if (object instanceof BigDecimal) {
bd = (BigDecimal) object;
} else {
try {
bd = new BigDecimal("" + object);
} catch (NumberFormatException e) {
throw new AssertJDBException("Expected <%s> can not be compared to a BigDecimal (<%s>)", expected, object);
}
}
BigDecimal bigTolerance = new BigDecimal("" + tolerance);
BigDecimal bigMin = expected.subtract(bigTolerance);
BigDecimal bigMax = expected.add(bigTolerance);
return bd.compareTo(bigMin) >= 0 && bd.compareTo(bigMax) <= 0;
}
/**
* Returns if nb is close to the {@code Number} in parameter with the tolerance in parameter.
*
* @param nb The {@code Float}.
* @param expected The {@code Number} to compare.
* @param tolerance The tolerance of the closeness.
* @return {@code true} if the value is close to the {@code Number} parameter, {@code false} otherwise.
*/
private static boolean isFloatCloseToNumber(Float nb, Number expected, Number tolerance) {
float fMin = expected.floatValue() - tolerance.floatValue();
float fMax = expected.floatValue() + tolerance.floatValue();
return nb >= fMin && nb <= fMax;
}
/**
* Returns if nb is close to the {@code Number} in parameter with the tolerance in parameter.
*
* @param nb The {@code Double}.
* @param expected The {@code Number} to compare.
* @param tolerance The tolerance of the closeness.
* @return {@code true} if the value is close to the {@code Number} parameter, {@code false} otherwise.
*/
private static boolean isDoubleCloseToNumber(Double nb, Number expected, Number tolerance) {
double dMin = expected.doubleValue() - tolerance.doubleValue();
double dMax = expected.doubleValue() + tolerance.doubleValue();
return nb >= dMin && nb <= dMax;
}
/**
* Returns if nb is close to the {@code Number} in parameter with the tolerance in parameter.
*
* @param nb The {@code BigInteger}.
* @param expected The {@code Number} to compare.
* @param tolerance The tolerance of the closeness.
* @return {@code true} if the value is close to the {@code Number} parameter, {@code false} otherwise.
*/
private static boolean isBigIntegerCloseToNumber(BigInteger nb, Number expected, Number tolerance) {
BigInteger bigExpected = new BigInteger("" + expected);
BigInteger bigTolerance = new BigInteger("" + tolerance);
BigInteger bigMin = bigExpected.subtract(bigTolerance);
BigInteger bigMax = bigExpected.add(bigTolerance);
return nb.compareTo(bigMin) >= 0 && nb.compareTo(bigMax) <= 0;
}
/**
* Returns if nb is close to the {@code Number} in parameter with the tolerance in parameter.
*
* @param nb The {@code BigDecimal}.
* @param expected The {@code Number} to compare.
* @param tolerance The tolerance of the closeness.
* @return {@code true} if the value is close to the {@code Number} parameter, {@code false} otherwise.
*/
private static boolean isBigDecimalCloseToNumber(BigDecimal nb, Number expected, Number tolerance) {
BigDecimal bigExpected = new BigDecimal("" + expected);
BigDecimal bigTolerance = new BigDecimal("" + tolerance);
BigDecimal bigMin = bigExpected.subtract(bigTolerance);
BigDecimal bigMax = bigExpected.add(bigTolerance);
return nb.compareTo(bigMin) >= 0 && nb.compareTo(bigMax) <= 0;
}
/**
* Returns the Long corresponding to object.
*
* @param object The object.
* @return The Long
*/
private static Long getLong(Object object) {
if (object instanceof Byte) {
return ((Byte) object).longValue();
} else if (object instanceof Short) {
return ((Short) object).longValue();
} else if (object instanceof Integer) {
return ((Integer) object).longValue();
} else if (object instanceof Long) {
return (Long) object;
}
return null;
}
/**
* Returns if nb is close to the {@code Float} in parameter with the tolerance in parameter.
*
* @param nb The {@code Long}.
* @param expected The {@code Float} to compare.
* @param tolerance The tolerance of the closeness.
* @return {@code true} if the value is close to the {@code Float} parameter, {@code false} otherwise.
*/
private static boolean isLongCloseToFloat(Long nb, Float expected, Number tolerance) {
if (tolerance instanceof Float) {
return nb >= expected - tolerance.floatValue() &&
nb <= expected + tolerance.floatValue();
} else if (tolerance instanceof Double) {
return nb >= expected - tolerance.doubleValue() &&
nb <= expected + tolerance.doubleValue();
} else {
return nb >= expected - tolerance.longValue() &&
nb <= expected + tolerance.longValue();
}
}
/**
* Returns if nb is close to the {@code Double} in parameter with the tolerance in parameter.
*
* @param nb The {@code Long}.
* @param expected The {@code Double} to compare.
* @param tolerance The tolerance of the closeness.
* @return {@code true} if the value is close to the {@code Double} parameter, {@code false} otherwise.
*/
private static boolean isLongCloseToDouble(Long nb, Double expected, Number tolerance) {
if (tolerance instanceof Float) {
return nb >= expected - tolerance.floatValue() &&
nb <= expected + tolerance.floatValue();
} else if (tolerance instanceof Double) {
return nb >= expected - tolerance.doubleValue() &&
nb <= expected + tolerance.doubleValue();
} else {
return nb >= expected - tolerance.longValue() &&
nb <= expected + tolerance.longValue();
}
}
/**
* Returns if nb is close to the {@code Number} in parameter with the tolerance in parameter.
*
* @param nb The {@code Long}.
* @param expected The {@code Number} to compare.
* @param tolerance The tolerance of the closeness.
* @return {@code true} if the value is close to the {@code Number} parameter, {@code false} otherwise.
*/
private static boolean isLongCloseToNumber(Long nb, Number expected, Number tolerance) {
if (tolerance instanceof Float) {
return nb >= expected.longValue() - tolerance.floatValue() &&
nb <= expected.longValue() + tolerance.floatValue();
} else if (tolerance instanceof Double) {
return nb >= expected.longValue() - tolerance.doubleValue() &&
nb <= expected.longValue() + tolerance.doubleValue();
} else {
return nb >= expected.longValue() - tolerance.longValue() &&
nb <= expected.longValue() + tolerance.longValue();
}
}
/**
* Returns if the value is close to the {@code Number} in parameter with the tolerance in parameter.
*
* @param value The value.
* @param expected The {@code Number} to compare.
* @param tolerance The tolerance of the closeness.
* @return {@code true} if the value is close to the {@code Number} parameter, {@code false} otherwise.
*/
public static boolean areClose(Value value, Number expected, Number tolerance) {
Object object = value.getValue();
if (expected == null) {
return object == null;
}
// If parameter is a BigInteger,
// change the actual in BigInteger to compare
if (expected instanceof BigInteger) {
return isObjectCloseToBigInteger(object, (BigInteger) expected, tolerance);
}
// If parameter is a BigDecimal,
// change the value in BigDecimal to compare
else if (expected instanceof BigDecimal) {
return isObjectCloseToBigDecimal(object, (BigDecimal) expected, tolerance);
}
// Otherwise
// If the value is Float, Double, BigInteger or BigDecimal
// change the value to compare to make the comparison possible
// else
// get the value value in Long to compare
else if (object instanceof Float) {
return isFloatCloseToNumber((Float) object, expected, tolerance);
} else if (object instanceof Double) {
return isDoubleCloseToNumber((Double) object, expected, tolerance);
} else if (object instanceof BigInteger) {
return isBigIntegerCloseToNumber((BigInteger) object, expected, tolerance);
} else if (object instanceof BigDecimal) {
return isBigDecimalCloseToNumber((BigDecimal) object, expected, tolerance);
} else {
Long actualValue = getLong(object);
if (actualValue != null) {
if (expected instanceof Float) {
return isLongCloseToFloat(actualValue, expected.floatValue(), tolerance);
} else if (expected instanceof Double) {
return isLongCloseToDouble(actualValue, expected.doubleValue(), tolerance);
} else {
return isLongCloseToNumber(actualValue, expected.longValue(), tolerance);
}
}
}
return false;
}
/**
* Returns if the value is close to the {@code DateValue} in parameter with the tolerance in parameter.
*
* @param value The value.
* @param expected The {@code DateValue} to compare.