Sfoglia il codice sorgente

poprawki i testy FileHelper

Ranides Atterwim 13 anni fa
parent
commit
13880aeccb

+ 1 - 13
src/main/java/net/ranides/assira/annotations/PMD.java

@@ -24,19 +24,7 @@ public final class PMD {
     private PMD() {
         // utility class
     }
-    
-    /**
-     * Klasa do obsługi konsoli, z definicji używa strumieni.
-     * <p><b>Suppress:</b></p> 
-     * <ul>
-     *      <li>PMD.SystemPrintln</li>
-     * </ul>
-     */
-    @Retention(SOURCE)
-    @Target({TYPE,METHOD})
-    public @interface Console {
-    }
-    
+   
     /**
      * Klasa testowa, może mieć dużo danych pomieszanych z kodem
      * <p><b>Suppress:</b></p> 

+ 19 - 14
src/main/java/net/ranides/assira/io/FileHelper.java

@@ -7,6 +7,7 @@
 
 package net.ranides.assira.io;
 
+import edu.umd.cs.findbugs.annotations.SuppressWarnings;
 import java.io.*;
 import java.util.Arrays;
 import java.util.MissingResourceException;
@@ -127,7 +128,10 @@ public final class FileHelper {
      * @param input
      * @throws IOException
      */
-    @edu.umd.cs.findbugs.annotations.SuppressWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE")
+    @SuppressWarnings({
+        "OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE", 
+        "OBL_UNSATISFIED_OBLIGATION"
+    })
     public static void copy(File output, File input) throws IOException {
         copy(new FileOutputStream(output), new FileInputStream(input));
     }
@@ -140,7 +144,10 @@ public final class FileHelper {
      * @param input
      * @throws IOException
      */
-    @edu.umd.cs.findbugs.annotations.SuppressWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE")
+    @SuppressWarnings({
+        "OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE", 
+        "OBL_UNSATISFIED_OBLIGATION"
+    })
     public static void copy(String output, String input) throws IOException {
         copy(new FileOutputStream(output), new FileInputStream(input));
     }
@@ -227,21 +234,18 @@ public final class FileHelper {
     }
 
 
-    private static final class TempService {
-        private TempService() { }
+    private static final class TempService { // NOPMD
 
-        
-        
-        private static final File location =  new File(PrivilegedActions.getProperty("java.io.tmpdir"));
+        private static final File LOCATION =  new File(PrivilegedActions.getProperty("java.io.tmpdir"));
 
         public static String path() {
-            return location.getPath();
+            return LOCATION.getPath();
         }
         public static File location(String name) {
-            return new File(location.getPath() + File.separator + name);
+            return new File(LOCATION.getPath() + File.separator + name);
         }
         public static File location() {
-            return location;
+            return LOCATION;
         }
     }
 
@@ -259,12 +263,13 @@ public final class FileHelper {
     public static File createTempFile(String prefix, String suffix, Class<?> clazz) throws IOException {
         final String dir = PathHelper.getClassDir(clazz);
         final File file = TempService.location(dir);
-        if( file.mkdirs() ) {
-            return File.createTempFile(clazz.getSimpleName() + prefix, suffix, file);
-        } else {
+        if(!file.exists() && !file.mkdirs()) {
             throw new IOException("can't create directory: " + file);
-                    
         }
+        if(!file.isDirectory()) {
+            throw new IOException("path is not a directory: " + file);
+        }
+        return File.createTempFile(clazz.getSimpleName() + prefix, suffix, file);
     }
 
     /**

+ 60 - 0
src/test/java/net/ranides/assira/io/FileHelperTest.java

@@ -0,0 +1,60 @@
+/*
+ * @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.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Random;
+import net.ranides.assira.math.LFSR;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author ranides
+ */
+public class FileHelperTest {
+    
+    public FileHelperTest() {
+    }
+
+    @Test
+    public void testFileInfo() throws IOException {
+        File file1 = FileHelper.createTempFile("io", FileHelperTest.class);
+        File file2 = FileHelper.createTempFile("io", "copy", FileHelperTest.class);
+        
+        Writer writer = null; 
+        BufferedReader reader = null;
+        
+        Random gen1 = new LFSR(0x45454545);
+        Random gen2 = new LFSR(0x45454545);
+        
+        try {
+            writer = LocalWriter.toFile(file1);
+            for(int i=0; i<10; i++) {
+                String line = Long.toString(Math.abs(gen1.nextLong()), 7);
+                writer.append(line).append('\n');
+            }
+        } finally {
+            FileHelper.close(writer);
+        }
+        
+        FileHelper.copy(file2, file1);
+        
+        try {
+            reader = new BufferedReader(LocalReader.fromFile(file2));
+            for(int i=0; i<10; i++) {
+                long line = Long.parseLong(reader.readLine(), 7);
+                assertEquals(Math.abs(gen2.nextLong()), line);
+            }
+        } finally {
+            FileHelper.close(reader);
+        }
+    }
+}