
com.webguys.ponzu.impl.test.Verify Maven / Gradle / Ivy
Show all versions of testutils Show documentation
/*
* Copyright 2011 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.webguys.ponzu.impl.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.concurrent.Callable;
import com.webguys.ponzu.api.block.predicate.Predicate;
import com.webguys.ponzu.api.collection.ImmutableCollection;
import com.webguys.ponzu.api.list.ImmutableList;
import com.webguys.ponzu.api.list.MutableList;
import com.webguys.ponzu.api.map.ImmutableMap;
import com.webguys.ponzu.api.map.MutableMap;
import com.webguys.ponzu.api.multimap.Multimap;
import com.webguys.ponzu.api.set.ImmutableSet;
import com.webguys.ponzu.api.set.MutableSet;
import com.webguys.ponzu.impl.block.factory.Comparators;
import com.webguys.ponzu.impl.block.factory.Predicates;
import com.webguys.ponzu.impl.factory.Lists;
import com.webguys.ponzu.impl.factory.Sets;
import com.webguys.ponzu.impl.map.mutable.UnifiedMap;
import com.webguys.ponzu.impl.set.mutable.UnifiedSet;
import com.webguys.ponzu.impl.tuple.ImmutableEntry;
import com.webguys.ponzu.impl.utility.ArrayIterate;
import com.webguys.ponzu.impl.utility.Iterate;
import org.apache.commons.codec.binary.Base64;
import org.junit.Assert;
/**
* An extension of the {@link Assert} class, which adds useful additional "assert" methods.
* You can import this class instead of Assert, and use it thus, e.g.:
*
* Verify.assertEquals("fred", name); // from original Assert class
* Verify.assertContains("fred", nameList); // from new extensions
* Verify.assertBefore("fred", "jim", orderedNamesList); // from new extensions
*
*/
public final class Verify extends Assert
{
private static final int MAX_DIFFERENCES = 5;
private static final byte[] LINE_SEPARATOR = {'\n'};
private Verify()
{
throw new AssertionError("Suppress default constructor for noninstantiability");
}
/**
* Mangles the stack trace of {@link AssertionError} so that it looks like its been thrown from the line that
* called to a custom assertion.
*
* This method behaves identically to {@link #throwMangledException(AssertionError, int)} and is provided
* for convenience for assert methods that only want to pop two stack frames. The only time that you would want to
* call the other {@link #throwMangledException(AssertionError, int)} method is if you have a custom assert
* that calls another custom assert i.e. the source line calling the custom asserts is more than two stack frames
* away
*
* @param e The exception to mangle.
* @see #throwMangledException(AssertionError, int)
*/
public static void throwMangledException(AssertionError e)
{
/*
* Note that we actually remove 3 frames from the stack trace because
* we wrap the real method doing the work: e.fillInStackTrace() will
* include us in the exceptions stack frame.
*/
throwMangledException(e, 3);
}
/**
* Mangles the stack trace of {@link AssertionError} so that it looks like
* its been thrown from the line that called to a custom assertion.
*
* This is useful for when you are in a debugging session and you want to go to the source
* of the problem in the test case quickly. The regular use case for this would be something
* along the lines of:
*
* 1 public class TestFoo extends junit.framework.TestCase {
* 2 public void testFoo() throws Exception {
* 3 Foo foo = new Foo();
* 4 ...
* 5 assertFoo(foo);
* 6 }
* 7
* 8 // Custom assert
* 9 private static void assertFoo(Foo foo) {
* 10 try {
* 11 assertEquals(...);
* 12 ....
* 13 assertSame(...);
* 14 } catch (AssertionFailedException e) {
* 15 AssertUtils.throwMangledException(e, 2);
* 16 }
* 17 }
* 18 }
*
*
* Without the try ... catch
block around lines 11-13 the stack trace following a test failure
* would look a little like:
*
*
* java.lang.AssertionError: ...
* at TestFoo.assertFoo(TestFoo.java:11)
* at TestFoo.testFoo(TestFoo.java:5)
* at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
* at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
* at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
* at java.lang.reflect.Method.invoke(Method.java:324)
* ...
*
*
* Note that the source of the error isn't readily apparent as the first line in the stack trace
* is the code within the custom assert. If we were debugging the failure we would be more interested
* in the second line of the stack trace which shows us where in our tests the assert failed.
*
* With the try ... catch
block around lines 11-13 the stack trace would look like the
* following:
*
*
* java.lang.AssertionError: ...
* at TestFoo.testFoo(TestFoo.java:5)
* at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
* at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
* at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
* at java.lang.reflect.Method.invoke(Method.java:324)
* ...
*
*
* Here the source of the error is more visible as we can instantly see that the testFoo test is
* failing at line 5.
*
* @param e The exception to mangle.
* @param framesToPop The number of frames to remove from the stack trace.
* @throws AssertionError that was given as an argument with its stack trace mangled.
*/
public static void throwMangledException(AssertionError e, int framesToPop)
{
e.fillInStackTrace();
StackTraceElement[] stackTrace = e.getStackTrace();
StackTraceElement[] newStackTrace = new StackTraceElement[stackTrace.length - framesToPop];
System.arraycopy(stackTrace, framesToPop, newStackTrace, 0, newStackTrace.length);
e.setStackTrace(newStackTrace);
throw e;
}
public static void fail(String message, Throwable cause)
{
AssertionError failedException = new AssertionError(message);
failedException.initCause(cause);
throwMangledException(failedException);
}
/**
* Assert that two items are not the same. If one item is null, the the other must be non-null.
*/
public static void assertNotEquals(String itemsName, Object item1, Object item2)
{
try
{
if (Comparators.nullSafeEquals(item1, item2) || Comparators.nullSafeEquals(item2, item1))
{
Assert.fail(itemsName + " should not be equal, item1:<" + item1 + ">, item2:<" + item2 + '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that two items are not the same. If one item is null, the the other must be non-null.
*/
public static void assertNotEquals(Object item1, Object item2)
{
try
{
assertNotEquals("items", item1, item2);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two Strings are not equal.
*/
public static void assertNotEquals(String itemName, String notExpected, String actual)
{
try
{
if (Comparators.nullSafeEquals(notExpected, actual))
{
Assert.fail(itemName + " should not equal:<" + notExpected + '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two Strings are not equal.
*/
public static void assertNotEquals(String notExpected, String actual)
{
try
{
assertNotEquals("string", notExpected, actual);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two doubles are not equal concerning a delta. If the expected value is infinity then the delta value
* is ignored.
*/
public static void assertNotEquals(String itemName, double notExpected, double actual, double delta)
{
// handle infinity specially since subtracting to infinite values gives NaN and the
// the following test fails
try
{
if (Double.isInfinite(notExpected) && notExpected == actual || Math.abs(notExpected - actual) <= delta)
{
Assert.fail(itemName + " should not be equal:<" + notExpected + '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two doubles are not equal concerning a delta. If the expected value is infinity then the delta value
* is ignored.
*/
public static void assertNotEquals(double notExpected, double actual, double delta)
{
try
{
assertNotEquals("double", notExpected, actual, delta);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two floats are not equal concerning a delta. If the expected value is infinity then the delta value
* is ignored.
*/
public static void assertNotEquals(String itemName, float notExpected, float actual, float delta)
{
try
{
// handle infinity specially since subtracting to infinite values gives NaN and the
// the following test fails
if (Float.isInfinite(notExpected) && notExpected == actual || Math.abs(notExpected - actual) <= delta)
{
Assert.fail(itemName + " should not be equal:<" + notExpected + '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two floats are not equal concerning a delta. If the expected value is infinity then the delta value
* is ignored.
*/
public static void assertNotEquals(float expected, float actual, float delta)
{
try
{
assertNotEquals("float", expected, actual, delta);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two longs are not equal.
*/
public static void assertNotEquals(String itemName, long notExpected, long actual)
{
try
{
if (notExpected == actual)
{
Assert.fail(itemName + " should not be equal:<" + notExpected + '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two longs are not equal.
*/
public static void assertNotEquals(long notExpected, long actual)
{
try
{
assertNotEquals("long", notExpected, actual);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two booleans are not equal.
*/
public static void assertNotEquals(String itemName, boolean notExpected, boolean actual)
{
try
{
if (notExpected == actual)
{
Assert.fail(itemName + " should not be equal:<" + notExpected + '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two booleans are not equal.
*/
public static void assertNotEquals(boolean notExpected, boolean actual)
{
try
{
assertNotEquals("boolean", notExpected, actual);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two bytes are not equal.
*/
public static void assertNotEquals(String itemName, byte notExpected, byte actual)
{
try
{
if (notExpected == actual)
{
Assert.fail(itemName + " should not be equal:<" + notExpected + '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two bytes are not equal.
*/
public static void assertNotEquals(byte notExpected, byte actual)
{
try
{
assertNotEquals("byte", notExpected, actual);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two chars are not equal.
*/
public static void assertNotEquals(String itemName, char notExpected, char actual)
{
try
{
if (notExpected == actual)
{
Assert.fail(itemName + " should not be equal:<" + notExpected + '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two chars are not equal.
*/
public static void assertNotEquals(char notExpected, char actual)
{
try
{
assertNotEquals("char", notExpected, actual);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two shorts are not equal.
*/
public static void assertNotEquals(String itemName, short notExpected, short actual)
{
try
{
if (notExpected == actual)
{
Assert.fail(itemName + " should not be equal:<" + notExpected + '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two shorts are not equal.
*/
public static void assertNotEquals(short notExpected, short actual)
{
try
{
assertNotEquals("short", notExpected, actual);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two ints are not equal.
*/
public static void assertNotEquals(String itemName, int notExpected, int actual)
{
try
{
if (notExpected == actual)
{
Assert.fail(itemName + " should not be equal:<" + notExpected + '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Asserts that two ints are not equal.
*/
public static void assertNotEquals(int notExpected, int actual)
{
try
{
assertNotEquals("int", notExpected, actual);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link Collection} is empty.
*/
public static void assertEmpty(Collection> actualCollection)
{
try
{
assertEmpty("collection", actualCollection);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link Collection} is empty.
*/
public static void assertEmpty(String collectionName, Collection> actualCollection)
{
try
{
assertObjectNotNull(collectionName, actualCollection);
if (!actualCollection.isEmpty())
{
Assert.fail(collectionName + " should be empty; actual size:<" + actualCollection.size() + '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link Iterable} is empty.
*/
public static void assertIterableEmpty(Iterable> iterable)
{
try
{
assertIterableEmpty("iterable", iterable);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link Iterable} is empty.
*/
public static void assertIterableEmpty(String iterableName, Iterable> iterable)
{
try
{
assertObjectNotNull(iterableName, iterable);
if (!Iterate.isEmpty(iterable))
{
Assert.fail(iterableName + " should be empty; actual size:<" + Iterate.sizeOf(iterable) + '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given object is an instanceof expectedClassType.
*/
public static void assertInstanceOf(Class> expectedClassType, Object actualObject)
{
try
{
Verify.assertInstanceOf(actualObject.getClass().getName(), expectedClassType, actualObject);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given object is an instanceof expectedClassType.
*/
public static void assertInstanceOf(String objectName, Class> expectedClassType, Object actualObject)
{
try
{
if (!expectedClassType.isInstance(actualObject))
{
Assert.fail(objectName + " is not an instance of " + expectedClassType.getName());
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link Map} is empty.
*/
public static void assertEmpty(Map, ?> actualMap)
{
try
{
assertEmpty("map", actualMap);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link Multimap} is empty.
*/
public static void assertEmpty(Multimap, ?> actualMultimap)
{
try
{
assertEmpty("multimap", actualMultimap);
}
catch (AssertionError e)
{
Verify.throwMangledException(e);
}
}
/**
* Assert that the given {@link Multimap} is empty.
*/
public static void assertEmpty(String multimapName, Multimap, ?> actualMultimap)
{
try
{
Verify.assertObjectNotNull(multimapName, actualMultimap);
if (!actualMultimap.isEmpty())
{
Assert.fail(multimapName + " should be empty; actual size:<" + actualMultimap.size() + '>');
}
}
catch (AssertionError e)
{
Verify.throwMangledException(e);
}
}
/**
* Assert that the given {@link Map} is empty.
*/
public static void assertEmpty(String mapName, Map, ?> actualMap)
{
try
{
assertObjectNotNull(mapName, actualMap);
if (!actualMap.isEmpty())
{
Assert.fail(mapName + " should be empty; actual size:<" + actualMap.size() + '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
public static void assertEmpty(ImmutableMap, ?> actualImmutableMap)
{
try
{
assertEmpty("immutable map", actualImmutableMap);
}
catch (AssertionError e)
{
Verify.throwMangledException(e);
}
}
public static void assertEmpty(String mapName, ImmutableMap, ?> actualImmutableMap)
{
try
{
assertObjectNotNull(mapName, actualImmutableMap);
if (!actualImmutableMap.isEmpty())
{
Assert.fail(mapName + " should be empty; actual size:<" + actualImmutableMap.size() + '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link Collection} is not empty.
*/
public static void assertNotEmpty(Collection> actualCollection)
{
try
{
assertNotEmpty("collection", actualCollection);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link Collection} is not empty.
*/
public static void assertNotEmpty(String collectionName, Collection> actualCollection)
{
try
{
assertObjectNotNull(collectionName, actualCollection);
Assert.assertFalse(collectionName + " should be non-empty, but was empty", actualCollection.isEmpty());
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link Iterable} is not empty.
*/
public static void assertIterableNotEmpty(Iterable> iterable)
{
try
{
assertIterableNotEmpty("iterable", iterable);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link Iterable} is not empty.
*/
public static void assertIterableNotEmpty(String iterableName, Iterable> iterable)
{
try
{
assertObjectNotNull(iterableName, iterable);
Assert.assertFalse(iterableName + " should be non-empty, but was empty", Iterate.isEmpty(iterable));
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link Map} is not empty.
*/
public static void assertNotEmpty(Map, ?> actualMap)
{
try
{
assertNotEmpty("map", actualMap);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link Map} is not empty.
*/
public static void assertNotEmpty(String mapName, Map, ?> actualMap)
{
try
{
assertObjectNotNull(mapName, actualMap);
Assert.assertFalse(mapName + " should be non-empty, but was empty", actualMap.isEmpty());
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link Multimap} is not empty.
*/
public static void assertNotEmpty(Multimap, ?> actualMultimap)
{
try
{
assertNotEmpty("multimap", actualMultimap);
}
catch (AssertionError e)
{
Verify.throwMangledException(e);
}
}
/**
* Assert that the given {@link Multimap} is not empty.
*/
public static void assertNotEmpty(String multimapName, Multimap, ?> actualMultimap)
{
try
{
Verify.assertObjectNotNull(multimapName, actualMultimap);
Assert.assertTrue(multimapName + " should be non-empty, but was empty", actualMultimap.notEmpty());
}
catch (AssertionError e)
{
Verify.throwMangledException(e);
}
}
public static void assertNotEmpty(String itemsName, T[] items)
{
try
{
assertObjectNotNull(itemsName, items);
assertNotEquals(itemsName, 0, items.length);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
public static void assertNotEmpty(T[] items)
{
try
{
assertNotEmpty("items", items);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert the size of the given array.
*/
public static void assertSize(int expectedSize, Object[] actualArray)
{
try
{
assertSize("array", expectedSize, actualArray);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert the size of the given array.
*/
public static void assertSize(String arrayName, int expectedSize, Object[] actualArray)
{
try
{
Assert.assertNotNull(arrayName + " should not be null", actualArray);
int actualSize = actualArray.length;
if (actualSize != expectedSize)
{
Assert.fail("Incorrect size for "
+ arrayName
+ "; expected:<"
+ expectedSize
+ "> but was:<"
+ actualSize
+ '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert the size of the given {@link Collection}.
*/
public static void assertSize(int expectedSize, Collection> actualCollection)
{
try
{
assertSize("collection", expectedSize, actualCollection);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert the size of the given {@link Collection}.
*/
public static void assertSize(
String collectionName,
int expectedSize,
Collection> actualCollection)
{
try
{
assertObjectNotNull(collectionName, actualCollection);
int actualSize = actualCollection.size();
if (actualSize != expectedSize)
{
Assert.fail("Incorrect size for "
+ collectionName
+ "; expected:<"
+ expectedSize
+ "> but was:<"
+ actualSize
+ '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert the size of the given {@link Iterable}.
*/
public static void assertIterableSize(int expectedSize, Iterable> actualIterable)
{
try
{
assertIterableSize("iterable", expectedSize, actualIterable);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert the size of the given {@link Iterable}.
*/
public static void assertIterableSize(
String iterableName,
int expectedSize,
Iterable> actualIterable)
{
try
{
assertObjectNotNull(iterableName, actualIterable);
int actualSize = Iterate.sizeOf(actualIterable);
if (actualSize != expectedSize)
{
Assert.fail("Incorrect size for "
+ iterableName
+ "; expected:<"
+ expectedSize
+ "> but was:<"
+ actualSize
+ '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert the size of the given {@link Map}.
*/
public static void assertSize(String mapName, int expectedSize, Map, ?> actualMap)
{
try
{
assertSize(mapName, expectedSize, actualMap.keySet());
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert the size of the given {@link Map}.
*/
public static void assertSize(int expectedSize, Map, ?> actualMap)
{
try
{
assertSize("map", expectedSize, actualMap);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert the size of the given {@link Multimap}.
*/
public static void assertSize(int expectedSize, Multimap, ?> actualMultimap)
{
try
{
Verify.assertSize("multimap", expectedSize, actualMultimap);
}
catch (AssertionError e)
{
Verify.throwMangledException(e);
}
}
/**
* Assert the size of the given {@link Multimap}.
*/
public static void assertSize(String multimapName, int expectedSize, Multimap, ?> actualMultimap)
{
try
{
int actualSize = actualMultimap.size();
if (actualSize != expectedSize)
{
Assert.fail("Incorrect size for "
+ multimapName
+ "; expected:<"
+ expectedSize
+ "> but was:<"
+ actualSize
+ '>');
}
}
catch (AssertionError e)
{
Verify.throwMangledException(e);
}
}
/**
* Assert the size of the given {@link ImmutableMap}.
*/
public static void assertSize(int expectedSize, ImmutableMap, ?> actualImmutableMap)
{
try
{
Verify.assertSize("immutable map", expectedSize, actualImmutableMap);
}
catch (AssertionError e)
{
Verify.throwMangledException(e);
}
}
/**
* Assert the size of the given {@link ImmutableMap}.
*/
public static void assertSize(String immutableMapName, int expectedSize, ImmutableMap, ?> actualImmutableMap)
{
try
{
int actualSize = actualImmutableMap.size();
if (actualSize != expectedSize)
{
Assert.fail("Incorrect size for "
+ immutableMapName
+ "; expected:<"
+ expectedSize
+ "> but was:<"
+ actualSize
+ '>');
}
}
catch (AssertionError e)
{
Verify.throwMangledException(e);
}
}
/**
* Assert the size of the given {@link ImmutableSet}.
*/
public static void assertSize(int expectedSize, ImmutableSet> actualImmutableSet)
{
try
{
Verify.assertSize("immutable set", expectedSize, actualImmutableSet);
}
catch (AssertionError e)
{
Verify.throwMangledException(e);
}
}
/**
* Assert the size of the given {@link ImmutableSet}.
*/
public static void assertSize(String immutableSetName, int expectedSize, ImmutableSet> actualImmutableSet)
{
try
{
int actualSize = actualImmutableSet.size();
if (actualSize != expectedSize)
{
Assert.fail("Incorrect size for "
+ immutableSetName
+ "; expected:<"
+ expectedSize
+ "> but was:<"
+ actualSize
+ '>');
}
}
catch (AssertionError e)
{
Verify.throwMangledException(e);
}
}
/**
* Assert that the given stringToFind
is contained within the stringToSearch
.
*/
public static void assertContains(String stringToFind, String stringToSearch)
{
try
{
assertContains("string", stringToFind, stringToSearch);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given stringToFind
is contained within the stringToSearch
.
*/
public static void assertContains(String stringName, String stringToFind, String stringToSearch)
{
try
{
Assert.assertNotNull("stringToFind should not be null", stringToFind);
Assert.assertNotNull("stringToSearch should not be null", stringToSearch);
if (!stringToSearch.contains(stringToFind))
{
Assert.fail(stringName
+ " did not "
+ "contain stringToFind:<"
+ stringToFind
+ "> "
+ "in stringToSearch:<"
+ stringToSearch
+ '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
public static void assertCount(
int expectedCount,
Iterable iterable,
Predicate super T> predicate)
{
Assert.assertEquals(expectedCount, Iterate.count(iterable, predicate));
}
public static void assertAllSatisfy(Iterable iterable, Predicate super T> predicate)
{
try
{
Verify.assertAllSatisfy("The following items failed to satisfy the condition", iterable, predicate);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
public static void assertAllSatisfy(Map map, Predicate super V> predicate)
{
try
{
assertAllSatisfy(map.values(), predicate);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
public static void assertAllSatisfy(String message, Iterable iterable, Predicate super T> predicate)
{
try
{
MutableList unnacceptable = Lists.mutable.of();
for (T each : iterable)
{
if (!predicate.accept(each))
{
unnacceptable.add(each);
}
}
if (unnacceptable.notEmpty())
{
Assert.fail(message + " <" + unnacceptable + '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
public static void assertAnySatisfy(Iterable iterable, Predicate super T> predicate)
{
try
{
assertAnySatisfy("No items satisfied the condition", iterable, predicate);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
public static void assertAnySatisfy(Map map, Predicate super V> predicate)
{
try
{
assertAnySatisfy(map.values(), predicate);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
public static void assertAnySatisfy(String message, Iterable iterable, Predicate super T> predicate)
{
try
{
Assert.assertTrue(message, Predicates.anySatisfy(predicate).accept(iterable));
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link Map} contains all of the given keys and values
*/
public static void assertContainsAllKeyValues(Map, ?> actualMap, Object... keyValues)
{
try
{
assertContainsAllKeyValues("map", actualMap, keyValues);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link Map} contains all of the given keys and values
*/
public static void assertContainsAllKeyValues(
String mapName,
Map, ?> actualMap,
Object... expectedKeyValues)
{
try
{
Verify.assertNotEmpty("Expected keys/values in assertion", expectedKeyValues);
if (expectedKeyValues.length % 2 != 0)
{
Assert.fail("Odd number of keys and values (every key must have a value)");
}
assertObjectNotNull(mapName, actualMap);
assertMapContainsKeys(mapName, actualMap, expectedKeyValues);
assertMapContainsValues(mapName, actualMap, expectedKeyValues);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link ImmutableMap} contains all of the given keys and values
*/
public static void assertContainsAllKeyValues(ImmutableMap, ?> actualImmutableMap, Object... keyValues)
{
try
{
assertContainsAllKeyValues("immutable map", actualImmutableMap, keyValues);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link ImmutableMap} contains all of the given keys and values
*/
public static void assertContainsAllKeyValues(
String immutableMapName,
ImmutableMap, ?> actualImmutableMap,
Object... expectedKeyValues)
{
try
{
Verify.assertNotEmpty("Expected keys/values in assertion", expectedKeyValues);
if (expectedKeyValues.length % 2 != 0)
{
Assert.fail("Odd number of keys and values (every key must have a value)");
}
assertObjectNotNull(immutableMapName, actualImmutableMap);
assertMapContainsKeys(immutableMapName, actualImmutableMap, expectedKeyValues);
assertMapContainsValues(immutableMapName, actualImmutableMap, expectedKeyValues);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
public static void denyContainsAny(Collection> actualCollection, Object... items)
{
try
{
denyContainsAny("collection", actualCollection, items);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
public static void assertContainsNone(Collection> actualCollection, Object... items)
{
try
{
denyContainsAny("collection", actualCollection, items);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link Collection} contains the given item.
*/
public static void assertContains(Object expectedItem, Collection> actualCollection)
{
try
{
assertContains("collection", expectedItem, actualCollection);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link Collection} contains the given item.
*/
public static void assertContains(
String collectionName,
Object expectedItem,
Collection> actualCollection)
{
try
{
assertObjectNotNull(collectionName, actualCollection);
if (!actualCollection.contains(expectedItem))
{
Assert.fail(collectionName + " did not contain expectedItem:<" + expectedItem + '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link ImmutableCollection} contains the given item.
*/
public static void assertContains(Object expectedItem, ImmutableCollection> actualImmutableCollection)
{
try
{
assertContains("ImmutableCollection", expectedItem, actualImmutableCollection);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
/**
* Assert that the given {@link ImmutableCollection} contains the given item.
*/
public static void assertContains(
String immutableCollectionName,
Object expectedItem,
ImmutableCollection> actualImmutableCollection)
{
try
{
assertObjectNotNull(immutableCollectionName, actualImmutableCollection);
if (!actualImmutableCollection.contains(expectedItem))
{
Assert.fail(immutableCollectionName + " did not contain expectedItem:<" + expectedItem + '>');
}
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
public static void assertContainsAll(
Iterable> iterable,
Object... items)
{
try
{
assertContainsAll("iterable", iterable, items);
}
catch (AssertionError e)
{
throwMangledException(e);
}
}
public static void assertContainsAll(
String collectionName,
final Iterable> iterable,
Object... items)
{
try
{
assertObjectNotNull(collectionName, iterable);
Verify.assertNotEmpty("Expected items in assertion", items);
Predicate