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