1 3 package org.jmock.builder; 4 5 import java.lang.reflect.Method ; 6 import junit.framework.AssertionFailedError; 7 import org.jmock.core.Constraint; 8 import org.jmock.core.InvocationMatcher; 9 import org.jmock.core.Stub; 10 import org.jmock.core.StubMatchersCollection; 11 import org.jmock.core.matcher.*; 12 import org.jmock.core.stub.VoidStub; 13 14 15 public class InvocationMockerBuilder 16 implements NameMatchBuilder 17 { 18 private StubMatchersCollection mocker; 19 private BuilderNamespace builderNamespace; 20 private Class mockedType; 21 22 public InvocationMockerBuilder( StubMatchersCollection mocker, 23 BuilderNamespace idTable, 24 Class mockedType ) 25 { 26 this.mocker = mocker; 27 this.builderNamespace = idTable; 28 this.mockedType = mockedType; 29 } 30 31 public ArgumentsMatchBuilder method( Constraint nameConstraint ) { 32 return addMatcher(new MethodNameMatcher(nameConstraint)); 33 } 34 35 public ArgumentsMatchBuilder method( String name ) { 36 checkLegalMethodName(name); 37 checkExistingMethodName(name); 38 39 addMatcher(new MethodNameMatcher(name)); 40 builderNamespace.registerMethodName(name, this); 41 42 return this; 43 } 44 45 private void checkLegalMethodName( String name ) { 46 if( !isLegalMethodName(name) ) { 47 throw new IllegalArgumentException ("illegal method name: " + name + " is not a legal Java identifier"); 48 } 49 } 50 51 private boolean isLegalMethodName( String name ) { 52 if( !Character.isJavaIdentifierStart(name.charAt(0)) ) return false; 53 for( int i = 1; i < name.length(); i++ ) { 54 if( !Character.isJavaIdentifierPart(name.charAt(i))) return false; 55 } 56 return true; 57 } 58 59 private void checkExistingMethodName( String name ) { 60 if( !typeDefinesMethodNamed(name) ) { 61 throw new AssertionFailedError("no method named " + name + " is defined in type "+mockedType); 62 } 63 } 64 65 private boolean typeDefinesMethodNamed( String name ) { 66 Method [] methods = mockedType.getMethods(); 67 68 for( int i = 0; i < methods.length; i++ ) { 69 if( methods[i].getName().equals(name) ) return true; 70 } 71 return false; 72 } 73 74 public MatchBuilder match( InvocationMatcher customMatcher ) { 75 return addMatcher(customMatcher); 76 } 77 78 public MatchBuilder with( Constraint arg1 ) { 79 return with(new Constraint[]{arg1}); 80 } 81 82 public MatchBuilder with( Constraint arg1, Constraint arg2 ) { 83 return with(new Constraint[]{arg1, arg2}); 84 } 85 86 public MatchBuilder with( Constraint arg1, Constraint arg2, Constraint arg3 ) { 87 return with(new Constraint[]{arg1, arg2, arg3}); 88 } 89 90 public MatchBuilder with( Constraint arg1, Constraint arg2, Constraint arg3, Constraint arg4 ) { 91 return with(new Constraint[]{arg1, arg2, arg3, arg4}); 92 } 93 94 public MatchBuilder with( Constraint[] constraints ) { 95 return addMatcher(new ArgumentsMatcher(constraints)); 96 } 97 98 public MatchBuilder withNoArguments() { 99 return addMatcher(NoArgumentsMatcher.INSTANCE); 100 } 101 102 public MatchBuilder withAnyArguments() { 103 return addMatcher(AnyArgumentsMatcher.INSTANCE); 104 } 105 106 public IdentityBuilder will( Stub stubAction ) { 107 setStub(stubAction); 108 return this; 109 } 110 111 public IdentityBuilder isVoid() { 112 setStub(VoidStub.INSTANCE); 113 return this; 114 } 115 116 private void setStub( Stub stubAction ) { 117 mocker.setStub(stubAction); 118 } 119 120 public IdentityBuilder expect( InvocationMatcher expectation ) { 121 return addMatcher(expectation); 122 } 123 124 public void id( String invocationID ) { 125 mocker.setName(invocationID); 126 builderNamespace.registerUniqueID(invocationID, this); 127 } 128 129 public MatchBuilder after( String priorCallID ) { 130 setupOrderingMatchers(builderNamespace, priorCallID, priorCallID); 131 return this; 132 } 133 134 public MatchBuilder after( BuilderNamespace otherMock, String priorCallID ) { 135 setupOrderingMatchers(otherMock, priorCallID, priorCallID + " on " + otherMock); 136 return this; 137 } 138 139 private void setupOrderingMatchers( BuilderNamespace idTable, 140 String priorCallID, 141 String priorCallDescription ) { 142 MatchBuilder priorCallBuilder = idTable.lookupID(priorCallID); 143 144 if (priorCallBuilder == this) { 145 throw new AssertionFailedError("confusing identifier of prior invocation \"" + priorCallID + "\"; " + 146 "give it an explicit call identifier"); 147 } 148 149 InvokedRecorder priorCallRecorder = new InvokedRecorder(); 150 151 priorCallBuilder.match(priorCallRecorder); 152 153 mocker.addMatcher(new InvokedAfterMatcher(priorCallRecorder, 154 priorCallDescription)); 155 } 156 157 private InvocationMockerBuilder addMatcher( InvocationMatcher matcher ) { 158 mocker.addMatcher(matcher); 159 return this; 160 } 161 } 162 | Popular Tags |