1 3 package test.jmock.core; 4 5 import java.lang.reflect.Method ; 6 import junit.framework.TestCase; 7 import org.jmock.core.Invocation; 8 import org.jmock.core.InvocationMatcher; 9 import org.jmock.core.InvocationMocker; 10 import org.jmock.core.matcher.StatelessInvocationMatcher; 11 import org.jmock.util.Verifier; 12 import test.jmock.core.testsupport.MethodFactory; 13 import test.jmock.core.testsupport.MockInvocationMatcher; 14 import test.jmock.core.testsupport.MockStub; 15 16 17 public class InvocationMockerTest extends TestCase 18 { 19 private InvocationMatcher matchAll = new StatelessInvocationMatcher() 20 { 21 public boolean matches( Invocation invocation ) { 22 return true; 23 } 24 25 public StringBuffer describeTo( StringBuffer buffer ) { 26 return buffer.append("match all"); 27 } 28 }; 29 private InvocationMatcher matchNone = new StatelessInvocationMatcher() 30 { 31 public boolean matches( Invocation invocation ) { 32 return false; 33 } 34 35 public StringBuffer describeTo( StringBuffer buffer ) { 36 return buffer.append("match none"); 37 } 38 }; 39 40 private static final String ARG2 = "arg2"; 41 private static final String ARG1 = "arg1"; 42 43 private Invocation exampleInvocation; 44 private InvocationMocker invocationMocker; 45 46 public void setUp() { 47 MethodFactory methodFactory = new MethodFactory(); 48 Method method = methodFactory.newMethod("example", new Class []{String .class, String .class}, Void .class, 49 MethodFactory.NO_EXCEPTIONS); 50 51 exampleInvocation = new Invocation(new Object (), method, new Object []{ARG1, ARG2}); 52 invocationMocker = new InvocationMocker(); 53 } 54 55 public void testCanAddMatchers() throws Throwable { 56 MockInvocationMatcher mockInvocationMatcher = new MockInvocationMatcher(); 57 invocationMocker.addMatcher(mockInvocationMatcher); 58 59 mockInvocationMatcher.matchesInvocation.setExpected(exampleInvocation); 60 mockInvocationMatcher.invokedInvocation.setExpected(exampleInvocation); 61 62 invocationMocker.matches(exampleInvocation); 63 invocationMocker.invoke(exampleInvocation); 64 65 Verifier.verifyObject(mockInvocationMatcher); 66 } 67 68 public void testMatchesIfAllMatchersMatch() { 69 invocationMocker.addMatcher(matchAll); 70 invocationMocker.addMatcher(matchAll); 71 72 assertTrue("Should have matched", invocationMocker.matches(exampleInvocation)); 73 } 74 75 public void testDoesNotMatchIfAnyMatcherDoesNotMatch() { 76 invocationMocker.addMatcher(matchAll); 77 invocationMocker.addMatcher(matchNone); 78 79 assertFalse("Should not have matched", invocationMocker.matches(exampleInvocation)); 80 } 81 82 public void testMatchesInvocationBeforeCallingStub() throws Throwable { 83 MockInvocationMatcher mockInvocationMatcher = new MockInvocationMatcher(); 84 invocationMocker.addMatcher(mockInvocationMatcher); 85 86 mockInvocationMatcher.invokedInvocation.setExpected(exampleInvocation); 87 88 invocationMocker.invoke(exampleInvocation); 89 90 Verifier.verifyObject(mockInvocationMatcher); 91 } 92 93 public void testDelegatesVerifyToInvocationMatchers() throws Throwable { 94 MockInvocationMatcher mockInvocationMatcher = new MockInvocationMatcher(); 95 invocationMocker.addMatcher(mockInvocationMatcher); 96 97 mockInvocationMatcher.verifyCalls.setExpected(1); 98 99 invocationMocker.verify(); 100 101 Verifier.verifyObject(mockInvocationMatcher); 102 } 103 104 public void testDelegatesInvocationToStubObject() throws Throwable { 105 MockStub mockStub = new MockStub(); 106 invocationMocker.setStub(mockStub); 107 108 String stubResult = "stub result"; 109 110 mockStub.invokeInvocation.setExpected(exampleInvocation); 111 mockStub.invokeResult = stubResult; 112 113 assertEquals("Should be invoke result", stubResult, 114 invocationMocker.invoke(exampleInvocation)); 115 116 Verifier.verifyObject(mockStub); 117 } 118 119 public void testCanBeNamed() { 120 String name = "~{MOCKER-NAME}~"; 121 InvocationMocker mocker = new InvocationMocker(); 122 123 mocker.setName(name); 124 125 String description = mocker.describeTo(new StringBuffer ()).toString(); 126 assertTrue("name should be in description", 127 description.indexOf(name) >= 0); 128 } 129 130 public void testDelegatesRequestForDescriptionDescriberObject() { 131 MockDescriber mockDescriber = new MockDescriber(); 132 MockInvocationMatcher mockMatcher1 = new MockInvocationMatcher(); 133 MockInvocationMatcher mockMatcher2 = new MockInvocationMatcher(); 134 MockStub mockStub = new MockStub(); 135 String invocationMockerName = "INVOCATION-MOCKER-NAME"; 136 StringBuffer buf = new StringBuffer (); 137 138 139 invocationMocker = new InvocationMocker(mockDescriber); 140 invocationMocker.addMatcher(mockMatcher1); 141 invocationMocker.addMatcher(mockMatcher2); 142 invocationMocker.setStub(mockStub); 143 invocationMocker.setName(invocationMockerName); 144 145 mockDescriber.hasDescriptionCalls.setExpected(1); 146 mockDescriber.hasDescriptionResult = true; 147 148 mockDescriber.describeToBuf.setExpected(buf); 149 mockDescriber.describeToMatchers.addActual(mockMatcher1); 150 mockDescriber.describeToMatchers.addActual(mockMatcher2); 151 mockDescriber.describeToStub.setExpected(mockStub); 152 mockDescriber.describeToName.setExpected(invocationMockerName); 153 154 assertTrue("should have description", invocationMocker.hasDescription()); 155 invocationMocker.describeTo(buf); 156 157 mockDescriber.verify(); 158 } 159 } 160 | Popular Tags |