KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junitx > framework > ThrowableAssertTest


1 package junitx.framework;
2
3 import junit.framework.AssertionFailedError;
4 import junit.framework.TestCase;
5
6 /**
7  * @version $Revision: 1.2 $ $Date: 2002/09/13 08:19:14 $
8  * @author <a HREF="mailto:vbossica@users.sourceforge.net">Vladimir Bossicard</a>
9  */

10 public class ThrowableAssertTest
11         extends TestCase {
12
13     public ThrowableAssertTest(String JavaDoc name) {
14         super(name);
15     }
16
17     public void testSuccessEquals() {
18         Throwable JavaDoc expected = new TestThrowable("This is a message");
19         Throwable JavaDoc actual = new TestThrowable("This is a message");
20
21         ThrowableAssert.assertEquals(expected, actual);
22     }
23
24     public void testSuccessEqualsLocal() {
25         Throwable JavaDoc expected = new TestThrowable("This is a message", "This is another message");
26         Throwable JavaDoc actual = new TestThrowable("This is a message", "This is another message");
27
28         ThrowableAssert.assertEquals(expected, actual);
29     }
30
31     public void testFailClass()
32             throws Exception JavaDoc {
33         try {
34             Throwable JavaDoc expected = new TestThrowable("This is a message");
35             Throwable JavaDoc actual = new NullPointerException JavaDoc("This is a message");
36
37             ThrowableAssert.assertEquals(expected, actual);
38         } catch (AssertionFailedError e) {
39             Assert.assertEquals("expected instance of class: <junitx.framework.ThrowableAssertTest$TestThrowable> but was of class: <java.lang.NullPointerException>", e.getMessage());
40             return;
41         }
42         fail("Should have thrown exception");
43     }
44
45     public void testFailMessage()
46             throws Exception JavaDoc {
47         try {
48             Throwable JavaDoc expected = new TestThrowable("This is a message");
49             Throwable JavaDoc actual = new TestThrowable("This is a wrong message");
50
51             ThrowableAssert.assertEquals(expected, actual);
52         } catch (AssertionFailedError e) {
53             return;
54         }
55         fail("Should have thrown exception");
56     }
57
58     public void testFailLocalized()
59             throws Exception JavaDoc {
60         try {
61             Throwable JavaDoc expected = new TestThrowable("This is a message", "This is another message");
62             Throwable JavaDoc actual = new TestThrowable("This is a message", "This is another wrong message");
63
64             ThrowableAssert.assertEquals(expected, actual);
65         } catch (AssertionFailedError e) {
66             return;
67         }
68         fail("Should have thrown exception");
69     }
70
71     public void testSuccessSimilar() {
72         Throwable JavaDoc expected = new TestThrowable("is a");
73         Throwable JavaDoc actual = new TestThrowable("This is a message");
74
75         ThrowableAssert.assertSimilar(expected, actual);
76     }
77
78     public void testSuccessSimilarLocal() {
79         Throwable JavaDoc expected = new TestThrowable("is another");
80         Throwable JavaDoc actual = new TestThrowable("This is a message", "This is another message");
81
82         ThrowableAssert.assertSimilar(expected, actual);
83     }
84
85     public void testFailSimilar()
86             throws Exception JavaDoc {
87         try {
88             Throwable JavaDoc expected = new TestThrowable("is another");
89             Throwable JavaDoc actual = new TestThrowable("This is a message");
90
91             ThrowableAssert.assertSimilar(expected, actual);
92         } catch (AssertionFailedError e) {
93             return;
94         }
95         fail("Should have thrown exception");
96     }
97
98     public void testFailSimilarLocal()
99             throws Exception JavaDoc {
100         try {
101             Throwable JavaDoc expected = new TestThrowable("has");
102             Throwable JavaDoc actual = new TestThrowable("This is a message", "This is another message");
103
104             ThrowableAssert.assertSimilar(expected, actual);
105         } catch (AssertionFailedError e) {
106             return;
107         }
108         fail("Should have thrown exception");
109     }
110
111     public static class TestThrowable
112             extends Throwable JavaDoc {
113
114         private String JavaDoc local;
115
116         public TestThrowable(String JavaDoc message) {
117             super(message);
118             this.local = message;
119         }
120
121         public TestThrowable(String JavaDoc message, String JavaDoc local) {
122             super(message);
123             this.local = local;
124         }
125
126         public String JavaDoc getLocalizedMessage() {
127             return local;
128         }
129
130     }
131
132 }
Popular Tags