1 54 55 package junitx.framework; 56 57 63 public class ObjectAssert { 64 65 68 private ObjectAssert() { 69 } 70 71 75 public static void assertInstanceOf(String message, 76 Class expected, 77 Object actual) { 78 if (expected.isInstance(actual)) { 79 return; 80 } 81 failInstanceOf(message, expected, actual); 82 } 83 84 88 public static void assertInstanceOf(Class expected, 89 Object actual) { 90 assertInstanceOf(null, expected, actual); 91 } 92 93 97 public static void assertNotInstanceOf(String message, 98 Class expected, 99 Object actual) { 100 if (!expected.isInstance(actual)) { 101 return; 102 } 103 failNotInstanceOf(message, expected); 104 } 105 106 110 public static void assertNotInstanceOf(Class expected, 111 Object actual) { 112 assertNotInstanceOf(null, expected, actual); 113 } 114 115 120 public static void assertSame(Object expected, 121 Object actual) { 122 assertSame(null, expected, actual); 123 } 124 125 130 public static void assertSame(String message, 131 Object expected, 132 Object actual) { 133 if (expected == actual) { 134 return; 135 } 136 failSame(message, expected, actual); 137 } 138 139 143 public static void assertNotSame(Object expected, 144 Object actual) { 145 assertNotSame(null, expected, actual); 146 } 147 148 152 public static void assertNotSame(String message, 153 Object expected, 154 Object actual) { 155 if (expected != actual) { 156 return; 157 } 158 failNotSame(message, expected); 159 } 160 161 static private void failInstanceOf(String message, 162 Class expected, 163 Object actual) { 164 String formatted = ""; 165 if (message != null) { 166 formatted = message + " "; 167 } 168 169 Assert.fail(formatted + "expected instance of class: <" + expected.getName() + "> but was of class: <" + actual.getClass().getName() + ">"); 170 } 171 172 static private void failNotInstanceOf(String message, 173 Class expected) { 174 String formatted = ""; 175 if (message != null) { 176 formatted = message + " "; 177 } 178 179 Assert.fail(formatted + "expected not instance of class: <" + expected.getName() + ">"); 180 } 181 182 static private void failSame(String message, 183 Object expected, 184 Object actual) { 185 String formatted = ""; 186 if (message != null) { 187 formatted = message + " "; 188 } 189 190 Assert.fail(formatted + "expected same as : <" + expected + "> but was <" + actual + ">"); 191 } 192 193 static private void failNotSame(String message, 194 Object expected) { 195 String formatted = ""; 196 if (message != null) { 197 formatted = message + " "; 198 } 199 200 Assert.fail(formatted + "expected not same as : <" + expected + ">"); 201 } 202 203 } 204 | Popular Tags |