KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > mockobjects > constraint > ConstraintsTest


1 /* Copyright (c) 2002 Nat Pryce. All rights reserved.
2  *
3  * Created on February 10, 2002, 11:24 PM
4  */

5 package test.mockobjects.constraint;
6
7 import com.mockobjects.constraint.*;
8 import com.mockobjects.dynamic.Mock;
9 import com.mockobjects.util.AssertMo;
10
11 import test.mockobjects.dynamic.DummyInterface;
12
13 import java.util.EventObject JavaDoc;
14
15 import junit.framework.*;
16
17 public class ConstraintsTest extends TestCase
18 {
19     class True implements Constraint {
20         public boolean eval( Object JavaDoc o ) { return true; }
21     }
22     
23     class False implements Constraint {
24         public boolean eval( Object JavaDoc o ) { return false; }
25     }
26     
27     /** Creates a new instance of Test_Predicates
28       */

29     public ConstraintsTest( String JavaDoc test ) {
30         super(test);
31     }
32     
33     public void testIsNull() {
34         Constraint p = new IsNull();
35         
36         assertTrue( p.eval(null) );
37         assertTrue( !p.eval(new Object JavaDoc()) );
38     }
39     
40     public void testIsSame() {
41         Object JavaDoc o1 = new Object JavaDoc();
42         Object JavaDoc o2 = new Object JavaDoc();
43         Constraint p = new IsSame(o1);
44         
45         assertTrue( p.eval(o1) );
46         assertTrue( !p.eval(o2) );
47     }
48     
49     public void testIsEqual() {
50         Integer JavaDoc i1 = new Integer JavaDoc(1);
51         Integer JavaDoc i2 = new Integer JavaDoc(2);
52         Constraint p = new IsEqual(i1);
53         
54         assertTrue( p.eval(i1) );
55         assertTrue( p.eval( new Integer JavaDoc(1) ) );
56         assertTrue( !p.eval(i2) );
57     }
58     
59     public void testIsEqualObjectArray() {
60         String JavaDoc[] s1 = new String JavaDoc[] { "a", "b" };
61         String JavaDoc[] s2 = new String JavaDoc[] { "a", "b" };
62         String JavaDoc[] s3 = new String JavaDoc[] { "c", "d" };
63         String JavaDoc[] s4 = new String JavaDoc[] { "a", "b", "c", "d" };
64         
65         Constraint p = new IsEqual(s1);
66         
67         assertTrue( "Should equal itself", p.eval(s1) );
68         assertTrue( "Should equal a similar array", p.eval( s2 ) );
69         assertTrue( "Should not equal a different array", !p.eval(s3) );
70         assertTrue( "Should not equal a different sized array", !p.eval(s4) );
71     }
72     
73     public void testIsEqualToStringForNestedConstraint() {
74         assertEquals("Should get an obvious toString to reflect nesting if viewed in a debugger",
75             " = = NestedConstraint", new IsEqual(new IsEqual("NestedConstraint")).toString());
76     }
77     public void testIsEqualToStringOnProxyArgument() {
78         // Required for error message reporting
79
Mock mockDummyInterface = new Mock(DummyInterface.class, "MockName");
80         Constraint p = new IsEqual(mockDummyInterface.proxy());
81         
82         AssertMo.assertIncludes("Should get resolved toString() with no expectation error", "MockName", p.toString());
83     }
84     
85     public void testIsEqualEquals() throws Exception JavaDoc {
86         assertEquals("Should be equal", new IsEqual("a"), new IsEqual("a"));
87         assertFalse("Should not be equal - same type different values", new IsEqual("a").equals(new IsEqual("b")));
88         assertFalse("Should not be equal - different type", new IsEqual("a").equals("b"));
89     }
90     
91     public void testIsGreaterThan() {
92         Constraint p = new IsGreaterThan( new Integer JavaDoc(1) );
93         
94         assertTrue( !p.eval( new Integer JavaDoc(0) ) );
95         assertTrue( !p.eval( new Integer JavaDoc(1) ) );
96         assertTrue( p.eval( new Integer JavaDoc(2) ) );
97     }
98     
99     public void testIsLessThan() {
100         Constraint p = new IsLessThan( new Integer JavaDoc(1) );
101         
102         assertTrue( p.eval( new Integer JavaDoc(0) ) );
103         assertTrue( !p.eval( new Integer JavaDoc(1) ) );
104         assertTrue( !p.eval( new Integer JavaDoc(2) ) );
105     }
106     
107     public void testIsAnything() {
108         Constraint p = new IsAnything();
109         assertTrue( p.eval(null) );
110         assertTrue( p.eval( new Object JavaDoc() ) );
111     }
112     
113     public void testIsInstanceOf() {
114         Constraint p = new IsInstanceOf( Number JavaDoc.class );
115         assertTrue( p.eval( new Integer JavaDoc(1) ) );
116         assertTrue( p.eval( new Double JavaDoc(1.0) ) );
117         assertTrue( !p.eval("a string") );
118         assertTrue( !p.eval(null) );
119     }
120     
121     public void testIsNot() {
122         Constraint p = new IsNot( new True() );
123         assertTrue( !p.eval(null) );
124         assertTrue( !p.eval( new Object JavaDoc() ) );
125     }
126     
127     public void testAnd() {
128         Object JavaDoc o = new Object JavaDoc();
129         assertTrue( new And( new True(), new True() ).eval(o) );
130         assertTrue( !new And( new False(), new True() ).eval(o) );
131         assertTrue( !new And( new True(), new False() ).eval(o) );
132         assertTrue( !new And( new False(), new False() ).eval(o) );
133     }
134     
135     public void testOr() {
136         Object JavaDoc o = new Object JavaDoc();
137         assertTrue( new Or( new True(), new True() ).eval(o) );
138         assertTrue( new Or( new False(), new True() ).eval(o) );
139         assertTrue( new Or( new True(), new False() ).eval(o) );
140         assertTrue( !new Or( new False(), new False() ).eval(o) );
141     }
142     
143     public void testIsEventFrom() {
144         Object JavaDoc o = new Object JavaDoc();
145         EventObject JavaDoc ev = new EventObject JavaDoc(o);
146         EventObject JavaDoc ev2 = new EventObject JavaDoc( new Object JavaDoc() );
147         
148         Constraint p = new IsEventFrom(o);
149         
150         assertTrue( p.eval(ev) );
151         assertTrue( "p should eval to false for an event not from o",
152                     !p.eval(ev2) );
153         assertTrue( "p should eval to false for objects that are not events",
154                     !p.eval(o) );
155     }
156     
157     private static class DerivedEvent extends EventObject JavaDoc {
158         public DerivedEvent( Object JavaDoc source ) {
159             super(source);
160         }
161     }
162     
163     public void testIsEventSubtypeFrom() {
164         Object JavaDoc o = new Object JavaDoc();
165         DerivedEvent good_ev = new DerivedEvent(o);
166         DerivedEvent wrong_source = new DerivedEvent(new Object JavaDoc());
167         EventObject JavaDoc wrong_type = new EventObject JavaDoc(o);
168         EventObject JavaDoc wrong_source_and_type = new EventObject JavaDoc(new Object JavaDoc());
169         
170         Constraint p = new IsEventFrom( DerivedEvent.class, o );
171         
172         assertTrue( p.eval(good_ev) );
173         assertTrue( "p should eval to false for an event not from o",
174                     !p.eval(wrong_source) );
175         assertTrue( "p should eval to false for an event of the wrong type",
176                     !p.eval(wrong_type) );
177         assertTrue( "p should eval to false for an event of the wrong type "+
178                     "and from the wrong source",
179                     !p.eval(wrong_source_and_type) );
180     }
181
182     public void testIsCloseTo() {
183         Constraint p = new IsCloseTo( 1.0, 0.5 );
184         
185         assertTrue( p.eval( new Double JavaDoc(1.0) ) );
186         assertTrue( p.eval( new Double JavaDoc(0.5) ) );
187         assertTrue( p.eval( new Double JavaDoc(1.5) ) );
188         
189         assertTrue( p.eval( new Float JavaDoc(1.0) ) );
190         assertTrue( p.eval( new Integer JavaDoc(1) ) );
191         
192         assertTrue( "number too large", !p.eval( new Double JavaDoc(2.0) ) );
193         assertTrue( "number too small", !p.eval( new Double JavaDoc(0.0) ) );
194         
195         try {
196             p.eval("wrong type");
197             fail("ClassCastException expected for wrong type of argument");
198         }
199         catch( ClassCastException JavaDoc ex ) {
200             // expected
201
}
202     }
203 }
204
Popular Tags