Przeglądaj źródła

fix: CheckedStream # flush

rename: MultiException -> RuntimeExceptions
new: IOExceptions
new: ExceptionInspector#print
Ranides Atterwim 11 lat temu
rodzic
commit
889739ef53

+ 6 - 0
src/main/java/net/ranides/assira/io/CheckedStream.java

@@ -49,6 +49,12 @@ public final class CheckedStream {
             checkstate();
             super.write(bytedata);
         }
+
+		@Override
+		public void flush() throws IOException {
+			checkstate();
+			super.flush();
+		}
         
         @Override
         public void close() throws IOException {

+ 66 - 0
src/main/java/net/ranides/assira/io/IOExceptions.java

@@ -0,0 +1,66 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.io;
+
+import java.io.IOException;
+import net.ranides.assira.trace.*;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ *
+ * @author ranides
+ */
+public class IOExceptions extends IOException implements Iterable<IOException> {
+
+    private List<IOException> causeList;
+    
+    public IOExceptions(Collection<? extends IOException> causeList) {
+        super();
+        this.causeList = new LinkedList<>(causeList);
+    }
+
+    public <T extends Throwable> IOExceptions(String message, Collection<? extends IOException> causeList) {
+        super(message);
+        this.causeList = new LinkedList<>(causeList);
+    }
+
+    public IOExceptions(String message) {
+        super(message);
+        this.causeList = new LinkedList<>();
+    }
+    
+    @Override
+    public Iterator<IOException> iterator() {
+		return causeList.iterator();
+    }
+    
+    public List<IOException> getCauseList() {
+        return causeList;
+    }
+    
+    public void addCause(IOException cause) {
+        causeList.add(cause);
+    }
+
+    @Override
+    public void printStackTrace(PrintStream stream) {
+        super.printStackTrace(stream);
+		ExceptionInspector.print(stream, causeList);
+    }
+
+    @Override
+    public void printStackTrace(PrintWriter writer) {
+        super.printStackTrace(writer);
+		ExceptionInspector.print(writer, causeList);
+    }
+    
+}

+ 2 - 8
src/main/java/net/ranides/assira/io/MultiOutputStream.java

@@ -14,7 +14,6 @@ import java.util.LinkedList;
 import java.util.List;
 import static net.ranides.assira.io.MultiOutputStream.tee;
 import net.ranides.assira.text.TextOutputStream;
-import net.ranides.assira.trace.MultiException;
 
 /**
  * Strumień zapisujący dane do więcej niż jednego celu jednocześnie.
@@ -117,14 +116,9 @@ public class MultiOutputStream extends OutputStream {
     }
     
     private static void throwErrors(List<IOException> list) throws IOException {
-        if(list == null) {
-            return;
+        if(list != null) {
+            throw new IOExceptions("exception caused by one or more slave stream", list);
         }
-        MultiException exception = new MultiException("exception caused by one or more slave stream");
-        for(IOException cause : list) {
-            exception.addCause(cause);
-        }
-        throw exception;
     }
     
     

+ 33 - 0
src/main/java/net/ranides/assira/trace/ExceptionInspector.java

@@ -7,6 +7,7 @@
 
 package net.ranides.assira.trace;
 
+import java.io.PrintStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.util.ArrayList;
@@ -90,5 +91,37 @@ public final class ExceptionInspector {
         error.initCause(cause);
         return error;
     }
+	
+	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);
+		}
+    }
 
 }

+ 0 - 102
src/main/java/net/ranides/assira/trace/MultiException.java

@@ -1,102 +0,0 @@
-/*
- * @author Ranides Atterwim <ranides@gmail.com>
- * @copyright Ranides Atterwim
- * @license WTFPL
- * @url http://ranides.net/projects/assira
- */
-package net.ranides.assira.trace;
-
-import java.io.PrintStream;
-import java.io.PrintWriter;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-
-/**
- *
- * @author ranides
- */
-public class MultiException extends RuntimeException implements Iterable<Throwable> {
-
-    private List<Throwable> causeList;
-    
-    public MultiException(Collection<? extends Throwable> causeList) {
-        super();
-        this.causeList = new LinkedList<>(causeList);
-    }
-
-    public <T extends Throwable> MultiException(String message, Collection<? extends Throwable> causeList) {
-        super(message);
-        this.causeList = new LinkedList<>(causeList);
-    }
-
-    public MultiException(String message) {
-        super(message);
-        this.causeList = null;
-    }
-    
-    @Override
-    public Iterator<Throwable> iterator() {
-        if( causeList == null) {
-            return Collections.<Throwable>emptyList().iterator();
-        } else {
-            return causeList.iterator();
-        }
-    }
-    
-    public List<Throwable> getCauseList() {
-        return causeList;
-    }
-    
-    public Throwable getCause(int index) {
-        if(null != causeList && index>=0 && index < causeList.size()) {
-            return causeList.get(index);
-        } else {
-            return null;
-        }
-    }
-    
-    public void addCause(Throwable cause) {
-        if(null == causeList) {
-            causeList = new LinkedList<>();
-        }
-        causeList.add(cause);
-    }
-
-    @Override
-    public void printStackTrace(PrintStream stream) {
-        super.printStackTrace(stream);
-        if(null != causeList) {
-            stream.println();
-            stream.println("CauseList: ");
-            for(int i=0, n=causeList.size(); i<n; i++) {
-                stream.println("\t[" + i + "] " + causeList.get(i));
-            }
-            stream.println();
-            for(int i=0, n=causeList.size(); i<n; i++) {
-                stream.print("CauseList details["+i+"]: ");
-                causeList.get(i).printStackTrace(stream);
-            }
-        }
-    }
-
-    @Override
-    public void printStackTrace(PrintWriter writer) {
-        super.printStackTrace(writer);
-        if(null != causeList) {
-            writer.println();
-            writer.println("CauseList: ");
-            for(int i=0, n=causeList.size(); i<n; i++) {
-                writer.println("\t[" + i + "] " + causeList.get(i));
-            }
-            writer.println();
-            for(int i=0, n=causeList.size(); i<n; i++) {
-                writer.println("Caused by["+i+"]:");
-                causeList.get(i).printStackTrace(writer);
-            }
-        }
-    }
-    
-}

+ 65 - 0
src/main/java/net/ranides/assira/trace/RuntimeExceptions.java

@@ -0,0 +1,65 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.trace;
+
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ *
+ * @author ranides
+ */
+public class RuntimeExceptions extends RuntimeException implements Iterable<Throwable> {
+
+    private List<Throwable> causeList;
+    
+    public RuntimeExceptions(Collection<? extends Throwable> causeList) {
+        super();
+        this.causeList = new LinkedList<>(causeList);
+    }
+
+    public <T extends Throwable> RuntimeExceptions(String message, Collection<? extends Throwable> causeList) {
+        super(message);
+        this.causeList = new LinkedList<>(causeList);
+    }
+
+    public RuntimeExceptions(String message) {
+        super(message);
+        this.causeList = new LinkedList<>();
+    }
+    
+    @Override
+    public Iterator<Throwable> iterator() {
+		return causeList.iterator();
+    }
+    
+    public List<Throwable> getCauseList() {
+        return causeList;
+    }
+    
+    public void addCause(Throwable cause) {
+        causeList.add(cause);
+    }
+
+    @Override
+    public void printStackTrace(PrintStream stream) {
+        super.printStackTrace(stream);
+		ExceptionInspector.print(stream, causeList);
+    }
+
+    @Override
+    public void printStackTrace(PrintWriter writer) {
+        super.printStackTrace(writer);
+		ExceptionInspector.print(writer, causeList);
+    }
+    
+}

+ 1 - 0
src/test/java/net/ranides/assira/io/AsyncStreamTest.java

@@ -191,6 +191,7 @@ public class AsyncStreamTest {
             fail("IOException expected");
         } catch(IOException ex) { assertTrue(true); }
         
+		TimeUtils.sleep(250);
         assertTrue(events1.eq(Arrays.asList(
             "IOEvent.Mark", 
             "IOEvent.ReadByte: 72", 

+ 90 - 0
src/test/java/net/ranides/assira/io/MultiOutputStreamTest.java

@@ -0,0 +1,90 @@
+/*
+ *  @author Ranides Atterwim <ranides@gmail.com>
+ *  @copyright Ranides Atterwim
+ *  @license WTFPL
+ *  @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.io;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import net.ranides.assira.text.TextEncoding;
+import net.ranides.assira.text.TextOutputStream;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author ranides
+ */
+public class MultiOutputStreamTest {
+	
+	@Test
+	public void testTee() throws IOException {
+		PrintStream prev = System.out;
+		
+		TextOutputStream stdout = new TextOutputStream();
+		System.setOut(new PrintStream(stdout));
+		
+		OutputStream stream = MultiOutputStream.tee();
+		System.out.print("Hello world.");
+		assertEquals("Hello world.", stdout.toString());
+		assertEquals("Hello world.", stream.toString());
+		
+		System.setOut(prev);
+	}
+
+	@Test
+	public void testStream() throws IOException {
+		OutputStream out1 = CheckedStream.wrap(new TextOutputStream());
+		OutputStream out2 = CheckedStream.wrap(new TextOutputStream());
+		OutputStream out3 = new TextOutputStream();
+		
+		MultiOutputStream ostream = new MultiOutputStream(out1, out2, out3);
+		
+		ostream.write("Hello".getBytes(TextEncoding.NIO_UTF8));
+		ostream.write(' ');
+		ostream.write(".world.".getBytes(TextEncoding.NIO_UTF8), 1, 5);
+		ostream.flush();
+		ostream.close();
+		
+		try {
+			ostream.flush();
+			fail("IOException expected");
+		} catch(IOException ex) {
+			assertEquals(2, ((IOExceptions)ex).getCauseList().size());
+		}
+		try {
+			ostream.close();
+			fail("IOException expected");
+		} catch(IOException ex) {
+			assertEquals(2, ((IOExceptions)ex).getCauseList().size());
+		}
+		try {
+			ostream.write('!');
+			fail("IOException expected");
+		} catch(IOException ex) {
+			assertEquals(2, ((IOExceptions)ex).getCauseList().size());
+		}
+		try {
+			ostream.write(" end".getBytes(TextEncoding.NIO_UTF8));
+			fail("IOException expected");
+		} catch(IOException ex) {
+			assertEquals(2, ((IOExceptions)ex).getCauseList().size());
+		}
+		try {
+			ostream.write("- :)-".getBytes(TextEncoding.NIO_UTF8),1,3);
+			fail("IOException expected");
+		} catch(IOException ex) {
+			assertEquals(2, ((IOExceptions)ex).getCauseList().size());
+		}
+		
+		System.out.println( out1.toString() );
+		System.out.println( out2.toString() );
+		System.out.println( out3.toString() );
+		
+		assertTrue(true);
+	}
+
+}

+ 3 - 3
src/test/java/net/ranides/assira/trace/MultiExceptionTest.java

@@ -15,15 +15,15 @@ import org.junit.Ignore;
  *
  * @author ranides
  */
-public class MultiExceptionTest {
+public class RuntimeExceptionsTest {
     
-    public MultiExceptionTest() {
+    public RuntimeExceptionsTest() {
     }
 
     @Ignore("verbose")
     @Test
     public void testStacktrace() {
-        MultiException mex = new MultiException("unknown error");
+        RuntimeExceptions mex = new RuntimeExceptions("unknown error");
         
         mex.addCause(new IOException(new RuntimeException("access denied")));