/* * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package jdk.testlibrary; import java.util.Arrays; import java.util.Collection; import java.util.List; /** * All-static helper to quickly generate check-and-throw statements. All methods * will throw {@link AssertionError} if the check on the arguments fail. Unlike * a java assert, this will try to actually communicate something about the * failing values. All methods take zero or more message strings as well, which * in case of an error will be added to the thrown error. */ public final class Asserts { private Asserts() { } private static void assertion(boolean cond, Object o1, Object o2, String cmp, Object... msgs) { if (cond) { return; } StringBuilder buf = new StringBuilder(); buf.append("Assertion failed: ").append(o1).append(' ').append(cmp) .append(' ').append(o2); for (Object o : msgs) { buf.append(" : ").append(o); } throw new AssertionError(buf.toString()); } public static void assertTrue(boolean b, Object... msgs) { assertion(b, b, true, "=", msgs); } public static void assertFalse(boolean b, Object... msgs) { assertion(!b, b, false, "=", msgs); } public static void assertEquals(T o1, T o2, Object... msgs) { assertion(o1 == o2 || (o1 != null && o1.equals(o2)), o1, o2, "=", msgs); } public static void assertNotEquals(T o1, T o2, Object... msgs) { assertion(o1 != o2 && (o1 == null || !o1.equals(o2)), o1, o2, "!=", msgs); } public static void assertNull(Object o, Object... msgs) { assertEquals(o, null, msgs); } public static void assertNotNull(Object o, Object... msgs) { assertNotEquals(o, null, msgs); } public static void assertLessThan(Comparable o1, T o2, Object... msgs) { assertion(o1.compareTo(o2) < 0, o1, o2, "<", msgs); } public static void assertLessOrEqualThan(Comparable o1, T o2, Object... msgs) { assertion(o1.compareTo(o2) <= 0, o1, o2, "<=", msgs); } public static void assertGreaterThan(Comparable o1, T o2, Object... msgs) { assertion(o1.compareTo(o2) > 0, o1, o2, ">", msgs); } public static void assertGreaterOrEqualThan(Comparable o1, T o2, Object... msgs) { assertion(o1.compareTo(o2) >= 0, o1, o2, ">=", msgs); } public static void assertEQ(T o1, T o2, Object... msgs) { assertEquals(o1, o2, msgs); } public static void assertNE(T o1, T o2, Object... msgs) { assertNotEquals(o1, o2, msgs); } public static void assertLT(Comparable o1, T o2, Object... msgs) { assertLessThan(o1, o2, msgs); } public static void assertGT(Comparable o1, T o2, Object... msgs) { assertGreaterThan(o1, o2, msgs); } public static void assertLE(Comparable o1, T o2, Object... msgs) { assertLessOrEqualThan(o1, o2, msgs); } public static void assertGE(Comparable o1, T o2, Object... msgs) { assertGreaterOrEqualThan(o1, o2, msgs); } public static > T assertEmpty(T c, Object... msgs) { try { assertNotNull(c, msgs); assertEquals(c.size(), 0, msgs); return c; } catch (AssertionError e) { throw new AssertionError(e.getMessage() + " : " + c); } } public static > T assertNotEmpty(T c, Object... msgs) { try { assertNotNull(c, msgs); assertGreaterThan(c.size(), 0, msgs); return c; } catch (AssertionError e) { throw new AssertionError(e.getMessage() + " : " + c); } } public static > T assertSize(T c, int size, Object... msgs) { try { assertNotNull(c, msgs); assertEquals(c.size(), size, msgs); return c; } catch (AssertionError e) { throw new AssertionError(e.getMessage() + " : " + c); } } public static > T assertSizeRange(T c, int minSizeInclusive, int maxSizeExclusive, Object... msgs) { try { assertNotNull(c, msgs); assertGreaterOrEqualThan(c.size(), minSizeInclusive, msgs); assertLessThan(c.size(), maxSizeExclusive, msgs); return c; } catch (AssertionError e) { throw new AssertionError(e.getMessage() + " : " + c); } } public static T assertOneElement(Collection c, Object... msgs) { try { return assertSize(c, 1, msgs).iterator().next(); } catch (AssertionError e) { throw new AssertionError(e.getMessage() + " : " + c); } } public static T assertOneElement(List c, Object... msgs) { try { return assertSize(c, 1, msgs).get(0); } catch (AssertionError e) { throw new AssertionError(e.getMessage() + " : " + c); } } public static void assertContains(Collection c, T[] elements) { assertContains(c, Arrays.asList(elements)); } public static void assertContains(Collection c, Collection elements) { for (T t : elements) { if (!c.contains(t)) { throw new AssertionError("Expected " + c + " to contain " + t); } } } }