KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > core > matcher > ArgumentsMatcherTest


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

3 package test.jmock.core.matcher;
4
5 import junit.framework.TestCase;
6 import org.jmock.core.Constraint;
7 import org.jmock.core.Invocation;
8 import org.jmock.core.constraint.IsSame;
9 import org.jmock.core.matcher.ArgumentsMatcher;
10 import test.jmock.core.testsupport.AlwaysFalse;
11 import test.jmock.core.testsupport.AlwaysTrue;
12 import test.jmock.core.testsupport.MethodFactory;
13
14
15 public class ArgumentsMatcherTest extends TestCase
16 {
17     private static final String JavaDoc INVOKED_OBJECT = "INVOKED-OBJECT";
18     private static final String JavaDoc exampleArg1 = "arg1";
19     private static final String JavaDoc exampleArg2 = "arg2";
20
21     private Invocation emptyInvocation;
22     private Invocation exampleInvocation;
23
24     public void setUp() {
25         MethodFactory methodFactory = new MethodFactory();
26
27         emptyInvocation = new Invocation(INVOKED_OBJECT,
28                                          methodFactory.newMethod("example", MethodFactory.NO_ARGUMENTS, Void JavaDoc.class,
29                                                                  MethodFactory.NO_EXCEPTIONS),
30                                          new Object JavaDoc[0]);
31
32         exampleInvocation = new Invocation(INVOKED_OBJECT,
33                                            methodFactory.newMethod("example", new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class}, Void JavaDoc.class,
34                                                                    MethodFactory.NO_EXCEPTIONS),
35                                            new Object JavaDoc[]{exampleArg1, exampleArg2});
36     }
37
38     public void testMatchWhenNoArgumentsOrConstraints() throws Throwable JavaDoc {
39         ArgumentsMatcher matcher = new ArgumentsMatcher(new Constraint[0]);
40
41         assertTrue("No arguments", matcher.matches(emptyInvocation));
42     }
43
44     public void testNoMatchWhenTooManyArguments() throws Throwable JavaDoc {
45         ArgumentsMatcher matcher = new ArgumentsMatcher(new Constraint[0]);
46
47         assertFalse("Too many arguments", matcher.matches(exampleInvocation));
48     }
49
50     public void testNoMatchWhenTooFewArguments() throws Throwable JavaDoc {
51         ArgumentsMatcher matcher =
52                 new ArgumentsMatcher(new Constraint[]{AlwaysTrue.INSTANCE, AlwaysTrue.INSTANCE, AlwaysTrue.INSTANCE});
53
54         assertFalse("Too many arguments", matcher.matches(exampleInvocation));
55     }
56
57     public void testNoMatchWhenAnyArgumentDoesNotConform() throws Throwable JavaDoc {
58         ArgumentsMatcher matcher =
59                 new ArgumentsMatcher(new Constraint[]{AlwaysTrue.INSTANCE, same("wrong")});
60
61         assertFalse("Incorrect argument", matcher.matches(exampleInvocation));
62     }
63
64     public void testArgumentsMatchWhenAllArgumentsMatch() throws Throwable JavaDoc {
65         ArgumentsMatcher matcher =
66                 new ArgumentsMatcher(new Constraint[]{AlwaysTrue.INSTANCE, same(exampleArg2)});
67
68         assertTrue("Arguments match", matcher.matches(exampleInvocation));
69     }
70
71     public void testClonesConstraintListDuringConstructor() {
72         Constraint[] constraintArray = {same(exampleArg1), same(exampleArg2)};
73
74         ArgumentsMatcher matcher = new ArgumentsMatcher(constraintArray);
75
76         constraintArray[0] = AlwaysFalse.INSTANCE;
77         assertTrue("arguments should match", matcher.matches(exampleInvocation));
78     }
79
80     private Constraint same( Object JavaDoc arg ) {
81         return new IsSame(arg);
82     }
83 }
84
Popular Tags