瀏覽代碼

Fix unit test.

Fix hand written YAML being rubbish.
Fix bad reading caused by bad YAML.
Generify asList casting.
pull/137/head
Greg Holmes 9 年之前
父節點
當前提交
27429cc063

+ 3
- 3
src/com/dmdirc/config/profiles/YamlProfileStore.java 查看文件

@@ -50,6 +50,7 @@ import static com.dmdirc.util.YamlReaderUtils.asList;
50 50
 import static com.dmdirc.util.YamlReaderUtils.asMap;
51 51
 import static com.dmdirc.util.YamlReaderUtils.optionalString;
52 52
 import static com.dmdirc.util.YamlReaderUtils.requiredString;
53
+import static com.dmdirc.util.YamlReaderUtils.uncheckedCast;
53 54
 
54 55
 /**
55 56
  * Store that reads and writes profiles from a YAML file on disk.
@@ -118,8 +119,7 @@ public class YamlProfileStore implements ProfileStore {
118 119
                 final String name = requiredString(map, "name");
119 120
                 final String realname = requiredString(map, "realname");
120 121
                 final Optional<String> ident = Optional.ofNullable(optionalString(map, "ident"));
121
-                final List<String> nicknames = Splitter.on('\n')
122
-                        .splitToList(requiredString(map, "nicknames"));
122
+                final List<String> nicknames = uncheckedCast(asList(map.get("nicknames")));
123 123
                 res.add(new Profile(name, realname, ident, nicknames));
124 124
             } catch (IllegalArgumentException ex) {
125 125
                 LOG.info("Unable to read alias", ex);
@@ -144,7 +144,7 @@ public class YamlProfileStore implements ProfileStore {
144 144
             if (profile.getIdent().isPresent()) {
145 145
                 map.put("ident", profile.getIdent().get());
146 146
             }
147
-            map.put("nicknames", profile.getNicknames());
147
+            map.put("nicknames", profile.getNicknames().toArray());
148 148
             res.add(map);
149 149
         }
150 150
         return res;

+ 2
- 2
src/com/dmdirc/util/YamlReaderUtils.java 查看文件

@@ -63,8 +63,8 @@ public final class YamlReaderUtils {
63 63
      * @return A usable object list.
64 64
      */
65 65
     @SuppressWarnings("unchecked")
66
-    public static List<Object> uncheckedCast(final List<?> list) {
67
-        return (List<Object>) list;
66
+    public static <T> List<T> uncheckedCast(final List<?> list) {
67
+        return (List<T>) list;
68 68
     }
69 69
 
70 70
     /**

+ 9
- 4
test-res/com/dmdirc/config/profiles/profiles.yml 查看文件

@@ -1,10 +1,15 @@
1 1
 - name: profile1
2 2
   realname: realname1
3
-  nicknames: nickname1, nickname2
3
+  nicknames:
4
+    - nickname1
5
+    - nickname2
4 6
 - name: profile2
5 7
   realname: realname2
6
-  nicknames: nickname1, nickname3
8
+  nicknames:
9
+    - nickname1
10
+    - nickname3
7 11
   ident: ident2
8 12
 - name: profile3
9
-  realname: profile3
10
-  nicknames: nickname3
13
+  realname: realname3
14
+  nicknames:
15
+    - nickname3

+ 6
- 2
test/com/dmdirc/config/profiles/YamlProfileStoreTest.java 查看文件

@@ -27,6 +27,8 @@ import com.google.common.collect.Sets;
27 27
 import com.google.common.jimfs.Configuration;
28 28
 import com.google.common.jimfs.Jimfs;
29 29
 
30
+import java.io.IOException;
31
+import java.net.URISyntaxException;
30 32
 import java.nio.file.FileSystem;
31 33
 import java.nio.file.Files;
32 34
 import java.nio.file.Paths;
@@ -61,7 +63,7 @@ public class YamlProfileStoreTest {
61 63
     }
62 64
 
63 65
     @Test
64
-    public void testReadProfiles() throws Exception {
66
+    public void testReadProfiles() throws URISyntaxException {
65 67
         final ProfileStore store = new YamlProfileStore(Paths.get(getClass()
66 68
                 .getResource("profiles.yml").toURI()));
67 69
         final Collection<Profile> profiles = store.readProfiles();
@@ -73,12 +75,14 @@ public class YamlProfileStoreTest {
73 75
     }
74 76
 
75 77
     @Test
76
-    public void testWriteProfiles() throws Exception {
78
+    public void testWriteProfiles() throws IOException {
77 79
         try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
78 80
             final ProfileStore store = new YamlProfileStore(fs.getPath("/")
79 81
                     .resolve("profiles.yml"));
80 82
             final List<Profile> profiles = Lists.newArrayList(profile1, profile2, profile3);
81 83
             store.writeProfiles(profiles);
84
+            System.out.println(Files.readAllLines(fs.getPath("/")
85
+                    .resolve("profiles.yml")));
82 86
             final Collection<Profile> readProfiles = store.readProfiles();
83 87
             assertEquals(3, readProfiles.size());
84 88
             assertTrue(readProfiles.contains(profile1));

Loading…
取消
儲存