sk.baka.tools.test
Class Assert

java.lang.Object
  extended by sk.baka.tools.test.Assert

public class Assert
extends Object

Adds several array-comparing asserts.

Author:
Martin Vysny

Constructor Summary
Assert()
           
 
Method Summary
static void assertArrayEquals(Object actual, Object expected)
          Asserts that two arrays contain the same elements in the same order.
static void assertArrayEquals(Object actual, Object expected, String message)
          Asserts that two arrays contain the same elements in the same order.
static void assertArrayEqualsNoOrder(Object actual, Object expected)
          Asserts that two arrays contain the same elements in no particular order.
static void assertArrayEqualsNoOrder(Object actual, Object expected, String message)
          Asserts that two arrays contain the same elements in no particular order.
static
<T> void
assertBeansEquals(T got, T expected)
          Verifies that given beans are equal.
static
<T> void
assertBeansEquals(T got, T expected, JavaUtils.IEqualsComparator comparator)
          Verifies that given beans are equal.
static
<T> void
assertCorrectEquals(T object, T differentInstance, T differentValues, T extendingClass)
          Tests given object for correct Object.equals(java.lang.Object) contract.
static void assertCorrectEqualsForJpaBean(Class<?> clazz, Object id1, Object id2)
          Verifies that given JPA bean has a correct implementation of Object.equals(java.lang.Object) and Object.hashCode() methods.
static void assertUtilityClass(Class<?> clazz)
          Checks that given class is really an utility class - it has a private zero-arg constructor, does not have any other constructors and the constructor throws AssertionError.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Assert

public Assert()
Method Detail

assertArrayEquals

public static void assertArrayEquals(Object actual,
                                     Object expected,
                                     String message)
Asserts that two arrays contain the same elements in the same order. If they do not, an AssertionError is thrown. Provides more verbose error message than Assert#assertEquals(java.lang.Object[], java.lang.Object[]) and supports more array types. The array type must match before any comparison will occur. For example, the function will fail when comparing e.g. Integer[] and Collection even when both arrays contain the same integers.

Parameters:
actual - the actual array, must not be null. Accepts Collections and arrays of both primitive and object types.
expected - the expected array, must not be null. Accepts Collections and arrays of both primitive and object types.
message - an optional message

assertArrayEquals

public static void assertArrayEquals(Object actual,
                                     Object expected)
Asserts that two arrays contain the same elements in the same order. If they do not, an AssertionError is thrown. Provides more verbose error message than Assert#assertEquals(java.lang.Object[], java.lang.Object[]).

Parameters:
actual - the actual array, must not be null. Accepts arrays of both primitive and object types and supports more array types. The array type must match before any comparison will occur. For example, the function will fail when comparing e.g. Integer[] and Collection even when both arrays contain the same integers.
expected - the expected array, must not be null. Accepts arrays of both primitive and object types.

assertArrayEqualsNoOrder

public static void assertArrayEqualsNoOrder(Object actual,
                                            Object expected)
Asserts that two arrays contain the same elements in no particular order. If they do not, an AssertionError, with the given message, is thrown. Provides more verbose error message than Assert#assertEqualsNoOrder(java.lang.Object[], java.lang.Object[], java.lang.String) and supports more array types. The array type must match before any comparison will occur. For example, the function will fail when comparing e.g. Integer[] and Collection even when both arrays contain the same integers.

Parameters:
actual - the actual value
expected - the expected value

assertArrayEqualsNoOrder

public static void assertArrayEqualsNoOrder(Object actual,
                                            Object expected,
                                            String message)
Asserts that two arrays contain the same elements in no particular order. If they do not, an AssertionError, with the given message, is thrown. Provides more verbose error message than Assert#assertEqualsNoOrder(java.lang.Object[], java.lang.Object[], java.lang.String) and supports more array types. The array type must match before any comparison will occur. For example, the function will fail when comparing e.g. Integer[] and Collection even when both arrays contain the same integers.

The items must obey contracts required by HashSet.

Parameters:
actual - the actual value
expected - the expected value
message - the assertion error message

assertCorrectEquals

public static <T> void assertCorrectEquals(T object,
                                           T differentInstance,
                                           T differentValues,
                                           T extendingClass)
Tests given object for correct Object.equals(java.lang.Object) contract. Inspired by the Gargoylesoftware's EqualsTester. Example:
final Object a = new Foo(4); // original object
 final Object b = new Foo(4); // another object that has the same values as the original
 final Object c = new Foo(5); // another object with different values
 final Object d = new Foo(4) {}; // a subclass of Foo with the same values as the original
 new EqualsTester(a, b, c, d);
Throws AssertionError on failure.

Type Parameters:
T - the object type
Parameters:
object - the original object
differentInstance - another object that has the same values as the original. May be the same instance as the object if the object is meant to be a singleton.
differentValues - another object with different values. May be null if the object is meant to be a singleton.
extendingClass - a subclass of Foo with the same values as the original. May be null if T is final.

assertUtilityClass

public static void assertUtilityClass(Class<?> clazz)
Checks that given class is really an utility class - it has a private zero-arg constructor, does not have any other constructors and the constructor throws AssertionError.

Parameters:
clazz - the class to construct, must not be null.

assertCorrectEqualsForJpaBean

public static void assertCorrectEqualsForJpaBean(Class<?> clazz,
                                                 Object id1,
                                                 Object id2)
Verifies that given JPA bean has a correct implementation of Object.equals(java.lang.Object) and Object.hashCode() methods. Requires the CGLIB library.

Parameters:
clazz - the JPA bean class. The bean is instantiated automatically using a zero-arg constructor. The bean is expected to have a method "setId()" which takes given id as a single parameter.
id1 - a first ID to feed to the JPA bean entity.
id2 - second ID to be set to a JPA bean instance. Must not be equal to the first ID.

assertBeansEquals

public static <T> void assertBeansEquals(T got,
                                         T expected)
Verifies that given beans are equal.

Type Parameters:
T - the bean type.
Parameters:
got - what we got.
expected - what was expected.

assertBeansEquals

public static <T> void assertBeansEquals(T got,
                                         T expected,
                                         JavaUtils.IEqualsComparator comparator)
Verifies that given beans are equal.

Type Parameters:
T - the bean type.
Parameters:
got - what we got.
expected - what was expected.
comparator - the comparator to use


Copyright © 2009-2010. All Rights Reserved.