1 package org.testng; 2 3 import org.testng.annotations.Test; 4 import org.testng.collections.Maps; 5 import org.testng.collections.Sets; 6 7 import java.util.Collection; 8 import java.util.HashMap; 9 import java.util.Map; 10 import java.util.Set; 11 12 13 /** 14 * This class/interface 15 */ 16 public class AssertTest { 17 @Test nullObjectArrayAssertEquals()18 public void nullObjectArrayAssertEquals() { 19 Object[] expected= null; 20 Object[] actual= null; 21 Assert.assertEquals(actual, expected); 22 } 23 24 @Test nullObjectArrayAssertNoOrder()25 public void nullObjectArrayAssertNoOrder() { 26 Object[] expected= null; 27 Object[] actual= null; 28 Assert.assertEqualsNoOrder(actual, expected); 29 } 30 31 @Test nullCollectionAssertEquals()32 public void nullCollectionAssertEquals() { 33 Collection expected = null; 34 Collection actual = null; 35 Assert.assertEquals(actual, expected); 36 } 37 38 @Test nullSetAssertEquals()39 public void nullSetAssertEquals() { 40 Set expected = null; 41 Set actual = null; 42 Assert.assertEquals(actual, expected); 43 } 44 45 @Test nullMapAssertEquals()46 public void nullMapAssertEquals() { 47 Map expected = null; 48 Map actual = null; 49 Assert.assertEquals(actual, expected); 50 } 51 52 @Test setAssertEquals()53 public void setAssertEquals() { 54 Set expected = Sets.newHashSet(); 55 Set actual = Sets.newHashSet(); 56 57 expected.add(1); 58 expected.add("a"); 59 actual.add("a"); 60 actual.add(1); 61 62 Assert.assertEquals(actual, expected); 63 } 64 65 @Test mapAssertEquals()66 public void mapAssertEquals() { 67 Map expected = Maps.newHashMap(); 68 Map actual = Maps.newHashMap(); 69 70 expected.put(null, "a"); 71 expected.put("a", "a"); 72 expected.put("b", "c"); 73 actual.put("b", "c"); 74 actual.put(null, "a"); 75 actual.put("a", "a"); 76 77 Assert.assertEquals(actual, expected); 78 } 79 80 @Test oneNullMapAssertEquals()81 public void oneNullMapAssertEquals() { 82 Map expected = Maps.newHashMap(); 83 Map actual = null; 84 try { 85 Assert.assertEquals(actual, expected); 86 Assert.fail("AssertEquals didn't fail"); 87 } 88 catch (AssertionError error) { 89 //do nothing 90 } 91 } 92 93 @Test oneNullSetAssertEquals()94 public void oneNullSetAssertEquals() { 95 Set expected = null; 96 Set actual = Sets.newHashSet(); 97 try { 98 Assert.assertEquals(actual, expected); 99 Assert.fail("AssertEquals didn't fail"); 100 } 101 catch (AssertionError error) { 102 //do nothing 103 } 104 } 105 106 @Test(expectedExceptions = AssertionError.class) assertEqualsMapShouldFail()107 public void assertEqualsMapShouldFail() { 108 Map<String, String> mapActual = new HashMap<String, String>() {{ 109 put("a","1"); 110 }}; 111 Map<String, String> mapExpected = new HashMap<String, String>() {{ 112 put("a","1"); 113 put("b","2"); 114 }}; 115 116 Assert.assertEquals(mapActual, mapExpected); 117 } 118 119 @Test(expectedExceptions = AssertionError.class) assertEqualsSymmetricScalar()120 public void assertEqualsSymmetricScalar() { 121 Assert.assertEquals(new Asymmetric(42, 'd'), new Contrived(42)); 122 } 123 124 @Test(expectedExceptions = AssertionError.class) assertEqualsSymmetricArrays()125 public void assertEqualsSymmetricArrays() { 126 Object[] actual = {1, new Asymmetric(42, 'd'), "inDay"}; 127 Object[] expected = {1, new Contrived(42), "inDay"}; 128 Assert.assertEquals(actual, expected); 129 } 130 131 class Contrived { 132 133 int integer; 134 Contrived(int integer)135 Contrived(int integer){ 136 this.integer = integer; 137 } 138 139 @Override equals(Object o)140 public boolean equals(Object o) { 141 if (this == o) return true; 142 if (!(o instanceof Contrived)) return false; 143 144 Contrived contrived = (Contrived) o; 145 146 if (integer != contrived.integer) return false; 147 148 return true; 149 } 150 151 @Override hashCode()152 public int hashCode() { 153 return integer; 154 } 155 } 156 157 class Asymmetric extends Contrived { 158 159 char character; 160 Asymmetric(int integer, char character)161 Asymmetric(int integer, char character) { 162 super(integer); 163 this.character = character; 164 } 165 166 @Override equals(Object o)167 public boolean equals(Object o) { 168 if (this == o) return true; 169 if (!(o instanceof Asymmetric)) return false; 170 if (!super.equals(o)) return false; 171 172 Asymmetric that = (Asymmetric) o; 173 174 if (character != that.character) return false; 175 176 return true; 177 } 178 179 @Override hashCode()180 public int hashCode() { 181 int result = super.hashCode(); 182 result = 31 * result + (int) character; 183 return result; 184 } 185 } 186 } 187