|
|
@@ -1,186 +1,211 @@
|
|
|
-/*
|
|
|
- * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
- * @copyright Ranides Atterwim
|
|
|
- * @license WTFPL
|
|
|
- * @url http://ranides.net/projects/assira.junit
|
|
|
- */
|
|
|
-package net.ranides.assira.junit;
|
|
|
-
|
|
|
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.ObjectInputStream;
|
|
|
-import java.io.ObjectOutputStream;
|
|
|
-import java.util.Collection;
|
|
|
-import java.util.Iterator;
|
|
|
-import java.util.function.BiPredicate;
|
|
|
-import static org.junit.Assert.assertEquals;
|
|
|
-import static org.junit.Assert.fail;
|
|
|
-
|
|
|
-/**
|
|
|
- *
|
|
|
- * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
- */
|
|
|
-public final class NewAssert extends org.junit.Assert {
|
|
|
-
|
|
|
- private NewAssert() { }
|
|
|
-
|
|
|
- public static void assertThrows(Class<? extends Throwable> error, Action action) {
|
|
|
- try {
|
|
|
- action.run();
|
|
|
- fail(error + " expected");
|
|
|
- } catch(Throwable cause) { // NOPMD
|
|
|
- if( !error.isInstance(cause)) {
|
|
|
- throw rethrow(cause);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static void assertSerializable(Object value) {
|
|
|
- ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
|
|
- try (ObjectOutputStream ostream = new ObjectOutputStream(buffer)) {
|
|
|
- ostream.writeObject(value);
|
|
|
-
|
|
|
- InputStream idata = new ByteArrayInputStream(buffer.toByteArray());
|
|
|
- try (ObjectInputStream istream = new ObjectInputStream(idata)) {
|
|
|
- assertEquals(value, istream.readObject());
|
|
|
- }
|
|
|
- } catch(IOException | ClassNotFoundException cause) {
|
|
|
- throw rethrow(cause);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Requirements (object : hash / pseudo-value)
|
|
|
- * <pre>
|
|
|
- * A : 1 / 1
|
|
|
- * B : 1 / 1
|
|
|
- * C : 1 / 2
|
|
|
- * D : 2 / 3
|
|
|
- * </pre>
|
|
|
- * @param a1
|
|
|
- * @param a2
|
|
|
- * @param b
|
|
|
- * @param c
|
|
|
- */
|
|
|
- @SuppressFBWarnings("SA_LOCAL_SELF_COMPARISON")
|
|
|
- public static void assertEquality(Object a, Object b, Object c, Object d) {
|
|
|
- int ha = a.hashCode();
|
|
|
- int hb = b.hashCode();
|
|
|
- int hc = c.hashCode();
|
|
|
- int hd = d.hashCode();
|
|
|
-
|
|
|
- assertTrue("hashcode: a==b", ha == hb);
|
|
|
- assertTrue("hashcode: a==c", ha == hc);
|
|
|
- assertTrue("hashcode: a!=d", ha != hd);
|
|
|
-
|
|
|
- assertTrue("a==a", a.equals(a));
|
|
|
- assertTrue("b==b", b.equals(b));
|
|
|
- assertTrue("c==c", c.equals(c));
|
|
|
- assertTrue("d==d", d.equals(d));
|
|
|
-
|
|
|
- assertSymEquals("a == b", a,b);
|
|
|
-
|
|
|
- assertSymNotEquals("a != c", a,c);
|
|
|
- assertSymNotEquals("a != d", a,d);
|
|
|
- assertSymNotEquals("b != c", b,c);
|
|
|
- assertSymNotEquals("b != d", b,d);
|
|
|
- assertSymNotEquals("c != d", c,d);
|
|
|
-
|
|
|
- assertFalse("a != object", a.equals(new Object()));
|
|
|
- assertFalse("b != object", b.equals(new Object()));
|
|
|
- assertFalse("c != object", c.equals(new Object()));
|
|
|
- assertFalse("d != object", d.equals(new Object()));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Requirements (object : hash / pseudo-value)
|
|
|
- * <pre>
|
|
|
- * A : 1 / 1
|
|
|
- * B : 1 / 1
|
|
|
- * D : 2 / 2
|
|
|
- * </pre>
|
|
|
- * @param a1
|
|
|
- * @param a2
|
|
|
- * @param b
|
|
|
- * @param c
|
|
|
- */
|
|
|
- @SuppressFBWarnings("SA_LOCAL_SELF_COMPARISON")
|
|
|
- public static void assertEquality(Object a, Object b, Object d) {
|
|
|
- int ha = a.hashCode();
|
|
|
- int hb = b.hashCode();
|
|
|
- int hd = d.hashCode();
|
|
|
-
|
|
|
- assertTrue("hashcode: a==b", ha == hb);
|
|
|
- assertTrue("hashcode: a!=d", ha != hd);
|
|
|
-
|
|
|
- assertTrue("a==a", a.equals(a));
|
|
|
- assertTrue("b==b", b.equals(b));
|
|
|
- assertTrue("d==d", d.equals(d));
|
|
|
-
|
|
|
- assertSymEquals("a == b", a,b);
|
|
|
-
|
|
|
- assertSymNotEquals("a != d", a,d);
|
|
|
- assertSymNotEquals("b != d", b,d);
|
|
|
-
|
|
|
- assertFalse("a != object", a.equals(new Object()));
|
|
|
- assertFalse("b != object", b.equals(new Object()));
|
|
|
- assertFalse("d != object", d.equals(new Object()));
|
|
|
- }
|
|
|
-
|
|
|
- public static void assertSymEquals(Object value1, Object value2) {
|
|
|
- assertEquals(value1,value2);
|
|
|
- assertEquals(value2,value1);
|
|
|
- }
|
|
|
-
|
|
|
- public static void assertSymEquals(String message, Object value1, Object value2) {
|
|
|
- assertTrue(message, value1.equals(value2) );
|
|
|
- assertTrue(message, value2.equals(value1) );
|
|
|
- }
|
|
|
-
|
|
|
- private static void assertSymNotEquals(String message, Object value1, Object value2) {
|
|
|
- assertFalse(message, value1.equals(value2) );
|
|
|
- assertFalse(message, value2.equals(value1) );
|
|
|
- }
|
|
|
-
|
|
|
- public static <A,B> void assertMatch(Collection<A> expected, Collection<B> values, BiPredicate<A,B> predicate) {
|
|
|
- assertMatch("", expected, values, predicate);
|
|
|
- }
|
|
|
-
|
|
|
- public static <A,B> void assertMatch(String message, Collection<A> expected, Collection<B> values, BiPredicate<A,B> predicate) {
|
|
|
- assertEquals(message + ": different size", expected.size(), values.size());
|
|
|
- Iterator<A> ia = expected.iterator();
|
|
|
- Iterator<B> ib = values.iterator();
|
|
|
- for(int i=0, n=expected.size(); i<n; i++) {
|
|
|
- A a = ia.next();
|
|
|
- B b = ib.next();
|
|
|
- if(!predicate.test(a,b)) {
|
|
|
- fail("line[" + i+"] expected '" + a +"' but was '" + b + "'");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public interface Action {
|
|
|
-
|
|
|
- void run() throws Exception;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- static RuntimeException rethrow(Throwable cause) {
|
|
|
- new ExceptionErasure<RuntimeException>().rethrow(cause);
|
|
|
-
|
|
|
- // not reachable, wyjątek bez wrappowania leci linię wyżej
|
|
|
- throw new RuntimeException(cause); // NOPMD
|
|
|
- }
|
|
|
-
|
|
|
- private static class ExceptionErasure<T extends Throwable> { // NOPMD
|
|
|
-
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- private void rethrow(Throwable exception) throws T {
|
|
|
- throw (T) exception;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-}
|
|
|
+/*
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ * @copyright Ranides Atterwim
|
|
|
+ * @license WTFPL
|
|
|
+ * @url http://ranides.net/projects/assira.junit
|
|
|
+ */
|
|
|
+package net.ranides.assira.junit;
|
|
|
+
|
|
|
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.ObjectInputStream;
|
|
|
+import java.io.ObjectOutputStream;
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.function.BiPredicate;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ */
|
|
|
+public final class NewAssert extends org.junit.Assert {
|
|
|
+
|
|
|
+ private NewAssert() { }
|
|
|
+
|
|
|
+ public static void assertThrows(Class<? extends Throwable> error, Action action) {
|
|
|
+ try {
|
|
|
+ action.run();
|
|
|
+ fail(error + " expected");
|
|
|
+ } catch(Throwable cause) { // NOPMD
|
|
|
+ if( !error.isInstance(cause)) {
|
|
|
+ throw rethrow(cause);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void assertSerializable(Object value) {
|
|
|
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
|
|
+ try (ObjectOutputStream ostream = new ObjectOutputStream(buffer)) {
|
|
|
+ ostream.writeObject(value);
|
|
|
+
|
|
|
+ InputStream idata = new ByteArrayInputStream(buffer.toByteArray());
|
|
|
+ try (ObjectInputStream istream = new ObjectInputStream(idata)) {
|
|
|
+ assertEquals(value, istream.readObject());
|
|
|
+ }
|
|
|
+ } catch(IOException | ClassNotFoundException cause) {
|
|
|
+ throw rethrow(cause);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Requirements (object : hash / pseudo-value)
|
|
|
+ * <pre>
|
|
|
+ * A : 1 / 1
|
|
|
+ * B : 1 / 1
|
|
|
+ * C : 1 / 2
|
|
|
+ * D : 2 / 3
|
|
|
+ * </pre>
|
|
|
+ * @param a1
|
|
|
+ * @param a2
|
|
|
+ * @param b
|
|
|
+ * @param c
|
|
|
+ */
|
|
|
+ @SuppressFBWarnings("SA_LOCAL_SELF_COMPARISON")
|
|
|
+ public static void assertEquality(Object a, Object b, Object c, Object d) {
|
|
|
+ int ha = a.hashCode();
|
|
|
+ int hb = b.hashCode();
|
|
|
+ int hc = c.hashCode();
|
|
|
+ int hd = d.hashCode();
|
|
|
+
|
|
|
+ assertTrue("hashcode: a==b", ha == hb);
|
|
|
+ assertTrue("hashcode: a==c", ha == hc);
|
|
|
+ assertTrue("hashcode: a!=d", ha != hd);
|
|
|
+
|
|
|
+ assertTrue("a==a", a.equals(a));
|
|
|
+ assertTrue("b==b", b.equals(b));
|
|
|
+ assertTrue("c==c", c.equals(c));
|
|
|
+ assertTrue("d==d", d.equals(d));
|
|
|
+
|
|
|
+ assertSymEquals("a == b", a,b);
|
|
|
+
|
|
|
+ assertSymNotEquals("a != c", a,c);
|
|
|
+ assertSymNotEquals("a != d", a,d);
|
|
|
+ assertSymNotEquals("b != c", b,c);
|
|
|
+ assertSymNotEquals("b != d", b,d);
|
|
|
+ assertSymNotEquals("c != d", c,d);
|
|
|
+
|
|
|
+ assertFalse("a != object", a.equals(new Object()));
|
|
|
+ assertFalse("b != object", b.equals(new Object()));
|
|
|
+ assertFalse("c != object", c.equals(new Object()));
|
|
|
+ assertFalse("d != object", d.equals(new Object()));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Requirements (object : hash / pseudo-value)
|
|
|
+ * <pre>
|
|
|
+ * A : 1 / 1
|
|
|
+ * B : 1 / 1
|
|
|
+ * D : 2 / 2
|
|
|
+ * </pre>
|
|
|
+ * @param a1
|
|
|
+ * @param a2
|
|
|
+ * @param b
|
|
|
+ * @param c
|
|
|
+ */
|
|
|
+ @SuppressFBWarnings("SA_LOCAL_SELF_COMPARISON")
|
|
|
+ public static void assertEquality(Object a, Object b, Object d) {
|
|
|
+ int ha = a.hashCode();
|
|
|
+ int hb = b.hashCode();
|
|
|
+ int hd = d.hashCode();
|
|
|
+
|
|
|
+ assertTrue("hashcode: a==b", ha == hb);
|
|
|
+ assertTrue("hashcode: a!=d", ha != hd);
|
|
|
+
|
|
|
+ assertTrue("a==a", a.equals(a));
|
|
|
+ assertTrue("b==b", b.equals(b));
|
|
|
+ assertTrue("d==d", d.equals(d));
|
|
|
+
|
|
|
+ assertSymEquals("a == b", a,b);
|
|
|
+
|
|
|
+ assertSymNotEquals("a != d", a,d);
|
|
|
+ assertSymNotEquals("b != d", b,d);
|
|
|
+
|
|
|
+ assertFalse("a != object", a.equals(new Object()));
|
|
|
+ assertFalse("b != object", b.equals(new Object()));
|
|
|
+ assertFalse("d != object", d.equals(new Object()));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void assertSymEquals(Object value1, Object value2) {
|
|
|
+ assertEquals(value1,value2);
|
|
|
+ assertEquals(value2,value1);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void assertSymEquals(String message, Object value1, Object value2) {
|
|
|
+ assertTrue(message, value1.equals(value2) );
|
|
|
+ assertTrue(message, value2.equals(value1) );
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void assertSymNotEquals(String message, Object value1, Object value2) {
|
|
|
+ assertFalse(message, value1.equals(value2) );
|
|
|
+ assertFalse(message, value2.equals(value1) );
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <A,B> void assertMatch(Collection<A> expected, Collection<B> values, BiPredicate<A,B> predicate) {
|
|
|
+ assertMatch("", expected, values, predicate);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <A,B> void assertMatch(String message, Collection<A> expected, Collection<B> values, BiPredicate<A,B> predicate) {
|
|
|
+ assertEquals(message + ": different size", expected.size(), values.size());
|
|
|
+ Iterator<A> ia = expected.iterator();
|
|
|
+ Iterator<B> ib = values.iterator();
|
|
|
+ for(int i=0, n=expected.size(); i<n; i++) {
|
|
|
+ A a = ia.next();
|
|
|
+ B b = ib.next();
|
|
|
+ if(!predicate.test(a,b)) {
|
|
|
+ fail("line[" + i+"] expected '" + a +"' but was '" + b + "'");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public static <E extends Enum<E>> void assertEnumInvariants(Class<E> enumtype) throws Exception {
|
|
|
+ Method mValues = enumtype.getMethod("values");
|
|
|
+ Method mValueOf = enumtype.getMethod("valueOf", String.class);
|
|
|
+
|
|
|
+ E[] values = (E[])mValues.invoke(null);
|
|
|
+
|
|
|
+ assertTrue( values.getClass().getComponentType().equals(enumtype) );
|
|
|
+ assertTrue( values.length > 0 );
|
|
|
+ assertTrue( enumtype.isInstance(values[0]) );
|
|
|
+
|
|
|
+ assertEquals(values[0], mValueOf.invoke(null, values[0].name()));
|
|
|
+
|
|
|
+ assertThrows(IllegalArgumentException.class, ()-> {
|
|
|
+ try {
|
|
|
+ mValueOf.invoke(null, "__NONAME_IN_ENUM__");
|
|
|
+ } catch(InvocationTargetException cause) {
|
|
|
+ throw rethrow(cause.getCause());
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ assertNotSame(mValues.invoke(null), mValues.invoke(null));
|
|
|
+ assertArrayEquals((E[])mValues.invoke(null), (E[])mValues.invoke(null));
|
|
|
+ }
|
|
|
+
|
|
|
+ public interface Action {
|
|
|
+
|
|
|
+ void run() throws Exception;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ static RuntimeException rethrow(Throwable cause) {
|
|
|
+ new ExceptionErasure<RuntimeException>().rethrow(cause);
|
|
|
+
|
|
|
+ // not reachable, wyjątek bez wrappowania leci linię wyżej
|
|
|
+ throw new RuntimeException(cause); // NOPMD
|
|
|
+ }
|
|
|
+
|
|
|
+ private static class ExceptionErasure<T extends Throwable> { // NOPMD
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private void rethrow(Throwable exception) throws T {
|
|
|
+ throw (T) exception;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|