소스 검색

Tidy up TextFile a bit.

pull/23/head
Greg Holmes 9 년 전
부모
커밋
20608b13fc
2개의 변경된 파일56개의 추가작업 그리고 71개의 파일을 삭제
  1. 2
    42
      src/com/dmdirc/util/io/TextFile.java
  2. 54
    29
      test/com/dmdirc/util/io/TextFileTest.java

+ 2
- 42
src/com/dmdirc/util/io/TextFile.java 파일 보기

@@ -55,46 +55,6 @@ public class TextFile {
55 55
      */
56 56
     private List<String> lines;
57 57
 
58
-    /**
59
-     * Creates a new instance of TextFile for the specified file, and uses the
60
-     * default charset.
61
-     *
62
-     * @param filename The file to be read/written
63
-     */
64
-    public TextFile(final String filename) {
65
-        this(new File(filename));
66
-    }
67
-
68
-    /**
69
-     * Creates a new instance of TextFile for the specified File, and uses the
70
-     * default charset.
71
-     *
72
-     * @param file The file to read
73
-     */
74
-    public TextFile(final File file) {
75
-        this(file, Charset.defaultCharset());
76
-    }
77
-
78
-    /**
79
-     * Creates a new instance of TextFile for the specified Path, and uses the
80
-     * default charset.
81
-     *
82
-     * @param path The path to read
83
-     */
84
-    public TextFile(final Path path) {
85
-        this(path, Charset.defaultCharset());
86
-    }
87
-
88
-    /**
89
-     * Creates a new instance of TextFile for an input stream, and uses the
90
-     * default charset.
91
-     *
92
-     * @param is The input stream to read from
93
-     */
94
-    public TextFile(final InputStream is) {
95
-        this(is, Charset.defaultCharset());
96
-    }
97
-
98 58
     /**
99 59
      * Creates a new instance of TextFile for the specified File, which is to be
100 60
      * read using the specified charset.
@@ -187,7 +147,7 @@ public class TextFile {
187 147
      * @throws IOException if an I/O exception occurs
188 148
      */
189 149
     public void writeLines(final Iterable<String> lines) throws IOException {
190
-        if (path == null) {
150
+        if (!isWritable()) {
191 151
             throw new UnsupportedOperationException("Cannot write to TextFile "
192 152
                     + "opened with an InputStream");
193 153
         }
@@ -201,7 +161,7 @@ public class TextFile {
201 161
      * @throws IOException if the file is unable to be deleted
202 162
      */
203 163
     public void delete() throws IOException {
204
-        if (path == null) {
164
+        if (!isWritable()) {
205 165
             throw new UnsupportedOperationException("Cannot delete TextFile "
206 166
                     + "opened with an InputStream");
207 167
         }

+ 54
- 29
test/com/dmdirc/util/io/TextFileTest.java 파일 보기

@@ -21,23 +21,63 @@
21 21
  */
22 22
 package com.dmdirc.util.io;
23 23
 
24
-import java.io.File;
24
+import com.google.common.jimfs.Configuration;
25
+import com.google.common.jimfs.Jimfs;
26
+
25 27
 import java.io.IOException;
28
+import java.net.URISyntaxException;
29
+import java.nio.charset.Charset;
30
+import java.nio.file.AccessDeniedException;
31
+import java.nio.file.AccessMode;
32
+import java.nio.file.FileSystem;
33
+import java.nio.file.Files;
34
+import java.nio.file.Path;
35
+import java.nio.file.spi.FileSystemProvider;
26 36
 import java.util.Arrays;
27 37
 import java.util.List;
28 38
 
39
+import org.junit.After;
40
+import org.junit.Before;
29 41
 import org.junit.Test;
42
+import org.junit.runner.RunWith;
43
+import org.mockito.Mock;
44
+import org.mockito.runners.MockitoJUnitRunner;
30 45
 
31 46
 import static org.junit.Assert.assertEquals;
32 47
 import static org.junit.Assert.assertFalse;
33 48
 import static org.junit.Assert.assertTrue;
49
+import static org.mockito.Mockito.doThrow;
50
+import static org.mockito.Mockito.when;
34 51
 
52
+@RunWith(MockitoJUnitRunner.class)
35 53
 public class TextFileTest {
36 54
 
55
+    @Mock private Path ro;
56
+    @Mock private FileSystem mockedFileSystem;
57
+    @Mock private FileSystemProvider fileSystemProvider;
58
+    private Path test1;
59
+    private Path temp;
60
+    private FileSystem fileSystem;
61
+
62
+    @Before
63
+    public void setup() throws IOException {
64
+        fileSystem = Jimfs.newFileSystem(Configuration.unix());
65
+        Files.copy(getClass().getResourceAsStream("test1.txt"), fileSystem.getPath("/test1.txt"));
66
+        test1 = fileSystem.getPath("/test1.txt");
67
+        temp = fileSystem.getPath("/temp.txt");
68
+        when(mockedFileSystem.provider()).thenReturn(fileSystemProvider);
69
+        when(ro.getFileSystem()).thenReturn(mockedFileSystem);
70
+        doThrow(new AccessDeniedException("Nup.")).when(fileSystemProvider).checkAccess(ro, AccessMode.WRITE);
71
+    }
72
+
73
+    @After
74
+    public void tearDown() throws IOException {
75
+        fileSystem.close();
76
+    }
77
+
37 78
     @Test
38
-    public void testGetLines() throws IOException {
39
-        final TextFile file
40
-                = new TextFile(getClass().getResourceAsStream("test1.txt"));
79
+    public void testGetLines() throws IOException, URISyntaxException {
80
+        final TextFile file = new TextFile(test1, Charset.forName("UTF-8"));
41 81
         final List<String> lines = file.getLines();
42 82
 
43 83
         assertEquals(7, lines.size());
@@ -45,9 +85,8 @@ public class TextFileTest {
45 85
     }
46 86
 
47 87
     @Test
48
-    public void testGetLines2() throws IOException {
49
-        final TextFile file
50
-                = new TextFile(getClass().getResourceAsStream("test1.txt"));
88
+    public void testGetLines2() throws IOException, URISyntaxException {
89
+        final TextFile file = new TextFile(test1, Charset.forName("UTF-8"));
51 90
         final List<String> lines = file.getLines();
52 91
 
53 92
         assertEquals(7, lines.size());
@@ -56,47 +95,33 @@ public class TextFileTest {
56 95
 
57 96
     @Test
58 97
     public void testWrite() throws IOException {
59
-        final File tempFile = File.createTempFile("dmdirc_unit_test", null);
60
-        TextFile file = new TextFile(tempFile);
61
-
98
+        final TextFile file = new TextFile(temp, Charset.forName("UTF-8"));
62 99
         final List<String> lines = Arrays.asList("hello", "this is a test", "meep");
63
-
64 100
         file.writeLines(lines);
65
-
66
-        file = new TextFile(tempFile);
67 101
         final List<String> newLines = file.getLines();
68
-
69 102
         assertEquals(lines, newLines);
70
-        tempFile.deleteOnExit();
71 103
     }
72 104
 
73 105
     @Test(expected = UnsupportedOperationException.class)
74
-    public void testIllegalWrite() throws IOException {
75
-        final TextFile file
76
-                = new TextFile(getClass().getResourceAsStream("test1.txt"));
106
+    public void testIllegalWrite() throws IOException, URISyntaxException {
107
+        final TextFile file = new TextFile(ro, Charset.forName("UTF-8"));
77 108
         file.writeLines(Arrays.asList("hello", "this is a test", "meep"));
78 109
     }
79 110
 
80 111
     @Test(expected = UnsupportedOperationException.class)
81
-    public void testIllegalDelete() throws IOException {
82
-        final TextFile file
83
-                = new TextFile(getClass().getResourceAsStream("test1.txt"));
112
+    public void testIllegalDelete() throws IOException, URISyntaxException {
113
+        final TextFile file = new TextFile(ro, Charset.forName("UTF-8"));
84 114
         file.delete();
85 115
     }
86 116
 
87 117
     @Test
88 118
     public void testDelete() throws IOException {
89
-        final File tempFile = File.createTempFile("dmdirc_unit_test", "de;ete");
90
-        TextFile file = new TextFile(tempFile);
91
-
119
+        final TextFile file = new TextFile(temp, Charset.forName("UTF-8"));
92 120
         final List<String> lines = Arrays.asList("hello", "this is a test", "meep");
93
-
94 121
         file.writeLines(lines);
95
-
96
-        assertTrue(tempFile.exists());
97
-        file = new TextFile(tempFile);
122
+        assertTrue(Files.exists(temp));
98 123
         file.delete();
99
-        assertFalse(tempFile.exists());
124
+        assertFalse(Files.exists(temp));
100 125
     }
101 126
 
102 127
 }

Loading…
취소
저장