|
|
@@ -1,34 +1,117 @@
|
|
|
-/*
|
|
|
- * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
- * @copyright Ranides Atterwim
|
|
|
- * @license WTFPL
|
|
|
- * @url http://ranides.net/projects/???
|
|
|
- */
|
|
|
-package net.ranides.assira.junit;
|
|
|
-
|
|
|
-import static org.junit.Assert.assertEquals;
|
|
|
-import static org.junit.Assert.fail;
|
|
|
-
|
|
|
-/**
|
|
|
- *
|
|
|
- * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
- */
|
|
|
-public final class QAssert {
|
|
|
-
|
|
|
- private QAssert() { }
|
|
|
-
|
|
|
- public static void assertThrows(Class<? extends Throwable> error, Action action) {
|
|
|
- try {
|
|
|
- action.run();
|
|
|
- fail(error + " expected");
|
|
|
- } catch(Throwable cause) {
|
|
|
- assertEquals(error, cause.getClass());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public interface Action {
|
|
|
-
|
|
|
- Object run() throws Exception;
|
|
|
-
|
|
|
- }
|
|
|
-}
|
|
|
+/*
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ * @copyright Ranides Atterwim
|
|
|
+ * @license WTFPL
|
|
|
+ * @url http://ranides.net/projects/???
|
|
|
+ */
|
|
|
+package net.ranides.assira.junit;
|
|
|
+
|
|
|
+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 static org.junit.Assert.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ */
|
|
|
+public final class QAssert {
|
|
|
+
|
|
|
+ private QAssert() { }
|
|
|
+
|
|
|
+ public static void assertThrows(Class<? extends Throwable> error, Action action) {
|
|
|
+ try {
|
|
|
+ action.run();
|
|
|
+ fail(error + " expected");
|
|
|
+ } catch(Throwable cause) {
|
|
|
+ assertEquals(error, cause.getClass());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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
|
|
|
+ */
|
|
|
+ @SuppressWarnings("PMD.ShortVar")
|
|
|
+ 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);
|
|
|
+
|
|
|
+ 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()));
|
|
|
+ }
|
|
|
+
|
|
|
+ private 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 interface Action {
|
|
|
+
|
|
|
+ void run() throws Exception;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static RuntimeException rethrow(Throwable cause) {
|
|
|
+ new ExceptionErasure<RuntimeException>().rethrow(cause);
|
|
|
+
|
|
|
+ // not reachable, wyjątek bez wrappowania leci linię wyżej
|
|
|
+ throw new RuntimeException(cause);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static class ExceptionErasure<T extends Throwable> { // NOPMD
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private void rethrow(Throwable exception) throws T {
|
|
|
+ throw (T) exception;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|