|
@@ -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
|
}
|