KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > expectation > AssertMoTest


1 /* Copyright (c) 2000-2004 jMock.org
2  */

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