1package org.testng.asserts;
2
3import java.util.Collection;
4import java.util.Map;
5import java.util.Set;
6
7/**
8 * An assert class with various hooks allowing its behavior to be modified
9 * by subclasses.
10 */
11public class Assertion implements IAssertLifecycle {
12  protected void doAssert(IAssert<?> assertCommand) {
13    onBeforeAssert(assertCommand);
14    try {
15      executeAssert(assertCommand);
16      onAssertSuccess(assertCommand);
17    } catch(AssertionError ex) {
18      onAssertFailure(assertCommand, ex);
19      throw ex;
20    } finally {
21      onAfterAssert(assertCommand);
22    }
23  }
24
25  /**
26   * Run the assert command in parameter. Meant to be overridden by subclasses.
27   */
28  @Override
29  public void executeAssert(IAssert<?> assertCommand) {
30    assertCommand.doAssert();
31  }
32
33  /**
34   * Invoked when an assert succeeds. Meant to be overridden by subclasses.
35   */
36  @Override
37  public void onAssertSuccess(IAssert<?> assertCommand) {
38  }
39
40  /**
41   * Invoked when an assert fails. Meant to be overridden by subclasses.
42   *
43   * @deprecated use onAssertFailure(IAssert assertCommand, AssertionError ex) instead of.
44   */
45  @Deprecated
46  @Override
47  public void onAssertFailure(IAssert<?> assertCommand) {
48  }
49
50  @Override
51  public void onAssertFailure(IAssert<?> assertCommand, AssertionError ex) {
52      onAssertFailure(assertCommand);
53  }
54
55  /**
56   * Invoked before an assert is run. Meant to be overridden by subclasses.
57   */
58  @Override
59  public void onBeforeAssert(IAssert<?> assertCommand) {
60  }
61
62  /**
63   * Invoked after an assert is run. Meant to be overridden by subclasses.
64   */
65  @Override
66  public void onAfterAssert(IAssert<?> assertCommand) {
67  }
68
69  abstract private static class SimpleAssert<T> implements IAssert<T> {
70    private final T actual;
71    private final T expected;
72    private final String m_message;
73
74    public SimpleAssert(String message) {
75      this(null, null, message);
76    }
77
78    public SimpleAssert(T actual, T expected) {
79      this(actual, expected, null);
80    }
81
82    public SimpleAssert(T actual, T expected, String message) {
83      this.actual = actual;
84      this.expected = expected;
85      m_message = message;
86    }
87
88    @Override
89    public String getMessage() {
90      return m_message;
91    }
92
93    @Override
94    public T getActual() {
95        return actual;
96    }
97
98    @Override
99    public T getExpected() {
100        return expected;
101    }
102
103    @Override
104    abstract public void doAssert();
105  }
106
107
108  public void assertTrue(final boolean condition, final String message) {
109    doAssert(new SimpleAssert<Boolean>(condition, Boolean.TRUE, message) {
110      @Override
111      public void doAssert() {
112        org.testng.Assert.assertTrue(condition, message);
113      }
114    });
115  }
116
117	public void assertTrue(final boolean condition) {
118		doAssert(new SimpleAssert<Boolean>(condition, Boolean.TRUE) {
119			@Override
120			public void doAssert() {
121				org.testng.Assert.assertTrue(condition);
122			}
123		});
124	}
125
126  public void assertFalse(final boolean condition, final String message) {
127    doAssert(new SimpleAssert<Boolean>(condition, Boolean.FALSE, message) {
128      @Override
129      public void doAssert() {
130        org.testng.Assert.assertFalse(condition, message);
131      }
132    });
133  }
134
135  public void assertFalse(final boolean condition) {
136    doAssert(new SimpleAssert<Boolean>(condition, Boolean.FALSE) {
137      @Override
138      public void doAssert() {
139        org.testng.Assert.assertFalse(condition);
140      }
141    });
142  }
143
144  public void fail(final String message, final Throwable realCause) {
145    doAssert(new SimpleAssert<Object>(message) {
146      @Override
147      public void doAssert() {
148        org.testng.Assert.fail(message, realCause);
149      }
150    });
151  }
152
153  public void fail(final String message) {
154    doAssert(new SimpleAssert<Object>(message) {
155      @Override
156      public void doAssert() {
157        org.testng.Assert.fail(message);
158      }
159    });
160  }
161
162  public void fail() {
163    doAssert(new SimpleAssert<Object>(null) {
164      @Override
165      public void doAssert() {
166        org.testng.Assert.fail();
167      }
168    });
169  }
170
171  public <T> void assertEquals(final T actual, final T expected, final String message) {
172    doAssert(new SimpleAssert<T>(actual, expected, message) {
173      @Override
174      public void doAssert() {
175        org.testng.Assert.assertEquals(actual, expected, message);
176      }
177    });
178  }
179
180  public <T> void assertEquals(final T actual, final T expected) {
181    doAssert(new SimpleAssert<T>(actual, expected) {
182      @Override
183      public void doAssert() {
184        org.testng.Assert.assertEquals(actual, expected);
185      }
186    });
187  }
188
189  public void assertEquals(final String actual, final String expected, final String message) {
190    doAssert(new SimpleAssert<String>(actual, expected, message) {
191      @Override
192      public void doAssert() {
193        org.testng.Assert.assertEquals(actual, expected, message);
194      }
195    });
196  }
197  public void assertEquals(final String actual, final String expected) {
198    doAssert(new SimpleAssert<String>(actual, expected) {
199      @Override
200      public void doAssert() {
201        org.testng.Assert.assertEquals(actual, expected);
202      }
203    });
204  }
205
206  public void assertEquals(final double actual, final double expected, final double delta,
207      final String message) {
208    doAssert(new SimpleAssert<Double>(actual, expected, message) {
209      @Override
210      public void doAssert() {
211        org.testng.Assert.assertEquals(actual, expected, delta, message);
212      }
213    });
214  }
215
216  public void assertEquals(final double actual, final double expected, final double delta) {
217    doAssert(new SimpleAssert<Double>(actual, expected) {
218      @Override
219      public void doAssert() {
220        org.testng.Assert.assertEquals(actual, expected, delta);
221      }
222    });
223  }
224
225  public void assertEquals(final float actual, final float expected, final float delta,
226      final String message) {
227    doAssert(new SimpleAssert<Float>(actual, expected, message) {
228      @Override
229      public void doAssert() {
230        org.testng.Assert.assertEquals(actual, expected, delta, message);
231      }
232    });
233  }
234
235  public void assertEquals(final float actual, final float expected, final float delta) {
236    doAssert(new SimpleAssert<Float>(actual, expected) {
237      @Override
238      public void doAssert() {
239        org.testng.Assert.assertEquals(actual, expected, delta);
240      }
241    });
242  }
243
244  public void assertEquals(final long actual, final long expected, final String message) {
245    doAssert(new SimpleAssert<Long>(actual, expected, message) {
246      @Override
247      public void doAssert() {
248        org.testng.Assert.assertEquals(actual, expected, message);
249      }
250    });
251  }
252
253  public void assertEquals(final long actual, final long expected) {
254    doAssert(new SimpleAssert<Long>(actual, expected) {
255      @Override
256      public void doAssert() {
257        org.testng.Assert.assertEquals(actual, expected);
258      }
259    });
260  }
261
262  public void assertEquals(final boolean actual, final boolean expected, final String message) {
263    doAssert(new SimpleAssert<Boolean>(actual, expected, message) {
264      @Override
265      public void doAssert() {
266        org.testng.Assert.assertEquals(actual, expected, message);
267      }
268    });
269  }
270
271  public void assertEquals(final boolean actual, final boolean expected) {
272    doAssert(new SimpleAssert<Boolean>(actual, expected) {
273      @Override
274      public void doAssert() {
275        org.testng.Assert.assertEquals(actual, expected);
276      }
277    });
278  }
279
280  public void assertEquals(final byte actual, final byte expected, final String message) {
281    doAssert(new SimpleAssert<Byte>(actual, expected, message) {
282      @Override
283      public void doAssert() {
284        org.testng.Assert.assertEquals(actual, expected, message);
285      }
286    });
287  }
288
289  public void assertEquals(final byte actual, final byte expected) {
290    doAssert(new SimpleAssert<Byte>(actual, expected) {
291      @Override
292      public void doAssert() {
293        org.testng.Assert.assertEquals(actual, expected);
294      }
295    });
296  }
297
298  public void assertEquals(final char actual, final char expected, final String message) {
299    doAssert(new SimpleAssert<Character>(actual, expected, message) {
300      @Override
301      public void doAssert() {
302        org.testng.Assert.assertEquals(actual, expected, message);
303      }
304    });
305  }
306
307  public void assertEquals(final char actual, final char expected) {
308    doAssert(new SimpleAssert<Character>(actual, expected) {
309      @Override
310      public void doAssert() {
311        org.testng.Assert.assertEquals(actual, expected);
312      }
313    });
314  }
315
316  public void assertEquals(final short actual, final short expected, final String message) {
317    doAssert(new SimpleAssert<Short>(actual, expected, message) {
318      @Override
319      public void doAssert() {
320        org.testng.Assert.assertEquals(actual, expected, message);
321      }
322    });
323  }
324
325  public void assertEquals(final short actual, final short expected) {
326    doAssert(new SimpleAssert<Short>(actual, expected) {
327      @Override
328      public void doAssert() {
329        org.testng.Assert.assertEquals(actual, expected);
330      }
331    });
332  }
333
334  public void assertEquals(final int actual, final  int expected, final String message) {
335    doAssert(new SimpleAssert<Integer>(actual, expected, message) {
336      @Override
337      public void doAssert() {
338        org.testng.Assert.assertEquals(actual, expected, message);
339      }
340    });
341  }
342
343  public void assertEquals(final int actual, final int expected) {
344    doAssert(new SimpleAssert<Integer>(actual, expected) {
345      @Override
346      public void doAssert() {
347        org.testng.Assert.assertEquals(actual, expected);
348      }
349    });
350  }
351
352  public void assertNotNull(final Object object) {
353    doAssert(new SimpleAssert<Object>(object, null) {
354      @Override
355      public void doAssert() {
356        org.testng.Assert.assertNotNull(object);
357      }
358    });
359  }
360
361  public void assertNotNull(final Object object, final String message) {
362    doAssert(new SimpleAssert<Object>(object, null, message) {
363      @Override
364      public void doAssert() {
365        org.testng.Assert.assertNotNull(object, message);
366      }
367    });
368  }
369
370  public void assertNull(final Object object) {
371    doAssert(new SimpleAssert<Object>(object, null) {
372      @Override
373      public void doAssert() {
374        org.testng.Assert.assertNull(object);
375      }
376    });
377  }
378
379  public void assertNull(final Object object, final String message) {
380    doAssert(new SimpleAssert<Object>(object, null, message) {
381      @Override
382      public void doAssert() {
383        org.testng.Assert.assertNull(object, message);
384      }
385    });
386  }
387
388  public void assertSame(final Object actual, final Object expected, final String message) {
389    doAssert(new SimpleAssert<Object>(actual, expected, message) {
390      @Override
391      public void doAssert() {
392        org.testng.Assert.assertSame(actual, expected, message);
393      }
394    });
395  }
396
397  public void assertSame(final Object actual, final Object expected) {
398    doAssert(new SimpleAssert<Object>(actual, expected) {
399      @Override
400      public void doAssert() {
401        org.testng.Assert.assertSame(actual, expected);
402      }
403    });
404  }
405
406  public void assertNotSame(final Object actual, final Object expected, final String message) {
407    doAssert(new SimpleAssert<Object>(actual, expected, message) {
408      @Override
409      public void doAssert() {
410        org.testng.Assert.assertNotSame(actual, expected, message);
411      }
412    });
413  }
414
415  public void assertNotSame(final Object actual, final Object expected) {
416    doAssert(new SimpleAssert<Object>(actual, expected) {
417      @Override
418      public void doAssert() {
419        org.testng.Assert.assertNotSame(actual, expected);
420      }
421    });
422  }
423
424  public void assertEquals(final Collection<?> actual, final Collection<?> expected) {
425    doAssert(new SimpleAssert<Collection<?>>(actual, expected) {
426      @Override
427      public void doAssert() {
428        org.testng.Assert.assertEquals(actual, expected);
429      }
430    });
431  }
432
433  public void assertEquals(final Collection<?> actual, final Collection<?> expected,
434      final String message) {
435    doAssert(new SimpleAssert<Collection<?>>(actual, expected, message) {
436      @Override
437      public void doAssert() {
438        org.testng.Assert.assertEquals(actual, expected, message);
439      }
440    });
441  }
442
443  public void assertEquals(final Object[] actual, final Object[] expected, final String message) {
444    doAssert(new SimpleAssert<Object[]>(actual, expected, message) {
445      @Override
446      public void doAssert() {
447        org.testng.Assert.assertEquals(actual, expected, message);
448      }
449    });
450  }
451
452  public void assertEqualsNoOrder(final Object[] actual, final Object[] expected,
453      final String message) {
454    doAssert(new SimpleAssert<Object[]>(actual, expected, message) {
455      @Override
456      public void doAssert() {
457        org.testng.Assert.assertEqualsNoOrder(actual, expected, message);
458      }
459    });
460  }
461
462  public void assertEquals(final Object[] actual, final Object[] expected) {
463    doAssert(new SimpleAssert<Object[]>(actual, expected) {
464      @Override
465      public void doAssert() {
466        org.testng.Assert.assertEquals(actual, expected);
467      }
468    });
469  }
470
471  public void assertEqualsNoOrder(final Object[] actual, final Object[] expected) {
472    doAssert(new SimpleAssert<Object[]>(actual, expected) {
473      @Override
474      public void doAssert() {
475        org.testng.Assert.assertEqualsNoOrder(actual, expected);
476      }
477    });
478  }
479
480  public void assertEquals(final byte[] actual, final byte[] expected) {
481    doAssert(new SimpleAssert<byte[]>(actual, expected) {
482      @Override
483      public void doAssert() {
484        org.testng.Assert.assertEquals(actual, expected);
485      }
486    });
487  }
488
489  public void assertEquals(final byte[] actual, final byte[] expected,
490      final String message) {
491    doAssert(new SimpleAssert<byte[]>(actual, expected, message) {
492      @Override
493      public void doAssert() {
494        org.testng.Assert.assertEquals(actual, expected, message);
495      }
496    });
497  }
498
499  public void assertEquals(final Set<?> actual, final Set<?> expected) {
500    doAssert(new SimpleAssert<Set<?>>(actual, expected) {
501      @Override
502      public void doAssert() {
503        org.testng.Assert.assertEquals(actual, expected);
504      }
505    });
506  }
507
508  public void assertEquals(final Set<?> actual, final Set<?> expected, final String message) {
509    doAssert(new SimpleAssert<Set<?>>(actual, expected, message) {
510      @Override
511      public void doAssert() {
512        org.testng.Assert.assertEquals(actual, expected, message);
513      }
514    });
515  }
516
517  public void assertEquals(final Map<?, ?> actual, final Map<?, ?> expected) {
518    doAssert(new SimpleAssert<Map<?, ?>>(actual, expected) {
519      @Override
520      public void doAssert() {
521        org.testng.Assert.assertEquals(actual, expected);
522      }
523    });
524  }
525
526  public  void assertNotEquals(final Object actual, final Object expected, final String message) {
527    doAssert(new SimpleAssert<Object>(actual, expected, message) {
528      @Override
529      public void doAssert() {
530        org.testng.Assert.assertNotEquals(actual, expected, message);
531      }
532    });
533  }
534
535  public void assertNotEquals(final Object actual, final Object expected) {
536    doAssert(new SimpleAssert<Object>(actual, expected) {
537      @Override
538      public void doAssert() {
539        org.testng.Assert.assertNotEquals(actual, expected);
540      }
541    });
542  }
543
544  void assertNotEquals(final String actual, final String expected, final String message) {
545    doAssert(new SimpleAssert<String>(actual, expected, message) {
546      @Override
547      public void doAssert() {
548        org.testng.Assert.assertNotEquals(actual, expected, message);
549      }
550    });
551  }
552
553  void assertNotEquals(final String actual, final String expected) {
554    doAssert(new SimpleAssert<String>(actual, expected) {
555      @Override
556      public void doAssert() {
557        org.testng.Assert.assertNotEquals(actual, expected);
558      }
559    });
560  }
561
562  void assertNotEquals(final long actual, final long expected, final String message) {
563    doAssert(new SimpleAssert<Long>(actual, expected, message) {
564      @Override
565      public void doAssert() {
566        org.testng.Assert.assertNotEquals(actual, expected, message);
567      }
568    });
569  }
570
571  void assertNotEquals(final long actual, final long expected) {
572    doAssert(new SimpleAssert<Long>(actual, expected) {
573      @Override
574      public void doAssert() {
575        org.testng.Assert.assertNotEquals(actual, expected);
576      }
577    });
578  }
579
580  void assertNotEquals(final boolean actual, final boolean expected, final String message) {
581    doAssert(new SimpleAssert<Boolean>(actual, expected, message) {
582      @Override
583      public void doAssert() {
584        org.testng.Assert.assertNotEquals(actual, expected, message);
585      }
586    });
587  }
588
589  void assertNotEquals(final boolean actual, final boolean expected) {
590    doAssert(new SimpleAssert<Boolean>(actual, expected) {
591      @Override
592      public void doAssert() {
593        org.testng.Assert.assertNotEquals(actual, expected);
594      }
595    });
596  }
597
598  void assertNotEquals(final byte actual, final byte expected, final String message) {
599    doAssert(new SimpleAssert<Byte>(actual, expected, message) {
600      @Override
601      public void doAssert() {
602        org.testng.Assert.assertNotEquals(actual, expected, message);
603      }
604    });
605  }
606
607  void assertNotEquals(final byte actual, final byte expected) {
608    doAssert(new SimpleAssert<Byte>(actual, expected) {
609      @Override
610      public void doAssert() {
611        org.testng.Assert.assertNotEquals(actual, expected);
612      }
613    });
614  }
615
616  void assertNotEquals(final char actual, final char expected, final String message) {
617    doAssert(new SimpleAssert<Character>(actual, expected, message) {
618      @Override
619      public void doAssert() {
620        org.testng.Assert.assertNotEquals(actual, expected, message);
621      }
622    });
623  }
624
625  void assertNotEquals(final char actual, final char expected) {
626    doAssert(new SimpleAssert<Character>(actual, expected) {
627      @Override
628      public void doAssert() {
629        org.testng.Assert.assertNotEquals(actual, expected);
630      }
631    });
632  }
633
634  void assertNotEquals(final short actual, final short expected, final String message) {
635    doAssert(new SimpleAssert<Short>(actual, expected, message) {
636      @Override
637      public void doAssert() {
638        org.testng.Assert.assertNotEquals(actual, expected, message);
639      }
640    });
641  }
642
643  void assertNotEquals(final short actual, final short expected) {
644    doAssert(new SimpleAssert<Short>(actual, expected) {
645      @Override
646      public void doAssert() {
647        org.testng.Assert.assertNotEquals(actual, expected);
648      }
649    });
650  }
651
652  void assertNotEquals(final int actual, final int expected, final String message) {
653    doAssert(new SimpleAssert<Integer>(actual, expected, message) {
654      @Override
655      public void doAssert() {
656        org.testng.Assert.assertNotEquals(actual, expected, message);
657      }
658    });
659  }
660
661  void assertNotEquals(final int actual, final int expected) {
662    doAssert(new SimpleAssert<Integer>(actual, expected) {
663      @Override
664      public void doAssert() {
665        org.testng.Assert.assertNotEquals(actual, expected);
666      }
667    });
668  }
669
670  public void assertNotEquals(final float actual, final float expected, final float delta,
671      final String message) {
672    doAssert(new SimpleAssert<Float>(actual, expected, message) {
673      @Override
674      public void doAssert() {
675        org.testng.Assert.assertNotEquals(actual, expected, delta, message);
676      }
677   });
678  }
679
680  public void assertNotEquals(final float actual, final float expected, final float delta) {
681    doAssert(new SimpleAssert<Float>(actual, expected) {
682      @Override
683      public void doAssert() {
684        org.testng.Assert.assertNotEquals(actual, expected, delta);
685      }
686    });
687  }
688
689  public void assertNotEquals(final double actual, final double expected, final double delta,
690      final String message) {
691    doAssert(new SimpleAssert<Double>(actual, expected, message) {
692      @Override
693      public void doAssert() {
694        org.testng.Assert.assertNotEquals(actual, expected, delta, message);
695      }
696    });
697  }
698
699  public void assertNotEquals(final double actual, final double expected, final double delta) {
700    doAssert(new SimpleAssert<Double>(actual, expected) {
701      @Override
702      public void doAssert() {
703        org.testng.Assert.assertNotEquals(actual, expected, delta);
704      }
705    });
706  }
707
708}
709