Java Utililty Methods Assert

List of utility methods to do Assert

Description

The list of methods to do Assert are organized into topic(s).

Method

voidassertContains(String needle, String haystack)
assert Contains
if (haystack.contains(needle))
    return;
throw new AssertionError("The string '" + haystack + "' does not contain '" + needle + "'.");
voidassertDescriptorName(String key)
assert Descriptor Name
assert key != null;
assert key.indexOf(GROUP_SEPARATOR) == -1 : key + " contains invalid chars";
assert key.indexOf(GROUPS_SEPARATOR) == -1 : key + " contains invalid chars";
assert key.indexOf(GROUP_VALUE_SEPARATOR) == -1 : key + " contains invalid chars";
assert !key.contains("=") : key + " contains invalid chars";
assert !key.contains("'") : key + " contains invalid chars";
assert !key.contains("\"") : key + " contains invalid chars";
voidassertDidNotThrow(final Runnable r)
If Java assertions are enabled, execute the given Runnable , and assert(false) if it throws any Throwable .
Can be used for asserting upon stuff which does not return false upon error but throws: A regular Java assert could only consume boolean-returning functions.

The Throwable will be passed as message to the assert, it will not be thrown.
boolean execute = false;
assert (execute = true);
if (execute) {
    try {
        r.run();
    } catch (Throwable t) {
        assert (false) : t;
voidassertDifferentInstance(final Object o1, final Object o2)
assert Different Instance
if (o1 == o2) {
    throw new AssertionError("The compared objects are the same instance of a class");
voidassertDotted(String name)
assert Dotted
if (name.indexOf("/") != -1) {
    throw new IllegalStateException("Expected a dotted name but was passed " + name);
intassertEntryCount(final long entryCount)
Dynamic sharding operations are not currently supported for indices with more than MAX_INT tuples.
if (entryCount >= Integer.MAX_VALUE)
    throw new UnsupportedOperationException("More than GT MAX_INT tuples: n=" + entryCount);
return (int) entryCount;
voidassertEnvVarPresent(String envVarKey, String errorMessage)
assert Env Var Present
if (System.getenv(envVarKey) == null) {
    throw new IllegalArgumentException(errorMessage);
voidassertException(Runnable task, Class clazz)
assert Exception
try {
    task.run();
    throw new AssertionError(String.format("expected exception of type %s", clazz.getSimpleName()));
} catch (Exception ex) {
    if (!ex.getClass().isAssignableFrom(clazz)) {
        new AssertionError(String.format("expected exception of type %s but found exception of type %s",
                clazz.getCanonicalName(), ex.getClass().getCanonicalName()));
voidassertFact(boolean truth, String failure)
assert Fact
if (!truth) {
    System.err.println("assertion failed: " + failure);
    System.exit(1);
voidassertFailed(Object expression, Object message)
assert Failed
if (message == null || "".equals(message)) {
    throw new AssertionError("Expression: " + expression);
} else {
    throw new AssertionError("" + message + ". Expression: " + expression);