Selaa lähdekoodia

Tidy up TextFile a bit.

pull/23/head
Greg Holmes 9 vuotta sitten
vanhempi
commit
20608b13fc
2 muutettua tiedostoa jossa 56 lisäystä ja 71 poistoa
  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 Näytä tiedosto

55
      */
55
      */
56
     private List<String> lines;
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
      * Creates a new instance of TextFile for the specified File, which is to be
59
      * Creates a new instance of TextFile for the specified File, which is to be
100
      * read using the specified charset.
60
      * read using the specified charset.
187
      * @throws IOException if an I/O exception occurs
147
      * @throws IOException if an I/O exception occurs
188
      */
148
      */
189
     public void writeLines(final Iterable<String> lines) throws IOException {
149
     public void writeLines(final Iterable<String> lines) throws IOException {
190
-        if (path == null) {
150
+        if (!isWritable()) {
191
             throw new UnsupportedOperationException("Cannot write to TextFile "
151
             throw new UnsupportedOperationException("Cannot write to TextFile "
192
                     + "opened with an InputStream");
152
                     + "opened with an InputStream");
193
         }
153
         }
201
      * @throws IOException if the file is unable to be deleted
161
      * @throws IOException if the file is unable to be deleted
202
      */
162
      */
203
     public void delete() throws IOException {
163
     public void delete() throws IOException {
204
-        if (path == null) {
164
+        if (!isWritable()) {
205
             throw new UnsupportedOperationException("Cannot delete TextFile "
165
             throw new UnsupportedOperationException("Cannot delete TextFile "
206
                     + "opened with an InputStream");
166
                     + "opened with an InputStream");
207
         }
167
         }

+ 54
- 29
test/com/dmdirc/util/io/TextFileTest.java Näytä tiedosto

21
  */
21
  */
22
 package com.dmdirc.util.io;
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
 import java.io.IOException;
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
 import java.util.Arrays;
36
 import java.util.Arrays;
27
 import java.util.List;
37
 import java.util.List;
28
 
38
 
39
+import org.junit.After;
40
+import org.junit.Before;
29
 import org.junit.Test;
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
 import static org.junit.Assert.assertEquals;
46
 import static org.junit.Assert.assertEquals;
32
 import static org.junit.Assert.assertFalse;
47
 import static org.junit.Assert.assertFalse;
33
 import static org.junit.Assert.assertTrue;
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
 public class TextFileTest {
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
     @Test
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
         final List<String> lines = file.getLines();
81
         final List<String> lines = file.getLines();
42
 
82
 
43
         assertEquals(7, lines.size());
83
         assertEquals(7, lines.size());
45
     }
85
     }
46
 
86
 
47
     @Test
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
         final List<String> lines = file.getLines();
90
         final List<String> lines = file.getLines();
52
 
91
 
53
         assertEquals(7, lines.size());
92
         assertEquals(7, lines.size());
56
 
95
 
57
     @Test
96
     @Test
58
     public void testWrite() throws IOException {
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
         final List<String> lines = Arrays.asList("hello", "this is a test", "meep");
99
         final List<String> lines = Arrays.asList("hello", "this is a test", "meep");
63
-
64
         file.writeLines(lines);
100
         file.writeLines(lines);
65
-
66
-        file = new TextFile(tempFile);
67
         final List<String> newLines = file.getLines();
101
         final List<String> newLines = file.getLines();
68
-
69
         assertEquals(lines, newLines);
102
         assertEquals(lines, newLines);
70
-        tempFile.deleteOnExit();
71
     }
103
     }
72
 
104
 
73
     @Test(expected = UnsupportedOperationException.class)
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
         file.writeLines(Arrays.asList("hello", "this is a test", "meep"));
108
         file.writeLines(Arrays.asList("hello", "this is a test", "meep"));
78
     }
109
     }
79
 
110
 
80
     @Test(expected = UnsupportedOperationException.class)
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
         file.delete();
114
         file.delete();
85
     }
115
     }
86
 
116
 
87
     @Test
117
     @Test
88
     public void testDelete() throws IOException {
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
         final List<String> lines = Arrays.asList("hello", "this is a test", "meep");
120
         final List<String> lines = Arrays.asList("hello", "this is a test", "meep");
93
-
94
         file.writeLines(lines);
121
         file.writeLines(lines);
95
-
96
-        assertTrue(tempFile.exists());
97
-        file = new TextFile(tempFile);
122
+        assertTrue(Files.exists(temp));
98
         file.delete();
123
         file.delete();
99
-        assertFalse(tempFile.exists());
124
+        assertFalse(Files.exists(temp));
100
     }
125
     }
101
 
126
 
102
 }
127
 }

Loading…
Peruuta
Tallenna