Sfoglia il codice sorgente

assira.junit: LogObserver
assira.junit: NewAssert#assertEnumInvariants

Ranides Atterwim 10 anni fa
parent
commit
a907170388

+ 29 - 1
assira.junit/src/main/java/net/ranides/assira/junit/LogObserver.java

@@ -6,8 +6,12 @@
  */
 package net.ranides.assira.junit;
 
+import java.util.List;
 import java.util.Queue;
 import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.function.IntPredicate;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
 /**
@@ -52,8 +56,32 @@ public final class LogObserver {
         LogObserver.enabled = enabled;
     }
     
-    public static Stream<LogMessage> messages() {
+    public static List<LogMessage> list() {
+        return TARGET.stream().collect(Collectors.toList());
+    }
+    
+    public static Stream<LogMessage> stream() {
         return TARGET.stream();
     }
     
+    public static Stream<LogMessage> stream(Predicate<LogMessage> filter) {
+        return stream().filter(filter);
+    }
+    
+    public static Stream<LogMessage> forText(Predicate<String> filter) {
+        return stream(m -> filter.test(m.text()));
+    }
+    
+    public static Stream<LogMessage> forText(String text) {
+        return stream(m -> text.equals(m.text()));
+    }
+    
+    public static Stream<LogMessage> forLevel(IntPredicate filter) {
+        return stream(m -> filter.test(m.level()));
+    }
+    
+    public static Stream<LogMessage> forLevel(int level) {
+        return stream(m -> m.level() >= level);
+    }
+    
 }

+ 211 - 186
assira.junit/src/main/java/net/ranides/assira/junit/NewAssert.java

@@ -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;
+        }
+
+    }
+}