Browse Source

Implement ConfigFile.delete and TextFile.delete, and tests for them both

Added unit test for TextFile.writeLines
Fixes issue 1115, work on issue 1106

git-svn-id: http://svn.dmdirc.com/trunk@3853 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6
Chris Smith 16 years ago
parent
commit
0fa4e23c8f

+ 1
- 1
src/com/dmdirc/util/ConfigFile.java View File

@@ -200,7 +200,7 @@ public class ConfigFile {
200 200
      * Deletes this config file.
201 201
      */
202 202
     public void delete() {
203
-        throw new UnsupportedOperationException("Not implemented yet");
203
+        file.delete();
204 204
     }
205 205
 
206 206
     /**

+ 8
- 1
src/com/dmdirc/util/TextFile.java View File

@@ -138,6 +138,13 @@ public class TextFile {
138 138
         return file;
139 139
     }
140 140
     
141
-    
141
+    /**
142
+     * Deletes the file associated with this textfile, if there is one.
143
+     */
144
+    public void delete() {
145
+        if (file != null) {
146
+            file.delete();
147
+        }
148
+    }
142 149
 
143 150
 }

+ 11
- 1
test/com/dmdirc/util/ConfigFileTest.java View File

@@ -222,6 +222,16 @@ public class ConfigFileTest extends junit.framework.TestCase {
222 222
     public void testUnescape() {
223 223
         final String input = "blah blah\\foo\r\nbar=:";
224 224
         assertEquals(input, ConfigFile.unescape(ConfigFile.escape(input)));
225
-    }    
225
+    }
226
+    
227
+    @Test
228
+    public void testDelete() throws IOException {
229
+        final File file = File.createTempFile("DMDirc_unittest", null);
230
+        ConfigFile config = new ConfigFile(new TextFile(file));
231
+        config.write();
232
+        assertTrue(file.exists());
233
+        config.delete();
234
+        assertFalse(file.exists());
235
+    }
226 236
 
227 237
 }

+ 46
- 31
test/com/dmdirc/util/TextFileTest.java View File

@@ -22,50 +22,65 @@
22 22
 
23 23
 package com.dmdirc.util;
24 24
 
25
+import java.io.File;
25 26
 import java.io.FileNotFoundException;
26 27
 import java.io.IOException;
27 28
 import java.net.URISyntaxException;
29
+import java.util.Arrays;
28 30
 import java.util.List;
29 31
 
30 32
 import org.junit.Test;
31 33
 import static org.junit.Assert.*;
32 34
 
33 35
 public class TextFileTest extends junit.framework.TestCase {
36
+    
37
+    private static File tfile;
38
+
39
+    @Test
40
+    public void testGetLines() throws IOException {
41
+        final TextFile file =
42
+                new TextFile(getClass().getClassLoader().
43
+                getResource("com/dmdirc/util/test1.txt").openStream());
44
+        final List<String> lines = file.getLines();
45
+
46
+        assertEquals(7, lines.size());
47
+        assertEquals("Line 1", lines.get(0));
48
+    }
49
+    
50
+    @Test
51
+    public void testGetLines2() throws IOException {
52
+        final TextFile file =
53
+                new TextFile(getClass().getClassLoader().
54
+                getResource("com/dmdirc/util/test1.txt").openStream());
55
+        final List<String> lines = file.getLines();
34 56
 
57
+        assertEquals(7, lines.size());
58
+        assertEquals("Line 1", lines.get(0));
59
+    }
60
+    
35 61
     @Test
36
-    public void testGetLines() {
37
-        try {
38
-            final TextFile file =
39
-                    new TextFile(getClass().getClassLoader().
40
-                    getResource("com/dmdirc/util/test1.txt").toURI());
41
-            final List<String> lines = file.getLines();
42
-            
43
-            assertEquals(7, lines.size());
44
-            assertEquals("Line 1", lines.get(0));
45
-        } catch (FileNotFoundException ex) {
46
-            assertFalse(true);
47
-        } catch (IOException ex) {
48
-            assertFalse(true);
49
-        } catch (URISyntaxException ex) {
50
-            // Do nothing
51
-        }
62
+    public void testWrite() throws IOException {
63
+        tfile = File.createTempFile("dmdirc_unit_test", null);
64
+        TextFile file = new TextFile(tfile);
65
+        
66
+        final List<String> lines = Arrays.asList(new String[]{
67
+            "hello", "this is a test", "meep"
68
+        });
69
+        
70
+        file.writeLines(lines);
71
+        
72
+        file = new TextFile(tfile);
73
+        final List<String> newLines = file.getLines();
74
+        
75
+        assertEquals(lines, newLines);
52 76
     }
53 77
     
54 78
     @Test
55
-    public void testGetLines2() {
56
-        try {
57
-            final TextFile file =
58
-                    new TextFile(getClass().getClassLoader().
59
-                    getResource("com/dmdirc/util/test1.txt").openStream());
60
-            final List<String> lines = file.getLines();
61
-            
62
-            assertEquals(7, lines.size());
63
-            assertEquals("Line 1", lines.get(0));
64
-        } catch (FileNotFoundException ex) {
65
-            assertFalse(true);
66
-        } catch (IOException ex) {
67
-            assertFalse(true);
68
-        }
69
-    }    
79
+    public void testDelete() {
80
+        assertTrue(tfile.exists());
81
+        TextFile file = new TextFile(tfile);
82
+        file.delete();
83
+        assertFalse(tfile.exists());
84
+    }
70 85
 
71 86
 }

Loading…
Cancel
Save