mockit.external.hamcrest.core.IsEqual Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmockit Show documentation
Show all versions of jmockit Show documentation
JMockit is a Java toolkit for developer (unit/integration) testing.
It contains mocking APIs and other tools, supporting both JUnit and TestNG.
The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested
in isolation from selected dependencies.
/* Copyright (c) 2000-2006 hamcrest.org
*/
package mockit.external.hamcrest.core;
import java.lang.reflect.Array;
import mockit.external.hamcrest.*;
/**
* Is the value equal to another value, as tested by the
* {@link java.lang.Object#equals} invokedMethod?
*/
public final class IsEqual extends BaseMatcher
{
private final Object object;
public IsEqual(T equalArg)
{
object = equalArg;
}
public boolean matches(Object arg)
{
return areEqual(arg, object);
}
public void describeTo(Description description)
{
description.appendValue(object);
}
public static boolean areEqual(Object o1, Object o2)
{
if (o1 == null) {
return o2 == null;
}
else if (o2 == null) {
return false;
}
else if (o1 == o2) {
return true;
}
return areEqualWhenNonNull(o1, o2);
}
public static boolean areEqualWhenNonNull(Object o1, Object o2)
{
if (isArray(o1)) {
return isArray(o2) && areArraysEqual(o1, o2);
}
return o1.equals(o2);
}
private static boolean areArraysEqual(Object o1, Object o2)
{
return areArrayLengthsEqual(o1, o2) && areArrayElementsEqual(o1, o2);
}
private static boolean areArrayLengthsEqual(Object o1, Object o2)
{
return Array.getLength(o1) == Array.getLength(o2);
}
private static boolean areArrayElementsEqual(Object o1, Object o2)
{
for (int i = 0; i < Array.getLength(o1); i++) {
if (!areEqual(Array.get(o1, i), Array.get(o2, i))) {
return false;
}
}
return true;
}
private static boolean isArray(Object o)
{
return o.getClass().isArray();
}
}