KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > mockobjects > dynamic > ConstraintMatcherTest


1 package test.mockobjects.dynamic;
2
3 import com.mockobjects.dynamic.*;
4 import com.mockobjects.dynamic.ConstraintMatcher;
5
6 import junit.framework.TestCase;
7
8 public class ConstraintMatcherTest extends TestCase {
9
10     public ConstraintMatcherTest(String JavaDoc name) {
11         super(name);
12     }
13
14     public void testNoMatchWhenTooManyArguments() throws Throwable JavaDoc {
15         String JavaDoc[] args = { "arg1", "arg2" };
16         MockConstraint[] constraints = { new MockConstraint("constraint1", args[0], true) };
17
18         ConstraintMatcher constraintMatcher = new FullConstraintMatcher(constraints);
19
20         assertFalse("Should not match if too many arguments", constraintMatcher.matches(args));
21     }
22
23     public void testNoMatchWhenTooFewArguments() throws Throwable JavaDoc {
24         String JavaDoc[] args = { "arg1" };
25         MockConstraint[] constraints = { new MockConstraint("constraint1", args[0], true), new MockConstraint("constraint2", args[0], true) };
26
27         ConstraintMatcher constraintMatcher = new FullConstraintMatcher(constraints);
28
29         assertFalse("Should not match if too few arguments", constraintMatcher.matches(args));
30     }
31
32     public void testNoMatchWhenConstraintIsViolated() throws Throwable JavaDoc {
33         String JavaDoc[] args = { "argA", "argB", "argC" };
34         MockConstraint[] constraints = {
35             new MockConstraint("constraintA", args[0], true), new MockConstraint("constraintB", args[1], false),
36             new MockConstraint("constraintC", args[2], true)
37         };
38
39         ConstraintMatcher constraintMatcher = new FullConstraintMatcher(constraints);
40
41         assertFalse("Should not match", constraintMatcher.matches(args));
42     }
43
44     public void testNoMatchWithNoArgumentsAndCalleeHasArguments()
45         throws Throwable JavaDoc {
46         String JavaDoc[] args = new String JavaDoc[] { "arg1", "arg2" };
47         MockConstraint[] constraints = new MockConstraint[0];
48
49         ConstraintMatcher constraintMatcher = new FullConstraintMatcher(constraints);
50
51         assertFalse("Should not match", constraintMatcher.matches(args));
52     }
53
54     public void testMatchWithNoArguments() throws Throwable JavaDoc {
55         String JavaDoc[] args = new String JavaDoc[0];
56         MockConstraint[] constraints = new MockConstraint[0];
57
58         ConstraintMatcher constraintMatcher = new FullConstraintMatcher(constraints);
59
60         assertTrue("Should match", constraintMatcher.matches(args));
61     }
62
63     public void testMatchAndArgumentsCheckedAgainstConstraints()
64         throws Throwable JavaDoc {
65         String JavaDoc[] args = { "argA", "argB", "argC" };
66         MockConstraint constraintA = new MockConstraint("constraintA", args[0], true);
67         MockConstraint constraintB = new MockConstraint("constraintB", args[1], true);
68         MockConstraint constraintC = new MockConstraint("constraintC", args[2], true);
69         MockConstraint[] constraints = { constraintA, constraintB, constraintC };
70
71         ConstraintMatcher constraintMatcher = new FullConstraintMatcher(constraints);
72
73         assertTrue("Should match", constraintMatcher.matches(args));
74         constraintA.verify();
75         constraintB.verify();
76         constraintC.verify();
77     }
78
79     public void testMatchWithArgument() throws Throwable JavaDoc {
80         String JavaDoc[] args = { "argA" };
81
82         MockConstraint constraintA = new MockConstraint("constraintA", args[0], true);
83         ConstraintMatcher constraintMatcher = new FullConstraintMatcher(new MockConstraint[] { constraintA });
84
85         assertTrue("Should match", constraintMatcher.matches(args));
86     }
87 }
88
Popular Tags