Bläddra i källkod

Merge pull request #25 from greboid/test

Remove unused constructors in ConfigFile.
pull/27/head
Chris Smith 9 år sedan
förälder
incheckning
786995097e
2 ändrade filer med 83 tillägg och 83 borttagningar
  1. 0
    19
      src/com/dmdirc/util/io/ConfigFile.java
  2. 83
    64
      test/com/dmdirc/util/io/ConfigFileTest.java

+ 0
- 19
src/com/dmdirc/util/io/ConfigFile.java Visa fil

24
 
24
 
25
 import com.dmdirc.util.collections.MapList;
25
 import com.dmdirc.util.collections.MapList;
26
 
26
 
27
-import java.io.File;
28
 import java.io.IOException;
27
 import java.io.IOException;
29
 import java.io.InputStream;
28
 import java.io.InputStream;
30
 import java.nio.charset.Charset;
29
 import java.nio.charset.Charset;
64
         super(is, Charset.forName("UTF-8"));
63
         super(is, Charset.forName("UTF-8"));
65
     }
64
     }
66
 
65
 
67
-    /**
68
-     * Creates a new Config File from the specified file.
69
-     *
70
-     * @param file The file to read/write
71
-     */
72
-    public ConfigFile(final File file) {
73
-        super(file, Charset.forName("UTF-8"));
74
-    }
75
-
76
-    /**
77
-     * Creates a new Config File from the specified file.
78
-     *
79
-     * @param filename The name of the file to read/write
80
-     */
81
-    public ConfigFile(final String filename) {
82
-        this(new File(filename));
83
-    }
84
-
85
     /**
66
     /**
86
      * Creates a new config file from the specified path.
67
      * Creates a new config file from the specified path.
87
      *
68
      *

+ 83
- 64
test/com/dmdirc/util/io/ConfigFileTest.java Visa fil

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.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
 import java.util.HashMap;
34
 import java.util.HashMap;
27
 import java.util.Map;
35
 import java.util.Map;
28
 
36
 
37
+import org.junit.After;
29
 import org.junit.Before;
38
 import org.junit.Before;
30
 import org.junit.Test;
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
 import static org.junit.Assert.assertEquals;
44
 import static org.junit.Assert.assertEquals;
33
 import static org.junit.Assert.assertFalse;
45
 import static org.junit.Assert.assertFalse;
34
 import static org.junit.Assert.assertTrue;
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
 public class ConfigFileTest {
51
 public class ConfigFileTest {
37
 
52
 
53
+    @Mock private Path ro;
54
+    @Mock private FileSystem mockedFileSystem;
55
+    @Mock private FileSystemProvider fileSystemProvider;
38
     private ConfigFile cf;
56
     private ConfigFile cf;
57
+    private Path temp;
58
+    private FileSystem fileSystem;
39
 
59
 
40
     @Before
60
     @Before
41
     public void setUp() throws Exception {
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
     @Test
76
     @Test
49
 
80
 
50
     @Test(expected = UnsupportedOperationException.class)
81
     @Test(expected = UnsupportedOperationException.class)
51
     public void testWrite() throws IOException {
82
     public void testWrite() throws IOException {
52
-        cf.write();
83
+        new ConfigFile(ro).write();
53
     }
84
     }
54
 
85
 
55
     @Test
86
     @Test
99
 
130
 
100
     @Test
131
     @Test
101
     public void testColons() throws IOException, InvalidConfigFileException {
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
         data.put("test1", "hello");
135
         data.put("test1", "hello");
107
         data.put("test:2", "hello");
136
         data.put("test:2", "hello");
108
         data.put("test3", "hello:");
137
         data.put("test3", "hello:");
109
         config.addDomain("test", data);
138
         config.addDomain("test", data);
110
         config.write();
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
     @Test
151
     @Test
123
     public void testEquals() throws IOException, InvalidConfigFileException {
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
         data.put("test1", "hello");
155
         data.put("test1", "hello");
129
         data.put("test=2", "hello");
156
         data.put("test=2", "hello");
130
         data.put("test3", "hello=");
157
         data.put("test3", "hello=");
131
         config.addDomain("test", data);
158
         config.addDomain("test", data);
132
         config.write();
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
     @Test
171
     @Test
145
     public void testNewlines() throws IOException, InvalidConfigFileException {
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
         data.put("test1", "hello");
175
         data.put("test1", "hello");
151
         data.put("test2", "hello\ngoodbye");
176
         data.put("test2", "hello\ngoodbye");
152
         data.put("test3", "hello\n");
177
         data.put("test3", "hello\n");
154
         config.addDomain("test", data);
179
         config.addDomain("test", data);
155
         config.write();
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
     @Test
193
     @Test
169
     public void testBackslash() throws IOException, InvalidConfigFileException {
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
         data.put("test1", "hello\\");
197
         data.put("test1", "hello\\");
175
         data.put("test2", "\\nhello");
198
         data.put("test2", "\\nhello");
176
         data.put("test3\\", "hello");
199
         data.put("test3\\", "hello");
177
         config.addDomain("test", data);
200
         config.addDomain("test", data);
178
         config.write();
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
     @Test
213
     @Test
191
     public void testHash() throws IOException, InvalidConfigFileException {
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
         data.put("test1#", "hello");
217
         data.put("test1#", "hello");
197
         data.put("#test2", "hello");
218
         data.put("#test2", "hello");
198
         data.put("test3", "#hello");
219
         data.put("test3", "#hello");
199
         config.addDomain("test", data);
220
         config.addDomain("test", data);
200
         config.write();
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
     @Test
233
     @Test
224
 
245
 
225
     @Test
246
     @Test
226
     public void testDelete() throws IOException {
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
         config.write();
249
         config.write();
231
-        assertTrue(file.exists());
250
+        assertTrue(Files.exists(temp));
232
         config.delete();
251
         config.delete();
233
-        assertFalse(file.exists());
252
+        assertFalse(Files.exists(temp));
234
     }
253
     }
235
 
254
 
236
     @Test
255
     @Test

Laddar…
Avbryt
Spara