KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > util > VerifierTest


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

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