/* * Copyright (c) 2007 Mockito contributors * This program is made available under the terms of the MIT License. */ package org.mockito; import org.mockito.internal.matchers.ArrayEquals; import org.mockito.internal.matchers.CompareEqual; import org.mockito.internal.matchers.EqualsWithDelta; import org.mockito.internal.matchers.Find; import org.mockito.internal.matchers.GreaterOrEqual; import org.mockito.internal.matchers.GreaterThan; import org.mockito.internal.matchers.LessOrEqual; import org.mockito.internal.matchers.LessThan; import org.mockito.internal.progress.HandyReturnValues; import org.mockito.internal.progress.MockingProgress; import org.mockito.internal.progress.ThreadSafeMockingProgress; /** * See {@link Matchers} for general info about matchers. *

* AdditionalMatchers provides rarely used matchers, kept only for somewhat compatibility with EasyMock. * Use additional matchers very judiciously because they may impact readability of a test. * It is recommended to use matchers from {@link Matchers} and keep stubbing and verification simple. *

* Example of using logical and(), not(), or() matchers: * *


 *   //anything but not "ejb"
 *   mock.someMethod(not(eq("ejb")));
 *   
 *   //not "ejb" and not "michael jackson"
 *   mock.someMethod(and(not(eq("ejb")), not(eq("michael jackson"))));
 *   
 *   //1 or 10
 *   mock.someMethod(or(eq(1), eq(10)));
 * 
* * Scroll down to see all methods - full list of matchers. */ public class AdditionalMatchers { private static MockingProgress mockingProgress = new ThreadSafeMockingProgress(); /** * argument greater than or equal the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return null. */ public static > T geq(Comparable value) { return reportMatcher(new GreaterOrEqual(value)).returnNull(); } /** * byte argument greater than or equal to the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static byte geq(byte value) { return reportMatcher(new GreaterOrEqual(value)).returnZero(); } /** * double argument greater than or equal to the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static double geq(double value) { return reportMatcher(new GreaterOrEqual(value)).returnZero(); } /** * float argument greater than or equal to the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static float geq(float value) { return reportMatcher(new GreaterOrEqual(value)).returnZero(); } /** * int argument greater than or equal to the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static int geq(int value) { return reportMatcher(new GreaterOrEqual(value)).returnZero(); } /** * long argument greater than or equal to the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static long geq(long value) { return reportMatcher(new GreaterOrEqual(value)).returnZero(); } /** * short argument greater than or equal to the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static short geq(short value) { return reportMatcher(new GreaterOrEqual(value)).returnZero(); } /** * comparable argument less than or equal the given value details. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return null. */ public static > T leq(Comparable value) { return reportMatcher(new LessOrEqual(value)).returnNull(); } /** * byte argument less than or equal to the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static byte leq(byte value) { return reportMatcher(new LessOrEqual(value)).returnZero(); } /** * double argument less than or equal to the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static double leq(double value) { return reportMatcher(new LessOrEqual(value)).returnZero(); } /** * float argument less than or equal to the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static float leq(float value) { return reportMatcher(new LessOrEqual(value)).returnZero(); } /** * int argument less than or equal to the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static int leq(int value) { return reportMatcher(new LessOrEqual(value)).returnZero(); } /** * long argument less than or equal to the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static long leq(long value) { return reportMatcher(new LessOrEqual(value)).returnZero(); } /** * short argument less than or equal to the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static short leq(short value) { return reportMatcher(new LessOrEqual(value)).returnZero(); } /** * comparable argument greater than the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return null. */ public static > T gt(Comparable value) { return reportMatcher(new GreaterThan(value)).returnNull(); } /** * byte argument greater than the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static byte gt(byte value) { return reportMatcher(new GreaterThan(value)).returnZero(); } /** * double argument greater than the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static double gt(double value) { return reportMatcher(new GreaterThan(value)).returnZero(); } /** * float argument greater than the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static float gt(float value) { return reportMatcher(new GreaterThan(value)).returnZero(); } /** * int argument greater than the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static int gt(int value) { return reportMatcher(new GreaterThan(value)).returnZero(); } /** * long argument greater than the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static long gt(long value) { return reportMatcher(new GreaterThan(value)).returnZero(); } /** * short argument greater than the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static short gt(short value) { return reportMatcher(new GreaterThan(value)).returnZero(); } /** * comparable argument less than the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return null. */ public static > T lt(Comparable value) { return reportMatcher(new LessThan(value)).returnNull(); } /** * byte argument less than the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static byte lt(byte value) { return reportMatcher(new LessThan(value)).returnZero(); } /** * double argument less than the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static double lt(double value) { return reportMatcher(new LessThan(value)).returnZero(); } /** * float argument less than the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static float lt(float value) { return reportMatcher(new LessThan(value)).returnZero(); } /** * int argument less than the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static int lt(int value) { return reportMatcher(new LessThan(value)).returnZero(); } /** * long argument less than the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static long lt(long value) { return reportMatcher(new LessThan(value)).returnZero(); } /** * short argument less than the given value. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return 0. */ public static short lt(short value) { return reportMatcher(new LessThan(value)).returnZero(); } /** * comparable argument equals to the given value according to their * compareTo method. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @return null. */ public static > T cmpEq(Comparable value) { return reportMatcher(new CompareEqual(value)).returnNull(); } /** * String argument that contains a substring that matches the given regular * expression. * * @param regex * the regular expression. * @return null. */ public static String find(String regex) { return reportMatcher(new Find(regex)).returnNull(); } /** * Object array argument that is equal to the given array, i.e. it has to * have the same type, length, and each element has to be equal. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param * the type of the array, it is passed through to prevent casts. * @param value * the given array. * @return null. */ public static T[] aryEq(T[] value) { return reportMatcher(new ArrayEquals(value)).returnNull(); } /** * short array argument that is equal to the given array, i.e. it has to * have the same length, and each element has to be equal. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given array. * @return null. */ public static short[] aryEq(short[] value) { return reportMatcher(new ArrayEquals(value)).returnNull(); } /** * long array argument that is equal to the given array, i.e. it has to have * the same length, and each element has to be equal. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given array. * @return null. */ public static long[] aryEq(long[] value) { return reportMatcher(new ArrayEquals(value)).returnNull(); } /** * int array argument that is equal to the given array, i.e. it has to have * the same length, and each element has to be equal. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given array. * @return null. */ public static int[] aryEq(int[] value) { return reportMatcher(new ArrayEquals(value)).returnNull(); } /** * float array argument that is equal to the given array, i.e. it has to * have the same length, and each element has to be equal. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given array. * @return null. */ public static float[] aryEq(float[] value) { return reportMatcher(new ArrayEquals(value)).returnNull(); } /** * double array argument that is equal to the given array, i.e. it has to * have the same length, and each element has to be equal. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given array. * @return null. */ public static double[] aryEq(double[] value) { return reportMatcher(new ArrayEquals(value)).returnNull(); } /** * char array argument that is equal to the given array, i.e. it has to have * the same length, and each element has to be equal. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given array. * @return null. */ public static char[] aryEq(char[] value) { return reportMatcher(new ArrayEquals(value)).returnNull(); } /** * byte array argument that is equal to the given array, i.e. it has to have * the same length, and each element has to be equal. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given array. * @return null. */ public static byte[] aryEq(byte[] value) { return reportMatcher(new ArrayEquals(value)).returnNull(); } /** * boolean array argument that is equal to the given array, i.e. it has to * have the same length, and each element has to be equal. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given array. * @return null. */ public static boolean[] aryEq(boolean[] value) { return reportMatcher(new ArrayEquals(value)).returnNull(); } /** * boolean argument that matches both given matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return false. */ public static boolean and(boolean first, boolean second) { return mockingProgress.getArgumentMatcherStorage().reportAnd().returnFalse(); } /** * byte argument that matches both given argument matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return 0. */ public static byte and(byte first, byte second) { return mockingProgress.getArgumentMatcherStorage().reportAnd().returnZero(); } /** * char argument that matches both given argument matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return 0. */ public static char and(char first, char second) { return mockingProgress.getArgumentMatcherStorage().reportAnd().returnChar(); } /** * double argument that matches both given argument matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return 0. */ public static double and(double first, double second) { return mockingProgress.getArgumentMatcherStorage().reportAnd().returnZero(); } /** * float argument that matches both given argument matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return 0. */ public static float and(float first, float second) { return mockingProgress.getArgumentMatcherStorage().reportAnd().returnZero(); } /** * int argument that matches both given argument matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return 0. */ public static int and(int first, int second) { return mockingProgress.getArgumentMatcherStorage().reportAnd().returnZero(); } /** * long argument that matches both given argument matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return 0. */ public static long and(long first, long second) { return mockingProgress.getArgumentMatcherStorage().reportAnd().returnZero(); } /** * short argument that matches both given argument matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return 0. */ public static short and(short first, short second) { return mockingProgress.getArgumentMatcherStorage().reportAnd().returnZero(); } /** * Object argument that matches both given argument matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param * the type of the object, it is passed through to prevent casts. * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return null. */ public static T and(T first, T second) { return mockingProgress.getArgumentMatcherStorage().reportAnd().returnNull(); } /** * boolean argument that matches any of the given argument matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return false. */ public static boolean or(boolean first, boolean second) { return mockingProgress.getArgumentMatcherStorage().reportOr().returnFalse(); } /** * Object argument that matches any of the given argument matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param * the type of the object, it is passed through to prevent casts. * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return null. */ public static T or(T first, T second) { return mockingProgress.getArgumentMatcherStorage().reportOr().returnNull(); } /** * short argument that matches any of the given argument matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return 0. */ public static short or(short first, short second) { return mockingProgress.getArgumentMatcherStorage().reportOr().returnZero(); } /** * long argument that matches any of the given argument matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return 0. */ public static long or(long first, long second) { return mockingProgress.getArgumentMatcherStorage().reportOr().returnZero(); } /** * int argument that matches any of the given argument matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return 0. */ public static int or(int first, int second) { return mockingProgress.getArgumentMatcherStorage().reportOr().returnZero(); } /** * float argument that matches any of the given argument matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return 0. */ public static float or(float first, float second) { return mockingProgress.getArgumentMatcherStorage().reportOr().returnZero(); } /** * double argument that matches any of the given argument matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return 0. */ public static double or(double first, double second) { return mockingProgress.getArgumentMatcherStorage().reportOr().returnZero(); } /** * char argument that matches any of the given argument matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return 0. */ public static char or(char first, char second) { return mockingProgress.getArgumentMatcherStorage().reportOr().returnChar(); } /** * byte argument that matches any of the given argument matchers. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the first argument matcher. * @param second * placeholder for the second argument matcher. * @return 0. */ public static byte or(byte first, byte second) { return mockingProgress.getArgumentMatcherStorage().reportOr().returnZero(); } /** * Object argument that does not match the given argument matcher. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param * the type of the object, it is passed through to prevent casts. * @param first * placeholder for the argument matcher. * @return null. */ public static T not(T first) { return mockingProgress.getArgumentMatcherStorage().reportNot().returnNull(); } /** * short argument that does not match the given argument matcher. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the argument matcher. * @return 0. */ public static short not(short first) { return mockingProgress.getArgumentMatcherStorage().reportNot().returnZero(); } /** * int argument that does not match the given argument matcher. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the argument matcher. * @return 0. */ public static int not(int first) { return mockingProgress.getArgumentMatcherStorage().reportNot().returnZero(); } /** * long argument that does not match the given argument matcher. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the argument matcher. * @return 0. */ public static long not(long first) { return mockingProgress.getArgumentMatcherStorage().reportNot().returnZero(); } /** * float argument that does not match the given argument matcher. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the argument matcher. * @return 0. */ public static float not(float first) { return mockingProgress.getArgumentMatcherStorage().reportNot().returnZero(); } /** * double argument that does not match the given argument matcher. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the argument matcher. * @return 0. */ public static double not(double first) { return mockingProgress.getArgumentMatcherStorage().reportNot().returnZero(); } /** * char argument that does not match the given argument matcher. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the argument matcher. * @return 0. */ public static char not(char first) { return mockingProgress.getArgumentMatcherStorage().reportNot().returnChar(); } /** * boolean argument that does not match the given argument matcher. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the argument matcher. * @return false. */ public static boolean not(boolean first) { return mockingProgress.getArgumentMatcherStorage().reportNot().returnFalse(); } /** * byte argument that does not match the given argument matcher. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param first * placeholder for the argument matcher. * @return 0. */ public static byte not(byte first) { return mockingProgress.getArgumentMatcherStorage().reportNot().returnZero(); } /** * double argument that has an absolute difference to the given value that * is less than the given delta details. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @param delta * the given delta. * @return 0. */ public static double eq(double value, double delta) { return reportMatcher(new EqualsWithDelta(value, delta)).returnZero(); } /** * float argument that has an absolute difference to the given value that is * less than the given delta details. *

* See examples in javadoc for {@link AdditionalMatchers} class * * @param value * the given value. * @param delta * the given delta. * @return 0. */ public static float eq(float value, float delta) { return reportMatcher(new EqualsWithDelta(value, delta)).returnZero(); } private static HandyReturnValues reportMatcher(ArgumentMatcher matcher) { return mockingProgress.getArgumentMatcherStorage().reportMatcher(matcher); } }