Преглед изворни кода

Improve tests, tidy up a little.

pull/137/head
Greg Holmes пре 9 година
родитељ
комит
5242fca351

+ 5
- 4
src/com/dmdirc/config/profiles/Profile.java Прегледај датотеку

@@ -46,7 +46,7 @@ public class Profile {
46 46
     private final List<String> nicknames;
47 47
 
48 48
     public Profile(final String name, final String realname, final Optional<String> ident,
49
-            final Iterable<String> nicknames) {
49
+            final List<String> nicknames) {
50 50
         this.name = name;
51 51
         this.realname = realname;
52 52
         this.ident = ident;
@@ -69,6 +69,7 @@ public class Profile {
69 69
         return Collections.unmodifiableList(nicknames);
70 70
     }
71 71
 
72
+    @Override
72 73
     public String toString() {
73 74
         return MoreObjects.toStringHelper(this)
74 75
                 .add("name", name)
@@ -89,12 +90,12 @@ public class Profile {
89 90
         final Profile profile = (Profile) o;
90 91
         return Objects.equals(name, profile.getName())
91 92
                 && Objects.equals(realname, profile.getRealname())
92
-                && Objects.equals(nicknames, profile.getNicknames())
93
-                && Objects.equals(ident, profile.getIdent());
93
+                && Objects.equals(ident, profile.getIdent())
94
+                && Objects.equals(nicknames, profile.getNicknames());
94 95
     }
95 96
 
96 97
     @Override
97 98
     public int hashCode() {
98
-        return Objects.hash(name, realname, ident, nicknames);
99
+        return Objects.hash(name, realname, ident);
99 100
     }
100 101
 }

+ 0
- 12
src/com/dmdirc/config/profiles/ProfileManager.java Прегледај датотеку

@@ -40,18 +40,6 @@ public class ProfileManager {
40 40
         profiles = new ArrayList<>();
41 41
     }
42 42
 
43
-    /**
44
-     * Starts this manager, registering listeners.
45
-     */
46
-    public void start() {
47
-    }
48
-
49
-    /**
50
-     * Stops this manager, unregistering listeners.
51
-     */
52
-    public void stop() {
53
-    }
54
-
55 43
     /**
56 44
      * Adds a profile to the manager.
57 45
      *

+ 6
- 3
src/com/dmdirc/config/profiles/ProfileManagerLifeCycleManager.java Прегледај датотеку

@@ -34,19 +34,22 @@ import javax.inject.Singleton;
34 34
 public class ProfileManagerLifeCycleManager implements SystemLifecycleComponent {
35 35
 
36 36
     private final ProfileManager profileManager;
37
+    private final ProfileStore profileStore;
37 38
 
38 39
     @Inject
39
-    public ProfileManagerLifeCycleManager(final ProfileManager profileManager) {
40
+    public ProfileManagerLifeCycleManager(final ProfileManager profileManager,
41
+            final ProfileStore profileStore) {
40 42
         this.profileManager = profileManager;
43
+        this.profileStore = profileStore;
41 44
     }
42 45
 
43 46
     @Override
44 47
     public void startUp() {
45
-        profileManager.start();
48
+        profileStore.readProfiles().forEach(profileManager::addProfile);
46 49
     }
47 50
 
48 51
     @Override
49 52
     public void shutDown() {
50
-        profileManager.stop();
53
+        profileStore.writeProfiles(profileManager.getProfiles());
51 54
     }
52 55
 }

+ 4
- 4
src/com/dmdirc/config/profiles/ProfileStore.java Прегледај датотеку

@@ -22,7 +22,7 @@
22 22
 
23 23
 package com.dmdirc.config.profiles;
24 24
 
25
-import java.util.Set;
25
+import java.util.Collection;
26 26
 
27 27
 /**
28 28
  * A store of {@link Profile}s that can read and write them to some kind of persistent media.
@@ -34,13 +34,13 @@ public interface ProfileStore {
34 34
      *
35 35
      * @return A set of known profiles, or an empty set if the store is uninitialised.
36 36
      */
37
-    Set<Profile> readProfiles();
37
+    Collection<Profile> readProfiles();
38 38
 
39 39
     /**
40 40
      * Writes all the given profiles to the store, replacing any existing commands.
41 41
      *
42
-     * @param profiles The set of profiles to be written to the store.
42
+     * @param profiles The collection of profiles to be written to the store.
43 43
      */
44
-    void writeProfiles(Set<Profile> profiles);
44
+    void writeProfiles(Collection<Profile> profiles);
45 45
 
46 46
 }

+ 14
- 0
src/com/dmdirc/config/profiles/ProfilesModule.java Прегледај датотеку

@@ -22,8 +22,15 @@
22 22
 
23 23
 package com.dmdirc.config.profiles;
24 24
 
25
+import com.dmdirc.commandline.CommandLineOptionsModule;
26
+import com.dmdirc.commandparser.auto.AutoCommandStore;
27
+import com.dmdirc.commandparser.auto.YamlAutoCommandStore;
25 28
 import com.dmdirc.interfaces.SystemLifecycleComponent;
26 29
 
30
+import java.nio.file.Path;
31
+
32
+import javax.inject.Singleton;
33
+
27 34
 import dagger.Module;
28 35
 import dagger.Provides;
29 36
 
@@ -33,6 +40,13 @@ import dagger.Provides;
33 40
 @Module(library = true, complete = false)
34 41
 public class ProfilesModule {
35 42
 
43
+    @Provides
44
+    @Singleton
45
+    public ProfileStore getProfileStore(
46
+            @CommandLineOptionsModule.Directory(CommandLineOptionsModule.DirectoryType.BASE) final Path directory) {
47
+        return new YamlProfileStore(directory.resolve("auto-commands.yml"));
48
+    }
49
+
36 50
     @Provides(type = Provides.Type.SET)
37 51
     public SystemLifecycleComponent getLifecycleComponent(
38 52
             final ProfileManagerLifeCycleManager manager) {

+ 8
- 9
src/com/dmdirc/config/profiles/YamlProfileStore.java Прегледај датотеку

@@ -73,25 +73,25 @@ public class YamlProfileStore implements ProfileStore {
73 73
     }
74 74
 
75 75
     @Override
76
-    public Set<Profile> readProfiles() {
77
-        final Set<Profile> commands = new HashSet<>();
76
+    public Collection<Profile> readProfiles() {
77
+        final Set<Profile> profiles = new HashSet<>();
78 78
         if (Files.exists(path)) {
79 79
             try (final InputStream stream = Files.newInputStream(path);
80 80
                     final InputStreamReader reader = new InputStreamReader(stream, CHARSET)) {
81 81
                 final YamlReader yamlReader = new YamlReader(reader);
82 82
                 final Object root = yamlReader.read();
83
-                commands.addAll(readProfilesFromYaml(asList(root)));
83
+                profiles.addAll(readProfilesFromYaml(asList(root)));
84 84
                 yamlReader.close();
85 85
             } catch (IOException | IllegalArgumentException ex) {
86 86
                 LOG.warn("Unable to read auto commands", ex);
87 87
             }
88 88
         }
89 89
 
90
-        return commands;
90
+        return profiles;
91 91
     }
92 92
 
93 93
     @Override
94
-    public void writeProfiles(final Set<Profile> profiles) {
94
+    public void writeProfiles(final Collection<Profile> profiles) {
95 95
         final List<Object> list = getProfilesForYaml(profiles);
96 96
         try (final OutputStream stream = Files.newOutputStream(path);
97 97
                 final OutputStreamWriter writer = new OutputStreamWriter(stream, CHARSET)) {
@@ -117,10 +117,9 @@ public class YamlProfileStore implements ProfileStore {
117 117
                 final Map<Object, Object> map = asMap(profile);
118 118
                 final String name = requiredString(map, "name");
119 119
                 final String realname = requiredString(map, "realname");
120
-                final Optional<String> ident =
121
-                        Optional.ofNullable(optionalString(map, "ident"));
122
-                final List<String> nicknames =
123
-                        Splitter.on('\n').splitToList(requiredString(map, "nicknames"));
120
+                final Optional<String> ident = Optional.ofNullable(optionalString(map, "ident"));
121
+                final List<String> nicknames = Splitter.on('\n')
122
+                        .splitToList(requiredString(map, "nicknames"));
124 123
                 res.add(new Profile(name, realname, ident, nicknames));
125 124
             } catch (IllegalArgumentException ex) {
126 125
                 LOG.info("Unable to read alias", ex);

+ 1
- 1
test-res/com/dmdirc/config/profiles/profiles.yml Прегледај датотеку

@@ -3,7 +3,7 @@
3 3
   nicknames: nickname1, nickname2
4 4
 - name: profile2
5 5
   realname: realname2
6
-  nicknames: nickname1, nickname1
6
+  nicknames: nickname1, nickname3
7 7
   ident: ident2
8 8
 - name: profile3
9 9
   realname: profile3

+ 26
- 5
test/com/dmdirc/config/profiles/YamlProfileStoreTest.java Прегледај датотеку

@@ -35,21 +35,41 @@ import java.util.List;
35 35
 import java.util.Optional;
36 36
 import java.util.Set;
37 37
 
38
+import org.junit.Before;
38 39
 import org.junit.Test;
39 40
 import org.junit.runner.RunWith;
40 41
 import org.mockito.runners.MockitoJUnitRunner;
41 42
 
42 43
 import static junit.framework.TestCase.assertEquals;
44
+import static junit.framework.TestCase.assertTrue;
43 45
 
44 46
 @RunWith(MockitoJUnitRunner.class)
45 47
 public class YamlProfileStoreTest {
46 48
 
49
+    private Profile profile1;
50
+    private Profile profile2;
51
+    private Profile profile3;
52
+
53
+    @Before
54
+    public void setup() {
55
+        profile1 = new Profile("profile1", "realname1", Optional.empty(),
56
+                Lists.newArrayList("nickname1", "nickname2"));
57
+        profile2 = new Profile("profile2", "realname2", Optional.of("ident2"),
58
+                Lists.newArrayList("nickname1", "nickname3"));
59
+        profile3 = new Profile("profile3", "realname3", Optional.empty(),
60
+                Lists.newArrayList("nickname3"));
61
+    }
62
+
47 63
     @Test
48 64
     public void testReadProfiles() throws Exception {
49 65
         final ProfileStore store = new YamlProfileStore(Paths.get(getClass()
50 66
                 .getResource("profiles.yml").toURI()));
51 67
         final Collection<Profile> profiles = store.readProfiles();
52 68
         assertEquals(3, profiles.size());
69
+        profiles.forEach(System.out::println);
70
+        assertTrue(profiles.contains(profile1));
71
+        assertTrue(profiles.contains(profile2));
72
+        assertTrue(profiles.contains(profile3));
53 73
     }
54 74
 
55 75
     @Test
@@ -57,12 +77,13 @@ public class YamlProfileStoreTest {
57 77
         try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
58 78
             final ProfileStore store = new YamlProfileStore(fs.getPath("/")
59 79
                     .resolve("profiles.yml"));
60
-            final Profile profile1 = new Profile("name", "realname", Optional.of("ident"),
61
-                    Lists.newArrayList("nickname"));
62
-            final Set<Profile> profiles = Sets.newHashSet(profile1);
80
+            final List<Profile> profiles = Lists.newArrayList(profile1, profile2, profile3);
63 81
             store.writeProfiles(profiles);
64
-            final List<String> lines = Files.readAllLines(fs.getPath("/").resolve("profiles.yml"));
65
-            assertEquals(5, lines.size());
82
+            final Collection<Profile> readProfiles = store.readProfiles();
83
+            assertEquals(3, readProfiles.size());
84
+            assertTrue(readProfiles.contains(profile1));
85
+            assertTrue(readProfiles.contains(profile2));
86
+            assertTrue(readProfiles.contains(profile3));
66 87
         }
67 88
     }
68 89
 }

Loading…
Откажи
Сачувај