forked from testng-team/testng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssertTest.java
483 lines (377 loc) · 13.6 KB
/
AssertTest.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
package test.asserttests;
import static org.testng.Assert.*;
import java.io.IOException;
import java.util.*;
import org.testng.Assert;
import org.testng.Assert.ThrowingRunnable;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.collections.Sets;
public class AssertTest {
@Test(description = "GITHUB-2652")
public void assertEqualsBoxedUnboxedDouble() {
Double a = 3.14;
double b = 3.14;
Double deltaA = 0.1;
double deltaB = 0.1;
assertEquals(a, b, deltaA);
assertEquals(a, b, deltaB);
assertEquals(a, b);
assertEquals(a, b, "");
assertEquals(b, a);
assertEquals(b, a, "");
}
@Test(description = "GITHUB-2652")
public void assertEqualsBoxedUnboxedFloat() {
Float a = 3.1f;
float b = 3.1f;
Float deltaA = 0.1f;
float deltaB = 0.1f;
assertEquals(a, b, deltaA);
assertEquals(a, b, deltaB);
assertEquals(a, b);
assertEquals(a, b, "");
assertEquals(b, a);
assertEquals(b, a, "");
}
@Test(description = "GITHUB-2652")
public void assertEqualsBoxedUnboxedLong() {
Long a = 3L;
long b = 3L;
Long deltaA = 1L;
long deltaB = 1L;
assertEquals(a, b, deltaA);
assertEquals(a, b, deltaB);
assertEquals(a, b);
assertEquals(a, b, "");
assertEquals(b, a);
assertEquals(b, a, "");
}
@Test(description = "GITHUB-2652")
public void assertEqualsBoxedUnboxedBoolean() {
Boolean a = true;
boolean b = true;
assertEquals(a, b);
assertEquals(a, b, "");
assertEquals(b, a);
assertEquals(b, a, "");
}
@Test(description = "GITHUB-2652")
public void assertEqualsBoxedUnboxedByte() {
Byte a = Byte.valueOf("3");
byte b = (byte) 3;
assertEquals(a, b);
assertEquals(a, b, "");
assertEquals(b, a);
assertEquals(b, a, "");
}
@Test(description = "GITHUB-2652")
public void assertEqualsBoxedUnboxedChar() {
Character a = 'a';
char b = 'a';
assertEquals(a, b);
assertEquals(a, b, "");
assertEquals(b, a);
assertEquals(b, a, "");
}
@Test(description = "GITHUB-2652")
public void assertEqualsBoxedUnboxedShort() {
Short a = Short.valueOf("3");
short b = (short) 3;
Short deltaA = Short.valueOf("3");
short deltaB = (short) 1;
assertEquals(a, b, deltaA);
assertEquals(a, b, deltaB);
assertEquals(a, b);
assertEquals(a, b, "");
assertEquals(b, a);
assertEquals(b, a, "");
}
@Test(description = "GITHUB-2652")
public void assertEqualsBoxedUnboxedInteger() {
Integer a = 3;
int b = 3;
Integer deltaA = 1;
int deltaB = 1;
assertEquals(a, b, deltaA);
assertEquals(a, b, deltaB);
assertEquals(a, b);
assertEquals(a, b, "");
assertEquals(b, a);
assertEquals(b, a, "");
}
@Test
public void noOrderSuccess() {
String[] rto1 = {
"boolean", "BigInteger", "List",
};
String[] rto2 = {
"List", "BigInteger", "boolean",
};
Assert.assertEqualsNoOrder(rto1, rto2);
}
@Test(expectedExceptions = AssertionError.class)
public void noOrderFailure() {
String[] rto1 = {
"a", "a", "b",
};
String[] rto2 = {
"a", "b", "b",
};
Assert.assertEqualsNoOrder(rto1, rto2);
}
@Test
public void intArray_Issue4() {
int[] intArr00 = {1};
int[] intArr01 = {1};
assertEquals(intArr00, intArr01);
}
@Test(expectedExceptions = AssertionError.class)
public void arraysFailures_1() {
int[] intArr = {1, 2};
long[] longArr = {1, 2};
assertEquals(intArr, longArr);
}
@Test(expectedExceptions = AssertionError.class)
public void arraysFailures_2() {
int[] intArr = {1, 2};
assertEquals(intArr, (long) 1);
}
@Test(expectedExceptions = AssertionError.class)
public void arraysFailures_3() {
long[] longArr = {1};
assertEquals((long) 1, longArr);
}
@Test
public void setsSuccess() {
Set<Integer> set1 = Sets.newHashSet();
Set<Integer> set2 = Sets.newHashSet();
set1.add(1);
set2.add(1);
set1.add(3);
set2.add(3);
set1.add(2);
set2.add(2);
assertEquals(set1, set2);
assertEquals(set2, set1);
}
@Test(expectedExceptions = AssertionError.class)
public void expectThrowsRequiresAnExceptionToBeThrown() {
expectThrows(Throwable.class, nonThrowingRunnable());
}
@Test
public void expectThrowsIncludesAnInformativeDefaultMessage() {
try {
expectThrows(Throwable.class, nonThrowingRunnable());
} catch (AssertionError ex) {
assertEquals("Expected Throwable to be thrown, but nothing was thrown", ex.getMessage());
return;
}
fail();
}
@Test
public void expectThrowsReturnsTheSameObjectThrown() {
NullPointerException npe = new NullPointerException();
Throwable throwable = expectThrows(Throwable.class, throwingRunnable(npe));
assertSame(npe, throwable);
}
@Test(expectedExceptions = AssertionError.class)
public void expectThrowsDetectsTypeMismatchesViaExplicitTypeHint() {
NullPointerException npe = new NullPointerException();
expectThrows(IOException.class, throwingRunnable(npe));
}
@Test
public void expectThrowsWrapsAndPropagatesUnexpectedExceptions() {
NullPointerException npe = new NullPointerException("inner-message");
try {
expectThrows(IOException.class, throwingRunnable(npe));
} catch (AssertionError ex) {
assertSame(npe, ex.getCause());
assertEquals("inner-message", ex.getCause().getMessage());
return;
}
fail();
}
@Test
public void expectThrowsSuppliesACoherentErrorMessageUponTypeMismatch() {
NullPointerException npe = new NullPointerException();
try {
expectThrows(IOException.class, throwingRunnable(npe));
} catch (AssertionError error) {
assertEquals(
"Expected IOException to be thrown, but NullPointerException was thrown",
error.getMessage());
assertSame(npe, error.getCause());
return;
}
fail();
}
private static ThrowingRunnable nonThrowingRunnable() {
return () -> {};
}
private static ThrowingRunnable throwingRunnable(final Throwable t) {
return () -> {
throw t;
};
}
@Test
public void doubleNaNAssertion() {
assertEquals(Double.NaN, Double.NaN, 0.0);
}
@DataProvider
public Object[][] identicalArraysWithNull() {
return new Object[][] {
{new String[] {"foo", "bar", null}, new String[] {"foo", "bar", null}},
{new String[] {"foo", null, "bar"}, new String[] {"foo", null, "bar"}},
{new String[] {null, "foo", "bar"}, new String[] {null, "foo", "bar"}}
};
}
@Test(dataProvider = "identicalArraysWithNull")
public void identicalArraysWithNullValues(String[] actual, String[] expected) {
assertEquals(actual, expected);
}
@DataProvider
public Object[][] nonIdenticalArraysWithNull() {
return new Object[][] {
{new String[] {"foo", "bar", null}, new String[] {"foo", "bar", "not-null"}},
{new String[] {"foo", "not-null", "bar"}, new String[] {"foo", null, "bar"}},
{new String[] {null, "foo", "bar"}, new String[] {" not-null", "foo", "bar"}}
};
}
@Test(dataProvider = "nonIdenticalArraysWithNull")
public void nonIdenticalarrayWithNullValue(String[] actual, String[] expected) {
Assert.assertNotEquals(actual, expected);
}
@Test(description = "GITHUB-2540", expectedExceptions = AssertionError.class)
public void checkCollectionEqualsFailsWhenDifferentOrder() {
Collection<String> collection1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
Collection<String> collection2 = new LinkedHashSet<>(Arrays.asList("a", "c", "b"));
assertEquals(collection1, collection2);
}
@Test(description = "GITHUB-2540")
public void checkCollectionEqualsNoOrder() {
Collection<String> collection1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
Collection<String> collection2 = new LinkedHashSet<>(Arrays.asList("a", "c", "b"));
assertEqualsNoOrder(collection1, collection2);
}
@Test(description = "GITHUB-2540")
public void checkCollectionEquals() {
Collection<String> collection1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
Collection<String> collection2 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
assertEquals(collection1, collection2);
}
@Test(description = "GITHUB-2540")
public void checkCollectionNotEquals() {
Collection<String> collection1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
Collection<String> collection2 = new LinkedHashSet<>(Arrays.asList("a", "c", "b"));
assertNotEquals(collection1, collection2);
}
@Test(description = "GITHUB-2540", expectedExceptions = AssertionError.class)
public void checkCollectionNotEqualsFailsWhenDifferentOrder() {
Collection<String> collection1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
Collection<String> collection2 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
assertNotEquals(collection1, collection2);
}
@Test(description = "GITHUB-2540", expectedExceptions = AssertionError.class)
public void checkIteratorEqualsFailsWhenDifferentOrder() {
Iterator<String> iterator1 = (new LinkedHashSet<>(Arrays.asList("a", "b", "c"))).iterator();
Iterator<String> iterator2 = (new LinkedHashSet<>(Arrays.asList("a", "c", "b"))).iterator();
assertEquals(iterator1, iterator2);
}
@Test(description = "GITHUB-2540")
public void checkIteratorEqualsNoOrder() {
Iterator<String> iterator1 = (new LinkedHashSet<>(Arrays.asList("a", "b", "c"))).iterator();
Iterator<String> iterator2 = (new LinkedHashSet<>(Arrays.asList("a", "c", "b"))).iterator();
assertEqualsNoOrder(iterator1, iterator2);
}
@Test(description = "GITHUB-2540")
public void checkIteratorEquals() {
Iterator<String> iterator1 = (new LinkedHashSet<>(Arrays.asList("a", "b", "c"))).iterator();
Iterator<String> iterator2 = (new LinkedHashSet<>(Arrays.asList("a", "b", "c"))).iterator();
assertEquals(iterator1, iterator2);
}
@Test(description = "GITHUB-2540")
public void checkIteratorNotEquals() {
Iterator<String> iterator1 = (new LinkedHashSet<>(Arrays.asList("a", "b", "c"))).iterator();
Iterator<String> iterator2 = (new LinkedHashSet<>(Arrays.asList("a", "c", "b"))).iterator();
assertNotEquals(iterator1, iterator2);
}
@Test(description = "GITHUB-2540", expectedExceptions = AssertionError.class)
public void checkIteratorNotEqualsFailsWhenDifferentOrder() {
Iterator<String> iterator1 = (new LinkedHashSet<>(Arrays.asList("a", "b", "c"))).iterator();
Iterator<String> iterator2 = (new LinkedHashSet<>(Arrays.asList("a", "b", "c"))).iterator();
assertNotEquals(iterator1, iterator2);
}
@Test(description = "GITHUB-2643")
public void checkSetEqualsWhenDifferentOrder() {
Set<String> set1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
Set<String> set2 = new LinkedHashSet<>(Arrays.asList("a", "c", "b"));
assertEquals(set1, set2);
}
@Test(description = "GITHUB-2540")
public void checkSetEqualsNoOrder() {
Set<String> set1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
Set<String> set2 = new LinkedHashSet<>(Arrays.asList("a", "c", "b"));
assertEqualsNoOrder(set1, set2);
}
@Test(description = "GITHUB-2540")
public void checkSetEquals() {
Set<String> set1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
Set<String> set2 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
assertEquals(set1, set2);
}
@Test(description = "GITHUB-2643", expectedExceptions = AssertionError.class)
public void checkSetNotEqualsFailsWhenDifferentOrder() {
Set<String> set1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
Set<String> set2 = new LinkedHashSet<>(Arrays.asList("a", "c", "b"));
assertNotEquals(set1, set2);
}
@Test(description = "GITHUB-2643", expectedExceptions = AssertionError.class)
public void checkSetNotEqualsFailsWhenSameOrder() {
Set<String> set1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
Set<String> set2 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
assertNotEquals(set1, set2);
}
@Test(description = "GITHUB-2643")
public void checkSetNotEqualsWhenNotSameSets() {
Set<String> set1 = new LinkedHashSet<>(Arrays.asList("a", "b"));
Set<String> set2 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
assertNotEquals(set1, set2);
}
@Test(description = "GITHUB-2643", expectedExceptions = AssertionError.class)
public void checkSetEqualsFailsWhenNotSameSets() {
Set<String> set1 = new LinkedHashSet<>(Arrays.asList("a", "b", "c"));
Set<String> set2 = new LinkedHashSet<>(Arrays.asList("a", "b"));
assertEquals(set1, set2);
}
@Test(description = "GITHUB-2540", expectedExceptions = AssertionError.class)
public void checkArrayEqualsFailsWhenDifferentOrder() {
String[] array1 = {"a", "b", "c"};
String[] array2 = {"a", "c", "b"};
assertEquals(array1, array2);
}
@Test(description = "GITHUB-2540")
public void checkArrayEqualsNoOrder() {
String[] array1 = {"a", "b", "c"};
String[] array2 = {"a", "c", "b"};
assertEqualsNoOrder(array1, array2);
}
@Test(description = "GITHUB-2540")
public void checkArrayEquals() {
String[] array1 = {"a", "b", "c"};
String[] array2 = {"a", "b", "c"};
assertEquals(array1, array2);
}
@Test(description = "GITHUB-2540")
public void checkArrayNotEquals() {
String[] array1 = {"a", "b", "c"};
String[] array2 = {"a", "c", "b"};
assertNotEquals(array1, array2);
}
@Test(description = "GITHUB-2540", expectedExceptions = AssertionError.class)
public void checkArrayNotEqualsFailsWhenDifferentOrder() {
String[] array1 = {"a", "b", "c"};
String[] array2 = {"a", "b", "c"};
assertNotEquals(array1, array2);
}
}