1 3 package test.jmock.builder; 4 5 import junit.framework.AssertionFailedError; 6 7 import org.jmock.builder.InvocationMockerBuilder; 8 import org.jmock.core.Constraint; 9 import org.jmock.core.InvocationMatcher; 10 import org.jmock.core.MockObjectSupportTestCase; 11 import org.jmock.core.Stub; 12 import org.jmock.core.matcher.AnyArgumentsMatcher; 13 import org.jmock.core.matcher.ArgumentsMatcher; 14 import org.jmock.core.matcher.MethodNameMatcher; 15 import org.jmock.core.matcher.NoArgumentsMatcher; 16 import org.jmock.core.stub.VoidStub; 17 import org.jmock.expectation.AssertMo; 18 import org.jmock.util.Dummy; 19 20 import test.jmock.builder.testsupport.MockBuilderIdentityTable; 21 import test.jmock.builder.testsupport.MockStubMatchersCollection; 22 23 24 public class InvocationMockerBuilderTest extends MockObjectSupportTestCase 25 { 26 public interface MockedInterface { 27 void method(); 28 } 29 30 private MockStubMatchersCollection mocker; 31 private MockBuilderIdentityTable idTable; 32 private InvocationMockerBuilder builder; 33 34 public void setUp() { 35 mocker = new MockStubMatchersCollection(); 36 idTable = new MockBuilderIdentityTable(); 37 38 builder = new InvocationMockerBuilder(mocker, idTable, MockedInterface.class); 39 } 40 41 public void testSpecifyingMethodNameNameAddsMethodNameMatcherAndAddsSelfToIdentityTable() { 42 mocker.addedMatcherType.setExpected(MethodNameMatcher.class); 43 idTable.registerMethodName.setExpected("method"); 44 idTable.registerMethodNameBuilder.setExpected(builder); 45 46 assertNotNull("Should be Stub Builder", builder.method("method")); 47 48 mocker.verifyExpectations(); 49 idTable.verify(); 50 } 51 52 public void testSpecifyingMethodWithIllegalNameThrowsIllegalArgumentError() { 53 String illegalMethodName = "illegalMethodName()"; 54 55 try { 56 builder.method(illegalMethodName); 57 } 58 catch( IllegalArgumentException ex ) { 59 AssertMo.assertIncludes( "should contain illegal method name", 60 illegalMethodName, ex.getMessage() ); 61 return; 62 } 63 fail("should have thrown IllegalArgumentException"); 64 } 65 66 public void testMethodNameNotInMockedTypeCausesTestFailure() { 67 String methodNameNotInMockedInterface = "methodNameNotInMockedInterface"; 68 69 try { 70 builder.method(methodNameNotInMockedInterface); 71 } 72 catch( AssertionFailedError ex ) { 73 AssertMo.assertIncludes( "should contain wrong method name", 74 methodNameNotInMockedInterface, ex.getMessage() ); 75 AssertMo.assertIncludes( "should contain name of mocked type", 76 MockedInterface.class.getName(), ex.getMessage() ); 77 return; 78 } 79 fail("should have thrown AssertionFailedError"); 80 } 81 82 public void testSpecifyingMethodWithConstraintAddsMethodNameMatcherButDoesNotAddSelfToIdentityTable() { 83 Constraint nameConstraint = (Constraint)newDummy(Constraint.class, "nameConstraint"); 84 85 mocker.addedMatcherType.setExpected(MethodNameMatcher.class); 86 87 assertNotNull("Should be Stub Builder", builder.method(nameConstraint)); 88 89 mocker.verifyExpectations(); 90 } 91 92 public void testCanAddCustomMatcher() { 93 InvocationMatcher matcher = 94 (InvocationMatcher)Dummy.newDummy(InvocationMatcher.class, "matcher"); 95 96 mocker.addedMatcher.setExpected(matcher); 97 98 assertNotNull("Should be Stub Builder", builder.match(matcher)); 99 100 mocker.verifyExpectations(); 101 } 102 103 public void testWithMethodAddsArgumentsMatcher() { 104 mocker.addedMatcherType.setExpected(ArgumentsMatcher.class); 105 106 assertNotNull("Should be Stub Builder", builder.with(new Constraint[0])); 107 108 mocker.verifyExpectations(); 109 } 110 111 public void testWithMethodWithOneObjectArgumentAddsArgumentsMatcher() { 112 mocker.addedMatcherType.setExpected(ArgumentsMatcher.class); 113 114 assertNotNull("Should be Stub Builder", builder.with(eq(new Object ()))); 115 116 mocker.verifyExpectations(); 117 } 118 119 public void testWithMethodWithTwoObjectArgumentsAddsArgumentsMatcher() { 120 mocker.addedMatcherType.setExpected(ArgumentsMatcher.class); 121 122 assertNotNull("Should be Stub Builder", 123 builder.with(eq(new Object ()), eq(new Object ()))); 124 125 mocker.verifyExpectations(); 126 } 127 128 public void testNoParamsAddsNoArgumentMatcher() { 129 mocker.addedMatcher.setExpected(NoArgumentsMatcher.INSTANCE); 130 131 assertNotNull("Should be Stub Builder", builder.withNoArguments()); 132 133 mocker.verifyExpectations(); 134 } 135 136 public void testAnyParamsAddsAnyArgumentMatcher() { 137 mocker.addedMatcher.setExpected(AnyArgumentsMatcher.INSTANCE); 138 139 assertNotNull("Should be Stub Builder", builder.withAnyArguments()); 140 141 mocker.verifyExpectations(); 142 } 143 144 public void testCanSetCustomStub() { 145 Stub stub = (Stub)Dummy.newDummy(Stub.class, "stub"); 146 147 mocker.setStub.setExpected(stub); 148 149 assertNotNull("should be expectation builder", builder.will(stub)); 150 } 151 152 public void testIsVoidSetsVoidStub() { 153 mocker.setStubType.setExpected(VoidStub.class); 154 155 assertNotNull("Should be expectation builder", builder.isVoid()); 156 157 mocker.verifyExpectations(); 158 } 159 160 static final String INVOCATION_ID = "INVOCATION-ID"; 161 162 public void testUniquelyIdentifyInvocationMockerAndRegisterItselfInBuilderIdentityTable() { 163 mocker.setName.setExpected(INVOCATION_ID); 164 idTable.registerUniqueID.setExpected(INVOCATION_ID); 165 idTable.registerUniqueIDBuilder.setExpected(builder); 166 167 builder.id(INVOCATION_ID); 168 169 idTable.verify(); 170 mocker.verifyExpectations(); 171 } 172 } 173 | Popular Tags |