Source Code Cross Referenced for Assert.java in  » Testing » DepUnit » org » junit » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » DepUnit » org.junit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.junit;
002:
003:        import java.lang.reflect.Array;
004:
005:        import org.junit.internal.ArrayComparisonFailure;
006:
007:        /**
008:         * A set of assertion methods useful for writing tests. Only failed assertions are recorded.
009:         * These methods can be used directly: <code>Assert.assertEquals(...)</code>, however, they
010:         * read better if they are referenced through static import:<br/>
011:         * <pre>
012:         * import static org.junit.Assert.*;
013:         *    ...
014:         *    assertEquals(...);
015:         * </pre>
016:         *   
017:         * @see AssertionError
018:         */
019:        public class Assert {
020:            /**
021:             * Protect constructor since it is a static only class
022:             */
023:            protected Assert() {
024:            }
025:
026:            /**
027:             * Asserts that a condition is true. If it isn't it throws an
028:             * {@link AssertionError} with the given message.
029:             * @param message the identifying message or <code>null</code> for the {@link AssertionError}
030:             * @param condition condition to be checked
031:             */
032:            static public void assertTrue(String message, boolean condition) {
033:                if (!condition)
034:                    fail(message);
035:            }
036:
037:            /**
038:             * Asserts that a condition is true. If it isn't it throws an
039:             * {@link AssertionError} without a message.
040:             * @param condition condition to be checked
041:             */
042:            static public void assertTrue(boolean condition) {
043:                assertTrue(null, condition);
044:            }
045:
046:            /**
047:             * Asserts that a condition is false. If it isn't it throws an
048:             * {@link AssertionError} with the given message.
049:             * @param message the identifying message or <code>null</code> for the {@link AssertionError}
050:             * @param condition condition to be checked
051:             */
052:            static public void assertFalse(String message, boolean condition) {
053:                assertTrue(message, !condition);
054:            }
055:
056:            /**
057:             * Asserts that a condition is false. If it isn't it throws an
058:             * {@link AssertionError} without a message.
059:             * @param condition condition to be checked
060:             */
061:            static public void assertFalse(boolean condition) {
062:                assertFalse(null, condition);
063:            }
064:
065:            /**
066:             * Fails a test with the given message.
067:             * @param message the identifying message or <code>null</code> for the {@link AssertionError}
068:             * @see AssertionError
069:             */
070:            static public void fail(String message) {
071:                throw new AssertionError(message == null ? "" : message);
072:            }
073:
074:            /**
075:             * Fails a test with no message.
076:             */
077:            static public void fail() {
078:                fail(null);
079:            }
080:
081:            /**
082:             * Asserts that two objects are equal. If they are not, an {@link AssertionError} 
083:             * is thrown with the given message. If <code>expected</code> and <code>actual</code>
084:             * are <code>null</code>, they are considered equal.
085:             * @param message the identifying message or <code>null</code> for the {@link AssertionError}
086:             * @param expected expected value
087:             * @param actual actual value
088:             */
089:            static public void assertEquals(String message, Object expected,
090:                    Object actual) {
091:                if (expected == null && actual == null)
092:                    return;
093:                if (expected != null && isEquals(expected, actual))
094:                    return;
095:                else if (expected instanceof  String && actual instanceof  String) {
096:                    String cleanMessage = message == null ? "" : message;
097:                    throw new ComparisonFailure(cleanMessage,
098:                            (String) expected, (String) actual);
099:                } else
100:                    failNotEquals(message, expected, actual);
101:            }
102:
103:            private static boolean isEquals(Object expected, Object actual) {
104:                if (expected instanceof  Number && actual instanceof  Number)
105:                    return ((Number) expected).longValue() == ((Number) actual)
106:                            .longValue();
107:                return expected.equals(actual);
108:            }
109:
110:            /**
111:             * Asserts that two objects are equal. If they are not, an {@link AssertionError} 
112:             * without a message is thrown. If <code>expected</code> and <code>actual</code>
113:             * are <code>null</code>, they are considered equal.
114:             * @param expected expected value
115:             * @param actual the value to check against <code>expected</code>
116:             */
117:            static public void assertEquals(Object expected, Object actual) {
118:                assertEquals(null, expected, actual);
119:            }
120:
121:            /**
122:             * Asserts that two object arrays are equal. If they are not, an
123:             * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and
124:             *  <code>actuals</code> are <code>null</code>, they are considered equal.
125:             * @param message the identifying message or <code>null</code> for the {@link AssertionError}
126:             * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values.
127:             * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
128:             */
129:            public static void assertArrayEquals(String message,
130:                    Object[] expecteds, Object[] actuals)
131:                    throws ArrayComparisonFailure {
132:                internalArrayEquals(message, expecteds, actuals);
133:            }
134:
135:            /**
136:             * Asserts that two object arrays are equal. If they are not, an {@link AssertionError} 
137:             * is thrown.  If <code>expected</code> and <code>actual</code> are <code>null</code>, 
138:             * they are considered equal.
139:             * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values
140:             * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
141:             */
142:            public static void assertArrayEquals(Object[] expecteds,
143:                    Object[] actuals) {
144:                assertArrayEquals(null, expecteds, actuals);
145:            }
146:
147:            /**
148:             * TODO: fix javadoc
149:             * Asserts that two object arrays are equal. If they are not, an
150:             * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and
151:             *  <code>actuals</code> are <code>null</code>, they are considered equal.
152:             * @param message the identifying message or <code>null</code> for the {@link AssertionError}
153:             * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values.
154:             * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
155:             */
156:            public static void assertArrayEquals(String message,
157:                    byte[] expecteds, byte[] actuals)
158:                    throws ArrayComparisonFailure {
159:                internalArrayEquals(message, expecteds, actuals);
160:            }
161:
162:            /**
163:             * TODO: fix javadoc
164:             * Asserts that two object arrays are equal. If they are not, an {@link AssertionError} 
165:             * is thrown.  If <code>expected</code> and <code>actual</code> are <code>null</code>, 
166:             * they are considered equal.
167:             * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values
168:             * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
169:             */
170:            public static void assertArrayEquals(byte[] expecteds,
171:                    byte[] actuals) {
172:                assertArrayEquals(null, expecteds, actuals);
173:            }
174:
175:            /**
176:             * TODO: fix javadoc
177:             * Asserts that two object arrays are equal. If they are not, an
178:             * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and
179:             *  <code>actuals</code> are <code>null</code>, they are considered equal.
180:             * @param message the identifying message or <code>null</code> for the {@link AssertionError}
181:             * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values.
182:             * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
183:             */
184:            public static void assertArrayEquals(String message,
185:                    char[] expecteds, char[] actuals)
186:                    throws ArrayComparisonFailure {
187:                internalArrayEquals(message, expecteds, actuals);
188:            }
189:
190:            /**
191:             * TODO: fix javadoc
192:             * Asserts that two object arrays are equal. If they are not, an {@link AssertionError} 
193:             * is thrown.  If <code>expected</code> and <code>actual</code> are <code>null</code>, 
194:             * they are considered equal.
195:             * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values
196:             * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
197:             */
198:            public static void assertArrayEquals(char[] expecteds,
199:                    char[] actuals) {
200:                assertArrayEquals(null, expecteds, actuals);
201:            }
202:
203:            /**
204:             * TODO: fix javadoc
205:             * Asserts that two object arrays are equal. If they are not, an
206:             * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and
207:             *  <code>actuals</code> are <code>null</code>, they are considered equal.
208:             * @param message the identifying message or <code>null</code> for the {@link AssertionError}
209:             * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values.
210:             * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
211:             */
212:            public static void assertArrayEquals(String message,
213:                    short[] expecteds, short[] actuals)
214:                    throws ArrayComparisonFailure {
215:                internalArrayEquals(message, expecteds, actuals);
216:            }
217:
218:            /**
219:             * TODO: fix javadoc
220:             * Asserts that two object arrays are equal. If they are not, an {@link AssertionError} 
221:             * is thrown.  If <code>expected</code> and <code>actual</code> are <code>null</code>, 
222:             * they are considered equal.
223:             * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values
224:             * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
225:             */
226:            public static void assertArrayEquals(short[] expecteds,
227:                    short[] actuals) {
228:                assertArrayEquals(null, expecteds, actuals);
229:            }
230:
231:            /**
232:             * TODO: fix javadoc
233:             * Asserts that two object arrays are equal. If they are not, an
234:             * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and
235:             *  <code>actuals</code> are <code>null</code>, they are considered equal.
236:             * @param message the identifying message or <code>null</code> for the {@link AssertionError}
237:             * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values.
238:             * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
239:             */
240:            public static void assertArrayEquals(String message,
241:                    int[] expecteds, int[] actuals)
242:                    throws ArrayComparisonFailure {
243:                internalArrayEquals(message, expecteds, actuals);
244:            }
245:
246:            /**
247:             * TODO: fix javadoc
248:             * Asserts that two object arrays are equal. If they are not, an {@link AssertionError} 
249:             * is thrown.  If <code>expected</code> and <code>actual</code> are <code>null</code>, 
250:             * they are considered equal.
251:             * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values
252:             * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
253:             */
254:            public static void assertArrayEquals(int[] expecteds, int[] actuals) {
255:                assertArrayEquals(null, expecteds, actuals);
256:            }
257:
258:            /**
259:             * TODO: fix javadoc
260:             * Asserts that two object arrays are equal. If they are not, an {@link AssertionError} 
261:             * is thrown.  If <code>expected</code> and <code>actual</code> are <code>null</code>, 
262:             * they are considered equal.
263:             * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values
264:             * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
265:             */
266:            public static void assertArrayEquals(long[] expecteds,
267:                    long[] actuals) {
268:                assertArrayEquals(null, expecteds, actuals);
269:            }
270:
271:            /**
272:             * TODO: fix javadoc
273:             * Asserts that two object arrays are equal. If they are not, an
274:             * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and
275:             *  <code>actuals</code> are <code>null</code>, they are considered equal.
276:             * @param message the identifying message or <code>null</code> for the {@link AssertionError}
277:             * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values.
278:             * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
279:             */
280:            public static void assertArrayEquals(String message,
281:                    long[] expecteds, long[] actuals)
282:                    throws ArrayComparisonFailure {
283:                internalArrayEquals(message, expecteds, actuals);
284:            }
285:
286:            /**
287:             * Asserts that two object arrays are equal. If they are not, an
288:             * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and
289:             *  <code>actuals</code> are <code>null</code>, they are considered equal.
290:             * @param message the identifying message or <code>null</code> for the {@link AssertionError}
291:             * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values.
292:             * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
293:             */
294:            private static void internalArrayEquals(String message,
295:                    Object expecteds, Object actuals)
296:                    throws ArrayComparisonFailure {
297:                if (expecteds == actuals)
298:                    return;
299:                String header = message == null ? "" : message + ": ";
300:                if (expecteds == null)
301:                    fail(header + "expected array was null");
302:                if (actuals == null)
303:                    fail(header + "actual array was null");
304:                int actualsLength = Array.getLength(actuals);
305:                int expectedsLength = Array.getLength(expecteds);
306:                if (actualsLength != expectedsLength)
307:                    fail(header + "array lengths differed, expected.length="
308:                            + expectedsLength + " actual.length="
309:                            + actualsLength);
310:
311:                for (int i = 0; i < expectedsLength; i++) {
312:                    Object expected = Array.get(expecteds, i);
313:                    Object actual = Array.get(actuals, i);
314:                    if (isArray(expected) && isArray(actual)) {
315:                        try {
316:                            internalArrayEquals(message, expected, actual);
317:                        } catch (ArrayComparisonFailure e) {
318:                            e.addDimension(i);
319:                            throw e;
320:                        }
321:                    } else
322:                        try {
323:                            assertEquals(expected, actual);
324:                        } catch (AssertionError e) {
325:                            throw new ArrayComparisonFailure(header, e, i);
326:                        }
327:                }
328:            }
329:
330:            private static boolean isArray(Object expected) {
331:                return expected != null && expected.getClass().isArray();
332:            }
333:
334:            /**
335:             * Asserts that two doubles or floats are equal to within a positive delta. If they
336:             * are not, an {@link AssertionError} is thrown with the given message. If the
337:             * expected value is infinity then the delta value is ignored. NaNs are
338:             * considered equal:
339:             * <code>assertEquals(Double.NaN, Double.NaN, *)</code> passes
340:             * @param message the identifying message or <code>null</code> for the {@link AssertionError}
341:             * @param expected expected value
342:             * @param actual the value to check against <code>expected</code>
343:             * @param delta the maximum delta between <code>expected</code> and <code>actual</code> for which 
344:             * both numbers are still considered equal.
345:             */
346:            static public void assertEquals(String message, double expected,
347:                    double actual, double delta) {
348:                if (Double.compare(expected, actual) == 0)
349:                    return;
350:                if (!(Math.abs(expected - actual) <= delta))
351:                    failNotEquals(message, new Double(expected), new Double(
352:                            actual));
353:            }
354:
355:            /**
356:             * Asserts that two doubles or floats are equal to within a positive delta. If they
357:             * are not, an {@link AssertionError} is thrown. If the
358:             * expected value is infinity then the delta value is ignored.NaNs are
359:             * considered equal:
360:             * <code>assertEquals(Double.NaN, Double.NaN, *)</code> passes
361:             * @param expected expected value
362:             * @param actual the value to check against <code>expected</code>
363:             * @param delta the maximum delta between <code>expected</code> and <code>actual</code> for which 
364:             * both numbers are still considered equal.
365:             */
366:            static public void assertEquals(double expected, double actual,
367:                    double delta) {
368:                assertEquals(null, expected, actual, delta);
369:            }
370:
371:            /**
372:             * Asserts that an object isn't null. If it is an {@link AssertionError} is
373:             * thrown with the given message.
374:             * @param message the identifying message or <code>null</code> for the {@link AssertionError}
375:             * @param object Object to check or <code>null</code>
376:             */
377:            static public void assertNotNull(String message, Object object) {
378:                assertTrue(message, object != null);
379:            }
380:
381:            /**
382:             * Asserts that an object isn't null. If it is an {@link AssertionError} is
383:             * thrown.
384:             * @param object Object to check or <code>null</code>
385:             */
386:            static public void assertNotNull(Object object) {
387:                assertNotNull(null, object);
388:            }
389:
390:            /**
391:             * Asserts that an object is null. If it is not, an {@link AssertionError} is
392:             * thrown with the given message.
393:             * @param message the identifying message or <code>null</code> for the {@link AssertionError}
394:             * @param object Object to check or <code>null</code>
395:             */
396:            static public void assertNull(String message, Object object) {
397:                assertTrue(message, object == null);
398:            }
399:
400:            /**
401:             * Asserts that an object is null. If it isn't an {@link AssertionError} is
402:             * thrown.
403:             * @param object Object to check or <code>null</code>
404:             */
405:            static public void assertNull(Object object) {
406:                assertNull(null, object);
407:            }
408:
409:            /**
410:             * Asserts that two objects refer to the same object. If they are not, an
411:             * {@link AssertionError} is thrown with the given message.
412:             * @param message the identifying message or <code>null</code> for the {@link AssertionError}
413:             * @param expected the expected object
414:             * @param actual the object to compare to <code>expected</code>
415:             */
416:            static public void assertSame(String message, Object expected,
417:                    Object actual) {
418:                if (expected == actual)
419:                    return;
420:                failNotSame(message, expected, actual);
421:            }
422:
423:            /**
424:             * Asserts that two objects refer to the same object. If they are not the
425:             * same, an {@link AssertionError} without a message is thrown.
426:             * @param expected the expected object
427:             * @param actual the object to compare to <code>expected</code>
428:             */
429:            static public void assertSame(Object expected, Object actual) {
430:                assertSame(null, expected, actual);
431:            }
432:
433:            /**
434:             * Asserts that two objects do not refer to the same object. If they do
435:             * refer to the same object, an {@link AssertionError} is thrown with the given
436:             * message.
437:             * @param message the identifying message or <code>null</code> for the {@link AssertionError}
438:             * @param unexpected the object you don't expect
439:             * @param actual the object to compare to <code>unexpected</code>
440:             */
441:            static public void assertNotSame(String message, Object unexpected,
442:                    Object actual) {
443:                if (unexpected == actual)
444:                    failSame(message);
445:            }
446:
447:            /**
448:             * Asserts that two objects do not refer to the same object. If they do
449:             * refer to the same object, an {@link AssertionError} without a message is thrown.
450:             * @param unexpected the object you don't expect
451:             * @param actual the object to compare to <code>unexpected</code>
452:             */
453:            static public void assertNotSame(Object unexpected, Object actual) {
454:                assertNotSame(null, unexpected, actual);
455:            }
456:
457:            static private void failSame(String message) {
458:                String formatted = "";
459:                if (message != null)
460:                    formatted = message + " ";
461:                fail(formatted + "expected not same");
462:            }
463:
464:            static private void failNotSame(String message, Object expected,
465:                    Object actual) {
466:                String formatted = "";
467:                if (message != null)
468:                    formatted = message + " ";
469:                fail(formatted + "expected same:<" + expected + "> was not:<"
470:                        + actual + ">");
471:            }
472:
473:            static private void failNotEquals(String message, Object expected,
474:                    Object actual) {
475:                fail(format(message, expected, actual));
476:            }
477:
478:            static String format(String message, Object expected, Object actual) {
479:                String formatted = "";
480:                if (message != null && !message.equals(""))
481:                    formatted = message + " ";
482:                String expectedString = expected.toString();
483:                String actualString = actual.toString();
484:                if (expectedString.equals(actualString))
485:                    return formatted + "expected: "
486:                            + expected.getClass().getName() + "<"
487:                            + expectedString + "> but was: "
488:                            + actual.getClass().getName() + "<" + actualString
489:                            + ">";
490:                else
491:                    return formatted + "expected:<" + expectedString
492:                            + "> but was:<" + actualString + ">";
493:            }
494:
495:            /**
496:             * Asserts that two object arrays are equal. If they are not, an
497:             * {@link AssertionError} is thrown with the given message. If <code>expecteds</code> and
498:             *  <code>actuals</code> are <code>null</code>, they are considered equal.
499:             * @param message the identifying message or <code>null</code> for the {@link AssertionError}
500:             * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values.
501:             * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
502:             * @deprecated use assertArrayEquals
503:             */
504:            @Deprecated
505:            public static void assertEquals(String message, Object[] expecteds,
506:                    Object[] actuals) {
507:                assertArrayEquals(message, expecteds, actuals);
508:            }
509:
510:            /**
511:             * Asserts that two object arrays are equal. If they are not, an {@link AssertionError} 
512:             * is thrown.  If <code>expected</code> and <code>actual</code> are <code>null</code>, 
513:             * they are considered equal.
514:             * @param expecteds Object array or array of arrays (multi-dimensional array) with expected values
515:             * @param actuals Object array or array of arrays (multi-dimensional array) with actual values
516:             * @deprecated use assertArrayEquals
517:             */
518:            @Deprecated
519:            public static void assertEquals(Object[] expecteds, Object[] actuals) {
520:                assertArrayEquals(expecteds, actuals);
521:            }
522:
523:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.