1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockito.internal.invocation; 6 7 import static java.util.Arrays.asList; 8 9 import static org.mockito.internal.invocation.InterceptedInvocation.NO_OP; 10 11 import java.lang.reflect.Method; 12 import java.util.LinkedList; 13 import java.util.List; 14 15 import org.mockito.Mockito; 16 import org.mockito.internal.debugging.LocationImpl; 17 import org.mockito.internal.invocation.mockref.MockReference; 18 import org.mockito.internal.invocation.mockref.MockStrongReference; 19 import org.mockito.invocation.Invocation; 20 import org.mockito.invocation.Location; 21 import org.mockitousage.IMethods; 22 23 /** 24 * Build an invocation. 25 */ 26 @SuppressWarnings("unchecked") 27 public class InvocationBuilder { 28 29 private String methodName = "simpleMethod"; 30 private int sequenceNumber = 0; 31 private Object[] args = new Object[] {}; 32 private Object mock = Mockito.mock(IMethods.class); 33 private Method method; 34 private boolean verified; 35 private List<Class<?>> argTypes; 36 private Location location; 37 38 /** 39 * Build the invocation 40 * <p> 41 * If the method was not specified, use IMethods methods. 42 * 43 * @return invocation 44 */ toInvocation()45 public Invocation toInvocation() { 46 if (method == null) { 47 if (argTypes == null) { 48 argTypes = new LinkedList<Class<?>>(); 49 for (Object arg : args) { 50 if (arg == null) { 51 argTypes.add(Object.class); 52 } else { 53 argTypes.add(arg.getClass()); 54 } 55 } 56 } 57 58 try { 59 method = 60 IMethods.class.getMethod( 61 methodName, argTypes.toArray(new Class[argTypes.size()])); 62 } catch (Exception e) { 63 throw new RuntimeException( 64 "builder only creates invocations of IMethods interface", e); 65 } 66 } 67 68 Invocation i = 69 createInvocation( 70 new MockStrongReference<Object>(mock, false), 71 new SerializableMethod(method), 72 args, 73 NO_OP, 74 location == null ? new LocationImpl() : location, 75 1); 76 if (verified) { 77 i.markVerified(); 78 } 79 return i; 80 } 81 createInvocation( MockReference<Object> mockRef, MockitoMethod mockitoMethod, Object[] arguments, RealMethod realMethod, Location location, int sequenceNumber)82 protected Invocation createInvocation( 83 MockReference<Object> mockRef, 84 MockitoMethod mockitoMethod, 85 Object[] arguments, 86 RealMethod realMethod, 87 Location location, 88 int sequenceNumber) { 89 return new InterceptedInvocation( 90 mockRef, mockitoMethod, arguments, realMethod, location, sequenceNumber); 91 } 92 method(String methodName)93 public InvocationBuilder method(String methodName) { 94 this.methodName = methodName; 95 return this; 96 } 97 seq(int sequenceNumber)98 public InvocationBuilder seq(int sequenceNumber) { 99 this.sequenceNumber = sequenceNumber; 100 return this; 101 } 102 args(Object... args)103 public InvocationBuilder args(Object... args) { 104 this.args = args; 105 return this; 106 } 107 arg(Object o)108 public InvocationBuilder arg(Object o) { 109 this.args = new Object[] {o}; 110 return this; 111 } 112 mock(Object mock)113 public InvocationBuilder mock(Object mock) { 114 this.mock = mock; 115 return this; 116 } 117 method(Method method)118 public InvocationBuilder method(Method method) { 119 this.method = method; 120 return this; 121 } 122 verified()123 public InvocationBuilder verified() { 124 this.verified = true; 125 return this; 126 } 127 toInvocationMatcher()128 public InvocationMatcher toInvocationMatcher() { 129 return new InvocationMatcher(toInvocation()); 130 } 131 simpleMethod()132 public InvocationBuilder simpleMethod() { 133 return this.method("simpleMethod"); 134 } 135 differentMethod()136 public InvocationBuilder differentMethod() { 137 return this.method("differentMethod"); 138 } 139 argTypes(Class<?>.... argTypes)140 public InvocationBuilder argTypes(Class<?>... argTypes) { 141 this.argTypes = asList(argTypes); 142 return this; 143 } 144 location(final String location)145 public InvocationBuilder location(final String location) { 146 this.location = 147 new Location() { 148 public String toString() { 149 return location; 150 } 151 152 public String getSourceFile() { 153 return "SomeClass"; 154 } 155 }; 156 return this; 157 } 158 } 159