How to use CapturesArguments class of org.mockito.internal.matchers package

Best Mockito code snippet using org.mockito.internal.matchers.CapturesArguments

Source:src_org_mockito_internal_invocation_InvocationMatcher.java Github

copy

Full Screen

...9import java.util.Collections;10import java.util.LinkedList;11import java.util.List;12import org.hamcrest.Matcher;13import org.mockito.internal.matchers.CapturesArguments;14import org.mockito.internal.matchers.MatcherDecorator;15import org.mockito.internal.matchers.VarargMatcher;16import org.mockito.internal.reporting.PrintSettings;17import org.mockito.invocation.DescribedInvocation;18import org.mockito.invocation.Invocation;19import org.mockito.invocation.Location;20@SuppressWarnings("unchecked")21public class InvocationMatcher implements DescribedInvocation, CapturesArgumensFromInvocation, Serializable {22 private static final long serialVersionUID = -3047126096857467610L;23 private final Invocation invocation;24 private final List<Matcher> matchers;25 public InvocationMatcher(Invocation invocation, List<Matcher> matchers) {26 this.invocation = invocation;27 if (matchers.isEmpty()) {28 this.matchers = ArgumentsProcessor.argumentsToMatchers(invocation.getArguments());29 } else {30 this.matchers = matchers;31 }32 }33 34 public InvocationMatcher(Invocation invocation) {35 this(invocation, Collections.<Matcher>emptyList());36 }37 public Method getMethod() {38 return invocation.getMethod();39 }40 41 public Invocation getInvocation() {42 return this.invocation;43 }44 45 public List<Matcher> getMatchers() {46 return this.matchers;47 }48 49 public String toString() {50 return new PrintSettings().print(matchers, invocation);51 }52 public boolean matches(Invocation actual) {53 return invocation.getMock().equals(actual.getMock())54 && hasSameMethod(actual)55 && new ArgumentsComparator().argumentsMatch(this, actual);56 }57 private boolean safelyArgumentsMatch(Object[] actualArgs) {58 try {59 return new ArgumentsComparator().argumentsMatch(this, actualArgs);60 } catch (Throwable t) {61 return false;62 }63 }64 /**65 * similar means the same method name, same mock, unverified 66 * and: if arguments are the same cannot be overloaded67 */68 public boolean hasSimilarMethod(Invocation candidate) {69 String wantedMethodName = getMethod().getName();70 String currentMethodName = candidate.getMethod().getName();71 72 final boolean methodNameEquals = wantedMethodName.equals(currentMethodName);73 final boolean isUnverified = !candidate.isVerified();74 final boolean mockIsTheSame = getInvocation().getMock() == candidate.getMock();75 final boolean methodEquals = hasSameMethod(candidate);76 if (!methodNameEquals || !isUnverified || !mockIsTheSame) {77 return false;78 }79 final boolean overloadedButSameArgs = !methodEquals && safelyArgumentsMatch(candidate.getArguments());80 return !overloadedButSameArgs;81 }82 public boolean hasSameMethod(Invocation candidate) {83 //not using method.equals() for 1 good reason:84 //sometimes java generates forwarding methods when generics are in play see JavaGenericsForwardingMethodsTest85 Method m1 = invocation.getMethod();86 Method m2 = candidate.getMethod();87 88 if (m1.getName() != null && m1.getName().equals(m2.getName())) {89 /* Avoid unnecessary cloning */90 Class[] params1 = m1.getParameterTypes();91 Class[] params2 = m2.getParameterTypes();92 if (params1.length == params2.length) {93 for (int i = 0; i < params1.length; i++) {94 if (params1[i] != params2[i])95 return false;96 }97 return true;98 }99 }100 return false;101 }102 103 public Location getLocation() {104 return invocation.getLocation();105 }106 public void captureArgumentsFrom(Invocation invocation) {107 if (invocation.getMethod().isVarArgs()) {108 int indexOfVararg = invocation.getRawArguments().length - 1;109 for (int position = 0; position < indexOfVararg; position++) {110 Matcher m = matchers.get(position);111 if (m instanceof CapturesArguments) {112 ((CapturesArguments) m).captureFrom(invocation.getArgumentAt(position, Object.class));113 }114 }115 for (int position = indexOfVararg; position < matchers.size(); position++) {116 Matcher m = matchers.get(position);117 if (m instanceof CapturesArguments) {118 ((CapturesArguments) m).captureFrom(invocation.getRawArguments()[position - indexOfVararg]);119 }120 }121 } else {122 for (int position = 0; position < matchers.size(); position++) {123 Matcher m = matchers.get(position);124 if (m instanceof CapturesArguments) {125 ((CapturesArguments) m).captureFrom(invocation.getArgumentAt(position, Object.class));126 }127 }128 }129// for (int position = 0; position < matchers.size(); position++) {130// Matcher m = matchers.get(position);131// if (m instanceof CapturesArguments && invocation.getRawArguments().length > position) {132// //TODO SF - this whole lot can be moved captureFrom implementation133// if(isVariableArgument(invocation, position) && isVarargMatcher(m)) {134// Object array = invocation.getRawArguments()[position];135// for (int i = 0; i < Array.getLength(array); i++) {136// ((CapturesArguments) m).captureFrom(Array.get(array, i));137// }138// //since we've captured all varargs already, it does not make sense to process other matchers.139// return;140// } else {141// ((CapturesArguments) m).captureFrom(invocation.getRawArguments()[position]);142// }143// }144// }145 }146 private boolean isVarargMatcher(Matcher matcher) {147 Matcher actualMatcher = matcher;148 if (actualMatcher instanceof MatcherDecorator) {149 actualMatcher = ((MatcherDecorator) actualMatcher).getActualMatcher();150 }151 return actualMatcher instanceof VarargMatcher;152 }153 private boolean isVariableArgument(Invocation invocation, int position) {154 return invocation.getRawArguments().length - 1 == position155 && invocation.getRawArguments()[position] != null...

Full Screen

Full Screen

Source:InvocationMatcher.java Github

copy

Full Screen

...9import java.util.Collections;10import java.util.LinkedList;11import java.util.List;12import org.hamcrest.Matcher;13import org.mockito.internal.matchers.CapturesArguments;14import org.mockito.internal.matchers.MatcherDecorator;15import org.mockito.internal.matchers.VarargMatcher;16import org.mockito.internal.reporting.PrintSettings;17import org.mockito.invocation.DescribedInvocation;18import org.mockito.invocation.Invocation;19import org.mockito.invocation.Location;20@SuppressWarnings("unchecked")21public class InvocationMatcher implements DescribedInvocation, CapturesArgumensFromInvocation, Serializable {22 private static final long serialVersionUID = -3047126096857467610L;23 private final Invocation invocation;24 private final List<Matcher> matchers;25 public InvocationMatcher(Invocation invocation, List<Matcher> matchers) {26 this.invocation = invocation;27 if (matchers.isEmpty()) {28 this.matchers = ArgumentsProcessor.argumentsToMatchers(invocation.getArguments());29 } else {30 this.matchers = matchers;31 }32 }33 34 public InvocationMatcher(Invocation invocation) {35 this(invocation, Collections.<Matcher>emptyList());36 }37 public Method getMethod() {38 return invocation.getMethod();39 }40 41 public Invocation getInvocation() {42 return this.invocation;43 }44 45 public List<Matcher> getMatchers() {46 return this.matchers;47 }48 49 public String toString() {50 return new PrintSettings().print(matchers, invocation);51 }52 public boolean matches(Invocation actual) {53 return invocation.getMock().equals(actual.getMock())54 && hasSameMethod(actual)55 && new ArgumentsComparator().argumentsMatch(this, actual);56 }57 private boolean safelyArgumentsMatch(Object[] actualArgs) {58 try {59 return new ArgumentsComparator().argumentsMatch(this, actualArgs);60 } catch (Throwable t) {61 return false;62 }63 }64 /**65 * similar means the same method name, same mock, unverified 66 * and: if arguments are the same cannot be overloaded67 */68 public boolean hasSimilarMethod(Invocation candidate) {69 String wantedMethodName = getMethod().getName();70 String currentMethodName = candidate.getMethod().getName();71 72 final boolean methodNameEquals = wantedMethodName.equals(currentMethodName);73 final boolean isUnverified = !candidate.isVerified();74 final boolean mockIsTheSame = getInvocation().getMock() == candidate.getMock();75 final boolean methodEquals = hasSameMethod(candidate);76 if (!methodNameEquals || !isUnverified || !mockIsTheSame) {77 return false;78 }79 final boolean overloadedButSameArgs = !methodEquals && safelyArgumentsMatch(candidate.getArguments());80 return !overloadedButSameArgs;81 }82 public boolean hasSameMethod(Invocation candidate) {83 //not using method.equals() for 1 good reason:84 //sometimes java generates forwarding methods when generics are in play see JavaGenericsForwardingMethodsTest85 Method m1 = invocation.getMethod();86 Method m2 = candidate.getMethod();87 88 if (m1.getName() != null && m1.getName().equals(m2.getName())) {89 /* Avoid unnecessary cloning */90 Class[] params1 = m1.getParameterTypes();91 Class[] params2 = m2.getParameterTypes();92 if (params1.length == params2.length) {93 for (int i = 0; i < params1.length; i++) {94 if (params1[i] != params2[i])95 return false;96 }97 return true;98 }99 }100 return false;101 }102 103 public Location getLocation() {104 return invocation.getLocation();105 }106 public void captureArgumentsFrom(Invocation invocation) {107 if (invocation.getMethod().isVarArgs()) {108 int indexOfVararg = invocation.getRawArguments().length - 1;109 for (int position = 0; position < indexOfVararg; position++) {110 Matcher m = matchers.get(position);111 if (m instanceof CapturesArguments) {112 ((CapturesArguments) m).captureFrom(invocation.getArgumentAt(position, Object.class));113 }114 }115 for (int position = indexOfVararg; position < matchers.size(); position++) {116 Matcher m = matchers.get(position);117 if (m instanceof CapturesArguments) {118 ((CapturesArguments) m).captureFrom(invocation.getRawArguments()[position - indexOfVararg]);119 }120 }121 } else {122 for (int position = 0; position < matchers.size(); position++) {123 Matcher m = matchers.get(position);124 if (m instanceof CapturesArguments) {125 ((CapturesArguments) m).captureFrom(invocation.getArgumentAt(position, Object.class));126 }127 }128 }129// for (int position = 0; position < matchers.size(); position++) {130// Matcher m = matchers.get(position);131// if (m instanceof CapturesArguments && invocation.getRawArguments().length > position) {132// //TODO SF - this whole lot can be moved captureFrom implementation133// if(isVariableArgument(invocation, position) && isVarargMatcher(m)) {134// Object array = invocation.getRawArguments()[position];135// for (int i = 0; i < Array.getLength(array); i++) {136// ((CapturesArguments) m).captureFrom(Array.get(array, i));137// }138// //since we've captured all varargs already, it does not make sense to process other matchers.139// return;140// } else {141// ((CapturesArguments) m).captureFrom(invocation.getRawArguments()[position]);142// }143// }144// }145 }146 private boolean isVarargMatcher(Matcher matcher) {147 Matcher actualMatcher = matcher;148 if (actualMatcher instanceof MatcherDecorator) {149 actualMatcher = ((MatcherDecorator) actualMatcher).getActualMatcher();150 }151 return actualMatcher instanceof VarargMatcher;152 }153 private boolean isVariableArgument(Invocation invocation, int position) {154 return invocation.getRawArguments().length - 1 == position155 && invocation.getRawArguments()[position] != null...

Full Screen

Full Screen

Source:Math_rank-5_old.java Github

copy

Full Screen

...9import java.util.Collections;10import java.util.LinkedList;11import java.util.List;12import org.hamcrest.Matcher;13import org.mockito.internal.matchers.CapturesArguments;14import org.mockito.internal.matchers.MatcherDecorator;15import org.mockito.internal.matchers.VarargMatcher;16import org.mockito.internal.reporting.PrintSettings;17import org.mockito.invocation.DescribedInvocation;18import org.mockito.invocation.Invocation;19import org.mockito.invocation.Location;20@SuppressWarnings("unchecked")21public class InvocationMatcher implements DescribedInvocation, CapturesArgumensFromInvocation, Serializable {22 private static final long serialVersionUID = -3047126096857467610L;23 private final Invocation invocation;24 private final List<Matcher> matchers;25 public InvocationMatcher(Invocation invocation, List<Matcher> matchers) {26 this.invocation = invocation;27 if (matchers.isEmpty()) {28 this.matchers = ArgumentsProcessor.argumentsToMatchers(invocation.getArguments());29 } else {30 this.matchers = matchers;31 }32 }33 34 public InvocationMatcher(Invocation invocation) {35 this(invocation, Collections.<Matcher>emptyList());36 }37 public Method getMethod() {38 return invocation.getMethod();39 }40 41 public Invocation getInvocation() {42 return this.invocation;43 }44 45 public List<Matcher> getMatchers() {46 return this.matchers;47 }48 49 public String toString() {50 return new PrintSettings().print(matchers, invocation);51 }52 public boolean matches(Invocation actual) {53 return invocation.getMock().equals(actual.getMock())54 && hasSameMethod(actual)55 && new ArgumentsComparator().argumentsMatch(this, actual);56 }57 private boolean safelyArgumentsMatch(Object[] actualArgs) {58 try {59 return new ArgumentsComparator().argumentsMatch(this, actualArgs);60 } catch (Throwable t) {61 return false;62 }63 }64 /**65 * similar means the same method name, same mock, unverified 66 * and: if arguments are the same cannot be overloaded67 */68 public boolean hasSimilarMethod(Invocation candidate) {69 String wantedMethodName = getMethod().getName();70 String currentMethodName = candidate.getMethod().getName();71 72 final boolean methodNameEquals = wantedMethodName.equals(currentMethodName);73 final boolean isUnverified = !candidate.isVerified();74 final boolean mockIsTheSame = getInvocation().getMock() == candidate.getMock();75 final boolean methodEquals = hasSameMethod(candidate);76 if (!methodNameEquals || !isUnverified || !mockIsTheSame) {77 return false;78 }79 final boolean overloadedButSameArgs = !methodEquals && safelyArgumentsMatch(candidate.getArguments());80 return !overloadedButSameArgs;81 }82 public boolean hasSameMethod(Invocation candidate) {83 //not using method.equals() for 1 good reason:84 //sometimes java generates forwarding methods when generics are in play see JavaGenericsForwardingMethodsTest85 Method m1 = invocation.getMethod();86 Method m2 = candidate.getMethod();87 88 if (m1.getName() != null && m1.getName().equals(m2.getName())) {89 /* Avoid unnecessary cloning */90 Class[] params1 = m1.getParameterTypes();91 Class[] params2 = m2.getParameterTypes();92 if (params1.length == params2.length) {93 for (int i = 0; i < params1.length; i++) {94 if (params1[i] != params2[i])95 return false;96 }97 return true;98 }99 }100 return false;101 }102 103 public Location getLocation() {104 return invocation.getLocation();105 }106 public void captureArgumentsFrom(Invocation invocation) {107 if (invocation.getMethod().isVarArgs()) {108 int indexOfVararg = invocation.getRawArguments().length - 1;109 for (int position = 0; position < indexOfVararg; position++) {110 Matcher m = matchers.get(position);111 if (m instanceof CapturesArguments) {112 ((CapturesArguments) m).captureFrom(invocation.getArgumentAt(position, Object.class));113 }114 }115 for (int position = indexOfVararg; position < matchers.size(); position++) {116 Matcher m = matchers.get(position);117 if (m instanceof CapturesArguments) {118 ((CapturesArguments) m).captureFrom(invocation.getRawArguments()[position - indexOfVararg]);119 }120 }121 } else {122 for (int position = 0; position < matchers.size(); position++) {123 Matcher m = matchers.get(position);124 if (m instanceof CapturesArguments) {125 ((CapturesArguments) m).captureFrom(invocation.getArgumentAt(position, Object.class));126 }127 }128 }129// for (int position = 0; position < matchers.size(); position++) {130// Matcher m = matchers.get(position);131// if (m instanceof CapturesArguments && invocation.getRawArguments().length > position) {132// //TODO SF - this whole lot can be moved captureFrom implementation133// if(isVariableArgument(invocation, position) && isVarargMatcher(m)) {134// Object array = invocation.getRawArguments()[position];135// for (int i = 0; i < Array.getLength(array); i++) {136// ((CapturesArguments) m).captureFrom(Array.get(array, i));137// }138// //since we've captured all varargs already, it does not make sense to process other matchers.139// return;140// } else {141// ((CapturesArguments) m).captureFrom(invocation.getRawArguments()[position]);142// }143// }144// }145 }146 private boolean isVarargMatcher(Matcher matcher) {147 Matcher actualMatcher = matcher;148 if (actualMatcher instanceof MatcherDecorator) {149 actualMatcher = ((MatcherDecorator) actualMatcher).getActualMatcher();150 }151 return actualMatcher instanceof VarargMatcher;152 }153 private boolean isVariableArgument(Invocation invocation, int position) {154 return invocation.getRawArguments().length - 1 == position155 && invocation.getRawArguments()[position] != null...

Full Screen

Full Screen

Source:Math_rank-4_old.java Github

copy

Full Screen

...9import java.util.Collections;10import java.util.LinkedList;11import java.util.List;12import org.hamcrest.Matcher;13import org.mockito.internal.matchers.CapturesArguments;14import org.mockito.internal.matchers.MatcherDecorator;15import org.mockito.internal.matchers.VarargMatcher;16import org.mockito.internal.reporting.PrintSettings;17import org.mockito.invocation.DescribedInvocation;18import org.mockito.invocation.Invocation;19import org.mockito.invocation.Location;20@SuppressWarnings("unchecked")21public class InvocationMatcher implements DescribedInvocation, CapturesArgumensFromInvocation, Serializable {22 private static final long serialVersionUID = -3047126096857467610L;23 private final Invocation invocation;24 private final List<Matcher> matchers;25 public InvocationMatcher(Invocation invocation, List<Matcher> matchers) {26 this.invocation = invocation;27 if (matchers.isEmpty()) {28 this.matchers = ArgumentsProcessor.argumentsToMatchers(invocation.getArguments());29 } else {30 this.matchers = matchers;31 }32 }33 34 public InvocationMatcher(Invocation invocation) {35 this(invocation, Collections.<Matcher>emptyList());36 }37 public Method getMethod() {38 return invocation.getMethod();39 }40 41 public Invocation getInvocation() {42 return this.invocation;43 }44 45 public List<Matcher> getMatchers() {46 return this.matchers;47 }48 49 public String toString() {50 return new PrintSettings().print(matchers, invocation);51 }52 public boolean matches(Invocation actual) {53 return invocation.getMock().equals(actual.getMock())54 && hasSameMethod(actual)55 && new ArgumentsComparator().argumentsMatch(this, actual);56 }57 private boolean safelyArgumentsMatch(Object[] actualArgs) {58 try {59 return new ArgumentsComparator().argumentsMatch(this, actualArgs);60 } catch (Throwable t) {61 return false;62 }63 }64 /**65 * similar means the same method name, same mock, unverified 66 * and: if arguments are the same cannot be overloaded67 */68 public boolean hasSimilarMethod(Invocation candidate) {69 String wantedMethodName = getMethod().getName();70 String currentMethodName = candidate.getMethod().getName();71 72 final boolean methodNameEquals = wantedMethodName.equals(currentMethodName);73 final boolean isUnverified = !candidate.isVerified();74 final boolean mockIsTheSame = getInvocation().getMock() == candidate.getMock();75 final boolean methodEquals = hasSameMethod(candidate);76 if (!methodNameEquals || !isUnverified || !mockIsTheSame) {77 return false;78 }79 final boolean overloadedButSameArgs = !methodEquals && safelyArgumentsMatch(candidate.getArguments());80 return !overloadedButSameArgs;81 }82 public boolean hasSameMethod(Invocation candidate) {83 //not using method.equals() for 1 good reason:84 //sometimes java generates forwarding methods when generics are in play see JavaGenericsForwardingMethodsTest85 Method m1 = invocation.getMethod();86 Method m2 = candidate.getMethod();87 88 if (m1.getName() != null && m1.getName().equals(m2.getName())) {89 /* Avoid unnecessary cloning */90 Class[] params1 = m1.getParameterTypes();91 Class[] params2 = m2.getParameterTypes();92 if (params1.length == params2.length) {93 for (int i = 0; i < params1.length; i++) {94 if (params1[i] != params2[i])95 return false;96 }97 return true;98 }99 }100 return false;101 }102 103 public Location getLocation() {104 return invocation.getLocation();105 }106 public void captureArgumentsFrom(Invocation invocation) {107 if (invocation.getMethod().isVarArgs()) {108 int indexOfVararg = invocation.getRawArguments().length - 1;109 for (int position = 0; position < indexOfVararg; position++) {110 Matcher m = matchers.get(position);111 if (m instanceof CapturesArguments) {112 ((CapturesArguments) m).captureFrom(invocation.getArgumentAt(position, Object.class));113 }114 }115 for (int position = indexOfVararg; position < matchers.size(); position++) {116 Matcher m = matchers.get(position);117 if (m instanceof CapturesArguments) {118 ((CapturesArguments) m).captureFrom(invocation.getRawArguments()[position - indexOfVararg]);119 }120 }121 } else {122 for (int position = 0; position < matchers.size(); position++) {123 Matcher m = matchers.get(position);124 if (m instanceof CapturesArguments) {125 ((CapturesArguments) m).captureFrom(invocation.getArgumentAt(position, Object.class));126 }127 }128 }129// for (int position = 0; position < matchers.size(); position++) {130// Matcher m = matchers.get(position);131// if (m instanceof CapturesArguments && invocation.getRawArguments().length > position) {132// //TODO SF - this whole lot can be moved captureFrom implementation133// if(isVariableArgument(invocation, position) && isVarargMatcher(m)) {134// Object array = invocation.getRawArguments()[position];135// for (int i = 0; i < Array.getLength(array); i++) {136// ((CapturesArguments) m).captureFrom(Array.get(array, i));137// }138// //since we've captured all varargs already, it does not make sense to process other matchers.139// return;140// } else {141// ((CapturesArguments) m).captureFrom(invocation.getRawArguments()[position]);142// }143// }144// }145 }146 private boolean isVarargMatcher(Matcher matcher) {147 Matcher actualMatcher = matcher;148 if (actualMatcher instanceof MatcherDecorator) {149 actualMatcher = ((MatcherDecorator) actualMatcher).getActualMatcher();150 }151 return actualMatcher instanceof VarargMatcher;152 }153 private boolean isVariableArgument(Invocation invocation, int position) {154 return invocation.getRawArguments().length - 1 == position155 && invocation.getRawArguments()[position] != null...

Full Screen

Full Screen

Source:Math_rank-6_old.java Github

copy

Full Screen

...9import java.util.Collections;10import java.util.LinkedList;11import java.util.List;12import org.hamcrest.Matcher;13import org.mockito.internal.matchers.CapturesArguments;14import org.mockito.internal.matchers.MatcherDecorator;15import org.mockito.internal.matchers.VarargMatcher;16import org.mockito.internal.reporting.PrintSettings;17import org.mockito.invocation.DescribedInvocation;18import org.mockito.invocation.Invocation;19import org.mockito.invocation.Location;20@SuppressWarnings("unchecked")21public class InvocationMatcher implements DescribedInvocation, CapturesArgumensFromInvocation, Serializable {22 private static final long serialVersionUID = -3047126096857467610L;23 private final Invocation invocation;24 private final List<Matcher> matchers;25 public InvocationMatcher(Invocation invocation, List<Matcher> matchers) {26 this.invocation = invocation;27 if (matchers.isEmpty()) {28 this.matchers = ArgumentsProcessor.argumentsToMatchers(invocation.getArguments());29 } else {30 this.matchers = matchers;31 }32 }33 34 public InvocationMatcher(Invocation invocation) {35 this(invocation, Collections.<Matcher>emptyList());36 }37 public Method getMethod() {38 return invocation.getMethod();39 }40 41 public Invocation getInvocation() {42 return this.invocation;43 }44 45 public List<Matcher> getMatchers() {46 return this.matchers;47 }48 49 public String toString() {50 return new PrintSettings().print(matchers, invocation);51 }52 public boolean matches(Invocation actual) {53 return invocation.getMock().equals(actual.getMock())54 && hasSameMethod(actual)55 && new ArgumentsComparator().argumentsMatch(this, actual);56 }57 private boolean safelyArgumentsMatch(Object[] actualArgs) {58 try {59 return new ArgumentsComparator().argumentsMatch(this, actualArgs);60 } catch (Throwable t) {61 return false;62 }63 }64 /**65 * similar means the same method name, same mock, unverified 66 * and: if arguments are the same cannot be overloaded67 */68 public boolean hasSimilarMethod(Invocation candidate) {69 String wantedMethodName = getMethod().getName();70 String currentMethodName = candidate.getMethod().getName();71 72 final boolean methodNameEquals = wantedMethodName.equals(currentMethodName);73 final boolean isUnverified = !candidate.isVerified();74 final boolean mockIsTheSame = getInvocation().getMock() == candidate.getMock();75 final boolean methodEquals = hasSameMethod(candidate);76 if (!methodNameEquals || !isUnverified || !mockIsTheSame) {77 return false;78 }79 final boolean overloadedButSameArgs = !methodEquals && safelyArgumentsMatch(candidate.getArguments());80 return !overloadedButSameArgs;81 }82 public boolean hasSameMethod(Invocation candidate) {83 //not using method.equals() for 1 good reason:84 //sometimes java generates forwarding methods when generics are in play see JavaGenericsForwardingMethodsTest85 Method m1 = invocation.getMethod();86 Method m2 = candidate.getMethod();87 88 if (m1.getName() != null && m1.getName().equals(m2.getName())) {89 /* Avoid unnecessary cloning */90 Class[] params1 = m1.getParameterTypes();91 Class[] params2 = m2.getParameterTypes();92 if (params1.length == params2.length) {93 for (int i = 0; i < params1.length; i++) {94 if (params1[i] != params2[i])95 return false;96 }97 return true;98 }99 }100 return false;101 }102 103 public Location getLocation() {104 return invocation.getLocation();105 }106 public void captureArgumentsFrom(Invocation invocation) {107 if (invocation.getMethod().isVarArgs()) {108 int indexOfVararg = invocation.getRawArguments().length - 1;109 for (int position = 0; position < indexOfVararg; position++) {110 Matcher m = matchers.get(position);111 if (m instanceof CapturesArguments) {112 ((CapturesArguments) m).captureFrom(invocation.getArgumentAt(position, Object.class));113 }114 }115 for (int position = indexOfVararg; position < matchers.size(); position++) {116 Matcher m = matchers.get(position);117 if (m instanceof CapturesArguments) {118 ((CapturesArguments) m).captureFrom(invocation.getRawArguments()[position - indexOfVararg]);119 }120 }121 } else {122 for (int position = 0; position < matchers.size(); position++) {123 Matcher m = matchers.get(position);124 if (m instanceof CapturesArguments) {125 ((CapturesArguments) m).captureFrom(invocation.getArgumentAt(position, Object.class));126 }127 }128 }129// for (int position = 0; position < matchers.size(); position++) {130// Matcher m = matchers.get(position);131// if (m instanceof CapturesArguments && invocation.getRawArguments().length > position) {132// //TODO SF - this whole lot can be moved captureFrom implementation133// if(isVariableArgument(invocation, position) && isVarargMatcher(m)) {134// Object array = invocation.getRawArguments()[position];135// for (int i = 0; i < Array.getLength(array); i++) {136// ((CapturesArguments) m).captureFrom(Array.get(array, i));137// }138// //since we've captured all varargs already, it does not make sense to process other matchers.139// return;140// } else {141// ((CapturesArguments) m).captureFrom(invocation.getRawArguments()[position]);142// }143// }144// }145 }146 private boolean isVarargMatcher(Matcher matcher) {147 Matcher actualMatcher = matcher;148 if (actualMatcher instanceof MatcherDecorator) {149 actualMatcher = ((MatcherDecorator) actualMatcher).getActualMatcher();150 }151 return actualMatcher instanceof VarargMatcher;152 }153 private boolean isVariableArgument(Invocation invocation, int position) {154 return invocation.getRawArguments().length - 1 == position155 && invocation.getRawArguments()[position] != null...

Full Screen

Full Screen

Source:Math_5_rank-5_old.java Github

copy

Full Screen

...9import java.util.Collections;10import java.util.LinkedList;11import java.util.List;12import org.hamcrest.Matcher;13import org.mockito.internal.matchers.CapturesArguments;14import org.mockito.internal.matchers.MatcherDecorator;15import org.mockito.internal.matchers.VarargMatcher;16import org.mockito.internal.reporting.PrintSettings;17import org.mockito.invocation.DescribedInvocation;18import org.mockito.invocation.Invocation;19import org.mockito.invocation.Location;20@SuppressWarnings("unchecked")21public class InvocationMatcher implements DescribedInvocation, CapturesArgumensFromInvocation, Serializable {22 private static final long serialVersionUID = -3047126096857467610L;23 private final Invocation invocation;24 private final List<Matcher> matchers;25 public InvocationMatcher(Invocation invocation, List<Matcher> matchers) {26 this.invocation = invocation;27 if (matchers.isEmpty()) {28 this.matchers = ArgumentsProcessor.argumentsToMatchers(invocation.getArguments());29 } else {30 this.matchers = matchers;31 }32 }33 34 public InvocationMatcher(Invocation invocation) {35 this(invocation, Collections.<Matcher>emptyList());36 }37 public Method getMethod() {38 return invocation.getMethod();39 }40 41 public Invocation getInvocation() {42 return this.invocation;43 }44 45 public List<Matcher> getMatchers() {46 return this.matchers;47 }48 49 public String toString() {50 return new PrintSettings().print(matchers, invocation);51 }52 public boolean matches(Invocation actual) {53 return invocation.getMock().equals(actual.getMock())54 && hasSameMethod(actual)55 && new ArgumentsComparator().argumentsMatch(this, actual);56 }57 private boolean safelyArgumentsMatch(Object[] actualArgs) {58 try {59 return new ArgumentsComparator().argumentsMatch(this, actualArgs);60 } catch (Throwable t) {61 return false;62 }63 }64 /**65 * similar means the same method name, same mock, unverified 66 * and: if arguments are the same cannot be overloaded67 */68 public boolean hasSimilarMethod(Invocation candidate) {69 String wantedMethodName = getMethod().getName();70 String currentMethodName = candidate.getMethod().getName();71 72 final boolean methodNameEquals = wantedMethodName.equals(currentMethodName);73 final boolean isUnverified = !candidate.isVerified();74 final boolean mockIsTheSame = getInvocation().getMock() == candidate.getMock();75 final boolean methodEquals = hasSameMethod(candidate);76 if (!methodNameEquals || !isUnverified || !mockIsTheSame) {77 return false;78 }79 final boolean overloadedButSameArgs = !methodEquals && safelyArgumentsMatch(candidate.getArguments());80 return !overloadedButSameArgs;81 }82 public boolean hasSameMethod(Invocation candidate) {83 //not using method.equals() for 1 good reason:84 //sometimes java generates forwarding methods when generics are in play see JavaGenericsForwardingMethodsTest85 Method m1 = invocation.getMethod();86 Method m2 = candidate.getMethod();87 88 if (m1.getName() != null && m1.getName().equals(m2.getName())) {89 /* Avoid unnecessary cloning */90 Class[] params1 = m1.getParameterTypes();91 Class[] params2 = m2.getParameterTypes();92 if (params1.length == params2.length) {93 for (int i = 0; i < params1.length; i++) {94 if (params1[i] != params2[i])95 return false;96 }97 return true;98 }99 }100 return false;101 }102 103 public Location getLocation() {104 return invocation.getLocation();105 }106 public void captureArgumentsFrom(Invocation invocation) {107 if (invocation.getMethod().isVarArgs()) {108 int indexOfVararg = invocation.getRawArguments().length - 1;109 for (int position = 0; position < indexOfVararg; position++) {110 Matcher m = matchers.get(position);111 if (m instanceof CapturesArguments) {112 ((CapturesArguments) m).captureFrom(invocation.getArgumentAt(position, Object.class));113 }114 }115 for (int position = indexOfVararg; position < matchers.size(); position++) {116 Matcher m = matchers.get(position);117 if (m instanceof CapturesArguments) {118 ((CapturesArguments) m).captureFrom(invocation.getRawArguments()[position - indexOfVararg]);119 }120 }121 } else {122 for (int position = 0; position < matchers.size(); position++) {123 Matcher m = matchers.get(position);124 if (m instanceof CapturesArguments) {125 ((CapturesArguments) m).captureFrom(invocation.getArgumentAt(position, Object.class));126 }127 }128 }129// for (int position = 0; position < matchers.size(); position++) {130// Matcher m = matchers.get(position);131// if (m instanceof CapturesArguments && invocation.getRawArguments().length > position) {132// //TODO SF - this whole lot can be moved captureFrom implementation133// if(isVariableArgument(invocation, position) && isVarargMatcher(m)) {134// Object array = invocation.getRawArguments()[position];135// for (int i = 0; i < Array.getLength(array); i++) {136// ((CapturesArguments) m).captureFrom(Array.get(array, i));137// }138// //since we've captured all varargs already, it does not make sense to process other matchers.139// return;140// } else {141// ((CapturesArguments) m).captureFrom(invocation.getRawArguments()[position]);142// }143// }144// }145 }146 private boolean isVarargMatcher(Matcher matcher) {147 Matcher actualMatcher = matcher;148 if (actualMatcher instanceof MatcherDecorator) {149 actualMatcher = ((MatcherDecorator) actualMatcher).getActualMatcher();150 }151 return actualMatcher instanceof VarargMatcher;152 }153 private boolean isVariableArgument(Invocation invocation, int position) {154 return invocation.getRawArguments().length - 1 == position155 && invocation.getRawArguments()[position] != null...

Full Screen

Full Screen

Source:33InvocationMatcher.java Github

copy

Full Screen

...10import java.util.List;11import org.hamcrest.Matcher;12import org.mockito.exceptions.PrintableInvocation;13import org.mockito.internal.debugging.Location;14import org.mockito.internal.matchers.CapturesArguments;15import org.mockito.internal.reporting.PrintSettings;16import org.mockito.internal.reporting.PrintingFriendlyInvocation;17@SuppressWarnings("unchecked")18public class InvocationMatcher implements PrintableInvocation, PrintingFriendlyInvocation, CapturesArgumensFromInvocation, Serializable {19 private static final long serialVersionUID = -3047126096857467610L;20 private final Invocation invocation;21 private final List<Matcher> matchers;22 public InvocationMatcher(Invocation invocation, List<Matcher> matchers) {23 this.invocation = invocation;24 if (matchers.isEmpty()) {25 this.matchers = invocation.argumentsToMatchers();26 } else {27 this.matchers = matchers;28 }29 }30 31 public InvocationMatcher(Invocation invocation) {32 this(invocation, Collections.<Matcher>emptyList());33 }34 public Method getMethod() {35 return invocation.getMethod();36 }37 38 public Invocation getInvocation() {39 return this.invocation;40 }41 42 public List<Matcher> getMatchers() {43 return this.matchers;44 }45 46 public String toString() {47 return invocation.toString(matchers, new PrintSettings());48 }49 public boolean matches(Invocation actual) {50 return invocation.getMock().equals(actual.getMock())51 && hasSameMethod(actual)52 && new ArgumentsComparator().argumentsMatch(this, actual);53 }54 private boolean safelyArgumentsMatch(Object[] actualArgs) {55 try {56 return new ArgumentsComparator().argumentsMatch(this, actualArgs);57 } catch (Throwable t) {58 return false;59 }60 }61 /**62 * similar means the same method name, same mock, unverified 63 * and: if arguments are the same cannot be overloaded64 */65 public boolean hasSimilarMethod(Invocation candidate) {66 String wantedMethodName = getMethod().getName();67 String currentMethodName = candidate.getMethod().getName();68 69 final boolean methodNameEquals = wantedMethodName.equals(currentMethodName);70 final boolean isUnverified = !candidate.isVerified();71 final boolean mockIsTheSame = getInvocation().getMock() == candidate.getMock();72 final boolean methodEquals = hasSameMethod(candidate);73 if (!methodNameEquals || !isUnverified || !mockIsTheSame) {74 return false;75 }76 final boolean overloadedButSameArgs = !methodEquals && safelyArgumentsMatch(candidate.getArguments());77 return !overloadedButSameArgs;78 }79 public boolean hasSameMethod(Invocation candidate) { 80 //not using method.equals() for 1 good reason:81 //sometimes java generates forwarding methods when generics are in play see JavaGenericsForwardingMethodsTest82 Method m1 = invocation.getMethod();83 Method m2 = candidate.getMethod();84 85 /* Avoid unnecessary cloning */86 return m1.equals(m2);87 }88 89 public Location getLocation() {90 return invocation.getLocation();91 }92 public String toString(PrintSettings printSettings) {93 return invocation.toString(matchers, printSettings);94 }95 public void captureArgumentsFrom(Invocation i) {96 int k = 0;97 for (Matcher m : matchers) {98 if (m instanceof CapturesArguments && i.getArguments().length > k) {99 ((CapturesArguments) m).captureFrom(i.getArguments()[k]);100 }101 k++;102 }103 }104 public static List<InvocationMatcher> createFrom(List<Invocation> invocations) {105 LinkedList<InvocationMatcher> out = new LinkedList<InvocationMatcher>();106 for (Invocation i : invocations) {107 out.add(new InvocationMatcher(i));108 }109 return out;110 }111}...

Full Screen

Full Screen

Source:34InvocationMatcher.java Github

copy

Full Screen

...9import java.util.List;10import org.hamcrest.Matcher;11import org.mockito.exceptions.PrintableInvocation;12import org.mockito.internal.debugging.Location;13import org.mockito.internal.matchers.CapturesArguments;14import org.mockito.internal.reporting.PrintSettings;15import org.mockito.internal.reporting.PrintingFriendlyInvocation;16@SuppressWarnings("unchecked")17public class InvocationMatcher implements PrintableInvocation, PrintingFriendlyInvocation, CapturesArgumensFromInvocation, Serializable {18 private static final long serialVersionUID = -3047126096857467610L;19 private final Invocation invocation;20 private final List<Matcher> matchers;21 public InvocationMatcher(Invocation invocation, List<Matcher> matchers) {22 this.invocation = invocation;23 if (matchers.isEmpty()) {24 this.matchers = invocation.argumentsToMatchers();25 } else {26 this.matchers = matchers;27 }28 }29 30 public InvocationMatcher(Invocation invocation) {31 this(invocation, Collections.<Matcher>emptyList());32 }33 public Method getMethod() {34 return invocation.getMethod();35 }36 37 public Invocation getInvocation() {38 return this.invocation;39 }40 41 public List<Matcher> getMatchers() {42 return this.matchers;43 }44 45 public String toString() {46 return invocation.toString(matchers, new PrintSettings());47 }48 public boolean matches(Invocation actual) {49 return invocation.getMock().equals(actual.getMock())50 && hasSameMethod(actual)51 && new ArgumentsComparator().argumentsMatch(this, actual);52 }53 private boolean safelyArgumentsMatch(Object[] actualArgs) {54 try {55 return new ArgumentsComparator().argumentsMatch(this, actualArgs);56 } catch (Throwable t) {57 return false;58 }59 }60 /**61 * similar means the same method name, same mock, unverified 62 * and: if arguments are the same cannot be overloaded63 */64 public boolean hasSimilarMethod(Invocation candidate) {65 String wantedMethodName = getMethod().getName();66 String currentMethodName = candidate.getMethod().getName();67 68 final boolean methodNameEquals = wantedMethodName.equals(currentMethodName);69 final boolean isUnverified = !candidate.isVerified();70 final boolean mockIsTheSame = getInvocation().getMock() == candidate.getMock();71 final boolean methodEquals = hasSameMethod(candidate);72 if (!methodNameEquals || !isUnverified || !mockIsTheSame) {73 return false;74 }75 final boolean overloadedButSameArgs = !methodEquals && safelyArgumentsMatch(candidate.getArguments());76 return !overloadedButSameArgs;77 }78 public boolean hasSameMethod(Invocation candidate) {79 return invocation.getMethod().equals(candidate.getMethod());80 }81 82 public Location getLocation() {83 return invocation.getLocation();84 }85 public String toString(PrintSettings printSettings) {86 return invocation.toString(matchers, printSettings);87 }88 public void captureArgumentsFrom(Invocation i) {89 int k = 0;90 for (Matcher m : matchers) {91 if (m instanceof CapturesArguments && i.getArguments().length > k) {92 ((CapturesArguments) m).captureFrom(i.getArguments()[k]);93 }94 k++;95 }96 }97}...

Full Screen

Full Screen

CapturesArguments

Using AI Code Generation

copy

Full Screen

1import org.mockito.internal.matchers.CapturesArguments;2import org.mockito.ArgumentCaptor;3import org.mockito.Captor;4import org.mockito.Mock;5import org.mockito.Mockito;6import org.mockito.MockitoAnnotations;7import org.mockito.runners.MockitoJUnitRunner;8import org.junit.runner.RunWith;9import org.junit.Before;10import org.junit.Test;11import static org.mockito.Mockito.*;12import static org.junit.Assert.*;13import java.util.List;14import java.util.ArrayList;15import java.util.Arrays;16@RunWith(MockitoJUnitRunner.class)17public class TestCapturesArguments {18 private List<String> mockedList;19 private ArgumentCaptor<List<String>> captor;20 public void setUp() {21 MockitoAnnotations.initMocks(this);22 }23 public void testCapture() {24 List<String> list = new ArrayList<String>();25 list.add("one");26 list.add("two");27 mockedList.addAll(list);28 verify(mockedList).addAll(captor.capture());29 List<String> capturedArgument = captor.getValue();30 assertEquals(2, capturedArgument.size());31 }32}33OK (2 tests)

Full Screen

Full Screen

CapturesArguments

Using AI Code Generation

copy

Full Screen

1import org.mockito.internal.matchers.CapturesArguments;2public class CapturesArgumentsTest {3 public static void main(String[] args) {4 CapturesArguments capturesArguments = new CapturesArguments();5 System.out.println(capturesArguments.describe());6 }7}8import org.mockito.internal.matchers.CapturesArguments;9public class CapturesArgumentsTest {10 public static void main(String[] args) {11 CapturesArguments capturesArguments = new CapturesArguments();12 System.out.println(capturesArguments.toString());13 }14}15import org.mockito.internal.matchers.CapturesArguments;16public class CapturesArgumentsTest {17 public static void main(String[] args) {18 CapturesArguments capturesArguments = new CapturesArguments();19 System.out.println(capturesArguments.matches(null));20 }21}22import org.mockito.internal.matchers.CapturesArguments;23public class CapturesArgumentsTest {24 public static void main(String[] args) {25 CapturesArguments capturesArguments = new CapturesArguments();26 System.out.println(capturesArguments.captures());27 }28}29import org.mockito.internal.matchers.CapturesArguments;30public class CapturesArgumentsTest {31 public static void main(String[] args) {32 CapturesArguments capturesArguments = new CapturesArguments();33 System.out.println(capturesArguments.captureFrom(null));34 }35}36import org.mockito.internal.matchers.CapturesArguments;37public class CapturesArgumentsTest {38 public static void main(String[] args) {39 CapturesArguments capturesArguments = new CapturesArguments();40 System.out.println(capturesArguments.captureFrom(null));41 }42}

Full Screen

Full Screen

CapturesArguments

Using AI Code Generation

copy

Full Screen

1package com.automationrhapsody.mockito;2import static org.mockito.Mockito.mock;3import static org.mockito.Mockito.verify;4import static org.mockito.Mockito.verifyNoMoreInteractions;5import static org.mockito.Mockito.when;6import org.junit.Test;7import org.mockito.ArgumentCaptor;8import org.mockito.internal.matchers.CapturesArguments;9import org.mockito.invocation.InvocationOnMock;10import org.mockito.stubbing.Answer;11public class CapturesArgumentsTest {12 public void testCapturesArguments() {13 CapturesArguments capturesArguments = new CapturesArguments();14 ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);15 when(capturesArguments.capture(argument.capture())).thenAnswer(new Answer() {16 public Object answer(InvocationOnMock invocation) throws Throwable {17 return invocation.getArguments()[0];18 }19 });20 String result = capturesArguments.capture("test");21 verify(capturesArguments).capture("test");22 verifyNoMoreInteractions(capturesArguments);23 System.out.println("argument.getValue(): " + argument.getValue());24 System.out.println("result: " + result);25 }26}27argument.getValue(): test28Mockito - ArgumentCaptor with @Captor annotation Mockito - ArgumentCaptor with Mockito.verify()29Mockito - ArgumentCaptor with Mockito.verify() Mockito - ArgumentCaptor with Mockito.verifyNoMoreInteractions()30Mockito - ArgumentCaptor with Mockito.verifyNoMoreInteractions() Mockito - ArgumentCaptor with Mockito.times()31Mockito - ArgumentCaptor with Mockito.times() Mockito - ArgumentCaptor with Mockito.atLeastOnce()32Mockito - ArgumentCaptor with Mockito.atLeastOnce() Mockito - ArgumentCaptor with Mockito.atLeast()33Mockito - ArgumentCaptor with Mockito.atLeast() Mockito - ArgumentCaptor with Mockito.atMost()34Mockito - ArgumentCaptor with Mockito.atMost() Mockito - ArgumentCaptor with Mockito.atMostOnce()35Mockito - ArgumentCaptor with Mockito.atMostOnce() Mockito - ArgumentCaptor with Mockito.atMostOnce()36Mockito - ArgumentCaptor with Mockito.atMostOnce() Mockito - ArgumentCaptor with Mockito.only()37Mockito - ArgumentCaptor with Mockito.only() Mockito - ArgumentCaptor with Mockito.never()38Mockito - ArgumentCaptor with Mockito.never() Mockito - ArgumentCaptor with Mockito.any()

Full Screen

Full Screen

CapturesArguments

Using AI Code Generation

copy

Full Screen

1import org.mockito.internal.matchers.*;2public class 1 {3 public static void main(String[] args) {4 CapturesArguments capture = new CapturesArguments();5 capture.captureFrom(new Object[]{1,2,3});6 System.out.println("Captured arguments: "+capture.getCapturedArguments());7 }8}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful