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