|
@@ -21,25 +21,56 @@
|
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.nio.file.AccessDeniedException;
|
|
29
|
+import java.nio.file.AccessMode;
|
|
30
|
+import java.nio.file.FileSystem;
|
|
31
|
+import java.nio.file.Files;
|
|
32
|
+import java.nio.file.Path;
|
|
33
|
+import java.nio.file.spi.FileSystemProvider;
|
26
|
34
|
import java.util.HashMap;
|
27
|
35
|
import java.util.Map;
|
28
|
36
|
|
|
37
|
+import org.junit.After;
|
29
|
38
|
import org.junit.Before;
|
30
|
39
|
import org.junit.Test;
|
|
40
|
+import org.junit.runner.RunWith;
|
|
41
|
+import org.mockito.Mock;
|
|
42
|
+import org.mockito.runners.MockitoJUnitRunner;
|
31
|
43
|
|
32
|
44
|
import static org.junit.Assert.assertEquals;
|
33
|
45
|
import static org.junit.Assert.assertFalse;
|
34
|
46
|
import static org.junit.Assert.assertTrue;
|
|
47
|
+import static org.mockito.Mockito.doThrow;
|
|
48
|
+import static org.mockito.Mockito.when;
|
35
|
49
|
|
|
50
|
+@RunWith(MockitoJUnitRunner.class)
|
36
|
51
|
public class ConfigFileTest {
|
37
|
52
|
|
|
53
|
+ @Mock private Path ro;
|
|
54
|
+ @Mock private FileSystem mockedFileSystem;
|
|
55
|
+ @Mock private FileSystemProvider fileSystemProvider;
|
38
|
56
|
private ConfigFile cf;
|
|
57
|
+ private Path temp;
|
|
58
|
+ private FileSystem fileSystem;
|
39
|
59
|
|
40
|
60
|
@Before
|
41
|
61
|
public void setUp() throws Exception {
|
42
|
|
- cf = new ConfigFile(getClass().getResourceAsStream("test2.txt"));
|
|
62
|
+ fileSystem = Jimfs.newFileSystem(Configuration.unix());
|
|
63
|
+ Files.copy(getClass().getResourceAsStream("test2.txt"), fileSystem.getPath("/test2.txt"));
|
|
64
|
+ cf = new ConfigFile(fileSystem.getPath("/test2.txt"));
|
|
65
|
+ temp = fileSystem.getPath("/temp.txt");
|
|
66
|
+ when(mockedFileSystem.provider()).thenReturn(fileSystemProvider);
|
|
67
|
+ when(ro.getFileSystem()).thenReturn(mockedFileSystem);
|
|
68
|
+ doThrow(new AccessDeniedException("Nup.")).when(fileSystemProvider).checkAccess(ro, AccessMode.WRITE);
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ @After
|
|
72
|
+ public void tearDown() throws Exception {
|
|
73
|
+ fileSystem.close();
|
43
|
74
|
}
|
44
|
75
|
|
45
|
76
|
@Test
|
|
@@ -49,7 +80,7 @@ public class ConfigFileTest {
|
49
|
80
|
|
50
|
81
|
@Test(expected = UnsupportedOperationException.class)
|
51
|
82
|
public void testWrite() throws IOException {
|
52
|
|
- cf.write();
|
|
83
|
+ new ConfigFile(ro).write();
|
53
|
84
|
}
|
54
|
85
|
|
55
|
86
|
@Test
|
|
@@ -99,54 +130,48 @@ public class ConfigFileTest {
|
99
|
130
|
|
100
|
131
|
@Test
|
101
|
132
|
public void testColons() throws IOException, InvalidConfigFileException {
|
102
|
|
- final File file = File.createTempFile("DMDirc.unittest", null);
|
103
|
|
- file.deleteOnExit();
|
104
|
|
- ConfigFile config = new ConfigFile(file);
|
105
|
|
- Map<String, String> data = new HashMap<>();
|
|
133
|
+ final ConfigFile config = new ConfigFile(temp);
|
|
134
|
+ final Map<String, String> data = new HashMap<>();
|
106
|
135
|
data.put("test1", "hello");
|
107
|
136
|
data.put("test:2", "hello");
|
108
|
137
|
data.put("test3", "hello:");
|
109
|
138
|
config.addDomain("test", data);
|
110
|
139
|
config.write();
|
111
|
140
|
|
112
|
|
- config = new ConfigFile(file);
|
113
|
|
- config.read();
|
|
141
|
+ final ConfigFile config2 = new ConfigFile(temp);
|
|
142
|
+ config2.read();
|
114
|
143
|
|
115
|
|
- assertTrue(config.isKeyDomain("test"));
|
116
|
|
- data = config.getKeyDomain("test");
|
117
|
|
- assertEquals("hello", data.get("test1"));
|
118
|
|
- assertEquals("hello", data.get("test:2"));
|
119
|
|
- assertEquals("hello:", data.get("test3"));
|
|
144
|
+ assertTrue(config2.isKeyDomain("test"));
|
|
145
|
+ final Map<String, String> test = config2.getKeyDomain("test");
|
|
146
|
+ assertEquals("hello", test.get("test1"));
|
|
147
|
+ assertEquals("hello", test.get("test:2"));
|
|
148
|
+ assertEquals("hello:", test.get("test3"));
|
120
|
149
|
}
|
121
|
150
|
|
122
|
151
|
@Test
|
123
|
152
|
public void testEquals() throws IOException, InvalidConfigFileException {
|
124
|
|
- final File file = File.createTempFile("DMDirc.unittest", null);
|
125
|
|
- file.deleteOnExit();
|
126
|
|
- ConfigFile config = new ConfigFile(file);
|
127
|
|
- Map<String, String> data = new HashMap<>();
|
|
153
|
+ final ConfigFile config = new ConfigFile(temp);
|
|
154
|
+ final Map<String, String> data = new HashMap<>();
|
128
|
155
|
data.put("test1", "hello");
|
129
|
156
|
data.put("test=2", "hello");
|
130
|
157
|
data.put("test3", "hello=");
|
131
|
158
|
config.addDomain("test", data);
|
132
|
159
|
config.write();
|
133
|
160
|
|
134
|
|
- config = new ConfigFile(file);
|
135
|
|
- config.read();
|
|
161
|
+ final ConfigFile config2 = new ConfigFile(temp);
|
|
162
|
+ config2.read();
|
136
|
163
|
|
137
|
|
- assertTrue(config.isKeyDomain("test"));
|
138
|
|
- data = config.getKeyDomain("test");
|
139
|
|
- assertEquals("hello", data.get("test1"));
|
140
|
|
- assertEquals("hello", data.get("test=2"));
|
141
|
|
- assertEquals("hello=", data.get("test3"));
|
|
164
|
+ assertTrue(config2.isKeyDomain("test"));
|
|
165
|
+ final Map<String, String> test = config2.getKeyDomain("test");
|
|
166
|
+ assertEquals("hello", test.get("test1"));
|
|
167
|
+ assertEquals("hello", test.get("test=2"));
|
|
168
|
+ assertEquals("hello=", test.get("test3"));
|
142
|
169
|
}
|
143
|
170
|
|
144
|
171
|
@Test
|
145
|
172
|
public void testNewlines() throws IOException, InvalidConfigFileException {
|
146
|
|
- final File file = File.createTempFile("DMDirc.unittest", null);
|
147
|
|
- file.deleteOnExit();
|
148
|
|
- ConfigFile config = new ConfigFile(file);
|
149
|
|
- Map<String, String> data = new HashMap<>();
|
|
173
|
+ final ConfigFile config = new ConfigFile(temp);
|
|
174
|
+ final Map<String, String> data = new HashMap<>();
|
150
|
175
|
data.put("test1", "hello");
|
151
|
176
|
data.put("test2", "hello\ngoodbye");
|
152
|
177
|
data.put("test3", "hello\n");
|
|
@@ -154,59 +179,55 @@ public class ConfigFileTest {
|
154
|
179
|
config.addDomain("test", data);
|
155
|
180
|
config.write();
|
156
|
181
|
|
157
|
|
- config = new ConfigFile(file);
|
158
|
|
- config.read();
|
|
182
|
+ final ConfigFile config2 = new ConfigFile(temp);
|
|
183
|
+ config2.read();
|
159
|
184
|
|
160
|
|
- assertTrue(config.isKeyDomain("test"));
|
161
|
|
- data = config.getKeyDomain("test");
|
162
|
|
- assertEquals("hello", data.get("test1"));
|
163
|
|
- assertEquals("hello\ngoodbye", data.get("test2"));
|
164
|
|
- assertEquals("hello\n", data.get("test3"));
|
165
|
|
- assertEquals("hello\r\ngoodbye", data.get("test4"));
|
|
185
|
+ assertTrue(config2.isKeyDomain("test"));
|
|
186
|
+ final Map<String, String> test = config2.getKeyDomain("test");
|
|
187
|
+ assertEquals("hello", test.get("test1"));
|
|
188
|
+ assertEquals("hello\ngoodbye", test.get("test2"));
|
|
189
|
+ assertEquals("hello\n", test.get("test3"));
|
|
190
|
+ assertEquals("hello\r\ngoodbye", test.get("test4"));
|
166
|
191
|
}
|
167
|
192
|
|
168
|
193
|
@Test
|
169
|
194
|
public void testBackslash() throws IOException, InvalidConfigFileException {
|
170
|
|
- final File file = File.createTempFile("DMDirc.unittest", null);
|
171
|
|
- file.deleteOnExit();
|
172
|
|
- ConfigFile config = new ConfigFile(file);
|
173
|
|
- Map<String, String> data = new HashMap<>();
|
|
195
|
+ final ConfigFile config = new ConfigFile(temp);
|
|
196
|
+ final Map<String, String> data = new HashMap<>();
|
174
|
197
|
data.put("test1", "hello\\");
|
175
|
198
|
data.put("test2", "\\nhello");
|
176
|
199
|
data.put("test3\\", "hello");
|
177
|
200
|
config.addDomain("test", data);
|
178
|
201
|
config.write();
|
179
|
202
|
|
180
|
|
- config = new ConfigFile(file);
|
181
|
|
- config.read();
|
|
203
|
+ final ConfigFile config2 = new ConfigFile(temp);
|
|
204
|
+ config2.read();
|
182
|
205
|
|
183
|
|
- assertTrue(config.isKeyDomain("test"));
|
184
|
|
- data = config.getKeyDomain("test");
|
185
|
|
- assertEquals("hello\\", data.get("test1"));
|
186
|
|
- assertEquals("\\nhello", data.get("test2"));
|
187
|
|
- assertEquals("hello", data.get("test3\\"));
|
|
206
|
+ assertTrue(config2.isKeyDomain("test"));
|
|
207
|
+ final Map<String, String> test = config2.getKeyDomain("test");
|
|
208
|
+ assertEquals("hello\\", test.get("test1"));
|
|
209
|
+ assertEquals("\\nhello", test.get("test2"));
|
|
210
|
+ assertEquals("hello", test.get("test3\\"));
|
188
|
211
|
}
|
189
|
212
|
|
190
|
213
|
@Test
|
191
|
214
|
public void testHash() throws IOException, InvalidConfigFileException {
|
192
|
|
- final File file = File.createTempFile("DMDirc.unittest", null);
|
193
|
|
- file.deleteOnExit();
|
194
|
|
- ConfigFile config = new ConfigFile(file);
|
195
|
|
- Map<String, String> data = new HashMap<>();
|
|
215
|
+ final ConfigFile config = new ConfigFile(temp);
|
|
216
|
+ final Map<String, String> data = new HashMap<>();
|
196
|
217
|
data.put("test1#", "hello");
|
197
|
218
|
data.put("#test2", "hello");
|
198
|
219
|
data.put("test3", "#hello");
|
199
|
220
|
config.addDomain("test", data);
|
200
|
221
|
config.write();
|
201
|
222
|
|
202
|
|
- config = new ConfigFile(file);
|
203
|
|
- config.read();
|
|
223
|
+ final ConfigFile config2 = new ConfigFile(temp);
|
|
224
|
+ config2.read();
|
204
|
225
|
|
205
|
|
- assertTrue(config.isKeyDomain("test"));
|
206
|
|
- data = config.getKeyDomain("test");
|
207
|
|
- assertEquals("hello", data.get("test1#"));
|
208
|
|
- assertEquals("hello", data.get("#test2"));
|
209
|
|
- assertEquals("#hello", data.get("test3"));
|
|
226
|
+ assertTrue(config2.isKeyDomain("test"));
|
|
227
|
+ final Map<String, String> test = config2.getKeyDomain("test");
|
|
228
|
+ assertEquals("hello", test.get("test1#"));
|
|
229
|
+ assertEquals("hello", test.get("#test2"));
|
|
230
|
+ assertEquals("#hello", test.get("test3"));
|
210
|
231
|
}
|
211
|
232
|
|
212
|
233
|
@Test
|
|
@@ -224,13 +245,11 @@ public class ConfigFileTest {
|
224
|
245
|
|
225
|
246
|
@Test
|
226
|
247
|
public void testDelete() throws IOException {
|
227
|
|
- final File file = File.createTempFile("DMDirc_unittest", null);
|
228
|
|
- file.deleteOnExit();
|
229
|
|
- final ConfigFile config = new ConfigFile(file);
|
|
248
|
+ final ConfigFile config = new ConfigFile(temp);
|
230
|
249
|
config.write();
|
231
|
|
- assertTrue(file.exists());
|
|
250
|
+ assertTrue(Files.exists(temp));
|
232
|
251
|
config.delete();
|
233
|
|
- assertFalse(file.exists());
|
|
252
|
+ assertFalse(Files.exists(temp));
|
234
|
253
|
}
|
235
|
254
|
|
236
|
255
|
@Test
|