KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > util > AssertMo


1 package com.mockobjects.util;
2
3
4 import junit.framework.Assert;
5 import junit.framework.AssertionFailedError;
6 import com.mockobjects.Verifiable;
7
8 public class AssertMo extends Assert {
9
10     protected AssertMo() {
11         super();
12     }
13
14     public static void assertEquals(
15         String JavaDoc description,
16         Object JavaDoc[] expectedArray,
17         Object JavaDoc[] actualArray) {
18         assertEquals(
19             description + " (different lengths)",
20             expectedArray.length,
21             actualArray.length);
22         for (int i = 0; i < expectedArray.length; i++) {
23             assertEquals(
24                 description + " (element " + i + ")",
25                 expectedArray[i],
26                 actualArray[i]);
27         }
28     }
29
30     public static void assertExcludes(
31         String JavaDoc description,
32         String JavaDoc excludeString,
33         String JavaDoc targetString) {
34         assertTrue(
35             description
36                 + "\nExclude String: "
37                 + excludeString
38                 + "\n Target String: "
39                 + targetString,
40             targetString.indexOf(excludeString) == -1);
41     }
42
43     public static void assertIncludes(
44         String JavaDoc description,
45         String JavaDoc includeString,
46         String JavaDoc targetString) {
47         assertTrue(
48             description
49                 + "\nInclude String: "
50                 + includeString
51                 + "\n Target String: "
52                 + targetString,
53             targetString.indexOf(includeString) != -1);
54     }
55
56     public static void assertStartsWith(
57         String JavaDoc description,
58         String JavaDoc startString,
59         String JavaDoc targetString) {
60         assertTrue(
61             description
62                 + "\n Start String: "
63                 + startString
64                 + "\nTarget String: "
65                 + targetString,
66             targetString.startsWith(startString));
67     }
68
69     public static void assertVerifyFails(Verifiable aVerifiable) {
70         boolean threwException = false;
71         try {
72             aVerifiable.verify();
73         } catch (AssertionFailedError ex) {
74             threwException = true;
75         }
76
77         assertTrue("Should not have verified", threwException);
78     }
79
80     static protected void failNotEquals(
81         String JavaDoc message,
82         Object JavaDoc expected,
83         Object JavaDoc actual) {
84         String JavaDoc formatted = "";
85         if (message != null)
86             formatted = message + " ";
87         fail(
88             formatted + "\nExpected:<" + expected + ">\nReceived:<" + actual + ">");
89     }
90
91     public static void notImplemented(String JavaDoc mockName) {
92         throw new NotImplementedException("Not Implemented in " + mockName);
93     }
94
95     public static void assertFails(String JavaDoc message, Runnable JavaDoc runnable) {
96         try {
97             runnable.run();
98         } catch (AssertionFailedError expected) {
99             return;
100         }
101         fail(message);
102     }
103 }
104
Popular Tags