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
|
/* *******************************************************************
* Copyright (c) 1999-2000 Xerox Corporation.
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0
* which accompanies this distribution and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Xerox/PARC initial implementation
* ******************************************************************/
package org.aspectj.testing;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import org.aspectj.bridge.IMessage;
import org.aspectj.bridge.IMessageHandler;
import org.aspectj.bridge.Message;
import org.aspectj.util.LangUtil;
/**
* Testing client interface for checking results and reporting
* to a delegate IMessageHandler.
* Harnesses providing this interface for test clients must
* set it up by calling
* {@link #setBASEDIR(File)}
* {@link #setMessageHandler(IMessageHandler)} and
* {@link #clear()} for each test, as appropriate.
* (That means that IMessageHandler must be loaded from a class
* loader common to the harness and Tester.)
* If clients submit a failing check, this registers the message
* and throws an AbortException holding the message; this
* AbortException <b>will not</b> have the correct stack trace;
* all the information should be encoded in the message.
* Find any original exception thrown in the message itself.
*/
// XXX consider creating exception for new API throwFailure(String m)
public class Tester {
/** delegate for reporting results */
private static IMessageHandler messageHandler;
/** base directory for calculating relative paths to event files */
private static File BASEDIR;
/**
* collection of notes submitted
*/
private static Set<String> notes;
/** <code>List</code> to hold events submitted. */
private static List<String> actualEvents = new ArrayList<>();
/** <code>List</code> to hold events we expect. */
private static List<String> expectedEvents = new ArrayList<>();
static {
setBASEDIR(new File("."));
setMessageHandler(IMessageHandler.SYSTEM_ERR);
clear();
}
/**
* Set directory used for calculating relative paths
* (currently only to an events file)
* @param baseDir the File for an existing directory
*/
public static void setBASEDIR(File baseDir) {
if (null == baseDir) throw new IllegalArgumentException("null baseDir");
if (!baseDir.isDirectory()) throw new IllegalArgumentException("not a directory: " + baseDir);
BASEDIR = baseDir;
}
public static File getBASEDIR() {
return BASEDIR;
}
/**
* Set the message handler used for this Tester.
* When given a message of kind FAIL, this handler
* must complete abruptly or return false (i.e., not handled completely)
* so the Tester throws an AbortException.
* @see checkFailed(..).
*/
public static void setMessageHandler(IMessageHandler handler) {
if (null == handler) throw new IllegalArgumentException("null handler");
if (messageHandler != handler) messageHandler = handler;
}
public static void clear() {
clearNotes();
clearEvents();
}
/** XXX deprecated #clear() */
public static void clearNotes() {
notes = new HashSet<>();
}
/** XXX deprecated #clear() */
public static void clearEvents() {
actualEvents = new ArrayList<>();
expectedEvents = new ArrayList<>();
}
/** Add an actual event */
public static void event(String s) {
actualEvents.add(s);
}
/**
* Add a note to {@link #notes}.
* @param note Message to add.
* XXX deprecated event(String)
*/
public static void note(String note) {
notes.add(note);
}
/**
* Checks that <code>note</code> was added using {@link #note},
* and fails using <code>note.toString()</code> is it wasn't found.
*
* @param note Message that should've been added using {@link #note}.
* XXX deprecated checkEvent(String)
*/
public static void check(String note) {
check(note, "expected note \"" + note.toString() + "\"");
}
/**
* Checks that <code>note</code> was added using {@link #note},
* and fails using <code>message</code> is it wasn't found.
*
* @param note Message that should've been added using {@link #note}.
* @param message Message with which to fail if <code>node</code>
* wasn't added.
*/
public static void check(String note, String message) {
check(notes.contains(note), message);
}
/**
* Reports that <code>t</code> shouldn't have been thrown.
* using <code>t</code> as the message.
*
* @param t Thrown exception.
* @see #throwable(Throwable,String)
*/
public static void throwable(Throwable t) {
throwable(t, null);
}
/**
* Reports that <code>t</code> shouldn't have been thrown.
* using <code>msg</code> as the message.
*
* @param thrown Thrown exception.
* @param msg Message with which to report error.
*/
public static void throwable(Throwable thrown, String msg) {
handle(msg, thrown, true);
}
/**
* Report the error <code>message</code> unconditionally.
*
* @param message Error to report.
*/
public static void checkFailed(String message) {
handle(message, null, true);
}
/**
* Check that <code>expectedNotes</code> is equal to {@link #notes}
* , fail with <code>msg</code> and create a new instance of {@link #notes}.
* <i>NOTE: <code>expectedNotes</code> is a <code>String</code>, so
* it must match with {@link java.util.HashSet#toString()}</i>.
*
* @param expectedNotes <code>String</code> we expect
* {@link #notes} to match.
* @param msg Message with which to fail.
*/
public static void checkAndClear(String expectedNotes, String msg) {
checkEqual(notes, expectedNotes, msg);
clearNotes();
}
/**
* Reports an error using <code>message</code> if
* <code>test == false</code>.
*
* @param test Determines whether we call {@link #checkFailed}.
* @param message Message to pass {@link #checkFailed} if
* <code>test == false</code>.
*/
public static void check(boolean test, String message) {
if (!test) checkFailed(message);
}
/**
* Checks that the values of <code>value</code> and
* <code>expectedValue</code> are equal. Both or either
* can be null. Calls {@link #checkFailed} with <code>message</code>
* if the arrays aren't equal.
*
* @param value One test set.
* @param expectedValue The other test set.
* @param message Message with which to fail.
*/
public static void checkEqual(Object[] value,
Object[] expectedValue,
String message)
{
if (value == null) {
if (expectedValue == null) return;
checkFailed(message+" null array found");
return;
}
int n = value.length;
if (n != expectedValue.length) {
checkFailed(message+" expected array of length "+expectedValue.length
+" got "+ n);
return;
}
for(int i=0; i<n; i++) {
if (!value[i].equals(expectedValue[i])) {
checkFailed(message+": "+value[i]+" != "+
expectedValue[i]+" at index "+i);
}
}
}
/**
* Fails if <code>s == t</code>.
*
* @param s a known value.
* @param t another known value.
*/
public static void checkNotEqual(boolean s, boolean t) {
checkNotEqual(s, t, s + " shouldn't equal " + t);
}
/**
* Fails with message <code>msg</code> if <code>s == t</code>.
*
* @param s a known value.
* @param t another known value.
* @param msg the failure message.
*/
public static void checkNotEqual(boolean s, boolean t, String msg) {
if (s == t) checkFailed(msg);
}
/**
* Fails if <code>s == t</code>.
*
* @param s a known value.
* @param t another known value.
*/
public static void checkNotEqual(byte s, byte t) {
checkNotEqual(s, t, s + " shouldn't equal " + t);
}
/**
* Fails with message <code>msg</code> if <code>s == t</code>.
*
* @param s a known value.
* @param t another known value.
* @param msg the failure message.
*/
public static void checkNotEqual(byte s, byte t, String msg) {
if (s == t) checkFailed(msg);
}
/**
* Fails if <code>s == t</code>.
*
* @param s a known value.
* @param t another known value.
*/
public static void checkNotEqual(char s, char t) {
checkNotEqual(s, t, s + " shouldn't equal " + t);
}
/**
* Fails with message <code>msg</code> if <code>s == t</code>.
*
* @param s a known value.
* @param t another known value.
* @param msg the failure message.
*/
public static void checkNotEqual(char s, char t, String msg) {
if (s == t) checkFailed(msg);
}
/**
* Fails if <code>s == t</code>.
*
* @param s a known value.
* @param t another known value.
*/
public static void checkNotEqual(short s, short t) {
checkNotEqual(s, t, s + " shouldn't equal " + t);
}
/**
* Fails with message <code>msg</code> if <code>s == t</code>.
*
* @param s a known value.
* @param t another known value.
* @param msg the failure message.
*/
public static void checkNotEqual(short s, short t, String msg) {
if (s == t) checkFailed(msg);
}
/**
* Fails if <code>s == t</code>.
*
* @param s a known value.
* @param t another known value.
*/
public static void checkNotEqual(int s, int t) {
checkNotEqual(s, t, s + " shouldn't equal " + t);
}
/**
* Fails with message <code>msg</code> if <code>s == t</code>.
*
* @param s a known value.
* @param t another known value.
* @param msg the failure message.
*/
public static void checkNotEqual(int s, int t, String msg) {
if (s == t) checkFailed(msg);
}
/**
* Fails if <code>s == t</code>.
*
* @param s a known value.
* @param t another known value.
*/
public static void checkNotEqual(long s, long t) {
checkNotEqual(s, t, s + " shouldn't equal " + t);
}
/**
* Fails with message <code>msg</code> if <code>s == t</code>.
*
* @param s a known value.
* @param t another known value.
* @param msg the failure message.
*/
public static void checkNotEqual(long s, long t, String msg) {
if (s == t) checkFailed(msg);
}
/**
* Fails if <code>s == t</code>.
*
* @param s a known value.
* @param t another known value.
*/
public static void checkNotEqual(float s, float t) {
checkNotEqual(s, t, s + " shouldn't equal " + t);
}
/**
* Fails with message <code>msg</code> if <code>s == t</code>.
*
* @param s a known value.
* @param t another known value.
* @param msg the failure message.
*/
public static void checkNotEqual(float s, float t, String msg) {
if (s == t) checkFailed(msg);
}
/**
* Fails if <code>s == t</code>.
*
* @param s a known value.
* @param t another known value.
*/
public static void checkNotEqual(double s, double t) {
checkNotEqual(s, t, s + " shouldn't equal " + t);
}
/**
* Fails with message <code>msg</code> if <code>s == t</code>.
*
* @param s a known value.
* @param t another known value.
* @param msg the failure message.
*/
public static void checkNotEqual(double s, double t, String msg) {
if (s == t) checkFailed(msg);
}
/**
* Fails if <code>s == t</code>.
*
* @param s a known value.
* @param t another known value.
*/
public static void checkNotEqual(Object s, Object t) {
checkNotEqual(s, t, s + " shouldn't equal " + t);
}
/**
* Fails with message <code>msg</code> if <code>s == t</code>
* or both <code>s</code> and <code>t</code> are <code>null</code>.
*
* @param s a known value.
* @param t another known value.
* @param msg the failure message.
*/
public static void checkNotEqual(Object s, Object t, String msg) {
if ((s != null && s.equals(t)) ||
(t != null && t.equals(s)) ||
(s == null && t == null)) {
checkFailed(msg);
}
}
/**
* Compares <code>value</code> and <code>expectedValue</code>
* with failing message <code>"compare"</code>.
*
* @param value Unkown value.
* @param expectedValue Expected value.
* @see #checkEqual(int,int,String)
*/
public static void checkEqual(int value, int expectedValue) {
checkEqual(value, expectedValue, "compare");
}
/**
* Fails if the passed in value is <code>null</code>.
*
* @param o the expected non-null thing.
* @param name the name of <code>o</code>.
*/
public static void checkNonNull(Object o, String name) {
if (o == null) checkFailed(name + " shouldn't be null");
}
/**
* Compared <code>value</code> and <code>expectedValue</code>
* and fails with <code>message</code> if they aren't equal.
*
* @param value Unkown value.
* @param expectedValue Expected value.
* @param msg Message with which to fail.
*/
public static void checkEqual(int value, int expectedValue, String message) {
if (value == expectedValue) return;
if (value < expectedValue) {
message = message+": "+value+" < "+expectedValue;
} else {
message = message+": "+value+" > "+expectedValue;
}
checkFailed(message);
}
/**
* Compares <code>value</code> and <code>expectedValue</code>
* with failing message <code>"compare"</code>.
*
* @param value Unkown value.
* @param expectedValue Expected value.
* @see #checkEqual(float,float,String)
*/
public static void checkEqual(float value, float expectedValue) {
checkEqual(value, expectedValue, "compare");
}
/**
* Compared <code>value</code> and <code>expectedValue</code>
* and fails with <code>message</code> if they aren't equal.
*
* @param value Unkown value.
* @param expectedValue Expected value.
* @param msg Message with which to fail.
*/
public static void checkEqual(float value, float expectedValue, String msg) {
if (Float.isNaN(value) && Float.isNaN(expectedValue)) return;
if (value == expectedValue) return;
if (value < expectedValue) {
msg = msg+": "+value+" < "+expectedValue;
} else {
msg = msg+": "+value+" > "+expectedValue;
}
checkFailed(msg);
}
/**
* Compares <code>value</code> and <code>expectedValue</code>
* with failing message <code>"compare"</code>.
*
* @param value Unkown value.
* @param expectedValue Expected value.
* @see #checkEqual(long,long,String)
*/
public static void checkEqual(long value, long expectedValue) {
checkEqual(value, expectedValue, "compare");
}
/**
* Compared <code>value</code> and <code>expectedValue</code>
* and fails with <code>message</code> if they aren't equal.
*
* @param value Unkown value.
* @param expectedValue Expected value.
* @param msg Message with which to fail.
*/
public static void checkEqual(long value, long expectedValue, String msg) {
if (value == expectedValue) return;
if (value < expectedValue) {
msg = msg+": "+value+" < "+expectedValue;
} else {
msg = msg+": "+value+" > "+expectedValue;
}
checkFailed(msg);
}
/**
* Compares <code>value</code> and <code>expectedValue</code>
* with failing message <code>"compare"</code>.
*
* @param value Unkown value.
* @param expectedValue Expected value.
* @see #checkEqual(double,double,String)
*/
public static void checkEqual(double value, double expectedValue) {
checkEqual(value, expectedValue, "compare");
}
/**
* Compared <code>value</code> and <code>expectedValue</code>
* and fails with <code>message</code> if they aren't equal.
*
* @param value Unkown value.
* @param expectedValue Expected value.
* @param msg Message with which to fail.
*/
public static void checkEqual(double value, double expectedValue, String msg) {
if (Double.isNaN(value) && Double.isNaN(expectedValue)) return;
if (value == expectedValue) return;
if (value < expectedValue) {
msg = msg+": "+value+" < "+expectedValue;
} else {
msg = msg+": "+value+" > "+expectedValue;
}
checkFailed(msg);
}
/**
* Compares <code>value</code> and <code>expectedValue</code>
* with failing message <code>"compare"</code>.
*
* @param value Unkown value.
* @param expectedValue Expected value.
* @see #checkEqual(short,short,String)
*/
public static void checkEqual(short value, short expectedValue) {
checkEqual(value, expectedValue, "compare");
}
/**
* Compared <code>value</code> and <code>expectedValue</code>
* and fails with <code>message</code> if they aren't equal.
*
* @param value Unkown value.
* @param expectedValue Expected value.
* @param msg Message with which to fail.
*/
public static void checkEqual(short value, short expectedValue, String msg) {
if (value == expectedValue) return;
if (value < expectedValue) {
msg = msg+": "+value+" < "+expectedValue;
} else {
msg = msg+": "+value+" > "+expectedValue;
}
checkFailed(msg);
}
/**
* Compares <code>value</code> and <code>expectedValue</code>
* with failing message <code>"compare"</code>.
*
* @param value Unkown value.
* @param expectedValue Expected value.
* @see #checkEqual(byte,byte,String)
*/
public static void checkEqual(byte value, byte expectedValue) {
checkEqual(value, expectedValue, "compare");
}
/**
* Compares <code>value</code> and <code>expectedValue</code>
* with failing message <code>msg</code>.
*
* @param value Unkown value.
* @param expectedValue Expected value.
* @param msg Message with which to fail.
*/
public static void checkEqual(byte value, byte expectedValue, String msg) {
if (value == expectedValue) return;
if (value < expectedValue) {
msg = msg+": "+value+" < "+expectedValue;
} else {
msg = msg+": "+value+" > "+expectedValue;
}
checkFailed(msg);
}
/**
* Compares <code>value</code> and <code>expectedValue</code>
* with failing message <code>"compare"</code>.
*
* @param value Unkown value.
* @param expectedValue Expected value.
* @see #checkEqual(char,char,String)
*/
public static void checkEqual(char value, char expectedValue) {
checkEqual(value, expectedValue, "compare");
}
/**
* Compares <code>value</code> and <code>expectedValue</code>
* with failing message <code>msg</code>.
*
* @param value Unkown value.
* @param expectedValue Expected value.
* @param msg Message with which to fail.
*/
public static void checkEqual(char value, char expectedValue, String msg) {
if (value == expectedValue) return;
if (value < expectedValue) {
msg = msg+": "+value+" < "+expectedValue;
} else {
msg = msg+": "+value+" > "+expectedValue;
}
checkFailed(msg);
}
/**
* Compares <code>value</code> and <code>expectedValue</code>
* with failing message <code>"compare"</code>.
*
* @param value Unkown value.
* @param expectedValue Expected value.
* @see #checkEqual(boolean,boolean,String)
*/
public static void checkEqual(boolean value, boolean expectedValue) {
checkEqual(value, expectedValue, "compare");
}
/**
* Compares <code>value</code> and <code>expectedValue</code>
* with failing message <code>msg</code>.
*
* @param value Unkown value.
* @param expectedValue Expected value.
* @param msg Message with which to fail.
*/
public static void checkEqual(boolean value, boolean expectedValue, String msg) {
if (value == expectedValue) return;
msg = msg+": "+value+" != "+expectedValue;
checkFailed(msg);
}
/**
* Checks whether the entries of <code>set</code> are equal
* using <code>equals</code> to the corresponding String in
* <code>expectedSet</code> and fails with message <code>msg</code>.
*
* @param set Unkown set of values.
* @param expectedSet Expected <code>String</code> of values.
* @param msg Message with which to fail.
*/
public static void checkEqual(Collection<String> set, String expectedSet, String msg) {
checkEqual(set, LangUtil.split(expectedSet), msg);
}
/**
* Checks whether the entries of <code>set</code> are equal
* using <code>equals</code> to the corresponding entry in
* <code>expectedSet</code> and fails with message <code>msg</code>,
* except that duplicate actual entries are ignored.
* This issues fail messages for each failure; when
* aborting on failure, only the first will be reported.
*
* @param set Unkown set of values.
* @param expectedSet Expected <code>String</code> of values.
* @param msg Message with which to fail.
*/
public static void checkEqualIgnoreDups(Collection<String> set, String[] expected, String msg,
boolean ignoreDups) {
String[] diffs = diffIgnoreDups(set, expected, msg, ignoreDups);
if (0 < diffs.length) {
check(false, "" + Arrays.asList(diffs));
}
// for (int i = 0; i < diffs.length; i++) {
// check(false, diffs[i]);
// }
}
/** @return String[] of differences '{un}expected msg "..." {not} found' */
private static String[] diffIgnoreDups(Collection<String> set, String[] expected, String msg,
boolean ignoreDups) {
ArrayList<String> result = new ArrayList<>();
List<String> actual = new ArrayList<>(set);
BitSet hits = new BitSet();
for (int i = 0; i < expected.length; i++) {
if (!actual.remove(expected[i])) {
result.add(" expected " + msg + " \"" + expected[i] + "\" not found");
} else {
hits.set(i);
if (ignoreDups) {
while (actual.remove(expected[i])) ; // remove all instances of it
}
}
}
for (String act: actual) {
result.add(" unexpected " + msg + " \"" + act + "\" found");
}
return result.toArray(new String[0]);
}
/**
* Checks whether the entries of <code>set</code> are equal
* using <code>equals</code> to the corresponding entry in
* <code>expectedSet</code> and fails with message <code>msg</code>.
*
* @param set Unkown set of values.
* @param expectedSet Expected <code>String</code> of values.
* @param msg Message with which to fail.
*/
public static void checkEqual(Collection<String> set, String[] expected, String msg) {
checkEqualIgnoreDups(set, expected, msg, false);
}
/**
* Compares <code>value</code> and <code>expectedValue</code>
* with failing message <code>"compare"</code>.
*
* @param value Unkown value.
* @param expectedValue Expected value.
* @see #checkEqual(Object,Object,String)
*/
public static void checkEqual(Object value, Object expectedValue) {
checkEqual(value, expectedValue, "compare");
}
/**
* Checks whether the entries of <code>set</code> are equal
* using <code>equals</code> to the corresponding String in
* <code>expectedSet</code> and fails with message <code>msg</code>.
*
* @param set Unkown set of values.
* @param expectedSet Expected <code>String</code> of values.
* @param msg Message with which to fail.
*/
public static void checkEqual(Object value, Object expectedValue, String msg) {
if (value == null && expectedValue == null) return;
if (value != null && value.equals(expectedValue)) return;
msg = msg+": "+value+" !equals "+expectedValue;
checkFailed(msg);
}
/**
* Checks whether the entries of <code>set</code> are equal
* using <code>equals</code> to the corresponding String in
* <code>expectedSet</code> and fails with message <code>msg</code>.
*
* @param set Unkown set of values.
* @param expectedSet Expected <code>String</code> of values.
* @param msg Message with which to fail.
*/
public static void checkEq(Object value, Object expectedValue, String msg) {
if (value == expectedValue) return;
msg = msg+": "+value+" != "+expectedValue;
checkFailed(msg);
}
/** add expected events */
public static void expectEvent(String s) {
if (null != s) {
expectedEvents.add(s);
}
}
/** add expected events */
public static void expectEvent(Object s) {
if (null != s) {
expectEvent(s.toString());
}
}
/**
* add expected events, parse out ; from string
* Expect those messages in <code>s</code> separated by
* <code>":;, "</code>.
*
* @param s String containg delimited,expected messages.
*/
public static void expectEventsInString(String s) {
if (null != s) {
StringTokenizer tok = new StringTokenizer(s, ":;, ");
while (tok.hasMoreTokens()) {
expectEvent(tok.nextToken());
}
}
}
public static void expectEventsInString(String[] ra) {
expectEvents((Object[]) ra);
}
/** add expected events */
public static void expectEvents(Object[] events) {
if (null != events) {
for (Object event : events) {
if (null != event) {
expectEvent(event.toString());
}
}
}
}
/** add expected events */
public static void expectEvents(String[] events) {
if (null != events) {
for (String event : events) {
if (null != event) {
expectEvent(event.toString());
}
}
}
}
/** check actual and expected have same members */
public static void checkAllEvents() {
checkAndClearEvents(expectedEvents.toArray(new String[0]));
}
/** also ignore duplicate actual entries for expected */
public static void checkAllEventsIgnoreDups() {
final boolean ignoreDups = true;
final String[] exp = expectedEvents.toArray(new String[0]);
checkEqualIgnoreDups(actualEvents, exp, "event", ignoreDups);
clearEvents();
}
/** Check events, file is line-delimited. If there is a non-match, signalls
* a single error for the first event that does not match the next event in
* the file. The equivalence is {@link #checkEqualLists}. Blank lines are
* ignored. lines that start with '//' are ignored. */
public static void checkEventsFromFile(String eventsFile) {
// XXX bug reads into current expected and checks all - separate read and check
try {
File file = new File(getBASEDIR(), eventsFile); // XXX TestDriver
BufferedReader in = new BufferedReader(new FileReader(file));
//final File parentDir = (null == file? null : file.getParentFile());
String line;
List<String> expEvents = new ArrayList<>();
while ((line = in.readLine()) != null) {
line = line.trim();
if ((line.length() < 1) || (line.startsWith("//"))) continue;
expEvents.add(line);
}
in.close();
checkEqualLists(actualEvents, expEvents, " from " + eventsFile);
} catch (IOException ioe) {
throwable(ioe);
}
}
/** Check to see that two lists of strings are the same. Order is important.
* Trimmable whitespace is not important. Case is important.
*
* @param actual one list to check
* @param expected another list
* @param message a context string for the resulting error message if the test fails.
*/
public static void checkEqualLists(List<String> actual, List<String> expected,
String message) {
Iterator<String> a = actual.iterator();
Iterator<String> e = expected.iterator();
int ai = 0;
int ei = 0;
for (; a.hasNext(); ) {
if (! e.hasNext()) {
checkFailed("unexpected [" + ai + "] \"" + a.next() + "\" " + message);
return;
}
String a0 = a.next().trim();
String e0 = e.next().trim();
if (! a0.equals(e0)) {
checkFailed("expected [" + ei + "] \"" + e0
+ "\"\n but found [" + ai + "] \"" + a0 + "\"\n " + message);
return;
}
ai++;
ei++;
}
while (e.hasNext()) {
checkFailed("expected [" + ei + "] \"" + e.next() + "\" " + message);
ei++;
}
}
/** Check events, expEvents is space delimited */
public static void checkEvents(String expEvents) {
checkEqual(actualEvents, expEvents, "event");
}
/** Check events, expEvents is an array */
public static void checkEvents(String[] expEvents) {
checkEqual(actualEvents, expEvents, "event");
}
/** Check events and clear after check*/
public static void checkAndClearEvents(String expEvents) {
checkEvents(expEvents);
clearEvents();
}
/** Check events and clear after check*/
public static void checkAndClearEvents(String[] expEvents) {
checkEvents(expEvents);
clearEvents();
}
/** XXX deprecated */
public static void printEvents() { // XXX no clients?
for (String actualEvent : actualEvents) {
System.out.println(actualEvent); // XXX System.out
}
}
/**
* Report an uncaught exeption as an error
* @param thrown <code>Throwable</code> to print.
* @see #maxStackTrace
*/
public void unexpectedExceptionFailure(Throwable thrown) {
handle("unexpectedExceptionFailure", thrown, true);
}
/**
* Handle message by delegation to message handler, doing
* IMessage.FAIL if (fail || (thrown != null) and IMessage.INFO
* otherwise.
*/
private static void handle(String message, Throwable thrown, boolean fail) {
final boolean failed = fail || (null != thrown);
IMessage.Kind kind = (failed ? IMessage.FAIL : IMessage.INFO);
IMessage m = new Message(message, kind, thrown, null);
/*final boolean handled = */messageHandler.handleMessage(m);
}
// private static void resofhandle(String message, Throwable thrown, boolean fail) {
// /* If FAIL and the message handler returns false (normally),
// * Then this preserves "abort" semantics by throwing an
// * abort exception.
// */
// if (failed) {
// if (handled) {
// String s = "Tester expecting handler to return false or "
// + "complete abruptly when passed a fail, for " + m;
// m = new Message(s, IMessage.DEBUG, null, null);
// messageHandler.handleMessage(m);
// } else {
// throw AbortException.borrowPorter(m);
// }
// }
// }
}
|