Browse Source

TextFile and ConfigFile can now be initialised with a URI

Added basic unit test for TextFile

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

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

@@ -24,6 +24,7 @@ package com.dmdirc.util;
24 24
 
25 25
 import java.io.FileNotFoundException;
26 26
 import java.io.IOException;
27
+import java.net.URI;
27 28
 import java.util.ArrayList;
28 29
 import java.util.GregorianCalendar;
29 30
 import java.util.HashMap;
@@ -58,6 +59,15 @@ public class ConfigFile {
58 59
     public ConfigFile(final String file) {
59 60
         this.file = new TextFile(file);
60 61
     }
62
+    
63
+    /**
64
+     * Creates a new instance of ConfigFile.
65
+     * 
66
+     * @param uri The URI of the file to be loaded
67
+     */
68
+    public ConfigFile(final URI uri) {
69
+        this.file = new TextFile(uri);
70
+    }
61 71
 
62 72
     /**
63 73
      * Reads the data from the file.

+ 10
- 0
src/com/dmdirc/util/TextFile.java View File

@@ -29,6 +29,7 @@ import java.io.FileNotFoundException;
29 29
 import java.io.FileReader;
30 30
 import java.io.FileWriter;
31 31
 import java.io.IOException;
32
+import java.net.URI;
32 33
 import java.util.ArrayList;
33 34
 import java.util.List;
34 35
 
@@ -51,6 +52,15 @@ public class TextFile {
51 52
         file = new File(filename);
52 53
     }
53 54
     
55
+    /**
56
+     * Creates a new instance of TextFile for the specified URI.
57
+     * 
58
+     * @param uri The URI of the file
59
+     */
60
+    public TextFile(final URI uri) {
61
+        file = new File(uri);
62
+    }
63
+    
54 64
     /**
55 65
      * Retrieves the contents of the file as a list of lines.
56 66
      * 

+ 54
- 0
test/com/dmdirc/util/TextFileTest.java View File

@@ -0,0 +1,54 @@
1
+/*
2
+ * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.util;
24
+
25
+import java.io.FileNotFoundException;
26
+import java.io.IOException;
27
+import java.net.URISyntaxException;
28
+import java.util.List;
29
+
30
+import org.junit.Test;
31
+import static org.junit.Assert.*;
32
+
33
+public class TextFileTest extends junit.framework.TestCase {
34
+
35
+    @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
+        }
52
+    }
53
+
54
+}

+ 7
- 0
test/com/dmdirc/util/test1.txt View File

@@ -0,0 +1,7 @@
1
+Line 1
2
+Line 2
3
+Line 3
4
+Line 4
5
+Line 5
6
+Line 6
7
+Line 7

+ 21
- 0
test/com/dmdirc/util/test2.txt View File

@@ -0,0 +1,21 @@
1
+# This is a DMDirc configuration file.
2
+# Written on: Mon Dec 17 23:32:19 GMT 2007
3
+
4
+# This section indicates which sections below take key/value
5
+# pairs, rather than a simple list. It should be placed above
6
+# any sections that take key/values.
7
+
8
+keysections:
9
+  section one
10
+
11
+section alpha:
12
+  line 1
13
+  line 2
14
+
15
+section one point one:
16
+  line 1
17
+
18
+section one:
19
+  1=one
20
+  2=two
21
+  3=three

Loading…
Cancel
Save