KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > mockobjects > TestAssertMo


1 package test.mockobjects;
2
3 import com.mockobjects.util.AssertMo;
4 import com.mockobjects.util.TestCaseMo;
5 import junit.framework.AssertionFailedError;
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8
9 import java.util.Vector JavaDoc;
10
11 public class TestAssertMo extends TestCaseMo {
12     private static final Class JavaDoc THIS = TestAssertMo.class;
13
14     public TestAssertMo(String JavaDoc name) {
15         super(name);
16     }
17
18     public static void main(String JavaDoc[] args) {
19         start(new String JavaDoc[] { THIS.getName()});
20     }
21
22     public static Test suite() {
23         return new TestSuite(THIS);
24     }
25
26     public void testAssertExcludes() {
27         AssertMo.assertExcludes(
28             "Should not include substring",
29             "dog",
30             "The quick brown fox");
31     }
32
33     public void testAssertExcludesFails() {
34         Throwable JavaDoc result = null;
35         try {
36             AssertMo.assertExcludes(
37                 "Should fail on exclude",
38                 "fox",
39                 "The quick brown fox");
40         } catch (AssertionFailedError ex) {
41             result = ex;
42         }
43
44         assertTrue("Should get an exception", result != null);
45
46     }
47
48     public void testAssertIncludes() {
49         AssertMo.assertIncludes(
50             "Should include a substring",
51             "fox",
52             "The quick brown fox");
53     }
54
55     public void testAssertIncludesFails() {
56         Throwable JavaDoc result = null;
57         try {
58             AssertMo.assertIncludes(
59                 "Should fail if no include",
60                 "dog",
61                 "The quick brown fox");
62         } catch (AssertionFailedError ex) {
63             result = ex;
64         }
65
66         assertTrue("Should get an exception", result != null);
67
68     }
69
70     public void testAssertStartsWith() {
71         AssertMo.assertStartsWith(
72             "Should start with fox",
73             "fox",
74             "fox quick brown");
75     }
76
77     public void testAssertStartsWithFails() {
78         Throwable JavaDoc result = null;
79         try {
80             AssertMo.assertStartsWith(
81                 "Should fail if it doesn't start with fox",
82                 "fox",
83                 "The quick brown fox");
84         } catch (AssertionFailedError ex) {
85             result = ex;
86         }
87
88         assertTrue("Should get an exception", result != null);
89
90     }
91
92     public void testDifferentArrays() {
93         Object JavaDoc[] anExpectedArray = new Object JavaDoc[] { "one", new Integer JavaDoc(2)};
94         Object JavaDoc[] anActualArray = new Object JavaDoc[] { "two", new Integer JavaDoc(2)};
95
96         boolean threwException = false;
97         try {
98             AssertMo.assertEquals(
99                 "Should be expected value",
100                 anExpectedArray,
101                 anActualArray);
102         } catch (AssertionFailedError ignoredException) {
103             threwException = true;
104         }
105         assertTrue("should have thrown assertion failure", threwException);
106     }
107
108     public void testDifferentLengthArrays() {
109         Object JavaDoc[] anExpectedArray = new Object JavaDoc[] { "one", new Integer JavaDoc(2)};
110         Object JavaDoc[] anActualArray = new Object JavaDoc[] { "one" };
111
112         boolean threwException = false;
113         try {
114             AssertMo.assertEquals(
115                 "Should be expected value",
116                 anExpectedArray,
117                 anActualArray);
118         } catch (AssertionFailedError ignoredException) {
119             threwException = true;
120         }
121         assertTrue("should have thrown assertion failure", threwException);
122     }
123
124     public void testDifferentObjectArrays() {
125         Object JavaDoc[] anExpectedArray = new Object JavaDoc[] { "one", new Integer JavaDoc(2)};
126         Object JavaDoc[] anActualArray = new Object JavaDoc[] { new Integer JavaDoc(2), new Vector JavaDoc()};
127
128         boolean threwException = false;
129         try {
130             AssertMo.assertEquals(
131                 "Should be expected value",
132                 anExpectedArray,
133                 anActualArray);
134         } catch (AssertionFailedError ignoredException) {
135             threwException = true;
136         }
137         assertTrue("should have thrown assertion failure", threwException);
138     }
139
140     public void testEqualArrays() {
141         Object JavaDoc[] anExpectedArray = new Object JavaDoc[] { "one", new Integer JavaDoc(2)};
142         Object JavaDoc[] anActualArray = new Object JavaDoc[] { "one", new Integer JavaDoc(2)};
143
144         AssertMo.assertEquals(
145             "Should be expected value",
146             anExpectedArray,
147             anActualArray);
148     }
149
150     public void testEqualEmptyArrays() {
151         Object JavaDoc[] anExpectedArray = new Object JavaDoc[0];
152         Object JavaDoc[] anActualArray = new Object JavaDoc[0];
153
154         AssertMo.assertEquals(
155             "Should be expected value",
156             anExpectedArray,
157             anActualArray);
158     }
159
160     public void testFailureCheckerWithFailure() {
161         AssertMo.assertFails("Test Description",
162                              new Runnable JavaDoc() {
163                                  public void run() { fail("Should not be propagated"); }
164                              });
165     }
166
167     public void testFailureCheckerWithoutFailure() {
168         final String JavaDoc TEST_MESSAGE = "Test Description";
169         try {
170             AssertMo.assertFails(TEST_MESSAGE, new Runnable JavaDoc() { public void run() {} });
171         } catch (AssertionFailedError expected) {
172             assertEquals(TEST_MESSAGE, expected.getMessage());
173             return;
174         }
175         fail("Should have thrown an exception");
176     }
177
178 }
179
Popular Tags