1 package test.asserttests;
2 
3 import org.testng.Assert;
4 import org.testng.Assert.ThrowingRunnable;
5 import org.testng.annotations.Test;
6 import org.testng.collections.Sets;
7 
8 import java.io.IOException;
9 import java.util.Set;
10 
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.fail;
14 import static org.testng.Assert.expectThrows;
15 
16 public class AssertTest {
17 
18   @Test
noOrderSuccess()19   public void noOrderSuccess() {
20     String[] rto1 = { "boolean", "BigInteger", "List",};
21     String[] rto2 = {  "List", "BigInteger", "boolean",};
22     Assert.assertEqualsNoOrder(rto1, rto2);
23   }
24 
25   @Test(expectedExceptions = AssertionError.class)
noOrderFailure()26   public void noOrderFailure() {
27     String[] rto1 = { "a", "a", "b",};
28     String[] rto2 = {  "a", "b", "b",};
29     Assert.assertEqualsNoOrder(rto1, rto2);
30   }
31 
32   @Test
intArray_Issue4()33   public void intArray_Issue4() {
34     int[] intArr00 = {1};
35     int[] intArr01 = {1};
36     Assert.assertEquals(intArr00, intArr01);
37   }
38 
39   @Test(expectedExceptions = AssertionError.class)
arraysFailures_1()40   public void arraysFailures_1() {
41     int[] intArr = {1, 2};
42     long[] longArr = {1, 2};
43     Assert.assertEquals(intArr, longArr);
44   }
45 
46   @Test(expectedExceptions = AssertionError.class)
arraysFailures_2()47   public void arraysFailures_2() {
48     int[] intArr = {1, 2};
49     Assert.assertEquals(intArr, (long) 1);
50   }
51 
52   @Test(expectedExceptions = AssertionError.class)
arraysFailures_3()53   public void arraysFailures_3() {
54     long[] longArr = {1};
55     Assert.assertEquals((long) 1, longArr);
56   }
57 
58   @Test
setsSuccess()59   public void setsSuccess() {
60     Set<Integer> set1 = Sets.newHashSet();
61     Set<Integer> set2 = Sets.newHashSet();
62 
63     set1.add(1);
64     set2.add(1);
65 
66     set1.add(3);
67     set2.add(3);
68 
69     set1.add(2);
70     set2.add(2);
71 
72     Assert.assertEquals(set1, set2);
73     Assert.assertEquals(set2, set1);
74   }
75 
76   @Test(expectedExceptions = AssertionError.class)
expectThrowsRequiresAnExceptionToBeThrown()77   public void expectThrowsRequiresAnExceptionToBeThrown() {
78     expectThrows(Throwable.class, nonThrowingRunnable());
79   }
80 
81   @Test
expectThrowsIncludesAnInformativeDefaultMessage()82   public void expectThrowsIncludesAnInformativeDefaultMessage() {
83     try {
84       expectThrows(Throwable.class, nonThrowingRunnable());
85     } catch (AssertionError ex) {
86       assertEquals("Expected Throwable to be thrown, but nothing was thrown", ex.getMessage());
87       return;
88     }
89     fail();
90   }
91 
92   @Test
expectThrowsReturnsTheSameObjectThrown()93   public void expectThrowsReturnsTheSameObjectThrown() {
94     NullPointerException npe = new NullPointerException();
95 
96     Throwable throwable = expectThrows(Throwable.class, throwingRunnable(npe));
97 
98     assertSame(npe, throwable);
99   }
100 
101   @Test(expectedExceptions = AssertionError.class)
expectThrowsDetectsTypeMismatchesViaExplicitTypeHint()102   public void expectThrowsDetectsTypeMismatchesViaExplicitTypeHint() {
103     NullPointerException npe = new NullPointerException();
104 
105     expectThrows(IOException.class, throwingRunnable(npe));
106   }
107 
108   @Test
expectThrowsWrapsAndPropagatesUnexpectedExceptions()109   public void expectThrowsWrapsAndPropagatesUnexpectedExceptions() {
110     NullPointerException npe = new NullPointerException("inner-message");
111 
112     try {
113       expectThrows(IOException.class, throwingRunnable(npe));
114     } catch (AssertionError ex) {
115       assertSame(npe, ex.getCause());
116       assertEquals("inner-message", ex.getCause().getMessage());
117       return;
118     }
119     fail();
120   }
121 
122   @Test
expectThrowsSuppliesACoherentErrorMessageUponTypeMismatch()123   public void expectThrowsSuppliesACoherentErrorMessageUponTypeMismatch() {
124     NullPointerException npe = new NullPointerException();
125 
126     try {
127       expectThrows(IOException.class, throwingRunnable(npe));
128     } catch (AssertionError error) {
129       assertEquals("Expected IOException to be thrown, but NullPointerException was thrown",
130               error.getMessage());
131       assertSame(npe, error.getCause());
132       return;
133     }
134     fail();
135   }
136 
nonThrowingRunnable()137   private static ThrowingRunnable nonThrowingRunnable() {
138     return new ThrowingRunnable() {
139       public void run() throws Throwable {
140       }
141     };
142   }
143 
throwingRunnable(final Throwable t)144   private static ThrowingRunnable throwingRunnable(final Throwable t) {
145     return new ThrowingRunnable() {
146       public void run() throws Throwable {
147         throw t;
148       }
149     };
150   }
151 
152   @Test
153   public void doubleNaNAssertion() {
154     Assert.assertEquals(Double.NaN, Double.NaN, 0.0);
155   }
156 }
157