/
AssertTest.java
410 lines (343 loc) · 12.7 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
package org.testng;
import com.google.common.collect.ImmutableMap;
import org.testng.annotations.Test;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
import org.testng.collections.Sets;
import testhelper.PerformanceUtils;
import java.util.*;
/**
* This class/interface
*/
public class AssertTest {
@Test
public void nullObjectArrayAssertEquals() {
Object[] expected= null;
Object[] actual= null;
Assert.assertEquals(actual, expected);
}
@Test
public void nullObjectArrayAssertNoOrder() {
Object[] expected= null;
Object[] actual= null;
Assert.assertEqualsNoOrder(actual, expected);
}
@Test
public void nullCollectionAssertEquals() {
Collection expected = null;
Collection actual = null;
Assert.assertEquals(actual, expected);
}
@Test
public void nullSetAssertEquals() {
Set expected = null;
Set actual = null;
Assert.assertEquals(actual, expected);
}
@Test
public void testCollectionAssertEquals() {
final Collection<Asymmetric> expected = Sets.newHashSet(new Asymmetric(10, 'a'), new Asymmetric(11, 'b'));
final Collection<Asymmetric> actual = Sets.newHashSet(new Asymmetric(11, 'b'), new Asymmetric(10, 'a'));
Assert.assertEquals(actual, expected);
}
@Test
public void testListAssertNotEquals() {
final Collection<Asymmetric> expected = Lists.newArrayList(new Asymmetric(10, 'a'), new Asymmetric(11, 'b'));
final Collection<Asymmetric> actual = Lists.newArrayList(new Asymmetric(11, 'b'), new Asymmetric(10, 'a'));
Assert.assertNotEquals(actual, expected);
}
@Test
public void testSetAssertEquals() {
final Set<Asymmetric> expected = Sets.newHashSet(new Asymmetric(10, 'a'), new Asymmetric(11, 'b'));
final Set<Asymmetric> actual = Sets.newHashSet(new Asymmetric(11, 'b'), new Asymmetric(10, 'a'));
Assert.assertEquals(actual, expected);
}
@Test
public void nullMapAssertEquals() {
Map expected = null;
Map actual = null;
Assert.assertEquals(actual, expected);
}
@Test
public void setAssertEquals() {
Set<Object> expected = Sets.newHashSet();
Set<Object> actual = Sets.newHashSet();
expected.add(1);
expected.add("a");
actual.add("a");
actual.add(1);
Assert.assertEquals(actual, expected);
}
@Test
public void mapAssertEquals() {
Map<Object, Object> expected = Maps.newHashMap();
Map<Object, Object> actual = Maps.newHashMap();
expected.put(null, "a");
expected.put("a", "a");
expected.put("b", "c");
actual.put("b", "c");
actual.put(null, "a");
actual.put("a", "a");
Assert.assertEquals(actual, expected);
}
@Test(description = "GITHUB-2211")
public void mapAssertEqualsWithMessage() {
String expected = "User provided message";
String actual = "";
try {
Assert.assertEquals(ImmutableMap.of("k", "v"), ImmutableMap.of(), expected);
} catch (AssertionError error) {
actual = error.getMessage();
}
Assert.assertEquals(actual, expected);
}
@Test
public void oneNullMapAssertEquals() {
Map expected = Maps.newHashMap();
Map actual = null;
try {
Assert.assertEquals(actual, expected);
Assert.fail("AssertEquals didn't fail");
}
catch (AssertionError error) {
//do nothing
}
}
@Test
public void oneNullSetAssertEquals() {
Set expected = null;
Set actual = Sets.newHashSet();
try {
Assert.assertEquals(actual, expected);
Assert.fail("AssertEquals didn't fail");
}
catch (AssertionError error) {
//do nothing
}
}
/**
* Testing comparison algorithm using big arrays.
*
* @see <a href="https://github.com/cbeust/testng/issues/1384">Issue #1384 – Huge performance issue between 6.5.2
* and 6.11</a>
*/
@Test
public void compareLargeArrays() {
int length = 1024 * 100;
byte[] first = new byte[length];
byte[] second = new byte[length];
Random rnd = new Random();
rnd.nextBytes(first);
System.arraycopy(first, 0, second, 0, length);
long before = PerformanceUtils.measureAllocatedMemory();
Assert.assertEquals(first, second);
long memoryUsage = PerformanceUtils.measureAllocatedMemory() - before;
// assertEquals() with primitive type arrays requires ~65Kb of memory
// assertEquals() with Object-type requires ~3Mb of memory when comparing 100Kb arrays.
// choosing 100Kb as a threshold
Assert.assertTrue(memoryUsage < 100 * 1024, "Amount of used memory should be approximately 65Kb");
}
@Test
public void compareShortArrays() {
short[] actual = {Short.MIN_VALUE, 0, Short.MAX_VALUE};
short[] expected = {Short.MIN_VALUE, 0, Short.MAX_VALUE};
Assert.assertEquals(actual, expected);
}
@Test
public void compareIntArrays() {
int[] actual = {Integer.MIN_VALUE, 0, Integer.MAX_VALUE};
int[] expected = {Integer.MIN_VALUE, 0, Integer.MAX_VALUE};
Assert.assertEquals(actual, expected);
}
@Test
public void compareLongArrays() {
long[] actual = {Long.MIN_VALUE, 0, Long.MAX_VALUE};
long[] expected = {Long.MIN_VALUE, 0, Long.MAX_VALUE};
Assert.assertEquals(actual, expected);
}
@Test
public void compareBooleanArrays() {
boolean[] actual = {true, false};
boolean[] expected = {true, false};
Assert.assertEquals(actual, expected);
}
@Test
public void compareCharacterArrays() {
char[] actual = {'a', '1', '#'};
char[] expected = {'a', '1', '#'};
Assert.assertEquals(actual, expected);
}
@Test
public void compareFloatArrays() {
float[] actual = {(float) Math.PI, (float) Math.E, Float.MIN_VALUE, Float.MIN_NORMAL, Float.MAX_VALUE,
Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY};
float[] expected = {(float) Math.PI, (float) Math.E, Float.MIN_VALUE, Float.MIN_NORMAL, Float.MAX_VALUE,
Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY};
Assert.assertEquals(actual, expected);
}
@Test
public void compareFloatArraysWithDelta() {
float[] actual = {0.1f, 0.2f, 0.3f, 0.4f};
float[] expected = {0.5f, 0.7f, 0.1f, 0.2f};
Assert.assertEquals(actual, expected, 0.5f);
}
@Test(expectedExceptions = AssertionError.class)
public void compareUnEqualFloatArraysWithDelta() {
float[] actual = {0.1f, 0.2f, 0.3f, 0.4f};
float[] expected = {0.5f, 0.7f, 0.1f, 0.2f};
Assert.assertEquals(actual, expected, 0.1f);
}
@Test
public void compareFloatArraysWithNaNValues() {
Assert.assertEquals(new float[] { Float.NaN }, new float[] { Float.NaN });
}
@Test
public void compareFloatWithNaNValues() {
Assert.assertEquals(Float.NaN, Float.NaN);
}
@Test
public void compareDoubleArrays() {
double[] actual = {Math.PI, Math.E, Double.MIN_VALUE, Double.MIN_NORMAL, Double.MAX_VALUE,
Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY};
double[] expected = {Math.PI, Math.E, Double.MIN_VALUE, Double.MIN_NORMAL, Double.MAX_VALUE,
Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY};
Assert.assertEquals(actual, expected);
}
@Test
public void compareDoubleArraysWithNaNValues() {
Assert.assertEquals(new double[] { Double.NaN }, new double[] { Double.NaN });
}
@Test
public void compareDoubleWithNaNValues() {
Assert.assertEquals(Double.NaN, Double.NaN);
}
@Test
public void compareDoubleArraysWithDelta() {
double[] actual = {0.1d, 0.2d, 0.3d, 0.4d};
double[] expected = {0.5d, 0.7d, 0.1d, 0.2d};
Assert.assertEquals(actual, expected, 0.5d);
}
@Test(expectedExceptions = AssertionError.class)
public void compareUnEqualDoubleArraysWithDelta() {
double[] actual = {0.1d, 0.2d, 0.3d, 0.4d};
double[] expected = {0.5d, 0.7d, 0.1d, 0.2d};
Assert.assertEquals(actual, expected, 0.1d);
}
@Test(expectedExceptions = AssertionError.class)
public void assertEqualsMapShouldFail() {
Map<String, String> mapActual = new HashMap<String, String>() {{
put("a","1");
}};
Map<String, String> mapExpected = new HashMap<String, String>() {{
put("a","1");
put("b","2");
}};
Assert.assertEquals(mapActual, mapExpected);
}
@Test(expectedExceptions = AssertionError.class)
public void assertEqualsSymmetricScalar() {
Assert.assertEquals(new Asymmetric(42, 'd'), new Contrived(42));
}
@Test(expectedExceptions = AssertionError.class)
public void assertEqualsSymmetricArrays() {
Object[] actual = {1, new Asymmetric(42, 'd'), "inDay"};
Object[] expected = {1, new Contrived(42), "inDay"};
Assert.assertEquals(actual, expected);
}
@Test(description = "GITHUB-1935", expectedExceptions = AssertionError.class,
expectedExceptionsMessageRegExp = "expected \\[y\\] but found \\[x\\]")
public void testInequalityMessage() {
Assert.assertEquals("x", "y");
}
@Test(description = "GITHUB-2080", expectedExceptions = AssertionError.class,
expectedExceptionsMessageRegExp = "test expected \\[true\\] but found \\[false\\]")
public void testAssertTrueMessage() {
Assert.assertTrue(false, "test");
}
@Test(description = "GITHUB-2080", expectedExceptions = AssertionError.class,
expectedExceptionsMessageRegExp = "test expected \\[false\\] but found \\[true\\]")
public void testAssertFalseMessage() {
Assert.assertFalse(true, "test");
}
@Test(description = "GITHUB-2080", expectedExceptions = AssertionError.class,
expectedExceptionsMessageRegExp = "did not expect \\[x\\] but found \\[x\\]")
public void testEqualityMessage() {
Assert.assertNotEquals("x", "x");
}
@Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class,
expectedExceptionsMessageRegExp = "did not expect \\[1.5\\] but found \\[1.5\\]")
public void testAssertNotEqualsFloat() {
Assert.assertNotEquals(1.5f, 1.5f, 0.5f);
}
@Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class,
expectedExceptionsMessageRegExp = "did not expect \\[1.5\\] but found \\[1.5\\]")
public void testAssertNotEqualsDouble() {
Assert.assertNotEquals(1.5, 1.5, 0.5);
}
@Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class,
expectedExceptionsMessageRegExp = "did not expect \\[\\[test\\]\\] but found \\[\\[test\\]\\]")
public void testAssertNotEqualsSet() {
Set<String> set = new HashSet<>();
set.add("test");
Assert.assertNotEquals(set, set);
}
@Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class,
expectedExceptionsMessageRegExp = "did not expect \\[\\[test\\]\\] but found \\[\\[test\\]\\]")
public void testAssertNotEqualsDeepSet() {
Set<String> set = new HashSet<>();
set.add("test");
Assert.assertNotEqualsDeep(set, set);
}
@Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class,
expectedExceptionsMessageRegExp = "did not expect \\[\\{key=value\\}\\] but found \\[\\{key=value\\}\\]")
public void testAssertNotEqualsMap() {
Map<String, String> map = new HashMap<>();
map.put("key", "value");
Assert.assertNotEquals(map, map);
}
@Test(description = "GITHUB-2118", expectedExceptions = AssertionError.class,
expectedExceptionsMessageRegExp = "did not expect \\[\\{key=value\\}\\] but found \\[\\{key=value\\}\\]")
public void testAssertNotEqualsDeepMap() {
Map<String, String> map = new HashMap<>();
map.put("key", "value");
Assert.assertNotEqualsDeep(map, map);
}
class Contrived {
int integer;
Contrived(int integer){
this.integer = integer;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Contrived)) return false;
Contrived contrived = (Contrived) o;
return integer == contrived.integer;
}
@Override
public int hashCode() {
return integer;
}
}
class Asymmetric extends Contrived {
char character;
Asymmetric(int integer, char character) {
super(integer);
this.character = character;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Asymmetric)) return false;
if (!super.equals(o)) return false;
Asymmetric that = (Asymmetric) o;
return character == that.character;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (int) character;
return result;
}
}
}