KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > mockobjects > TestVerifier


1 package test.mockobjects;
2
3 import com.mockobjects.ExpectationValue;
4 import com.mockobjects.MockObject;
5 import com.mockobjects.util.TestCaseMo;
6 import junit.framework.AssertionFailedError;
7 import junit.framework.Test;
8 import junit.framework.TestSuite;
9
10 public class TestVerifier extends TestCaseMo {
11     private static final Class JavaDoc THIS = TestVerifier.class;
12
13     class OneVerifiable extends MockObject {
14         private ExpectationValue myValue = new ExpectationValue("should fail");
15         private int unusedField;
16
17         public OneVerifiable() {
18             myValue.setFailOnVerify();
19             myValue.setExpected("good");
20         }
21
22         public void setValue(String JavaDoc aValue) {
23             myValue.setActual(aValue);
24         }
25     }
26
27     class InheritVerifiable extends OneVerifiable {
28     }
29
30     class LoopingVerifiable extends MockObject {
31         LoopingVerifiable myRef = this;
32         boolean inVerify = false;
33
34         LoopingVerifiable() {
35             super();
36         }
37
38         public void setRef(LoopingVerifiable aRef) {
39             myRef = aRef;
40         }
41
42         public void verify() {
43             assertTrue("Looping verification detected", !inVerify);
44             inVerify = true;
45             super.verify();
46             inVerify = false;
47         }
48     }
49
50     public TestVerifier(String JavaDoc name) {
51         super(name);
52     }
53
54
55     public static void main(String JavaDoc[] args) {
56         start(new String JavaDoc[]{THIS.getName()});
57     }
58
59
60     public static Test suite() {
61         return new TestSuite(THIS);
62     }
63
64
65     public void testInheritVerifiableFails() {
66         InheritVerifiable inheritVerifiable = new InheritVerifiable();
67         inheritVerifiable.setValue("bad");
68
69         boolean hasThrownException = false;
70         try {
71             inheritVerifiable.verify();
72         } catch (AssertionFailedError ex) {
73             hasThrownException = true;
74         }
75         assertTrue("Should have thrown exception", hasThrownException);
76     }
77
78
79     public void testInheritVerifiablePasses() {
80         InheritVerifiable inheritVerifiable = new InheritVerifiable();
81         inheritVerifiable.setValue("good");
82
83         inheritVerifiable.verify();
84     }
85
86
87     public void testNoVerifiables() {
88         class NoVerifiables extends MockObject {
89         }
90
91         new NoVerifiables().verify();
92     }
93
94
95     public void testOneVerifiableFails() {
96         OneVerifiable oneVerifiable = new OneVerifiable();
97         oneVerifiable.setValue("bad");
98
99         boolean hasThrownException = false;
100         try {
101             oneVerifiable.verify();
102         } catch (AssertionFailedError ex) {
103             hasThrownException = true;
104         }
105         assertTrue("Should have thrown exception", hasThrownException);
106     }
107
108
109     public void testOneVerifiablePasses() {
110         OneVerifiable oneVerifiable = new OneVerifiable();
111         oneVerifiable.setValue("good");
112
113         oneVerifiable.verify();
114     }
115
116     public void testNoLoopVerifySingleLevel() {
117         new LoopingVerifiable().verify();
118     }
119
120     public void testNoLoopVerifyMultiLevel() {
121         LoopingVerifiable a = new LoopingVerifiable();
122         LoopingVerifiable b = new LoopingVerifiable();
123
124         a.setRef(b);
125         b.setRef(a);
126         a.verify();
127     }
128 }
129
Popular Tags