Ranides Atterwim 10 лет назад
Родитель
Сommit
f53eff2ff0
2 измененных файлов с 88 добавлено и 8 удалено
  1. 88 8
      assira/src/test/java/net/ranides/assira/io/IOUtilsTest.java
  2. BIN
      doc/coverage.ods

+ 88 - 8
assira/src/test/java/net/ranides/assira/io/IOUtilsTest.java

@@ -6,12 +6,15 @@
  */
 package net.ranides.assira.io;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
 import java.io.IOException;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Random;
 import java.util.stream.Collectors;
 import net.ranides.assira.events.Events;
 import net.ranides.assira.events.EventListener;
@@ -51,7 +54,7 @@ public class IOUtilsTest {
         assertEquals(Arrays.asList(7,8), out);
                 
         assertThrows(IOException.class, ()->{
-            IOUtils.close((Object)new CFile());
+            IOUtils.close((Object)new CFile("IO"));
         });
         assertThrows(SQLException.class, ()->{
             IOUtils.close((Object)new SFile());
@@ -68,24 +71,27 @@ public class IOUtilsTest {
         assertEquals(Arrays.asList(7), out);
                 
         assertThrows(IOException.class, ()->{
-            IOUtils.close(new CFile());
+            IOUtils.close(new CFile("IO"));
         });
     }
  
     @Test
     public void testClose_Consumer() {
-        List<String> errors = new ArrayList<>();
+        List<Throwable> errors = new ArrayList<>();
         
         EventListener<Events.Failure> listener = (e) -> {
-            errors.add(e.cause().toString());
+            errors.add(e.cause());
         };
         
         // listener
-        IOUtils.close(new CFile(), listener.consumer(e -> Events.failure(e)));
+        IOUtils.close(new CFile("IO"), listener.consumer(e -> Events.failure(e)));
         
         // listener
         IOUtils.close(new SFile(), listener.consumer(Events::failure));
         
+        // consumer
+        IOUtils.close(new CFile("NC"), errors::add);
+        
         // real NOP - ignore exception silently
         IOUtils.close(new SFile(), e -> {});
         
@@ -95,7 +101,13 @@ public class IOUtilsTest {
         // explicit logger
         IOUtils.close(new SFile(), e -> LOGGER.error("close!", e));
         
-        assertEquals(Arrays.asList("java.io.IOException: IO", "java.sql.SQLException: SQL"), errors);
+        List<String> expected = Arrays.asList(
+                "java.io.IOException: IO", 
+                "java.sql.SQLException: SQL", 
+                "java.io.IOException: NC"
+        );
+        List<String> errstr = errors.stream().map(Object::toString).collect(Collectors.toList());
+        assertEquals(expected, errstr);
         
         assertTrue("WARN expected", LogObserver.match(m->
             m.text().equals("[main] WARN net.ranides.assira.io.IOUtils - Exception ignored on #close @ java.sql.SQLException: SQL")
@@ -105,16 +117,84 @@ public class IOUtilsTest {
         ));
     }
     
+    @Test
+    public void testCopy() throws IOException {
+        assertCopy(1);
+        assertCopy(7);
+        assertCopy(740);
+        assertCopy(4096 + 0);
+        assertCopy(4096 + 1);
+        assertCopy(4096 + 740);
+        assertCopy(7* 4096 + 0);
+        assertCopy(7* 4096 + 1);
+        assertCopy(7* 4096 + 740);
+    }
+    
+    @Test
+    public void testEquivalent() throws IOException {
+        byte[] data1 = random(7*4096+33, 777L);
+        byte[] data2 = random(7*4096+33, 777L);
+        byte[] data3 = random(7*4096+33, 778L);
+        byte[] data4 = random(7*4096+34, 777L);
+        assertTrue(IOUtils.equivalent(new ByteArrayInputStream(data1), new ByteArrayInputStream(data1)));
+        assertTrue(IOUtils.equivalent(new ByteArrayInputStream(data1), new ByteArrayInputStream(data2)));
+        assertFalse(IOUtils.equivalent(new ByteArrayInputStream(data1), new ByteArrayInputStream(data3)));
+        assertFalse(IOUtils.equivalent(new ByteArrayInputStream(data1), new ByteArrayInputStream(data4)));
+    }
+    
+    private static void assertCopy(int size) throws IOException {
+        byte[] data = random(size, 777L);
+        ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
+        
+        IOUtils.copy(new ByteArrayInputStream(data), bos1);
+        assertArrayEquals(data, bos1.toByteArray());
+        
+        ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
+        List<Long> info = new ArrayList<>();
+        IOUtils.copy(new ByteArrayInputStream(data), bos2, info::add);
+        assertEquals( (int)Math.ceil(size/4096.0), info.size());
+        assertEquals( size, info.get(info.size()-1).longValue() );
+    }
+    
+    
+    @Test
+    public void testWrap() {
+        assertEquals(IOException.class, IOUtils.wrap(new SQLException()).getClass());
+        IOException e = new IOException();
+        assertSame(e, IOUtils.wrap(e));
+    }
+    
+    @Test
+    public void testRethrow() {
+        assertThrows(IOException.class, ()->{
+            throw IOUtils.rethrow(new SQLException());
+        });
+        assertThrows(IOException.class, ()->{
+            throw IOUtils.rethrow(new IOException());
+        });
+    }
+    
+    private static byte[] random(int size, long seed) {
+        byte[] ret = new byte[size];
+        new Random(seed).nextBytes(ret);
+        return ret;
+    }
+    
     private static final class CFile implements Closeable {
+        
+        private final String msg;
+
+        public CFile(String msg) {
+            this.msg = msg;
+        }
 
         @Override
         public void close() throws IOException {
-            throw new IOException("IO");
+            throw new IOException(msg);
         }
     
     }
     
-    
     private static final class SFile implements AutoCloseable {
 
         @Override

BIN
doc/coverage.ods