Bladeren bron

test (85)

Ranides Atterwim 10 jaren geleden
bovenliggende
commit
498590527b

+ 11 - 38
assira/src/main/java/net/ranides/assira/trace/ExceptionInspector.java

@@ -21,7 +21,6 @@ import java.util.stream.StreamSupport;
 /**
  *
  * @author ranides
- * @todo (assira # 1) totalnie to przebudować, bo 3/4 metod jest bez sensu.
  */
 public final class ExceptionInspector {
 
@@ -57,38 +56,6 @@ public final class ExceptionInspector {
             throw (T) exception;
         }
 
-    }
-
-	public static void print(PrintStream stream, List<? extends Throwable> list) {
-		if(list.isEmpty()) {
-			return;
-		}
-		stream.println();
-		stream.println("CauseList: ");
-		for(int i=0, n=list.size(); i<n; i++) {
-			stream.println("\t[" + i + "] " + list.get(i));
-		}
-		stream.println();
-		for(int i=0, n=list.size(); i<n; i++) {
-			stream.print("CauseList details["+i+"]: ");
-			list.get(i).printStackTrace(stream);
-		}
-    }
-
-    public static void print(PrintWriter writer, List<? extends Throwable> list) {
-        if(list.isEmpty()) {
-			return;
-		}
-		writer.println();
-		writer.println("CauseList: ");
-		for(int i=0, n=list.size(); i<n; i++) {
-			writer.println("\t[" + i + "] " + list.get(i));
-		}
-		writer.println();
-		for(int i=0, n=list.size(); i<n; i++) {
-			writer.println("Caused by["+i+"]:");
-			list.get(i).printStackTrace(writer);
-		}
     }
 	
 	private static final class CauseSpliterator extends Spliterators.AbstractSpliterator<Throwable> {
@@ -102,11 +69,17 @@ public final class ExceptionInspector {
 
 		@Override
 		public boolean tryAdvance(Consumer<? super Throwable> action) {
-			if(null == (current = current.getCause())) {
-				return false;
-			}
-			action.accept(current);
-			return true;	
+            if(null == current) {
+                return false;
+            }
+            action.accept(current);
+            current = current.getCause();
+            return true;
+//			if(null == (current = current.getCause())) {
+//				return false;
+//			}
+//			action.accept(current);
+//			return true;	
 		}
 
 	}

+ 71 - 0
assira/src/test/java/net/ranides/assira/trace/ExceptionInspectorTest.java

@@ -0,0 +1,71 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/???
+ */
+package net.ranides.assira.trace;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.junit.Test;
+import static net.ranides.assira.junit.NewAssert.*;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class ExceptionInspectorTest {
+    
+    public ExceptionInspectorTest() {
+    }
+
+    @Test
+    public void testCauseStream() {
+        Exception e = new IOException(new RuntimeException(new IllegalArgumentException()));
+        
+        List<String> s = ExceptionInspector.getCauseStream(e).map((f)->f.getClass().getName()).collect(Collectors.toList());
+        List<String> expected = Arrays.asList(
+            "java.io.IOException", 
+            "java.lang.RuntimeException", 
+            "java.lang.IllegalArgumentException"
+        );
+        assertEquals(expected, s);
+    }
+    
+    @Test
+    public void testCauseList() {
+        Exception e = new IOException(new RuntimeException(new IllegalArgumentException()));
+        
+        List<Throwable> s = ExceptionInspector.getCauseList(e);
+        List<Throwable> expected = Arrays.asList(
+            e, 
+            e.getCause(), 
+            e.getCause().getCause()
+        );
+        assertEquals(expected, s);
+    }
+    
+    @Test
+    public void testStacktrace() {
+        String st = ExceptionInspector.getStackTrace(new IOException());
+        assertTrue(st.contains("ExceptionInspectorTest"));
+        assertTrue(st.contains("org.junit."));
+        assertTrue(st.contains("org.apache.maven."));
+    }
+    
+    @Test
+    public void testRethrow() {
+        assertThrows(IOException.class, this::rm);
+    }
+    
+    private void rm() {
+        ExceptionInspector.rethrow(new IOException());
+    }
+   
+}