git clone of logicmail with some fixes/features added
at master 248 lines 7.0 kB view raw
1//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2// This file is part of J2MEUnit, a Java 2 Micro Edition unit testing framework. 3// 4// J2MEUnit is free software distributed under the Common Public License (CPL). 5// It may be redistributed and/or modified under the terms of the CPL. You 6// should have received a copy of the license along with J2MEUnit. It is also 7// available from the website of the Open Source Initiative at 8// http://www.opensource.org. 9// 10// J2MEUnit is distributed in the hope that it will be useful, but WITHOUT ANY 11// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 12// FOR A PARTICULAR PURPOSE. 13//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 14package j2meunit.framework; 15 16/******************************************************************** 17 * A set of assert methods. 18 */ 19public class Assert 20{ 21 //~ Constructors ----------------------------------------------------------- 22 23 /*************************************** 24 * Creates a new Assert object. 25 */ 26 public Assert() 27 { 28 } 29 30 //~ Methods ---------------------------------------------------------------- 31 32 /*************************************** 33 * Asserts that two longs are equal. 34 * 35 * @param expected the expected value of an object 36 * @param actual the actual value of an object 37 */ 38 public void assertEquals(long expected, long actual) 39 { 40 assertEquals(null, expected, actual); 41 } 42 43 /*************************************** 44 * Asserts that two objects are equal. If they are not an 45 * AssertionFailedError is thrown. 46 * 47 * @param expected the expected value of an object 48 * @param actual the actual value of an object 49 */ 50 public void assertEquals(Object expected, Object actual) 51 { 52 assertEquals(null, expected, actual); 53 } 54 55 /*************************************** 56 * Asserts that two longs are equal. 57 * 58 * @param message the detail message for this assertion 59 * @param expected the expected value of an object 60 * @param actual the actual value of an object 61 */ 62 public void assertEquals(String message, long expected, long actual) 63 { 64 assertEquals(message, new Long(expected), new Long(actual)); 65 } 66 67 /*************************************** 68 * Asserts that two objects are equal. If they are not an 69 * AssertionFailedError is thrown. 70 * 71 * @param message The detail message for this assertion 72 * @param expected The expected value of an object 73 * @param actual The actual value of an object 74 */ 75 public void assertEquals(String message, Object expected, Object actual) 76 { 77 onAssertion(); 78 79 if ((expected == null) && (actual == null)) 80 return; 81 82 if ((expected != null) && expected.equals(actual)) 83 return; 84 85 failNotEquals(message, expected, actual); 86 } 87 88 /*************************************** 89 * Asserts that an object isn't null. 90 * 91 * @param object The object to test 92 */ 93 public void assertNotNull(Object object) 94 { 95 assertNotNull(null, object); 96 } 97 98 /*************************************** 99 * Asserts that an object isn't null. 100 * 101 * @param message The detail message for this assertion 102 * @param object The object to test 103 */ 104 public void assertNotNull(String message, Object object) 105 { 106 assertTrue(message, object != null); 107 } 108 109 /*************************************** 110 * Asserts that an object is null. 111 * 112 * @param object The object to test 113 */ 114 public void assertNull(Object object) 115 { 116 assertNull(null, object); 117 } 118 119 /*************************************** 120 * Asserts that an object is null. 121 * 122 * @param message The detail message for this assertion 123 * @param object The object to test 124 */ 125 public void assertNull(String message, Object object) 126 { 127 assertTrue(message, object == null); 128 } 129 130 /*************************************** 131 * Asserts that two objects refer to the same object. If they are not the 132 * same an AssertionFailedError is thrown. 133 * 134 * @param expected The expected value of an object 135 * @param actual The actual value of an object 136 */ 137 public void assertSame(Object expected, Object actual) 138 { 139 assertSame(null, expected, actual); 140 } 141 142 /*************************************** 143 * Asserts that two objects refer to the same object. If they are not an 144 * AssertionFailedError is thrown. 145 * 146 * @param message The detail message for this assertion 147 * @param expected The expected value of an object 148 * @param actual The actual value of an object 149 */ 150 public void assertSame(String message, Object expected, Object actual) 151 { 152 onAssertion(); 153 154 if (expected == actual) 155 return; 156 157 failNotSame(message, expected, actual); 158 } 159 160 /*************************************** 161 * Asserts that a condition is true. If it isn't it throws an 162 * AssertionFailedError with the given message. 163 * 164 * @param message The detail message for this assertion 165 * @param condition The boolean value to test 166 */ 167 public void assertTrue(String message, boolean condition) 168 { 169 onAssertion(); 170 171 if (!condition) 172 fail(message); 173 } 174 175 /*************************************** 176 * Asserts that a condition is true. If it isn't it throws an 177 * AssertionFailedError. 178 * 179 * @param condition The boolean value to test 180 */ 181 public void assertTrue(boolean condition) 182 { 183 assertTrue(null, condition); 184 } 185 186 /*************************************** 187 * Fails a test with no message. 188 */ 189 public void fail() 190 { 191 fail(null); 192 } 193 194 /*************************************** 195 * Fails a test with the given error message. 196 * 197 * @param message The message to display 198 * 199 * @throws AssertionFailedError Containing the error message 200 */ 201 public void fail(String message) 202 { 203 throw new AssertionFailedError(message); 204 } 205 206 /*************************************** 207 * Fails an "assertEquals" test with the given error message. 208 * 209 * @param message The message to display 210 * @param expected The expected value 211 * @param actual The actual value 212 */ 213 protected void failNotEquals(String message, Object expected, Object actual) 214 { 215 String formatted = ""; 216 217 if (message != null) 218 formatted = message + " "; 219 220 fail(formatted + "expected:<" + expected + "> but was:<" + actual + 221 ">"); 222 } 223 224 /*************************************** 225 * Fails an "assertSame" test with the given error message. 226 * 227 * @param message The message to display 228 * @param expected The expected value 229 * @param actual The actual value 230 */ 231 protected void failNotSame(String message, Object expected, Object actual) 232 { 233 String formatted = ""; 234 235 if (message != null) 236 formatted = message + " "; 237 238 fail(formatted + "expected same"); 239 } 240 241 /*************************************** 242 * This method is called every time an assertion is made (this does not 243 * include direct fails). 244 */ 245 protected void onAssertion() 246 { 247 } 248}