Ranides Atterwim преди 11 години
родител
ревизия
2e4e483be5
променени са 2 файла, в които са добавени 93 реда и са изтрити 10 реда
  1. 21 9
      src/main/java/net/ranides/assira/io/FileHelper.java
  2. 72 1
      src/test/java/net/ranides/assira/io/FileHelperTest.java

+ 21 - 9
src/main/java/net/ranides/assira/io/FileHelper.java

@@ -213,6 +213,19 @@ public final class FileHelper {
     public static boolean equals(InputStream left, InputStream right) throws IOException {
         return equals(left, right, BUFFER_SIZE);
     }
+    
+    public static boolean equals(File left, File right) throws IOException {
+        InputStream istream1 = null;
+        InputStream istream2 = null;
+        try {
+            istream1 = new FileInputStream(left);
+            istream2 = new FileInputStream(right);
+            return equals(istream1, istream2);
+        } finally {
+            close(istream1);
+            close(istream2);
+        }
+    }
 
     /**
      * Wypakowuje z pliku JAR zasób o podanej nazwie do pliku zewnętrznego o
@@ -224,18 +237,17 @@ public final class FileHelper {
      * @throws IOException
      */
     public static void extractResource(Class<?> clazz, String resource, File path) throws IOException {
-        InputStream istream = clazz.getResourceAsStream(resource);
-        if(null==istream) {
-            throw new MissingResourceException("", clazz.getName(), resource);
-        }
+        InputStream istream = null;
+        OutputStream ostream = null;
         try {
-            OutputStream ostream = new BufferedOutputStream(new FileOutputStream(path));
-            try {
-                copy(ostream, istream);
-            } finally {
-                ostream.close();
+            istream = clazz.getResourceAsStream(resource);
+            if(null==istream) {
+                throw new MissingResourceException("", clazz.getName(), resource);
             }
+            ostream = new BufferedOutputStream(new FileOutputStream(path));
+            copy(ostream, istream);
         } finally {
+            close(ostream);
             close(istream);
         }
     }

+ 72 - 1
src/test/java/net/ranides/assira/io/FileHelperTest.java

@@ -6,12 +6,20 @@
  */
 package net.ranides.assira.io;
 
+import java.io.BufferedOutputStream;
 import java.io.BufferedReader;
+import java.io.DataOutputStream;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.Writer;
+import java.util.MissingResourceException;
 import java.util.Random;
 import net.ranides.assira.math.LFSR;
+import net.ranides.assira.text.StringLoader;
+import net.ranides.assira.text.Strings;
 import org.junit.Test;
 import static org.junit.Assert.*;
 
@@ -21,7 +29,70 @@ import static org.junit.Assert.*;
  */
 public class FileHelperTest {
     
-    public FileHelperTest() {
+    @Test
+    public void testExtractResource() throws IOException {
+        File file1 = FileHelper.createTempFile("resource", FileHelperTest.class);
+        FileHelper.extractResource("/native-types.txt", file1);
+        String expected = Strings.raw(
+/*Native types (8)
+-------------------
+byte
+short
+int
+long
+float
+double
+char
+boolean
+
+Boxed types (8)
+-------------------
+Byte
+Short
+Integer
+Long
+Float
+Double
+Character
+Boolean*/);
+        assertEquals(expected, StringLoader.fromFile(file1)); 
+        
+        try {
+            FileHelper.extractResource("/not-found.resource", file1);
+            fail("MissingResourceException expected");
+        } catch(MissingResourceException ex) {
+            assertTrue(true);
+        }
+    }
+    
+    @Test
+    public void testCopyEquals() throws IOException {
+        File file1 = FileHelper.createTempFile("equals", "", FileHelperTest.class);
+        File file2 = FileHelper.createTempFile("equals", "", FileHelperTest.class);
+        File file3 = FileHelper.createTempFile("equals", "", FileHelperTest.class);
+        File file4 = FileHelper.createTempFile("equals", "", FileHelperTest.class);
+        File file5 = FileHelper.createTempFile("equals", "", FileHelperTest.class);
+        
+        DataOutputStream out1 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file1)));
+        DataOutputStream out4 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file4)));
+        DataOutputStream out5 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file5)));
+        
+        Random gen1 = new LFSR(0x45454545);
+        for(int i=0; i<256*1024; i++) {
+            out1.writeInt(gen1.nextInt());
+            out4.writeInt(gen1.nextInt());
+            out5.writeLong(gen1.nextLong());
+        }
+        FileHelper.close(out1);
+        FileHelper.close(out4);
+        
+        FileHelper.copy(file2, file1);
+        FileHelper.copy(file3.toString(), file1.toString());
+        
+        assertTrue(FileHelper.equals(file1, file2));
+        assertTrue(FileHelper.equals(file1, file3));
+        assertFalse(FileHelper.equals(file1, file4));
+        assertFalse(FileHelper.equals(file1, file5));
     }
 
     @Test